Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / lesson / view.php
blob3243160def9bf97c0b81a0fdf1f366bb07c48cf9
1 <?php // $Id$
2 /**
3 * This page prints a particular instance of lesson
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
10 require_once('../../config.php');
11 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
12 require_once($CFG->dirroot.'/mod/lesson/lib.php');
13 require_once($CFG->dirroot.'/mod/lesson/pagelib.php');
14 require_once($CFG->libdir.'/blocklib.php');
16 $id = required_param('id', PARAM_INT); // Course Module ID
17 $pageid = optional_param('pageid', NULL, PARAM_INT); // Lesson Page ID
18 $edit = optional_param('edit', -1, PARAM_BOOL);
20 list($cm, $course, $lesson) = lesson_get_basics($id);
22 require_login($course->id, false, $cm);
24 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
26 /// Check these for students only TODO: Find a better method for doing this!
27 /// Check lesson availability
28 /// Check for password
29 /// Check dependencies
30 /// Check for high scores
31 if (!has_capability('mod/lesson:manage', $context)) {
33 if (($lesson->available != 0 and time() < $lesson->available) or
34 ($lesson->deadline != 0 and time() > $lesson->deadline)) { // Deadline restrictions
35 if ($lesson->deadline != 0 and time() > $lesson->deadline) {
36 $message = get_string('lessonclosed', 'lesson', userdate($lesson->deadline));
37 } else {
38 $message = get_string('lessonopen', 'lesson', userdate($lesson->available));
41 lesson_print_header($cm, $course, $lesson);
42 print_simple_box_start('center');
43 echo '<div style="text-align:center;">';
44 echo '<p>'.$message.'</p>';
45 echo '<div class="lessonbutton standardbutton" style="padding: 5px;"><a href="'.$CFG->wwwroot.'/course/view.php?id='. $course->id .'">'. get_string('returnto', 'lesson', format_string($course->fullname, true)) .'</a></div>';
46 echo '</div>';
47 print_simple_box_end();
48 print_footer($course);
49 exit();
51 } else if ($lesson->usepassword and empty($USER->lessonloggedin[$lesson->id])) { // Password protected lesson code
52 $correctpass = false;
53 if ($password = optional_param('userpassword', '', PARAM_CLEAN)) {
54 if ($lesson->password == md5(trim($password))) {
55 $USER->lessonloggedin[$lesson->id] = true;
56 $correctpass = true;
57 if ($lesson->highscores) {
58 // Logged in - redirect so we go through all of these checks before starting the lesson.
59 redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id");
64 if (!$correctpass) {
65 lesson_print_header($cm, $course, $lesson);
66 echo "<div class=\"password-form\">\n";
67 print_simple_box_start('center');
68 echo '<form id="password" method="post" action="'.$CFG->wwwroot.'/mod/lesson/view.php" autocomplete="off">' . "\n";
69 echo '<fieldset class="invisiblefieldset">';
70 echo '<input type="hidden" name="id" value="'. $cm->id .'" />' . "\n";
71 if (optional_param('userpassword', 0, PARAM_CLEAN)) {
72 notify(get_string('loginfail', 'lesson'));
75 echo get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name))."<br /><br />\n".
76 get_string('enterpassword', 'lesson')." <input type=\"password\" name=\"userpassword\" /><br /><br />\n<center>".
77 '<span class="lessonbutton standardbutton"><a href="'.$CFG->wwwroot.'/course/view.php?id='. $course->id .'">'. get_string('cancel', 'lesson') .'</a></span> ';
79 lesson_print_submit_link(get_string('continue', 'lesson'), 'password', 'center', 'standardbutton submitbutton');
80 echo '</fieldset></form>';
81 print_simple_box_end();
82 echo "</div>\n";
83 print_footer($course);
84 exit();
87 } else if ($lesson->dependency) { // check for dependencies
88 if ($dependentlesson = get_record('lesson', 'id', $lesson->dependency)) {
89 // lesson exists, so we can proceed
90 $conditions = unserialize($lesson->conditions);
91 // assume false for all
92 $timespent = false;
93 $completed = false;
94 $gradebetterthan = false;
95 // check for the timespent condition
96 if ($conditions->timespent) {
97 if ($attempttimes = get_records_select('lesson_timer', "userid = $USER->id AND lessonid = $dependentlesson->id")) {
98 // go through all the times and test to see if any of them satisfy the condition
99 foreach($attempttimes as $attempttime) {
100 $duration = $attempttime->lessontime - $attempttime->starttime;
101 if ($conditions->timespent < $duration/60) {
102 $timespent = true;
106 } else {
107 $timespent = true; // there isn't one set
110 // check for the gradebetterthan condition
111 if($conditions->gradebetterthan) {
112 if ($studentgrades = get_records_select('lesson_grades', "userid = $USER->id AND lessonid = $dependentlesson->id")) {
113 // go through all the grades and test to see if any of them satisfy the condition
114 foreach($studentgrades as $studentgrade) {
115 if ($studentgrade->grade >= $conditions->gradebetterthan) {
116 $gradebetterthan = true;
120 } else {
121 $gradebetterthan = true; // there isn't one set
124 // check for the completed condition
125 if ($conditions->completed) {
126 if (count_records('lesson_grades', 'userid', $USER->id, 'lessonid', $dependentlesson->id)) {
127 $completed = true;
129 } else {
130 $completed = true; // not set
133 $errors = array();
134 // collect all of our error statements
135 if (!$timespent) {
136 $errors[] = get_string('timespenterror', 'lesson', $conditions->timespent);
138 if (!$completed) {
139 $errors[] = get_string('completederror', 'lesson');
141 if (!$gradebetterthan) {
142 $errors[] = get_string('gradebetterthanerror', 'lesson', $conditions->gradebetterthan);
144 if (!empty($errors)) { // print out the errors if any
145 lesson_print_header($cm, $course, $lesson);
146 echo '<p>';
147 print_simple_box_start('center');
148 print_string('completethefollowingconditions', 'lesson', $dependentlesson->name);
149 echo '<p style="text-align:center;">'.implode('<br />'.get_string('and', 'lesson').'<br />', $errors).'</p>';
150 print_simple_box_end();
151 echo '</p>';
152 print_footer($course);
153 exit();
157 } else if ($lesson->highscores and !$lesson->practice and !optional_param('viewed', 0) and empty($pageid)) {
158 // Display high scores before starting lesson
159 redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id");
163 // set up some general variables
164 $path = $CFG->wwwroot .'/course';
166 // this is called if a student leaves during a lesson
167 if($pageid == LESSON_UNSEENBRANCHPAGE) {
168 $pageid = lesson_unseen_question_jump($lesson->id, $USER->id, $pageid);
171 // display individual pages and their sets of answers
172 // if pageid is EOL then the end of the lesson has been reached
173 // for flow, changed to simple echo for flow styles, michaelp, moved lesson name and page title down
174 $attemptflag = false;
175 if (empty($pageid)) {
176 // make sure there are pages to view
177 if (!get_field('lesson_pages', 'id', 'lessonid', $lesson->id, 'prevpageid', 0)) {
178 if (!has_capability('mod/lesson:manage', $context)) {
179 lesson_set_message(get_string('lessonnotready', 'lesson', $course->teacher)); // a nice message to the student
180 } else {
181 if (!count_records('lesson_pages', 'lessonid', $lesson->id)) {
182 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); // no pages - redirect to add pages
183 } else {
184 lesson_set_message(get_string('lessonpagelinkingbroken', 'lesson')); // ok, bad mojo
189 add_to_log($course->id, 'lesson', 'start', 'view.php?id='. $cm->id, $lesson->id, $cm->id);
191 // if no pageid given see if the lesson has been started
192 if ($grades = get_records_select('lesson_grades', 'lessonid = '. $lesson->id .' AND userid = '. $USER->id,
193 'grade DESC')) {
194 $retries = count($grades);
195 } else {
196 $retries = 0;
198 if ($retries) {
199 $attemptflag = true;
202 if (isset($USER->modattempts[$lesson->id])) {
203 unset($USER->modattempts[$lesson->id]); // if no pageid, then student is NOT reviewing
206 // if there are any questions have been answered correctly in this attempt
207 if ($attempts = get_records_select('lesson_attempts',
208 "lessonid = $lesson->id AND userid = $USER->id AND retry = $retries AND
209 correct = 1", 'timeseen DESC')) {
211 foreach ($attempts as $attempt) {
212 $jumpto = get_field('lesson_answers', 'jumpto', 'id', $attempt->answerid);
213 // convert the jumpto to a proper page id
214 if ($jumpto == 0) { // unlikely value!
215 $lastpageseen = $attempt->pageid;
216 } elseif ($jumpto == LESSON_NEXTPAGE) {
217 if (!$lastpageseen = get_field('lesson_pages', 'nextpageid', 'id',
218 $attempt->pageid)) {
219 // no nextpage go to end of lesson
220 $lastpageseen = LESSON_EOL;
222 } else {
223 $lastpageseen = $jumpto;
225 break; // only look at the latest correct attempt
227 } else {
228 $attempts = NULL;
231 if ($branchtables = get_records_select('lesson_branch',
232 "lessonid = $lesson->id AND userid = $USER->id AND retry = $retries", 'timeseen DESC')) {
233 // in here, user has viewed a branch table
234 $lastbranchtable = current($branchtables);
235 if ($attempts != NULL) {
236 foreach($attempts as $attempt) {
237 if ($lastbranchtable->timeseen > $attempt->timeseen) {
238 // branch table was viewed later than the last attempt
239 $lastpageseen = $lastbranchtable->pageid;
241 break;
243 } else {
244 // hasnt answered any questions but has viewed a branch table
245 $lastpageseen = $lastbranchtable->pageid;
248 //if ($lastpageseen != $firstpageid) {
249 if (isset($lastpageseen) and count_records('lesson_attempts', 'lessonid', $lesson->id, 'userid', $USER->id, 'retry', $retries) > 0) {
250 // get the first page
251 if (!$firstpageid = get_field('lesson_pages', 'id', 'lessonid', $lesson->id,
252 'prevpageid', 0)) {
253 error('Navigation: first page not found');
255 lesson_print_header($cm, $course, $lesson);
256 if ($lesson->timed) {
257 if ($lesson->retake) {
258 print_simple_box('<p style="text-align:center;">'. get_string('leftduringtimed', 'lesson') .'</p>', 'center');
259 echo '<div style="text-align:center;" class="lessonbutton standardbutton">'.
260 '<a href="view.php?id='.$cm->id.'&amp;pageid='.$firstpageid.'&amp;startlastseen=no">'.
261 get_string('continue', 'lesson').'</a></div>';
262 } else {
263 print_simple_box_start('center');
264 echo '<div style="text-align:center;">';
265 echo get_string('leftduringtimednoretake', 'lesson');
266 echo '<br /><br /><div class="lessonbutton standardbutton"><a href="../../course/view.php?id='. $course->id .'">'. get_string('returntocourse', 'lesson') .'</a></div>';
267 echo '</div>';
268 print_simple_box_end();
271 } else {
272 print_simple_box("<p style=\"text-align:center;\">".get_string('youhaveseen','lesson').'</p>',
273 "center");
275 echo '<div style="text-align:center;">';
276 echo '<span class="lessonbutton standardbutton">'.
277 '<a href="view.php?id='.$cm->id.'&amp;pageid='.$lastpageseen.'&amp;startlastseen=yes">'.
278 get_string('yes').'</a></span>&nbsp;&nbsp;&nbsp;';
279 echo '<span class="lessonbutton standardbutton">'.
280 '<a href="view.php?id='.$cm->id.'&amp;pageid='.$firstpageid.'&amp;startlastseen=no">'.
281 get_string('no').'</a></div>';
282 echo '</span>';
284 print_footer($course);
285 exit();
288 if ($grades) {
289 foreach ($grades as $grade) {
290 $bestgrade = $grade->grade;
291 break;
293 if (!$lesson->retake) {
294 lesson_print_header($cm, $course, $lesson, 'view');
295 print_simple_box_start('center');
296 echo "<div style=\"text-align:center;\">";
297 echo get_string("noretake", "lesson");
298 echo "<br /><br /><div class=\"lessonbutton standardbutton\"><a href=\"../../course/view.php?id=$course->id\">".get_string('returntocourse', 'lesson').'</a></div>';
299 echo "</div>";
300 print_simple_box_end();
301 print_footer($course);
302 exit();
303 //redirect("../../course/view.php?id=$course->id", get_string("alreadytaken", "lesson"));
304 // allow student to retake course even if they have the maximum grade
305 // } elseif ($bestgrade == 100) {
306 // redirect("../../course/view.php?id=$course->id", get_string("maximumgradeachieved",
307 // "lesson"));
310 // start at the first page
311 if (!$pageid = get_field('lesson_pages', 'id', 'lessonid', $lesson->id, 'prevpageid', 0)) {
312 error('Navigation: first page not found');
314 /// This is the code for starting a timed test
315 if(!isset($USER->startlesson[$lesson->id]) && !has_capability('mod/lesson:manage', $context)) {
316 $USER->startlesson[$lesson->id] = true;
317 $startlesson = new stdClass;
318 $startlesson->lessonid = $lesson->id;
319 $startlesson->userid = $USER->id;
320 $startlesson->starttime = time();
321 $startlesson->lessontime = time();
323 if (!insert_record('lesson_timer', $startlesson)) {
324 error('Error: could not insert row into lesson_timer table');
326 if ($lesson->timed) {
327 lesson_set_message(get_string('maxtimewarning', 'lesson', $lesson->maxtime), 'center');
331 if ($pageid != LESSON_EOL) {
332 /// This is the code updates the lessontime for a timed test
333 if ($startlastseen = optional_param('startlastseen', '', PARAM_ALPHA)) { /// this deletes old records not totally sure if this is necessary anymore
334 if ($startlastseen == 'no') {
335 if ($grades = get_records_select('lesson_grades', "lessonid = $lesson->id AND userid = $USER->id",
336 'grade DESC')) {
337 $retries = count($grades);
338 } else {
339 $retries = 0;
341 if (!delete_records('lesson_attempts', 'userid', $USER->id, 'lessonid', $lesson->id, 'retry', $retries)) {
342 error('Error: could not delete old attempts');
344 if (!delete_records('lesson_branch', 'userid', $USER->id, 'lessonid', $lesson->id, 'retry', $retries)) {
345 error('Error: could not delete old seen branches');
350 add_to_log($course->id, 'lesson', 'view', 'view.php?id='. $cm->id, $pageid, $cm->id);
352 if (!$page = get_record('lesson_pages', 'id', $pageid)) {
353 error('Navigation: the page record not found');
356 if ($page->qtype == LESSON_CLUSTER) { //this only gets called when a user starts up a new lesson and the first page is a cluster page
357 if (!has_capability('mod/lesson:manage', $context)) {
358 // get new id
359 $pageid = lesson_cluster_jump($lesson->id, $USER->id, $pageid);
360 // get new page info
361 if (!$page = get_record('lesson_pages', 'id', $pageid)) {
362 error('Navigation: the page record not found');
364 add_to_log($course->id, 'lesson', 'view', 'view.php?id='. $cm->id, $pageid, $cm->id);
365 } else {
366 // get the next page
367 $pageid = $page->nextpageid;
368 if (!$page = get_record('lesson_pages', 'id', $pageid)) {
369 error('Navigation: the page record not found');
372 } elseif ($page->qtype == LESSON_ENDOFCLUSTER) { // Check for endofclusters
373 if ($page->nextpageid == 0) {
374 $nextpageid = LESSON_EOL;
375 } else {
376 $nextpageid = $page->nextpageid;
378 redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&amp;pageid=$nextpageid");
379 } else if ($page->qtype == LESSON_ENDOFBRANCH) { // Check for endofbranches
380 if ($answers = get_records('lesson_answers', 'pageid', $page->id, 'id')) {
381 // print_heading(get_string('endofbranch', 'lesson'));
382 foreach ($answers as $answer) {
383 // just need the first answer
384 if ($answer->jumpto == LESSON_RANDOMBRANCH) {
385 $answer->jumpto = lesson_unseen_branch_jump($lesson->id, $USER->id);
386 } elseif ($answer->jumpto == LESSON_CLUSTERJUMP) {
387 if (!has_capability('mod/lesson:manage', $context)) {
388 $answer->jumpto = lesson_cluster_jump($lesson->id, $USER->id, $pageid);
389 } else {
390 if ($page->nextpageid == 0) {
391 $answer->jumpto = LESSON_EOL;
392 } else {
393 $answer->jumpto = $page->nextpageid;
396 } else if ($answer->jumpto == LESSON_NEXTPAGE) {
397 if ($page->nextpageid == 0) {
398 $answer->jumpto = LESSON_EOL;
399 } else {
400 $answer->jumpto = $page->nextpageid;
402 } else if ($answer->jumpto == 0) {
403 $answer->jumpto = $page->id;
404 } else if ($answer->jumpto == LESSON_PREVIOUSPAGE) {
405 $answer->jumpto = $page->prevpageid;
407 redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&amp;pageid=$answer->jumpto");
408 break;
410 } else {
411 error('Navigation: No answers on EOB');
415 // check to see if the user can see the left menu
416 if (!has_capability('mod/lesson:manage', $context)) {
417 $lesson->displayleft = lesson_displayleftif($lesson);
420 // This is where several messages (usually warnings) are displayed
421 // all of this is displayed above the actual page
423 // clock code
424 // get time information for this user
425 $timer = new stdClass;
426 if(!has_capability('mod/lesson:manage', $context)) {
427 if (!$timer = get_records_select('lesson_timer', "lessonid = $lesson->id AND userid = $USER->id", 'starttime')) {
428 error('Error: could not find records');
429 } else {
430 $timer = array_pop($timer); // this will get the latest start time record
434 $startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
435 if ($startlastseen == 'yes') { // continue a previous test, need to update the clock (think this option is disabled atm)
436 $timer->starttime = time() - ($timer->lessontime - $timer->starttime);
437 $timer->lessontime = time();
438 } else if ($startlastseen == 'no') { // starting over
439 // starting over, so reset the clock
440 $timer->starttime = time();
441 $timer->lessontime = time();
444 // for timed lessons, display clock
445 if ($lesson->timed) {
446 if(has_capability('mod/lesson:manage', $context)) {
447 lesson_set_message(get_string('teachertimerwarning', 'lesson'));
448 } else {
449 $timeleft = ($timer->starttime + $lesson->maxtime * 60) - time();
451 if ($timeleft <= 0) {
452 // Out of time
453 lesson_set_message(get_string('eolstudentoutoftime', 'lesson'));
454 redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&amp;pageid=".LESSON_EOL."&amp;outoftime=normal");
455 die; // Shouldn't be reached, but make sure
456 } else if ($timeleft < 60) {
457 // One minute warning
458 lesson_set_message(get_string('studentoneminwarning', 'lesson'));
463 // update the clock
464 if (!has_capability('mod/lesson:manage', $context)) {
465 $timer->lessontime = time();
466 if (!update_record('lesson_timer', $timer)) {
467 error('Error: could not update lesson_timer table');
471 /// This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
472 if(has_capability('mod/lesson:manage', $context)) {
473 if (lesson_display_teacher_warning($lesson->id)) {
474 $warningvars->cluster = get_string('clusterjump', 'lesson');
475 $warningvars->unseen = get_string('unseenpageinbranch', 'lesson');
476 lesson_set_message(get_string('teacherjumpwarning', 'lesson', $warningvars));
480 if ($page->qtype == LESSON_BRANCHTABLE) {
481 if ($lesson->minquestions and !has_capability('mod/lesson:manage', $context)) {
482 // tell student how many questions they have seen, how many are required and their grade
483 $ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
485 $gradeinfo = lesson_grade($lesson, $ntries);
487 if ($gradeinfo->attempts) {
488 if ($gradeinfo->nquestions < $lesson->minquestions) {
489 $a = new stdClass;
490 $a->nquestions = $gradeinfo->nquestions;
491 $a->minquestions = $lesson->minquestions;
492 lesson_set_message(get_string('numberofpagesviewednotice', 'lesson', $a));
494 lesson_set_message(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'notify');
495 $a = new stdClass;
496 $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
497 $a->total = $lesson->grade;
498 lesson_set_message(get_string('yourcurrentgradeisoutof', 'lesson', $a), 'notify');
503 $PAGE = page_create_instance($lesson->id);
504 $PAGE->set_lessonpageid($page->id);
505 $pageblocks = blocks_setup($PAGE);
507 $leftcolumnwidth = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
508 $rightcolumnwidth = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);
510 if (($edit != -1) and $PAGE->user_allowed_editing()) {
511 $USER->editing = $edit;
514 /// Print the page header, heading and tabs
515 $PAGE->print_header();
517 if ($attemptflag) {
518 print_heading(get_string('attempt', 'lesson', $retries + 1));
521 /// This calculates and prints the ongoing score
522 if ($lesson->ongoing and !empty($pageid)) {
523 lesson_print_ongoing_score($lesson);
526 require($CFG->dirroot.'/mod/lesson/viewstart.html');
528 // now starting to print the page's contents
529 if ($page->qtype == LESSON_BRANCHTABLE) {
530 print_heading(format_string($page->title));
531 } else {
532 $lesson->slideshow = false; // turn off slide show for all pages other than LESSON_BRANTCHTABLE
535 if (!$lesson->slideshow) {
536 $options = new stdClass;
537 $options->noclean = true;
538 print_simple_box('<div class="contents">'.
539 format_text($page->contents, FORMAT_MOODLE, $options).
540 '</div>', 'center');
543 // this is for modattempts option. Find the users previous answer to this page,
544 // and then display it below in answer processing
545 if (isset($USER->modattempts[$lesson->id])) {
546 $retries = count_records('lesson_grades', "lessonid", $lesson->id, "userid", $USER->id);
547 $retries--;
548 if (! $attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $USER->id AND pageid = $page->id AND retry = $retries", "timeseen")) {
549 error("Previous attempt record could not be found!");
551 $attempt = end($attempts);
554 // get the answers in a set order, the id order
555 if ($answers = get_records("lesson_answers", "pageid", $page->id, "id")) {
556 if ($page->qtype != LESSON_BRANCHTABLE) { // To fix XHTML problem (BT have their own forms)
557 echo "<form id=\"answerform\" method =\"post\" action=\"lesson.php\" autocomplete=\"off\">";
558 echo '<fieldset class="invisiblefieldset">';
559 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
560 echo "<input type=\"hidden\" name=\"action\" value=\"continue\" />";
561 echo "<input type=\"hidden\" name=\"pageid\" value=\"$pageid\" />";
562 echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
563 print_simple_box_start("center");
564 echo '<table width="100%">';
566 // default format text options
567 $options = new stdClass;
568 $options->para = false; // no <p></p>
569 $options->noclean = true;
570 // echo "qtype is $page->qtype"; // debug
571 switch ($page->qtype) {
572 case LESSON_SHORTANSWER :
573 case LESSON_NUMERICAL :
574 if (isset($USER->modattempts[$lesson->id])) {
575 $value = 'value="'.s($attempt->useranswer).'"';
576 } else {
577 $value = "";
579 echo '<tr><td style="text-align:center;"><label for="answer">'.get_string('youranswer', 'lesson').'</label>'.
580 ": <input type=\"text\" id=\"answer\" name=\"answer\" size=\"50\" maxlength=\"200\" $value />\n";
581 echo '</td></tr></table>';
582 print_simple_box_end();
583 lesson_print_submit_link(get_string('pleaseenteryouranswerinthebox', 'lesson'), 'answerform');
584 break;
585 case LESSON_TRUEFALSE :
586 shuffle($answers);
587 $i = 0;
588 foreach ($answers as $answer) {
589 echo '<tr><td valign="top">';
590 if (isset($USER->modattempts[$lesson->id]) && $answer->id == $attempt->answerid) {
591 $checked = 'checked="checked"';
592 } else {
593 $checked = '';
595 echo "<input type=\"radio\" id=\"answerid$i\" name=\"answerid\" value=\"{$answer->id}\" $checked />";
596 echo "</td><td>";
597 echo "<label for=\"answerid$i\">".format_text(trim($answer->answer), FORMAT_MOODLE, $options).'</label>';
598 echo '</td></tr>';
599 if ($answer != end($answers)) {
600 echo "<tr><td><br /></td></tr>";
602 $i++;
604 echo '</table>';
605 print_simple_box_end();
606 lesson_print_submit_link(get_string('pleasecheckoneanswer', 'lesson'), 'answerform');
607 break;
608 case LESSON_MULTICHOICE :
609 $i = 0;
610 shuffle($answers);
612 foreach ($answers as $answer) {
613 echo '<tr><td valign="top">';
614 if ($page->qoption) {
615 $checked = '';
616 if (isset($USER->modattempts[$lesson->id])) {
617 $answerids = explode(",", $attempt->useranswer);
618 if (in_array($answer->id, $answerids)) {
619 $checked = ' checked="checked"';
620 } else {
621 $checked = '';
624 // more than one answer allowed
625 echo "<input type=\"checkbox\" id=\"answerid$i\" name=\"answer[$i]\" value=\"{$answer->id}\"$checked />";
626 } else {
627 if (isset($USER->modattempts[$lesson->id]) && $answer->id == $attempt->answerid) {
628 $checked = ' checked="checked"';
629 } else {
630 $checked = '';
632 // only one answer allowed
633 echo "<input type=\"radio\" id=\"answerid$i\" name=\"answerid\" value=\"{$answer->id}\"$checked />";
635 echo '</td><td>';
636 echo "<label for=\"answerid$i\" >".format_text(trim($answer->answer), FORMAT_MOODLE, $options).'</label>';
637 echo '</td></tr>';
638 if ($answer != end($answers)) {
639 echo '<tr><td><br /></td></tr>';
641 $i++;
643 echo '</table>';
644 print_simple_box_end();
645 if ($page->qoption) {
646 $linkname = get_string('pleasecheckoneormoreanswers', 'lesson');
647 } else {
648 $linkname = get_string('pleasecheckoneanswer', 'lesson');
650 lesson_print_submit_link($linkname, 'answerform');
651 break;
653 case LESSON_MATCHING :
654 // don't suffle answers (could be an option??)
655 foreach ($answers as $answer) {
656 // get all the response
657 if ($answer->response != NULL) {
658 $responses[] = trim($answer->response);
662 $responseoptions = array();
663 if (!empty($responses)) {
664 shuffle($responses);
665 $responses = array_unique($responses);
666 foreach ($responses as $response) {
667 $responseoptions[htmlspecialchars(trim($response))] = $response;
670 if (isset($USER->modattempts[$lesson->id])) {
671 $useranswers = explode(',', $attempt->useranswer);
672 $t = 0;
674 foreach ($answers as $answer) {
675 if ($answer->response != NULL) {
676 echo '<tr><td align="right">';
677 echo "<b><label for=\"menuresponse[$answer->id]\">".
678 format_text($answer->answer,FORMAT_MOODLE,$options).
679 '</label>: </b></td><td valign="bottom">';
681 if (isset($USER->modattempts[$lesson->id])) {
682 $selected = htmlspecialchars(trim($answers[$useranswers[$t]]->response)); // gets the user's previous answer
683 choose_from_menu ($responseoptions, "response[$answer->id]", $selected);
684 $t++;
685 } else {
686 choose_from_menu ($responseoptions, "response[$answer->id]");
688 echo '</td></tr>';
689 if ($answer != end($answers)) {
690 echo '<tr><td><br /></td></tr>';
694 echo '</table>';
695 print_simple_box_end();
696 lesson_print_submit_link(get_string('pleasematchtheabovepairs', 'lesson'), 'answerform');
697 break;
698 case LESSON_BRANCHTABLE :
699 $options = new stdClass;
700 $options->para = false;
701 $buttons = array();
702 $i = 0;
703 foreach ($answers as $answer) {
704 // Each button must have its own form inorder for it to work with JavaScript turned off
705 $button = "<form id=\"answerform$i\" method=\"post\" action=\"$CFG->wwwroot/mod/lesson/lesson.php\">\n".
706 '<div>'.
707 "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n".
708 "<input type=\"hidden\" name=\"action\" value=\"continue\" />\n".
709 "<input type=\"hidden\" name=\"pageid\" value=\"$pageid\" />\n".
710 "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />\n".
711 "<input type=\"hidden\" name=\"jumpto\" value=\"$answer->jumpto\" />\n".
712 lesson_print_submit_link(strip_tags(format_text($answer->answer, FORMAT_MOODLE, $options)), "answerform$i", '', '', '', '', true).
713 '</div>'.
714 '</form>';
716 $buttons[] = $button;
717 $i++;
720 /// Set the orientation
721 if ($page->layout) {
722 $orientation = 'horizontal';
723 } else {
724 $orientation = 'vertical';
727 $fullbuttonhtml = "\n<div class=\"branchbuttoncontainer $orientation\">\n" .
728 implode("\n", $buttons).
729 "\n</div>\n";
731 if ($lesson->slideshow) {
732 $options = new stdClass;
733 $options->noclean = true;
734 echo '<div class="contents">'.format_text($page->contents, FORMAT_MOODLE, $options)."</div>\n";
735 echo '</div><!--end slideshow div-->';
736 echo $fullbuttonhtml;
737 } else {
738 echo $fullbuttonhtml;
741 break;
742 case LESSON_ESSAY :
743 if (isset($USER->modattempts[$lesson->id])) {
744 $essayinfo = unserialize($attempt->useranswer);
745 $value = s(stripslashes_safe($essayinfo->answer));
746 } else {
747 $value = "";
749 echo '<tr><td style="text-align:center;" valign="top" nowrap="nowrap"><label for="answer">'.get_string("youranswer", "lesson").'</label>:</td><td>'.
750 '<textarea id="answer" name="answer" rows="15" cols="60">'.$value."</textarea>\n";
751 echo '</td></tr></table>';
752 print_simple_box_end();
753 lesson_print_submit_link(get_string('pleaseenteryouranswerinthebox', 'lesson'), 'answerform');
754 break;
755 default: // close the tags MDL-7861
756 echo ('</table>');
757 print_simple_box_end();
758 break;
760 if ($page->qtype != LESSON_BRANCHTABLE) { // To fix XHTML problem (BT have their own forms)
761 echo '</fieldset>';
762 echo "</form>\n";
764 } else {
765 // a page without answers - find the next (logical) page
766 echo "<form id=\"pageform\" method=\"post\" action=\"$CFG->wwwroot/mod/lesson/view.php\">\n";
767 echo '<div>';
768 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
769 if ($lesson->nextpagedefault) {
770 // in Flash Card mode...
771 // ...first get number of retakes
772 $nretakes = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
773 // ...then get the page ids (lessonid the 5th param is needed to make get_records play)
774 $allpages = get_records("lesson_pages", "lessonid", $lesson->id, "id", "id,lessonid");
775 shuffle ($allpages);
776 $found = false;
777 if ($lesson->nextpagedefault == LESSON_UNSEENPAGE) {
778 foreach ($allpages as $thispage) {
779 if (!count_records("lesson_attempts", "pageid", $thispage->id, "userid",
780 $USER->id, "retry", $nretakes)) {
781 $found = true;
782 break;
785 } elseif ($lesson->nextpagedefault == LESSON_UNANSWEREDPAGE) {
786 foreach ($allpages as $thispage) {
787 if (!count_records_select("lesson_attempts", "pageid = $thispage->id AND
788 userid = $USER->id AND correct = 1 AND retry = $nretakes")) {
789 $found = true;
790 break;
794 if ($found) {
795 $newpageid = $thispage->id;
796 if ($lesson->maxpages) {
797 // check number of pages viewed (in the lesson)
798 if (count_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id,
799 "retry", $nretakes) >= $lesson->maxpages) {
800 $newpageid = LESSON_EOL;
803 } else {
804 $newpageid = LESSON_EOL;
806 } else {
807 // in normal lesson mode...
808 if (!$newpageid = get_field("lesson_pages", "nextpageid", "id", $pageid)) {
809 // this is the last page - flag end of lesson
810 $newpageid = LESSON_EOL;
813 echo "<input type=\"hidden\" name=\"pageid\" value=\"$newpageid\" />\n";
814 lesson_print_submit_link(get_string('continue', 'lesson'), 'pageform');
815 echo '</div>';
816 echo "</form>\n";
819 // Finish of the page
820 lesson_print_progress_bar($lesson, $course);
821 require($CFG->dirroot.'/mod/lesson/viewend.html');
822 } else {
823 // end of lesson reached work out grade
825 // Used to check to see if the student ran out of time
826 $outoftime = optional_param('outoftime', '', PARAM_ALPHA);
828 // Update the clock / get time information for this user
829 if (!has_capability('mod/lesson:manage', $context)) {
830 unset($USER->startlesson[$lesson->id]);
831 if (!$timer = get_records_select('lesson_timer', "lessonid = $lesson->id AND userid = $USER->id", 'starttime')) {
832 error('Error: could not find records');
833 } else {
834 $timer = array_pop($timer); // this will get the latest start time record
836 $timer->lessontime = time();
838 if (!update_record("lesson_timer", $timer)) {
839 error("Error: could not update lesson_timer table");
843 add_to_log($course->id, "lesson", "end", "view.php?id=$cm->id", "$lesson->id", $cm->id);
845 lesson_print_header($cm, $course, $lesson, 'view');
846 print_heading(get_string("congratulations", "lesson"));
847 print_simple_box_start("center");
848 $ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
849 if (isset($USER->modattempts[$lesson->id])) {
850 $ntries--; // need to look at the old attempts :)
852 if (!has_capability('mod/lesson:manage', $context)) {
854 $gradeinfo = lesson_grade($lesson, $ntries);
856 if ($gradeinfo->attempts) {
857 if (!$lesson->custom) {
858 echo "<p style=\"text-align:center;\">".get_string("numberofpagesviewed", "lesson", $gradeinfo->nquestions).
859 "</p>\n";
860 if ($lesson->minquestions) {
861 if ($gradeinfo->nquestions < $lesson->minquestions) {
862 // print a warning and set nviewed to minquestions
863 echo "<p style=\"text-align:center;\">".get_string("youshouldview", "lesson",
864 $lesson->minquestions)."</p>\n";
867 echo "<p style=\"text-align:center;\">".get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned).
868 "</p>\n";
870 $a = new stdClass;
871 $a->score = $gradeinfo->earned;
872 $a->grade = $gradeinfo->total;
873 if ($gradeinfo->nmanual) {
874 $a->tempmaxgrade = $gradeinfo->total - $gradeinfo->manualpoints;
875 $a->essayquestions = $gradeinfo->nmanual;
876 echo "<div style=\"text-align:center;\">".get_string("displayscorewithessays", "lesson", $a)."</div>";
877 } else {
878 echo "<div style=\"text-align:center;\">".get_string("displayscorewithoutessays", "lesson", $a)."</div>";
880 $a = new stdClass;
881 $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
882 $a->total = $lesson->grade;
883 echo "<p style=\"text-align:center;\">".get_string('yourcurrentgradeisoutof', 'lesson', $a)."</p>\n";
885 $grade->lessonid = $lesson->id;
886 $grade->userid = $USER->id;
887 $grade->grade = $gradeinfo->grade;
888 $grade->completed = time();
889 if (!$lesson->practice) {
890 if (isset($USER->modattempts[$lesson->id])) { // if reviewing, make sure update old grade record
891 if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id and userid = $USER->id", "completed")) {
892 error("Could not find Grade Records");
894 $oldgrade = end($grades);
895 $grade->id = $oldgrade->id;
896 if (!$update = update_record("lesson_grades", $grade)) {
897 error("Navigation: grade not updated");
899 } else {
900 if (!$newgradeid = insert_record("lesson_grades", $grade)) {
901 error("Navigation: grade not inserted");
904 } else {
905 if (!delete_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id, "retry", $ntries)) {
906 error("Could not delete lesson attempts");
909 } else {
910 if ($lesson->timed) {
911 if ($outoftime == 'normal') {
912 $grade = new stdClass;
913 $grade->lessonid = $lesson->id;
914 $grade->userid = $USER->id;
915 $grade->grade = 0;
916 $grade->completed = time();
917 if (!$lesson->practice) {
918 if (!$newgradeid = insert_record("lesson_grades", $grade)) {
919 error("Navigation: grade not inserted");
922 echo get_string("eolstudentoutoftimenoanswers", "lesson");
924 } else {
925 echo get_string("welldone", "lesson");
929 // update central gradebook
930 lesson_update_grades($lesson, $USER->id);
932 } else {
933 // display for teacher
934 echo "<p style=\"text-align:center;\">".get_string("displayofgrade", "lesson")."</p>\n";
936 print_simple_box_end(); //End of Lesson button to Continue.
938 // after all the grade processing, check to see if "Show Grades" is off for the course
939 // if yes, redirect to the course page
940 if (!$course->showgrades) {
941 redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
944 // high scores code
945 if ($lesson->highscores && !has_capability('mod/lesson:manage', $context) && !$lesson->practice) {
946 echo "<div style=\"text-align:center;\"><br />";
947 if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
948 $madeit = false;
949 if ($highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
950 // get all the high scores into an array
951 $topscores = array();
952 $uniquescores = array();
953 foreach ($highscores as $highscore) {
954 $grade = $grades[$highscore->gradeid]->grade;
955 $topscores[] = $grade;
956 $uniquescores[$grade] = 1;
958 // sort to find the lowest score
959 sort($topscores);
960 $lowscore = $topscores[0];
962 if ($gradeinfo->grade >= $lowscore || count($uniquescores) <= $lesson->maxhighscores) {
963 $madeit = true;
966 if (!$highscores or $madeit) {
967 echo '<p>'.get_string("youmadehighscore", "lesson", $lesson->maxhighscores).
968 '</p>
969 <form method="post" id="highscores" action="'.$CFG->wwwroot.'/mod/lesson/highscores.php">
970 <div>
971 <input type="hidden" name="mode" value="add" />
972 <input type="hidden" name="id" value="'.$cm->id.'" />
973 <input type="hidden" name="sesskey" value="'.sesskey().'" />
974 <p>';
975 lesson_print_submit_link(get_string('clicktopost', 'lesson'), 'highscores');
976 echo '</p>
977 </div>
978 </form>';
979 } else {
980 echo get_string("nothighscore", "lesson", $lesson->maxhighscores)."<br />";
983 echo "<br /><div style=\"padding: 5px;\" class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&amp;link=1\">".get_string("viewhighscores", "lesson").'</a></div>';
984 echo "</div>";
987 if ($lesson->modattempts && !has_capability('mod/lesson:manage', $context)) {
988 // make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
989 // look at the attempt records to find the first QUESTION page that the user answered, then use that page id
990 // to pass to view again. This is slick cause it wont call the empty($pageid) code
991 // $ntries is decremented above
992 if (!$attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $USER->id AND retry = $ntries", "timeseen")) {
993 $attempts = array();
995 $firstattempt = current($attempts);
996 $pageid = $firstattempt->pageid;
997 // IF the student wishes to review, need to know the last question page that the student answered. This will help to make
998 // sure that the student can leave the lesson via pushing the continue button.
999 $lastattempt = end($attempts);
1000 $USER->modattempts[$lesson->id] = $lastattempt->pageid;
1001 echo "<div style=\"text-align:center; padding:5px;\" class=\"lessonbutton standardbutton\"><a href=\"view.php?id=$cm->id&amp;pageid=$pageid\">".get_string("reviewlesson", "lesson")."</a></div>\n";
1002 } elseif ($lesson->modattempts && has_capability('mod/lesson:manage', $context)) {
1003 echo "<p style=\"text-align:center;\">".get_string("modattemptsnoteacher", "lesson")."</p>";
1006 if ($lesson->activitylink) {
1007 if ($module = get_record('course_modules', 'id', $lesson->activitylink)) {
1008 if ($modname = get_field('modules', 'name', 'id', $module->module))
1009 if ($instance = get_record($modname, 'id', $module->instance)) {
1010 echo "<div style=\"text-align:center; padding:5px;\" class=\"lessonbutton standardbutton\">".
1011 "<a href=\"$CFG->wwwroot/mod/$modname/view.php?id=$lesson->activitylink\">".
1012 get_string('activitylinkname', 'lesson', $instance->name)."</a></div>\n";
1017 echo "<div style=\"text-align:center; padding:5px;\" class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">".get_string('returnto', 'lesson', format_string($course->fullname, true))."</a></div>\n";
1018 echo "<div style=\"text-align:center; padding:5px;\" class=\"lessonbutton standardbutton\"><a href=\"$CFG->wwwroot/grade/index.php?id=$course->id\">".get_string('viewgrades', 'lesson')."</a></div>\n";
1021 /// Finish the page
1022 print_footer($course);