]> git.mjollnir.org Git - moodle.git/commitdiff
Checking in what I got so far .... Not working yet but I'm going to bed.
authormoodler <moodler>
Mon, 14 Oct 2002 15:57:33 +0000 (15:57 +0000)
committermoodler <moodler>
Mon, 14 Oct 2002 15:57:33 +0000 (15:57 +0000)
mod/quiz/attempt.php
mod/quiz/edit.php
mod/quiz/lib.php
mod/quiz/mod.html
mod/quiz/multichoice.html [new file with mode: 0644]
mod/quiz/question.php
mod/quiz/shortanswer.html [new file with mode: 0644]
mod/quiz/truefalse.html [new file with mode: 0644]
mod/quiz/view.php

index da1f7e6244e4b12a764e926537a63a1844d4c67f..5c4b438ef2cd50433dbe4698c00080e40ff667c4 100644 (file)
         $numattempts = 1;
     }
 
-    if ($numattempts > $quiz->attempts) {
-        error("Sorry, you've had $quiz->attempts attempts already.", "view.php?id=$cm->id");
+    if ($quiz->attempts) {
+        if ($numattempts > $quiz->attempts) {
+            error("Sorry, you've had $quiz->attempts attempts already.", "view.php?id=$cm->id");
+        }
     }
 
     if ($course->format == "weeks" and $quiz->days) {
             error("Sorry! Could not calculate your best grade!");
         }
 
-        print_heading(get_string("grade", "quiz").": $result->grade/$quiz->grade  ($result->sumgrades / $quiz->sumgrades = $percent %)");
+        print_heading(get_string("grade", "quiz").": $result->grade/$quiz->grade  ($result->sumgrades / $quiz->sumgrades = $result->percentage %)");
 
         print_continue("view.php?id=$cm->id");
 
         error("Sorry, this quiz is not available", "view.php?id=$cm->id");
     }
 
-    print_heading("Attempt $numattempts out of $quiz->attempts");
+    print_heading(get_string("attempt", "quiz", $numattempts));
 
     print_simple_box($quiz->intro, "CENTER");
 
 
     echo "<BR>";
 
-    quiz_print_quiz_questions($quiz);
+    if (! quiz_print_quiz_questions($quiz)) {
+        print_continue("view.php?id=$cm->id");
+    }
 
 
 /// Finish the page
index 8cc5c179de3c5695e097fadb5081cc04c2fbc4e1..3219641a941c6f752af568684c67462d6b73c4c2 100644 (file)
@@ -12,7 +12,6 @@
             error(get_string("filloutallfields"), $HTTP_REFERER);
         }
 
-
         $SESSION->modform = $modform;    // Save the form in the current session
         save_session("SESSION");
 
@@ -42,7 +41,7 @@
 
     // Now, check for commands on this page and modify variables as necessary
 
