MDL-51735 mod_lesson: A lesson with no content shows a message.
[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 *
9b24f68b 21 * @package mod_lesson
cc3dbaaa
PS
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
5631925a 24 **/
bbcbc0fe 25
1fcf0ca8 26require_once(__DIR__ . '/../../config.php');
0a4abb73
SH
27require_once($CFG->dirroot.'/mod/lesson/locallib.php');
28require_once($CFG->dirroot.'/mod/lesson/view_form.php');
29require_once($CFG->libdir . '/completionlib.php');
667ef028 30require_once($CFG->libdir . '/grade/constants.php');
7143a284 31
0a4abb73 32$id = required_param('id', PARAM_INT); // Course Module ID
ecea65ca 33$pageid = optional_param('pageid', null, PARAM_INT); // Lesson Page ID
0a4abb73 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
0e35ba6f 38$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
74df2951 39$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
b3daa926
PS
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
e0e1a83e
JMV
48// Apply overrides.
49$lesson->update_effective_access($USER->id);
50
6553cda7 51// Mark as viewed
52$completion = new completion_info($course);
53$completion->set_module_viewed($cm);
54
a6855934 55$url = new moodle_url('/mod/lesson/view.php', array('id'=>$id));
0a4abb73
SH
56if ($pageid !== null) {
57 $url->param('pageid', $pageid);
58}
59$PAGE->set_url($url);
fe75e76b 60
5918e371 61$context = context_module::instance($cm->id);
0a4abb73
SH
62$canmanage = has_capability('mod/lesson:manage', $context);
63
649cf95d 64$lessonoutput = $PAGE->get_renderer('mod_lesson');
bbcbc0fe 65
abd5c24e
RW
66$reviewmode = false;
67$userhasgrade = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
68if ($userhasgrade && !$lesson->retake) {
69 $reviewmode = true;
70}
71
5631925a 72/// Check these for students only TODO: Find a better method for doing this!
fb992d71 73/// Check lesson availability
74/// Check for password
5631925a 75/// Check dependencies
0a4abb73
SH
76if (!$canmanage) {
77 if (!$lesson->is_accessible()) { // Deadline restrictions
d43d3418 78 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('notavailable'));
0a4abb73
SH
79 if ($lesson->deadline != 0 && time() > $lesson->deadline) {
80 echo $lessonoutput->lesson_inaccessible(get_string('lessonclosed', 'lesson', userdate($lesson->deadline)));
81 } else {
82 echo $lessonoutput->lesson_inaccessible(get_string('lessonopen', 'lesson', userdate($lesson->available)));
83 }
84 echo $lessonoutput->footer();
85 exit();
86 } else if ($lesson->usepassword && empty($USER->lessonloggedin[$lesson->id])) { // Password protected lesson code
87 $correctpass = false;
88 if (!empty($userpassword) && (($lesson->password == md5(trim($userpassword))) || ($lesson->password == trim($userpassword)))) {
541c5b85 89 require_sesskey();
0a4abb73 90 // with or without md5 for backward compatibility (MDL-11090)
e0e1a83e 91 $correctpass = true;
0a4abb73 92 $USER->lessonloggedin[$lesson->id] = true;
bb685cf3 93
e0e1a83e 94 } else if (isset($lesson->extrapasswords)) {
541c5b85 95
e0e1a83e
JMV
96 // Group overrides may have additional passwords.
97 foreach ($lesson->extrapasswords as $password) {
98 if (strcmp($password, md5(trim($userpassword))) === 0 || strcmp($password, trim($userpassword)) === 0) {
541c5b85 99 require_sesskey();
e0e1a83e
JMV
100 $correctpass = true;
101 $USER->lessonloggedin[$lesson->id] = true;
e0e1a83e
JMV
102 }
103 }
104 }
105 if (!$correctpass) {
d42bc7dc 106 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)));
bfc638bb 107 echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
0a4abb73 108 echo $lessonoutput->footer();
5620acd8 109 exit();
0a4abb73
SH
110 }
111 } else if ($lesson->dependency) { // check for dependencies
112 if ($dependentlesson = $DB->get_record('lesson', array('id' => $lesson->dependency))) {
113 // lesson exists, so we can proceed
114 $conditions = unserialize($lesson->conditions);
115 // assume false for all
116 $errors = array();
fe75e76b 117
0a4abb73
SH
118 // check for the timespent condition
119 if ($conditions->timespent) {
120 $timespent = false;
121 if ($attempttimes = $DB->get_records('lesson_timer', array("userid"=>$USER->id, "lessonid"=>$dependentlesson->id))) {
122 // go through all the times and test to see if any of them satisfy the condition
123 foreach($attempttimes as $attempttime) {
124 $duration = $attempttime->lessontime - $attempttime->starttime;
125 if ($conditions->timespent < $duration/60) {
126 $timespent = true;
127 }
d1b025d0 128 }
5620acd8 129 }
0a4abb73
SH
130 if (!$timespent) {
131 $errors[] = get_string('timespenterror', 'lesson', $conditions->timespent);
5620acd8 132 }
5620acd8 133 }
fe75e76b 134
0a4abb73
SH
135 // check for the gradebetterthan condition
136 if($conditions->gradebetterthan) {
fb992d71 137 $gradebetterthan = false;
0a4abb73
SH
138 if ($studentgrades = $DB->get_records('lesson_grades', array("userid"=>$USER->id, "lessonid"=>$dependentlesson->id))) {
139 // go through all the grades and test to see if any of them satisfy the condition
140 foreach($studentgrades as $studentgrade) {
141 if ($studentgrade->grade >= $conditions->gradebetterthan) {
142 $gradebetterthan = true;
ee3f2275 143 }
ee3f2275 144 }
fb992d71 145 }
146 if (!$gradebetterthan) {
147 $errors[] = get_string('gradebetterthanerror', 'lesson', $conditions->gradebetterthan);
148 }
0a4abb73
SH
149 }
150
151 // check for the completed condition
152 if ($conditions->completed) {
153 if (!$DB->count_records('lesson_grades', array('userid'=>$USER->id, 'lessonid'=>$dependentlesson->id))) {
154 $errors[] = get_string('completederror', 'lesson');
fe75e76b 155 }
ac8e16be 156 }
fe75e76b 157
0a4abb73 158 if (!empty($errors)) { // print out the errors if any
d42bc7dc 159 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('completethefollowingconditions', 'lesson', format_string($lesson->name)));
0a4abb73
SH
160 echo $lessonoutput->dependancy_errors($dependentlesson, $errors);
161 echo $lessonoutput->footer();
162 exit();
163 }
5631925a 164 }
165 }
0a4abb73 166}
5631925a 167
168 // this is called if a student leaves during a lesson
0a4abb73
SH
169if ($pageid == LESSON_UNSEENBRANCHPAGE) {
170 $pageid = lesson_unseen_question_jump($lesson, $USER->id, $pageid);
171}
172
173// display individual pages and their sets of answers
174// if pageid is EOL then the end of the lesson has been reached
175// for flow, changed to simple echo for flow styles, michaelp, moved lesson name and page title down
176$attemptflag = false;
177if (empty($pageid)) {
178 // make sure there are pages to view
179 if (!$DB->get_field('lesson_pages', 'id', array('lessonid' => $lesson->id, 'prevpageid' => 0))) {
180 if (!$canmanage) {
181 $lesson->add_message(get_string('lessonnotready2', 'lesson')); // a nice message to the student
182 } else {
183 if (!$DB->count_records('lesson_pages', array('lessonid'=>$lesson->id))) {
184 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); // no pages - redirect to add pages
5631925a 185 } else {
0a4abb73 186 $lesson->add_message(get_string('lessonpagelinkingbroken', 'lesson')); // ok, bad mojo
5631925a 187 }
fb992d71 188 }
0a4abb73 189 }
fe75e76b 190
0a4abb73
SH
191 // if no pageid given see if the lesson has been started
192 $retries = $DB->count_records('lesson_grades', array("lessonid" => $lesson->id, "userid" => $USER->id));
193 if ($retries > 0) {
194 $attemptflag = true;
195 }
fe75e76b 196
0a4abb73
SH
197 if (isset($USER->modattempts[$lesson->id])) {
198 unset($USER->modattempts[$lesson->id]); // if no pageid, then student is NOT reviewing
199 }
86b7a428 200
bc8a409c 201 // If there are any questions that have been answered correctly (or not) in this attempt.
202 $allattempts = $lesson->get_attempts($retries);
203 if (!empty($allattempts)) {
204 $attempt = end($allattempts);
205 $attemptpage = $lesson->load_page($attempt->pageid);
49ef5d4c
RW
206 $jumpto = $DB->get_field('lesson_answers', 'jumpto', array('id' => $attempt->answerid));
207 // convert the jumpto to a proper page id
bc8a409c 208 if ($jumpto == 0) {
209 // Check if a question has been incorrectly answered AND no more attempts at it are left.
210 $nattempts = $lesson->get_attempts($attempt->retry, false, $attempt->pageid, $USER->id);
211 if (count($nattempts) >= $lesson->maxattempts) {
212 $lastpageseen = $lesson->get_next_page($attemptpage->nextpageid);
213 } else {
214 $lastpageseen = $attempt->pageid;
b882b8c6 215 }
49ef5d4c 216 } elseif ($jumpto == LESSON_NEXTPAGE) {
bc8a409c 217 $lastpageseen = $lesson->get_next_page($attemptpage->nextpageid);
83f3ac52
SB
218 } else if ($jumpto == LESSON_CLUSTERJUMP) {
219 $lastpageseen = $lesson->cluster_jump($attempt->pageid);
49ef5d4c
RW
220 } else {
221 $lastpageseen = $jumpto;
fb992d71 222 }
0a4abb73 223 }
fe75e76b 224
bc8a409c 225 if ($branchtables = $DB->get_records('lesson_branch', array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $retries), 'timeseen DESC')) {
0a4abb73
SH
226 // in here, user has viewed a branch table
227 $lastbranchtable = current($branchtables);
bc8a409c 228 if (count($allattempts) > 0) {
6695d594
SB
229 if ($lastbranchtable->timeseen > $attempt->timeseen) {
230 // This branch table was viewed more recently than the question page.
231 if (!empty($lastbranchtable->nextpageid)) {
232 $lastpageseen = $lastbranchtable->nextpageid;
233 } else {
234 // Next page ID did not exist prior to MDL-34006.
0a4abb73
SH
235 $lastpageseen = $lastbranchtable->pageid;
236 }
fb992d71 237 }
0a4abb73 238 } else {
6695d594
SB
239 // Has not answered any questions but has viewed a branch table.
240 if (!empty($lastbranchtable->nextpageid)) {
241 $lastpageseen = $lastbranchtable->nextpageid;
242 } else {
243 // Next page ID did not exist prior to MDL-34006.
244 $lastpageseen = $lastbranchtable->pageid;
245 }
fb992d71 246 }
fb992d71 247 }
6695d594
SB
248 // Check to see if end of lesson was reached.
249 if ((isset($lastpageseen) && ($lastpageseen != LESSON_EOL))) {
250 if (($DB->count_records('lesson_attempts', array('lessonid' => $lesson->id, 'userid' => $USER->id, 'retry' => $retries)) > 0)
251 || $DB->count_records('lesson_branch', array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $retries)) > 0) {
252
253 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('leftduringtimedsession', 'lesson'));
0967a20d 254 if ($lesson->timelimit) {
6695d594
SB
255 if ($lesson->retake) {
256 $continuelink = new single_button(new moodle_url('/mod/lesson/view.php',
257 array('id' => $cm->id, 'pageid' => $lesson->firstpageid, 'startlastseen' => 'no')),
258 get_string('continue', 'lesson'), 'get');
259
260 echo html_writer::div($lessonoutput->message(get_string('leftduringtimed', 'lesson'), $continuelink),
261 'center leftduring');
262
263 } else {
264 $courselink = new single_button(new moodle_url('/course/view.php',
265 array('id' => $PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
266
267 echo html_writer::div($lessonoutput->message(get_string('leftduringtimednoretake', 'lesson'), $courselink),
268 'center leftduring');
269 }
6707a280 270 } else {
6695d594 271 echo $lessonoutput->continue_links($lesson, $lastpageseen);
6707a280 272 }
6695d594
SB
273 echo $lessonoutput->footer();
274 exit();
fb992d71 275 }
0a4abb73 276 }
fe75e76b 277
0a4abb73
SH
278 if ($attemptflag) {
279 if (!$lesson->retake) {
d42bc7dc 280 echo $lessonoutput->header($lesson, $cm, 'view', '', null, get_string("noretake", "lesson"));
a6855934 281 $courselink = new single_button(new moodle_url('/course/view.php', array('id'=>$PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
0a4abb73
SH
282 echo $lessonoutput->message(get_string("noretake", "lesson"), $courselink);
283 echo $lessonoutput->footer();
284 exit();
fb992d71 285 }
0a4abb73
SH
286 }
287 // start at the first page
288 if (!$pageid = $DB->get_field('lesson_pages', 'id', array('lessonid' => $lesson->id, 'prevpageid' => 0))) {
89644a40
AG
289 echo $lessonoutput->header($lesson, $cm, 'view', '', null);
290 // Lesson currently has no content. A message for display has been prepared and will be displayed by the header method
291 // of the lesson renderer.
292 echo $lessonoutput->footer();
293 exit();
0a4abb73
SH
294 }
295 /// This is the code for starting a timed test
296 if(!isset($USER->startlesson[$lesson->id]) && !$canmanage) {
297 $lesson->start_timer();
298 }
299}
300
301$currenttab = 'view';
302$extraeditbuttons = false;
303$lessonpageid = null;
304$timer = null;
305
306if ($pageid != LESSON_EOL) {
307 /// This is the code updates the lessontime for a timed test
308 $startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
fe75e76b 309
2f67a9b3 310 $page = $lesson->load_page($pageid);
0a4abb73
SH
311 // Check if the page is of a special type and if so take any nessecary action
312 $newpageid = $page->callback_on_view($canmanage);
313 if (is_numeric($newpageid)) {
314 $page = $lesson->load_page($newpageid);
315 }
5aeebbc8 316
7edf27c4
MN
317 // Trigger module viewed event.
318 $event = \mod_lesson\event\course_module_viewed::create(array(
319 'objectid' => $lesson->id,
fbc4b778 320 'context' => $context
7edf27c4 321 ));
fbc4b778
MG
322 $event->add_record_snapshot('course_modules', $cm);
323 $event->add_record_snapshot('course', $course);
7edf27c4 324 $event->trigger();
fe75e76b 325
0a4abb73
SH
326 // This is where several messages (usually warnings) are displayed
327 // all of this is displayed above the actual page
4c99e342 328
0a4abb73
SH
329 // check to see if the user can see the left menu
330 if (!$canmanage) {
331 $lesson->displayleft = lesson_displayleftif($lesson);
c022b496 332
0a4abb73
SH
333 $continue = ($startlastseen !== '');
334 $restart = ($continue && $startlastseen == 'yes');
335 $timer = $lesson->update_timer($continue, $restart);
6707a280 336
a1acc001
JMV
337 if ($lesson->timelimit) {
338 $timeleft = $timer->starttime + $lesson->timelimit - time();
0a4abb73
SH
339 if ($timeleft <= 0) {
340 // Out of time
341 $lesson->add_message(get_string('eolstudentoutoftime', 'lesson'));
a6855934 342 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>LESSON_EOL, 'outoftime'=>'normal')));
0a4abb73
SH
343 die; // Shouldn't be reached, but make sure
344 } else if ($timeleft < 60) {
345 // One minute warning
346 $lesson->add_message(get_string('studentoneminwarning', 'lesson'));
fb992d71 347 }
348 }
fe75e76b 349
0a4abb73
SH
350 if ($page->qtype == LESSON_PAGE_BRANCHTABLE && $lesson->minquestions) {
351 // tell student how many questions they have seen, how many are required and their grade
352 $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
353 $gradeinfo = lesson_grade($lesson, $ntries);
354 if ($gradeinfo->attempts) {
355 if ($gradeinfo->nquestions < $lesson->minquestions) {
5631925a 356 $a = new stdClass;
0a4abb73
SH
357 $a->nquestions = $gradeinfo->nquestions;
358 $a->minquestions = $lesson->minquestions;
359 $lesson->add_message(get_string('numberofpagesviewednotice', 'lesson', $a));
9e93d4d0 360 }
abd5c24e 361
abd5c24e
RW
362 if (!$reviewmode && !$lesson->retake){
363 $lesson->add_message(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'notify');
49aaac3e
JMV
364 if ($lesson->grade != GRADE_TYPE_NONE) {
365 $a = new stdClass;
366 $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
367 $a->total = $lesson->grade;
368 $lesson->add_message(get_string('yourcurrentgradeisoutof', 'lesson', $a), 'notify');
369 }
abd5c24e 370 }
9e93d4d0 371 }
fb992d71 372 }
0a4abb73
SH
373 } else {
374 $timer = null;
a1acc001 375 if ($lesson->timelimit) {
0a4abb73 376 $lesson->add_message(get_string('teachertimerwarning', 'lesson'));
4262a2f8 377 }
0a4abb73
SH
378 if (lesson_display_teacher_warning($lesson)) {
379 // This is the warning msg for teachers to inform them that cluster
380 // and unseen does not work while logged in as a teacher
46ac70b2 381 $warningvars = new stdClass();
0a4abb73
SH
382 $warningvars->cluster = get_string('clusterjump', 'lesson');
383 $warningvars->unseen = get_string('unseenpageinbranch', 'lesson');
384 $lesson->add_message(get_string('teacherjumpwarning', 'lesson', $warningvars));
4262a2f8 385 }
0a4abb73 386 }
5631925a 387
0a4abb73
SH
388 $PAGE->set_subpage($page->id);
389 $currenttab = 'view';
390 $extraeditbuttons = true;
391 $lessonpageid = $page->id;
27affa26 392 $extrapagetitle = $page->title;
fe75e76b 393
0a4abb73
SH
394 if (($edit != -1) && $PAGE->user_allowed_editing()) {
395 $USER->editing = $edit;
396 }
fe75e76b 397
0a4abb73 398 if (is_array($page->answers) && count($page->answers)>0) {
fb992d71 399 // this is for modattempts option. Find the users previous answer to this page,
400 // and then display it below in answer processing
fe75e76b 401 if (isset($USER->modattempts[$lesson->id])) {
318e3745 402 $retries = $DB->count_records('lesson_grades', array("lessonid"=>$lesson->id, "userid"=>$USER->id));
0a4abb73 403 if (!$attempts = $lesson->get_attempts($retries-1, false, $page->id)) {
1858ba0c 404 print_error('cannotfindpreattempt', 'lesson');
5620acd8 405 }
fb992d71 406 $attempt = end($attempts);
abd5c24e 407 $USER->modattempts[$lesson->id] = $attempt;
0a4abb73
SH
408 } else {
409 $attempt = false;
fb992d71 410 }
0a4abb73
SH
411 $lessoncontent = $lessonoutput->display_page($lesson, $page, $attempt);
412 } else {
0a4abb73
SH
413 $data = new stdClass;
414 $data->id = $PAGE->cm->id;
1c510e2e 415 $data->pageid = $page->id;
0343553f 416 $data->newpageid = $lesson->get_next_page($page->nextpageid);
fe75e76b 417
1c510e2e
SH
418 $customdata = array(
419 'title' => $page->title,
420 'contents' => $page->get_contents()
421 );
422 $mform = new lesson_page_without_answers($CFG->wwwroot.'/mod/lesson/continue.php', $customdata);
0a4abb73
SH
423 $mform->set_data($data);
424 ob_start();
425 $mform->display();
426 $lessoncontent = ob_get_contents();
427 ob_end_clean();
428 }
fe75e76b 429
d9c26e21 430 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
27affa26 431 echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, $extrapagetitle);
0a4abb73 432 if ($attemptflag) {
e481859f
RW
433 // We are using level 3 header because attempt heading is a sub-heading of lesson title (MDL-30911).
434 echo $OUTPUT->heading(get_string('attempt', 'lesson', $retries), 3);
0a4abb73
SH
435 }
436 /// This calculates and prints the ongoing score
abd5c24e 437 if ($lesson->ongoing && !empty($pageid) && !$reviewmode) {
0a4abb73
SH
438 echo $lessonoutput->ongoing_score($lesson);
439 }
440 if ($lesson->displayleft) {
441 echo '<a name="maincontent" id="maincontent" title="' . get_string('anchortitle', 'lesson') . '"></a>';
442 }
443 echo $lessoncontent;
0a4abb73
SH
444 echo $lessonoutput->progress_bar($lesson);
445 echo $lessonoutput->footer();
446
447} else {
448
449 $lessoncontent = '';
450 // end of lesson reached work out grade
451 // Used to check to see if the student ran out of time
452 $outoftime = optional_param('outoftime', '', PARAM_ALPHA);
453
0a4abb73
SH
454 $ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
455 if (isset($USER->modattempts[$lesson->id])) {
456 $ntries--; // need to look at the old attempts :)
457 }
139645d4
MN
458 $gradelesson = true;
459 $gradeinfo = lesson_grade($lesson, $ntries);
460 if ($lesson->custom && !$canmanage) {
461 // Before we calculate the custom score make sure they answered the minimum
462 // number of questions. We only need to do this for custom scoring as we can
463 // not get the miniumum score the user should achieve. If we are not using
464 // custom scoring (so all questions are valued as 1) then we simply check if
465 // they answered more than the minimum questions, if not, we mark it out of the
466 // number specified in the minimum questions setting - which is done in lesson_grade().
467 // Get the number of answers given.
468 if ($gradeinfo->nquestions < $lesson->minquestions) {
469 $gradelesson = false;
470 $a = new stdClass;
471 $a->nquestions = $gradeinfo->nquestions;
472 $a->minquestions = $lesson->minquestions;
473 $lessoncontent .= $OUTPUT->box_start('generalbox boxaligncenter');
474 $lesson->add_message(get_string('numberofpagesviewednotice', 'lesson', $a));
25345cb4 475 }
139645d4
MN
476 }
477 if ($gradelesson) {
478 // We are using level 3 header because the page title is a sub-heading of lesson title (MDL-30911).
479 $lessoncontent .= $OUTPUT->heading(get_string("congratulations", "lesson"), 3);
480 $lessoncontent .= $OUTPUT->box_start('generalbox boxaligncenter');
481 }
482 if (!$canmanage) {
483 if ($gradelesson) {
484 // Store this now before any modifications to pages viewed.
485 $progressbar = $lessonoutput->progress_bar($lesson);
486 // Update the clock / get time information for this user.
487 $lesson->stop_timer();
488
489 // Update completion state.
490 $completion = new completion_info($course);
491 if ($completion->is_enabled($cm) && $lesson->completionendreached) {
492 $completion->update_state($cm, COMPLETION_COMPLETE);
d0445cf7
JMV
493 }
494
139645d4
MN
495 if ($lesson->completiontimespent > 0) {
496 $duration = $DB->get_field_sql(
497 "SELECT SUM(lessontime - starttime)
498 FROM {lesson_timer}
499 WHERE lessonid = :lessonid
500 AND userid = :userid",
501 array('userid' => $USER->id, 'lessonid' => $lesson->id));
502 if (!$duration) {
503 $duration = 0;
504 }
505
506 // If student has not spend enough time in the lesson, display a message.
507 if ($duration < $lesson->completiontimespent) {
508 $a = new stdClass;
509 $a->timespent = format_time($duration);
510 $a->timerequired = format_time($lesson->completiontimespent);
511 $lessoncontent .= $lessonoutput->paragraph(get_string("notenoughtimespent", "lesson", $a), 'center');
512 }
d0445cf7 513 }
d0445cf7
JMV
514
515
139645d4
MN
516 if ($gradeinfo->attempts) {
517 if (!$lesson->custom) {
518 $lessoncontent .= $lessonoutput->paragraph(get_string("numberofpagesviewed", "lesson", $gradeinfo->nquestions), 'center');
519 if ($lesson->minquestions) {
520 if ($gradeinfo->nquestions < $lesson->minquestions) {
521 // print a warning and set nviewed to minquestions
522 $lessoncontent .= $lessonoutput->paragraph(get_string("youshouldview", "lesson", $lesson->minquestions), 'center');
523 }
fb992d71 524 }
139645d4 525 $lessoncontent .= $lessonoutput->paragraph(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'center');
0a4abb73 526 }
49aaac3e 527 $a = new stdClass;
139645d4
MN
528 $a->score = $gradeinfo->earned;
529 $a->grade = $gradeinfo->total;
530 if ($gradeinfo->nmanual) {
531 $a->tempmaxgrade = $gradeinfo->total - $gradeinfo->manualpoints;
532 $a->essayquestions = $gradeinfo->nmanual;
533 $lessoncontent .= $OUTPUT->box(get_string("displayscorewithessays", "lesson", $a), 'center');
534 } else {
535 $lessoncontent .= $OUTPUT->box(get_string("displayscorewithoutessays", "lesson", $a), 'center');
536 }
537 if ($lesson->grade != GRADE_TYPE_NONE) {
538 $a = new stdClass;
539 $a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
540 $a->total = $lesson->grade;
541 $lessoncontent .= $lessonoutput->paragraph(get_string("yourcurrentgradeisoutof", "lesson", $a), 'center');
542 }
0a4abb73 543
139645d4
MN
544 $grade = new stdClass();
545 $grade->lessonid = $lesson->id;
546 $grade->userid = $USER->id;
547 $grade->grade = $gradeinfo->grade;
548 $grade->completed = time();
549 if (isset($USER->modattempts[$lesson->id])) { // If reviewing, make sure update old grade record.
550 if (!$grades = $DB->get_records("lesson_grades",
c7d1a37b 551 array("lessonid" => $lesson->id, "userid" => $USER->id), "completed DESC", '*', 0, 1)) {
139645d4
MN
552 print_error('cannotfindgrade', 'lesson');
553 }
554 $oldgrade = array_shift($grades);
555 $grade->id = $oldgrade->id;
556 $DB->update_record("lesson_grades", $grade);
557 } else {
c7d1a37b 558 $newgradeid = $DB->insert_record("lesson_grades", $grade);
0a4abb73 559 }
fb992d71 560 } else {
139645d4
MN
561 if ($lesson->timelimit) {
562 if ($outoftime == 'normal') {
563 $grade = new stdClass();
564 $grade->lessonid = $lesson->id;
565 $grade->userid = $USER->id;
566 $grade->grade = 0;
567 $grade->completed = time();
568 $newgradeid = $DB->insert_record("lesson_grades", $grade);
569 $lessoncontent .= $lessonoutput->paragraph(get_string("eolstudentoutoftimenoanswers", "lesson"));
570 }
571 } else {
572 $lessoncontent .= $lessonoutput->paragraph(get_string("welldone", "lesson"));
573 }
ac8e16be 574 }
4262a2f8 575
139645d4
MN
576 // update central gradebook
577 lesson_update_grades($lesson, $USER->id);
578 $lessoncontent .= $progressbar;
579 }
0a4abb73
SH
580 } else {
581 // display for teacher
49aaac3e
JMV
582 if ($lesson->grade != GRADE_TYPE_NONE) {
583 $lessoncontent .= $lessonoutput->paragraph(get_string("displayofgrade", "lesson"), 'center');
584 }
0a4abb73
SH
585 }
586 $lessoncontent .= $OUTPUT->box_end(); //End of Lesson button to Continue.
fe75e76b 587
0a4abb73
SH
588 if ($lesson->modattempts && !$canmanage) {
589 // make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
590 // look at the attempt records to find the first QUESTION page that the user answered, then use that page id
591 // to pass to view again. This is slick cause it wont call the empty($pageid) code
592 // $ntries is decremented above
593 if (!$attempts = $lesson->get_attempts($ntries)) {
594 $attempts = array();
abd5c24e
RW
595 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id));
596 } else {
597 $firstattempt = current($attempts);
598 $pageid = $firstattempt->pageid;
599 // IF the student wishes to review, need to know the last question page that the student answered. This will help to make
600 // sure that the student can leave the lesson via pushing the continue button.
601 $lastattempt = end($attempts);
602 $USER->modattempts[$lesson->id] = $lastattempt->pageid;
603
604 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$PAGE->cm->id, 'pageid'=>$pageid));
0a4abb73 605 }
0f4c64b7 606 $lessoncontent .= html_writer::link($url, get_string('reviewlesson', 'lesson'), array('class' => 'centerpadded lessonbutton standardbutton'));
0a4abb73
SH
607 } elseif ($lesson->modattempts && $canmanage) {
608 $lessoncontent .= $lessonoutput->paragraph(get_string("modattemptsnoteacher", "lesson"), 'centerpadded');
609 }
114f47a7 610
0a4abb73 611 if ($lesson->activitylink) {
57cd3739 612 $lessoncontent .= $lesson->link_for_activitylink();
0a4abb73 613 }
fe75e76b 614
a6855934 615 $url = new moodle_url('/course/view.php', array('id'=>$course->id));
0f4c64b7 616 $lessoncontent .= html_writer::link($url, get_string('returnto', 'lesson', format_string($course->fullname, true)), array('class'=>'centerpadded lessonbutton standardbutton'));
bbcbc0fe 617
ccf2a054 618 if (has_capability('gradereport/user:view', context_course::instance($course->id))
59d7a9dc 619 && $course->showgrades && $lesson->grade != 0 && !$lesson->practice) {
9bb712a0
DP
620 $url = new moodle_url('/grade/index.php', array('id' => $course->id));
621 $lessoncontent .= html_writer::link($url, get_string('viewgrades', 'lesson'),
622 array('class' => 'centerpadded lessonbutton standardbutton'));
623 }
fb992d71 624
d9c26e21 625 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
d42bc7dc 626 echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, get_string("congratulations", "lesson"));
0a4abb73
SH
627 echo $lessoncontent;
628 echo $lessonoutput->footer();
629}