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->navbar
->add(get_string('continue', 'lesson'));
47 // This is the code updates the lesson time for a timed test
48 // get time information for this user
50 $lesson->displayleft
= lesson_displayleftif($lesson);
51 $timer = $lesson->update_timer();
53 $timeleft = ($timer->starttime +
$lesson->maxtime
* 60) - time();
56 $lesson->add_message(get_string('eolstudentoutoftime', 'lesson'));
57 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id
,'pageid'=>LESSON_EOL
, 'outoftime'=>'normal')));
58 } else if ($timeleft < 60) {
60 $lesson->add_message(get_string("studentoneminwarning", "lesson"));
64 $timer = new stdClass
;
67 // record answer (if necessary) and show response (if none say if answer is correct or not)
68 $page = $lesson->load_page(required_param('pageid', PARAM_INT
));
70 $userhasgrade = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id
, "userid"=>$USER->id
));
72 if ($userhasgrade && !$lesson->retake
) {
76 // Check the page has answers [MDL-25632]
77 if (count($page->answers
) > 0) {
78 $result = $page->record_attempt($context);
80 // The page has no answers so we will just progress to the next page in the
81 // sequence (as set by newpageid).
82 $result = new stdClass
;
83 $result->newpageid
= optional_param('newpageid', $page->nextpageid
, PARAM_INT
);
84 $result->nodefaultresponse
= true;
87 if (isset($USER->modattempts
[$lesson->id
])) {
88 // make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
89 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
90 $result->newpageid
= LESSON_EOL
;
92 $nretakes = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id
, "userid"=>$USER->id
));
93 $nretakes--; // make sure we are looking at the right try.
94 $attempts = $DB->get_records("lesson_attempts", array("lessonid"=>$lesson->id
, "userid"=>$USER->id
, "retry"=>$nretakes), "timeseen", "id, pageid");
97 // Make sure that the newpageid always defaults to something valid.
98 $result->newpageid
= LESSON_EOL
;
99 foreach($attempts as $attempt) {
100 if ($found && $temppageid != $attempt->pageid
) { // now try to find the next page, make sure next few attempts do no belong to current page
101 $result->newpageid
= $attempt->pageid
;
104 if ($attempt->pageid
== $page->id
) {
105 $found = true; // if found current page
106 $temppageid = $attempt->pageid
;
110 } elseif ($result->newpageid
!= LESSON_CLUSTERJUMP
&& $page->id
!= 0 && $result->newpageid
> 0) {
111 // going to check to see if the page that the user is going to view next, is a cluster page.
112 // If so, dont display, go into the cluster. The $result->newpageid > 0 is used to filter out all of the negative code jumps.
113 $newpage = $lesson->load_page($result->newpageid
);
114 if ($newpageid = $newpage->override_next_page($result->newpageid
)) {
115 $result->newpageid
= $newpageid;
117 } elseif ($result->newpageid
== LESSON_UNSEENBRANCHPAGE
) {
119 if ($page->nextpageid
== 0) {
120 $result->newpageid
= LESSON_EOL
;
122 $result->newpageid
= $page->nextpageid
;
125 $result->newpageid
= lesson_unseen_question_jump($lesson, $USER->id
, $page->id
);
127 } elseif ($result->newpageid
== LESSON_PREVIOUSPAGE
) {
128 $result->newpageid
= $page->prevpageid
;
129 } elseif ($result->newpageid
== LESSON_RANDOMPAGE
) {
130 $result->newpageid
= lesson_random_question_jump($lesson, $page->id
);
131 } elseif ($result->newpageid
== LESSON_CLUSTERJUMP
) {
133 if ($page->nextpageid
== 0) { // if teacher, go to next page
134 $result->newpageid
= LESSON_EOL
;
136 $result->newpageid
= $page->nextpageid
;
139 $result->newpageid
= $lesson->cluster_jump($page->id
);
143 if ($result->nodefaultresponse
) {
144 // Don't display feedback
145 redirect(new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id
,'pageid'=>$result->newpageid
)));
151 // This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
152 if(lesson_display_teacher_warning($lesson)) {
153 $warningvars = new stdClass();
154 $warningvars->cluster
= get_string("clusterjump", "lesson");
155 $warningvars->unseen
= get_string("unseenpageinbranch", "lesson");
156 $lesson->add_message(get_string("teacherjumpwarning", "lesson", $warningvars));
158 // Inform teacher that s/he will not see the timer
159 if ($lesson->timed
) {
160 $lesson->add_message(get_string("teachertimerwarning", "lesson"));
163 // Report attempts remaining
164 if ($result->attemptsremaining
!= 0 && !$lesson->review
&& !$reviewmode) {
165 $lesson->add_message(get_string('attemptsremaining', 'lesson', $result->attemptsremaining
));
167 // Report if max attempts reached
168 if ($result->maxattemptsreached
!= 0 && !$lesson->review
&& !$reviewmode) {
169 $lesson->add_message('('.get_string("maximumnumberofattemptsreached", "lesson").')');
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 echo $result->feedback
;
188 // User is modifying attempts - save button and some instructions
189 if (isset($USER->modattempts
[$lesson->id
])) {
190 $url = $CFG->wwwroot
.'/mod/lesson/view.php';
191 $content = $OUTPUT->box(get_string("gotoendoflesson", "lesson"), 'center');
192 $content .= $OUTPUT->box(get_string("or", "lesson"), 'center');
193 $content .= $OUTPUT->box(get_string("continuetonextpage", "lesson"), 'center');
194 $content .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$cm->id
));
195 $content .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>LESSON_EOL
));
196 $content .= html_writer
::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('finish', 'lesson')));
197 echo html_writer
::tag('form', "<div>$content</div>", array('method'=>'post', 'action'=>$url));
200 // Review button back
201 if (!$result->correctanswer
&& !$result->noanswer
&& !$result->isessayquestion
&& !$reviewmode && $lesson->review
) {
202 $url = $CFG->wwwroot
.'/mod/lesson/view.php';
203 $content = html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'id', 'value'=>$cm->id
));
204 $content .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'pageid', 'value'=>$page->id
));
205 $content .= html_writer
::empty_tag('input', array('type'=>'submit', 'name'=>'submit', 'value'=>get_string('reviewquestionback', 'lesson')));
206 echo html_writer
::tag('form', "<div class=\"singlebutton\">$content</div>", array('method'=>'post', 'action'=>$url));
209 $url = new moodle_url('/mod/lesson/view.php', array('id'=>$cm->id
, 'pageid'=>$result->newpageid
));
210 if ($lesson->review
&& !$result->correctanswer
&& !$result->noanswer
&& !$result->isessayquestion
) {
211 // Review button continue
212 echo $OUTPUT->single_button($url, get_string('reviewquestioncontinue', 'lesson'));
214 // Normal continue button
215 echo $OUTPUT->single_button($url, get_string('continue', 'lesson'));
218 echo $lessonoutput->footer();