shortname: $strcategories", "$site->fullname", "admin/index.php\">$stradministration -> $strcategories"); print_heading($strcategories); /// If data submitted, then process and store. if ($form = data_submitted()) { $categories = array(); // Peel out all the data from variable names. foreach ($form as $key => $val) { if ($key == "new") { if (!empty($val)) { if (get_records("course_categories", "name", $val)) { notify(get_string("categoryduplicate", "", $val)); } else { $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 ($existingcats = get_records("course_categories", "name", $val)) { foreach($existingcats as $existingcat) { if ($existingcat->id != $cat->id) { notify(get_string("categoryduplicate", "", $val)); continue 2; } } } 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 "
"; foreach ($categories as $category) { $count = count_records("course", "category", $category->id); if ($category->id == $default) { $delete = ""; // Can't delete default category } else { $delete = "id\">$strdelete"; } $table->data[] = array ("id\" VALUE=\"$category->name\" SIZE=30>", "id\">$count", $delete); } $table->data[] = array ("", "", "$stradd"); print_table($table); echo "

"; echo "
"; echo "
"; print_footer(); ?>