]> git.mjollnir.org Git - moodle.git/commitdiff
Added sanity check before calculating grade.
authorrkingdon <rkingdon>
Tue, 17 Feb 2004 11:11:00 +0000 (11:11 +0000)
committerrkingdon <rkingdon>
Tue, 17 Feb 2004 11:11:00 +0000 (11:11 +0000)
mod/lesson/lesson.php
mod/lesson/view.php

index 96ef5a8f273d7b13579b9144f01683e36cd16ae2..0af5be31778a81aba5f53070ea6189c30ac92616 100644 (file)
@@ -5,6 +5,7 @@
 
        addpage
     confirmdelete
+    continue
        delete
        editpage
     insertpage
index 964de2c72c22bdc14065bd26fa36cdea2b75612c..fc57d81bc335e267b370ae7d0f33a469b1dd16a6 100644 (file)
             print_simple_box_start("center");
             $ntries = count_records("lesson_grades", "lessonid", $lesson->id, "userid", $USER->id);
             if (isstudent($course->id)) {
-                $ncorrect = count_records_select("lesson_attempts", "lessonid = $lesson->id AND
-                        userid = $USER->id AND retry = $ntries AND correct = 1");
-                $nviewed = count_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id,
-                        "retry", $ntries);
-                if ($nviewed) {
-                    $thegrade = intval(100 * $ncorrect / $nviewed);
+                // do a sanity check on the user's path through the lesson
+                if ($attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND
+                        userid = $USER->id AND retry = $ntries", "timeseen ASC")) {
+                    $check = true;
+                    if (!$thispageid = get_field("lesson_pages", "id", "lessonid", $lesson->id,
+                                "prevpageid", 0)) {
+                        error("Navigation Check: first page not found");
+                    }
+                    foreach ($attempts as $attempt) {
+                        // skip any page without answers
+                        while (!$answers = get_records("lesson_answers", "pageid", $thispageid)) {
+                            if (!$thispageid = get_field("lesson_pages", "nextpageid", "id", $thispageid)) {
+                                error("Navigation Check: nextpageid not found");
+                            }
+                        }
+                        if ($attempt->pageid != $thispageid) {
+                            // something odd
+                            // echo "<p>\$thispageid: $thispageid; \$attempt->pageid: $attempt->pageid</p>\n";
+                            $check = false;
+                            break;
+                        }
+                        if (!$answer = get_record("lesson_answers", "id", $attempt->answerid)) {
+                            error("Navigation Check: answer not found");
+                        }
+                        if ($answer->jumpto) {
+                            if ($answer->jumpto == NEXTPAGE) {
+                                if (!$thispageid = get_field("lesson_pages", "nextpageid", "id", 
+                                            $thispageid)) {
+                                    $thispageid = EOL; // end of foreach loop should have been reached
+                                }
+                            } else {
+                                $thispageid = $answer->jumpto;
+                            }
+                        }
+                    }
+                    if ($check) {
+                        $ncorrect = count_records_select("lesson_attempts", "lessonid = $lesson->id AND
+                                userid = $USER->id AND retry = $ntries AND correct = 1");
+                        $nviewed = count_records("lesson_attempts", "lessonid", $lesson->id, "userid", 
+                                $USER->id, "retry", $ntries);
+                        if ($nviewed) {
+                            $thegrade = intval(100 * $ncorrect / $nviewed);
+                        } else {
+                            $thegrade = 0;
+                        }
+                        echo "<p align=\"center\">".get_string("numberofpagesviewed", "lesson", $nviewed).
+                            "</p>\n";
+                        echo "<p align=\"center\">".get_string("numberofcorrectanswers", "lesson", $ncorrect).
+                            "</p>\n";
+                        echo "<p align=\"center\">".get_string("gradeis", "lesson", 
+                                number_format($thegrade * $lesson->grade / 100, 1)).
+                            " (".get_string("outof", "lesson", $lesson->grade).")</p>\n";
+                        $grade->lessonid = $lesson->id;
+                        $grade->userid = $USER->id;
+                        $grade->grade = $thegrade;
+                        $grade->completed = time();
+                        if (!$newgradeid = insert_record("lesson_grades", $grade)) {
+                            error("Navigation: grade not inserted");
+                        }
+                    } else {
+                        print_string("sanitycheckfailed", "lesson");
+                        delete_records("lesson_attempts", "lessonid", $lesson->id, "userid", $USER->id,
+                                "retry", $ntries);
+                    }
                 } else {
-                    $thegrade = 0;
-                }
-                echo "<p align=\"center\">".get_string("numberofpagesviewed", "lesson", $nviewed)."</p>\n";
-                echo "<p align=\"center\">".get_string("numberofcorrectanswers", "lesson", $ncorrect).
-                    "</p>\n";
-                echo "<p align=\"center\">".get_string("gradeis", "lesson", 
-                        number_format($thegrade * $lesson->grade / 100, 1)).
-                        " (".get_string("outof", "lesson", $lesson->grade).")</p>\n";
-                $grade->lessonid = $lesson->id;
-                $grade->userid = $USER->id;
-                $grade->grade = $thegrade;
-                $grade->completed = time();
-                if (!$newgradeid = insert_record("lesson_grades", $grade)) {
-                    error("Navigation: grade not inserted");
-                }
+                    print_string("noattemptrecordsfound", "lesson");
+                }   
             } else { 
                 // display for teacher
                 echo "<p align=\"center\">".get_string("displayofgrade", "lesson")."</p>\n";
         } else {
             // print the pages
             echo "<center><table cellpadding=\"5\" border=\"0\" width=\"80%\">\n";
-            if (isteacheredit($course>id)) {
+            if (isteacheredit($course->id)) {
                 echo "<tr><td align=\"right\"><a href=\"lesson.php?id=$cm->id&action=addpage&pageid=0\"><small>".
                     get_string("addpagehere", "lesson")."</small></a></td></tr>\n";
             }