';
print_simple_box_end();
print_footer($course);
exit();
} else if ($lesson->usepassword and empty($USER->lessonloggedin[$lesson->id])) { // Password protected lesson code
$correctpass = false;
if (!empty($userpassword)) {
// with or without md5 for backward compatibility (MDL-11090)
if (($lesson->password == md5(trim($userpassword))) or ($lesson->password == trim($userpassword))) {
$USER->lessonloggedin[$lesson->id] = true;
$correctpass = true;
if ($lesson->highscores) {
// Logged in - redirect so we go through all of these checks before starting the lesson.
redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id");
}
}
}
if (!$correctpass) {
lesson_print_header($cm, $course, $lesson);
echo "
\n";
print_footer($course);
exit();
}
} else if ($lesson->dependency) { // check for dependencies
if ($dependentlesson = get_record('lesson', 'id', $lesson->dependency)) {
// lesson exists, so we can proceed
$conditions = unserialize($lesson->conditions);
// assume false for all
$timespent = false;
$completed = false;
$gradebetterthan = false;
// check for the timespent condition
if ($conditions->timespent) {
if ($attempttimes = get_records_select('lesson_timer', "userid = $USER->id AND lessonid = $dependentlesson->id")) {
// go through all the times and test to see if any of them satisfy the condition
foreach($attempttimes as $attempttime) {
$duration = $attempttime->lessontime - $attempttime->starttime;
if ($conditions->timespent < $duration/60) {
$timespent = true;
}
}
}
} else {
$timespent = true; // there isn't one set
}
// check for the gradebetterthan condition
if($conditions->gradebetterthan) {
if ($studentgrades = get_records_select('lesson_grades', "userid = $USER->id AND lessonid = $dependentlesson->id")) {
// go through all the grades and test to see if any of them satisfy the condition
foreach($studentgrades as $studentgrade) {
if ($studentgrade->grade >= $conditions->gradebetterthan) {
$gradebetterthan = true;
}
}
}
} else {
$gradebetterthan = true; // there isn't one set
}
// check for the completed condition
if ($conditions->completed) {
if (count_records('lesson_grades', 'userid', $USER->id, 'lessonid', $dependentlesson->id)) {
$completed = true;
}
} else {
$completed = true; // not set
}
$errors = array();
// collect all of our error statements
if (!$timespent) {
$errors[] = get_string('timespenterror', 'lesson', $conditions->timespent);
}
if (!$completed) {
$errors[] = get_string('completederror', 'lesson');
}
if (!$gradebetterthan) {
$errors[] = get_string('gradebetterthanerror', 'lesson', $conditions->gradebetterthan);
}
if (!empty($errors)) { // print out the errors if any
lesson_print_header($cm, $course, $lesson);
echo '
';
print_simple_box_end();
echo '';
print_footer($course);
exit();
}
}
} else if ($lesson->highscores and !$lesson->practice and !optional_param('viewed', 0) and empty($pageid)) {
// Display high scores before starting lesson
redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id");
}
}
// set up some general variables
$path = $CFG->wwwroot .'/course';
// this is called if a student leaves during a lesson
if($pageid == LESSON_UNSEENBRANCHPAGE) {
$pageid = lesson_unseen_question_jump($lesson->id, $USER->id, $pageid);
}
// display individual pages and their sets of answers
// if pageid is EOL then the end of the lesson has been reached
// for flow, changed to simple echo for flow styles, michaelp, moved lesson name and page title down
$attemptflag = false;
if (empty($pageid)) {
// make sure there are pages to view
if (!get_field('lesson_pages', 'id', 'lessonid', $lesson->id, 'prevpageid', 0)) {
if (!has_capability('mod/lesson:manage', $context)) {
lesson_set_message(get_string('lessonnotready', 'lesson', $course->teacher)); // a nice message to the student
} else {
if (!count_records('lesson_pages', 'lessonid', $lesson->id)) {
redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id"); // no pages - redirect to add pages
} else {
lesson_set_message(get_string('lessonpagelinkingbroken', 'lesson')); // ok, bad mojo
}
}
}
add_to_log($course->id, 'lesson', 'start', 'view.php?id='. $cm->id, $lesson->id, $cm->id);
// if no pageid given see if the lesson has been started
if ($grades = get_records_select('lesson_grades', 'lessonid = '. $lesson->id .' AND userid = '. $USER->id,
'grade DESC')) {
$retries = count($grades);
} else {
$retries = 0;
}
if ($retries) {
$attemptflag = true;
}
if (isset($USER->modattempts[$lesson->id])) {
unset($USER->modattempts[$lesson->id]); // if no pageid, then student is NOT reviewing
}
// if there are any questions have been answered correctly in this attempt
if ($attempts = get_records_select('lesson_attempts',
"lessonid = $lesson->id AND userid = $USER->id AND retry = $retries AND
correct = 1", 'timeseen DESC')) {
foreach ($attempts as $attempt) {
$jumpto = get_field('lesson_answers', 'jumpto', 'id', $attempt->answerid);
// convert the jumpto to a proper page id
if ($jumpto == 0) { // unlikely value!
$lastpageseen = $attempt->pageid;
} elseif ($jumpto == LESSON_NEXTPAGE) {
if (!$lastpageseen = get_field('lesson_pages', 'nextpageid', 'id',
$attempt->pageid)) {
// no nextpage go to end of lesson
$lastpageseen = LESSON_EOL;
}
} else {
$lastpageseen = $jumpto;
}
break; // only look at the latest correct attempt
}
} else {
$attempts = NULL;
}
if ($branchtables = get_records_select('lesson_branch',
"lessonid = $lesson->id AND userid = $USER->id AND retry = $retries", 'timeseen DESC')) {
// in here, user has viewed a branch table
$lastbranchtable = current($branchtables);
if ($attempts != NULL) {
foreach($attempts as $attempt) {
if ($lastbranchtable->timeseen > $attempt->timeseen) {
// branch table was viewed later than the last attempt
$lastpageseen = $lastbranchtable->pageid;
}
break;
}
} else {
// hasnt answered any questions but has viewed a branch table
$lastpageseen = $lastbranchtable->pageid;
}
}
//if ($lastpageseen != $firstpageid) {
if (isset($lastpageseen) and count_records('lesson_attempts', 'lessonid', $lesson->id, 'userid', $USER->id, 'retry', $retries) > 0) {
// get the first page
if (!$firstpageid = get_field('lesson_pages', 'id', 'lessonid', $lesson->id,
'prevpageid', 0)) {
error('Navigation: first page not found');
}
lesson_print_header($cm, $course, $lesson);
if ($lesson->timed) {
if ($lesson->retake) {
print_simple_box('
";
print_simple_box_end();
print_footer($course);
exit();
//redirect("../../course/view.php?id=$course->id", get_string("alreadytaken", "lesson"));
// allow student to retake course even if they have the maximum grade
// } elseif ($bestgrade == 100) {
// redirect("../../course/view.php?id=$course->id", get_string("maximumgradeachieved",
// "lesson"));
}
}
// start at the first page
if (!$pageid = get_field('lesson_pages', 'id', 'lessonid', $lesson->id, 'prevpageid', 0)) {
error('Navigation: first page not found');
}
/// This is the code for starting a timed test
if(!isset($USER->startlesson[$lesson->id]) && !has_capability('mod/lesson:manage', $context)) {
$USER->startlesson[$lesson->id] = true;
$startlesson = new stdClass;
$startlesson->lessonid = $lesson->id;
$startlesson->userid = $USER->id;
$startlesson->starttime = time();
$startlesson->lessontime = time();
if (!insert_record('lesson_timer', $startlesson)) {
error('Error: could not insert row into lesson_timer table');
}
if ($lesson->timed) {
lesson_set_message(get_string('maxtimewarning', 'lesson', $lesson->maxtime), 'center');
}
}
}
if ($pageid != LESSON_EOL) {
/// This is the code updates the lessontime for a timed test
if ($startlastseen = optional_param('startlastseen', '', PARAM_ALPHA)) { /// this deletes old records not totally sure if this is necessary anymore
if ($startlastseen == 'no') {
if ($grades = get_records_select('lesson_grades', "lessonid = $lesson->id AND userid = $USER->id",
'grade DESC')) {
$retries = count($grades);
} else {
$retries = 0;
}
if (!delete_records('lesson_attempts', 'userid', $USER->id, 'lessonid', $lesson->id, 'retry', $retries)) {
error('Error: could not delete old attempts');
}
if (!delete_records('lesson_branch', 'userid', $USER->id, 'lessonid', $lesson->id, 'retry', $retries)) {
error('Error: could not delete old seen branches');
}
}
}
add_to_log($course->id, 'lesson', 'view', 'view.php?id='. $cm->id, $pageid, $cm->id);
if (!$page = get_record('lesson_pages', 'id', $pageid)) {
error('Navigation: the page record not found');
}
if ($page->qtype == LESSON_CLUSTER) { //this only gets called when a user starts up a new lesson and the first page is a cluster page
if (!has_capability('mod/lesson:manage', $context)) {
// get new id
$pageid = lesson_cluster_jump($lesson->id, $USER->id, $pageid);
// get new page info
if (!$page = get_record('lesson_pages', 'id', $pageid)) {
error('Navigation: the page record not found');
}
add_to_log($course->id, 'lesson', 'view', 'view.php?id='. $cm->id, $pageid, $cm->id);
} else {
// get the next page
$pageid = $page->nextpageid;
if (!$page = get_record('lesson_pages', 'id', $pageid)) {
error('Navigation: the page record not found');
}
}
} elseif ($page->qtype == LESSON_ENDOFCLUSTER) { // Check for endofclusters
if ($page->nextpageid == 0) {
$nextpageid = LESSON_EOL;
} else {
$nextpageid = $page->nextpageid;
}
redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&pageid=$nextpageid");
} else if ($page->qtype == LESSON_ENDOFBRANCH) { // Check for endofbranches
if ($answers = get_records('lesson_answers', 'pageid', $page->id, 'id')) {
// print_heading(get_string('endofbranch', 'lesson'));
foreach ($answers as $answer) {
// just need the first answer
if ($answer->jumpto == LESSON_RANDOMBRANCH) {
$answer->jumpto = lesson_unseen_branch_jump($lesson->id, $USER->id);
} elseif ($answer->jumpto == LESSON_CLUSTERJUMP) {
if (!has_capability('mod/lesson:manage', $context)) {
$answer->jumpto = lesson_cluster_jump($lesson->id, $USER->id, $pageid);
} else {
if ($page->nextpageid == 0) {
$answer->jumpto = LESSON_EOL;
} else {
$answer->jumpto = $page->nextpageid;
}
}
} else if ($answer->jumpto == LESSON_NEXTPAGE) {
if ($page->nextpageid == 0) {
$answer->jumpto = LESSON_EOL;
} else {
$answer->jumpto = $page->nextpageid;
}
} else if ($answer->jumpto == 0) {
$answer->jumpto = $page->id;
} else if ($answer->jumpto == LESSON_PREVIOUSPAGE) {
$answer->jumpto = $page->prevpageid;
}
redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&pageid=$answer->jumpto");
break;
}
} else {
error('Navigation: No answers on EOB');
}
}
// check to see if the user can see the left menu
if (!has_capability('mod/lesson:manage', $context)) {
$lesson->displayleft = lesson_displayleftif($lesson);
}
// This is where several messages (usually warnings) are displayed
// all of this is displayed above the actual page
// clock code
// get time information for this user
$timer = new stdClass;
if(!has_capability('mod/lesson:manage', $context)) {
if (!$timer = get_records_select('lesson_timer', "lessonid = $lesson->id AND userid = $USER->id", 'starttime')) {
error('Error: could not find records');
} else {
$timer = array_pop($timer); // this will get the latest start time record
}
}
$startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
if ($startlastseen == 'yes') { // continue a previous test, need to update the clock (think this option is disabled atm)
$timer->starttime = time() - ($timer->lessontime - $timer->starttime);
$timer->lessontime = time();
} else if ($startlastseen == 'no') { // starting over
// starting over, so reset the clock
$timer->starttime = time();
$timer->lessontime = time();
}
// for timed lessons, display clock
if ($lesson->timed) {
if(has_capability('mod/lesson:manage', $context)) {
lesson_set_message(get_string('teachertimerwarning', 'lesson'));
} else {
$timeleft = ($timer->starttime + $lesson->maxtime * 60) - time();
if ($timeleft <= 0) {
// Out of time
lesson_set_message(get_string('eolstudentoutoftime', 'lesson'));
redirect("$CFG->wwwroot/mod/lesson/view.php?id=$cm->id&pageid=".LESSON_EOL."&outoftime=normal");
die; // Shouldn't be reached, but make sure
} else if ($timeleft < 60) {
// One minute warning
lesson_set_message(get_string('studentoneminwarning', 'lesson'));
}
}
}
// update the clock
if (!has_capability('mod/lesson:manage', $context)) {
$timer->lessontime = time();
if (!update_record('lesson_timer', $timer)) {
error('Error: could not update lesson_timer table');
}
}
/// This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
if(has_capability('mod/lesson:manage', $context)) {
if (lesson_display_teacher_warning($lesson->id)) {
$warningvars->cluster = get_string('clusterjump', 'lesson');
$warningvars->unseen = get_string('unseenpageinbranch', 'lesson');
lesson_set_message(get_string('teacherjumpwarning', 'lesson', $warningvars));
}
}
if ($page->qtype == LESSON_BRANCHTABLE) {
if ($lesson->minquestions and !has_capability('mod/lesson:manage', $context)) {
// tell student how many questions they have seen, how many are required and their grade
$ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
$gradeinfo = lesson_grade($lesson, $ntries);
if ($gradeinfo->attempts) {
if ($gradeinfo->nquestions < $lesson->minquestions) {
$a = new stdClass;
$a->nquestions = $gradeinfo->nquestions;
$a->minquestions = $lesson->minquestions;
lesson_set_message(get_string('numberofpagesviewednotice', 'lesson', $a));
}
lesson_set_message(get_string("numberofcorrectanswers", "lesson", $gradeinfo->earned), 'notify');
$a = new stdClass;
$a->grade = number_format($gradeinfo->grade * $lesson->grade / 100, 1);
$a->total = $lesson->grade;
lesson_set_message(get_string('yourcurrentgradeisoutof', 'lesson', $a), 'notify');
}
}
}
$PAGE = page_create_instance($lesson->id);
$PAGE->set_lessonpageid($page->id);
$pageblocks = blocks_setup($PAGE);
$leftcolumnwidth = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
$rightcolumnwidth = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);
if (($edit != -1) and $PAGE->user_allowed_editing()) {
$USER->editing = $edit;
}
/// Print the page header, heading and tabs
$PAGE->print_header();
if ($attemptflag) {
print_heading(get_string('attempt', 'lesson', $retries + 1));
}
/// This calculates and prints the ongoing score
if ($lesson->ongoing and !empty($pageid)) {
lesson_print_ongoing_score($lesson);
}
require($CFG->dirroot.'/mod/lesson/viewstart.html');
// now starting to print the page's contents
if ($page->qtype == LESSON_BRANCHTABLE) {
print_heading(format_string($page->title));
} else {
$lesson->slideshow = false; // turn off slide show for all pages other than LESSON_BRANTCHTABLE
}
if (!$lesson->slideshow) {
$options = new stdClass;
$options->noclean = true;
print_simple_box('
', 'center');
}
// this is for modattempts option. Find the users previous answer to this page,
// and then display it below in answer processing
if (isset($USER->modattempts[$lesson->id])) {
$retries = count_records('lesson_grades', "lessonid", $lesson->id, "userid", $USER->id);
$retries--;
if (! $attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $USER->id AND pageid = $page->id AND retry = $retries", "timeseen")) {
error("Previous attempt record could not be found!");
}
$attempt = end($attempts);
}
// get the answers in a set order, the id order
if ($answers = get_records("lesson_answers", "pageid", $page->id, "id")) {
if ($page->qtype != LESSON_BRANCHTABLE) { // To fix XHTML problem (BT have their own forms)
echo "\n";
}
} else {
// a page without answers - find the next (logical) page
echo "
wwwroot/mod/lesson/view.php\">\n";
echo '
';
echo "id\" />\n";
if ($lesson->nextpagedefault) {
// in Flash Card mode...
// ...first get number of retakes
$nretakes = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
// ...then get the page ids (lessonid the 5th param is needed to make get_records play)
$allpages = get_records("lesson_pages", "lessonid", $lesson->id, "id", "id,lessonid");
shuffle ($allpages);
$found = false;
if ($lesson->nextpagedefault == LESSON_UNSEENPAGE) {
foreach ($allpages as $thispage) {
if (!count_records("lesson_attempts", "pageid", $thispage->id, "userid",
$USER->id, "retry", $nretakes)) {
$found = true;
break;
}
}
} elseif ($lesson->nextpagedefault == LESSON_UNANSWEREDPAGE) {
foreach ($allpages as $thispage) {
if (!count_records_select("lesson_attempts", "pageid = $thispage->id AND
userid = $USER->id AND correct = 1 AND retry = $nretakes")) {
$found = true;
break;
}
}
}
if ($found) {
$newpageid = $thispage->id;
if ($lesson->maxpages) {
// check number of pages viewed (in the lesson)
if (count_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id,
"retry", $nretakes) >= $lesson->maxpages) {
$newpageid = LESSON_EOL;
}
}
} else {
$newpageid = LESSON_EOL;
}
} else {
// in normal lesson mode...
if (!$newpageid = get_field("lesson_pages", "nextpageid", "id", $pageid)) {
// this is the last page - flag end of lesson
$newpageid = LESSON_EOL;
}
}
echo "\n";
lesson_print_submit_link(get_string('continue', 'lesson'), 'pageform');
echo '
';
echo "
\n";
}
// Finish of the page
lesson_print_progress_bar($lesson, $course);
require($CFG->dirroot.'/mod/lesson/viewend.html');
} else {
// end of lesson reached work out grade
// Used to check to see if the student ran out of time
$outoftime = optional_param('outoftime', '', PARAM_ALPHA);
// Update the clock / get time information for this user
if (!has_capability('mod/lesson:manage', $context)) {
unset($USER->startlesson[$lesson->id]);
if (!$timer = get_records_select('lesson_timer', "lessonid = $lesson->id AND userid = $USER->id", 'starttime')) {
error('Error: could not find records');
} else {
$timer = array_pop($timer); // this will get the latest start time record
}
$timer->lessontime = time();
if (!update_record("lesson_timer", $timer)) {
error("Error: could not update lesson_timer table");
}
}
add_to_log($course->id, "lesson", "end", "view.php?id=$cm->id", "$lesson->id", $cm->id);
lesson_print_header($cm, $course, $lesson, 'view');
print_heading(get_string("congratulations", "lesson"));
print_simple_box_start("center");
$ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
if (isset($USER->modattempts[$lesson->id])) {
$ntries--; // need to look at the old attempts :)
}
if (!has_capability('mod/lesson:manage', $context)) {
$gradeinfo = lesson_grade($lesson, $ntries);
if ($gradeinfo->attempts) {
if (!$lesson->custom) {
echo "
\n";
$grade->lessonid = $lesson->id;
$grade->userid = $USER->id;
$grade->grade = $gradeinfo->grade;
$grade->completed = time();
if (!$lesson->practice) {
if (isset($USER->modattempts[$lesson->id])) { // if reviewing, make sure update old grade record
if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id and userid = $USER->id", "completed")) {
error("Could not find Grade Records");
}
$oldgrade = end($grades);
$grade->id = $oldgrade->id;
if (!$update = update_record("lesson_grades", $grade)) {
error("Navigation: grade not updated");
}
} else {
if (!$newgradeid = insert_record("lesson_grades", $grade)) {
error("Navigation: grade not inserted");
}
}
} else {
if (!delete_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id, "retry", $ntries)) {
error("Could not delete lesson attempts");
}
}
} else {
if ($lesson->timed) {
if ($outoftime == 'normal') {
$grade = new stdClass;
$grade->lessonid = $lesson->id;
$grade->userid = $USER->id;
$grade->grade = 0;
$grade->completed = time();
if (!$lesson->practice) {
if (!$newgradeid = insert_record("lesson_grades", $grade)) {
error("Navigation: grade not inserted");
}
}
echo get_string("eolstudentoutoftimenoanswers", "lesson");
}
} else {
echo get_string("welldone", "lesson");
}
}
// update central gradebook
lesson_update_grades($lesson, $USER->id);
} else {
// display for teacher
echo "
".get_string("displayofgrade", "lesson")."
\n";
}
print_simple_box_end(); //End of Lesson button to Continue.
// after all the grade processing, check to see if "Show Grades" is off for the course
// if yes, redirect to the course page
if (!$course->showgrades) {
redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
}
// high scores code
if ($lesson->highscores && !has_capability('mod/lesson:manage', $context) && !$lesson->practice) {
echo "
";
if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
$madeit = false;
if ($highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
// get all the high scores into an array
$topscores = array();
$uniquescores = array();
foreach ($highscores as $highscore) {
$grade = $grades[$highscore->gradeid]->grade;
$topscores[] = $grade;
$uniquescores[$grade] = 1;
}
// sort to find the lowest score
sort($topscores);
$lowscore = $topscores[0];
if ($gradeinfo->grade >= $lowscore || count($uniquescores) <= $lesson->maxhighscores) {
$madeit = true;
}
}
if (!$highscores or $madeit) {
echo '
";
}
if ($lesson->modattempts && !has_capability('mod/lesson:manage', $context)) {
// make sure if the student is reviewing, that he/she sees the same pages/page path that he/she saw the first time
// look at the attempt records to find the first QUESTION page that the user answered, then use that page id
// to pass to view again. This is slick cause it wont call the empty($pageid) code
// $ntries is decremented above
if (!$attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $USER->id AND retry = $ntries", "timeseen")) {
$attempts = array();
}
$firstattempt = current($attempts);
$pageid = $firstattempt->pageid;
// IF the student wishes to review, need to know the last question page that the student answered. This will help to make
// sure that the student can leave the lesson via pushing the continue button.
$lastattempt = end($attempts);
$USER->modattempts[$lesson->id] = $lastattempt->pageid;
echo "