MDL-31775 lesson: fix undefined variable warnings
[moodle.git] / mod / lesson / view.php
CommitLineData
86342d63 1<?php
0a4abb73
SH
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
5631925a 18/**
19 * This page prints a particular instance of lesson
20 *
cc3dbaaa
PS
21 * @package mod
22 * @subpackage lesson
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
5631925a 25 **/
bbcbc0fe 26
0a4abb73
SH
27require_once(dirname(__FILE__) . '/../../config.php');
28require_once($CFG->dirroot.'/mod/lesson/locallib.php');
29require_once($CFG->dirroot.'/mod/lesson/view_form.php');
30require_once($CFG->libdir . '/completionlib.php');
7143a284 31
0a4abb73
SH
32$id = required_param('id', PARAM_INT); // Course Module ID
33$pageid = optional_param('pageid', NULL, PARAM_INT); // Lesson Page ID
34$edit = optional_param('edit', -1, PARAM_BOOL);
bfc638bb 35$userpassword = optional_param('userpassword','',PARAM_RAW);
510f8b0e 36$backtocourse = optional_param('backtocourse', false, PARAM_RAW);
fe75e76b 37
b3daa926
PS
38$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);;
39$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
40$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
41
0a4abb73 42require_login($course, false, $cm);
bbcbc0fe 43
510f8b0e
PS
44if ($backtocourse) {
45 redirect(new moodle_url('/course/view.php', array('id'=>$course->id)));
46}
47
6553cda7 48// Mark as viewed
49$completion = new completion_info($course);
50$completion->set_module_viewed($cm);
51
a6855934 52$url = new moodle_url('/mod/lesson/view.php', array('id'=>$id));
0a4abb73
SH
53if ($pageid !== null) {
54 $url->param('pageid', $pageid);
55}
56$PAGE->set_url($url);
fe75e76b 57
0a4abb73
SH
58$context = get_context_instance(CONTEXT_MODULE, $cm->id);
59$canmanage = has_capability('mod/lesson:manage', $context);
60
649cf95d 61$lessonoutput = $PAGE->get_renderer('mod_lesson');
bbcbc0fe 62
abd5c24e
RW
63$reviewmode = false;
64$userhasgrade = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
65if ($userhasgrade && !$lesson->retake) {
66 $reviewmode = true;
67}
68
5631925a 69/// Check these for students only TODO: Find a better method for doing this!
fb992d71 70/// Check lesson availability
71/// Check for password
5631925a 72/// Check dependencies
fb992d71 73/// Check for high scores
0a4abb73
SH
74if (!$canmanage) {
75 if (!$lesson->is_accessible()) { // Deadline restrictions
448052a5 76 echo $lessonoutput->header($lesson, $cm);
0a4abb73
SH
77 if ($lesson->deadline != 0 && time() > $lesson->deadline) {
78 echo $lessonoutput->lesson_inaccessible(get_string('lessonclosed', 'lesson', userdate($lesson->deadline)));
79 } else {
80 echo $lessonoutput->lesson_inaccessible(get_string('lessonopen', 'lesson', userdate($lesson->available)));
81 }
82 echo $lessonoutput->footer();
83 exit();
84 } else if ($lesson->usepassword && empty($USER->lessonloggedin[$lesson->id])) { // Password protected lesson code
85 $correctpass = false;
86 if (!empty($userpassword) && (($lesson->password == md5(trim($userpassword))) || ($lesson->password == trim($userpassword)))) {
87 // with or without md5 for backward compatibility (MDL-11090)
88 $USER->lessonloggedin[$lesson->id] = true;
89 if ($lesson->highscores) {
90 // Logged in - redirect so we go through all of these checks before starting the lesson.
91 redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id");
f521f98a 92 }
0a4abb73 93 } else {
448052a5 94 echo $lessonoutput->header($lesson, $cm);
bfc638bb 95 echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
0a4abb73 96 echo $lessonoutput->footer();
5620acd8 97 exit();
0a4abb73
SH
98 }
99 } else if ($lesson->dependency) { // check for dependencies
100 if ($dependentlesson = $DB->get_record('lesson', array('id' => $lesson->dependency))) {
101 // lesson exists, so we can proceed
102 $conditions = unserialize($lesson->conditions);
103 // assume false for all
104 $errors = array();
fe75e76b 105
0a4abb73
SH
106 // check for the timespent condition
107 if ($conditions->timespent) {
108 $timespent = false;
109 if ($attempttimes = $DB->get_records('lesson_timer', array("userid"=>$USER->id, "lessonid"=>$dependentlesson->id))) {
110 // go through all the times and test to see if any of them satisfy the condition
111 foreach($attempttimes as $attempttime) {
112 $duration = $attempttime->lessontime - $attempttime->starttime;
113 if ($conditions->timespent < $duration/60) {
114 $timespent = true;
115 }
d1b025d0 116 }
5620acd8 117 }
0a4abb73
SH
118 if (!$timespent) {
119 $errors[] = get_string('timespenterror', 'lesson', $conditions->timespent);
5620acd8 120 }
5620acd8 121 }
fe75e76b 122
0a4abb73
SH
123 // check for the gradebetterthan condition
124 if($conditions->gradebetterthan) {
fb992d71 125 $gradebetterthan = false;
0a4abb73
SH
126 if ($studentgrades = $DB->get_records('lesson_grades', array("userid"=>$USER->id, "lessonid"=>$dependentlesson->id))) {
127 // go through all the grades and test to see if any of them satisfy the condition
128 foreach($studentgrades as $studentgrade) {
129 if ($studentgrade->grade >= $conditions->gradebetterthan) {
130 $gradebetterthan = true;
ee3f2275 131 }
ee3f2275 132 }
fb992d71 133 }
134 if (!$gradebetterthan) {
135 $errors[] = get_string('gradebetterthanerror', 'lesson', $conditions->gradebetterthan);
136 }
0a4abb73
SH
137 }
138
139 // check for the completed condition
140 if ($conditions->completed) {
141 if (!$DB->count_records('lesson_grades', array('userid'=>$USER->id, 'lessonid'=>$dependentlesson->id))) {
142 $errors[] = get_string('completederror', 'lesson');
fe75e76b 143 }
ac8e16be 144 }
fe75e76b 145
0a4abb73 146 if (!empty($errors)) { // print out the errors if any
448052a5 147 echo $lessonoutput->header($lesson, $cm);
0a4abb73
SH
148 echo $lessonoutput->dependancy_errors($dependentlesson, $errors);
149 echo $lessonoutput->footer();
150 exit();
151 }
5631925a 152 }
0a4abb73
SH
153 } else if ($lesson->highscores && !$lesson->practice && !optional_param('viewed', 0, PARAM_INT) && empty($pageid)) {
154 // Display high scores before starting lesson
a6855934 155 redirect(new moodle_url('/mod/lesson/highscores.php', array("id"=>$cm->id)));
5631925a 156 }
0a4abb73 157}
5631925a 158
159 // this is called if a student leaves during a lesson
0a4abb73
SH
160if ($pageid == LESSON_UNSEENBRANCHPAGE) {
161 $pageid = lesson_unseen_question_jump($lesson, $USER->id, $pageid);
162}
163
164// display individual pages and their sets of answers
165// if pageid is EOL then the end of the lesson has been reached
166// for flow, changed to simple echo for flow styles, michaelp, moved lesson name and page title down
167$attemptflag = false;
168if (empty($pageid)) {
169 // make sure there are pages to view
170 if (!$DB->get_field('lesson_pages', 'id', array('lessonid' => $lesson->id, 'prevpageid' => 0))) {
171 if (!$canmanage) {
172 $lesson->add_message(get_string('lessonnotready2', 'lesson')); // a nice message to the student
173 } else {
174 if (!$DB->count_records('lesson_pages', array('lessonid'=>$lesson->id))) {
175 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); // no pages - redirect to add pages
5631925a 176 } else {
0a4abb73 177 $lesson->add_message(get_string('lessonpagelinkingbroken', 'lesson')); // ok, bad mojo
5631925a 178 }
fb992d71 179 }
0a4abb73 180 }
fe75e76b 181
0a4abb73 182 add_to_log($course->id, 'lesson', 'start', 'view.php?id='. $cm->id, $lesson->id, $cm->id);
fe75e76b 183
0a4abb73
SH
184 // if no pageid given see if the lesson has been started
185 $retries = $DB->count_records('lesson_grades', array("lessonid" => $lesson->id, "userid" => $USER->id));
186 if ($retries > 0) {
187 $attemptflag = true;
188 }
fe75e76b 189
0a4abb73
SH
190 if (isset($USER->modattempts[$lesson->id])) {
191 unset($USER->modattempts[$lesson->id]); // if no pageid, then student is NOT reviewing
192 }
86b7a428 193
0a4abb73
SH
194 // if there are any questions have been answered correctly in this attempt
195 $corrrectattempts = $lesson->get_attempts($retries, true);
0094c90c 196 if (!empty($corrrectattempts)) {
49ef5d4c
RW
197 $attempt = end($corrrectattempts);
198 $jumpto = $DB->get_field('lesson_answers', 'jumpto', array('id' => $attempt->answerid));
199 // convert the jumpto to a proper page id
200 if ($jumpto == 0) { // unlikely value!
201 $lastpageseen = $attempt->pageid;
202 } elseif ($jumpto == LESSON_NEXTPAGE) {
203 if (!$lastpageseen = $DB->get_field('lesson_pages', 'nextpageid', array('id' => $attempt->pageid))) {
204 // no nextpage go to end of lesson
205 $lastpageseen = LESSON_EOL;
271fea97 206 }
49ef5d4c
RW
207 } else {
208 $lastpageseen = $jumpto;
fb992d71 209 }
0a4abb73 210 }
fe75e76b 211
0a4abb73
SH
212 if ($branchtables = $DB->get_records('lesson_branch', array("lessonid"=>$lesson->id, "userid"=>$USER->id, "retry"=>$retries), 'timeseen DESC')) {
213 // in here, user has viewed a branch table
214 $lastbranchtable = current($branchtables);
215 if (count($corrrectattempts)>0) {
216 foreach($corrrectattempts as $attempt) {
217 if ($lastbranchtable->timeseen > $attempt->timeseen) {
218 // branch table was viewed later than the last attempt
219 $lastpageseen = $lastbranchtable->pageid;
220 }
fb992d71 221 break;
222 }
0a4abb73
SH
223 } else {
224 // hasnt answered any questions but has viewed a branch table
225 $lastpageseen = $lastbranchtable->pageid;
fb992d71 226 }
fb992d71 227 }
0a4abb73 228 if (isset($lastpageseen) && $DB->count_records('lesson_attempts', array('lessonid'=>$lesson->id, 'userid'=>$USER->id, 'retry'=>$retries)) > 0) {
448052a5 229 echo $lessonoutput->header($lesson, $cm);
0a4abb73
SH
230 if ($lesson->timed) {
231 if ($lesson->retake) {
a6855934 232 $continuelink = new single_button(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id, 'pageid'=>$lesson->firstpageid, 'startlastseen'=>'no')), get_string('continue', 'lesson'), 'get');
a82fb0b6 233 echo '<div class="center leftduring">'.$lessonoutput->message(get_string('leftduringtimed', 'lesson'), $continuelink).'</div>';
6707a280 234 } else {
a6855934 235 $courselink = new single_button(new moodle_url('/course/view.php', array('id'=>$PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
a82fb0b6 236 echo '<div class="center leftduring">'.$lessonoutput->message(get_string('leftduringtimednoretake', 'lesson'), $courselink).'</div>';
6707a280 237 }
0a4abb73
SH
238 } else {
239 echo $lessonoutput->continue_links($lesson, $lastpageseen);
fb992d71 240 }
0a4abb73
SH
241 echo $lessonoutput->footer();
242 exit();
243 }
fe75e76b 244
0a4abb73
SH
245 if ($attemptflag) {
246 if (!$lesson->retake) {
448052a5 247 echo $lessonoutput->header($lesson, $cm, 'view');
a6855934 248 $courselink = new single_button(new moodle_url('/course/view.php', array('id'=>$PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
0a4abb73
SH
249 echo $lessonoutput->message(get_string("noretake", "lesson"), $courselink);
250 echo $lessonoutput->footer();
251 exit();
fb992d71 252 }
0a4abb73
SH
253 }
254 // start at the first page
255 if (!$pageid = $DB->get_field('lesson_pages', 'id', array('lessonid' => $lesson->id, 'prevpageid' => 0))) {
256 print_error('cannotfindfirstpage', 'lesson');
257 }
258 /// This is the code for starting a timed test
259 if(!isset($USER->startlesson[$lesson->id]) && !$canmanage) {
260 $lesson->start_timer();
261 }
262}
263
264$currenttab = 'view';
265$extraeditbuttons = false;
266$lessonpageid = null;
267$timer = null;
268
269if ($pageid != LESSON_EOL) {
270 /// This is the code updates the lessontime for a timed test
271 $startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
272 if ($startlastseen == 'no') {
273 // this deletes old records not totally sure if this is necessary anymore
274 $retries = $DB->count_records('lesson_grades', array('lessonid'=>$lesson->id, 'userid'=>$USER->id));
275 $DB->delete_records('lesson_attempts', array('userid' => $USER->id, 'lessonid' => $lesson->id, 'retry' => $retries));
276 $DB->delete_records('lesson_branch', array('userid' => $USER->id, 'lessonid' => $lesson->id, 'retry' => $retries));
277 }
fe75e76b 278
2f67a9b3 279 $page = $lesson->load_page($pageid);
0a4abb73
SH
280 // Check if the page is of a special type and if so take any nessecary action
281 $newpageid = $page->callback_on_view($canmanage);
282 if (is_numeric($newpageid)) {
283 $page = $lesson->load_page($newpageid);
284 }
5aeebbc8 285
0a4abb73 286 add_to_log($PAGE->course->id, 'lesson', 'view', 'view.php?id='. $PAGE->cm->id, $page->id, $PAGE->cm->id);
fe75e76b 287
0a4abb73
SH
288 // This is where several messages (usually warnings) are displayed
289 // all of this is displayed above the actual page
4c99e342 290
0a4abb73
SH
291 // check to see if the user can see the left menu
292 if (!$canmanage) {
293 $lesson->displayleft = lesson_displayleftif($lesson);
c022b496 294
0a4abb73
SH
295 $continue = ($startlastseen !== '');
296 $restart = ($continue && $startlastseen == 'yes');
297 $timer = $lesson->update_timer($continue, $restart);
6707a280 298
0a4abb73
SH
299 if ($lesson->timed) {
300 $timeleft = ($timer->starttime + $lesson->maxtime * 60) - time();
301 if ($timeleft <= 0) {
302 // Out of time
303 $lesson->add_message(get_string('eolstudentoutoftime', 'lesson'));
a6855934 304 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>LESSON_EOL, 'outoftime'=>'normal')));
0a4abb73
SH
305 die; // Shouldn't be reached, but make sure
306 } else if ($timeleft < 60) {
307 // One minute warning
308 $lesson->add_message(get_string('studentoneminwarning', 'lesson'));
fb992d71 309 }
310 }
fe75e76b 311
0a4abb73
SH
312 if ($page->qtype == LESSON_PAGE_BRANCHTABLE && $lesson->minquestions) {
313 // tell student how many questions they have seen, how many are required and their grade
314 $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
315 $gradeinfo = lesson_grade($lesson, $ntries);
316 if ($gradeinfo->attempts) {
317 if ($gradeinfo->nquestions < $lesson->minquestions) {
5631925a 318 $a = new stdClass;
0a4abb73
SH
319 $a->nquestions = $gradeinfo->nquestions;
320 $a->minquestions = $lesson->minquestions;
321 $lesson->add_message(get_string('numberofpagesviewednotice', 'lesson', $a));
9e93d4d0 322 }
abd5c24e 323
0a4abb73
SH
324 $a = new stdClass;
325 $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
326 $a->total = $lesson->grade;
abd5c24e
RW
327 if (!$reviewmode && !$lesson->retake){
328 $lesson->add_message(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'notify');
329 $lesson->add_message(get_string('yourcurrentgradeisoutof', 'lesson', $a), 'notify');
330 }
9e93d4d0 331 }
fb992d71 332 }
0a4abb73
SH
333 } else {
334 $timer = null;
335 if ($lesson->timed) {
336 $lesson->add_message(get_string('teachertimerwarning', 'lesson'));
4262a2f8 337 }
0a4abb73
SH
338 if (lesson_display_teacher_warning($lesson)) {
339 // This is the warning msg for teachers to inform them that cluster
340 // and unseen does not work while logged in as a teacher
341 $warningvars->cluster = get_string('clusterjump', 'lesson');
342 $warningvars->unseen = get_string('unseenpageinbranch', 'lesson');
343 $lesson->add_message(get_string('teacherjumpwarning', 'lesson', $warningvars));
4262a2f8 344 }
0a4abb73 345 }
5631925a 346
a6855934 347 $PAGE->set_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => $page->id));
0a4abb73
SH
348 $PAGE->set_subpage($page->id);
349 $currenttab = 'view';
350 $extraeditbuttons = true;
351 $lessonpageid = $page->id;
fe75e76b 352
0a4abb73
SH
353 if (($edit != -1) && $PAGE->user_allowed_editing()) {
354 $USER->editing = $edit;
355 }
fe75e76b 356
0a4abb73 357 if (is_array($page->answers) && count($page->answers)>0) {
fb992d71 358 // this is for modattempts option. Find the users previous answer to this page,
359 // and then display it below in answer processing
fe75e76b 360 if (isset($USER->modattempts[$lesson->id])) {
318e3745 361 $retries = $DB->count_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$USER->id));
0a4abb73 362 if (!$attempts = $lesson->get_attempts($retries-1, false, $page->id)) {
1858ba0c 363 print_error('cannotfindpreattempt', 'lesson');
5620acd8 364 }
fb992d71 365 $attempt = end($attempts);
abd5c24e 366 $USER->modattempts[$lesson->id] = $attempt;
0a4abb73
SH
367 } else {
368 $attempt = false;
fb992d71 369 }
0a4abb73
SH
370 $lessoncontent = $lessonoutput->display_page($lesson, $page, $attempt);
371 } else {
0a4abb73
SH
372 $data = new stdClass;
373 $data->id = $PAGE->cm->id;
1c510e2e 374 $data->pageid = $page->id;
0343553f 375 $data->newpageid = $lesson->get_next_page($page->nextpageid);
fe75e76b 376
1c510e2e
SH
377 $customdata = array(
378 'title' => $page->title,
379 'contents' => $page->get_contents()
380 );
381 $mform = new lesson_page_without_answers($CFG->wwwroot.'/mod/lesson/continue.php', $customdata);
0a4abb73
SH
382 $mform->set_data($data);
383 ob_start();
384 $mform->display();
385 $lessoncontent = ob_get_contents();
386 ob_end_clean();
387 }
fe75e76b 388
d9c26e21 389 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
448052a5 390 echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid);
0a4abb73 391 if ($attemptflag) {
e481859f
RW
392 // We are using level 3 header because attempt heading is a sub-heading of lesson title (MDL-30911).
393 echo $OUTPUT->heading(get_string('attempt', 'lesson', $retries), 3);
0a4abb73
SH
394 }
395 /// This calculates and prints the ongoing score
abd5c24e 396 if ($lesson->ongoing && !empty($pageid) && !$reviewmode) {
0a4abb73
SH
397 echo $lessonoutput->ongoing_score($lesson);
398 }
399 if ($lesson->displayleft) {
400 echo '<a name="maincontent" id="maincontent" title="' . get_string('anchortitle', 'lesson') . '"></a>';
401 }
402 echo $lessoncontent;
403 echo $lessonoutput->slideshow_end();
404 echo $lessonoutput->progress_bar($lesson);
405 echo $lessonoutput->footer();
406
407} else {
408
409 $lessoncontent = '';
410 // end of lesson reached work out grade
411 // Used to check to see if the student ran out of time
412 $outoftime = optional_param('outoftime', '', PARAM_ALPHA);
413
414 // Update the clock / get time information for this user
415 add_to_log($course->id, "lesson", "end", "view.php?id=".$PAGE->cm->id, "$lesson->id", $PAGE->cm->id);
416
e481859f
RW
417 // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
418 $lessoncontent .= $OUTPUT->heading(get_string("congratulations", "lesson"), 3);
0a4abb73
SH
419 $lessoncontent .= $OUTPUT->box_start('generalbox boxaligncenter');
420 $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
421 if (isset($USER->modattempts[$lesson->id])) {
422 $ntries--; // need to look at the old attempts :)
423 }
424 if (!$canmanage) {
425 $lesson->stop_timer();
426 $gradeinfo = lesson_grade($lesson, $ntries);
427
428 if ($gradeinfo->attempts) {
429 if (!$lesson->custom) {
430 $lessoncontent .= $lessonoutput->paragraph(get_string("numberofpagesviewed", "lesson", $gradeinfo->nquestions), 'center');
431 if ($lesson->minquestions) {
432 if ($gradeinfo->nquestions < $lesson->minquestions) {
433 // print a warning and set nviewed to minquestions
434 $lessoncontent .= $lessonoutput->paragraph(get_string("youshouldview", "lesson", $lesson->minquestions), 'center');
fb992d71 435 }
0a4abb73
SH
436 }
437 $lessoncontent .= $lessonoutput->paragraph(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'center');
fb992d71 438 }
0a4abb73
SH
439 $a = new stdClass;
440 $a->score = $gradeinfo->earned;
441 $a->grade = $gradeinfo->total;
442 if ($gradeinfo->nmanual) {
443 $a->tempmaxgrade = $gradeinfo->total - $gradeinfo->manualpoints;
444 $a->essayquestions = $gradeinfo->nmanual;
445 $lessoncontent .= $OUTPUT->box(get_string("displayscorewithessays", "lesson", $a), 'center');
446 } else {
447 $lessoncontent .= $OUTPUT->box(get_string("displayscorewithoutessays", "lesson", $a), 'center');
362f2ce9 448 }
0a4abb73
SH
449 $a = new stdClass;
450 $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
451 $a->total = $lesson->grade;
452 $lessoncontent .= $lessonoutput->paragraph(get_string("yourcurrentgradeisoutof", "lesson", $a), 'center');
453
39790bd8 454 $grade = new stdClass();
0a4abb73
SH
455 $grade->lessonid = $lesson->id;
456 $grade->userid = $USER->id;
457 $grade->grade = $gradeinfo->grade;
458 $grade->completed = time();
459 if (!$lesson->practice) {
460 if (isset($USER->modattempts[$lesson->id])) { // if reviewing, make sure update old grade record
461 if (!$grades = $DB->get_records("lesson_grades", array("lessonid" => $lesson->id, "userid" => $USER->id), "completed DESC", '*', 0, 1)) {
462 print_error('cannotfindgrade', 'lesson');
45c27c7e 463 }
0a4abb73
SH
464 $oldgrade = array_shift($grades);
465 $grade->id = $oldgrade->id;
466 $DB->update_record("lesson_grades", $grade);
45c27c7e 467 } else {
0a4abb73 468 $newgradeid = $DB->insert_record("lesson_grades", $grade);
bbcbc0fe 469 }
fb992d71 470 } else {
0a4abb73 471 $DB->delete_records("lesson_attempts", array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $ntries));
5620acd8 472 }
0a4abb73
SH
473 } else {
474 if ($lesson->timed) {
475 if ($outoftime == 'normal') {
39790bd8 476 $grade = new stdClass();;
0a4abb73
SH
477 $grade->lessonid = $lesson->id;
478 $grade->userid = $USER->id;
479 $grade->grade = 0;
480 $grade->completed = time();
481 if (!$lesson->practice) {
482 $newgradeid = $DB->insert_record("lesson_grades", $grade);
483 }
484 $lessoncontent .= get_string("eolstudentoutoftimenoanswers", "lesson");
485 }
fb992d71 486 } else {
0a4abb73 487 $lessoncontent .= get_string("welldone", "lesson");
ac8e16be 488 }
fb992d71 489 }
4262a2f8 490
0a4abb73
SH
491 // update central gradebook
492 lesson_update_grades($lesson, $USER->id);
4262a2f8 493
0a4abb73
SH
494 } else {
495 // display for teacher
496 $lessoncontent .= $lessonoutput->paragraph(get_string("displayofgrade", "lesson"), 'center');
497 }
498 $lessoncontent .= $OUTPUT->box_end(); //End of Lesson button to Continue.
fe75e76b 499
0a4abb73
SH
500 // after all the grade processing, check to see if "Show Grades" is off for the course
501 // if yes, redirect to the course page
502 if (!$course->showgrades) {
a6855934 503 redirect(new moodle_url('/course/view.php', array('id'=>$course->id)));
0a4abb73 504 }
fe75e76b 505
0a4abb73
SH
506 // high scores code
507 if ($lesson->highscores && !$canmanage && !$lesson->practice) {
508 $lessoncontent .= $OUTPUT->box_start('center');
509 if ($grades = $DB->get_records("lesson_grades", array("lessonid" => $lesson->id), "completed")) {
510 $madeit = false;
511 if ($highscores = $DB->get_records("lesson_high_scores", array("lessonid" => $lesson->id))) {
512 // get all the high scores into an array
513 $topscores = array();
514 $uniquescores = array();
515 foreach ($highscores as $highscore) {
516 $grade = $grades[$highscore->gradeid]->grade;
517 $topscores[] = $grade;
518 $uniquescores[$grade] = 1;
fb992d71 519 }
0a4abb73
SH
520 // sort to find the lowest score
521 sort($topscores);
522 $lowscore = $topscores[0];
fe75e76b 523
0a4abb73
SH
524 if ($gradeinfo->grade >= $lowscore || count($uniquescores) <= $lesson->maxhighscores) {
525 $madeit = true;
fb992d71 526 }
0a4abb73
SH
527 }
528 if (!$highscores or $madeit) {
529 $lessoncontent .= $lessonoutput->paragraph(get_string("youmadehighscore", "lesson", $lesson->maxhighscores), 'center');
a6855934 530 $aurl = new moodle_url('/mod/lesson/highscores.php', array('id'=>$PAGE->cm->id, 'sesskey'=>sesskey()));
5c2ed7e2 531 $lessoncontent .= $OUTPUT->single_button($aurl, get_string('clicktopost', 'lesson'));
fb992d71 532 } else {
0a4abb73 533 $lessoncontent .= get_string("nothighscore", "lesson", $lesson->maxhighscores)."<br />";
92bcca38 534 }
fb992d71 535 }
a6855934 536 $url = new moodle_url('/mod/lesson/highscores.php', array('id'=>$PAGE->cm->id, 'link'=>'1'));
0f4c64b7 537 $lessoncontent .= html_writer::link($url, get_string('viewhighscores', 'lesson'), array('class'=>'centerpadded lessonbutton standardbutton'));
0a4abb73
SH
538 $lessoncontent .= $OUTPUT->box_end();
539 }
fe75e76b 540
0a4abb73
SH
541 if ($lesson->modattempts && !$canmanage) {
542 // make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
543 // look at the attempt records to find the first QUESTION page that the user answered, then use that page id
544 // to pass to view again. This is slick cause it wont call the empty($pageid) code
545 // $ntries is decremented above
546 if (!$attempts = $lesson->get_attempts($ntries)) {
547 $attempts = array();
abd5c24e
RW
548 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id));
549 } else {
550 $firstattempt = current($attempts);
551 $pageid = $firstattempt->pageid;
552 // IF the student wishes to review, need to know the last question page that the student answered. This will help to make
553 // sure that the student can leave the lesson via pushing the continue button.
554 $lastattempt = end($attempts);
555 $USER->modattempts[$lesson->id] = $lastattempt->pageid;
556
557 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$pageid));
0a4abb73 558 }
0f4c64b7 559 $lessoncontent .= html_writer::link($url, get_string('reviewlesson', 'lesson'), array('class' => 'centerpadded lessonbutton standardbutton'));
0a4abb73
SH
560 } elseif ($lesson->modattempts && $canmanage) {
561 $lessoncontent .= $lessonoutput->paragraph(get_string("modattemptsnoteacher", "lesson"), 'centerpadded');
562 }
114f47a7 563
0a4abb73 564 if ($lesson->activitylink) {
57cd3739 565 $lessoncontent .= $lesson->link_for_activitylink();
0a4abb73 566 }
fe75e76b 567
a6855934 568 $url = new moodle_url('/course/view.php', array('id'=>$course->id));
0f4c64b7 569 $lessoncontent .= html_writer::link($url, get_string('returnto', 'lesson', format_string($course->fullname, true)), array('class'=>'centerpadded lessonbutton standardbutton'));
bbcbc0fe 570
a6855934 571 $url = new moodle_url('/grade/index.php', array('id'=>$course->id));
0f4c64b7 572 $lessoncontent .= html_writer::link($url, get_string('viewgrades', 'lesson'), array('class'=>'centerpadded lessonbutton standardbutton'));
fb992d71 573
d9c26e21 574 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
448052a5 575 echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid);
0a4abb73
SH
576 echo $lessoncontent;
577 echo $lessonoutput->footer();
578}