3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Action for processing page answers by users
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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);
39 $context = context_module
::instance($cm->id
);
40 $canmanage = has_capability('mod/lesson:manage', $context);
41 $lessonoutput = $PAGE->get_renderer('mod_lesson');
43 $url = new moodle_url('/mod/lesson/continue.php', array('id'=>$cm->id
));
45 $PAGE->set_pagetype('mod-lesson-view');
46 $PAGE->navbar
->add(get_string('continue', 'lesson'));
48 // This is the code updates the lesson time for a timed test
49 // get time information for this user
51 $lesson->displayleft
= lesson_displayleftif($lesson);
52 $timer = $lesson->update_timer();
54 $timeleft = ($timer->starttime +
$lesson->maxtime
* 60) - time();
57 $lesson->add_message(get_string('eolstudentoutoftime', 'lesson'));
58 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id
,'pageid'=>LESSON_EOL
, 'outoftime'=>'normal')));
59 } else if ($timeleft < 60) {
61 $lesson->add_message(get_string("studentoneminwarning", "lesson"));
65 $timer = new stdClass
;
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
));
71 $userhasgrade = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id
, "userid"=>$USER->id
));
73 if ($userhasgrade && !$lesson->retake
) {
77 // Check the page has answers [MDL-25632]
78 if (count($page->answers
) > 0) {
79 $result = $page->record_attempt($context);
81 // The page has no answers so we will just progress to the next page in the
82 // sequence (as set by newpageid).
83 $result = new stdClass
;
84 $result->newpageid
= optional_param('newpageid', $page->nextpageid
, PARAM_INT
);
85 $result->nodefaultresponse
= true;
88 if (isset($USER->modattempts
[$lesson->id
])) {
89 // make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
90 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
91 $result->newpageid
= LESSON_EOL
;
93 $nretakes = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id
, "userid"=>$USER->id
));
94 $nretakes--; // make sure we are looking at the right try.
95 $attempts = $DB->get_records("lesson_attempts", array("lessonid"=>$lesson->id
, "userid"=>$USER->id
, "retry"=>$nretakes), "timeseen", "id, pageid");
98 // Make sure that the newpageid always defaults to something valid.
99 $result->newpageid
= LESSON_EOL
;
100 foreach($attempts as $attempt) {
101 if ($found && $temppageid != $attempt->pageid
) { // now try to find the next page, make sure next few attempts do no belong to current page
102 $result->newpageid
= $attempt->pageid
;
105 if ($attempt->pageid
== $page->id
) {
106 $found = true; // if found current page
107 $temppageid = $attempt->pageid
;
111 } elseif ($result->newpageid
!= LESSON_CLUSTERJUMP
&& $page->id
!= 0 && $result->newpageid
> 0) {
112 // going to check to see if the page that the user is going to view next, is a cluster page.
113 // If so, dont display, go into the cluster. The $result->newpageid > 0 is used to filter out all of the negative code jumps.
114 $newpage = $lesson->load_page($result->newpageid
);
115 if ($newpageid = $newpage->override_next_page($result->newpageid
)) {
116 $result->newpageid
= $newpageid;
118 } elseif ($result->newpageid
== LESSON_UNSEENBRANCHPAGE
) {
120 if ($page->nextpageid
== 0) {
121 $result->newpageid
= LESSON_EOL
;
123 $result->newpageid
= $page->nextpageid
;
126 $result->newpageid
= lesson_unseen_question_jump($lesson, $USER->id
, $page->id
);
128 } elseif ($result->newpageid
== LESSON_PREVIOUSPAGE
) {
129 $result->newpageid
= $page->prevpageid
;
130 } elseif ($result->newpageid
== LESSON_RANDOMPAGE
) {
131 $result->newpageid
= lesson_random_question_jump($lesson, $page->id
);
132 } elseif ($result->newpageid
== LESSON_CLUSTERJUMP
) {
134 if ($page->nextpageid
== 0) { // if teacher, go to next page
135 $result->newpageid
= LESSON_EOL
;
137 $result->newpageid
= $page->nextpageid
;
140 $result->newpageid
= $lesson->cluster_jump($page->id
);
144 if ($result->nodefaultresponse
) {
145 // Don't display feedback
146 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id
,'pageid'=>$result->newpageid
)));
152 // This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
153 if(lesson_display_teacher_warning($lesson)) {
154 $warningvars = new stdClass();
155 $warningvars->cluster
= get_string("clusterjump", "lesson");
156 $warningvars->unseen
= get_string("unseenpageinbranch", "lesson");
157 $lesson->add_message(get_string("teacherjumpwarning", "lesson", $warningvars));
159 // Inform teacher that s/he will not see the timer
160 if ($lesson->timed
) {
161 $lesson->add_message(get_string("teachertimerwarning", "lesson"));
164 // Report attempts remaining
165 if ($result->attemptsremaining
!= 0 && $lesson->review
&& !$reviewmode) {
166 $lesson->add_message(get_string('attemptsremaining', 'lesson', $result->attemptsremaining
));
168 // Report if max attempts reached
169 if ($result->maxattemptsreached
!= 0 && $lesson->review
&& !$reviewmode) {
170 $lesson->add_message('('.get_string("maximumnumberofattemptsreached", "lesson").')');
173 $PAGE->set_url('/mod/lesson/view.php', array('id' => $cm->id
, 'pageid' => $page->id
));
174 $PAGE->set_subpage($page->id
);
176 /// Print the header, heading and tabs
177 lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
178 echo $lessonoutput->header($lesson, $cm, 'view', true, $page->id
, get_string('continue', 'lesson'));
180 if ($lesson->displayleft
) {
181 echo '<a name="maincontent" id="maincontent" title="'.get_string('anchortitle', 'lesson').'"></a>';
183 // This calculates and prints the ongoing score message
184 if ($lesson->ongoing
&& !$reviewmode) {
185 echo $lessonoutput->ongoing_score($lesson);
187 if (!$result->maxattemptsreached
&& !$reviewmode) {
188 echo $result->feedback
;
191 // User is modifying attempts - save button and some instructions
192 if (isset($USER->modattempts
[$lesson->id
])) {
193 $url = $CFG->wwwroot
.'/mod/lesson/view.php';
194 $content = $OUTPUT->box(get_string("gotoendoflesson", "lesson"), 'center');
195 $content .= $OUTPUT->box(get_string("or", "lesson"), 'center');
196 $content .= $OUTPUT->box(get_string("continuetonextpage", "lesson"), 'center');
197 $content .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$cm->id
));
198 $content .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>LESSON_EOL
));
199 $content .= html_writer
::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('finish', 'lesson')));
200 echo html_writer
::tag('form', "<div>$content</div>", array('method'=>'post', 'action'=>$url));
203 // Review button back
204 if (!$result->correctanswer
&& !$result->noanswer
&& !$result->isessayquestion
&& !$reviewmode && $lesson->review
&& !$result->maxattemptsreached
) {
205 $url = $CFG->wwwroot
.'/mod/lesson/view.php';
206 $content = html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$cm->id
));
207 $content .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$page->id
));
208 $content .= html_writer
::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('reviewquestionback', 'lesson')));
209 echo html_writer
::tag('form', "<div class=\"singlebutton\">$content</div>", array('method'=>'post', 'action'=>$url));
212 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id
, 'pageid'=>$result->newpageid
));
213 if ($lesson->review
&& !$result->correctanswer
&& !$result->noanswer
&& !$result->isessayquestion
&& !$result->maxattemptsreached
) {
214 // Review button continue
215 echo $OUTPUT->single_button($url, get_string('reviewquestioncontinue', 'lesson'));
217 // Normal continue button
218 echo $OUTPUT->single_button($url, get_string('continue', 'lesson'));
221 echo $lessonoutput->footer();