Merge branch 'primary_secondary_nav' of https://github.com/Chocolate-lightning/moodle
[moodle.git] / mod / lesson / view.php
blob37372d956ee02192ca99db1464316dc3f7ad1b61
1 <?php
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.
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/>.
18 /**
19 * This page prints a particular instance of lesson
21 * @package mod_lesson
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
24 **/
26 define('NO_OUTPUT_BUFFERING', true);
28 require_once(__DIR__ . '/../../config.php');
29 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
30 require_once($CFG->libdir . '/grade/constants.php');
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);
35 $userpassword = optional_param('userpassword','',PARAM_RAW);
36 $backtocourse = optional_param('backtocourse', false, PARAM_RAW);
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), $cm, $course);
42 require_login($course, false, $cm);
44 if ($backtocourse) {
45 redirect(new moodle_url('/course/view.php', array('id'=>$course->id)));
48 // Apply overrides.
49 $lesson->update_effective_access($USER->id);
51 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$id));
52 if ($pageid !== null) {
53 $url->param('pageid', $pageid);
55 $PAGE->set_url($url);
56 $PAGE->force_settings_menu();
58 $context = $lesson->context;
59 $canmanage = $lesson->can_manage();
61 $lessonoutput = $PAGE->get_renderer('mod_lesson');
63 $reviewmode = $lesson->is_in_review_mode();
65 if ($lesson->usepassword && !empty($userpassword)) {
66 require_sesskey();
69 // Check these for students only TODO: Find a better method for doing this!
70 if ($timerestriction = $lesson->get_time_restriction_status()) { // Deadline restrictions.
71 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('notavailable'));
72 // No need to display warnings because activity dates are displayed at the top of the page.
73 echo $lessonoutput->lesson_inaccessible('');
74 echo $lessonoutput->footer();
75 exit();
76 } else if ($passwordrestriction = $lesson->get_password_restriction_status($userpassword)) { // Password protected lesson code.
77 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)));
78 echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
79 echo $lessonoutput->footer();
80 exit();
81 } else if ($dependenciesrestriction = $lesson->get_dependencies_restriction_status()) { // Check for dependencies.
82 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('completethefollowingconditions', 'lesson', format_string($lesson->name)));
83 echo $lessonoutput->dependancy_errors($dependenciesrestriction->dependentlesson, $dependenciesrestriction->errors);
84 echo $lessonoutput->footer();
85 exit();
88 // This is called if a student leaves during a lesson.
89 if ($pageid == LESSON_UNSEENBRANCHPAGE) {
90 $pageid = lesson_unseen_question_jump($lesson, $USER->id, $pageid);
93 // To avoid multiple calls, store the magic property firstpage.
94 $lessonfirstpage = $lesson->firstpage;
95 $lessonfirstpageid = $lessonfirstpage ? $lessonfirstpage->id : false;
97 // display individual pages and their sets of answers
98 // if pageid is EOL then the end of the lesson has been reached
99 // for flow, changed to simple echo for flow styles, michaelp, moved lesson name and page title down
100 $attemptflag = false;
101 if (empty($pageid)) {
102 // make sure there are pages to view
103 if (!$lessonfirstpageid) {
104 if (!$canmanage) {
105 $lesson->add_message(get_string('lessonnotready2', 'lesson')); // a nice message to the student
106 } else {
107 if (!$DB->count_records('lesson_pages', array('lessonid'=>$lesson->id))) {
108 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); // no pages - redirect to add pages
109 } else {
110 $lesson->add_message(get_string('lessonpagelinkingbroken', 'lesson')); // ok, bad mojo
115 // if no pageid given see if the lesson has been started
116 $retries = $lesson->count_user_retries($USER->id);
117 if ($retries > 0) {
118 $attemptflag = true;
121 if (isset($USER->modattempts[$lesson->id])) {
122 unset($USER->modattempts[$lesson->id]); // if no pageid, then student is NOT reviewing
125 $lastpageseen = $lesson->get_last_page_seen($retries);
127 // Check if the lesson was attempted in an external device like the mobile app.
128 // This check makes sense only when the lesson allows offline attempts.
129 if ($lesson->allowofflineattempts && $timers = $lesson->get_user_timers($USER->id, 'starttime DESC', '*', 0, 1)) {
130 $timer = current($timers);
131 if (!empty($timer->timemodifiedoffline)) {
132 $lasttime = format_time(time() - $timer->timemodifiedoffline);
133 $lesson->add_message(get_string('offlinedatamessage', 'lesson', $lasttime), 'warning');
137 // Check to see if end of lesson was reached.
138 if (($lastpageseen !== false && ($lastpageseen != LESSON_EOL))) {
139 // End not reached. Check if the user left.
140 if ($lesson->left_during_timed_session($retries)) {
142 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('leftduringtimedsession', 'lesson'));
143 if ($lesson->timelimit) {
144 if ($lesson->retake) {
145 $continuelink = new single_button(new moodle_url('/mod/lesson/view.php',
146 array('id' => $cm->id, 'pageid' => $lesson->firstpageid, 'startlastseen' => 'no')),
147 get_string('continue', 'lesson'), 'get');
149 echo html_writer::div($lessonoutput->message(get_string('leftduringtimed', 'lesson'), $continuelink),
150 'center leftduring');
152 } else {
153 $courselink = new single_button(new moodle_url('/course/view.php',
154 array('id' => $PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
156 echo html_writer::div($lessonoutput->message(get_string('leftduringtimednoretake', 'lesson'), $courselink),
157 'center leftduring');
159 } else {
160 echo $lessonoutput->continue_links($lesson, $lastpageseen);
162 echo $lessonoutput->footer();
163 exit();
167 if ($attemptflag) {
168 if (!$lesson->retake) {
169 echo $lessonoutput->header($lesson, $cm, 'view', '', null, get_string("noretake", "lesson"));
170 $courselink = new single_button(new moodle_url('/course/view.php', array('id'=>$PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
171 echo $lessonoutput->message(get_string("noretake", "lesson"), $courselink);
172 echo $lessonoutput->footer();
173 exit();
176 // start at the first page
177 if (!$pageid = $lessonfirstpageid) {
178 echo $lessonoutput->header($lesson, $cm, 'view', '', null);
179 // Lesson currently has no content. A message for display has been prepared and will be displayed by the header method
180 // of the lesson renderer.
181 echo $lessonoutput->footer();
182 exit();
184 /// This is the code for starting a timed test
185 if(!isset($USER->startlesson[$lesson->id]) && !$canmanage) {
186 $lesson->start_timer();
190 $currenttab = 'view';
191 $extraeditbuttons = false;
192 $lessonpageid = null;
193 $timer = null;
195 if ($pageid != LESSON_EOL) {
197 $lesson->set_module_viewed();
199 $timer = null;
200 // This is the code updates the lessontime for a timed test.
201 $startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
203 // Check to see if the user can see the left menu.
204 if (!$canmanage) {
205 $lesson->displayleft = lesson_displayleftif($lesson);
207 $continue = ($startlastseen !== '');
208 $restart = ($continue && $startlastseen == 'yes');
209 $timer = $lesson->update_timer($continue, $restart);
211 // Check time limit.
212 if (!$lesson->check_time($timer)) {
213 redirect(new moodle_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => LESSON_EOL, 'outoftime' => 'normal')));
214 die; // Shouldn't be reached, but make sure.
218 list($newpageid, $page, $lessoncontent) = $lesson->prepare_page_and_contents($pageid, $lessonoutput, $reviewmode);
220 if (($edit != -1) && $PAGE->user_allowed_editing()) {
221 $USER->editing = $edit;
224 $PAGE->set_subpage($page->id);
225 $currenttab = 'view';
226 $extraeditbuttons = true;
227 $lessonpageid = $page->id;
228 $extrapagetitle = $page->title;
230 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
231 echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, $extrapagetitle);
232 if ($attemptflag) {
233 // We are using level 3 header because attempt heading is a sub-heading of lesson title (MDL-30911).
234 echo $OUTPUT->heading(get_string('attempt', 'lesson', $retries), 3);
236 // This calculates and prints the ongoing score.
237 if ($lesson->ongoing && !empty($pageid) && !$reviewmode) {
238 echo $lessonoutput->ongoing_score($lesson);
240 if ($lesson->displayleft) {
241 echo '<a name="maincontent" id="maincontent" title="' . get_string('anchortitle', 'lesson') . '"></a>';
243 echo $lessoncontent;
244 echo $lessonoutput->progress_bar($lesson);
245 echo $lessonoutput->footer();
247 } else {
249 // End of lesson reached work out grade.
250 // Used to check to see if the student ran out of time.
251 $outoftime = optional_param('outoftime', '', PARAM_ALPHA);
253 $data = $lesson->process_eol_page($outoftime);
254 $lessoncontent = $lessonoutput->display_eol_page($lesson, $data);
256 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
257 echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, get_string("congratulations", "lesson"));
258 echo $lessoncontent;
259 echo $lessonoutput->footer();