<?php

/* For licensing terms, see /license.txt */
/**
 *
 * @package chamilo.include.search
 */
/**
 * Code
 */
include_once dirname(__FILE__) . '/../../../global.inc.php';
require_once dirname(__FILE__) . '/search_processor.class.php';

/**
 * Process documents before pass it to search listing scripts
 * @package chamilo.include.search
 */
class document_processor extends search_processor {

    function document_processor($rows) {
        $this->rows = $rows;
    }

    public function process() {
        $results = array();
        foreach ($this->rows as $row_val) {
            $search_show_unlinked_results = (api_get_setting('search_show_unlinked_results') == 'true');
            $course_visible_for_user = api_is_course_visible_for_user(NULL, $row_val['courseid']);
            // can view course?
            if ($course_visible_for_user || $search_show_unlinked_results) {
                // is visible?
                $visibility = api_get_item_visibility(api_get_course_info($row_val['courseid']), TOOL_DOCUMENT, $row_val['xapian_data'][SE_DATA]['doc_id']);
                if ($visibility) {
                    list($thumbnail, $image, $name, $author, $url) = $this->get_information($row_val['courseid'], $row_val['xapian_data'][SE_DATA]['doc_id']);
                    $result = array(
                        'toolid' => TOOL_DOCUMENT,
                        'score' => $row_val['score'],
                        'url' => $url,
                        'thumbnail' => $thumbnail,
                        'image' => $image,
                        'title' => $name,
                        'author' => $author,
                    );
                    if ($course_visible_for_user) {
                        $results[] = $result;
                    } else { // course not visible for user
                        if ($search_show_unlinked_results) {
                            $result['url'] = '';
                            $results[] = $result;
                        }
                    }
                }
            }
        }

        // get information to sort
        foreach ($results as $key => $row) {
            $score[$key] = $row['score'];
        }

        // Sort results with score descending
        array_multisort($score, SORT_DESC, $results);

        return $results;
    }

    /**
     * Get document information
     */
    private function get_information($course_id, $doc_id) {
        $course_information = api_get_course_info($course_id);
        $course_id = $course_information['real_id'];
        $course_path = $course_information['path'];
        if (!empty($course_information)) {
            $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
            $doc_table = Database::get_course_table(TABLE_DOCUMENT);

            $doc_id = Database::escape_string($doc_id);
            $sql = "SELECT * FROM       $doc_table
                    WHERE      $doc_table.id = $doc_id AND c_id = $course_id 
                    LIMIT 1";
            $dk_result = Database::query($sql);

            $sql = "SELECT insert_user_id FROM       $item_property_table
                    WHERE   ref = $doc_id AND tool = '" . TOOL_DOCUMENT . "' AND c_id = $course_id 
                    LIMIT 1";
            $name = '';
            if ($row = Database::fetch_array($dk_result)) {
                $name = $row['title'];
                $url = api_get_path(WEB_PATH) . 'courses/%s/document%s';
                $url = sprintf($url, $course_path, $row['path']);
                // Get the image path
                include_once api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php';
                $icon = choose_image(basename($row['path']));
                $thumbnail = api_get_path(WEB_CODE_PATH) . 'img/' . $icon;
                $image = $thumbnail;
                //FIXME: use big images
                // get author
                $author = '';
                $item_result = Database::query($sql);
                if ($row = Database::fetch_array($item_result)) {
                    $user_data = api_get_user_info($row['insert_user_id']);
                    $author = api_get_person_name($user_data['firstName'], $user_data['lastName']);
                }
            }
            return array($thumbnail, $image, $name, $author, $url); // FIXME: is it posible to get an author here?
        } else {
            return array();
        }
    }

}
