blocklib MDL-25654 fix API inconsistency be renaming add_pretend_block to add_fake_block.
[moodle.git] / mod / lesson / continue.php
CommitLineData
0a4abb73
SH
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 * Action for processing page answers by users
20 *
cc3dbaaa
PS
21 * @package mod
22 * @subpackage lesson
23 * @copyright 2009 Sam Hemelryk
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0a4abb73
SH
25 **/
26
27/** Require the specific libraries */
28require_once("../../config.php");
29require_once($CFG->dirroot.'/mod/lesson/locallib.php');
30
b3daa926
PS
31$id = required_param('id', PARAM_INT);
32
33$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);;
34$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
35$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
0a4abb73
SH
36
37require_login($course, false, $cm);
38require_sesskey();
39
40$context = get_context_instance(CONTEXT_MODULE, $cm->id);
41$canmanage = has_capability('mod/lesson:manage', $context);
649cf95d 42$lessonoutput = $PAGE->get_renderer('mod_lesson');
0a4abb73 43
a6855934 44$url = new moodle_url('/mod/lesson/continue.php', array('id'=>$cm->id));
0a4abb73
SH
45$PAGE->set_url($url);
46$PAGE->navbar->add(get_string('continue', 'lesson'));
47
48// This is the code updates the lesson time for a timed test
49// get time information for this user
50if (!$canmanage) {
51 $lesson->displayleft = lesson_displayleftif($lesson);
52 $timer = $lesson->update_timer();
53 if ($lesson->timed) {
54 $timeleft = ($timer->starttime + $lesson->maxtime * 60) - time();
55 if ($timeleft <= 0) {
56 // Out of time
57 $lesson->add_message(get_string('eolstudentoutoftime', 'lesson'));
a6855934 58 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>LESSON_EOL, 'outoftime'=>'normal')));
0a4abb73
SH
59 } else if ($timeleft < 60) {
60 // One minute warning
61 $lesson->add_message(get_string("studentoneminwarning", "lesson"));
62 }
63 }
64} else {
65 $timer = new stdClass;
66}
67
68// record answer (if necessary) and show response (if none say if answer is correct or not)
69$page = $lesson->load_page(required_param('pageid', PARAM_INT));
70$result = $page->record_attempt($context);
71
72if (isset($USER->modattempts[$lesson->id])) {
73 // make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
465a9b4f 74 if ($USER->modattempts[$lesson->id] == $page->id && $page->nextpageid == 0) { // remember, this session variable holds the pageid of the last page that the user saw
0a4abb73
SH
75 $result->newpageid = LESSON_EOL;
76 } else {
8f639ad1 77 $nretakes = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
0a4abb73
SH
78 $nretakes--; // make sure we are looking at the right try.
79 $attempts = $DB->get_records("lesson_attempts", array("lessonid"=>$lesson->id, "userid"=>$USER->id, "retry"=>$nretakes), "timeseen", "id, pageid");
80 $found = false;
81 $temppageid = 0;
82 foreach($attempts as $attempt) {
83 if ($found && $temppageid != $attempt->pageid) { // now try to find the next page, make sure next few attempts do no belong to current page
84 $result->newpageid = $attempt->pageid;
85 break;
86 }
87 if ($attempt->pageid == $page->id) {
88 $found = true; // if found current page
89 $temppageid = $attempt->pageid;
90 }
91 }
92 }
93} elseif ($result->newpageid != LESSON_CLUSTERJUMP && $page->id != 0 && $result->newpageid > 0) {
94 // going to check to see if the page that the user is going to view next, is a cluster page.
95 // If so, dont display, go into the cluster. The $result->newpageid > 0 is used to filter out all of the negative code jumps.
96 $newpage = $lesson->load_page($result->newpageid);
97 if ($newpageid = $newpage->override_next_page($result->newpageid)) {
98 $result->newpageid = $newpageid;
99 }
100} elseif ($result->newpageid == LESSON_UNSEENBRANCHPAGE) {
101 if ($canmanage) {
102 if ($page->nextpageid == 0) {
103 $result->newpageid = LESSON_EOL;
104 } else {
105 $result->newpageid = $page->nextpageid;
106 }
107 } else {
108 $result->newpageid = lesson_unseen_question_jump($lesson, $USER->id, $page->id);
109 }
110} elseif ($result->newpageid == LESSON_PREVIOUSPAGE) {
111 $result->newpageid = $page->prevpageid;
112} elseif ($result->newpageid == LESSON_RANDOMPAGE) {
113 $result->newpageid = lesson_random_question_jump($lesson, $page->id);
114} elseif ($result->newpageid == LESSON_CLUSTERJUMP) {
115 if ($canmanage) {
116 if ($page->nextpageid == 0) { // if teacher, go to next page
117 $result->newpageid = LESSON_EOL;
118 } else {
119 $result->newpageid = $page->nextpageid;
120 }
121 } else {
122 $result->newpageid = $lesson->cluster_jump($page->id);
123 }
124}
125
126if ($result->nodefaultresponse) {
127 // Don't display feedback
a6855934 128 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id,'pageid'=>$result->newpageid)));
0a4abb73
SH
129}
130
131/// Set Messages
132
133if ($canmanage) {
134 // This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
135 if(lesson_display_teacher_warning($lesson)) {
39790bd8 136 $warningvars = new stdClass();
0a4abb73
SH
137 $warningvars->cluster = get_string("clusterjump", "lesson");
138 $warningvars->unseen = get_string("unseenpageinbranch", "lesson");
139 $lesson->add_message(get_string("teacherjumpwarning", "lesson", $warningvars));
140 }
141 // Inform teacher that s/he will not see the timer
142 if ($lesson->timed) {
143 $lesson->add_message(get_string("teachertimerwarning", "lesson"));
144 }
145}
146// Report attempts remaining
f3b21891 147if ($result->attemptsremaining != 0 && !$lesson->review) {
0a4abb73
SH
148 $lesson->add_message(get_string('attemptsremaining', 'lesson', $result->attemptsremaining));
149}
150// Report if max attempts reached
f3b21891 151if ($result->maxattemptsreached != 0 && !$lesson->review) {
0a4abb73
SH
152 $lesson->add_message('('.get_string("maximumnumberofattemptsreached", "lesson").')');
153}
154
a6855934 155$PAGE->set_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => $page->id));
0a4abb73
SH
156$PAGE->set_subpage($page->id);
157
158/// Print the header, heading and tabs
d9c26e21 159lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
448052a5 160echo $lessonoutput->header($lesson, $cm, 'view', true, $page->id);
0a4abb73
SH
161
162if ($lesson->displayleft) {
163 echo '<a name="maincontent" id="maincontent" title="'.get_string('anchortitle', 'lesson').'"></a>';
164}
165// This calculates and prints the ongoing score message
166if ($lesson->ongoing) {
167 echo $lessonoutput->ongoing_score($lesson);
168}
169echo $result->feedback;
170
171// User is modifying attempts - save button and some instructions
172if (isset($USER->modattempts[$lesson->id])) {
173 $url = $CFG->wwwroot.'/mod/lesson/view.php';
0a4abb73
SH
174 $content = $OUTPUT->box(get_string("savechangesandeol", "lesson"), 'center');
175 $content .= $OUTPUT->box(get_string("or", "lesson"), 'center');
176 $content .= $OUTPUT->box(get_string("continuetoanswer", "lesson"), 'center');
dc88d782 177 $content .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$cm->id));
916276fc
PS
178 $content .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>LESSON_EOL));
179 $content .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('savechanges', 'lesson')));
39f5b5c1 180 echo html_writer::tag('form', "<div>$content</div>", array('method'=>'post', 'action'=>$url));
0a4abb73
SH
181}
182
183// Review button back
184if ($lesson->review && !$result->correctanswer && !$result->noanswer && !$result->isessayquestion) {
185 $url = $CFG->wwwroot.'/mod/lesson/view.php';
dc88d782 186 $content = html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$cm->id));
916276fc
PS
187 $content .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$page->id));
188 $content .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('reviewquestionback', 'lesson')));
39f5b5c1 189 echo html_writer::tag('form', "<div class=\"singlebutton\">$content</div>", array('method'=>'post', 'action'=>$url));
0a4abb73
SH
190}
191
a6855934 192$url = new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id, 'pageid'=>$result->newpageid));
0a4abb73
SH
193if ($lesson->review && !$result->correctanswer && !$result->noanswer && !$result->isessayquestion) {
194 // Review button continue
5c2ed7e2 195 echo $OUTPUT->single_button($url, get_string('reviewquestioncontinue', 'lesson'));
0a4abb73
SH
196} else {
197 // Normal continue button
5c2ed7e2 198 echo $OUTPUT->single_button($url, get_string('continue', 'lesson'));
0a4abb73 199}
5c2ed7e2 200
0a4abb73 201echo $lessonoutput->footer();