示例#1
0
/**
* returns a single data search document based on a data entry id
* @uses $DB
* @param in $id the id of the record
* @param string $itemtype the type of the information
* @return a single searchable document
*/
function data_single_document($id, $itemtype)
{
    global $DB;
    if ($itemtype == 'record') {
        // get main record
        $recordMetaData = $DB->get_record('data_records', array('id' => $id));
        // get context
        $record_course = $DB->get_field('data', 'course', array('id' => $recordMetaData->dataid));
        $coursemodule = $DB->get_field('modules', 'id', array('name' => 'data'));
        $cm = $DB->get_record('course_modules', array('course' => $record_course, 'module' => $coursemodule, 'instance' => $recordMetaData->dataid));
        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
        // compute text
        $recordData = data_get_records($recordMetaData->dataid, 'text,textarea', $id);
        if ($recordData) {
            $dataArray = array_values($recordData);
            $record_content = $dataArray[0];
            // We cannot have more than one record here
            // extract title as first record in order
            $first = $record_content['_first'];
            unset($record_content['_first']);
            // concatenates all other texts
            $content = '';
            foreach ($record_content as $aField) {
                $content = @$content . ' ' . $aField;
            }
            unset($recordMetaData);
            $recordMetaData = $DB->get_record('data_records', array('id' => $id));
            $recordMetaData->title = $first;
            $recordMetaData->content = $content;
            return new DataSearchDocument(get_object_vars($recordMetaData), $record_course, $context->id);
        }
    } elseif ($itemtype == 'comment') {
        // get main records
        $comment = $DB->get_record('data_comments', array('id' => $id));
        $record = $DB->get_record('data_records', array('id' => $comment->recordid));
        // get context
        $record_course = $DB->get_field('data', 'course', array('id' => $record->dataid));
        $coursemodule = $DB->get_field('modules', 'id', array('name' => 'data'));
        $cm = $DB->get_record('course_modules', array('course' => $record_course, 'module' => $coursemodule, 'instance' => $recordMetaData->dataid));
        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
        // add extra fields
        $comment->title = $DB->get_field('search_document', 'title', array('docid' => $record->id, 'itemtype' => 'record'));
        $comment->dataid = $record->dataid;
        $comment->groupid = $record->groupid;
        $authoruser = $DB->get_record('user', array('id' => $comment->userid));
        $comment->author = fullname($authoruser);
        // make document
        return new DataCommentSearchDocument(get_object_vars($comment), $record_course, $context->id);
    } else {
        mtrace('Error : bad or missing item type');
        return NULL;
    }
}
/**
* part of search engine API
* @param database the database instance
* @return an array of searchable documents
*/
function data_get_content_for_index(&$database)
{
    $documents = array();
    $recordTitles = array();
    $coursemodule = get_field('modules', 'id', 'name', 'data');
    $cm = get_record('course_modules', 'course', $database->course, 'module', $coursemodule, 'instance', $database->id);
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    // getting records for indexing
    $records_content = data_get_records($database->id, 'text');
    if ($records_content) {
        foreach (array_keys($records_content) as $aRecordId) {
            // extract title as first record in order
            $first = $records_content[$aRecordId]['_first'];
            unset($records_content[$aRecordId]['_first']);
            // concatenates all other texts
            foreach ($records_content[$aRecordId] as $aField) {
                $content = @$content . ' ' . $aField;
            }
            if (strlen($content) > 0) {
                unset($recordMetaData);
                $recordMetaData = get_record('data_records', 'id', $aRecordId);
                $recordMetaData->title = $first;
                $recordTitles[$aRecordId] = $first;
                $recordMetaData->content = $content;
                $documents[] = new DataSearchDocument(get_object_vars($recordMetaData), $database->course, $context->id);
            }
        }
    }
    // getting comments for indexing
    $records_comments = data_get_comments($database->id);
    if ($records_comments) {
        foreach ($records_comments as $aComment) {
            $aComment->title = $recordsTitle[$aComment->recordid];
            $documents[] = new DataCommentSearchDocument(get_object_vars($aComment), $database->course, $context->id);
        }
    }
    return $documents;
}