';
print_simple_box_end();
print_footer($course);
exit();
} elseif ($lesson->highscores) {
$action = 'highscores';
} else {
$action = 'navigation';
}
}
/// CDC-FLAG changed the update_module_button and added another button when a teacher is checking the navigation of the lesson
if (isteacheredit($course->id)) {
$button = '
';
print_simple_box_end();
}
} else {
echo "\n"; echo "";///CDC Chris Berri added close div tag
}
print_footer($course);
exit();
}
if ($grades) {
foreach ($grades as $grade) {
$bestgrade = $grade->grade;
break;
}
if (!$lesson->retake) {
print_simple_box_start('center');
echo "
";
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');
}
/// CDC-FLAG /// -- This is the code for starting a timed test
if(!isset($USER->startlesson[$lesson->id]) && !isteacher($course->id)) {
$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) {
$timedflag = true;
}
}
/// CDC-FLAG ///
}
if ($pageid != LESSON_EOL) {
/// This is the code updates the lessontime for a timed test
if (isset($_POST['startlastseen'])) { /// this deletes old records not totally sure if this is necessary anymore
if ($_POST['startlastseen'] == 'no') {
if ($grades = get_records_select('lesson_grades', "lessonid = $lesson->id AND userid = $USER->id",
'grade DESC')) {
$retries = count($grades);
} else {
$retries = 0;
}
// NoticeFix big fix on the two delete_records
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 (!isteacher($course->id)) {
// 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) {
if ($page->nextpageid == 0) {
$nextpageid = LESSON_EOL;
} else {
$nextpageid = $page->nextpageid;
}
redirect("view.php?id=$cm->id&action=navigation&pageid=$nextpageid", get_string('endofclustertitle', 'lesson'));
}
// start of left menu
if ($lesson->displayleft) {
echo '
'; // only want this if no left menu
}
// starts the slideshow div
if($lesson->slideshow && $page->qtype == LESSON_BRANCHTABLE) {
echo "
bgcolor;
height: ".$lesson->height."px;
width: ".$lesson->width."px;
overflow: auto;
border: 0px solid #ccc;
padding-right: 16px; /* for the benefit of macIE5 only */
/* \ commented backslash hack - recover from macIE5 workarounds, it will ignore the following rule */
padding-right: 0;
padding: 15px;
\">\n";
echo "
\n";
} else {
echo "
\n";
$lesson->slideshow = false; // turn off slide show for all pages other than LESSON_BRANTCHTABLE
}
// 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
if(!isteacher($course->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
}
}
if (isset($_POST['startlastseen'])) {
if ($_POST['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();
} elseif ($_POST['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(isteacher($course->id)) {
echo '
'. get_string('teachertimerwarning', 'lesson') .'
';
} else {
if ((($timer->starttime + $lesson->maxtime * 60) - time()) > 0) {
// code for the clock
print_simple_box_start("right", "150px", "#ffffff", 0);
echo "
".
"
".get_string("timeremaining", "lesson").
"
";
echo "\n";
echo "
";
print_simple_box_end();
echo "
";
} else {
redirect("view.php?id=$cm->id&action=navigation&pageid=".LESSON_EOL."&outoftime=normal", get_string("outoftime", "lesson"));
}
// update clock when viewing a new page... no special treatment
if ((($timer->starttime + $lesson->maxtime * 60) - time()) < 60) {
echo "
".get_string('studentoneminwarning', 'lesson')."
";
}
if ($timedflag) {
print_simple_box(get_string('maxtimewarning', 'lesson', $lesson->maxtime), 'center');
}
}
}
// update the clock
if (!isteacher($course->id)) {
$timer->lessontime = time();
if (!update_record('lesson_timer', $timer)) {
error('Error: could not update lesson_timer table');
}
}
if ($attemptflag) {
print_heading(get_string('attempt', 'lesson', $retries + 1));
}
// before we output everything check to see if the page is a EOB, if so jump directly
// to it's associated branch table
if ($page->qtype == LESSON_ENDOFBRANCH) {
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
/// CDC-FLAG 6/21/04 ///
if ($answer->jumpto == LESSON_RANDOMBRANCH) {
$answer->jumpto = lesson_unseen_branch_jump($lesson->id, $USER->id);
} elseif ($answer->jumpto == LESSON_CLUSTERJUMP) {
if (!isteacher($course->id)) {
$answer->jumpto = lesson_cluster_jump($lesson->id, $USER->id, $pageid);
} else {
if ($page->nextpageid == 0) {
$answer->jumpto = LESSON_EOL;
} else {
$answer->jumpto = $page->nextpageid;
}
}
}
/// CDC-FLAG ///
redirect("view.php?id=$cm->id&action=navigation&pageid=$answer->jumpto",
get_string("endofbranch", "lesson"));
break;
}
print_footer($course);
exit();
} else {
error('Navigation: No answers on EOB');
}
}
/// This is the warning msg for teachers to inform them that cluster and unseen does not work while logged in as a teacher
if(isteacher($course->id)) {
if (execute_teacherwarning($lesson->id)) {
$warningvars->cluster = get_string('clusterjump', 'lesson');
$warningvars->unseen = get_string('unseenpageinbranch', 'lesson');
echo '
';
}
}
/// this calculates the ongoing score
if ($lesson->ongoing && !empty($pageid)) {
if (isteacher($course->id)) {
echo "
".get_string('teacherongoingwarning', 'lesson').'
';
} else {
$ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
if (isset($USER->modattempts[$lesson->id])) {
$ntries--;
}
lesson_calculate_ongoing_score($lesson, $USER->id, $ntries);
}
}
if ($page->qtype == LESSON_BRANCHTABLE) {
if ($lesson->minquestions and isstudent($course->id)) {
// 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);
$nviewed = count_records("lesson_attempts", "lessonid", $lesson->id, "userid",
$USER->id, "retry", $ntries);
if ($nviewed) {
echo "
".get_string("numberofpagesviewed", "lesson", $nviewed).
"; (".get_string("youshouldview", "lesson", $lesson->minquestions).") ";
// count the number of distinct correct pages
if ($correctpages = get_records_select("lesson_attempts", "lessonid = $lesson->id
AND userid = $USER->id AND retry = $ntries AND correct = 1")) {
foreach ($correctpages as $correctpage) {
$temp[$correctpage->pageid] = 1;
}
$ncorrect = count($temp);
} else {
$nccorrect = 0;
}
if ($nviewed < $lesson->minquestions) {
$nviewed = $lesson->minquestions;
}
echo get_string("numberofcorrectanswers", "lesson", $ncorrect)." \n";
$thegrade = intval(100 * $ncorrect / $nviewed);
echo get_string("yourcurrentgradeis", "lesson",
number_format($thegrade * $lesson->grade / 100, 1)).
" (".get_string("outof", "lesson", $lesson->grade).")
\n";
}
}
}
// now starting to print the page's contents
echo "
";
if ($lesson->slideshow) {
echo format_text($page->contents);
} else {
print_simple_box(format_text($page->contents), 'center');
}
echo " \n";
// 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")) {
echo "
';
print_simple_box_end();
echo "\n";
break;
case LESSON_MULTICHOICE :
$i = 0;
shuffle($answers);
foreach ($answers as $answer) {
echo "
";
if ($page->qoption) {
$checked = "";
if (isset($USER->modattempts[$lesson->id])) {
$answerids = explode(",", $attempt->useranswer);
if (in_array($answer->id, $answerids)) {
$checked = "checked=\"checked\"";
} else {
$checked = "";
}
}
// more than one answer allowed
echo "id}\" $checked />"; //CDC hidden label added.
} else {
if (isset($USER->modattempts[$lesson->id]) && $answer->id == $attempt->answerid) {
$checked = "checked=\"checked\"";
} else {
$checked = "";
}
// only one answer allowed
echo "id}\" $checked />"; //CDC hidden label added.
}
echo "
";
} else {
// next 3 foreach loops print out the links in correct order
foreach ($nextprevious as $jump) {
if ($jump->jumpto == LESSON_NEXTPAGE) {
echo "
";
print_simple_box_end();
echo "\n";
break;
}
echo "\n";
} else {
// a page without answers - find the next (logical) page
echo "\n";
}
echo "\n";
} else {
// end of lesson reached work out grade
/// CDC-FLAG ///
if ($lesson->timed && !isteacher($course->id)) {
if (isset($_GET["outoftime"])) {
if ($_GET["outoftime"] == "normal") {
print_simple_box(get_string("eolstudentoutoftime", "lesson"), "center");
}
}
}
if (isset($USER->lessonloggedin[$lesson->id])) {
unset($USER->lessonloggedin[$lesson->id]);
}
// Update the clock / get time information for this user
if (!isteacher($course->id)) {
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);
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 (isstudent($course->id)) {
if ($nviewed = count_records("lesson_attempts", "lessonid", $lesson->id, "userid",
$USER->id, "retry", $ntries)) {
/// CDC-FLAG /// 6/11/04
if (!$lesson->custom) {
$ncorrect = 0;
$temp = array();
if ($pagesanswered = get_records_select("lesson_attempts", "lessonid = $lesson->id AND
userid = $USER->id AND retry = $ntries order by timeseen")) {
foreach ($pagesanswered as $pageanswered) {
if (!array_key_exists($pageanswered->pageid, $temp)) {
$temp[$pageanswered->pageid] = array($pageanswered->correct, 1);
} else {
if ($temp[$pageanswered->pageid][1] < $lesson->maxattempts) {
$n = $temp[$pageanswered->pageid][1] + 1;
$temp[$pageanswered->pageid] = array($pageanswered->correct, $n);
}
}
}
foreach ($temp as $value => $key) {
if ($key[0] == 1) {
$ncorrect += 1;
}
}
}
$nviewed = count($temp); // this counts number of Questions the user viewed
echo "
\n";
} else {
$score = 0;
$essayquestions = 0;
$essayquestionpoints = 0;
if ($useranswers = get_records_select("lesson_attempts", "lessonid = $lesson->id AND
userid = $USER->id AND retry = $ntries", "timeseen")) {
// group each try with its page
foreach ($useranswers as $useranswer) {
$attemptset[$useranswer->pageid][] = $useranswer;
}
$pageids = array_keys($attemptset);
$pageids = implode(",", $pageids);
// get only the pages and their answers that the user answered
$answeredpages = get_records_select("lesson_pages", "lessonid = $lesson->id AND id IN($pageids)");
$pageanswers = get_records_select("lesson_answers", "lessonid = $lesson->id AND pageid IN($pageids)");
foreach ($attemptset as $attempts) {
if(count($attempts) > $lesson->maxattempts) { // if there are more tries than the max that is allowed, grab the last "legal" attempt
$attempt = $attempts[$lesson->maxattempts - 1];
} else {
// else, user attempted the question less than the max, so grab the last one
$attempt = end($attempts);
}
// if essay question, handle it, otherwise add to score
if ($answeredpages[$attempt->pageid]->qtype == LESSON_ESSAY) {
$essayinfo = unserialize($attempt->useranswer);
$score += $essayinfo->score;
$essayquestions++;
$essayquestionpoints += $pageanswers[$attempt->answerid]->score;
} else {
$score += $pageanswers[$attempt->answerid]->score;
}
}
$bestscores = array();
// find the highest possible score per page
foreach ($pageanswers as $pageanswer) {
if(isset($bestscores[$pageanswer->pageid])) {
if ($bestscores[$pageanswer->pageid] < $pageanswer->score) {
$bestscores[$pageanswer->pageid] = $pageanswer->score;
}
} else {
$bestscores[$pageanswer->pageid] = $pageanswer->score;
}
}
$bestscore = array_sum($bestscores);
}
$thegrade = intval(100 * $score / $bestscore);
$a = new stdClass;
if ($essayquestions > 0) {
$a->score = $score;
$a->tempmaxgrade = $bestscore - $essayquestionpoints;
$a->essayquestions = $essayquestions;
$a->grade = $bestscore;
echo "
\n";
}
/// CDC-FLAG ///
$grade->lessonid = $lesson->id;
$grade->userid = $USER->id;
$grade->grade = $thegrade;
$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 {
//print_string("noattemptrecordsfound", "lesson");
if ($lesson->timed) {
if (isset($_GET["outoftime"])) {
if ($_GET["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");
}
}
} 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);
}
///CDC-FLAG /// high scores code
if ($lesson->highscores && !isteacher($course->id)) {
echo "
";
if (!$grades = get_records_select("lesson_grades", "lessonid = $lesson->id", "completed")) {
echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)." ";
echo "id&action=nameforhighscores\">".get_string("clicktopost", "lesson")." ";
} else {
if (!$highscores = get_records_select("lesson_high_scores", "lessonid = $lesson->id")) {
echo get_string("youmadehighscore", "lesson", $lesson->maxhighsores)." ";
echo "id&action=nameforhighscores\">".get_string("clicktopost", "lesson")." ";
} else {
// get all the high scores into an array
foreach ($highscores as $highscore) {
$grade = $grades[$highscore->gradeid]->grade;
$topscores[] = $grade;
}
// sort to find the lowest score
sort($topscores);
$lowscore = $topscores[0];
if ($thegrade >= $lowscore || count($topscores) <= $lesson->maxhighscores) {
echo get_string("youmadehighscore", "lesson", $lesson->maxhighscores)." ";
echo "id&action=nameforhighscores\">".get_string("clicktopost", "lesson")." ";
} else {
echo get_string("nothighscore", "lesson", $lesson->maxhighscores)." ";
}
}
}
echo " id&action=highscores&link=1\">".get_string("viewhighscores", "lesson")."";
echo "
";
}
/// CDC-FLAG ///
if ($lesson->modattempts && !isteacher($course->id)) {
// 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
$attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $USER->id AND retry = $ntries", "timeseen");
$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 "";
echo "
\n"; //CDC view grades
}
if ($lesson->displayleft || $lesson->slideshow) { // this ends the table cell and table for the leftmenu or for slideshow
echo "";
}
}
/*******************teacher view **************************************/
elseif ($action == 'teacherview') {
print_heading_with_help($lesson->name, "overview", "lesson");
// get number of pages
if ($page = get_record_select("lesson_pages", "lessonid = $lesson->id AND prevpageid = 0")) {
$npages = 1;
while (true) {
if ($page->nextpageid) {
if (!$page = get_record("lesson_pages", "id", $page->nextpageid)) {
error("Teacher view: Next page not found!");
}
} else {
// last page reached
break;
}
$npages++;
}
}
if (!$page = get_record_select("lesson_pages", "lessonid = $lesson->id AND prevpageid = 0")) {
/// CDC-FLAG ///
// if there are no pages give teacher the option to create a new page or a new branch table
echo "
";
if (isteacheredit($course->id)) {
print_simple_box( "