moodle/mod/quiz/category.php
gustav_delius 839f2456bb XHTML compliance
- fixed (hopefully) all <img> tags
- global replace on <br> to <br />
- &amp; in URLs
- got the forum module XHTML compliant
Julian Sedding
2004-09-16 17:13:57 +00:00

183 lines
7.0 KiB
PHP

<?php // $Id$
// Allows a teacher to create, edit and delete categories
require_once("../../config.php");
require_once("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(isset($modform->instance) ? "editingquiz"
: "editquestions",
"quiz");
$streditcategories = get_string("editcategories", "quiz");
print_header_simple("$streditcategories", " $streditcategories",
"<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 (! quiz_move_questions($category->id, $category2->id)) {
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
echo "<p align=\"center\"><font size=\"3\">";
echo $streditcategories;
helpbutton("categories", $streditcategories, "quiz");
echo "</font></p>";
/// If data submitted, then process and store.
if ($form = data_submitted()) {
$form = (array)$form;
// Peel out all the data from variable names.
foreach ($form as $key => $val) {
$cat = NULL;
if ($key == "new" and $val != "") {
$cat->name = $val;
$cat->info = $form['newinfo'];
$cat->publish = $form['newpublish'];
$cat->course = $course->id;
$cat->stamp = make_unique_id_code();
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 ("80", "80", "40", "40", "50");
$table->width = 200;
$table->nowrap = true;
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&amp;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();
?>