]> git.mjollnir.org Git - moodle.git/commitdiff
Category editing in quizzes is now supported. Some other little
authormoodler <moodler>
Sun, 20 Oct 2002 17:15:39 +0000 (17:15 +0000)
committermoodler <moodler>
Sun, 20 Oct 2002 17:15:39 +0000 (17:15 +0000)
cleanups along the way ...

lang/en/quiz.php
lib/moodlelib.php
mod/quiz/category.php
mod/quiz/lib.php
mod/quiz/multichoice.html
mod/quiz/question.php
mod/quiz/shortanswer.html
mod/quiz/truefalse.html

index 7d3abf86b350d4c1c6642eabc3b0366d5ea3cc8c..5acb098e58a8f31503acf293bfa512e3e91bdb34 100644 (file)
@@ -7,7 +7,7 @@ $string['modulenameplural'] = "Quizzes";
 
 $string['addselectedtoquiz'] = "Add selected to quiz";
 $string['alwaysavailable'] = "Always available";
-$string['alreadysubmitted'] = "Perhaps you already submitted this attempt";
+$string['alreadysubmitted'] = "It is likely that you have already submitted this attempt";
 $string['answer'] = "Answer";
 $string['answerhowmany'] = "One or multiple answers?";
 $string['answersingleyes'] = "One answer only";
@@ -18,12 +18,16 @@ $string['attemptlast'] = "Last attempt";
 $string['attempts'] = "Attempts";
 $string['attemptsallowed'] = "Attempts allowed";
 $string['attemptsunlimited'] = "Unlimited attempts";
+$string['backtoquiz'] = "Back to quiz editing";
 $string['bestgrade'] = "Best grade";
-$string['categories'] = "Categories";
-$string['category'] = "Category";
 $string['casesensitive'] = "Case sensitivity";
 $string['caseyes'] = "Yes, case must match";
 $string['caseno'] = "No, case is unimportant";
+$string['categories'] = "Categories";
+$string['category'] = "Category";
+$string['categoryinfo'] = "Category info";
+$string['categorymove'] = "The category '\$a->name' contains \$a->count questions.  Please choose another category to move them to.";
+$string['categorymoveto'] = "Move them to this category";
 $string['choice'] = "Choice";
 $string['choices'] = "Available choices";
 $string['correctanswer'] = "Correct answer";
@@ -53,7 +57,9 @@ $string['multichoice'] = "Multiple Choice";
 $string['noanswers'] = "No answers were selected!";
 $string['nomoreattempts'] = "No more attempts are allowed";
 $string['noquestions'] = "No questions have been added yet";
+$string['publish'] = "Publish";
 $string['question'] = "Question";
+$string['questions'] = "Questions";
 $string['questionname'] = "Question name";
 $string['quizavailable'] = "The quiz is available until: \$a";
 $string['quizclose'] = "Close the quiz";
@@ -79,5 +85,6 @@ $string['timetaken'] = "Time taken";
 $string['timecompleted'] = "Completed";
 $string['true'] = "True";
 $string['truefalse'] = "True/False";
+$string['type'] = "Type";
 $string['viewallanswers'] = "View \$a completed quizzes";
 $string['yourfinalgradeis'] = "Your final grade for this quiz is \$a";
