2002-09-09 11:48:11 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
// Allows the admin to create, delete and rename course categories
|
|
|
|
|
|
|
|
require("../config.php");
|
|
|
|
require("lib.php");
|
|
|
|
|
|
|
|
if (!isadmin()) {
|
|
|
|
error("Only administrators can use this course!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$site = get_site()) {
|
|
|
|
error("Site isn't defined!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Print headings
|
|
|
|
|
|
|
|
$stradministration = get_string("administration");
|
|
|
|
$strcategories = get_string("categories");
|
|
|
|
$strcategory = get_string("category");
|
|
|
|
$strcourses = get_string("courses");
|
|
|
|
$stredit = get_string("edit");
|
|
|
|
$strdelete = get_string("delete");
|
2002-09-09 15:50:55 +00:00
|
|
|
$straction = get_string("action");
|
2002-09-09 11:48:11 +00:00
|
|
|
$stradd = get_string("add");
|
|
|
|
|
2002-09-27 14:26:02 +00:00
|
|
|
print_header("$site->shortname: $strcategories", "$site->fullname",
|
2002-12-11 16:30:53 +00:00
|
|
|
"<A HREF=\"$CFG->wwwroot/admin/\">$stradministration</A> -> $strcategories");
|
2002-09-09 11:48:11 +00:00
|
|
|
|
|
|
|
print_heading($strcategories);
|
|
|
|
|
|
|
|
/// If data submitted, then process and store.
|
|
|
|
|
|
|
|
if (match_referer() && isset($HTTP_POST_VARS)) {
|
|
|
|
|
|
|
|
$categories = array();
|
|
|
|
|
|
|
|
// Peel out all the data from variable names.
|
|
|
|
foreach ($HTTP_POST_VARS as $key => $val) {
|
|
|
|
if ($key == "new" and $val != "") {
|
|
|
|
$cat->name = $val;
|
|
|
|
if (!insert_record("course_categories", $cat)) {
|
|
|
|
error("Could not insert the new category '$val'");
|
|
|
|
} else {
|
|
|
|
notify(get_string("categoryadded", "", $val));
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$cat->id = substr($key,1);
|
|
|
|
$cat->name = $val;
|
|
|
|
if (!update_record("course_categories", $cat)) {
|
|
|
|
error("Could not update the category '$val'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the existing categories
|
2002-12-20 14:44:14 +00:00
|
|
|
if (!$categories = get_categories()) {
|
2002-09-09 11:48:11 +00:00
|
|
|
// Try and make one
|
|
|
|
$cat->name = get_string("miscellaneous");
|
|
|
|
if ($cat->id = insert_record("course_categories", $cat)) {
|
|
|
|
$categories[$cat->id] = $cat;
|
|
|
|
} else {
|
|
|
|
error("Serious error: Could not create a default category!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete category if the user wants to delete it
|
|
|
|
if (isset($delete)) {
|
|
|
|
if (delete_records("course_categories", "id", $delete)) {
|
|
|
|
notify(get_string("categorydeleted", "", $categories[$delete]->name));
|
|
|
|
unset($categories[$delete]);
|
|
|
|
} else {
|
|
|
|
error("An error occurred while trying to delete a category");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find lowest ID category - this is the default category
|
|
|
|
$default = 99999;
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if ($category->id < $default) {
|
|
|
|
$default = $category->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find any orphan courses that don't yet have a valid category and set to default
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($courses = get_courses()) {
|
2002-09-09 11:48:11 +00:00
|
|
|
foreach ($courses as $course) {
|
|
|
|
if (!isset( $categories[$course->category] )) {
|
|
|
|
set_field("course", "category", $default, "id", $course->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Print the table of all categories
|
2002-09-09 15:50:55 +00:00
|
|
|
$table->head = array ($strcategory, $strcourses, $straction);
|
2002-09-09 11:48:11 +00:00
|
|
|
$table->align = array ("LEFT", "CENTER", "CENTER");
|
2002-12-07 08:39:24 +00:00
|
|
|
$table->size = array ("80", "50", "50");
|
2002-09-09 11:48:11 +00:00
|
|
|
$table->width = 100;
|
|
|
|
|
|
|
|
echo "<FORM ACTION=categories.php METHOD=post>";
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
$count = count_records("course", "category", $category->id);
|
|
|
|
if ($category->id == $default) {
|
|
|
|
$delete = ""; // Can't delete default category
|
|
|
|
} else {
|
|
|
|
$delete = "<A HREF=\"categories.php?delete=$category->id\">$strdelete</A>";
|
|
|
|
}
|
|
|
|
$table->data[] = array ("<INPUT TYPE=text NAME=\"c$category->id\" VALUE=\"$category->name\" SIZE=30>",
|
2002-09-09 15:50:55 +00:00
|
|
|
"<A HREF=\"index.php?category=$category->id\">$count</A>", $delete);
|
2002-09-09 11:48:11 +00:00
|
|
|
}
|
|
|
|
$table->data[] = array ("<INPUT TYPE=text NAME=\"new\" VALUE=\"\" SIZE=30>", "", "$stradd");
|
|
|
|
print_table($table);
|
|
|
|
echo "<CENTER><BR><INPUT TYPE=submit VALUE=\"".get_string("savechanges")."\"> ";
|
|
|
|
echo "</CENTER>";
|
|
|
|
echo "</FORM>";
|
|
|
|
|
|
|
|
print_footer();
|
|
|
|
|
|
|
|
?>
|