MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / mod / lesson / view.php
blobb05ca8875698ce71eed330afdc02e2d1486b7572
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();
57 $PAGE->add_body_class('limitedwidth');
59 $context = $lesson->context;
60 $canmanage = $lesson->can_manage();
62 $lessonoutput = $PAGE->get_renderer('mod_lesson');
64 $editbuttons = new \mod_lesson\output\edit_action_buttons($lesson);
66 $reviewmode = $lesson->is_in_review_mode();
68 if ($lesson->usepassword && !empty($userpassword)) {
69 require_sesskey();
72 // Check these for students only TODO: Find a better method for doing this!
73 if ($timerestriction = $lesson->get_time_restriction_status()) { // Deadline restrictions.
74 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('notavailable'));
75 echo $lessonoutput->render($editbuttons);
76 // No need to display warnings because activity dates are displayed at the top of the page.
77 echo $lessonoutput->lesson_inaccessible('');
78 echo $lessonoutput->footer();
79 exit();
80 } else if ($passwordrestriction = $lesson->get_password_restriction_status($userpassword)) { // Password protected lesson code.
81 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)));
82 echo $lessonoutput->render($editbuttons);
83 echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
84 echo $lessonoutput->footer();
85 exit();
86 } else if ($dependenciesrestriction = $lesson->get_dependencies_restriction_status()) { // Check for dependencies.
87 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('completethefollowingconditions', 'lesson', format_string($lesson->name)));
88 echo $lessonoutput->render($editbuttons);
89 echo $lessonoutput->dependancy_errors($dependenciesrestriction->dependentlesson, $dependenciesrestriction->errors);
90 echo $lessonoutput->footer();
91 exit();
94 // This is called if a student leaves during a lesson.
95 if ($pageid == LESSON_UNSEENBRANCHPAGE) {
96 $pageid = lesson_unseen_question_jump($lesson, $USER->id, $pageid);
99 // To avoid multiple calls, store the magic property firstpage.
100 $lessonfirstpage = $lesson->firstpage;
101 $lessonfirstpageid = $lessonfirstpage ? $lessonfirstpage->id : false;
103 // display individual pages and their sets of answers
104 // if pageid is EOL then the end of the lesson has been reached
105 // for flow, changed to simple echo for flow styles, michaelp, moved lesson name and page title down
106 $attemptflag = false;
107 if (empty($pageid)) {
108 // make sure there are pages to view
109 if (!$lessonfirstpageid) {
110 if (!$canmanage) {
111 $lesson->add_message(get_string('lessonnotready2', 'lesson')); // a nice message to the student
112 } else {
113 if (!$DB->count_records('lesson_pages', array('lessonid'=>$lesson->id))) {
114 redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); // no pages - redirect to add pages
115 } else {
116 $lesson->add_message(get_string('lessonpagelinkingbroken', 'lesson')); // ok, bad mojo
121 // if no pageid given see if the lesson has been started
122 $retries = $lesson->count_user_retries($USER->id);
123 if ($retries > 0) {
124 $attemptflag = true;
127 if (isset($USER->modattempts[$lesson->id])) {
128 unset($USER->modattempts[$lesson->id]); // if no pageid, then student is NOT reviewing
131 $lastpageseen = $lesson->get_last_page_seen($retries);
133 // Check if the lesson was attempted in an external device like the mobile app.
134 // This check makes sense only when the lesson allows offline attempts.
135 if ($lesson->allowofflineattempts && $timers = $lesson->get_user_timers($USER->id, 'starttime DESC', '*', 0, 1)) {
136 $timer = current($timers);
137 if (!empty($timer->timemodifiedoffline)) {
138 $lasttime = format_time(time() - $timer->timemodifiedoffline);
139 $lesson->add_message(get_string('offlinedatamessage', 'lesson', $lasttime), 'warning');
143 // Check to see if end of lesson was reached.
144 if (($lastpageseen !== false && ($lastpageseen != LESSON_EOL))) {
145 // End not reached. Check if the user left.
146 if ($lesson->left_during_timed_session($retries)) {
148 echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('leftduringtimedsession', 'lesson'));
149 echo $lessonoutput->render($editbuttons);
150 if ($lesson->timelimit) {
151 if ($lesson->retake) {
152 $continuelink = new single_button(new moodle_url('/mod/lesson/view.php',
153 array('id' => $cm->id, 'pageid' => $lesson->firstpageid, 'startlastseen' => 'no')),
154 get_string('continue', 'lesson'), 'get');
156 echo html_writer::div($lessonoutput->message(get_string('leftduringtimed', 'lesson'), $continuelink),
157 'center leftduring');
159 } else {
160 $courselink = new single_button(new moodle_url('/course/view.php',
161 array('id' => $PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
163 echo html_writer::div($lessonoutput->message(get_string('leftduringtimednoretake', 'lesson'), $courselink),
164 'center leftduring');
166 } else {
167 echo $lessonoutput->continue_links($lesson, $lastpageseen);
169 echo $lessonoutput->footer();
170 exit();
174 if ($attemptflag) {
175 if (!$lesson->retake) {
176 echo $lessonoutput->header($lesson, $cm, 'view', '', null, get_string("noretake", "lesson"));
177 echo $lessonoutput->render($editbuttons);
178 $courselink = new single_button(new moodle_url('/course/view.php', array('id'=>$PAGE->course->id)), get_string('returntocourse', 'lesson'), 'get');
179 echo $lessonoutput->message(get_string("noretake", "lesson"), $courselink);
180 echo $lessonoutput->footer();
181 exit();
184 // start at the first page
185 if (!$pageid = $lessonfirstpageid) {
186 echo $lessonoutput->header($lesson, $cm, 'view', '', null);
187 echo $lessonoutput->render($editbuttons);
188 // Lesson currently has no content. A message for display has been prepared and will be displayed by the header method
189 // of the lesson renderer.
190 echo $lessonoutput->footer();
191 exit();
193 /// This is the code for starting a timed test
194 if(!isset($USER->startlesson[$lesson->id]) && !$canmanage) {
195 $lesson->start_timer();
199 $currenttab = 'view';
200 $extraeditbuttons = false;
201 $lessonpageid = null;
202 $timer = null;
204 if ($pageid != LESSON_EOL) {
206 $lesson->set_module_viewed();
208 $timer = null;
209 // This is the code updates the lessontime for a timed test.
210 $startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
212 // Check to see if the user can see the left menu.
213 if (!$canmanage) {
214 $lesson->displayleft = lesson_displayleftif($lesson);
216 $continue = ($startlastseen !== '');
217 $restart = ($continue && $startlastseen == 'yes');
218 $timer = $lesson->update_timer($continue, $restart);
220 // Check time limit.
221 if (!$lesson->check_time($timer)) {
222 redirect(new moodle_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => LESSON_EOL, 'outoftime' => 'normal')));
223 die; // Shouldn't be reached, but make sure.
227 list($newpageid, $page, $lessoncontent) = $lesson->prepare_page_and_contents($pageid, $lessonoutput, $reviewmode);
229 if (($edit != -1) && $PAGE->user_allowed_editing()) {
230 $USER->editing = $edit;
233 $PAGE->set_subpage($page->id);
234 $currenttab = 'view';
235 $extraeditbuttons = true;
236 $lessonpageid = $page->id;
237 $extrapagetitle = $page->title;
239 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
240 echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, $extrapagetitle);
241 $editbuttons->set_currentpage($lessonpageid);
242 echo $lessonoutput->render($editbuttons);
244 if ($attemptflag) {
245 // We are using level 3 header because attempt heading is a sub-heading of lesson title (MDL-30911).
246 echo $OUTPUT->heading(get_string('attempt', 'lesson', $retries), 3);
248 // This calculates and prints the ongoing score.
249 if ($lesson->ongoing && !empty($pageid) && !$reviewmode) {
250 echo $lessonoutput->ongoing_score($lesson);
252 if ($lesson->displayleft) {
253 echo '<a name="maincontent" id="maincontent" title="' . get_string('anchortitle', 'lesson') . '"></a>';
255 echo $lessoncontent;
256 echo $lessonoutput->progress_bar($lesson);
257 echo $lessonoutput->footer();
259 } else {
261 // End of lesson reached work out grade.
262 // Used to check to see if the student ran out of time.
263 $outoftime = optional_param('outoftime', '', PARAM_ALPHA);
265 $data = $lesson->process_eol_page($outoftime);
266 $lessoncontent = $lessonoutput->display_eol_page($lesson, $data);
268 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
269 echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, get_string("congratulations", "lesson"));
270 $editbuttons->set_currentpage($lessonpageid);
271 echo $lessonoutput->render($editbuttons);
272 echo $lessoncontent;
273 echo $lessonoutput->footer();