index e32c493fddb9f2b261635178d536e72ab7974767..ac7dcc33a235de0a086611efe0194710c864b9a9 100644 (file)
@@ -681,7 +681,7 @@ function optional_variable(&$var, $default=0) {
 
 /// DATABASE HANDLING ////////////////////////////////////////////////
 
-function execute_sql($command) {
+function execute_sql($command, $feedback=true) {
 // Completely general
 
     global $db;
@@ -689,10 +689,14 @@ function execute_sql($command) {
     $result = $db->Execute("$command");
 
     if ($result) {
-        echo "<P><FONT COLOR=green><B>".get_string("success")."</B></FONT></P>";
+        if ($feedback) {
+            echo "<P><FONT COLOR=green><B>".get_string("success")."</B></FONT></P>";
+        }
         return true;
     } else {
-        echo "<P><FONT COLOR=red><B>".get_string("error")."</B></FONT></P>";
+        if ($feedback) {
+            echo "<P><FONT COLOR=red><B>".get_string("error")."</B></FONT></P>";
+        }
         return false;
     }
 }
index b3ce80f42d32366c156648b1e57d5ea5d6289277..4de75c88b1091d329cda1311d81d631e339dfa4e 100644 (file)
@@ -1,4 +1,175 @@
 <?PHP // $Id$
+      // Allows a teacher to create, edit and delete categories
+
+       require("../../config.php");
+       require("lib.php");
+
+    require_variable($id);   // course
+
+    if (! $course = get_record("course", "id", $id)) {
+        error("Course ID is incorrect");
+    }
+
+    if (isset($backtoquiz)) {
+        redirect("edit.php");
+    }
+
+    require_login($course->id);
+
+    if (!isteacher($course->id)) {
+        error("Only teachers can use this page!");
+    }
+
+
+/// Print headings
+
+    $strcategory = get_string("category", "quiz");
+    $strcategoryinfo = get_string("categoryinfo", "quiz");
+    $strquestions = get_string("questions", "quiz");
+    $strpublish = get_string("publish", "quiz");
+    $strdelete = get_string("delete");
+    $straction = get_string("action");
+    $stradd = get_string("add");
+    $strcancel = get_string("cancel");
+    $strsavechanges = get_string("savechanges");
+    $strbacktoquiz = get_string("backtoquiz", "quiz");
+
+    $streditingquiz = get_string("editingquiz", "quiz");
+    $streditcategories = get_string("editcategories", "quiz");
+
+    print_header("$course->shortname: $streditcategories", "$course->shortname: $streditcategories",
+                 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> 
+                   -> <A HREF=\"edit.php\">$streditingquiz</A> -> $streditcategories");
+
+
+/// Delete category if the user wants to delete it
+
+    if (isset($delete) and !isset($cancel)) {
+        if (!$category = get_record("quiz_categories", "id", $delete)) {  // security
+            error("No such category $delete!");
+        }
+
+        if (isset($confirm)) { // Need to move some questions before deleting the category
+            if (!$category2 = get_record("quiz_categories", "id", $confirm)) {  // security
+                error("No such category $confirm!");
+            }
+            if (! execute_sql("UPDATE quiz_questions SET category = '$category2->id' WHERE category = '$category->id'", false)) {
+                error("Error while moving questions from category '$category->name' to '$category2->name'");
+            }
+
+        } else {
+            if ($count = count_records("quiz_questions", "category", $category->id)) {
+                $vars->name = $category->name;
+                $vars->count = $count;
+                print_simple_box(get_string("categorymove", "quiz", $vars), "CENTER");
+                $categories = quiz_get_category_menu($course->id);
+                unset($categories[$category->id]);
+                echo "<CENTER><P><FORM ACTION=category.php METHOD=get>";
+                echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
+                echo "<INPUT TYPE=hidden NAME=delete VALUE=\"$category->id\">";
+                choose_from_menu($categories, "confirm", "", "");
+                echo "<INPUT TYPE=submit VALUE=\"".get_string("categorymoveto", "quiz")."\">";
+                echo "<INPUT TYPE=submit NAME=cancel VALUE=\"$strcancel\">";
+                echo "</FORM></P></CENTER>";
+                print_footer($course);
+                exit;
+            }
+        }
+        delete_records("quiz_categories", "id", $category->id);
+        notify(get_string("categorydeleted", "", $category->name));
+    }
+
+/// Print heading
+
+    print_heading($streditcategories);
+
+/// If data submitted, then process and store.
+
+       if (match_referer() && isset($HTTP_POST_VARS)) {
+
+        $form = $HTTP_POST_VARS;
+
+        // Peel out all the data from variable names.
+        foreach ($form as $key => $val) {
+            if ($key == "new" and $val != "") {
+                $cat->name = $val;
+                $cat->info = $form['newinfo'];
+                $cat->publish = $form['newpublish'];
+                $cat->course = $course->id;
+                if (!insert_record("quiz_categories", $cat)) {
+                    error("Could not insert the new quiz category '$val'");
+                } else {
+                    notify(get_string("categoryadded", "", $val));
+                }
+            
+            } else if (substr($key,0,1) == "c") {
+                $cat->id  = substr($key,1);
+                $cat->name = $val;
+                $cat->info = $form["i$cat->id"];
+                $cat->publish = $form["p$cat->id"];
+                $cat->course = $course->id;
+                if (!update_record("quiz_categories", $cat)) {
+                    error("Could not update the quiz category '$val'");
+                }
+            }
+        }
+       }
+
+
+/// Get the existing categories
+    if (!$categories = get_records("quiz_categories", "course", $course->id, "id ASC")) {
+        unset($categories);
+        if (!$categories[] = quiz_get_default_category($course->id)) {
+            error("Error: Could not find or make a category!");
+        }
+    }
+
+
+/// Find lowest ID category - this is the default category
+    $default = 99999;
+    foreach ($categories as $category) {
+        if ($category->id < $default) {
+            $default = $category->id;
+        }
+    }
+
+
+    $publishoptions[0] = get_string("no");
+    $publishoptions[1] = get_string("yes");
+
+
+/// Print the table of all categories
+    $table->head  = array ($strcategory, $strcategoryinfo, $strpublish, $strquestions, $straction);
+    $table->align = array ("LEFT", "LEFT", "CENTER", "CENTER", "CENTER");
+    $table->size = array ("50", "50", "10", "10", "20");
+    $table->width = 200;
+
+    echo "<FORM ACTION=category.php METHOD=post>";
+    foreach ($categories as $category) {
+        $count = count_records("quiz_questions", "category", $category->id);
+        if ($category->id == $default) { 
+            $delete = "";  // Can't delete default category
+        } else {
+            $delete = "<A HREF=\"category.php?id=$course->id&delete=$category->id\">$strdelete</A>";
+        }
+        $table->data[] = array ("<INPUT TYPE=text NAME=\"c$category->id\" VALUE=\"$category->name\" SIZE=15>",
+                                "<INPUT TYPE=text NAME=\"i$category->id\" VALUE=\"$category->info\" SIZE=50>",
+                                choose_from_menu ($publishoptions, "p$category->id", "$category->publish", "", "", "", true),
+                                "$count", 
+                                $delete);
+    }
+    $table->data[] = array ("<INPUT TYPE=text NAME=\"new\" VALUE=\"\" SIZE=15>", 
+                            "<INPUT TYPE=text NAME=\"newinfo\" VALUE=\"\" SIZE=50>", 
+                            choose_from_menu ($publishoptions, "newpublish", "", "", "", "", true),
+                            "", 
+                            "$stradd");
+    print_table($table);
+    echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
+    echo "<CENTER><BR><INPUT TYPE=submit VALUE=\"$strsavechanges\"> ";
+    echo "<BR><BR><INPUT TYPE=submit NAME=backtoquiz VALUE=\"$strbacktoquiz\"> ";
+    echo "</CENTER>";
+    echo "</FORM>";
+
+    print_footer();
 
-echo "not done yet";
 ?>
index aec475b2cee341b49f3193939b6ea5fadc0f4403..9fccbba01f37836e2de3bbde1849b7d5ea09003f 100644 (file)
@@ -221,14 +221,28 @@ function quiz_print_correctanswer($text) {
     echo "<P ALIGN=RIGHT><SPAN CLASS=highlight>$text</SPAN></P>";
 }
 
-function  quiz_print_question($number, $questionid, $grade, $courseid, 
+function quiz_print_question_icon($question) {
+// Prints a question icon
+    switch ($question->type) {
+        case SHORTANSWER:
+            echo "<IMG HEIGHT=16 WIDTH=16 SRC=\"pix/sa.gif\">";
+            break;
+        case TRUEFALSE:
+            echo "<IMG HEIGHT=16 WIDTH=16 SRC=\"pix/tf.gif\">";
+            break;
+        case MULTICHOICE:
+            echo "<IMG HEIGHT=16 WIDTH=16 SRC=\"pix/mc.gif\">";
+            break;
+        case RANDOM:
+            echo "<IMG HEIGHT=16 WIDTH=16 SRC=\"pix/rs.gif\">";
+            break;
+    }
+}
+
+function quiz_print_question($number, $questionid, $grade, $courseid, 
                               $feedback=NULL, $response=NULL, $actualgrade=NULL, $correct=NULL) {
 /// Prints a quiz question, any format
-    global $THEME;
 
-    $comment = $THEME->cellheading2;
-    $green = "#AAFFAA";
-    
     if (!$question = get_record("quiz_questions", "id", $questionid)) {
         notify("Error: Question not found!");
     }
@@ -263,7 +277,7 @@ function  quiz_print_question($number, $questionid, $grade, $courseid,
                quiz_print_comment("<P ALIGN=right>$feedback[0]</P>");
            }
            if ($correct) {
-               $correctanswers = implode(",", $correct);
+               $correctanswers = implode(", ", $correct);
                quiz_print_correctanswer($correctanswers);
            }
            break;
@@ -445,12 +459,17 @@ function quiz_get_default_category($courseid) {
     return $category;
 }
 
+function quiz_get_category_menu($courseid, $published=false) {
+    if ($published) {
+        $publish = "OR publish = '1'";
+    }
+    return get_records_sql_menu("SELECT id,name FROM quiz_categories WHERE course='$courseid' $publish ORDER by name ASC");
+}
+
 function quiz_print_category_form($course, $current) {
 // Prints a form to choose categories
 
-    if (!$categories = get_records_sql_menu("SELECT id,name FROM quiz_categories 
-                                             WHERE course='$course->id' OR publish = '1'
-                                             ORDER by name ASC")) {
+    if (!$categories = quiz_get_category_menu($course->id, true)) {
         if (!$category = quiz_get_default_category($course->id)) {
             notify("Error creating a default category!");
             return false;
@@ -533,6 +552,7 @@ function quiz_print_question_list($questionlist, $grades) {
     $strmoveup = get_string("moveup");
     $strmovedown = get_string("movedown");
     $strsavegrades = get_string("savegrades", "quiz");
+    $strtype = get_string("type", "quiz");
 
     for ($i=10; $i>=0; $i--) {
         $gradesmenu[$i] = $i;
@@ -542,7 +562,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 WIDTH=10 COLSPAN=3>$strorder</TH><TH align=left WIDTH=\"100%\">$strquestionname</TH><TH WIDTH=10>$strgrade</TH><TH WIDTH=10>$stredit</TH></TR>";
+    echo "<TR><TH WIDTH=10 COLSPAN=3>$strorder</TH><TH align=left WIDTH=\"100%\">$strquestionname</TH><TH width=16>$strtype</TH><TH WIDTH=10>$strgrade</TH><TH WIDTH=10>$stredit</TH></TR>";
     foreach ($order as $qnum) {
         $count++;
         echo "<TR BGCOLOR=\"$THEME->cellcontent\">";
@@ -560,6 +580,9 @@ function quiz_print_question_list($questionlist, $grades) {
         }
         echo "</TD>";
         echo "<TD>".$questions[$qnum]->name."</TD>";
+        echo "<TD WIDTH=16 ALIGN=CENTER>";
+        quiz_print_question_icon($questions[$qnum]);
+        echo "</TD>";
         echo "<TD>";
         choose_from_menu($gradesmenu, "q$qnum", (string)$grades[$qnum], "");
         echo "<TD>";
@@ -571,7 +594,7 @@ function quiz_print_question_list($questionlist, $grades) {
 
         $sumgrade += $grades[$qnum];
     }
-    echo "<TR><TD COLSPAN=3><TD ALIGN=right>";
+    echo "<TR><TD COLSPAN=5 ALIGN=right>";
     echo "<INPUT TYPE=submit VALUE=\"$strsavegrades:\">";
     echo "<INPUT TYPE=hidden NAME=setgrades VALUE=\"save\">";
     echo "<TD ALIGN=LEFT BGCOLOR=\"$THEME->cellcontent\">";
@@ -598,6 +621,7 @@ function quiz_print_cat_question_list($categoryid) {
     $strdelete = get_string("delete");
     $stredit = get_string("edit");
     $straddselectedtoquiz = get_string("addselectedtoquiz", "quiz");
+    $strtype = get_string("type", "quiz");
 
     if (!$categoryid) {
         echo "<P align=center>";
@@ -632,7 +656,7 @@ function quiz_print_cat_question_list($categoryid) {
 
     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>";
+    echo "<TR><TH width=10>$strselect</TH><TH width=* align=left>$strquestionname</TH><TH WIDTH=16>$strtype</TH>";
     if ($canedit) {
         echo "<TH width=10>$stredit</TH>";
     }
@@ -643,6 +667,9 @@ function quiz_print_cat_question_list($categoryid) {
         echo "<INPUT TYPE=CHECKBOX NAME=q$question->id VALUE=\"1\">";
         echo "</TD>";
         echo "<TD>".$question->name."</TD>";
+        echo "<TD WIDTH=16 ALIGN=CENTER>";
+        quiz_print_question_icon($question);
+        echo "</TD>";
         if ($canedit) {
             echo "<TD>";
                 echo "<A TITLE=\"$strdelete\" HREF=\"question.php?delete=$question->id\"><IMG 
index a6b8e57df64e7d1325d5f2f2ccf9506267edc42f..01794f7cac1c0b9cfc545faad8332ca90249c5a0 100644 (file)
 \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
 </CENTER>\r
index b91cb2145f10d0d5c713856b6efc4a07ee40dd17..6a9a2ccc07bb306b717e544f9c1631da213708a0 100644 (file)
@@ -16,7 +16,7 @@
         }
 
         if (! $category = get_record("quiz_categories", "id", $question->category)) {
-            error("This question doesn't belong to a vald category!");
+            error("This question doesn't belong to a valid category!");
         }
         if (! $course = get_record("course", "id", $category->course)) {
             error("This question category doesn't belong to a valid course!");
@@ -61,6 +61,7 @@
         $question->name         = $form->name;
         $question->questiontext = $form->questiontext;
         $question->image        = $form->image;
+        $question->category     = $form->category;
 
         if ($question->id) { // Question already exists
             if (!update_record("quiz_questions", $question)) {
     arsort($gradeoptions, SORT_NUMERIC);
     arsort($gradeoptionsfull, 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")) {
+    if (!$categories = quiz_get_category_menu($course->id, true)) {
         error("No categories!");
     }
 
             require("multichoice.html");
         break;
             case RANDOM:
-                print_heading("Not supported yet");
+                print_heading("Sorry, random questions are not supported yet");
                 print_continue("edit.php");
             break;
 
index 0fd7a112571d405a0acf5df4714fd8aeb4208872..733dfe3ed142ccd01aec51f8f487b15d52a77245 100644 (file)
 \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
 </CENTER>\r
index fd2184c10dda3e8ed5963443bf81876cbacb6561..86616b5ee6c1d1b152ba1b02ea776997895718d9 100644 (file)
@@ -57,7 +57,6 @@
 \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
 </CENTER>\r