2009-12-16 02:00:48 +00:00
< ? php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Action for processing page answers by users
*
2010-07-25 10:54:39 +00:00
* @ package mod
* @ subpackage lesson
* @ copyright 2009 Sam Hemelryk
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
2009-12-16 02:00:48 +00:00
**/
/** Require the specific libraries */
require_once ( " ../../config.php " );
require_once ( $CFG -> dirroot . '/mod/lesson/locallib.php' );
2010-07-25 09:30:09 +00:00
$id = required_param ( 'id' , PARAM_INT );
2012-11-15 09:51:26 +08:00
$cm = get_coursemodule_from_id ( 'lesson' , $id , 0 , false , MUST_EXIST );
2013-08-21 13:42:30 +08:00
$course = $DB -> get_record ( 'course' , array ( 'id' => $cm -> course ), '*' , MUST_EXIST );
2010-07-25 09:30:09 +00:00
$lesson = new lesson ( $DB -> get_record ( 'lesson' , array ( 'id' => $cm -> instance ), '*' , MUST_EXIST ));
2009-12-16 02:00:48 +00:00
require_login ( $course , false , $cm );
require_sesskey ();
2012-07-27 13:26:35 +08:00
$context = context_module :: instance ( $cm -> id );
2009-12-16 02:00:48 +00:00
$canmanage = has_capability ( 'mod/lesson:manage' , $context );
2009-12-17 14:06:22 +00:00
$lessonoutput = $PAGE -> get_renderer ( 'mod_lesson' );
2009-12-16 02:00:48 +00:00
2010-01-16 15:39:56 +00:00
$url = new moodle_url ( '/mod/lesson/continue.php' , array ( 'id' => $cm -> id ));
2009-12-16 02:00:48 +00:00
$PAGE -> set_url ( $url );
$PAGE -> navbar -> add ( get_string ( 'continue' , 'lesson' ));
// This is the code updates the lesson time for a timed test
// get time information for this user
if ( ! $canmanage ) {
$lesson -> displayleft = lesson_displayleftif ( $lesson );
$timer = $lesson -> update_timer ();
if ( $lesson -> timed ) {
$timeleft = ( $timer -> starttime + $lesson -> maxtime * 60 ) - time ();
if ( $timeleft <= 0 ) {
// Out of time
$lesson -> add_message ( get_string ( 'eolstudentoutoftime' , 'lesson' ));
2010-01-16 15:39:56 +00:00
redirect ( new moodle_url ( '/mod/lesson/view.php' , array ( 'id' => $cm -> id , 'pageid' => LESSON_EOL , 'outoftime' => 'normal' )));
2009-12-16 02:00:48 +00:00
} else if ( $timeleft < 60 ) {
// One minute warning
$lesson -> add_message ( get_string ( " studentoneminwarning " , " lesson " ));
}
}
} else {
$timer = new stdClass ;
}
// record answer (if necessary) and show response (if none say if answer is correct or not)
$page = $lesson -> load_page ( required_param ( 'pageid' , PARAM_INT ));
2011-06-29 18:18:52 +08:00
$userhasgrade = $DB -> count_records ( " lesson_grades " , array ( " lessonid " => $lesson -> id , " userid " => $USER -> id ));
$reviewmode = false ;
if ( $userhasgrade && ! $lesson -> retake ) {
$reviewmode = true ;
}
2010-12-14 15:06:08 +08:00
// Check the page has answers [MDL-25632]
if ( count ( $page -> answers ) > 0 ) {
$result = $page -> record_attempt ( $context );
} else {
// The page has no answers so we will just progress to the next page in the
// sequence (as set by newpageid).
$result = new stdClass ;
$result -> newpageid = optional_param ( 'newpageid' , $page -> nextpageid , PARAM_INT );
$result -> nodefaultresponse = true ;
}
2009-12-16 02:00:48 +00:00
if ( isset ( $USER -> modattempts [ $lesson -> id ])) {
// make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
2011-06-29 18:18:52 +08:00
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
2009-12-16 02:00:48 +00:00
$result -> newpageid = LESSON_EOL ;
} else {
2010-08-09 09:17:56 +00:00
$nretakes = $DB -> count_records ( " lesson_grades " , array ( " lessonid " => $lesson -> id , " userid " => $USER -> id ));
2009-12-16 02:00:48 +00:00
$nretakes -- ; // make sure we are looking at the right try.
$attempts = $DB -> get_records ( " lesson_attempts " , array ( " lessonid " => $lesson -> id , " userid " => $USER -> id , " retry " => $nretakes ), " timeseen " , " id, pageid " );
$found = false ;
$temppageid = 0 ;
foreach ( $attempts as $attempt ) {
if ( $found && $temppageid != $attempt -> pageid ) { // now try to find the next page, make sure next few attempts do no belong to current page
$result -> newpageid = $attempt -> pageid ;
break ;
}
if ( $attempt -> pageid == $page -> id ) {
$found = true ; // if found current page
$temppageid = $attempt -> pageid ;
}
}
}
} elseif ( $result -> newpageid != LESSON_CLUSTERJUMP && $page -> id != 0 && $result -> newpageid > 0 ) {
// going to check to see if the page that the user is going to view next, is a cluster page.
// If so, dont display, go into the cluster. The $result->newpageid > 0 is used to filter out all of the negative code jumps.
$newpage = $lesson -> load_page ( $result -> newpageid );
if ( $newpageid = $newpage -> override_next_page ( $result -> newpageid )) {
$result -> newpageid = $newpageid ;
}
} elseif ( $result -> newpageid == LESSON_UNSEENBRANCHPAGE ) {
if ( $canmanage ) {
if ( $page -> nextpageid == 0 ) {
$result -> newpageid = LESSON_EOL ;
} else {
$result -> newpageid = $page -> nextpageid ;
}
} else {
$result -> newpageid = lesson_unseen_question_jump ( $lesson , $USER -> id , $page -> id );
}
} elseif ( $result -> newpageid == LESSON_PREVIOUSPAGE ) {
$result -> newpageid = $page -> prevpageid ;
} elseif ( $result -> newpageid == LESSON_RANDOMPAGE ) {
$result -> newpageid = lesson_random_question_jump ( $lesson , $page -> id );
} elseif ( $result -> newpageid == LESSON_CLUSTERJUMP ) {
if ( $canmanage ) {
if ( $page -> nextpageid == 0 ) { // if teacher, go to next page
$result -> newpageid = LESSON_EOL ;
} else {
$result -> newpageid = $page -> nextpageid ;
}
} else {
$result -> newpageid = $lesson -> cluster_jump ( $page -> id );
}
}
if ( $result -> nodefaultresponse ) {
// Don't display feedback
2010-01-16 15:39:56 +00:00
redirect ( new moodle_url ( '/mod/lesson/view.php' , array ( 'id' => $cm -> id , 'pageid' => $result -> newpageid )));
2009-12-16 02:00:48 +00:00
}
/// Set Messages
if ( $canmanage ) {
// This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
if ( lesson_display_teacher_warning ( $lesson )) {
2010-09-21 08:37:36 +00:00
$warningvars = new stdClass ();
2009-12-16 02:00:48 +00:00
$warningvars -> cluster = get_string ( " clusterjump " , " lesson " );
$warningvars -> unseen = get_string ( " unseenpageinbranch " , " lesson " );
$lesson -> add_message ( get_string ( " teacherjumpwarning " , " lesson " , $warningvars ));
}
// Inform teacher that s/he will not see the timer
if ( $lesson -> timed ) {
$lesson -> add_message ( get_string ( " teachertimerwarning " , " lesson " ));
}
}
// Report attempts remaining
2011-06-29 18:18:52 +08:00
if ( $result -> attemptsremaining != 0 && ! $lesson -> review && ! $reviewmode ) {
2009-12-16 02:00:48 +00:00
$lesson -> add_message ( get_string ( 'attemptsremaining' , 'lesson' , $result -> attemptsremaining ));
}
// Report if max attempts reached
2011-06-29 18:18:52 +08:00
if ( $result -> maxattemptsreached != 0 && ! $lesson -> review && ! $reviewmode ) {
2009-12-16 02:00:48 +00:00
$lesson -> add_message ( '(' . get_string ( " maximumnumberofattemptsreached " , " lesson " ) . ')' );
}
2010-01-16 15:39:56 +00:00
$PAGE -> set_url ( '/mod/lesson/view.php' , array ( 'id' => $cm -> id , 'pageid' => $page -> id ));
2009-12-16 02:00:48 +00:00
$PAGE -> set_subpage ( $page -> id );
/// Print the header, heading and tabs
2010-12-13 13:23:49 +00:00
lesson_add_fake_blocks ( $PAGE , $cm , $lesson , $timer );
2012-09-13 16:37:46 +08:00
echo $lessonoutput -> header ( $lesson , $cm , 'view' , true , $page -> id , get_string ( 'continue' , 'lesson' ));
2009-12-16 02:00:48 +00:00
if ( $lesson -> displayleft ) {
echo '<a name="maincontent" id="maincontent" title="' . get_string ( 'anchortitle' , 'lesson' ) . '"></a>' ;
}
// This calculates and prints the ongoing score message
2011-06-29 18:18:52 +08:00
if ( $lesson -> ongoing && ! $reviewmode ) {
2009-12-16 02:00:48 +00:00
echo $lessonoutput -> ongoing_score ( $lesson );
}
echo $result -> feedback ;
// User is modifying attempts - save button and some instructions
if ( isset ( $USER -> modattempts [ $lesson -> id ])) {
$url = $CFG -> wwwroot . '/mod/lesson/view.php' ;
2011-06-22 17:33:34 +08:00
$content = $OUTPUT -> box ( get_string ( " gotoendoflesson " , " lesson " ), 'center' );
2009-12-16 02:00:48 +00:00
$content .= $OUTPUT -> box ( get_string ( " or " , " lesson " ), 'center' );
2011-06-22 17:33:34 +08:00
$content .= $OUTPUT -> box ( get_string ( " continuetonextpage " , " lesson " ), 'center' );
2010-07-25 10:02:42 +00:00
$content .= html_writer :: empty_tag ( 'input' , array ( 'type' => 'hidden' , 'name' => 'id' , 'value' => $cm -> id ));
2010-02-07 20:12:31 +00:00
$content .= html_writer :: empty_tag ( 'input' , array ( 'type' => 'hidden' , 'name' => 'pageid' , 'value' => LESSON_EOL ));
2011-06-22 17:33:34 +08:00
$content .= html_writer :: empty_tag ( 'input' , array ( 'type' => 'submit' , 'name' => 'submit' , 'value' => get_string ( 'finish' , 'lesson' )));
2010-07-28 13:54:32 +00:00
echo html_writer :: tag ( 'form' , " <div> $content </div> " , array ( 'method' => 'post' , 'action' => $url ));
2009-12-16 02:00:48 +00:00
}
// Review button back
2011-11-08 12:13:48 +08:00
if ( ! $result -> correctanswer && ! $result -> noanswer && ! $result -> isessayquestion && ! $reviewmode && $lesson -> review ) {
2009-12-16 02:00:48 +00:00
$url = $CFG -> wwwroot . '/mod/lesson/view.php' ;
2010-07-25 10:02:42 +00:00
$content = html_writer :: empty_tag ( 'input' , array ( 'type' => 'hidden' , 'name' => 'id' , 'value' => $cm -> id ));
2010-02-07 20:12:31 +00:00
$content .= html_writer :: empty_tag ( 'input' , array ( 'type' => 'hidden' , 'name' => 'pageid' , 'value' => $page -> id ));
$content .= html_writer :: empty_tag ( 'input' , array ( 'type' => 'submit' , 'name' => 'submit' , 'value' => get_string ( 'reviewquestionback' , 'lesson' )));
2010-07-28 13:54:32 +00:00
echo html_writer :: tag ( 'form' , " <div class= \" singlebutton \" > $content </div> " , array ( 'method' => 'post' , 'action' => $url ));
2009-12-16 02:00:48 +00:00
}
2010-01-16 15:39:56 +00:00
$url = new moodle_url ( '/mod/lesson/view.php' , array ( 'id' => $cm -> id , 'pageid' => $result -> newpageid ));
2009-12-16 02:00:48 +00:00
if ( $lesson -> review && ! $result -> correctanswer && ! $result -> noanswer && ! $result -> isessayquestion ) {
// Review button continue
2010-01-03 15:46:14 +00:00
echo $OUTPUT -> single_button ( $url , get_string ( 'reviewquestioncontinue' , 'lesson' ));
2009-12-16 02:00:48 +00:00
} else {
// Normal continue button
2010-01-03 15:46:14 +00:00
echo $OUTPUT -> single_button ( $url , get_string ( 'continue' , 'lesson' ));
2009-12-16 02:00:48 +00:00
}
2010-01-03 15:46:14 +00:00
2012-11-15 09:51:26 +08:00
echo $lessonoutput -> footer ();