From: moodler Date: Sun, 20 Oct 2002 17:15:39 +0000 (+0000) Subject: Category editing in quizzes is now supported. Some other little X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=c74a0ca5c24473ccf5dc1ba9b8a5b44dcecb2fb9;p=moodle.git Category editing in quizzes is now supported. Some other little cleanups along the way ... --- diff --git a/lang/en/quiz.php b/lang/en/quiz.php index 7d3abf86b3..5acb098e58 100644 --- a/lang/en/quiz.php +++ b/lang/en/quiz.php @@ -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"; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index e32c493fdd..ac7dcc33a2 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -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 "

".get_string("success")."

"; + if ($feedback) { + echo "

".get_string("success")."

"; + } return true; } else { - echo "

".get_string("error")."

"; + if ($feedback) { + echo "

".get_string("error")."

"; + } return false; } } diff --git a/mod/quiz/category.php b/mod/quiz/category.php index b3ce80f42d..4de75c88b1 100644 --- a/mod/quiz/category.php +++ b/mod/quiz/category.php @@ -1,4 +1,175 @@ 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", + "wwwroot/course/view.php?id=$course->id\">$course->shortname + -> $streditingquiz -> $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 "

"; + echo "id\">"; + echo "id\">"; + choose_from_menu($categories, "confirm", "", ""); + echo ""; + echo ""; + echo "

"; + 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 "
"; + foreach ($categories as $category) { + $count = count_records("quiz_questions", "category", $category->id); + if ($category->id == $default) { + $delete = ""; // Can't delete default category + } else { + $delete = "id&delete=$category->id\">$strdelete"; + } + $table->data[] = array ("id\" VALUE=\"$category->name\" SIZE=15>", + "id\" VALUE=\"$category->info\" SIZE=50>", + choose_from_menu ($publishoptions, "p$category->id", "$category->publish", "", "", "", true), + "$count", + $delete); + } + $table->data[] = array ("", + "", + choose_from_menu ($publishoptions, "newpublish", "", "", "", "", true), + "", + "$stradd"); + print_table($table); + echo "id\">"; + echo "

"; + echo "

"; + echo "
"; + echo "
"; + + print_footer(); -echo "not done yet"; ?> diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index aec475b2ce..9fccbba01f 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -221,14 +221,28 @@ function quiz_print_correctanswer($text) { echo "

$text

"; } -function quiz_print_question($number, $questionid, $grade, $courseid, +function quiz_print_question_icon($question) { +// Prints a question icon + switch ($question->type) { + case SHORTANSWER: + echo ""; + break; + case TRUEFALSE: + echo ""; + break; + case MULTICHOICE: + echo ""; + break; + case RANDOM: + echo ""; + 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("

$feedback[0]

"); } 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 "
"; echo ""; - echo ""; + echo ""; foreach ($order as $qnum) { $count++; echo "cellcontent\">"; @@ -560,6 +580,9 @@ function quiz_print_question_list($questionlist, $grades) { } echo ""; echo ""; + echo ""; echo "
$strorder$strquestionname$strgrade$stredit
$strorder$strquestionname$strtype$strgrade$stredit
".$questions[$qnum]->name.""; + quiz_print_question_icon($questions[$qnum]); + echo ""; choose_from_menu($gradesmenu, "q$qnum", (string)$grades[$qnum], ""); echo ""; @@ -571,7 +594,7 @@ function quiz_print_question_list($questionlist, $grades) { $sumgrade += $grades[$qnum]; } - echo "
"; + echo "
"; echo ""; echo ""; echo "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 "

"; @@ -632,7 +656,7 @@ function quiz_print_cat_question_list($categoryid) { echo ""; echo ""; - echo ""; + echo ""; if ($canedit) { echo ""; } @@ -643,6 +667,9 @@ function quiz_print_cat_question_list($categoryid) { echo "id VALUE=\"1\">"; echo ""; echo ""; + echo ""; if ($canedit) { echo "
$strselect$strquestionname
$strselect$strquestionname$strtype$stredit".$question->name.""; + quiz_print_question_icon($question); + echo ""; echo "id\"> - "> diff --git a/mod/quiz/question.php b/mod/quiz/question.php index b91cb2145f..6a9a2ccc07 100644 --- a/mod/quiz/question.php +++ b/mod/quiz/question.php @@ -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)) { @@ -187,9 +188,7 @@ 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!"); } @@ -241,7 +240,7 @@ 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; diff --git a/mod/quiz/shortanswer.html b/mod/quiz/shortanswer.html index 0fd7a11257..733dfe3ed1 100644 --- a/mod/quiz/shortanswer.html +++ b/mod/quiz/shortanswer.html @@ -154,7 +154,6 @@ - "> diff --git a/mod/quiz/truefalse.html b/mod/quiz/truefalse.html index fd2184c10d..86616b5ee6 100644 --- a/mod/quiz/truefalse.html +++ b/mod/quiz/truefalse.html @@ -57,7 +57,6 @@ - ">