-    if ($up) { //------------------------------------------------------------
+    if ($up) { /// Move the given question up a slot
         $questions = explode(",", $modform->questions);
         if ($questions[0] <> $up) {
             foreach ($questions as $key => $question) {
@@ -57,7 +56,7 @@
         }
     }
 
-    if ($down) { //----------------------------------------------------------
+    if ($down) { /// Move the given question down a slot
         $questions = explode(",", $modform->questions);
         if ($questions[count($questions)-1] <> $down) {
             foreach ($questions as $key => $question) {
         }
     }
 
-    if ($add) { //-----------------------------------------------------------
+    if ($add) { /// Add a question to the current quiz
         $rawquestions = $HTTP_POST_VARS;
-        $questions = explode(",", $modform->questions);
+        if ($modform->questions) {
+            $questions = explode(",", $modform->questions);
+        }
         foreach ($rawquestions as $key => $value) {    // Parse input for question ids
             if (substr($key, 0, 1) == "q") {
                 $key = substr($key,1);
-                foreach ($questions as $question) {
-                    if ($question == $key) {
-                        continue 2;
+                if ($questions) {
+                    foreach ($questions as $question) {
+                        if ($question == $key) {
+                            continue 2;
+                        }
                     }
                 }
                 $questions[] = $key;
-                $modform->grades[$key] = 1;
+                $modform->grades[$key] = 1;   // default score
             }
         }
         $modform->questions = implode(",", $questions);
     }
 
-    if ($delete) { //--------------------------------------------------------
+    if ($delete) { /// Delete a question from the list 
         $questions = explode(",", $modform->questions);
         foreach ($questions as $key => $question) {
             if ($question == $delete) {
         $modform->questions = implode(",", $questions);
     }
 
-    if ($grade) { //---------------------------------------------------------
+    if ($grade) { /// The grades have been updated, so update our internal list
         $rawgrades = $HTTP_POST_VARS;
         foreach ($rawgrades as $key => $value) {    // Parse input for question -> grades
             if (substr($key, 0, 1) == "q") {
         }
     }
 
+    $modform->sumgrades = 0;
+    if ($modform->grades) {
+        foreach ($modform->grades as $grade) {
+            $modform->sumgrades += $grade;
+        }
+    }
+
     $SESSION->modform = $modform;
     save_session("SESSION");
 
     // Print basic page layout.
 
     echo "<TABLE BORDER=0 WIDTH=\"100%\" CELLPADDING=2 CELLSPACING=0>";
-    echo "<TR><TD WIDTH=50%>";
+    echo "<TR><TD WIDTH=50% VALIGN=TOP>";
         print_simple_box_start("CENTER", "100%", $THEME->body);
         print_heading($modform->name);
         quiz_print_question_list($modform->questions, $modform->grades); 
index 0523ef5aff5678e884e4d2df9dc83427ca8cb144..7643bba22d9d659e99ede07061d58724a52bff6f 100644 (file)
@@ -36,7 +36,7 @@ function quiz_add_instance($quiz) {
         return false;  // some error occurred
     }
 
-    // The grades for each question in this quiz are stored in an array
+    // The grades for every question in this quiz are stored in an array
     if ($quiz->grades) {
         foreach ($quiz->grades as $question => $grade) {
             $questiongrade->quiz = $quiz->id;
@@ -65,7 +65,7 @@ function quiz_update_instance($quiz) {
     }
 
 
-    // The grades for each question in this quiz are stored in an array
+    // The grades for every question in this quiz are stored in an array
     // Insert or update records as appropriate
 
     $existing = get_records("quiz_question_grades", "quiz", $quiz->id, "", "question,grade,id");
@@ -105,7 +105,25 @@ function quiz_delete_instance($id) {
 
     $result = true;
 
-    # Delete any dependent records here #
+    if ($attempts = get_record("quiz_attempts", "quiz", "$quiz->id")) {
+        foreach ($attempts as $attempt) {
+            if (! delete_records("quiz_responses", "attempt", "$attempt->id")) {
+                $result = false;
+            }
+        }
+    }
+
+    if (! delete_records("quiz_attempts", "quiz", "$quiz->id")) {
+        $result = false;
+    }
+
+    if (! delete_records("quiz_grades", "quiz", "$quiz->id")) {
+        $result = false;
+    }
+
+    if (! delete_records("quiz_question_grades", "quiz", "$quiz->id")) {
+        $result = false;
+    }
 
     if (! delete_records("quiz", "id", "$quiz->id")) {
         $result = false;
@@ -253,13 +271,15 @@ function quiz_print_quiz_questions($quiz, $results=NULL) {
 // Prints a whole quiz on one page.
 
     if (!$quiz->questions) {
-        error("No questions have been defined!", "view.php?id=$cm->id");
+        notify("No questions have been defined!", "view.php?id=$cm->id");
+        return false;
     }
 
     $questions = explode(",", $quiz->questions);
 
     if (!$grades = get_records_list("quiz_question_grades", "question", $quiz->questions, "", "question,grade")) {
-        error("No grades were found for these questions!");
+        notify("No grades were found for these questions!");
+        return false;
     }
 
     echo "<FORM METHOD=POST ACTION=attempt.php>";
@@ -272,6 +292,8 @@ function quiz_print_quiz_questions($quiz, $results=NULL) {
     }
     echo "<CENTER><INPUT TYPE=submit VALUE=\"".get_string("savemyanswers", "quiz")."\"></CENTER>";
     echo "</FORM>";
+
+    return true;
 }
  
 function quiz_get_default_category($courseid) {
@@ -282,8 +304,8 @@ function quiz_get_default_category($courseid) {
     }
 
     // Otherwise, we need to make one
-    $category->name = get_string("miscellaneous", "quiz");
-    $category->info = get_string("miscellaneous", "quiz");
+    $category->name = get_string("default", "quiz");
+    $category->info = get_string("defaultinfo", "quiz");
     $category->course = $courseid;
     $category->publish = 0;
 
@@ -308,7 +330,7 @@ function quiz_print_category_form($course, $current) {
     }
     $strcategory = get_string("category", "quiz");
     $strshow = get_string("show", "quiz");
-    $strrename = get_string("rename", "quiz");
+    $stredit = get_string("edit");
     $strdelete = get_string("delete");
     $strnew = get_string("new");
 
@@ -316,8 +338,8 @@ function quiz_print_category_form($course, $current) {
     echo "<B>$strcategory:</B>&nbsp;";
     choose_from_menu($categories, "cat", "$current");
     echo "<INPUT TYPE=submit NAME=catshow VALUE=\"$strshow\">";
-    echo "<INPUT TYPE=submit NAME=catrename VALUE=\"$strrename\">";
-    echo "<INPUT TYPE=submit NAME=catdelete VALUE=\"$strdelete\">";
+    echo "<INPUT TYPE=submit NAME=catedit VALUE=\"$stredit\">";
+    echo "<INPUT TYPE=submit NAME=catdelete VALUE=\"$strdelete\">&nbsp;";
     echo "<INPUT TYPE=submit NAME=catnew VALUE=\"$strnew\">";
     echo "</FORM>";
 }
@@ -374,7 +396,7 @@ function quiz_print_question_list($questionlist, $grades) {
     $strmovedown = get_string("movedown");
     $strsavegrades = get_string("savegrades", "quiz");
 
-    for ($i=100; $i>=0; $i--) {
+    for ($i=10; $i>=0; $i--) {
         $gradesmenu[$i] = $i;
     }
     $count = 0;
@@ -382,7 +404,7 @@ function quiz_print_question_list($questionlist, $grades) {
     $total = count($order);
     echo "<FORM METHOD=post ACTION=edit.php>";
     echo "<TABLE BORDER=0 CELLPADDING=5 CELLSPACING=2 WIDTH=\"100%\">";
-    echo "<TR><TH COLSPAN=3>$strorder</TH><TH align=left>$strquestionname</TH><TH>$strgrade</TH><TH>$stredit</TH></TR>";
+    echo "<TR><TH WIDTH=10 COLSPAN=3>$strorder</TH><TH align=left WIDTH=\"100%\">$strquestionname</TH><TH WIDTH=10>$strgrade</TH><TH WIDTH=10>$stredit</TH></TR>";
     foreach ($order as $qnum) {
         $count++;
         echo "<TR BGCOLOR=\"$THEME->cellcontent\">";
@@ -419,6 +441,8 @@ function quiz_print_question_list($questionlist, $grades) {
     echo "</TD><TD></TD></TR>";
     echo "</TABLE>";
     echo "</FORM>";
+
+    return $sumgrade;
 }
 
 
@@ -427,6 +451,7 @@ function quiz_print_cat_question_list($categoryid) {
 
     global $THEME, $QUIZ_QUESTION_TYPE;
 
+    $strcategory = get_string("category", "quiz");
     $strquestion = get_string("question", "quiz");
     $strnoquestions = get_string("noquestions", "quiz");
     $strselect = get_string("select", "quiz");
@@ -447,9 +472,10 @@ function quiz_print_cat_question_list($categoryid) {
         notify("Category not found!");
         return;
     }
-    echo "<P>$category->info</P>";
+    echo "<P><B>$strcategory:</B> $category->name</P>\n";
+    echo text_to_html($category->info);
 
-    echo "<FORM METHOD=POST ACTION=question.php>"; 
+    echo "<FORM METHOD=GET ACTION=question.php>"; 
     echo "<B>$strquestion:</B>&nbsp;";
     choose_from_menu($QUIZ_QUESTION_TYPE, "type", "", "");
     echo "<INPUT TYPE=hidden NAME=category VALUE=\"$category->id\">";
@@ -463,21 +489,30 @@ function quiz_print_cat_question_list($categoryid) {
         return;
     }
 
+    $canedit = isteacher($category->course);
+
     echo "<FORM METHOD=post ACTION=edit.php>";
     echo "<TABLE BORDER=0 CELLPADDING=5 CELLSPACING=2 WIDTH=\"100%\">";
-    echo "<TR><TH width=10>$strselect</TH><TH width=* align=left>$strquestionname</TH><TH width=10>$stredit</TH></TR>";
+    echo "<TR><TH width=10>$strselect</TH><TH width=* align=left>$strquestionname</TH>";
+    if ($canedit) {
+        echo "<TH width=10>$stredit</TH>";
+    }
+    echo "</TR>";
     foreach ($questions as $question) {
         echo "<TR BGCOLOR=\"$THEME->cellcontent\">";
         echo "<TD ALIGN=CENTER>";
         echo "<INPUT TYPE=CHECKBOX NAME=q$question->id VALUE=\"1\">";
         echo "</TD>";
         echo "<TD>".$question->name."</TD>";
-        echo "<TD>";
-            echo "<A TITLE=\"$strdelete\" HREF=\"question.php?delete=$question->id\"><IMG 
-                 SRC=\"../../pix/t/delete.gif\" BORDER=0></A>&nbsp;";
-            echo "<A TITLE=\"$stredit\" HREF=\"question.php?id=$question->id\"><IMG 
-                 SRC=\"../../pix/t/edit.gif\" BORDER=0></A>";
-        echo "</TD></TR>";
+        if ($canedit) {
+            echo "<TD>";
+                echo "<A TITLE=\"$strdelete\" HREF=\"question.php?delete=$question->id\"><IMG 
+                     SRC=\"../../pix/t/delete.gif\" BORDER=0></A>&nbsp;";
+                echo "<A TITLE=\"$stredit\" HREF=\"question.php?id=$question->id\"><IMG 
+                     SRC=\"../../pix/t/edit.gif\" BORDER=0></A>";
+            echo "</TD></TR>";
+        }
+        echo "</TR>";
     }
     echo "<TR><TD COLSPAN=3>";
     echo "<INPUT TYPE=hidden NAME=add VALUE=\"1\">";
@@ -733,6 +768,7 @@ function quiz_grade_attempt_results($quiz, $questions) {
         if ($grade < 0.0) {   // No negative grades
             $grade = 0.0;
         }
+
         $result->grades[$question->id] = $grade;
         $result->sumgrades += $grade;
         $result->feedback[$question->id] = $feedback;
index 3323b3c9534500393fc756a0c8f443577a55ae85..2a6f29cf7080742321f516350e840759c065b129 100644 (file)
@@ -3,8 +3,6 @@
 
 <? include_once("$CFG->dirroot/mod/quiz/lib.php") ?>
 
-<H1 ALIGN=CENTER><FONT COLOR=RED>This module is not ready for use</FONT></H1>
-
 <FORM name="form" method="post" action="<?=$CFG->wwwroot?>/mod/quiz/edit.php">
 <CENTER>
 <TABLE cellpadding=5>
@@ -95,7 +93,7 @@
     <TD align=right><P><B><? print_string("maximumgrade") ?>:</B></P></TD>
     <TD>
     <?
-        for ($i=100; $i>=0; $i--) {
+        for ($i=100; $i>=1; $i--) {
             $grades[$i] = $i;
         }
         choose_from_menu($grades, "grade", "$form->grade", "");
diff --git a/mod/quiz/multichoice.html b/mod/quiz/multichoice.html
new file mode 100644 (file)
index 0000000..0f0f0fa
--- /dev/null
@@ -0,0 +1,46 @@
+<FORM name="theform" method="post" action="question.php">\r
+<CENTER>\r
+<TABLE cellpadding=5>\r
+<TR valign=top>\r
+    <TD align=right><P><B><? print_string("questionname", "quiz") ?>:</B></P></TD>\r
+    <TD>\r
+        <INPUT type="text" name="name" size=40 value="<? p($question->name) ?>">\r
+    </TD>\r
+</TR>\r
+<TR valign=top>\r
+    <TD align=right><P><B><? print_string("question", "quiz") ?>:</B></P></TD>\r
+    <TD>\r
+        <textarea name="intro" rows=5 cols=50 wrap="virtual"><? p($question->question) ?></textarea>\r
+        <? helpbutton("text", get_string("helptext")); ?>\r
+    </TD>\r
+</TR>\r
+<TR valign=top>\r
+    <TD align=right><P><B><? print_string("category", "quiz") ?>:</B></P></TD>\r
+    <TD>\r
+    <?\r
+        choose_from_menu($categories, "category", "$question->category", "");\r
+     ?>\r
+    </TD>\r
+</TR>\r
+<TR valign=top>\r
+    <TD align=right><P><B><? print_string("attemptsallowed", "quiz") ?>:</B></P></TD>\r
+    <TD>\r
+    <?\r
+        $options[0] = get_string("answersingleno", "quiz");\r
+        $options[1] = get_string("answersingleyes", "quiz");\r
+        choose_from_menu($options, "single", "$options->single", "");\r
+     ?>\r
+    </TD>\r
+</TR>\r
+</TABLE>\r
+<INPUT type="hidden" name=id value="<? p($question->id) ?>">\r
+<INPUT type="hidden" name=type value="<? p($question->type) ?>">\r
+<INPUT type="hidden" name=category value="<? p($question->category) ?>">\r
+<INPUT type="submit" value="<? print_string("savechanges") ?>">\r
+\r
+\r
+\r
+answers go here ...\r
+\r
+</CENTER>\r
+</FORM>\r
index 3e100f7ae3f476a9a5b67cd37f469c9046527f0e..76eb5cd1ba59b9b0e16ac4d1c7ca498a8762a438 100644 (file)
@@ -1,3 +1,122 @@
 <?PHP // $Id$
+      /// For creating and editing quiz questions.
 
+    require("../../config.php");
+    require("lib.php");
+
+    optional_variable($id);
+
+    optional_variable($type);
+    optional_variable($category);
+
+    if ($id) {
+        if (! $question = get_record("quiz_questions", "id", $id)) {
+            error("This question doesn't exist");
+        }
+
+        if (! $category = get_record("quiz_categories", "id", $question->category)) {
+            error("This question doesn't belong to a vald category!");
+        }
+        if (! $course = get_record("course", "id", $category->course)) {
+            error("This question category doesn't belong to a valid course!");
+        }
+
+        $type = $question->type;
+
+        switch ($type) {
+            case SHORTANSWER:
+                print_heading(get_string("editingshortanswer", "quiz"));
+                require("shortanswer.html");
+            break;
+            case TRUEFALSE:
+                print_heading(get_string("editingtruefalse", "quiz"));
+                require("truefalse.html");
+            break;
+            case MULTICHOICE:
+                print_heading(get_string("editingmultichoice", "quiz"));
+                require("multichoice.html");
+            break;
+        }
+
+    } else if ($category) {
+        if (! $category = get_record("quiz_categories", "id", $category)) {
+            error("This wasn't a valid category!");
+        }
+        if (! $course = get_record("course", "id", $category->course)) {
+            error("This category doesn't belong to a valid course!");
+        }
+
+        $question->category = $category->id;
+        $question->type     = $type;
+
+    } else {
+        error("Must specify question id or category");
+    }
+
+    require_login($course->id);
+
+    if (!isteacher($course->id)) {
+        error("You can't modify this course!");
+    }
+
+    $streditingquiz = get_string("editingquiz", "quiz");
+    $streditingquestion = get_string("editingquestion", "quiz");
+
+    print_header("$course->shortname: $streditingquestion", "$course->shortname: $streditingquestion",
+                 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> 
+                   -> <A HREF=\"$HTTP_REFERER\">$streditingquiz</A> -> $streditingquestion");
+
+    if (match_referer() and isset($HTTP_POST_VARS)) {    // question submitted
+
+    } 
+
+    $grades = array(100,90,80,75,70,66.66,60,50,40,33.33,30,25,20,10,5);
+    foreach ($grades as $grade) {
+        $gradeoptions[$grade] = $grade;
+        $gradeoptions[-$grade] = -$grade;
+    }
+    arsort($gradeoptions, SORT_NUMERIC);
+
+    if (!$categories = get_records_sql_menu("SELECT id,name FROM quiz_categories 
+                                             WHERE course='$course->id' OR publish = '1'
+                                             ORDER by name ASC")) {
+        error("No categories!");
+    }
+
+    // Print the question editing form
+
+    switch ($type) {
+        case SHORTANSWER:
+            $options = get_record("quiz_shortanswer", "question", "$question->id");// OK to fail
+            $answer  = get_record("quiz_answers", "id", "$options->answer");       // OK to fail
+            print_heading(get_string("editingshortanswer", "quiz"));
+            require("shortanswer.html");
+        break;
+
+        case TRUEFALSE:
+            $options = get_record("quiz_truefalse", "question", "$question->id");  // OK to fail
+            $true    = get_record("quiz_answers", "id", "$options->true");         // OK to fail
+            $false   = get_record("quiz_answers", "id", "$options->false");        // OK to fail
+            print_heading(get_string("editingtruefalse", "quiz"));
+            require("truefalse.html");
+        break;
+
+        case MULTICHOICE:
+            $options = get_record("quiz_multichoice", "question", "$question->id");// OK to fail
+            $answersraw = get_records_list("quiz_answers", "id", "$options->answers");// OK to fail
+            if ($answersraw) {
+                foreach ($answersraw as $answer) {
+                    $answers[] = $answer;   // to renumber index 0,1,2...
+                }
+            }
+            print_heading(get_string("editingmultichoice", "quiz"));
+            require("multichoice.html");
+        break;
+
+        default:
+            error("Invalid question type");
+        break;
+    }
+
+    print_footer($course);
 ?>
diff --git a/mod/quiz/shortanswer.html b/mod/quiz/shortanswer.html
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/mod/quiz/truefalse.html b/mod/quiz/truefalse.html
new file mode 100644 (file)
index 0000000..e69de29
index 2a2d362b6a8ed8532024c66c4658a8dbfe266253..c7e0c2b0f37d78f9e999099142245bf9631686e9 100644 (file)
 
     $mygrade = quiz_get_best_grade($quiz->id, $USER->id);
 
-    if ($numattempts < $quiz->attempts) { 
+    if ($numattempts < $quiz->attempts or !$quiz->attempts) { 
         if ($available) {
-            $options["id"] = $quiz->id;
+            $options["id"] = $cm->id;
             if ($numattempts) {
                 print_heading("Your best grade so far is $mygrade / $quiz->grade.");
             }
+            echo "<BR>";
             echo "<DIV align=CENTER>";
             print_single_button("attempt.php", $options, $label="Attempt quiz now");
             echo "</P>";