3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Local library file for Lesson. These are non-standard functions that are used
24 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
28 /** Make sure this isn't being directly accessed */
29 defined('MOODLE_INTERNAL') || die();
31 /** Include the files that are required by this module */
32 require_once($CFG->dirroot.'/course/moodleform_mod.php');
33 require_once($CFG->dirroot . '/mod/lesson/lib.php');
36 define('LESSON_THISPAGE', 0);
37 /** Next page -> any page not seen before */
38 define("LESSON_UNSEENPAGE", 1);
39 /** Next page -> any page not answered correctly */
40 define("LESSON_UNANSWEREDPAGE", 2);
41 /** Jump to Next Page */
42 define("LESSON_NEXTPAGE", -1);
44 define("LESSON_EOL", -9);
45 /** Jump to an unseen page within a branch and end of branch or end of lesson */
46 define("LESSON_UNSEENBRANCHPAGE", -50);
47 /** Jump to Previous Page */
48 define("LESSON_PREVIOUSPAGE", -40);
49 /** Jump to a random page within a branch and end of branch or end of lesson */
50 define("LESSON_RANDOMPAGE", -60);
51 /** Jump to a random Branch */
52 define("LESSON_RANDOMBRANCH", -70);
54 define("LESSON_CLUSTERJUMP", -80);
56 define("LESSON_UNDEFINED", -99);
58 /** LESSON_MAX_EVENT_LENGTH = 432000 ; 5 days maximum */
59 define("LESSON_MAX_EVENT_LENGTH", "432000");
62 //////////////////////////////////////////////////////////////////////////////////////
63 /// Any other lesson functions go here. Each of them must have a name that
64 /// starts with lesson_
67 * Checks to see if a LESSON_CLUSTERJUMP or
68 * a LESSON_UNSEENBRANCHPAGE is used in a lesson.
70 * This function is only executed when a teacher is
71 * checking the navigation for a lesson.
73 * @param int $lesson Id of the lesson that is to be checked.
74 * @return boolean True or false.
76 function lesson_display_teacher_warning($lesson) {
79 // get all of the lesson answers
80 $params = array ("lessonid" => $lesson->id);
81 if (!$lessonanswers = $DB->get_records_select("lesson_answers", "lessonid = :lessonid", $params)) {
82 // no answers, then not useing cluster or unseen
85 // just check for the first one that fulfills the requirements
86 foreach ($lessonanswers as $lessonanswer) {
87 if ($lessonanswer->jumpto == LESSON_CLUSTERJUMP || $lessonanswer->jumpto == LESSON_UNSEENBRANCHPAGE) {
92 // if no answers use either of the two jumps
97 * Interprets the LESSON_UNSEENBRANCHPAGE jump.
99 * will return the pageid of a random unseen page that is within a branch
101 * @param lesson $lesson
102 * @param int $userid Id of the user.
103 * @param int $pageid Id of the page from which we are jumping.
104 * @return int Id of the next page.
106 function lesson_unseen_question_jump($lesson, $user, $pageid) {
109 // get the number of retakes
110 if (!$retakes = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$user))) {
114 // get all the lesson_attempts aka what the user has seen
115 if ($viewedpages = $DB->get_records("lesson_attempts", array("lessonid"=>$lesson->id, "userid"=>$user, "retry"=>$retakes), "timeseen DESC")) {
116 foreach($viewedpages as $viewed) {
117 $seenpages[] = $viewed->pageid;
120 $seenpages = array();
123 // get the lesson pages
124 $lessonpages = $lesson->load_all_pages();
126 if ($pageid == LESSON_UNSEENBRANCHPAGE) { // this only happens when a student leaves in the middle of an unseen question within a branch series
127 $pageid = $seenpages[0]; // just change the pageid to the last page viewed inside the branch table
130 // go up the pages till branch table
131 while ($pageid != 0) { // this condition should never be satisfied... only happens if there are no branch tables above this page
132 if ($lessonpages[$pageid]->qtype == LESSON_PAGE_BRANCHTABLE) {
135 $pageid = $lessonpages[$pageid]->prevpageid;
138 $pagesinbranch = $this->get_sub_pages_of($pageid, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
140 // this foreach loop stores all the pages that are within the branch table but are not in the $seenpages array
142 foreach($pagesinbranch as $page) {
143 if (!in_array($page->id, $seenpages)) {
144 $unseen[] = $page->id;
148 if(count($unseen) == 0) {
149 if(isset($pagesinbranch)) {
150 $temp = end($pagesinbranch);
151 $nextpage = $temp->nextpageid; // they have seen all the pages in the branch, so go to EOB/next branch table/EOL
153 // there are no pages inside the branch, so return the next page
154 $nextpage = $lessonpages[$pageid]->nextpageid;
156 if ($nextpage == 0) {
162 return $unseen[rand(0, count($unseen)-1)]; // returns a random page id for the next page
167 * Handles the unseen branch table jump.
169 * @param lesson $lesson
170 * @param int $userid User id.
171 * @return int Will return the page id of a branch table or end of lesson
173 function lesson_unseen_branch_jump($lesson, $userid) {
176 if (!$retakes = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$userid))) {
180 $params = array ("lessonid" => $lesson->id, "userid" => $userid, "retry" => $retakes);
181 if (!$seenbranches = $DB->get_records_select("lesson_branch", "lessonid = :lessonid AND userid = :userid AND retry = :retry", $params,
183 print_error('cannotfindrecords', 'lesson');
186 // get the lesson pages
187 $lessonpages = $lesson->load_all_pages();
189 // this loads all the viewed branch tables into $seen until it finds the branch table with the flag
190 // which is the branch table that starts the unseenbranch function
192 foreach ($seenbranches as $seenbranch) {
193 if (!$seenbranch->flag) {
194 $seen[$seenbranch->pageid] = $seenbranch->pageid;
196 $start = $seenbranch->pageid;
200 // this function searches through the lesson pages to find all the branch tables
201 // that follow the flagged branch table
202 $pageid = $lessonpages[$start]->nextpageid; // move down from the flagged branch table
203 while ($pageid != 0) { // grab all of the branch table till eol
204 if ($lessonpages[$pageid]->qtype == LESSON_PAGE_BRANCHTABLE) {
205 $branchtables[] = $lessonpages[$pageid]->id;
207 $pageid = $lessonpages[$pageid]->nextpageid;
210 foreach ($branchtables as $branchtable) {
211 // load all of the unseen branch tables into unseen
212 if (!array_key_exists($branchtable, $seen)) {
213 $unseen[] = $branchtable;
216 if (count($unseen) > 0) {
217 return $unseen[rand(0, count($unseen)-1)]; // returns a random page id for the next page
219 return LESSON_EOL; // has viewed all of the branch tables
224 * Handles the random jump between a branch table and end of branch or end of lesson (LESSON_RANDOMPAGE).
226 * @param lesson $lesson
227 * @param int $pageid The id of the page that we are jumping from (?)
228 * @return int The pageid of a random page that is within a branch table
230 function lesson_random_question_jump($lesson, $pageid) {
233 // get the lesson pages
234 $params = array ("lessonid" => $lesson->id);
235 if (!$lessonpages = $DB->get_records_select("lesson_pages", "lessonid = :lessonid", $params)) {
236 print_error('cannotfindpages', 'lesson');
239 // go up the pages till branch table
240 while ($pageid != 0) { // this condition should never be satisfied... only happens if there are no branch tables above this page
242 if ($lessonpages[$pageid]->qtype == LESSON_PAGE_BRANCHTABLE) {
245 $pageid = $lessonpages[$pageid]->prevpageid;
248 // get the pages within the branch
249 $pagesinbranch = $this->get_sub_pages_of($pageid, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
251 if(count($pagesinbranch) == 0) {
252 // there are no pages inside the branch, so return the next page
253 return $lessonpages[$pageid]->nextpageid;
255 return $pagesinbranch[rand(0, count($pagesinbranch)-1)]->id; // returns a random page id for the next page
260 * Calculates a user's grade for a lesson.
262 * @param object $lesson The lesson that the user is taking.
263 * @param int $retries The attempt number.
264 * @param int $userid Id of the user (optional, default current user).
265 * @return object { nquestions => number of questions answered
266 attempts => number of question attempts
267 total => max points possible
268 earned => points earned by student
269 grade => calculated percentage grade
270 nmanual => number of manually graded questions
271 manualpoints => point value for manually graded questions }
273 function lesson_grade($lesson, $ntries, $userid = 0) {
276 if (empty($userid)) {
280 // Zero out everything
291 $params = array ("lessonid" => $lesson->id, "userid" => $userid, "retry" => $ntries);
292 if ($useranswers = $DB->get_records_select("lesson_attempts", "lessonid = :lessonid AND
293 userid = :userid AND retry = :retry", $params, "timeseen")) {
294 // group each try with its page
295 $attemptset = array();
296 foreach ($useranswers as $useranswer) {
297 $attemptset[$useranswer->pageid][] = $useranswer;
300 // Drop all attempts that go beyond max attempts for the lesson
301 foreach ($attemptset as $key => $set) {
302 $attemptset[$key] = array_slice($set, 0, $lesson->maxattempts);
305 // get only the pages and their answers that the user answered
306 list($usql, $parameters) = $DB->get_in_or_equal(array_keys($attemptset));
307 array_unshift($parameters, $lesson->id);
308 $pages = $DB->get_records_select("lesson_pages", "lessonid = ? AND id $usql", $parameters);
309 $answers = $DB->get_records_select("lesson_answers", "lessonid = ? AND pageid $usql", $parameters);
311 // Number of pages answered
312 $nquestions = count($pages);
314 foreach ($attemptset as $attempts) {
315 $page = lesson_page::load($pages[end($attempts)->pageid], $lesson);
316 if ($lesson->custom) {
317 $attempt = end($attempts);
318 // If essay question, handle it, otherwise add to score
319 if ($page->requires_manual_grading()) {
320 $earned += $page->earned_score($answers, $attempt);
322 $manualpoints += $answers[$attempt->answerid]->score;
323 } else if (!empty($attempt->answerid)) {
324 $earned += $page->earned_score($answers, $attempt);
327 foreach ($attempts as $attempt) {
328 $earned += $attempt->correct;
330 $attempt = end($attempts); // doesn't matter which one
331 // If essay question, increase numbers
332 if ($page->requires_manual_grading()) {
337 // Number of times answered
338 $nviewed += count($attempts);
341 if ($lesson->custom) {
342 $bestscores = array();
343 // Find the highest possible score per page to get our total
344 foreach ($answers as $answer) {
345 if(!isset($bestscores[$answer->pageid])) {
346 $bestscores[$answer->pageid] = $answer->score;
347 } else if ($bestscores[$answer->pageid] < $answer->score) {
348 $bestscores[$answer->pageid] = $answer->score;
351 $total = array_sum($bestscores);
353 // Check to make sure the student has answered the minimum questions
354 if ($lesson->minquestions and $nquestions < $lesson->minquestions) {
355 // Nope, increase number viewed by the amount of unanswered questions
356 $total = $nviewed + ($lesson->minquestions - $nquestions);
363 if ($total) { // not zero
364 $thegrade = round(100 * $earned / $total, 5);
367 // Build the grade information object
368 $gradeinfo = new stdClass;
369 $gradeinfo->nquestions = $nquestions;
370 $gradeinfo->attempts = $nviewed;
371 $gradeinfo->total = $total;
372 $gradeinfo->earned = $earned;
373 $gradeinfo->grade = $thegrade;
374 $gradeinfo->nmanual = $nmanual;
375 $gradeinfo->manualpoints = $manualpoints;
381 * Determines if a user can view the left menu. The determining factor
382 * is whether a user has a grade greater than or equal to the lesson setting
385 * @param object $lesson Lesson object of the current lesson
386 * @return boolean 0 if the user cannot see, or $lesson->displayleft to keep displayleft unchanged
388 function lesson_displayleftif($lesson) {
389 global $CFG, $USER, $DB;
391 if (!empty($lesson->displayleftif)) {
392 // get the current user's max grade for this lesson
393 $params = array ("userid" => $USER->id, "lessonid" => $lesson->id);
394 if ($maxgrade = $DB->get_record_sql('SELECT userid, MAX(grade) AS maxgrade FROM {lesson_grades} WHERE userid = :userid AND lessonid = :lessonid GROUP BY userid', $params)) {
395 if ($maxgrade->maxgrade < $lesson->displayleftif) {
396 return 0; // turn off the displayleft
399 return 0; // no grades
403 // if we get to here, keep the original state of displayleft lesson setting
404 return $lesson->displayleft;
412 * @return unknown_type
414 function lesson_add_pretend_blocks($page, $cm, $lesson, $timer = null) {
415 $bc = lesson_menu_block_contents($cm->id, $lesson);
417 $regions = $page->blocks->get_regions();
418 $firstregion = reset($regions);
419 $page->blocks->add_pretend_block($bc, $firstregion);
422 $bc = lesson_mediafile_block_contents($cm->id, $lesson);
424 $page->blocks->add_pretend_block($bc, $page->blocks->get_default_region());
427 if (!empty($timer)) {
428 $bc = lesson_clock_block_contents($cm->id, $lesson, $timer, $page);
430 $page->blocks->add_pretend_block($bc, $page->blocks->get_default_region());
436 * If there is a media file associated with this
437 * lesson, return a block_contents that displays it.
439 * @param int $cmid Course Module ID for this lesson
440 * @param object $lesson Full lesson record object
441 * @return block_contents
443 function lesson_mediafile_block_contents($cmid, $lesson) {
445 if (empty($lesson->mediafile) && empty($lesson->mediafileid)) {
450 $options['menubar'] = 0;
451 $options['location'] = 0;
452 $options['left'] = 5;
454 $options['scrollbars'] = 1;
455 $options['resizable'] = 1;
456 $options['width'] = $lesson->mediawidth;
457 $options['height'] = $lesson->mediaheight;
459 $link = new moodle_url('/mod/lesson/mediafile.php?id='.$cmid);
460 $action = new popup_action('click', $link, 'lessonmediafile', $options);
461 $content = $OUTPUT->action_link($link, get_string('mediafilepopup', 'lesson'), $action, array('title'=>get_string('mediafilepopup', 'lesson')));
463 $bc = new block_contents();
464 $bc->title = get_string('linkedmedia', 'lesson');
465 $bc->attributes['class'] = 'mediafile';
466 $bc->content = $content;
472 * If a timed lesson and not a teacher, then
473 * return a block_contents containing the clock.
475 * @param int $cmid Course Module ID for this lesson
476 * @param object $lesson Full lesson record object
477 * @param object $timer Full timer record object
478 * @return block_contents
480 function lesson_clock_block_contents($cmid, $lesson, $timer, $page) {
481 // Display for timed lessons and for students only
482 $context = get_context_instance(CONTEXT_MODULE, $cmid);
483 if(!$lesson->timed || has_capability('mod/lesson:manage', $context)) {
487 $content = '<div class="jshidewhenenabled">';
488 $content .= $lesson->time_remaining($timer->starttime);
489 $content .= '</div>';
491 $clocksettings = array('starttime'=>$timer->starttime, 'servertime'=>time(),'testlength'=>($lesson->maxtime * 60));
492 $page->requires->data_for_js('clocksettings', $clocksettings);
493 $page->requires->js('/mod/lesson/timer.js');
494 $page->requires->js_function_call('show_clock');
496 $bc = new block_contents();
497 $bc->title = get_string('timeremaining', 'lesson');
498 $bc->attributes['class'] = 'clock block';
499 $bc->content = $content;
505 * If left menu is turned on, then this will
506 * print the menu in a block
508 * @param int $cmid Course Module ID for this lesson
509 * @param lesson $lesson Full lesson record object
512 function lesson_menu_block_contents($cmid, $lesson) {
515 if (!$lesson->displayleft) {
519 $pages = $lesson->load_all_pages();
520 foreach ($pages as $page) {
521 if ((int)$page->prevpageid === 0) {
526 $currentpageid = optional_param('pageid', $pageid, PARAM_INT);
528 if (!$pageid || !$pages) {
532 $content = '<a href="#maincontent" class="skip">'.get_string('skip', 'lesson')."</a>\n<div class=\"menuwrapper\">\n<ul>\n";
534 while ($pageid != 0) {
535 $page = $pages[$pageid];
537 // Only process branch tables with display turned on
538 if ($page->displayinmenublock && $page->display) {
539 if ($page->id == $currentpageid) {
540 $content .= '<li class="selected">'.format_string($page->title,true)."</li>\n";
542 $content .= "<li class=\"notselected\"><a href=\"$CFG->wwwroot/mod/lesson/view.php?id=$cmid&pageid=$page->id\">".format_string($page->title,true)."</a></li>\n";
546 $pageid = $page->nextpageid;
548 $content .= "</ul>\n</div>\n";
550 $bc = new block_contents();
551 $bc->title = get_string('lessonmenu', 'lesson');
552 $bc->attributes['class'] = 'menu block';
553 $bc->content = $content;
559 * Adds header buttons to the page for the lesson
562 * @param object $context
563 * @param bool $extraeditbuttons
564 * @param int $lessonpageid
566 function lesson_add_header_buttons($cm, $context, $extraeditbuttons=false, $lessonpageid=null) {
567 global $CFG, $PAGE, $OUTPUT;
568 if (has_capability('mod/lesson:edit', $context) && $extraeditbuttons) {
569 if ($lessonpageid === null) {
570 print_error('invalidpageid', 'lesson');
572 if (!empty($lessonpageid) && $lessonpageid != LESSON_EOL) {
573 $url = new moodle_url('/mod/lesson/editpage.php', array('id'=>$cm->id, 'pageid'=>$lessonpageid, 'edit'=>1));
574 $PAGE->set_button($OUTPUT->single_button($url, get_string('editpagecontent', 'lesson')));
580 * This is a function used to detect media types and generate html code.
582 * @global object $CFG
583 * @global object $PAGE
584 * @param object $lesson
585 * @param object $context
586 * @return string $code the html code of media
588 function lesson_get_media_html($lesson, $context) {
589 global $CFG, $PAGE, $OUTPUT;
590 require_once("$CFG->libdir/resourcelib.php");
592 // get the media file link
593 $url = moodle_url::make_pluginfile_url($context->id, 'mod_lesson', 'mediafile', $lesson->timemodified, '/', $lesson->mediafile);
594 $title = $lesson->mediafile;
596 $clicktoopen = html_writer::link(new moodle_url($url), get_string('download'));
598 $mimetype = resourcelib_guess_url_mimetype($url);
600 // find the correct type and print it out
601 if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) { // It's an image
602 $code = resourcelib_embed_image($url, $title);
604 } else if ($mimetype == 'audio/mp3') {
606 $code = resourcelib_embed_mp3($url, $title, $clicktoopen);
608 } else if ($mimetype == 'video/x-flv') {
610 $code = resourcelib_embed_flashvideo($url, $title, $clicktoopen);
612 } else if ($mimetype == 'application/x-shockwave-flash') {
614 $code = resourcelib_embed_flash($url, $title, $clicktoopen);
616 } else if (substr($mimetype, 0, 10) == 'video/x-ms') {
617 // Windows Media Player file
618 $code = resourcelib_embed_mediaplayer($url, $title, $clicktoopen);
620 } else if ($mimetype == 'video/quicktime') {
622 $code = resourcelib_embed_quicktime($url, $title, $clicktoopen);
624 } else if ($mimetype == 'video/mpeg') {
626 $code = resourcelib_embed_mpeg($url, $title, $clicktoopen);
628 } else if ($mimetype == 'audio/x-pn-realaudio-plugin') {
630 $code = resourcelib_embed_real($url, $title, $clicktoopen);
633 // anything else - just try object tag enlarged as much as possible
634 $code = resourcelib_embed_general($url, $title, $clicktoopen, $mimetype);
642 * Abstract class that page type's MUST inherit from.
644 * This is the abstract class that ALL add page type forms must extend.
645 * You will notice that all but two of the methods this class contains are final.
646 * Essentially the only thing that extending classes can do is extend custom_definition.
647 * OR if it has a special requirement on creation it can extend construction_override
650 * @copyright 2009 Sam Hemelryk
651 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
653 abstract class lesson_add_page_form_base extends moodleform {
656 * This is the classic define that is used to identify this pagetype.
657 * Will be one of LESSON_*
663 * The simple string that describes the page type e.g. truefalse, multichoice
669 * An array of options used in the htmleditor
672 protected $editoroptions = array();
675 * True if this is a standard page of false if it does something special.
676 * Questions are standard pages, branch tables are not
679 protected $standard = true;
682 * Each page type can and should override this to add any custom elements to
683 * the basic form that they want
685 public function custom_definition() {}
688 * Used to determine if this is a standard page or a special page
691 public final function is_standard() {
692 return (bool)$this->standard;
696 * Add the required basic elements to the form.
698 * This method adds the basic elements to the form including title and contents
699 * and then calls custom_definition();
701 public final function definition() {
702 $mform = $this->_form;
703 $editoroptions = $this->_customdata['editoroptions'];
705 $mform->addElement('header', 'qtypeheading', get_string('addaquestionpage', 'lesson', get_string($this->qtypestring, 'lesson')));
707 $mform->addElement('hidden', 'id');
708 $mform->setType('id', PARAM_INT);
710 $mform->addElement('hidden', 'pageid');
711 $mform->setType('pageid', PARAM_INT);
713 if ($this->standard === true) {
714 $mform->addElement('hidden', 'qtype');
715 $mform->setType('qtype', PARAM_SAFEDIR);
717 $mform->addElement('text', 'title', get_string('pagetitle', 'lesson'), array('size'=>70));
718 $mform->setType('title', PARAM_TEXT);
719 $mform->addRule('title', get_string('required'), 'required', null, 'client');
721 $this->editoroptions = array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$this->_customdata['maxbytes']);
722 $mform->addElement('editor', 'contents_editor', get_string('pagecontents', 'lesson'), null, $this->editoroptions);
723 $mform->setType('contents_editor', PARAM_RAW);
724 $mform->addRule('contents_editor', get_string('required'), 'required', null, 'client');
727 $this->custom_definition();
729 if ($this->_customdata['edit'] === true) {
730 $mform->addElement('hidden', 'edit', 1);
731 $this->add_action_buttons(get_string('cancel'), get_string('savepage', 'lesson'));
733 $this->add_action_buttons(get_string('cancel'), get_string('addaquestionpage', 'lesson'));
738 * Convenience function: Adds a jumpto select element
740 * @param string $name
741 * @param string|null $label
742 * @param int $selected The page to select by default
744 protected final function add_jumpto($name, $label=null, $selected=LESSON_NEXTPAGE) {
745 $title = get_string("jump", "lesson");
746 if ($label === null) {
750 $name = "jumpto[$name]";
752 $this->_form->addElement('select', $name, $label, $this->_customdata['jumpto']);
753 $this->_form->setDefault($name, $selected);
754 $this->_form->addHelpButton($name, 'jumps', 'lesson');
758 * Convenience function: Adds a score input element
760 * @param string $name
761 * @param string|null $label
762 * @param mixed $value The default value
764 protected final function add_score($name, $label=null, $value=null) {
765 if ($label === null) {
766 $label = get_string("score", "lesson");
769 $name = "score[$name]";
771 $this->_form->addElement('text', $name, $label, array('size'=>5));
772 if ($value !== null) {
773 $this->_form->setDefault($name, $value);
778 * Convenience function: Adds an answer editor
780 * @param int $count The count of the element to add
781 * @param string $label, NULL means default
782 * @param bool $required
785 protected final function add_answer($count, $label = NULL, $required = false) {
786 if ($label === NULL) {
787 $label = get_string('answer', 'lesson');
789 $this->_form->addElement('editor', 'answer_editor['.$count.']', $label, array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
790 $this->_form->setDefault('answer_editor['.$count.']', array('text'=>'', 'format'=>FORMAT_MOODLE));
792 $this->_form->addRule('answer_editor['.$count.']', get_string('required'), 'required', null, 'client');
796 * Convenience function: Adds an response editor
798 * @param int $count The count of the element to add
799 * @param string $label, NULL means default
800 * @param bool $required
803 protected final function add_response($count, $label = NULL, $required = false) {
804 if ($label === NULL) {
805 $label = get_string('response', 'lesson');
807 $this->_form->addElement('editor', 'response_editor['.$count.']', $label, array('rows'=>'4', 'columns'=>'80'), array('noclean'=>true));
808 $this->_form->setDefault('response_editor['.$count.']', array('text'=>'', 'format'=>FORMAT_MOODLE));
810 $this->_form->addRule('response_editor['.$count.']', get_string('required'), 'required', null, 'client');
815 * A function that gets called upon init of this object by the calling script.
817 * This can be used to process an immediate action if required. Currently it
818 * is only used in special cases by non-standard page types.
822 public function construction_override() {
830 * Class representation of a lesson
832 * This class is used the interact with, and manage a lesson once instantiated.
833 * If you need to fetch a lesson object you can do so by calling
836 * lesson::load($lessonid);
838 * $lessonrecord = $DB->get_record('lesson', $lessonid);
839 * $lesson = new lesson($lessonrecord);
842 * The class itself extends lesson_base as all classes within the lesson module should
844 * These properties are from the database
845 * @property int $id The id of this lesson
846 * @property int $course The ID of the course this lesson belongs to
847 * @property string $name The name of this lesson
848 * @property int $practice Flag to toggle this as a practice lesson
849 * @property int $modattempts Toggle to allow the user to go back and review answers
850 * @property int $usepassword Toggle the use of a password for entry
851 * @property string $password The password to require users to enter
852 * @property int $dependency ID of another lesson this lesson is dependent on
853 * @property string $conditions Conditions of the lesson dependency
854 * @property int $grade The maximum grade a user can achieve (%)
855 * @property int $custom Toggle custom scoring on or off
856 * @property int $ongoing Toggle display of an ongoing score
857 * @property int $usemaxgrade How retakes are handled (max=1, mean=0)
858 * @property int $maxanswers The max number of answers or branches
859 * @property int $maxattempts The maximum number of attempts a user can record
860 * @property int $review Toggle use or wrong answer review button
861 * @property int $nextpagedefault Override the default next page
862 * @property int $feedback Toggles display of default feedback
863 * @property int $minquestions Sets a minimum value of pages seen when calculating grades
864 * @property int $maxpages Maximum number of pages this lesson can contain
865 * @property int $retake Flag to allow users to retake a lesson
866 * @property int $activitylink Relate this lesson to another lesson
867 * @property string $mediafile File to pop up to or webpage to display
868 * @property int $mediaheight Sets the height of the media file popup
869 * @property int $mediawidth Sets the width of the media file popup
870 * @property int $mediaclose Toggle display of a media close button
871 * @property int $slideshow Flag for whether branch pages should be shown as slideshows
872 * @property int $width Width of slideshow
873 * @property int $height Height of slideshow
874 * @property string $bgcolor Background colour of slideshow
875 * @property int $displayleft Display a left menu
876 * @property int $displayleftif Sets the condition on which the left menu is displayed
877 * @property int $progressbar Flag to toggle display of a lesson progress bar
878 * @property int $highscores Flag to toggle collection of high scores
879 * @property int $maxhighscores Number of high scores to limit to
880 * @property int $available Timestamp of when this lesson becomes available
881 * @property int $deadline Timestamp of when this lesson is no longer available
882 * @property int $timemodified Timestamp when lesson was last modified
884 * These properties are calculated
885 * @property int $firstpageid Id of the first page of this lesson (prevpageid=0)
886 * @property int $lastpageid Id of the last page of this lesson (nextpageid=0)
888 * @copyright 2009 Sam Hemelryk
889 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
891 class lesson extends lesson_base {
894 * The id of the first page (where prevpageid = 0) gets set and retrieved by
895 * {@see get_firstpageid()} by directly calling <code>$lesson->firstpageid;</code>
898 protected $firstpageid = null;
900 * The id of the last page (where nextpageid = 0) gets set and retrieved by
901 * {@see get_lastpageid()} by directly calling <code>$lesson->lastpageid;</code>
904 protected $lastpageid = null;
906 * An array used to cache the pages associated with this lesson after the first
907 * time they have been loaded.
908 * A note to developers: If you are going to be working with MORE than one or
909 * two pages from a lesson you should probably call {@see $lesson->load_all_pages()}
910 * in order to save excess database queries.
911 * @var array An array of lesson_page objects
913 protected $pages = array();
915 * Flag that gets set to true once all of the pages associated with the lesson
919 protected $loadedallpages = false;
922 * Simply generates a lesson object given an array/object of properties
923 * Overrides {@see lesson_base->create()}
925 * @param object|array $properties
928 public static function create($properties) {
929 return new lesson($properties);
933 * Generates a lesson object from the database given its id
935 * @param int $lessonid
938 public static function load($lessonid) {
939 if (!$lesson = $DB->get_record('lesson', array('id' => $lessonid))) {
940 print_error('invalidcoursemodule');
942 return new lesson($lesson);
946 * Deletes this lesson from the database
948 public function delete() {
950 require_once($CFG->libdir.'/gradelib.php');
951 require_once($CFG->dirroot.'/calendar/lib.php');
953 $DB->delete_records("lesson", array("id"=>$this->properties->id));;
954 $DB->delete_records("lesson_pages", array("lessonid"=>$this->properties->id));
955 $DB->delete_records("lesson_answers", array("lessonid"=>$this->properties->id));
956 $DB->delete_records("lesson_attempts", array("lessonid"=>$this->properties->id));
957 $DB->delete_records("lesson_grades", array("lessonid"=>$this->properties->id));
958 $DB->delete_records("lesson_timer", array("lessonid"=>$this->properties->id));
959 $DB->delete_records("lesson_branch", array("lessonid"=>$this->properties->id));
960 $DB->delete_records("lesson_high_scores", array("lessonid"=>$this->properties->id));
961 if ($events = $DB->get_records('event', array("modulename"=>'lesson', "instance"=>$this->properties->id))) {
962 foreach($events as $event) {
963 $event = calendar_event::load($event);
968 grade_update('mod/lesson', $this->properties->course, 'mod', 'lesson', $this->properties->id, 0, NULL, array('deleted'=>1));
973 * Fetches messages from the session that may have been set in previous page
977 * // Do not call this method directly instead use
983 protected function get_messages() {
987 if (!empty($SESSION->lesson_messages) && is_array($SESSION->lesson_messages) && array_key_exists($this->properties->id, $SESSION->lesson_messages)) {
988 $messages = $SESSION->lesson_messages[$this->properties->id];
989 unset($SESSION->lesson_messages[$this->properties->id]);
996 * Get all of the attempts for the current user.
998 * @param int $retries
999 * @param bool $correct Optional: only fetch correct attempts
1000 * @param int $pageid Optional: only fetch attempts at the given page
1001 * @param int $userid Optional: defaults to the current user if not set
1002 * @return array|false
1004 public function get_attempts($retries, $correct=false, $pageid=null, $userid=null) {
1006 $params = array("lessonid"=>$this->properties->id, "userid"=>$userid, "retry"=>$retries);
1008 $params['correct'] = 1;
1010 if ($pageid !== null) {
1011 $params['pageid'] = $pageid;
1013 if ($userid === null) {
1014 $params['userid'] = $USER->id;
1016 return $DB->get_records('lesson_attempts', $params, 'timeseen ASC');
1020 * Returns the first page for the lesson or false if there isn't one.
1022 * This method should be called via the magic method __get();
1024 * $firstpage = $lesson->firstpage;
1027 * @return lesson_page|bool Returns the lesson_page specialised object or false
1029 protected function get_firstpage() {
1030 $pages = $this->load_all_pages();
1031 if (count($pages) > 0) {
1032 foreach ($pages as $page) {
1033 if ((int)$page->prevpageid === 0) {
1042 * Returns the last page for the lesson or false if there isn't one.
1044 * This method should be called via the magic method __get();
1046 * $lastpage = $lesson->lastpage;
1049 * @return lesson_page|bool Returns the lesson_page specialised object or false
1051 protected function get_lastpage() {
1052 $pages = $this->load_all_pages();
1053 if (count($pages) > 0) {
1054 foreach ($pages as $page) {
1055 if ((int)$page->nextpageid === 0) {
1064 * Returns the id of the first page of this lesson. (prevpageid = 0)
1067 protected function get_firstpageid() {
1069 if ($this->firstpageid == null) {
1070 if (!$this->loadedallpages) {
1071 $firstpageid = $DB->get_field('lesson_pages', 'id', array('lessonid'=>$this->properties->id, 'prevpageid'=>0));
1072 if (!$firstpageid) {
1073 print_error('cannotfindfirstpage', 'lesson');
1075 $this->firstpageid = $firstpageid;
1077 $firstpage = $this->get_firstpage();
1078 $this->firstpageid = $firstpage->id;
1081 return $this->firstpageid;
1085 * Returns the id of the last page of this lesson. (nextpageid = 0)
1088 public function get_lastpageid() {
1090 if ($this->lastpageid == null) {
1091 if (!$this->loadedallpages) {
1092 $lastpageid = $DB->get_field('lesson_pages', 'id', array('lessonid'=>$this->properties->id, 'nextpageid'=>0));
1094 print_error('cannotfindlastpage', 'lesson');
1096 $this->lastpageid = $lastpageid;
1098 $lastpageid = $this->get_lastpage();
1099 $this->lastpageid = $lastpageid->id;
1103 return $this->lastpageid;
1107 * Gets the next page to display after the one that is provided.
1108 * @param int $nextpageid
1111 public function get_next_page($nextpageid) {
1113 $allpages = $this->load_all_pages();
1114 if ($this->properties->nextpagedefault) {
1115 // in Flash Card mode...first get number of retakes
1118 if ($this->properties->nextpagedefault == LESSON_UNSEENPAGE) {
1119 foreach ($allpages as $nextpage) {
1120 if (!$DB->count_records("lesson_attempts", array("pageid"=>$nextpage->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
1125 } elseif ($this->properties->nextpagedefault == LESSON_UNANSWEREDPAGE) {
1126 foreach ($allpages as $nextpage) {
1127 if (!$DB->count_records("lesson_attempts", array('pageid'=>$nextpage->id, 'userid'=>$USER->id, 'correct'=>1, 'retry'=>$nretakes))) {
1134 if ($this->properties->maxpages) {
1135 // check number of pages viewed (in the lesson)
1136 $nretakes = $DB->count_records("lesson_grades", array("lessonid"=>$this->properties->id, "userid"=>$USER->id));
1137 if ($DB->count_records("lesson_attempts", array("lessonid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes)) >= $this->properties->maxpages) {
1144 // In a normal lesson mode
1145 foreach ($allpages as $nextpage) {
1146 if ((int)$nextpage->id===(int)$nextpageid) {
1154 * Sets a message against the session for this lesson that will displayed next
1155 * time the lesson processes messages
1157 * @param string $message
1158 * @param string $class
1159 * @param string $align
1162 public function add_message($message, $class="notifyproblem", $align='center') {
1165 if (empty($SESSION->lesson_messages) || !is_array($SESSION->lesson_messages)) {
1166 $SESSION->lesson_messages = array();
1167 $SESSION->lesson_messages[$this->properties->id] = array();
1168 } else if (!array_key_exists($this->properties->id, $SESSION->lesson_messages)) {
1169 $SESSION->lesson_messages[$this->properties->id] = array();
1172 $SESSION->lesson_messages[$this->properties->id][] = array($message, $class, $align);
1178 * Check if the lesson is accessible at the present time
1179 * @return bool True if the lesson is accessible, false otherwise
1181 public function is_accessible() {
1182 $available = $this->properties->available;
1183 $deadline = $this->properties->deadline;
1184 return (($available == 0 || time() >= $available) && ($deadline == 0 || time() < $deadline));
1188 * Starts the lesson time for the current user
1189 * @return bool Returns true
1191 public function start_timer() {
1193 $USER->startlesson[$this->properties->id] = true;
1194 $startlesson = new stdClass;
1195 $startlesson->lessonid = $this->properties->id;
1196 $startlesson->userid = $USER->id;
1197 $startlesson->starttime = time();
1198 $startlesson->lessontime = time();
1199 $DB->insert_record('lesson_timer', $startlesson);
1200 if ($this->properties->timed) {
1201 $this->add_message(get_string('maxtimewarning', 'lesson', $this->properties->maxtime), 'center');
1207 * Updates the timer to the current time and returns the new timer object
1208 * @param bool $restart If set to true the timer is restarted
1209 * @param bool $continue If set to true AND $restart=true then the timer
1210 * will continue from a previous attempt
1211 * @return stdClass The new timer
1213 public function update_timer($restart=false, $continue=false) {
1216 // get time information for this user
1217 if (!$timer = $DB->get_records('lesson_timer', array ("lessonid" => $this->properties->id, "userid" => $USER->id), 'starttime DESC', '*', 0, 1)) {
1218 print_error('cannotfindtimer', 'lesson');
1220 $timer = current($timer); // this will get the latest start time record
1225 // continue a previous test, need to update the clock (think this option is disabled atm)
1226 $timer->starttime = time() - ($timer->lessontime - $timer->starttime);
1228 // starting over, so reset the clock
1229 $timer->starttime = time();
1233 $timer->lessontime = time();
1234 $DB->update_record('lesson_timer', $timer);
1239 * Updates the timer to the current time then stops it by unsetting the user var
1240 * @return bool Returns true
1242 public function stop_timer() {
1244 unset($USER->startlesson[$this->properties->id]);
1245 return $this->update_timer(false, false);
1249 * Checks to see if the lesson has pages
1251 public function has_pages() {
1253 $pagecount = $DB->count_records('lesson_pages', array('lessonid'=>$this->properties->id));
1254 return ($pagecount>0);
1258 * Returns the link for the related activity
1259 * @return array|false
1261 public function link_for_activitylink() {
1263 $module = $DB->get_record('course_modules', array('id' => $this->properties->activitylink));
1265 $modname = $DB->get_field('modules', 'name', array('id' => $module->module));
1267 $instancename = $DB->get_field($modname, 'name', array('id' => $module->instance));
1268 if ($instancename) {
1269 return html_writer::link(new moodle_url('/mod/'.$modname.'/view.php', array('id'=>$this->properties->activitylink)),
1270 get_string('returnto', 'lesson', get_string('activitylinkname', 'lesson', $instancename)),
1271 array('class'=>'centerpadded lessonbutton standardbutton'));
1279 * Loads the requested page.
1281 * This function will return the requested page id as either a specialised
1282 * lesson_page object OR as a generic lesson_page.
1283 * If the page has been loaded previously it will be returned from the pages
1284 * array, otherwise it will be loaded from the database first
1286 * @param int $pageid
1287 * @return lesson_page A lesson_page object or an object that extends it
1289 public function load_page($pageid) {
1290 if (!array_key_exists($pageid, $this->pages)) {
1291 $manager = lesson_page_type_manager::get($this);
1292 $this->pages[$pageid] = $manager->load_page($pageid, $this);
1294 return $this->pages[$pageid];
1298 * Loads ALL of the pages for this lesson
1300 * @return array An array containing all pages from this lesson
1302 public function load_all_pages() {
1303 if (!$this->loadedallpages) {
1304 $manager = lesson_page_type_manager::get($this);
1305 $this->pages = $manager->load_all_pages($this);
1306 $this->loadedallpages = true;
1308 return $this->pages;
1312 * Determines if a jumpto value is correct or not.
1314 * returns true if jumpto page is (logically) after the pageid page or
1315 * if the jumpto value is a special value. Returns false in all other cases.
1317 * @param int $pageid Id of the page from which you are jumping from.
1318 * @param int $jumpto The jumpto number.
1319 * @return boolean True or false after a series of tests.
1321 public function jumpto_is_correct($pageid, $jumpto) {
1324 // first test the special values
1328 } elseif ($jumpto == LESSON_NEXTPAGE) {
1330 } elseif ($jumpto == LESSON_UNSEENBRANCHPAGE) {
1332 } elseif ($jumpto == LESSON_RANDOMPAGE) {
1334 } elseif ($jumpto == LESSON_CLUSTERJUMP) {
1336 } elseif ($jumpto == LESSON_EOL) {
1340 $pages = $this->load_all_pages();
1341 $apageid = $pages[$pageid]->nextpageid;
1342 while ($apageid != 0) {
1343 if ($jumpto == $apageid) {
1346 $apageid = $pages[$apageid]->nextpageid;
1352 * Returns the time a user has remaining on this lesson
1353 * @param int $starttime Starttime timestamp
1356 public function time_remaining($starttime) {
1357 $timeleft = $starttime + $this->maxtime * 60 - time();
1358 $hours = floor($timeleft/3600);
1359 $timeleft = $timeleft - ($hours * 3600);
1360 $minutes = floor($timeleft/60);
1361 $secs = $timeleft - ($minutes * 60);
1363 if ($minutes < 10) {
1364 $minutes = "0$minutes";
1371 $output[] = $minutes;
1373 $output = implode(':', $output);
1378 * Interprets LESSON_CLUSTERJUMP jumpto value.
1380 * This will select a page randomly
1381 * and the page selected will be inbetween a cluster page and end of clutter or end of lesson
1382 * and the page selected will be a page that has not been viewed already
1383 * and if any pages are within a branch table or end of branch then only 1 page within
1384 * the branch table or end of branch will be randomly selected (sub clustering).
1386 * @param int $pageid Id of the current page from which we are jumping from.
1387 * @param int $userid Id of the user.
1388 * @return int The id of the next page.
1390 public function cluster_jump($pageid, $userid=null) {
1393 if ($userid===null) {
1394 $userid = $USER->id;
1396 // get the number of retakes
1397 if (!$retakes = $DB->count_records("lesson_grades", array("lessonid"=>$this->properties->id, "userid"=>$userid))) {
1400 // get all the lesson_attempts aka what the user has seen
1401 $seenpages = array();
1402 if ($attempts = $this->get_attempts($retakes)) {
1403 foreach ($attempts as $attempt) {
1404 $seenpages[$attempt->pageid] = $attempt->pageid;
1409 // get the lesson pages
1410 $lessonpages = $this->load_all_pages();
1411 // find the start of the cluster
1412 while ($pageid != 0) { // this condition should not be satisfied... should be a cluster page
1413 if ($lessonpages[$pageid]->qtype == LESSON_PAGE_CLUSTER) {
1416 $pageid = $lessonpages[$pageid]->prevpageid;
1419 $clusterpages = array();
1420 $clusterpages = $this->get_sub_pages_of($pageid, array(LESSON_PAGE_ENDOFCLUSTER));
1422 foreach ($clusterpages as $key=>$cluster) {
1423 if ($cluster->type !== lesson_page::TYPE_QUESTION) {
1424 unset($clusterpages[$key]);
1425 } elseif ($cluster->is_unseen($seenpages)) {
1426 $unseen[] = $cluster;
1430 if (count($unseen) > 0) {
1431 // it does not contain elements, then use exitjump, otherwise find out next page/branch
1432 $nextpage = $unseen[rand(0, count($unseen)-1)];
1433 if ($nextpage->qtype == LESSON_PAGE_BRANCHTABLE) {
1434 // if branch table, then pick a random page inside of it
1435 $branchpages = $this->get_sub_pages_of($nextpage->id, array(LESSON_PAGE_BRANCHTABLE, LESSON_PAGE_ENDOFBRANCH));
1436 return $branchpages[rand(0, count($branchpages)-1)]->id;
1437 } else { // otherwise, return the page's id
1438 return $nextpage->id;
1441 // seen all there is to see, leave the cluster
1442 if (end($clusterpages)->nextpageid == 0) {
1445 $clusterendid = $pageid;
1446 while ($clusterendid != 0) { // this condition should not be satisfied... should be a cluster page
1447 if ($lessonpages[$clusterendid]->qtype == LESSON_PAGE_CLUSTER) {
1450 $clusterendid = $lessonpages[$clusterendid]->prevpageid;
1452 $exitjump = $DB->get_field("lesson_answers", "jumpto", array("pageid" => $clusterendid, "lessonid" => $this->properties->id));
1453 if ($exitjump == LESSON_NEXTPAGE) {
1454 $exitjump = $lessonpages[$pageid]->nextpageid;
1456 if ($exitjump == 0) {
1458 } else if (in_array($exitjump, array(LESSON_EOL, LESSON_PREVIOUSPAGE))) {
1461 if (!array_key_exists($exitjump, $lessonpages)) {
1463 foreach ($lessonpages as $page) {
1464 if ($page->id === $clusterendid) {
1466 } else if ($page->qtype == LESSON_PAGE_ENDOFCLUSTER) {
1467 $exitjump = $DB->get_field("lesson_answers", "jumpto", array("pageid" => $page->id, "lessonid" => $this->properties->id));
1472 if (!array_key_exists($exitjump, $lessonpages)) {
1482 * Finds all pages that appear to be a subtype of the provided pageid until
1483 * an end point specified within $ends is encountered or no more pages exist
1485 * @param int $pageid
1486 * @param array $ends An array of LESSON_PAGE_* types that signify an end of
1488 * @return array An array of specialised lesson_page objects
1490 public function get_sub_pages_of($pageid, array $ends) {
1491 $lessonpages = $this->load_all_pages();
1492 $pageid = $lessonpages[$pageid]->nextpageid; // move to the first page after the branch table
1496 if ($pageid == 0 || in_array($lessonpages[$pageid]->qtype, $ends)) {
1499 $pages[] = $lessonpages[$pageid];
1500 $pageid = $lessonpages[$pageid]->nextpageid;
1507 * Checks to see if the specified page[id] is a subpage of a type specified in
1508 * the $types array, until either there are no more pages of we find a type
1509 * corresponding to that of a type specified in $ends
1511 * @param int $pageid The id of the page to check
1512 * @param array $types An array of types that would signify this page was a subpage
1513 * @param array $ends An array of types that mean this is not a subpage
1516 public function is_sub_page_of_type($pageid, array $types, array $ends) {
1517 $pages = $this->load_all_pages();
1518 $pageid = $pages[$pageid]->prevpageid; // move up one
1520 array_unshift($ends, 0);
1521 // go up the pages till branch table
1523 if ($pageid==0 || in_array($pages[$pageid]->qtype, $ends)) {
1525 } else if (in_array($pages[$pageid]->qtype, $types)) {
1528 $pageid = $pages[$pageid]->prevpageid;
1535 * Abstract class to provide a core functions to the all lesson classes
1537 * This class should be abstracted by ALL classes with the lesson module to ensure
1538 * that all classes within this module can be interacted with in the same way.
1540 * This class provides the user with a basic properties array that can be fetched
1541 * or set via magic methods, or alternatively by defining methods get_blah() or
1542 * set_blah() within the extending object.
1544 * @copyright 2009 Sam Hemelryk
1545 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1547 abstract class lesson_base {
1550 * An object containing properties
1553 protected $properties;
1557 * @param stdClass $properties
1559 public function __construct($properties) {
1560 $this->properties = (object)$properties;
1564 * Magic property method
1566 * Attempts to call a set_$key method if one exists otherwise falls back
1567 * to simply set the property
1569 * @param string $key
1570 * @param mixed $value
1572 public function __set($key, $value) {
1573 if (method_exists($this, 'set_'.$key)) {
1574 $this->{'set_'.$key}($value);
1576 $this->properties->{$key} = $value;
1582 * Attempts to call a get_$key method to return the property and ralls over
1583 * to return the raw property
1588 public function __get($key) {
1589 if (method_exists($this, 'get_'.$key)) {
1590 return $this->{'get_'.$key}();
1592 return $this->properties->{$key};
1596 * Stupid PHP needs an isset magic method if you use the get magic method and
1597 * still want empty calls to work.... blah ~!
1599 * @param string $key
1602 public function __isset($key) {
1603 if (method_exists($this, 'get_'.$key)) {
1604 $val = $this->{'get_'.$key}();
1605 return !empty($val);
1607 return !empty($this->properties->{$key});
1611 * If overridden should create a new instance, save it in the DB and return it
1613 public static function create() {}
1615 * If overridden should load an instance from the DB and return it
1617 public static function load() {}
1619 * Fetches all of the properties of the object
1622 public function properties() {
1623 return $this->properties;
1629 * Abstract class representation of a page associated with a lesson.
1631 * This class should MUST be extended by all specialised page types defined in
1632 * mod/lesson/pagetypes/.
1633 * There are a handful of abstract methods that need to be defined as well as
1634 * severl methods that can optionally be defined in order to make the page type
1635 * operate in the desired way
1637 * Database properties
1638 * @property int $id The id of this lesson page
1639 * @property int $lessonid The id of the lesson this page belongs to
1640 * @property int $prevpageid The id of the page before this one
1641 * @property int $nextpageid The id of the next page in the page sequence
1642 * @property int $qtype Identifies the page type of this page
1643 * @property int $qoption Used to record page type specific options
1644 * @property int $layout Used to record page specific layout selections
1645 * @property int $display Used to record page specific display selections
1646 * @property int $timecreated Timestamp for when the page was created
1647 * @property int $timemodified Timestamp for when the page was last modified
1648 * @property string $title The title of this page
1649 * @property string $contents The rich content shown to describe the page
1650 * @property int $contentsformat The format of the contents field
1652 * Calculated properties
1653 * @property-read array $answers An array of answers for this page
1654 * @property-read bool $displayinmenublock Toggles display in the left menu block
1655 * @property-read array $jumps An array containing all the jumps this page uses
1656 * @property-read lesson $lesson The lesson this page belongs to
1657 * @property-read int $type The type of the page [question | structure]
1658 * @property-read typeid The unique identifier for the page type
1659 * @property-read typestring The string that describes this page type
1662 * @copyright 2009 Sam Hemelryk
1663 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1665 abstract class lesson_page extends lesson_base {
1668 * A reference to the lesson this page belongs to
1671 protected $lesson = null;
1673 * Contains the answers to this lesson_page once loaded
1676 protected $answers = null;
1678 * This sets the type of the page, can be one of the constants defined below
1681 protected $type = 0;
1684 * Constants used to identify the type of the page
1686 const TYPE_QUESTION = 0;
1687 const TYPE_STRUCTURE = 1;
1690 * This method should return the integer used to identify the page type within
1691 * the database and throughout code. This maps back to the defines used in 1.x
1695 abstract protected function get_typeid();
1697 * This method should return the string that describes the pagetype
1701 abstract protected function get_typestring();
1704 * This method gets called to display the page to the user taking the lesson
1706 * @param object $renderer
1707 * @param object $attempt
1710 abstract public function display($renderer, $attempt);
1713 * Creates a new lesson_page within the database and returns the correct pagetype
1714 * object to use to interact with the new lesson
1718 * @param object $properties
1719 * @param lesson $lesson
1720 * @return lesson_page Specialised object that extends lesson_page
1722 final public static function create($properties, lesson $lesson, $context, $maxbytes) {
1724 $newpage = new stdClass;
1725 $newpage->title = $properties->title;
1726 $newpage->contents = $properties->contents_editor['text'];
1727 $newpage->contentsformat = $properties->contents_editor['format'];
1728 $newpage->lessonid = $lesson->id;
1729 $newpage->timecreated = time();
1730 $newpage->qtype = $properties->qtype;
1731 $newpage->qoption = (isset($properties->qoption))?1:0;
1732 $newpage->layout = (isset($properties->layout))?1:0;
1733 $newpage->display = (isset($properties->display))?1:0;
1734 $newpage->prevpageid = 0; // this is a first page
1735 $newpage->nextpageid = 0; // this is the only page
1737 if ($properties->pageid) {
1738 $prevpage = $DB->get_record("lesson_pages", array("id" => $properties->pageid), 'id, nextpageid');
1740 print_error('cannotfindpages', 'lesson');
1742 $newpage->prevpageid = $prevpage->id;
1743 $newpage->nextpageid = $prevpage->nextpageid;
1745 $nextpage = $DB->get_record('lesson_pages', array('lessonid'=>$lesson->id, 'prevpageid'=>0), 'id');
1747 // This is the first page, there are existing pages put this at the start
1748 $newpage->nextpageid = $nextpage->id;
1752 $newpage->id = $DB->insert_record("lesson_pages", $newpage);
1754 $editor = new stdClass;
1755 $editor->id = $newpage->id;
1756 $editor->contents_editor = $properties->contents_editor;
1757 $editor = file_postupdate_standard_editor($editor, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$maxbytes), $context, 'mod_lesson', 'page_contents', $editor->id);
1758 $DB->update_record("lesson_pages", $editor);
1760 if ($newpage->prevpageid > 0) {
1761 $DB->set_field("lesson_pages", "nextpageid", $newpage->id, array("id" => $newpage->prevpageid));
1763 if ($newpage->nextpageid > 0) {
1764 $DB->set_field("lesson_pages", "prevpageid", $newpage->id, array("id" => $newpage->nextpageid));
1767 $page = lesson_page::load($newpage, $lesson);
1768 $page->create_answers($properties);
1770 $lesson->add_message(get_string('insertedpage', 'lesson').': '.format_string($newpage->title, true), 'notifysuccess');
1776 * This method loads a page object from the database and returns it as a
1777 * specialised object that extends lesson_page
1782 * @param lesson $lesson
1783 * @return lesson_page Specialised lesson_page object
1785 final public static function load($id, lesson $lesson) {
1788 if (is_object($id) && !empty($id->qtype)) {
1791 $page = $DB->get_record("lesson_pages", array("id" => $id));
1793 print_error('cannotfindpages', 'lesson');
1796 $manager = lesson_page_type_manager::get($lesson);
1798 $class = 'lesson_page_type_'.$manager->get_page_type_idstring($page->qtype);
1799 if (!class_exists($class)) {
1800 $class = 'lesson_page';
1803 return new $class($page, $lesson);
1807 * Deletes a lesson_page from the database as well as any associated records.
1811 final public function delete() {
1813 // first delete all the associated records...
1814 $DB->delete_records("lesson_attempts", array("pageid" => $this->properties->id));
1815 // ...now delete the answers...
1816 $DB->delete_records("lesson_answers", array("pageid" => $this->properties->id));
1817 // ..and the page itself
1818 $DB->delete_records("lesson_pages", array("id" => $this->properties->id));
1820 // repair the hole in the linkage
1821 if (!$this->properties->prevpageid && !$this->properties->nextpageid) {
1822 //This is the only page, no repair needed
1823 } elseif (!$this->properties->prevpageid) {
1824 // this is the first page...
1825 $page = $this->lesson->load_page($this->properties->nextpageid);
1826 $page->move(null, 0);
1827 } elseif (!$this->properties->nextpageid) {
1828 // this is the last page...
1829 $page = $this->lesson->load_page($this->properties->prevpageid);
1832 // page is in the middle...
1833 $prevpage = $this->lesson->load_page($this->properties->prevpageid);
1834 $nextpage = $this->lesson->load_page($this->properties->nextpageid);
1836 $prevpage->move($nextpage->id);
1837 $nextpage->move(null, $prevpage->id);
1843 * Moves a page by updating its nextpageid and prevpageid values within
1847 * @param int $nextpageid
1848 * @param int $prevpageid
1850 final public function move($nextpageid=null, $prevpageid=null) {
1852 if ($nextpageid === null) {
1853 $nextpageid = $this->properties->nextpageid;
1855 if ($prevpageid === null) {
1856 $prevpageid = $this->properties->prevpageid;
1858 $obj = new stdClass;
1859 $obj->id = $this->properties->id;
1860 $obj->prevpageid = $prevpageid;
1861 $obj->nextpageid = $nextpageid;
1862 $DB->update_record('lesson_pages', $obj);
1866 * Returns the answers that are associated with this page in the database
1871 final public function get_answers() {
1873 if ($this->answers === null) {
1874 $this->answers = array();
1875 $answers = $DB->get_records('lesson_answers', array('pageid'=>$this->properties->id, 'lessonid'=>$this->lesson->id), 'id');
1877 debugging(get_string('cannotfindanswer', 'lesson'));
1880 foreach ($answers as $answer) {
1881 $this->answers[count($this->answers)] = new lesson_page_answer($answer);
1884 return $this->answers;
1888 * Returns the lesson this page is associated with
1892 final protected function get_lesson() {
1893 return $this->lesson;
1897 * Returns the type of page this is. Not to be confused with page type
1901 final protected function get_type() {
1906 * Records an attempt at this page
1909 * @param stdClass $context
1910 * @return stdClass Returns the result of the attempt
1912 final public function record_attempt($context) {
1913 global $DB, $USER, $OUTPUT;
1916 * This should be overridden by each page type to actually check the response
1917 * against what ever custom criteria they have defined
1919 $result = $this->check_answer();
1921 $result->attemptsremaining = 0;
1922 $result->maxattemptsreached = false;
1924 if ($result->noanswer) {
1925 $result->newpageid = $this->properties->id; // display same page again
1926 $result->feedback = get_string('noanswer', 'lesson');
1928 if (!has_capability('mod/lesson:manage', $context)) {
1929 $nretakes = $DB->count_records("lesson_grades", array("lessonid"=>$this->lesson->id, "userid"=>$USER->id));
1930 // record student's attempt
1931 $attempt = new stdClass;
1932 $attempt->lessonid = $this->lesson->id;
1933 $attempt->pageid = $this->properties->id;
1934 $attempt->userid = $USER->id;
1935 $attempt->answerid = $result->answerid;
1936 $attempt->retry = $nretakes;
1937 $attempt->correct = $result->correctanswer;
1938 if($result->userresponse !== null) {
1939 $attempt->useranswer = $result->userresponse;
1942 $attempt->timeseen = time();
1943 // if allow modattempts, then update the old attempt record, otherwise, insert new answer record
1944 if (isset($USER->modattempts[$this->lesson->id])) {
1945 $attempt->retry = $nretakes - 1; // they are going through on review, $nretakes will be too high
1948 $DB->insert_record("lesson_attempts", $attempt);
1949 // "number of attempts remaining" message if $this->lesson->maxattempts > 1
1950 // displaying of message(s) is at the end of page for more ergonomic display
1951 if (!$result->correctanswer && ($result->newpageid == 0)) {
1952 // wrong answer and student is stuck on this page - check how many attempts
1953 // the student has had at this page/question
1954 $nattempts = $DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id),"retry", $nretakes);
1955 // retreive the number of attempts left counter for displaying at bottom of feedback page
1956 if ($nattempts >= $this->lesson->maxattempts) {
1957 if ($this->lesson->maxattempts > 1) { // don't bother with message if only one attempt
1958 $result->maxattemptsreached = true;
1960 $result->newpageid = LESSON_NEXTPAGE;
1961 } else if ($this->lesson->maxattempts > 1) { // don't bother with message if only one attempt
1962 $result->attemptsremaining = $this->lesson->maxattempts - $nattempts;
1966 // TODO: merge this code with the jump code below. Convert jumpto page into a proper page id
1967 if ($result->newpageid == 0) {
1968 $result->newpageid = $this->properties->id;
1969 } elseif ($result->newpageid == LESSON_NEXTPAGE) {
1970 $nextpage = $this->lesson->get_next_page($this->properties->nextpageid);
1971 if ($nextpage === false) {
1972 $result->newpageid = LESSON_EOL;
1974 $result->newpageid = $nextpage->id;
1978 // Determine default feedback if necessary
1979 if (empty($result->response)) {
1980 if (!$this->lesson->feedback && !$result->noanswer && !($this->lesson->review & !$result->correctanswer && !$result->isessayquestion)) {
1981 // These conditions have been met:
1982 // 1. The lesson manager has not supplied feedback to the student
1983 // 2. Not displaying default feedback
1984 // 3. The user did provide an answer
1985 // 4. We are not reviewing with an incorrect answer (and not reviewing an essay question)
1987 $result->nodefaultresponse = true; // This will cause a redirect below
1988 } else if ($result->isessayquestion) {
1989 $result->response = get_string('defaultessayresponse', 'lesson');
1990 } else if ($result->correctanswer) {
1991 $result->response = get_string('thatsthecorrectanswer', 'lesson');
1993 $result->response = get_string('thatsthewronganswer', 'lesson');
1997 if ($result->response) {
1998 if ($this->lesson->review && !$result->correctanswer && !$result->isessayquestion) {
1999 $nretakes = $DB->count_records("lesson_grades", array("lessonid"=>$this->lesson->id, "userid"=>$USER->id));
2000 $qattempts = $DB->count_records("lesson_attempts", array("userid"=>$USER->id, "retry"=>$nretakes, "pageid"=>$this->properties->id));
2001 if ($qattempts == 1) {
2002 $result->feedback = $OUTPUT->box(get_string("firstwrong", "lesson"), 'feedback');
2004 $result->feedback = $OUTPUT->BOX(get_string("secondpluswrong", "lesson"), 'feedback');
2007 $class = 'response';
2008 if ($result->correctanswer) {
2009 $class .= ' correct'; //CSS over-ride this if they exist (!important)
2010 } else if (!$result->isessayquestion) {
2011 $class .= ' incorrect'; //CSS over-ride this if they exist (!important)
2013 $options = new stdClass;
2014 $options->noclean = true;
2015 $options->para = true;
2016 $result->feedback = $OUTPUT->box(format_text($this->properties->contents, $this->properties->contentsformat, $options), 'generalbox boxaligncenter');
2017 $result->feedback .= '<div class="correctanswer generalbox"><em>'.get_string("youranswer", "lesson").'</em> : '.$result->studentanswer; // already in clean html
2018 $result->feedback .= $OUTPUT->box($result->response, $class); // already conerted to HTML
2028 * Returns the string for a jump name
2031 * @param int $jumpto Jump code or page ID
2034 final protected function get_jump_name($jumpto) {
2036 static $jumpnames = array();
2038 if (!array_key_exists($jumpto, $jumpnames)) {
2039 if ($jumpto == LESSON_THISPAGE) {
2040 $jumptitle = get_string('thispage', 'lesson');
2041 } elseif ($jumpto == LESSON_NEXTPAGE) {
2042 $jumptitle = get_string('nextpage', 'lesson');
2043 } elseif ($jumpto == LESSON_EOL) {
2044 $jumptitle = get_string('endoflesson', 'lesson');
2045 } elseif ($jumpto == LESSON_UNSEENBRANCHPAGE) {
2046 $jumptitle = get_string('unseenpageinbranch', 'lesson');
2047 } elseif ($jumpto == LESSON_PREVIOUSPAGE) {
2048 $jumptitle = get_string('previouspage', 'lesson');
2049 } elseif ($jumpto == LESSON_RANDOMPAGE) {
2050 $jumptitle = get_string('randompageinbranch', 'lesson');
2051 } elseif ($jumpto == LESSON_RANDOMBRANCH) {
2052 $jumptitle = get_string('randombranch', 'lesson');
2053 } elseif ($jumpto == LESSON_CLUSTERJUMP) {
2054 $jumptitle = get_string('clusterjump', 'lesson');
2056 if (!$jumptitle = $DB->get_field('lesson_pages', 'title', array('id' => $jumpto))) {
2057 $jumptitle = '<strong>'.get_string('notdefined', 'lesson').'</strong>';
2060 $jumpnames[$jumpto] = format_string($jumptitle,true);
2063 return $jumpnames[$jumpto];
2067 * Constructor method
2068 * @param object $properties
2069 * @param lesson $lesson
2071 public function __construct($properties, lesson $lesson) {
2072 parent::__construct($properties);
2073 $this->lesson = $lesson;
2077 * Returns the score for the attempt
2078 * This may be overridden by page types that require manual grading
2079 * @param array $answers
2080 * @param object $attempt
2083 public function earned_score($answers, $attempt) {
2084 return $answers[$attempt->answerid]->score;
2088 * This is a callback method that can be override and gets called when ever a page
2091 * @param bool $canmanage True if the user has the manage cap
2094 public function callback_on_view($canmanage) {
2099 * Updates a lesson page and its answers within the database
2101 * @param object $properties
2104 public function update($properties, $context = null, $maxbytes = null) {
2106 $answers = $this->get_answers();
2107 $properties->id = $this->properties->id;
2108 $properties->lessonid = $this->lesson->id;
2109 if (empty($properties->qoption)) {
2110 $properties->qoption = '0';
2112 if (empty($context)) {
2113 $context = $PAGE->context;
2115 if ($maxbytes === null) {
2116 $maxbytes =get_max_upload_file_size();
2118 $properties = file_postupdate_standard_editor($properties, 'contents', array('noclean'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$maxbytes), $context, 'mod_lesson', 'page_contents', $properties->id);
2119 $DB->update_record("lesson_pages", $properties);
2121 for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
2122 if (!array_key_exists($i, $this->answers)) {
2123 $this->answers[$i] = new stdClass;
2124 $this->answers[$i]->lessonid = $this->lesson->id;
2125 $this->answers[$i]->pageid = $this->id;
2126 $this->answers[$i]->timecreated = $this->timecreated;
2128 if (!empty($properties->answer_editor[$i])) {
2129 $this->answers[$i]->answer = $properties->answer_editor[$i]['text'];
2130 $this->answers[$i]->answerformat = $properties->answer_editor[$i]['format'];
2131 if (isset($properties->response_editor[$i])) {
2132 $this->answers[$i]->response = $properties->response_editor[$i]['text'];
2133 $this->answers[$i]->responseformat = $properties->response_editor[$i]['format'];
2135 if (isset($properties->jumpto[$i])) {
2136 $this->answers[$i]->jumpto = $properties->jumpto[$i];
2138 if ($this->lesson->custom && isset($properties->score[$i])) {
2139 $this->answers[$i]->score = $properties->score[$i];
2141 if (!isset($this->answers[$i]->id)) {
2142 $this->answers[$i]->id = $DB->insert_record("lesson_answers", $this->answers[$i]);
2144 $DB->update_record("lesson_answers", $this->answers[$i]->properties());
2155 * Can be set to true if the page requires a static link to create a new instance
2156 * instead of simply being included in the dropdown
2157 * @param int $previd
2160 public function add_page_link($previd) {
2165 * Returns true if a page has been viewed before
2167 * @param array|int $param Either an array of pages that have been seen or the
2168 * number of retakes a user has had
2171 public function is_unseen($param) {
2173 if (is_array($param)) {
2174 $seenpages = $param;
2175 return (!array_key_exists($this->properties->id, $seenpages));
2178 if (!$DB->count_records("lesson_attempts", array("pageid"=>$this->properties->id, "userid"=>$USER->id, "retry"=>$nretakes))) {
2186 * Checks to see if a page has been answered previously
2187 * @param int $nretakes
2190 public function is_unanswered($nretakes) {
2192 if (!$DB->count_records("lesson_attempts", array('pageid'=>$this->properties->id, 'userid'=>$USER->id, 'correct'=>1, 'retry'=>$nretakes))) {
2199 * Creates answers within the database for this lesson_page. Usually only ever
2200 * called when creating a new page instance
2201 * @param object $properties
2204 public function create_answers($properties) {
2206 // now add the answers
2207 $newanswer = new stdClass;
2208 $newanswer->lessonid = $this->lesson->id;
2209 $newanswer->pageid = $this->properties->id;
2210 $newanswer->timecreated = $this->properties->timecreated;
2214 for ($i = 0; $i < $this->lesson->maxanswers; $i++) {
2215 $answer = clone($newanswer);
2216 if (!empty($properties->answer_editor[$i])) {
2217 $answer->answer = $properties->answer_editor[$i]['text'];
2218 $answer->answerformat = $properties->answer_editor[$i]['format'];
2219 if (isset($properties->response_editor[$i])) {
2220 $answer->response = $properties->response_editor[$i]['text'];
2221 $answer->responseformat = $properties->response_editor[$i]['format'];
2223 if (isset($properties->jumpto[$i])) {
2224 $answer->jumpto = $properties->jumpto[$i];
2226 if ($this->lesson->custom && isset($properties->score[$i])) {
2227 $answer->score = $properties->score[$i];
2229 $answer->id = $DB->insert_record("lesson_answers", $answer);
2230 $answers[$answer->id] = new lesson_page_answer($answer);
2236 $this->answers = $answers;
2241 * This method MUST be overridden by all question page types, or page types that
2242 * wish to score a page.
2244 * The structure of result should always be the same so it is a good idea when
2245 * overriding this method on a page type to call
2247 * $result = parent::check_answer();
2249 * before modifying it as required.
2253 public function check_answer() {
2254 $result = new stdClass;
2255 $result->answerid = 0;
2256 $result->noanswer = false;
2257 $result->correctanswer = false;
2258 $result->isessayquestion = false; // use this to turn off review button on essay questions
2259 $result->response = '';
2260 $result->newpageid = 0; // stay on the page
2261 $result->studentanswer = ''; // use this to store student's answer(s) in order to display it on feedback page
2262 $result->userresponse = null;
2263 $result->feedback = '';
2264 $result->nodefaultresponse = false; // Flag for redirecting when default feedback is turned off
2269 * True if the page uses a custom option
2271 * Should be override and set to true if the page uses a custom option.
2275 public function has_option() {
2280 * Returns the maximum number of answers for this page given the maximum number
2281 * of answers permitted by the lesson.
2283 * @param int $default
2286 public function max_answers($default) {
2291 * Returns the properties of this lesson page as an object
2294 public function properties() {
2295 $properties = clone($this->properties);
2296 if ($this->answers === null) {
2297 $this->get_answers();
2299 if (count($this->answers)>0) {
2301 foreach ($this->answers as $answer) {
2302 $properties->{'answer_editor['.$count.']'} = array('text'=>$answer->answer, 'format'=>$answer->answerformat);
2303 $properties->{'response_editor['.$count.']'} = array('text'=>$answer->response, 'format'=>$answer->responseformat);
2304 $properties->{'jumpto['.$count.']'} = $answer->jumpto;
2305 $properties->{'score['.$count.']'} = $answer->score;
2313 * Returns an array of options to display when choosing the jumpto for a page/answer
2315 * @param int $pageid
2316 * @param lesson $lesson
2319 public static function get_jumptooptions($pageid, lesson $lesson) {
2322 $jump[0] = get_string("thispage", "lesson");
2323 $jump[LESSON_NEXTPAGE] = get_string("nextpage", "lesson");
2324 $jump[LESSON_PREVIOUSPAGE] = get_string("previouspage", "lesson");
2325 $jump[LESSON_EOL] = get_string("endoflesson", "lesson");
2331 $pages = $lesson->load_all_pages();
2332 if ($pages[$pageid]->qtype == LESSON_PAGE_BRANCHTABLE || $lesson->is_sub_page_of_type($pageid, array(LESSON_PAGE_BRANCHTABLE), array(LESSON_PAGE_ENDOFBRANCH, LESSON_PAGE_CLUSTER))) {
2333 $jump[LESSON_UNSEENBRANCHPAGE] = get_string("unseenpageinbranch", "lesson");
2334 $jump[LESSON_RANDOMPAGE] = get_string("randompageinbranch", "lesson");
2336 if($pages[$pageid]->qtype == LESSON_PAGE_CLUSTER || $lesson->is_sub_page_of_type($pageid, array(LESSON_PAGE_CLUSTER), array(LESSON_PAGE_ENDOFCLUSTER))) {
2337 $jump[LESSON_CLUSTERJUMP] = get_string("clusterjump", "lesson");
2339 if (!optional_param('firstpage', 0, PARAM_INT)) {
2340 $apageid = $DB->get_field("lesson_pages", "id", array("lessonid" => $lesson->id, "prevpageid" => 0));
2343 $title = $DB->get_field("lesson_pages", "title", array("id" => $apageid));
2344 $jump[$apageid] = strip_tags(format_string($title,true));
2345 $apageid = $DB->get_field("lesson_pages", "nextpageid", array("id" => $apageid));
2347 // last page reached
2355 * Returns the contents field for the page properly formatted and with plugin
2356 * file url's converted
2359 public function get_contents() {
2361 if (!empty($this->properties->contents)) {
2362 if (!isset($this->properties->contentsformat)) {
2363 $this->properties->contentsformat = FORMAT_HTML;
2365 $context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->id);
2366 $contents = format_text($this->properties->contents); //format text so glossary autolinking happens
2367 return file_rewrite_pluginfile_urls($contents, 'pluginfile.php', $context->id, 'mod_lesson', 'page_contents', $this->properties->id);
2374 * Set to true if this page should display in the menu block
2377 protected function get_displayinmenublock() {
2382 * Get the string that describes the options of this page type
2385 public function option_description_string() {
2390 * Updates a table with the answers for this page
2391 * @param html_table $table
2392 * @return html_table
2394 public function display_answers(html_table $table) {
2395 $answers = $this->get_answers();
2397 foreach ($answers as $answer) {
2399 $cells[] = "<span class=\"label\">".get_string("jump", "lesson")." $i<span>: ";
2400 $cells[] = $this->get_jump_name($answer->jumpto);
2401 $table->data[] = new html_table_row($cells);
2403 $table->data[count($table->data)-1]->cells[0]->style = 'width:20%;';
2411 * Determines if this page should be grayed out on the management/report screens
2412 * @return int 0 or 1
2414 protected function get_grayout() {
2419 * Adds stats for this page to the &pagestats object. This should be defined
2420 * for all page types that grade
2421 * @param array $pagestats
2425 public function stats(array &$pagestats, $tries) {
2430 * Formats the answers of this page for a report
2432 * @param object $answerpage
2433 * @param object $answerdata
2434 * @param object $useranswer
2435 * @param array $pagestats
2436 * @param int $i Count of first level answers
2437 * @param int $n Count of second level answers
2438 * @return object The answer page for this
2440 public function report_answers($answerpage, $answerdata, $useranswer, $pagestats, &$i, &$n) {
2441 $answers = $this->get_answers();
2442 $formattextdefoptions = new stdClass;
2443 $formattextdefoptions->para = false; //I'll use it widely in this page
2444 foreach ($answers as $answer) {
2445 $data = get_string('jumpsto', 'lesson', $this->get_jump_name($answer->jumpto));
2446 $answerdata->answers[] = array($data, "");
2447 $answerpage->answerdata = $answerdata;
2453 * Gets an array of the jumps used by the answers of this page
2457 public function get_jumps() {
2460 $params = array ("lessonid" => $this->lesson->id, "pageid" => $this->properties->id);
2461 if ($answers = $this->get_answers()) {
2462 foreach ($answers as $answer) {
2463 $jumps[] = $this->get_jump_name($answer->jumpto);
2469 * Informs whether this page type require manual grading or not
2472 public function requires_manual_grading() {
2477 * A callback method that allows a page to override the next page a user will
2478 * see during when this page is being completed.
2481 public function override_next_page() {
2486 * This method is used to determine if this page is a valid page
2488 * @param array $validpages
2489 * @param array $pageviews
2490 * @return int The next page id to check
2492 public function valid_page_and_view(&$validpages, &$pageviews) {
2493 $validpages[$this->properties->id] = 1;
2494 return $this->properties->nextpageid;
2501 * Class used to represent an answer to a page
2503 * @property int $id The ID of this answer in the database
2504 * @property int $lessonid The ID of the lesson this answer belongs to
2505 * @property int $pageid The ID of the page this answer belongs to
2506 * @property int $jumpto Identifies where the user goes upon completing a page with this answer
2507 * @property int $grade The grade this answer is worth
2508 * @property int $score The score this answer will give
2509 * @property int $flags Used to store options for the answer
2510 * @property int $timecreated A timestamp of when the answer was created
2511 * @property int $timemodified A timestamp of when the answer was modified
2512 * @property string $answer The answer itself
2513 * @property string $response The response the user sees if selecting this answer
2515 * @copyright 2009 Sam Hemelryk
2516 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2518 class lesson_page_answer extends lesson_base {
2521 * Loads an page answer from the DB
2524 * @return lesson_page_answer
2526 public static function load($id) {
2528 $answer = $DB->get_record("lesson_answers", array("id" => $id));
2529 return new lesson_page_answer($answer);
2533 * Given an object of properties and a page created answer(s) and saves them
2536 * @param stdClass $properties
2537 * @param lesson_page $page
2540 public static function create($properties, lesson_page $page) {
2541 return $page->create_answers($properties);
2547 * A management class for page types
2549 * This class is responsible for managing the different pages. A manager object can
2550 * be retrieved by calling the following line of code:
2552 * $manager = lesson_page_type_manager::get($lesson);
2554 * The first time the page type manager is retrieved the it includes all of the
2555 * different page types located in mod/lesson/pagetypes.
2557 * @copyright 2009 Sam Hemelryk
2558 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2560 class lesson_page_type_manager {
2563 * An array of different page type classes
2566 protected $types = array();
2569 * Retrieves the lesson page type manager object
2571 * If the object hasn't yet been created it is created here.
2573 * @staticvar lesson_page_type_manager $pagetypemanager
2574 * @param lesson $lesson
2575 * @return lesson_page_type_manager
2577 public static function get(lesson $lesson) {
2578 static $pagetypemanager;
2579 if (!($pagetypemanager instanceof lesson_page_type_manager)) {
2580 $pagetypemanager = new lesson_page_type_manager();
2581 $pagetypemanager->load_lesson_types($lesson);
2583 return $pagetypemanager;
2587 * Finds and loads all lesson page types in mod/lesson/pagetypes
2589 * @param lesson $lesson
2591 public function load_lesson_types(lesson $lesson) {
2593 $basedir = $CFG->dirroot.'/mod/lesson/pagetypes/';
2594 $dir = dir($basedir);
2595 while (false !== ($entry = $dir->read())) {
2596 if (strpos($entry, '.')===0 || !preg_match('#^[a-zA-Z]+\.php#i', $entry)) {
2599 require_once($basedir.$entry);
2600 $class = 'lesson_page_type_'.strtok($entry,'.');
2601 if (class_exists($class)) {
2602 $pagetype = new $class(new stdClass, $lesson);
2603 $this->types[$pagetype->typeid] = $pagetype;
2610 * Returns an array of strings to describe the loaded page types
2612 * @param int $type Can be used to return JUST the string for the requested type
2615 public function get_page_type_strings($type=null, $special=true) {
2617 foreach ($this->types as $pagetype) {
2618 if (($type===null || $pagetype->type===$type) && ($special===true || $pagetype->is_standard())) {
2619 $types[$pagetype->typeid] = $pagetype->typestring;
2626 * Returns the basic string used to identify a page type provided with an id
2628 * This string can be used to instantiate or identify the page type class.
2629 * If the page type id is unknown then 'unknown' is returned
2634 public function get_page_type_idstring($id) {
2635 foreach ($this->types as $pagetype) {
2636 if ((int)$pagetype->typeid === (int)$id) {
2637 return $pagetype->idstring;
2644 * Loads a page for the provided lesson given it's id
2646 * This function loads a page from the lesson when given both the lesson it belongs
2647 * to as well as the page's id.
2648 * If the page doesn't exist an error is thrown
2650 * @param int $pageid The id of the page to load
2651 * @param lesson $lesson The lesson the page belongs to
2652 * @return lesson_page A class that extends lesson_page
2654 public function load_page($pageid, lesson $lesson) {
2656 if (!($page =$DB->get_record('lesson_pages', array('id'=>$pageid, 'lessonid'=>$lesson->id)))) {
2657 print_error('cannotfindpages', 'lesson');
2659 $pagetype = get_class($this->types[$page->qtype]);
2660 $page = new $pagetype($page, $lesson);
2665 * This function loads ALL pages that belong to the lesson.
2667 * @param lesson $lesson
2668 * @return array An array of lesson_page_type_*
2670 public function load_all_pages(lesson $lesson) {
2672 if (!($pages =$DB->get_records('lesson_pages', array('lessonid'=>$lesson->id)))) {
2673 print_error('cannotfindpages', 'lesson');
2675 foreach ($pages as $key=>$page) {
2676 $pagetype = get_class($this->types[$page->qtype]);
2677 $pages[$key] = new $pagetype($page, $lesson);
2680 $orderedpages = array();
2684 foreach ($pages as $page) {
2685 if ((int)$page->prevpageid === (int)$lastpageid) {
2686 $orderedpages[$page->id] = $page;
2687 unset($pages[$page->id]);
2688 $lastpageid = $page->id;
2689 if ((int)$page->nextpageid===0) {
2698 return $orderedpages;
2702 * Fetches an mform that can be used to create/edit an page
2704 * @param int $type The id for the page type
2705 * @param array $arguments Any arguments to pass to the mform
2706 * @return lesson_add_page_form_base
2708 public function get_page_form($type, $arguments) {
2709 $class = 'lesson_add_page_form_'.$this->get_page_type_idstring($type);
2710 if (!class_exists($class) || get_parent_class($class)!=='lesson_add_page_form_base') {
2711 debugging('Lesson page type unknown class requested '.$class, DEBUG_DEVELOPER);
2712 $class = 'lesson_add_page_form_selection';
2713 } else if ($class === 'lesson_add_page_form_unknown') {
2714 $class = 'lesson_add_page_form_selection';
2716 return new $class(null, $arguments);
2720 * Returns an array of links to use as add page links
2721 * @param int $previd The id of the previous page
2724 public function get_add_page_type_links($previd) {
2729 foreach ($this->types as $key=>$type) {
2730 if ($link = $type->add_page_link($previd)) {
2731 $links[$key] = $link;