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