Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / search / documents / lesson_document.php
blob674dc0b63ca57f37cb72f6747ed6ee0e216d28ac
1 <?php
2 /**
3 * Global Search Engine for Moodle
5 * @package search
6 * @category core
7 * @subpackage document_wrappers
8 * @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
9 * @date 2008/03/31
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * document handling for lesson activity module
13 * This file contains the mapping between a lesson page and it's indexable counterpart,
15 * Functions for iterating and retrieving the necessary records are now also included
16 * in this file, rather than mod/lesson/lib.php
19 /**
20 * includes and requires
22 require_once("$CFG->dirroot/search/documents/document.php");
23 require_once("$CFG->dirroot/mod/lesson/lib.php");
25 /**
26 * a class for representing searchable information
29 class LessonPageSearchDocument extends SearchDocument {
31 /**
32 * constructor
35 public function __construct(&$page, $lessonmodule_id, $course_id, $itemtype, $context_id) {
36 // generic information
37 $doc->docid = $page['id'];
38 $doc->documenttype = SEARCH_TYPE_LESSON;
39 $doc->itemtype = $itemtype;
40 $doc->contextid = $context_id;
42 $doc->title = $page['title'];
44 $doc->author = '';
45 $doc->contents = $page['contents'];
46 $doc->date = $page['timecreated'];
47 $doc->url = lesson_make_link($lessonmodule_id, $page['id'], $itemtype);
49 // module specific information
50 $data->lesson = $page['lessonid'];
52 parent::__construct($doc, $data, $course_id, 0, 0, PATH_FOR_SEARCH_TYPE_LESSON);
56 /**
57 * constructs a valid link to a chat content
58 * @param int $lessonid the lesson module
59 * @param int $itemid the id of a single page
60 * @return a well formed link to lesson page
62 function lesson_make_link($lessonmoduleid, $itemid, $itemtype) {
63 global $CFG;
65 if ($itemtype == 'page'){
66 return "{$CFG->wwwroot}/mod/lesson/view.php?id={$lessonmoduleid}&amp;pageid={$itemid}";
68 return $CFG->wwwroot.'/mod/lesson/view.php?id='.$lessonmoduleid;
71 /**
72 * search standard API
75 function lesson_iterator() {
76 $lessons = get_records('lesson');
77 return $lessons;
80 /**
81 * search standard API
82 * @param object $lesson a lesson instance (by ref)
83 * @return an array of searchable documents
85 function lesson_get_content_for_index(&$lesson) {
87 $documents = array();
88 if (!$lesson) return $documents;
90 $pages = get_records('lesson_pages', 'lessonid', $lesson->id);
91 if ($pages){
92 $coursemodule = get_field('modules', 'id', 'name', 'lesson');
93 $cm = get_record('course_modules', 'course', $lesson->course, 'module', $coursemodule, 'instance', $lesson->id);
94 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
95 foreach($pages as $aPage){
96 $documents[] = new LessonPageSearchDocument(get_object_vars($aPage), $cm->id, $lesson->course, 'page', $context->id);
100 return $documents;
104 * returns a single lesson search document based on a lesson page id
105 * @param int $id an id for a single information item
106 * @param string $itemtype the type of information
108 function lesson_single_document($id, $itemtype) {
110 // only page is known yet
111 $page = get_record('lesson_pages', 'id', $id);
112 $lesson = get_record('lesson', 'id', $page->lessonid);
113 $coursemodule = get_field('modules', 'id', 'name', 'lesson');
114 $cm = get_record('course_modules', 'course', $lesson->course, 'module', $coursemodule, 'instance', $page->lessonid);
115 if ($cm){
116 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
117 $lesson->groupid = 0;
118 return new LessonPageSearchDocument(get_object_vars($page), $cm->id, $lesson->course, $itemtype, $context->id);
120 return null;
124 * dummy delete function that aggregates id with itemtype.
125 * this was here for a reason, but I can't remember it at the moment.
128 function lesson_delete($info, $itemtype) {
129 $object->id = $info;
130 $object->itemtype = $itemtype;
131 return $object;
135 * returns the var names needed to build a sql query for addition/deletions
138 function lesson_db_names() {
139 //[primary id], [table name], [time created field name], [time modified field name] [itemtype] [select for getting itemtype]
140 return array(
141 array('id', 'lesson_pages', 'timecreated', 'timemodified', 'page')
146 * this function handles the access policy to contents indexed as searchable documents. If this
147 * function does not exist, the search engine assumes access is allowed.
148 * When this point is reached, we already know that :
149 * - user is legitimate in the surrounding context
150 * - user may be guest and guest access is allowed to the module
151 * - the function may perform local checks within the module information logic
152 * @param string $path the access path to the module script code
153 * @param string $itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
154 * @param int $this_id the item id within the information class denoted by itemtype. In lessons, this id
155 * points out the individual page.
156 * @param object $user the user record denoting the user who searches
157 * @param int $group_id the current group used by the user when searching
158 * @param int $context_id the id of the context used when indexing
159 * @uses CFG, USER
160 * @return true if access is allowed, false elsewhere
162 function lesson_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
163 global $CFG, $USER;
165 include_once("{$CFG->dirroot}/{$path}/lib.php");
167 // get the lesson page
168 $page = get_record('lesson_pages', 'id', $this_id);
169 $lesson = get_record('lesson', 'id', $page->lessonid);
170 $context = get_record('context', 'id', $context_id);
171 $cm = get_record('course_modules', 'id', $context->instanceid);
172 // $lesson = get_record('lesson', 'id', $page->lessonid);
173 // $cm = get_coursemodule_from_instance('lesson', $page->lessonid, $lesson->course);
174 // $context = get_context_instance(CONTEXT_MODULE, $cm->id);
176 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){
177 if (!empty($CFG->search_access_debug)) echo "search reject : hidden lesson ";
178 return false;
181 $lessonsuperuser = has_capability('mod/lesson:edit', $context) or has_capability('mod/lesson:manage', $context);
182 // approval check : entries should be approved for being viewed, or belongs to the user
183 if (time() < $lesson->available && !$lessonsuperuser ){
184 if (!empty($CFG->search_access_debug)) echo "search reject : lesson is not available ";
185 return false;
188 if ($lesson->usepassword && !$lessonsuperuser){
189 if (!empty($CFG->search_access_debug)) echo "search reject : password required, cannot output in searches ";
190 return false;
193 // the user have it seen yet ? did he tried one time at least
194 $attempt = get_record('lesson_attempts', 'lessonid', $lesson->id, 'pageid', $page->id, 'userid', $USER->id);
195 if (!$attempt && !$lessonsuperuser){
196 if (!empty($CFG->search_access_debug)) echo "search reject : never tried this lesson ";
197 return false;
200 if ($attempt && !$attempt->correct && !$lessonsuperuser && !$lesson->retake){
201 if (!empty($CFG->search_access_debug)) echo "search reject : one try only, still not good ";
202 return false;
205 return true;
209 * this call back is called when displaying the link for some last post processing
212 function lesson_link_post_processing($title){
213 return mb_convert_encoding($title, 'UTF-8', 'auto');