cleanups along the way ...
$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";
$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";
$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";
$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";
/// DATABASE HANDLING ////////////////////////////////////////////////
-function execute_sql($command) {
+function execute_sql($command, $feedback=true) {
// Completely general
global $db;
$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;
}
}
<?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";
?>
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!");
}
quiz_print_comment("<P ALIGN=right>$feedback[0]</P>");
}
if ($correct) {
- $correctanswers = implode(",", $correct);
+ $correctanswers = implode(", ", $correct);
quiz_print_correctanswer($correctanswers);
}
break;
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;
$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;
$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\">";
}
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>";
$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\">";
$strdelete = get_string("delete");
$stredit = get_string("edit");
$straddselectedtoquiz = get_string("addselectedtoquiz", "quiz");
+ $strtype = get_string("type", "quiz");
if (!$categoryid) {
echo "<P align=center>";
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>";
}
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
\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
}
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!");
$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;
\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
\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