Addresses MDL-18638 (Search code does not follow coding guidelines) and MDL-18474 (global search should use of PATH_SEPARATOR)

This commit is contained in:
diml 2009-03-23 21:35:30 +00:00
parent cbc5cd4140
commit 8fe956133f
20 changed files with 790 additions and 350 deletions

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for assignment activity module
*
@ -16,8 +17,8 @@
/**
* includes and requires
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once("$CFG->dirroot/mod/assignment/lib.php");
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/assignment/lib.php');
/**
* a class for representing searchable information
@ -71,24 +72,30 @@ function assignment_make_link($cm_id, $itemtype, $owner) {
/**
* part of search engine API
* @uses $DB
*
*/
function assignment_iterator() {
$assignments = get_records('assignment');
global $DB;
if ($assignments = $DB->get_records('assignment'))
return $assignments;
else
return array();
}
/**
* part of search engine API
* @uses $CFG, $DB
*
*/
function assignment_get_content_for_index(&$assignment) {
global $CFG;
global $CFG, $DB;
$documents = array();
$course = get_record('course', 'id', $assignment->course);
$coursemodule = get_field('modules', 'id', 'name', 'assignment');
$cm = get_record('course_modules', 'course', $assignment->course, 'module', $coursemodule, 'instance', $assignment->id);
$course = $DB->get_record('course', array('id' => $assignment->course));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'assignment'));
$cm = $DB->get_record('course_modules', array('course' => $assignment->course, 'module' => $coursemodule, 'instance' => $assignment->id));
if ($cm){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
@ -99,7 +106,7 @@ function assignment_get_content_for_index(&$assignment) {
$submissions = assignment_get_all_submissions($assignment);
if ($submissions){
foreach($submissions as $submission){
$owner = get_record('user', 'id', $submission->userid);
$owner = $DB->get_record('user', array('id' => $submission->userid));
$submission->authors = fullname($owner);
$submission->assignmenttype = $assignment->assignmenttype;
$submission->date = $submission->timemodified;
@ -154,16 +161,17 @@ function assignment_get_content_for_index(&$assignment) {
/**
* get text from a physical file in an assignment submission
* @uses $CFG, $DB
* @param object $submission a submission for which to fetch some representative text
* @param object $assignment the relevant assignment as a context
* @param object $cm the corresponding coursemodule
* @param string $path a file from which to fetch some representative text
* @param int $contextid the moodle context if needed
* @param documents the array of documents, by ref, where to add the new document.
* @param array $documents the array of documents, by ref, where to add the new document.
* @return a search document when unique or false.
*/
function assignment_get_physical_file(&$submission, &$assignment, &$cm, $path, $context_id, &$documents = null){
global $CFG;
global $CFG, $DB;
$fileparts = pathinfo($path);
// cannot index unknown or masked types
@ -185,7 +193,7 @@ function assignment_get_physical_file(&$submission, &$assignment, &$cm, $path, $
$submission->description = $function_name(null, $path);
// get authors
$user = get_record('user', 'id', $submission->userid);
$user = $DB->get_record('user', array('id' => $submission->userid));
$submission->authors = fullname($user);
// we need a real id on file
@ -209,24 +217,27 @@ function assignment_get_physical_file(&$submission, &$assignment, &$cm, $path, $
/**
* returns a single data search document based on an assignment
* @uses $DB
* @param string $id the id of the searchable item
* @param string $itemtype the type of information
*/
function assignment_single_document($id, $itemtype) {
global $DB;
if ($itemtype == 'requirement'){
if (!$assignment = get_record('assignment', 'id', $id)){
if (!$assignment = $DB->get_record('assignment', 'id', $id)){
return null;
}
} elseif ($itemtype == 'submission') {
if ($submission = get_record('assignment_submissions', 'id', $id)){
$assignment = get_record('assignment', 'id', $submission->assignment);
if ($submission = $DB->get_record('assignment_submissions', array('id' => $id))){
$assignment = $DB->get_record('assignment', array('id' => $submission->assignment));
} else {
return null;
}
}
$course = get_record('course', 'id', $assignment->course);
$coursemodule = get_field('modules', 'id', 'name', 'assignment');
$cm = get_record('course_modules', 'course', $course->id, 'module', $coursemodule, 'instance', $assignment->id);
$course = $DB->get_record('course', array('id' => $assignment->course));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'assignment'));
$cm = $DB->get_record('course_modules', array('course' => $course->id, 'module' => $coursemodule, 'instance' => $assignment->id));
if ($cm){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
@ -273,29 +284,29 @@ function assignment_db_names() {
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by entry_type. In chats, this id
* @uses $CFG, $USER, $DB
* @param string $path the access path to the module script code
* @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param int $this_id the item id within the information class denoted by entry_type. In chats, this id
* points out a session history which is a close sequence of messages.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG
* @param int $user the user record denoting the user who searches
* @param int $group_id the current group used by the user when searching
* @return true if access is allowed, false elsewhere
*/
function assignment_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG, $USER;
global $CFG, $USER, $DB;
include_once("{$CFG->dirroot}/{$path}/lib.php");
// get the chat session and all related stuff
if ($itemtype == 'description'){
$assignment = get_record('assignment', 'id', $this_id);
$assignment = $DB->get_record('assignment', array('id' => $this_id));
} elseif ($itemtype == 'submitted'){
$submission = get_record('assignment_submissions', 'id', $this_id);
$assignment = get_record('assignment', 'id', $submission->assignment);
$submission = $DB->get_record('assignment_submissions', array('id' => $this_id));
$assignment = $DB->get_record('assignment', array('id' => $submission->assignment));
}
$context = get_record('context', 'id', $context_id);
$cm = get_record('course_modules', 'id', $context->instanceid);
$context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){
if (!empty($CFG->search_access_debug)) echo "search reject : hidden assignment ";
@ -342,6 +353,7 @@ function assignment_check_text_access($path, $itemtype, $this_id, $user, $group_
*
*/
function assignment_link_post_processing($title){
global $CFG;
if (!function_exists('search_assignment_getstring')){
function search_assignment_getstring($matches){
@ -350,6 +362,10 @@ function assignment_link_post_processing($title){
}
$title = preg_replace_callback('/^(description|submitted)/', 'search_assignment_getstring', $title);
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}
?>

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for chat activity module
* This file contains the mapping between a chat history and it's indexable counterpart,
@ -20,8 +21,8 @@
/**
* includes and requires
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once("$CFG->dirroot/mod/chat/lib.php");
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/chat/lib.php');
/**
* a class for representing searchable information
@ -61,9 +62,9 @@ class ChatTrackSearchDocument extends SearchDocument {
/**
* constructs a valid link to a chat content
* @param cm_id the chat course module
* @param start the start time of the session
* @param end th end time of the session
* @uses CFG
* @param int $start the start time of the session
* @param int $end th end time of the session
* @uses $CFG
* @return a well formed link to session display
*/
function chat_make_link($cm_id, $start, $end) {
@ -79,22 +80,22 @@ function chat_make_link($cm_id, $start, $end) {
* @param int $chat_id the database
* @param int $fromtime
* @param int $totime
* @uses CFG
* @uses $CFG, $DB
* @return an array of objects representing the chat sessions.
*/
function chat_get_session_tracks($chat_id, $fromtime = 0, $totime = 0) {
global $CFG;
global $CFG, $DB;
$chat = get_record('chat', 'id', $chat_id);
$course = get_record('course', 'id', $chat->course);
$coursemodule = get_field('modules', 'id', 'name', 'data');
$cm = get_record('course_modules', 'course', $course->id, 'module', $coursemodule, 'instance', $chat->id);
$chat = $DB->get_record('chat', array('id' => $chat_id));
$course = $DB->get_record('course', array('id' => $chat->course));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'data'));
$cm = $DB->get_record('course_modules', array('course' => $course->id, 'module' => $coursemodule, 'instance' => $chat->id));
$groupmode = groupmode($course, $cm);
$fromtimeclause = ($fromtime) ? "AND timestamp >= {$fromtime}" : '';
$totimeclause = ($totime) ? "AND timestamp <= {$totime}" : '';
$tracks = array();
$messages = get_records_select('chat_messages', "chatid = '{$chat_id}' $fromtimeclause $totimeclause", "timestamp DESC");
$messages = $DB->get_records_select('chat_messages', "chatid = ':chatid' :from :to", array('chatid' => $chat_id, 'from' => $fromtimeclause, 'to' => $totimeclause), 'timestamp DESC');
if ($messages){
// splits discussions against groups
$groupedMessages = array();
@ -150,22 +151,29 @@ function chat_get_session_tracks($chat_id, $fromtime = 0, $totime = 0) {
/**
* part of search engine API
* @uses $DB
*
*/
function chat_iterator() {
$chatrooms = get_records('chat');
global $DB;
$chatrooms = $DB->get_records('chat');
return $chatrooms;
}
/**
* part of search engine API
* @uses $DB
* @param reference $chat
*
*/
function chat_get_content_for_index(&$chat) {
global $DB;
$documents = array();
$course = get_record('course', 'id', $chat->course);
$coursemodule = get_field('modules', 'id', 'name', 'chat');
$cm = get_record('course_modules', 'course', $chat->course, 'module', $coursemodule, 'instance', $chat->id);
$course = $DB->get_record('course', array('id' => $chat->course));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'chat'));
$cm = $DB->get_record('course_modules', array('course' => $chat->course, 'module' => $coursemodule, 'instance' => $chat->id));
if ($cm){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
@ -174,7 +182,7 @@ function chat_get_content_for_index(&$chat) {
if ($sessionTracks){
foreach($sessionTracks as $aTrackId => $aTrack) {
foreach($aTrack->sessionusers as $aUserId){
$user = get_record('user', 'id', $aUserId);
$user = $DB->get_record('user', array('id' => $aUserId));
$aTrack->authors = ($user) ? fullname($user) : '' ;
$documents[] = new ChatTrackSearchDocument(get_object_vars($aTrack), $chat->id, $cm->id, $chat->course, $aTrack->groupid, $context->id);
}
@ -191,15 +199,18 @@ function chat_get_content_for_index(&$chat) {
* - the chat id
* - the timestamp when the session starts
* - the timestamp when the session ends
* @uses $DB
* @param id the multipart chat session id
* @param itemtype the type of information (session is the only type)
*/
function chat_single_document($id, $itemtype) {
global $DB;
list($chat_id, $sessionstart, $sessionend) = split('-', $id);
$chat = get_record('chat', 'id', $chat_id);
$course = get_record('course', 'id', $chat->course);
$coursemodule = get_field('modules', 'id', 'name', 'chat');
$cm = get_record('course_modules', 'course', $course->id, 'module', $coursemodule, 'instance', $chat->id);
$chat = $DB->get_record('chat', array('id' => $chat_id));
$course = $DB->get_record('course', array('id' => $chat->course));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'chat'));
$cm = $DB->get_record('course_modules', array('course' => $course->id, 'module' => $coursemodule, 'instance' => $chat->id));
if ($cm){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
@ -241,27 +252,25 @@ function chat_db_names() {
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by entry_type. In chats, this id
* @param string $path the access path to the module script code
* @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param int $this_id the item id within the information class denoted by entry_type. In chats, this id
* points out a session history which is a close sequence of messages.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG
* @param int $user the user record denoting the user who searches
* @param int $group_id the current group used by the user when searching
* @uses $CFG, $DB
* @return true if access is allowed, false elsewhere
*/
function chat_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG;
global $CFG, $DB;
include_once("{$CFG->dirroot}/{$path}/lib.php");
list($chat_id, $sessionstart, $sessionend) = split('-', $this_id);
// get the chat session and all related stuff
$chat = get_record('chat', 'id', $chat_id);
$context = get_record('context', 'id', $context_id);
$cm = get_record('course_modules', 'id', $context->instanceid);
// $cm = get_coursemodule_from_instance('chat', $chat->id, $chat->course);
// $context = get_context_instance(CONTEXT_MODULE, $cm->id);
$chat = $DB->get_record('chat', array('id' => $chat_id));
$context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){
if (!empty($CFG->search_access_debug)) echo "search reject : hidden chat ";
@ -270,7 +279,7 @@ function chat_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
//group consistency check : checks the following situations about groups
// trap if user is not same group and groups are separated
$course = get_record('course', 'id', $chat->course);
$course = $DB->get_record('course', array('id' => $chat->course));
if ((groupmode($course, $cm) == SEPARATEGROUPS) && !ismember($group_id) && !has_capability('moodle/site:accessallgroups', $context)){
if (!empty($CFG->search_access_debug)) echo "search reject : chat element is in separated group ";
return false;
@ -289,11 +298,18 @@ function chat_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
/**
* this call back is called when displaying the link for some last post processing
* @uses $CFG
* @param string $title
*
*/
function chat_link_post_processing($title){
global $CFG;
setLocale(LC_TIME, substr(current_language(), 0, 2));
$title = preg_replace('/TT_(.*)_TT/e', "userdate(\\1)", $title);
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}
?>

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for data activity module
* This file contains the mapping between a database object and it's indexable counterpart,
@ -20,8 +21,8 @@
/**
* includes and requires
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once("$CFG->dirroot/mod/data/lib.php");
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/data/lib.php');
/**
* a class for representing searchable information (data records)
@ -33,6 +34,8 @@ class DataSearchDocument extends SearchDocument {
* constructor
*/
public function __construct(&$record, $course_id, $context_id) {
global $DB;
// generic information; required
$doc->docid = $record['id'];
$doc->documenttype = SEARCH_TYPE_DATA;
@ -43,7 +46,7 @@ class DataSearchDocument extends SearchDocument {
$doc->date = $record['timemodified'];
//remove '(ip.ip.ip.ip)' from data record author field
if ($record['userid']){
$user = get_record('user', 'id', $record['userid']);
$user = $DB->get_record('user', array('id' => $record['userid']));
}
$doc->author = (isset($user)) ? $user->firstname.' '.$user->lastname : '' ;
$doc->contents = $record['content'];
@ -91,9 +94,9 @@ class DataCommentSearchDocument extends SearchDocument {
/**
* constructs a valid link to a data record content
* @param database_id the database reference
* @param record_id the record reference
* @uses CFG
* @param int $database_id the database reference
* @param int $record_id the record reference
* @uses $CFG
* @return a valid url top access the information as a string
*/
function data_make_link($database_id, $record_id) {
@ -104,28 +107,28 @@ function data_make_link($database_id, $record_id) {
/**
* fetches all the records for a given database
* @param database_id the database
* @param typematch a comma separated list of types that should be considered for searching or *
* @uses CFG
* @param int $database_id the database
* @param string $typematch a comma separated list of types that should be considered for searching or *
* @uses $CFG, $DB
* @return an array of objects representing the data records.
*/
function data_get_records($database_id, $typematch = '*') {
global $CFG;
global $CFG, $DB;
$fieldset = get_records('data_fields', 'dataid', $database_id);
$fieldset = $DB->get_records('data_fields', array('dataid' => $database_id));
$query = "
SELECT
c.*
FROM
{data_content} as c,
{data_records} as r
{data_content} c,
{data_records} r
WHERE
c.recordid = r.id AND
r.dataid = {$database_id}
r.dataid = ?
ORDER BY
c.fieldid
";
$data = get_records_sql($query);
$data = $DB->get_records_sql($query, array($database_id));
$records = array();
if ($data){
foreach($data as $aDatum){
@ -143,12 +146,12 @@ function data_get_records($database_id, $typematch = '*') {
/**
* fetches all the comments for a given database
* @param database_id the database
* @uses CFG
* @param int $database_id the database
* @uses $CFG, $DB
* @return an array of objects representing the data record comments.
*/
function data_get_comments($database_id) {
global $CFG;
global $CFG, $DB;
$query = "
SELECT
@ -161,56 +164,63 @@ function data_get_comments($database_id) {
c.modified,
r.dataid
FROM
{data_comments} as c,
{data_records} as r
{data_comments} c,
{data_records} r
WHERE
c.recordid = r.id
c.recordid = r.id AND
r.dataid = ?
";
$comments = get_records_sql($query);
$comments = $DB->get_records_sql($query, array($database_id));
return $comments;
}
/**
* part of search engine API
* @uses $DB
*
*/
function data_iterator() {
$databases = get_records('data');
global $DB;
$databases = $DB->get_records('data');
return $databases;
}
/**
* part of search engine API
* @param database the database instance
* @uses $DB
* @param reference $database the database instance
* @return an array of searchable documents
*/
function data_get_content_for_index(&$database) {
global $DB;
$documents = array();
$recordTitles = array();
$coursemodule = get_field('modules', 'id', 'name', 'data');
$cm = get_record('course_modules', 'course', $database->course, 'module', $coursemodule, 'instance', $database->id);
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'data'));
$cm = $DB->get_record('course_modules', array('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) {
foreach(array_keys($records_content) as $arecordid) {
// extract title as first record in order
$first = $records_content[$aRecordId]['_first'];
unset($records_content[$aRecordId]['_first']);
$first = $records_content[$arecordid]['_first'];
unset($records_content[$arecordid]['_first']);
// concatenates all other texts
foreach($records_content[$aRecordId] as $aField){
$content = @$content.' '.$aField;
$content = '';
foreach($records_content[$arecordid] as $afield){
$content = @$content.' '.$afield;
}
if (strlen($content) > 0) {
unset($recordMetaData);
$recordMetaData = get_record('data_records', 'id', $aRecordId);
$recordMetaData = $DB->get_record('data_records', array('id' => $arecordid));
$recordMetaData->title = $first;
$recordTitles[$aRecordId] = $first;
$recordTitles[$arecordid] = $first;
$recordMetaData->content = $content;
$documents[] = new DataSearchDocument(get_object_vars($recordMetaData), $database->course, $context->id);
}
@ -230,22 +240,24 @@ function data_get_content_for_index(&$database) {
/**
* returns a single data search document based on a data entry id
* @param id the id of the record
* @param the type of the information
* @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 = get_record('data_records', 'id', $id);
$recordMetaData = $DB->get_record('data_records', array('id' => $id));
// get context
$record_course = get_field('data', 'course', 'id', $recordMetaData->dataid);
$coursemodule = get_field('modules', 'id', 'name', 'data');
$cm = get_record('course_modules', 'course', $record_course, 'module', $coursemodule, 'instance', $recordMetaData->dataid);
$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 = get_records_select('data_content', "recordid = $id AND type = 'text'", 'recordid');
$recordData = $DB->get_records_select('data_content', "recordid = ? AND type = 'text'", array($id), 'recordid');
$accumulator = '';
if ($recordData){
$first = $recordData[0];
@ -263,15 +275,15 @@ function data_single_document($id, $itemtype) {
$documents[] = new DataSearchDocument(get_object_vars($recordMetaData), $record_course, $context->id);
} elseif($itemtype == 'comment') {
// get main records
$comment = get_record('data_comments', 'id', $id);
$record = get_record('data_records', 'id', $comment->recordid);
$comment = $DB->get_record('data_comments', array('id' => $id));
$record = $DB->get_record('data_records', array('id' => $comment->recordid));
// get context
$record_course = get_field('data', 'course', 'id', $record->dataid);
$coursemodule = get_field('modules', 'id', 'name', 'data');
$cm = get_record('course_modules', 'course', $record_course, 'module', $coursemodule, 'instance', $recordMetaData->dataid);
$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 = get_field('search_document', 'title', 'docid', $record->id, 'itemtype', 'record');
$comment->title = $DB->get_field('search_document', 'title', array('docid' => $record->id, 'itemtype' => 'record'));
$comment->dataid = $record->dataid;
$comment->groupid = $record->groupid;
// make document
@ -311,44 +323,42 @@ function data_db_names() {
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by itemtype. In databases, this id
* @param string $path the access path to the module script code
* @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param int $this_id the item id within the information class denoted by itemtype. In databases, this id
* points out an indexed data record page.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG
* @param object $user the user record denoting the user who searches
* @param int $group_id the current group used by the user when searching
* @uses $CFG, $DB
* @return true if access is allowed, false elsewhere
*/
function data_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG;
global $CFG, $DB;
// get the database object and all related stuff
if ($itemtype == 'record'){
$record = get_record('data_records', 'id', $this_id);
$record = $DB->get_record('data_records', array('id' => $this_id));
}
elseif($itemtype == 'comment'){
$comment = get_record('data_comments', 'id', $this_id);
$record = get_record('data_records', 'id', $comment->recordid);
$comment = $DB->get_record('data_comments', array('id' => $this_id));
$record = $DB->get_record('data_records', array('id' => $comment->recordid));
}
else{
// we do not know what type of information is required
return false;
}
$data = get_record('data', 'id', $record->dataid);
$context = get_record('context', 'id', $context_id);
$cm = get_record('course_modules', 'id', $context->instanceid);
// $cm = get_coursemodule_from_instance('data', $data->id, $data->course);
// $context = get_context_instance(CONTEXT_MODULE, $cm->id);
$data = $DB->get_record('data', array('id' => $record->dataid));
$context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $context)) {
if (!empty($CFG->search_access_debug)) echo "search reject : hidden database ";
return false;
}
//group consistency check : checks the following situations about groups
// trap if user is not same group and groups are separated
$course = get_record('course', 'id', $data->course);
$course = $DB->get_record('course', 'id', $data->course);
if ((groupmode($course, $cm) == SEPARATEGROUPS) && !ismember($group_id) && !has_capability('moodle/site:accessallgroups', $context)){
if (!empty($CFG->search_access_debug)) echo "search reject : separated group owned resource ";
return false;
@ -366,7 +376,7 @@ function data_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
//approval check
// trap if unapproved and has not approval capabilities
// TODO : report a potential capability lack of : mod/data:approve
$approval = get_field('data_records', 'approved', 'id', $record->id);
$approval = $DB->get_field('data_records', 'approved', array('id' => $record->id));
if (!$approval && !has_capability('mod/data:manageentries', $context)){
if (!empty($CFG->search_access_debug)) echo "search reject : unapproved resource ";
return false;
@ -375,7 +385,7 @@ function data_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
//minimum records to view check
// trap if too few records
// TODO : report a potential capability lack of : mod/data:viewhiddenentries
$recordsAmount = count_records('data_records', 'dataid', $data->id);
$recordsAmount = $DB->count_records('data_records', array('dataid' => $data->id));
if ($data->requiredentriestoview > $recordsAmount && !has_capability('mod/data:manageentries', $context)) {
if (!empty($CFG->search_access_debug)) echo "search reject : not enough records to view ";
return false;
@ -404,7 +414,12 @@ function data_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
* @param string $title
*/
function data_link_post_processing($title){
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}
?>

View file

@ -17,7 +17,7 @@
*
*/
abstract class SearchDocument extends Zend_Search_Lucene_Document {
public function __construct(&$doc, &$data, $course_id, $group_id, $user_id, $path) {
public function __construct(&$doc, &$data, $course_id, $group_id, $user_id, $path, $additional_keyset = null) {
//document identification and indexing
$this->addField(Zend_Search_Lucene_Field::Keyword('docid', $doc->docid));
//document type : the name of the Moodle element that manages it
@ -59,6 +59,16 @@ abstract class SearchDocument extends Zend_Search_Lucene_Document {
// of multiple capabilities in their code. This possibility should be left open here.
$this->addField(Zend_Search_Lucene_Field::UnIndexed('capabilities', $caps));
*/
/*
// Additional key set allows a module to ask for extensible criteria based search
// depending on the module internal needs.
*/
if (!empty($additional_keyset)){
foreach($additional_keyset as $keyname => $keyvalue){
$this->addField(Zend_Search_Lucene_Field::Keyword($keyname, $keyvalue));
}
}
}
}

View file

@ -8,6 +8,7 @@
* @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for forum activity module
* This file contains the mapping between a forum post and it's indexable counterpart,
@ -20,8 +21,8 @@
/**
* includes and requires
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once("$CFG->dirroot/mod/forum/lib.php");
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/forum/lib.php');
/**
* a class for representing searchable information
@ -31,8 +32,11 @@ class ForumSearchDocument extends SearchDocument {
/**
* constructor
* @uses $DB;
*/
public function __construct(&$post, $forum_id, $course_id, $itemtype, $context_id) {
global $DB;
// generic information
$doc->docid = $post['id'];
$doc->documenttype = SEARCH_TYPE_FORUM;
@ -41,7 +45,7 @@ class ForumSearchDocument extends SearchDocument {
$doc->title = $post['subject'];
$user = get_record('user', 'id', $post['userid']);
$user = $DB->get_record('user', array('id' => $post['userid']));
$doc->author = fullname($user);
$doc->contents = $post['message'];
$doc->date = $post['created'];
@ -57,8 +61,9 @@ class ForumSearchDocument extends SearchDocument {
/**
* constructs a valid link to a chat content
* @param discussion_id the discussion
* @param post_id the id of a single post
* @uses $CFG
* @param int $discussion_id the discussion
* @param int $post_id the id of a single post
* @return a well formed link to forum message display
*/
function forum_make_link($discussion_id, $post_id) {
@ -69,19 +74,24 @@ function forum_make_link($discussion_id, $post_id) {
/**
* search standard API
* @uses $DB;
*
*/
function forum_iterator() {
$forums = get_records('forum');
global $DB;
$forums = $DB->get_records('forum');
return $forums;
}
/**
* search standard API
* @param forum a forum instance
* @uses $DB
* @param reference $forum a forum instance
* @return an array of searchable documents
*/
function forum_get_content_for_index(&$forum) {
global $DB;
$documents = array();
if (!$forum) return $documents;
@ -90,8 +100,8 @@ function forum_get_content_for_index(&$forum) {
mtrace("Found ".count($posts)." discussions to analyse in forum ".$forum->name);
if (!$posts) return $documents;
$coursemodule = get_field('modules', 'id', 'name', 'forum');
$cm = get_record('course_modules', 'course', $forum->course, 'module', $coursemodule, 'instance', $forum->id);
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'forum'));
$cm = $DB->get_record('course_modules', array('course' => $forum->course, 'module' => $coursemodule, 'instance' => $forum->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
foreach($posts as $aPost) {
@ -118,16 +128,18 @@ function forum_get_content_for_index(&$forum) {
/**
* returns a single forum search document based on a forum entry id
* @param id an id for a single information stub
* @param itemtype the type of information
* @uses $DB
* @param int $id an id for a single information stub
* @param string $itemtype the type of information
*/
function forum_single_document($id, $itemtype) {
global $DB;
// both known item types are posts so get them the same way
$post = get_record('forum_posts', 'id', $id);
$discussion = get_record('forum_discussions', 'id', $post->discussion);
$coursemodule = get_field('modules', 'id', 'name', 'forum');
$cm = get_record('course_modules', 'course', $discussion->course, 'module', $coursemodule, 'instance', $discussion->forum);
$post = $DB->get_record('forum_posts', array('id' => $id));
$discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'forum'));
$cm = $DB->get_record('course_modules', array('course' => $discussion->course, 'module' => $coursemodule, 'instance' => $discussion->forum));
if ($cm){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$post->groupid = $discussion->groupid;
@ -161,17 +173,18 @@ function forum_db_names() {
/**
* reworked faster version from /mod/forum/lib.php
* @param forum_id a forum identifier
* @uses CFG, USER
* @param int $forum_id a forum identifier
* @uses $CFG, $USER, $DB
* @return an array of posts
* @todo get rid of old isteacher() call
*/
function forum_get_discussions_fast($forum_id) {
global $CFG, $USER;
global $CFG, $USER, $DB;
$timelimit='';
if (!empty($CFG->forum_enabletimedposts)) {
if (!((has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))
and !empty($CFG->admineditalways)) || isteacher(get_field('forum', 'course', 'id', $forum_id)))) {
&& !empty($CFG->admineditalways)) || isteacher(get_field('forum', 'course', 'id', $forum_id)))) {
$now = time();
$timelimit = " AND ((d.timestart = 0 OR d.timestart <= '$now') AND (d.timeend = 0 OR d.timeend > '$now')";
if (!empty($USER->id)) {
@ -203,24 +216,24 @@ function forum_get_discussions_fast($forum_id) {
ON
p.userid = u.id
WHERE
d.forum = '{$forum_id}' AND
d.forum = ? AND
p.parent = 0
$timelimit
ORDER BY
d.timemodified DESC
";
return get_records_sql($query);
return $DB->get_records_sql($query, array($forum_id));
}
/**
* reworked faster version from /mod/forum/lib.php
* @param parent the id of the first post within the discussion
* @param forum_id the forum identifier
* @uses CFG
* @param int $parent the id of the first post within the discussion
* @param int $forum_id the forum identifier
* @uses $CFG, $DB
* @return an array of posts
*/
function forum_get_child_posts_fast($parent, $forum_id) {
global $CFG;
global $CFG, $DB;
$query = "
SELECT
@ -229,7 +242,7 @@ function forum_get_child_posts_fast($parent, $forum_id) {
p.discussion,
p.message,
p.created,
{$forum_id} AS forum,
? AS forum,
p.userid,
d.groupid,
u.firstname,
@ -245,11 +258,11 @@ function forum_get_child_posts_fast($parent, $forum_id) {
ON
p.userid = u.id
WHERE
p.parent = '{$parent}'
p.parent = ?
ORDER BY
p.created ASC
";
return get_records_sql($query);
return $DB->get_records_sql($query, array($forum_id, $parent));
}
/**
@ -259,25 +272,25 @@ function forum_get_child_posts_fast($parent, $forum_id) {
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by itemtype. In forums, this id
* @param string $path the access path to the module script code
* @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param int $this_id the item id within the information class denoted by itemtype. In forums, this id
* points out the individual post.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG, USER
* @param object $user the user record denoting the user who searches
* @param int $group_id the current group used by the user when searching
* @uses $CFG, $USER, $DB
* @return true if access is allowed, false elsewhere
*/
function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG, $USER;
global $CFG, $USER, $DB;
include_once("{$CFG->dirroot}/{$path}/lib.php");
// get the forum post and all related stuff
$post = get_record('forum_posts', 'id', $this_id);
$discussion = get_record('forum_discussions', 'id', $post->discussion);
$context = get_record('context', 'id', $context_id);
$cm = get_record('course_modules', 'id', $context->instanceid);
$post = $DB->get_record('forum_posts', array('id' => $this_id));
$discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion));
$context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
// $cm = get_coursemodule_from_instance('forum', $discussion->forum, $discussion->course);
// $context = get_context_instance(CONTEXT_MODULE, $cm->id);
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){
@ -293,7 +306,7 @@ function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id, $
// group check : entries should be in accessible groups
$current_group = get_current_group($discussion->course);
$course = get_record('course', 'id', $discussion->course);
$course = $DB->get_record('course', array('id' => $discussion->course));
if ($group_id >= 0 && (groupmode($course, $cm) == SEPARATEGROUPS) && ($group_id != $current_group) && !has_capability('mod/forum:viewdiscussionsfromallgroups', $context)){
if (!empty($CFG->search_access_debug)) echo "search reject : separated grouped forum item";
return false;
@ -304,10 +317,15 @@ function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id, $
/**
* post processes the url for cleaner output.
* @uses $CFG
* @param string $title
*/
function forum_link_post_processing($title){
// return mb_convert_encoding($title, 'UTF-8', 'auto');
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}

View file

@ -8,6 +8,7 @@
* @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for glossary activity module
* This file contains a mapping between a glossary entry and it's indexable counterpart,
@ -20,7 +21,7 @@
/**
* includes and requires
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once($CFG->dirroot.'/search/documents/document.php');
/**
* a class for representing searchable information
@ -33,6 +34,8 @@ class GlossarySearchDocument extends SearchDocument {
*
*/
public function __construct(&$entry, $course_id, $context_id) {
global $DB;
// generic information; required
$doc->docid = $entry['id'];
$doc->documenttype = SEARCH_TYPE_GLOSSARY;
@ -43,7 +46,7 @@ class GlossarySearchDocument extends SearchDocument {
$doc->date = $entry['timecreated'];
if ($entry['userid'])
$user = get_record('user', 'id', $entry['userid']);
$user = $DB->get_record('user', array('id' => $entry['userid']));
$doc->author = ($user ) ? $user->firstname.' '.$user->lastname : '' ;
$doc->contents = strip_tags($entry['definition']);
$doc->url = glossary_make_link($entry['id']);
@ -64,8 +67,11 @@ class GlossaryCommentSearchDocument extends SearchDocument {
/**
* document constructor
* @uses $DB
*/
public function __construct(&$entry, $glossary_id, $course_id, $context_id) {
global $DB;
// generic information; required
$doc->docid = $entry['id'];
$doc->documenttype = SEARCH_TYPE_GLOSSARY;
@ -76,7 +82,7 @@ class GlossaryCommentSearchDocument extends SearchDocument {
$doc->date = $entry['timemodified'];
if ($entry['userid'])
$user = get_record('user', 'id', $entry['userid']);
$user = $DB->get_record('user', array('id' => $entry['userid']));
$doc->author = ($user ) ? $user->firstname.' '.$user->lastname : '' ;
$doc->contents = strip_tags($entry['entrycomment']);
$doc->url = glossary_make_link($entry['entryid']);
@ -85,13 +91,14 @@ class GlossaryCommentSearchDocument extends SearchDocument {
$data->glossary = $glossary_id;
// construct the parent class
parent::__construct($doc, $data, $course_id, -1, $entry['userid'], PATH_FOR_SEARCH_TYPE_GLOSSARY);
parent::__construct($doc, $data, $course_id, -1, $entry['userid'], 'mod/'.SEARCH_TYPE_GLOSSARY);
}
}
/**
* constructs valid access links to information
* @param entry_id the id of the glossary entry
* @uses $CFG
* @param int $entry_id the id of the glossary entry
* @return a full featured link element as a string
*/
function glossary_make_link($entry_id) {
@ -104,7 +111,7 @@ function glossary_make_link($entry_id) {
// Suggestion : bounce on popup within the glossarie's showentry page
// preserve glossary pop-up, be careful where you place your ' and "s
//this function is meant to return a url that is placed between href='[url here]'
return "$CFG->wwwroot/mod/glossary/showentry.php?eid=$entry_id' onclick='return openpopup(\"/mod/glossary/showentry.php?eid=$entry_id\", \"entry\", DEFAULT_POPUP_SETTINGS, 0);";
return "$CFG->wwwroot/mod/glossary/showentry.php?eid=$entry_id' onclick='return openpopup(\"/mod/glossary/showentry.php?eid={$entry_id}\", \"entry\", DEFAULT_POPUP_SETTINGS, 0);";
}
/**
@ -112,27 +119,30 @@ function glossary_make_link($entry_id) {
*
*/
function glossary_iterator() {
$glossaries = get_records('glossary');
global $DB;
$glossaries = $DB->get_records('glossary');
return $glossaries;
}
/**
* part of search engine API
* @glossary a glossary instance
* @uses $DB
* @param object $glossary a glossary instance
* @return an array of searchable documents
*/
function glossary_get_content_for_index(&$glossary) {
global $DB;
// get context
$coursemodule = get_field('modules', 'id', 'name', 'glossary');
$cm = get_record('course_modules', 'course', $glossary->course, 'module', $coursemodule, 'instance', $glossary->id);
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'glossary'));
$cm = $DB->get_record('course_modules', array('course' => $glossary->course, 'module' => $coursemodule, 'instance' => $glossary->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$documents = array();
$entryIds = array();
// index entries
$entries = get_records('glossary_entries', 'glossaryid', $glossary->id);
$entries = $DB->get_records('glossary_entries', array('glossaryid' => $glossary->id));
if ($entries){
foreach($entries as $entry) {
$concepts[$entry->id] = $entry->concept;
@ -160,21 +170,24 @@ function glossary_get_content_for_index(&$glossary) {
/**
* part of search engine API
* @param id the glossary entry identifier
* @itemtype the type of information
* @uses $DB
* @param int $id the glossary entry identifier
* @param string $itemtype the type of information
* @return a single search document based on a glossary entry
*/
function glossary_single_document($id, $itemtype) {
global $DB;
if ($itemtype == 'standard'){
$entry = get_record('glossary_entries', 'id', $id);
$entry = $DB->get_record('glossary_entries', array('id' => $id));
}
elseif ($itemtype == 'comment'){
$comment = get_record('glossary_comments', 'id', $id);
$entry = get_record('glossary_entries', 'id', $comment->entryid);
$comment = $DB->get_record('glossary_comments', array('id' => $id));
$entry = $DB->get_record('glossary_entries', array('id' => $comment->entryid));
}
$glossary_course = get_field('glossary', 'course', 'id', $entry->glossaryid);
$coursemodule = get_field('modules', 'id', 'name', 'glossary');
$cm = get_record('course_modules', 'course', $glossary_course, 'module', $coursemodule, 'instance', $entry->glossaryid);
$glossary_course = $DB->get_field('glossary', 'course', array('id' => $entry->glossaryid));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'glossary'));
$cm = $DB->get_record('course_modules', array('course' => $glossary_course, 'module' => $coursemodule, 'instance' => $entry->glossaryid));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if ($itemtype == 'standard'){
return new GlossarySearchDocument(get_object_vars($entry), $glossary_course, $context->id);
@ -214,6 +227,7 @@ function glossary_db_names() {
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @uses $CFG, $DB
* @param string $path the access path to the module script code
* @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param int $this_id the item id within the information class denoted by itemtype. In glossaries, this id
@ -224,15 +238,13 @@ function glossary_db_names() {
* @return true if access is allowed, false elsewhere
*/
function glossary_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG;
global $CFG, $DB;
// get the glossary object and all related stuff
$entry = get_record('glossary_entries', 'id', $this_id);
$glossary = get_record('glossary', 'id', $entry->glossaryid);
$context = get_record('context', 'id', $context_id);
$cm = get_record('course_modules', 'id', $context->instanceid);
// $cm = get_coursemodule_from_instance('glossary', $glossary->id, $glossary->course);
// $context = get_context_instance(CONTEXT_MODULE, $cm->id);
$entry = $DB->get_record('glossary_entries', array('id' => $this_id));
$glossary = $DB->get_record('glossary', array('id' => $entry->glossaryid));
$context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $context)) {
return false;
@ -251,6 +263,11 @@ function glossary_check_text_access($path, $itemtype, $this_id, $user, $group_id
* @param string $title
*/
function glossary_link_post_processing($title){
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}

View file

@ -0,0 +1,188 @@
<?php
/**
* Global Search Engine for Moodle
*
* @package search
* @category core
* @subpackage document_wrappers
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.9
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for all resources
* This file contains the mapping between a resource and it's indexable counterpart,
*
* Functions for iterating and retrieving the necessary records are now also included
* in this file, rather than mod/resource/lib.php
*/
/**
* requires and includes
*/
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/resource/lib.php');
/* *
* a class for representing searchable information
*
*/
class LabelSearchDocument extends SearchDocument {
public function __construct(&$label, $context_id) {
// generic information; required
$doc->docid = $label['id'];
$doc->documenttype = SEARCH_TYPE_LABEL;
$doc->itemtype = 'label';
$doc->contextid = $context_id;
$doc->title = strip_tags($label['name']);
$doc->date = $label['timemodified'];
$doc->author = '';
$doc->contents = strip_tags($label['content']);
$doc->url = label_make_link($label['course']);
// module specific information; optional
$data = array();
// construct the parent class
parent::__construct($doc, $data, $label['course'], 0, 0, 'mod/'.SEARCH_TYPE_LABEL);
} //constructor
}
/**
* constructs valid access links to information
* @param int $resourceId the of the resource
* @return a full featured link element as a string
*/
function label_make_link($course_id) {
global $CFG;
return $CFG->wwwroot.'/course/view.php?id='.$course_id;
}
/**
* part of standard API
*
*/
function label_iterator() {
global $DB;
//trick to leave search indexer functionality intact, but allow
//this document to only use the below function to return info
//to be searched
$labels = $DB->get_records('label');
return $labels;
}
/**
* part of standard API
* this function does not need a content iterator, returns all the info
* itself;
* @param $label notneeded to comply API, remember to fake the iterator array though
* @uses $CFG, $DB
* @return an array of searchable documents
*/
function label_get_content_for_index(&$label) {
global $CFG, $DB;
// starting with Moodle native resources
$documents = array();
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'label'));
$cm = $DB->get_record('course_modules', array('course' => $label->course, 'module' => $coursemodule, 'instance' => $label->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$documents[] = new LabelSearchDocument(get_object_vars($label), $context->id);
mtrace("finished label {$label->id}");
return $documents;
}
/**
* part of standard API.
* returns a single resource search document based on a label id
* @uses $CFG, $DB
* @param int $id the id of the accessible document
* @param string $itemtype the nature of the information making the document
* @return a searchable object or null if failure
*/
function label_single_document($id, $itemtype) {
global $CFG, $DB;
$label = $DB->get_record('label', array('id' => $id));
if ($label){
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'label'));
$cm = $DB->get_record('course_modules', array('id' => $label->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
return new LabelSearchDocument(get_object_vars($label), $context->id);
}
return null;
}
/**
* dummy delete function that aggregates id with itemtype.
* this was here for a reason, but I can't remember it at the moment.
*
*/
function label_delete($info, $itemtype) {
$object->id = $info;
$object->itemtype = $itemtype;
return $object;
} //resource_delete
/**
* returns the var names needed to build a sql query for addition/deletions
*
*/
function label_db_names() {
//[primary id], [table name], [time created field name], [time modified field name], [docsubtype], [additional where conditions for sql]
return array(array('id', 'label', 'timemodified', 'timemodified', 'label', ''));
}
/**
* this function handles the access policy to contents indexed as searchable documents. If this
* function does not exist, the search engine assumes access is allowed.
* @uses $CFG, $DB
* @param string $path the access path to the module script code
* @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param int $this_id the item id within the information class denoted by itemtype. In resources, this id
* points to the resource record and not to the module that shows it.
* @param object $user the user record denoting the user who searches
* @param int $group_id the current group used by the user when searching
* @return true if access is allowed, false elsewhere
*/
function label_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG, $DB;
$r = $DB->get_record('label', array('id' => $this_id));
$module_context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $module_context->instanceid));
$course_context = get_context_instance(CONTEXT_COURSE, $r->course);
//check if englobing course is visible
if (!has_capability('moodle/course:view', $course_context)){
return false;
}
//check if found course module is visible
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $module_context)){
return false;
}
return true;
}
/**
* post processes the url for cleaner output.
* @param string $title
*/
function label_link_post_processing($title){
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding("(".shorten_text(clean_text($title), 60)."...) ", 'UTF-8', 'auto');
}
return mb_convert_encoding("(".shorten_text(clean_text($title), 60)."...) ", 'auto', 'UTF-8');
}
?>

View file

@ -8,6 +8,7 @@
* @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for lesson activity module
* This file contains the mapping between a lesson page and it's indexable counterpart,
@ -19,8 +20,8 @@
/**
* includes and requires
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once("$CFG->dirroot/mod/lesson/lib.php");
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/lesson/lib.php');
/**
* a class for representing searchable information
@ -57,40 +58,49 @@ class LessonPageSearchDocument extends SearchDocument {
* constructs a valid link to a chat content
* @param int $lessonid the lesson module
* @param int $itemid the id of a single page
* @param string $itemtype the nature of the indexed object
* @return a well formed link to lesson page
*/
function lesson_make_link($lessonmoduleid, $itemid, $itemtype) {
global $CFG;
if ($itemtype == 'page'){
return "{$CFG->wwwroot}/mod/lesson/view.php?id={$lessonmoduleid}&amp;pageid={$itemid}";
return $CFG->wwwroot."/mod/lesson/view.php?id={$lessonmoduleid}&amp;pageid={$itemid}";
}
return $CFG->wwwroot.'/mod/lesson/view.php?id='.$lessonmoduleid;
}
/**
* search standard API
* @uses $DB
*
*/
function lesson_iterator() {
$lessons = get_records('lesson');
global $DB;
if ($lessons = $DB->get_records('lesson')){
return $lessons;
} else {
return array();
}
}
/**
* search standard API
* @param object $lesson a lesson instance (by ref)
* @uses $DB
* @param reference $lesson a lesson instance (by ref)
* @return an array of searchable documents
*/
function lesson_get_content_for_index(&$lesson) {
global $DB;
$documents = array();
if (!$lesson) return $documents;
$pages = get_records('lesson_pages', 'lessonid', $lesson->id);
$pages = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id));
if ($pages){
$coursemodule = get_field('modules', 'id', 'name', 'lesson');
$cm = get_record('course_modules', 'course', $lesson->course, 'module', $coursemodule, 'instance', $lesson->id);
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'lesson'));
$cm = $DB->get_record('course_modules', array('course' => $lesson->course, 'module' => $coursemodule, 'instance' => $lesson->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
foreach($pages as $aPage){
$documents[] = new LessonPageSearchDocument(get_object_vars($aPage), $cm->id, $lesson->course, 'page', $context->id);
@ -102,16 +112,18 @@ function lesson_get_content_for_index(&$lesson) {
/**
* returns a single lesson search document based on a lesson page id
* @uses $DB
* @param int $id an id for a single information item
* @param string $itemtype the type of information
*/
function lesson_single_document($id, $itemtype) {
global $DB;
// only page is known yet
$page = get_record('lesson_pages', 'id', $id);
$lesson = get_record('lesson', 'id', $page->lessonid);
$coursemodule = get_field('modules', 'id', 'name', 'lesson');
$cm = get_record('course_modules', 'course', $lesson->course, 'module', $coursemodule, 'instance', $page->lessonid);
$page = $DB->get_record('lesson_pages', array('id' => $id));
$lesson = $DB->get_record('lesson', array('id' => $page->lessonid));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'lesson'));
$cm = $DB->get_record('course_modules', array('course' => $lesson->course, 'module' => $coursemodule, 'instance' => $page->lessonid));
if ($cm){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$lesson->groupid = 0;
@ -156,22 +168,19 @@ function lesson_db_names() {
* @param object $user the user record denoting the user who searches
* @param int $group_id the current group used by the user when searching
* @param int $context_id the id of the context used when indexing
* @uses CFG, USER
* @uses $CFG, $USER, $DB
* @return true if access is allowed, false elsewhere
*/
function lesson_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG, $USER;
global $CFG, $USER, $DB;
include_once("{$CFG->dirroot}/{$path}/lib.php");
// get the lesson page
$page = get_record('lesson_pages', 'id', $this_id);
$lesson = get_record('lesson', 'id', $page->lessonid);
$context = get_record('context', 'id', $context_id);
$cm = get_record('course_modules', 'id', $context->instanceid);
// $lesson = get_record('lesson', 'id', $page->lessonid);
// $cm = get_coursemodule_from_instance('lesson', $page->lessonid, $lesson->course);
// $context = get_context_instance(CONTEXT_MODULE, $cm->id);
$page = $DB->get_record('lesson_pages', array('id' => $this_id));
$lesson = $DB->get_record('lesson', array('id' => $page->lessonid));
$context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){
if (!empty($CFG->search_access_debug)) echo "search reject : hidden lesson ";
@ -210,6 +219,11 @@ function lesson_check_text_access($path, $itemtype, $this_id, $user, $group_id,
*
*/
function lesson_link_post_processing($title){
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* this is a format handler for getting text out of a proprietary binary format
* so it can be indexed by Lucene search engine
@ -16,6 +17,7 @@
/**
* MS Word extractor
* @param object $resource
* @param string $directfile if the resource is given as a direct file path, use it as reference to the file
* @uses $CFG
*/
function get_text_for_indexing_doc(&$resource, $directfile = ''){
@ -24,7 +26,12 @@ function get_text_for_indexing_doc(&$resource, $directfile = ''){
// SECURITY : do not allow non admin execute anything on system !!
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
$moodleroot = (@$CFG->block_search_usemoodleroot) ? "{$CFG->dirroot}/" : '' ;
// adds moodle root switch if none was defined
if (!isset($CFG->block_search_usemoodleroot)){
set_config('block_search_usemoodleroot', 1);
}
$moodleroot = ($CFG->block_search_usemoodleroot) ? "{$CFG->dirroot}/" : '' ;
// just call pdftotext over stdout and capture the output
if (!empty($CFG->block_search_word_to_text_cmd)){

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* this is a format handler for getting text out of a proprietary binary format
* so it can be indexed by Lucene search engine
@ -15,6 +16,7 @@
/**
* @param object $resource
* @param string $directfile if the resource is given as a direct file path, use it as reference to the file
* @uses $CFG
*/
function get_text_for_indexing_htm(&$resource, $directfile = ''){

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* this is a format handler for getting text out of a standard html format
* so it can be indexed by Lucene search engine

View file

@ -0,0 +1,62 @@
<?php
/**
* Global Search Engine for Moodle
*
* @package search
* @category core
* @subpackage document_wrappers
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* this is a format handler for getting text out of the opensource ODT binary format
* so it can be indexed by Lucene search engine
*/
/**
* OpenOffice Odt extractor
* @param object $resource
* @param string $directfile if the resource is given as a direct file path, use it as reference to the file
* @uses $CFG
*/
function get_text_for_indexing_odt(&$resource, $directfile = ''){
global $CFG;
// SECURITY : do not allow non admin execute anything on system !!
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// adds moodle root switch if none was defined
if (!isset($CFG->block_search_usemoodleroot)){
set_config('block_search_usemoodleroot', 1);
}
$moodleroot = ($CFG->block_search_usemoodleroot) ? "{$CFG->dirroot}/" : '' ;
// just call pdftotext over stdout and capture the output
if (!empty($CFG->block_search_odt_to_text_cmd)){
if (!file_exists("{$moodleroot}{$CFG->block_search_odt_to_text_cmd}")){
mtrace('Error with OpenOffice ODT to text converter command : exectuable not found.');
} else {
if ($directfile == ''){
$file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}");
} else {
$file = escapeshellarg("{$CFG->dataroot}/{$directfile}");
}
$command = trim($CFG->block_search_odt_to_text_cmd);
$text_converter_cmd = "{$moodleroot}{$command} --encoding=UTF-8 $file";
mtrace("Executing : $text_converter_cmd");
$result = shell_exec($text_converter_cmd);
if ($result){
return $result;
} else {
mtrace('Error with OpenOffice ODT to text converter command : execution failed. ');
return '';
}
}
} else {
mtrace('Error with OpenOffice ODT to text converter command : command not set up. Execute once search block configuration.');
return '';
}
}
?>

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* this is a format handler for getting text out of a proprietary binary format
* so it can be indexed by Lucene search engine
@ -15,6 +16,7 @@
/**
* @param object $resource
* @param string $directfile if the resource is given as a direct file path, use it as reference to the file
* @uses $CFG
*/
function get_text_for_indexing_pdf(&$resource, $directfile = ''){

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* this is a format handler for getting text out of a proprietary binary format
* so it can be indexed by Lucene search engine
@ -29,6 +30,7 @@
/**
* @param object $resource
* @param string $directfile if the resource is given as a direct file path, use it as reference to the file
* @uses $CFG
*/
function get_text_for_indexing_ppt(&$resource, $directfile = ''){

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* @note : The Adobe SWF Converters library is not GPL, although it can be of free use in some
* situations. This file is provided for convenience, but should use having a glance at
@ -19,6 +20,7 @@
/**
* @param object $resource
* @param string $directfile if the resource is given as a direct file path, use it as reference to the file
* @uses $CFG
*/
function get_text_for_indexing_swf(&$resource, $directfile = ''){

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* this is a format handler for getting text out of a proprietary binary format
* so it can be indexed by Lucene search engine
@ -15,6 +16,7 @@
/**
* @param object $resource
* @param string $directfile if the resource is given as a direct file path, use it as reference to the file
* @uses $CFG
*/
function get_text_for_indexing_txt(&$resource, $directfile = ''){

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version revised for Moodle 2.0
*
* this is a format handler for getting text out of a proprietary binary format
* so it can be indexed by Lucene search engine
@ -15,6 +16,7 @@
/**
* @param object $resource
* @param string $directfile if the resource is given as a direct file path, use it as reference to the file
* @uses $CFG
*/
function get_text_for_indexing_xml(&$resource, $directfile = ''){

View file

@ -8,6 +8,7 @@
* @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for all resources
* This file contains the mapping between a resource and it's indexable counterpart,
@ -19,8 +20,8 @@
/**
* requires and includes
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once("$CFG->dirroot/mod/resource/lib.php");
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/resource/lib.php');
/* *
* a class for representing searchable information
@ -45,8 +46,8 @@ class ResourceSearchDocument extends SearchDocument {
// construct the parent class
parent::__construct($doc, $data, $resource['course'], 0, 0, 'mod/'.SEARCH_TYPE_RESOURCE);
} //constructor
} //ResourceSearchDocument
}
}
/**
* constructs valid access links to information
@ -57,7 +58,7 @@ function resource_make_link($resource_id) {
global $CFG;
return $CFG->wwwroot.'/mod/resource/view.php?id='.$resource_id;
} //resource_make_link
}
/**
* part of standard API
@ -68,18 +69,18 @@ function resource_iterator() {
//this document to only use the below function to return info
//to be searched
return array(true);
} //resource_iterator
}
/**
* part of standard API
* this function does not need a content iterator, returns all the info
* itself;
* @param notneeded to comply API, remember to fake the iterator array though
* @uses CFG
* @param void $notneeded to comply API, remember to fake the iterator array though
* @uses $CFG, $DB
* @return an array of searchable documents
*/
function resource_get_content_for_index(&$notneeded) {
global $CFG;
global $CFG, $DB;
// starting with Moodle native resources
$documents = array();
@ -88,23 +89,23 @@ function resource_get_content_for_index(&$notneeded) {
id as trueid,
r.*
FROM
{resource} as r
{resource} r
WHERE
alltext != '' AND
alltext != ' ' AND
alltext != '&nbsp;' AND
type != 'file'
";
$resources = get_records_sql($query);
if ($resources = $DB->get_records_sql($query)){
foreach($resources as $aResource){
$coursemodule = get_field('modules', 'id', 'name', 'resource');
$cm = get_record('course_modules', 'course', $aResource->course, 'module', $coursemodule, 'instance', $aResource->id);
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'resource'));
$cm = $DB->get_record('course_modules', array('course' => $aResource->course, 'module' => $coursemodule, 'instance' => $aResource->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$aResource->id = $cm->id;
$documents[] = new ResourceSearchDocument(get_object_vars($aResource), $context->id);
mtrace("finished $aResource->name");
}
}
// special physical files handling
/**
@ -125,9 +126,9 @@ function resource_get_content_for_index(&$notneeded) {
r.type as type,
r.timemodified as timemodified
FROM
{resource} as r,
{course_modules} as cm,
{modules} as m
{resource} r,
{course_modules} cm,
{modules} m
WHERE
r.type = 'file' AND
cm.instance = r.id AND
@ -135,28 +136,28 @@ function resource_get_content_for_index(&$notneeded) {
cm.module = m.id AND
m.name = 'resource'
";
$resources = get_records_sql($query);
if ($resources = $DB->get_records_sql($query)){
// invokes external content extractor if exists.
if ($resources){
foreach($resources as $aResource){
// fetches a physical indexable document and adds it to documents passed by ref
$coursemodule = get_field('modules', 'id', 'name', 'resource');
$cm = get_record('course_modules', 'id', $aResource->id);
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'resource'));
$cm = $DB->get_record('course_modules', array('id' => $aResource->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
resource_get_physical_file($aResource, $context->id, false, $documents);
}
}
}
return $documents;
} //resource_get_content_for_index
}
/**
* get text from a physical file
* @param resource a resource for which to fetch some representative text
* @param getsingle if true, returns a single search document, elsewhere return the array
* @uses $CFG
* @param reference $resource a resource for which to fetch some representative text
* @param int $context_id the context associated with the resource
* @param bool $getsingle if true, returns a single search document, elsewhere return the array
* given as documents increased by one
* @param documents the array of documents, by ref, where to add the new document.
* @param array $documents the array of documents, by ref, where to add the new document.
* @return a search document when unique or false.
*/
function resource_get_physical_file(&$resource, $context_id, $getsingle, &$documents = null){
@ -218,11 +219,12 @@ function resource_get_physical_file(&$resource, $context_id, $getsingle, &$docum
/**
* part of standard API.
* returns a single resource search document based on a resource_entry id
* @uses $CFG, $DB
* @param id the id of the accessible document
* @return a searchable object or null if failure
*/
function resource_single_document($id, $itemtype) {
global $CFG;
global $CFG, $DB;
// rewriting with legacy moodle databse API
$query = "
@ -237,9 +239,9 @@ function resource_single_document($id, $itemtype) {
r.type as type,
r.timemodified as timemodified
FROM
{resource} as r,
{course_modules} as cm,
{modules} as m
{resource} r,
{course_modules} cm,
{modules} m
WHERE
cm.instance = r.id AND
cm.course = r.course AND
@ -250,13 +252,13 @@ function resource_single_document($id, $itemtype) {
r.alltext != ' ' AND
r.alltext != '&nbsp;') OR
r.type = 'file') AND
r.id = '{$id}'
r.id = '?'
";
$resource = get_record_sql($query);
$resource = $DB->get_record_sql($query, array($id));
if ($resource){
$coursemodule = get_field('modules', 'id', 'name', 'resource');
$cm = get_record('course_modules', 'id', $resource->id);
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'resource'));
$cm = $DB->get_record('course_modules', array('id' => $resource->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if ($resource->type == 'file' && @$CFG->block_search_enable_file_indexing){
$document = resource_get_physical_file($resource, true, $context->id);
@ -266,9 +268,9 @@ function resource_single_document($id, $itemtype) {
return new ResourceSearchDocument(get_object_vars($resource), $context->id);
}
}
mtrace("null resource");
mtrace('null resource');
return null;
} //resource_single_document
}
/**
* dummy delete function that aggregates id with itemtype.
@ -279,7 +281,7 @@ function resource_delete($info, $itemtype) {
$object->id = $info;
$object->itemtype = $itemtype;
return $object;
} //resource_delete
}
/**
* returns the var names needed to build a sql query for addition/deletions
@ -288,11 +290,12 @@ function resource_delete($info, $itemtype) {
function resource_db_names() {
//[primary id], [table name], [time created field name], [time modified field name], [additional where conditions for sql]
return array(array('id', 'resource', 'timemodified', 'timemodified', 'any', " (alltext != '' AND alltext != ' ' AND alltext != '&nbsp;' AND TYPE != 'file') OR TYPE = 'file' "));
} //resource_db_names
}
/**
* this function handles the access policy to contents indexed as searchable documents. If this
* function does not exist, the search engine assumes access is allowed.
* @uses $CFG, $DB
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by itemtype. In resources, this id
@ -302,13 +305,26 @@ function resource_db_names() {
* @return true if access is allowed, false elsewhere
*/
function resource_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG;
global $CFG, $DB;
include_once("{$CFG->dirroot}/{$path}/lib.php");
// include_once("{$CFG->dirroot}/{$path}/lib.php");
$r = $DB->get_record('resource', array('id' => $this_id));
$module_context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $module_context->instanceid));
$course = $DB->get_record('course', array('id' => $r->course));
$course_context = get_context_instance(CONTEXT_COURSE, $r->course);
//check if course is visible
if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $course_context)) {
return false;
}
//check if user is registered in course or course is open to guests
if (!$course->guest && !has_capability('moodle/course:view', $course_context)) {
return false;
}
$r = get_record('resource', 'id', $this_id);
$module_context = get_record('context', 'id', $context_id);
$cm = get_record('course_modules', 'id', $module_context->instanceid);
//check if found course module is visible
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $module_context)){
@ -323,7 +339,11 @@ function resource_check_text_access($path, $itemtype, $this_id, $user, $group_id
* @param string $title
*/
function resource_link_post_processing($title){
// return mb_convert_encoding($title, 'UTF-8', 'auto');
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}
?>

View file

@ -8,6 +8,7 @@
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* special (EXTRA) document handling for user related data
*
@ -16,8 +17,8 @@
/**
* includes and requires
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once("$CFG->dirroot/blog/lib.php");
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/blog/lib.php');
/**
* a class for representing searchable information in user metadata
@ -27,8 +28,10 @@ class UserSearchDocument extends SearchDocument {
/**
* constructor
* @uses $DB
*/
public function __construct(&$userhash, $user_id, $context_id) {
global $DB;
// generic information; required
$doc->docid = $userhash['id'];
@ -36,12 +39,12 @@ class UserSearchDocument extends SearchDocument {
$doc->itemtype = 'user';
$doc->contextid = $context_id;
$user = get_record('user', 'id', $user_id);
$user = $DB->get_record('user', array('id' => $user_id));
$doc->title = get_string('user').': '.fullname($user);
$doc->date = ($userhash['lastaccess']) ? $userhash['lastaccess'] : time() ;
//remove '(ip.ip.ip.ip)' from chat author list
$doc->author = '';
$doc->author = $user->id;
$doc->contents = $userhash['description'];
$doc->url = user_make_link($user_id, 'user');
@ -60,15 +63,18 @@ class UserPostSearchDocument extends SearchDocument {
/**
* constructor
* @uses $DB
*/
public function __construct(&$post, $user_id, $context_id) {
global $DB;
// generic information; required
$doc->docid = $post['id'];
$doc->documenttype = SEARCH_TYPE_USER;
$doc->itemtype = 'post';
$doc->contextid = $context_id;
$user = get_record('user', 'id', $user_id);
$user = $DB->get_record('user', array('id' => $user_id));
// we cannot call userdate with relevant locale at indexing time.
$doc->title = get_string('post').': '.fullname($user);
@ -94,15 +100,18 @@ class UserBlogAttachmentSearchDocument extends SearchDocument {
/**
* constructor
* @uses $DB
*/
public function __construct(&$post, $context_id) {
global $DB;
// generic information; required
$doc->docid = $post['id'];
$doc->documenttype = SEARCH_TYPE_USER;
$doc->itemtype = 'attachment';
$doc->contextid = $context_id;
$user = get_record('user', 'id', $post['userid']);
$user = $DB->get_record('user', 'id', $post['userid']);
// we cannot call userdate with relevant locale at indexing time.
$doc->title = get_string('file').' : '.$post['subject'];
@ -123,20 +132,20 @@ class UserBlogAttachmentSearchDocument extends SearchDocument {
/**
* constructs a valid link to a user record
* @param userid the user
* @param itemtype
* @uses CFG
* @param int $userid the user
* @param string $itemtype
* @uses $CFG, $DB
* @return a well formed link to user information
*/
function user_make_link($itemid, $itemtype) {
global $CFG;
global $CFG, $DB;
if ($itemtype == 'user'){
return $CFG->wwwroot.'/user/view.php?id='.$itemid;
} elseif ($itemtype == 'post') {
return $CFG->wwwroot.'/blog/index.php?userid='.$itemid;
} elseif ($itemtype == 'attachment') {
$post = get_record('post', 'id', $itemid);
$post = $DB->get_record('post', array('id' => $itemid));
if (!$CFG->slasharguments){
return $CFG->wwwroot."/file.php?file=/blog/attachments/{$post->id}/{$post->attachment}";
} else {
@ -149,33 +158,37 @@ function user_make_link($itemid, $itemtype) {
/**
* part of search engine API
* @uses $DB
*
*/
function user_iterator() {
$users = get_records('user');
global $DB;
$users = $DB->get_records('user');
return $users;
}
/**
* part of search engine API
* @uses CFG
* @uses $CFG, $DB
* @param reference $user a user record
* @return an array of documents generated from data
*/
function user_get_content_for_index(&$user) {
global $CFG;
global $CFG, $DB;
$documents = array();
$userhash = get_object_vars($user);
$documents[] = new UserSearchDocument($userhash, $user->id, null);
if ($posts = get_records('post', 'userid', $user->id, 'created')){
if ($posts = $DB->get_records('post', array('userid' => $user->id), 'created')){
foreach($posts as $post){
$texts = array();
$texts[] = $post->subject;
$texts[] = $post->summary;
$texts[] = $post->content;
$post->description = implode(" ", $texts);
$post->description = implode(' ', $texts);
// record the attachment if any and physical files can be indexed
if (@$CFG->block_search_enable_file_indexing){
@ -193,6 +206,7 @@ function user_get_content_for_index(&$user) {
/**
* get text from a physical file
* @uses $CFG
* @param object $post a post to whech the file is attached to
* @param boolean $context_id if in future we need recording a context along with the search document, pass it here
* @param boolean $getsingle if true, returns a single search document, elsewhere return the array
@ -255,17 +269,20 @@ function user_get_physical_file(&$post, $context_id, $getsingle, &$documents = n
/**
* returns a single user search document
* @uses $DB
* @param composite $id a unique document id made with
* @param itemtype the type of information (session is the only type)
*/
function user_single_document($id, $itemtype) {
global $DB;
if ($itemtype == 'user'){
if ($user = get_record('user', 'id', $id)){
if ($user = $DB->get_record('user', array('id' => $id))){
$userhash = get_object_vars($user);
return new UserSearchDocument($userhash, $user->id, 'user', null);
}
} elseif ($itemtype == 'post') {
if ($post = get_records('post', 'id', $id)){
if ($post = $DB->get_records('post', array('id' => $id))){
$texts = array();
$texts[] = $post->subject;
$texts[] = $post->summary;
@ -275,7 +292,7 @@ function user_single_document($id, $itemtype) {
return new UserPostSearchDocument($posthash, $user->id, 'post', null);
}
} elseif ($itemtype == 'attachment' && @$CFG->block_search_enable_file_indexing) {
if ($post = get_records('post', 'id', $id)){
if ($post = $DB->get_records('post', array('id' => $id))){
if ($post->attachment){
return user_get_physical_file($post, null, true);
}
@ -315,23 +332,23 @@ function user_db_names() {
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by entry_type. In chats, this id
* @param string $path the access path to the module script code
* @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param int $this_id the item id within the information class denoted by entry_type. In chats, this id
* points out a session history which is a close sequence of messages.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG
* @param object $user the user record denoting the user who searches
* @param int $group_id the current group used by the user when searching
* @uses $CFG, $DB
* @return true if access is allowed, false elsewhere
*/
function user_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG;
global $CFG, $DB;
include_once("{$CFG->dirroot}/{$path}/lib.php");
if ($itemtype == 'user'){
// get the user
$userrecord = get_record('user', 'id', $this_id);
$userrecord = $DB->get_record('user', array('id' => $this_id));
// we cannot see nothing from unconfirmed users
if (!$userrecord->confirmed and !has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))){
@ -340,13 +357,13 @@ function user_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
}
} elseif ($itemtype == 'post' || $itemtype == 'attachment'){
// get the post
$post = get_record('post', 'id', $this_id);
$userrecord = get_record('user', 'id', $post->userid);
$post = $DB->get_record('post', array('id' => $this_id));
$userrecord = $DB->get_record('user', array('id' => $post->userid));
// we can try using blog visibility check
return blog_user_can_view_user_post($user->id, $post);
}
$context = get_record('context', 'id', $context_id);
$context = $DB->get_record('context', array('id' => $context_id));
return true;
}
@ -356,6 +373,11 @@ function user_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
*
*/
function user_link_post_processing($title){
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}
?>

View file

@ -8,6 +8,7 @@
* @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @version Moodle 2.0
*
* document handling for wiki activity module
* This file contains the mapping between a wiki page and it's indexable counterpart,
@ -20,8 +21,8 @@
/**
* includes and requires
*/
require_once("$CFG->dirroot/search/documents/document.php");
require_once("$CFG->dirroot/mod/wiki/lib.php");
require_once($CFG->dirroot.'/search/documents/document.php');
require_once($CFG->dirroot.'/mod/wiki/lib.php');
/**
* All the $doc->___ fields are required by the base document class!
@ -68,35 +69,38 @@ function wiki_name_convert($str) {
* @param int $wikiId
* @param string $title
* @param int $version
* @uses CFG
* @uses $CFG
*/
function wiki_make_link($wikiId, $title, $version) {
global $CFG;
return $CFG->wwwroot.'/mod/wiki/view.php?wid='.$wikiId.'&amp;page='.wiki_name_convert($title).'&amp;version='.$version;
} //wiki_make_link
}
/**
* rescued and converted from ewikimoodlelib.php
* retrieves latest version of a page
* @uses $DB
* @param object $entry the wiki object as a reference
* @param string $pagename the name of the page known by the wiki engine
* @param int $version
*/
function wiki_get_latest_page(&$entry, $pagename, $version = 0) {
global $DB;
$pagename = "'".addslashes($pagename)."'";
if ($version > 0 and is_int($version)) {
$version = "AND (version=$version)";
if ($version > 0 && is_int($version)) {
$versionclause = "AND ( version = '$version' )";
} else {
$version = '';
$versionclause = '';
}
$select = "(pagename=$pagename) AND wiki=".$entry->id." $version ";
$select = "( pagename = ':pagename' ) AND wiki = :wikiid :versionclause ";
$sort = 'version DESC';
//change this to recordset_select, as per http://docs.moodle.org/en/Datalib_Notes
if ($result_arr = get_records_select('wiki_pages', $select, $sort, '*', 0, 1)) {
if ($result_arr = $DB->get_records_select('wiki_pages', $select, array('pagename' => $pagename, 'wikiid' => $entry->id, 'versionclause' => $versionclause), $sort, '*', 0, 1)) {
foreach ($result_arr as $obj) {
$result_obj = $obj;
}
@ -112,18 +116,24 @@ function wiki_get_latest_page(&$entry, $pagename, $version = 0) {
/**
* fetches all pages, including old versions
* @uses $DB
* @param object $entry the wiki object as a reference
* @return an array of record objects that represents pages of this wiki object
*/
function wiki_get_pages(&$entry) {
return get_records('wiki_pages', 'wiki', $entry->id);
global $DB;
return $DB->get_records('wiki_pages', array('wiki', $entry->id));
}
/**
* fetches all the latest versions of all the pages
* @param object $entry
* @uses $DB
* @param reference $entry
*/
function wiki_get_latest_pages(&$entry) {
global $DB;
//== (My)SQL for this
/* select * from wiki_pages
inner join
@ -135,8 +145,8 @@ function wiki_get_latest_pages(&$entry) {
$pages = array();
//http://moodle.org/bugs/bug.php?op=show&bugid=5877&pos=0
if ($ids = get_records('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
if ($pagesets = get_records('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
if ($ids = $DB->get_records('wiki_pages', array('wiki' => $entry->id), '', 'distinct pagename')) {
if ($pagesets = $DB->get_records('wiki_pages', array('wiki' => $entry->id), '', 'distinct pagename')) {
foreach ($pagesets as $aPageset) {
$pages[] = wiki_get_latest_page($entry, $aPageset->pagename);
}
@ -149,25 +159,30 @@ function wiki_get_latest_pages(&$entry) {
/**
* part of search engine API
* @uses $DB;
*
*/
function wiki_iterator() {
$wikis = get_records('wiki');
global $DB;
$wikis = $DB->get_records('wiki');
return $wikis;
}
/**
* part of search engine API
* @param wiki a wiki instance
* @uses $DB
* @param reference $wiki a wiki instance
* @return an array of searchable deocuments
*/
function wiki_get_content_for_index(&$wiki) {
global $DB;
$documents = array();
$entries = wiki_get_entries($wiki);
if ($entries){
$coursemodule = get_field('modules', 'id', 'name', 'wiki');
$cm = get_record('course_modules', 'course', $wiki->course, 'module', $coursemodule, 'instance', $wiki->id);
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'wiki'));
$cm = $DB->get_record('course_modules', array('course' => $wiki->course, 'module' => $coursemodule, 'instance' => $wiki->id));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
foreach($entries as $entry) {
@ -190,15 +205,18 @@ function wiki_get_content_for_index(&$wiki) {
/**
* returns a single wiki search document based on a wiki_entry id
* @param id the id of the wiki
* @param itemtype the type of information (standard)
* @uses $DB;
* @param int $id the id of the wiki
* @param string $itemtype the type of information (standard)
* @return a searchable document
*/
function wiki_single_document($id, $itemtype) {
$page = get_record('wiki_pages', 'id', $id);
$entry = get_record('wiki_entries', 'id', $page->wiki);
$coursemodule = get_field('modules', 'id', 'name', 'wiki');
$cm = get_record('course_modules', 'course', $entry->course, 'module', $coursemodule, 'instance', $entry->wikiid);
global $DB;
$page = $DB->get_record('wiki_pages', array('id' => $id));
$entry = $DB->get_record('wiki_entries', array('id' => $page->wiki));
$coursemodule = $DB->get_field('modules', 'id', array('name' => 'wiki'));
$cm = $DB->get_record('course_modules', array('course' => $entry->course, 'module' => $coursemodule, 'instance' => $entry->wikiid));
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
return new WikiSearchDocument(get_object_vars($page), $entry->wikiid, $entry->course, $entry->groupid, $page->userid, $context->id);
}
@ -227,27 +245,26 @@ function wiki_db_names() {
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by itemtype. In wikies, this id
* @param string $path the access path to the module script code
* @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param int $this_id the item id within the information class denoted by itemtype. In wikies, this id
* points out the indexed wiki page.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG
* @param object $user the user record denoting the user who searches
* @param int $group_id the current group used by the user when searching
* @param int $context_id a context that eventually comes with the object
* @uses $CFG, $DB
* @return true if access is allowed, false elsewhere
*/
function wiki_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
global $CFG;
global $CFG, $DB;
// get the wiki object and all related stuff
$page = get_record('wiki_pages', 'id', $id);
$entry = get_record('wiki_entries', 'id', $page->wiki);
$course = get_record('course', 'id', $entry->course);
$context = get_record('context', 'id', $context_id);
$cm = get_record('course_modules', 'id', $context->instanceid);
// $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
// $context = get_record('context', 'id', $cm->id);
if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
$page = $DB->get_record('wiki_pages', array('id' => $id));
$entry = $DB->get_record('wiki_entries', array('id' => $page->wiki));
$course = $DB->get_record('course', array('id' => $entry->course));
$context = $DB->get_record('context', array('id' => $context_id));
$cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
if (!$cm->visible && !has_capability('moodle/course:viewhiddenactivities', $context)) {
if (!empty($CFG->search_access_debug)) echo "search reject : hidden wiki ";
return false;
}
@ -268,7 +285,12 @@ function wiki_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
*
*/
function wiki_link_post_processing($title){
global $CFG;
if ($CFG->block_search_utf8dir){
return mb_convert_encoding($title, 'UTF-8', 'auto');
}
return mb_convert_encoding($title, 'auto', 'UTF-8');
}
?>