mirror of
https://github.com/moodle/moodle.git
synced 2025-01-19 06:18:28 +01:00
9fa49e22ab
Basically all the Database functions are in lib/datalib.php and the web functions are all in lib/weblib.php, so moodlelib.php is much thinner than it was. Data functions have been extended ... most old calls will still work, but now many more SQL commands can be performed using the datalib functions rather than using SQL. I'm currently moving through the whole tree replacing SQL calls or at least concentrating them in one section of mod/xxx/lib.php Still working on forums, quizzes, surveys, resources. The tree is currently not full working ... some things are half-completed ... will resume tomorrow.
124 lines
4.0 KiB
PHP
124 lines
4.0 KiB
PHP
<?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");
|
|
$straction = get_string("action");
|
|
$stradd = get_string("add");
|
|
|
|
print_header("$site->shortname: $strcategories", "$site->fullname",
|
|
"<A HREF=\"$CFG->wwwroot/admin/\">$stradministration</A> -> $strcategories");
|
|
|
|
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
|
|
if (!$categories = get_categories()) {
|
|
// 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
|
|
if ($courses = get_courses()) {
|
|
foreach ($courses as $course) {
|
|
if (!isset( $categories[$course->category] )) {
|
|
set_field("course", "category", $default, "id", $course->id);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// Print the table of all categories
|
|
$table->head = array ($strcategory, $strcourses, $straction);
|
|
$table->align = array ("LEFT", "CENTER", "CENTER");
|
|
$table->size = array ("80", "50", "50");
|
|
$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>",
|
|
"<A HREF=\"index.php?category=$category->id\">$count</A>", $delete);
|
|
}
|
|
$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();
|
|
|
|
?>
|