MDL-58030 gradingform_guide: Init $commentchooser before use
[moodle.git] / mod / lesson / continue.php
blobba5e7690ace942cb4c69051cd2a184262f008530
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 * Action for processing page answers by users
21 * @package mod_lesson
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 **/
26 /** Require the specific libraries */
27 require_once("../../config.php");
28 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
30 $id = required_param('id', PARAM_INT);
32 $cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
33 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
34 $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
36 require_login($course, false, $cm);
37 require_sesskey();
39 // Apply overrides.
40 $lesson->update_effective_access($USER->id);
42 $context = context_module::instance($cm->id);
43 $canmanage = has_capability('mod/lesson:manage', $context);
44 $lessonoutput = $PAGE->get_renderer('mod_lesson');
46 $url = new moodle_url('/mod/lesson/continue.php', array('id'=>$cm->id));
47 $PAGE->set_url($url);
48 $PAGE->set_pagetype('mod-lesson-view');
49 $PAGE->navbar->add(get_string('continue', 'lesson'));
51 // This is the code updates the lesson time for a timed test
52 // get time information for this user
53 if (!$canmanage) {
54 $lesson->displayleft = lesson_displayleftif($lesson);
55 $timer = $lesson->update_timer();
56 if ($lesson->timelimit) {
57 $timeleft = ($timer->starttime + $lesson->timelimit) - time();
58 if ($timeleft <= 0) {
59 // Out of time
60 $lesson->add_message(get_string('eolstudentoutoftime', 'lesson'));
61 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>LESSON_EOL, 'outoftime'=>'normal')));
62 } else if ($timeleft < 60) {
63 // One minute warning
64 $lesson->add_message(get_string("studentoneminwarning", "lesson"));
67 } else {
68 $timer = new stdClass;
71 // record answer (if necessary) and show response (if none say if answer is correct or not)
72 $page = $lesson->load_page(required_param('pageid', PARAM_INT));
74 $userhasgrade = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
75 $reviewmode = false;
76 if ($userhasgrade && !$lesson->retake) {
77 $reviewmode = true;
80 // Check the page has answers [MDL-25632]
81 if (count($page->answers) > 0) {
82 $result = $page->record_attempt($context);
83 } else {
84 // The page has no answers so we will just progress to the next page in the
85 // sequence (as set by newpageid).
86 $result = new stdClass;
87 $result->newpageid = optional_param('newpageid', $page->nextpageid, PARAM_INT);
88 $result->nodefaultresponse = true;
91 if (isset($USER->modattempts[$lesson->id])) {
92 // make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
93 if ($USER->modattempts[$lesson->id]->pageid == $page->id && $page->nextpageid == 0) { // remember, this session variable holds the pageid of the last page that the user saw
94 $result->newpageid = LESSON_EOL;
95 } else {
96 $nretakes = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
97 $nretakes--; // make sure we are looking at the right try.
98 $attempts = $DB->get_records("lesson_attempts", array("lessonid"=>$lesson->id, "userid"=>$USER->id, "retry"=>$nretakes), "timeseen", "id, pageid");
99 $found = false;
100 $temppageid = 0;
101 // Make sure that the newpageid always defaults to something valid.
102 $result->newpageid = LESSON_EOL;
103 foreach($attempts as $attempt) {
104 if ($found && $temppageid != $attempt->pageid) { // now try to find the next page, make sure next few attempts do no belong to current page
105 $result->newpageid = $attempt->pageid;
106 break;
108 if ($attempt->pageid == $page->id) {
109 $found = true; // if found current page
110 $temppageid = $attempt->pageid;
114 } elseif ($result->newpageid != LESSON_CLUSTERJUMP && $page->id != 0 && $result->newpageid > 0) {
115 // going to check to see if the page that the user is going to view next, is a cluster page.
116 // If so, dont display, go into the cluster. The $result->newpageid > 0 is used to filter out all of the negative code jumps.
117 $newpage = $lesson->load_page($result->newpageid);
118 if ($newpageid = $newpage->override_next_page($result->newpageid)) {
119 $result->newpageid = $newpageid;
121 } elseif ($result->newpageid == LESSON_UNSEENBRANCHPAGE) {
122 if ($canmanage) {
123 if ($page->nextpageid == 0) {
124 $result->newpageid = LESSON_EOL;
125 } else {
126 $result->newpageid = $page->nextpageid;
128 } else {
129 $result->newpageid = lesson_unseen_question_jump($lesson, $USER->id, $page->id);
131 } elseif ($result->newpageid == LESSON_PREVIOUSPAGE) {
132 $result->newpageid = $page->prevpageid;
133 } elseif ($result->newpageid == LESSON_RANDOMPAGE) {
134 $result->newpageid = lesson_random_question_jump($lesson, $page->id);
135 } elseif ($result->newpageid == LESSON_CLUSTERJUMP) {
136 if ($canmanage) {
137 if ($page->nextpageid == 0) { // if teacher, go to next page
138 $result->newpageid = LESSON_EOL;
139 } else {
140 $result->newpageid = $page->nextpageid;
142 } else {
143 $result->newpageid = $lesson->cluster_jump($page->id);
147 if ($result->nodefaultresponse) {
148 // Don't display feedback
149 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>$result->newpageid)));
152 /// Set Messages
154 if ($canmanage) {
155 // This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
156 if(lesson_display_teacher_warning($lesson)) {
157 $warningvars = new stdClass();
158 $warningvars->cluster = get_string("clusterjump", "lesson");
159 $warningvars->unseen = get_string("unseenpageinbranch", "lesson");
160 $lesson->add_message(get_string("teacherjumpwarning", "lesson", $warningvars));
162 // Inform teacher that s/he will not see the timer
163 if ($lesson->timelimit) {
164 $lesson->add_message(get_string("teachertimerwarning", "lesson"));
167 // Report attempts remaining
168 if ($result->attemptsremaining != 0 && $lesson->review && !$reviewmode) {
169 $lesson->add_message(get_string('attemptsremaining', 'lesson', $result->attemptsremaining));
172 $PAGE->set_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => $page->id));
173 $PAGE->set_subpage($page->id);
175 /// Print the header, heading and tabs
176 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
177 echo $lessonoutput->header($lesson, $cm, 'view', true, $page->id, get_string('continue', 'lesson'));
179 if ($lesson->displayleft) {
180 echo '<a name="maincontent" id="maincontent" title="'.get_string('anchortitle', 'lesson').'"></a>';
182 // This calculates and prints the ongoing score message
183 if ($lesson->ongoing && !$reviewmode) {
184 echo $lessonoutput->ongoing_score($lesson);
186 if (!$reviewmode) {
187 echo $result->feedback;
190 // User is modifying attempts - save button and some instructions
191 if (isset($USER->modattempts[$lesson->id])) {
192 $url = $CFG->wwwroot.'/mod/lesson/view.php';
193 $content = $OUTPUT->box(get_string("gotoendoflesson", "lesson"), 'center');
194 $content .= $OUTPUT->box(get_string("or", "lesson"), 'center');
195 $content .= $OUTPUT->box(get_string("continuetonextpage", "lesson"), 'center');
196 $content .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$cm->id));
197 $content .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>LESSON_EOL));
198 $content .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('finish', 'lesson')));
199 echo html_writer::tag('form', "<div>$content</div>", array('method'=>'post', 'action'=>$url));
202 // Review button back
203 if (!$result->correctanswer && !$result->noanswer && !$result->isessayquestion && !$reviewmode && $lesson->review && !$result->maxattemptsreached) {
204 $url = $CFG->wwwroot.'/mod/lesson/view.php';
205 $content = html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$cm->id));
206 $content .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$page->id));
207 $content .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('reviewquestionback', 'lesson')));
208 echo html_writer::tag('form', "<div class=\"singlebutton\">$content</div>", array('method'=>'post', 'action'=>$url));
211 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id, 'pageid'=>$result->newpageid));
212 if ($lesson->review && !$result->correctanswer && !$result->noanswer && !$result->isessayquestion && !$result->maxattemptsreached) {
213 // Review button continue
214 echo $OUTPUT->single_button($url, get_string('reviewquestioncontinue', 'lesson'));
215 } else {
216 // Normal continue button
217 echo $OUTPUT->single_button($url, get_string('continue', 'lesson'));
220 echo $lessonoutput->footer();