forcelogin) {
require_login();
}
if (!$category = get_record("course_categories", "id", $id)) {
error("Category not known!");
}
if (iscreator()) {
if (isset($_GET['edit'])) {
if ($edit == "on") {
$USER->categoryediting = true;
} else if ($edit == "off") {
$USER->categoryediting = false;
}
}
$navbaritem = update_category_button($category->id);
$creatorediting = !empty($USER->categoryediting);
$adminediting = (isadmin() and $creatorediting);
} else {
if (!$category->visible) {
error(get_string('notavailable', 'error'));
}
$navbaritem = print_course_search("", true, "navbar");
$adminediting = false;
$creatorediting = false;
}
if (isadmin()) {
/// Rename the category if requested
if (!empty($_POST['rename'])) {
$category->name = $_POST['rename'];
if (! set_field("course_categories", "name", $category->name, "id", $category->id)) {
notify("An error occurred while renaming the category");
}
}
/// Resort the category if requested
if (!empty($_GET['resort'])) {
if ($courses = get_courses($category->id, "fullname ASC")) {
$count = 0;
foreach ($courses as $course) {
set_field('course', 'sortorder', $count, 'id', $course->id);
$count++;
}
fix_course_sortorder();
}
}
}
/// Print headings
$numcategories = count_records("course_categories");
$stradministration = get_string("administration");
$strcategories = get_string("categories");
$strcategory = get_string("category");
$strcourses = get_string("courses");
if ($creatorediting) {
if ($adminediting) {
print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
"admin/index.php\">$stradministration -> ".
"$strcategories -> $category->name",
"", "", true, $navbaritem);
} else {
print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
"$strcategories -> $category->name", "", "", true, $navbaritem);
}
} else {
print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
"$strcategories -> $category->name", "", "", true, $navbaritem);
}
/// Print the category selector
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, "");
echo '
';
echo " $strcategories: ";
echo " | ";
popup_form("category.php?id=", $displaylist, "switchcategory", "$category->id", "", "", "", false);
echo " |
";
/// Editing functions
if ($adminediting) {
/// Move a specified course to a new category
if (isset($moveto) and $data = data_submitted()) { // Some courses are being moved
if (! $destcategory = get_record("course_categories", "id", $data->moveto)) {
error("Error finding the category");
}
unset($data->moveto);
unset($data->id);
if ($data) {
foreach ($data as $code => $junk) {
$courseid = substr($code, 1);
if (! $course = get_record("course", "id", $courseid)) {
notify("Error finding course $courseid");
} else {
if (!set_field("course", "category", $destcategory->id, "id", $course->id)) {
notify("An error occurred - course not moved!");
}
fix_course_sortorder();
$category = get_record("course_categories", "id", $category->id);
}
}
}
}
/// Hide or show a course
if (isset($hide) or isset($show)) {
if (isset($hide)) {
$course = get_record("course", "id", $hide);
$visible = 0;
} else {
$course = get_record("course", "id", $show);
$visible = 1;
}
if ($course) {
if (! set_field("course", "visible", $visible, "id", $course->id)) {
notify("Could not update that course!");
}
}
}
/// Move a course up or down
if (isset($moveup) or isset($movedown)) {
$movecourse = NULL;
$swapcourse = NULL;
$courses = get_courses($category->id);
if (isset($moveup)) {
if ($movecourse = get_record("course", "id", $moveup)) {
foreach ($courses as $course) {
if ($course->id == $movecourse->id) {
break;
}
$swapcourse = $course;
}
}
}
if (isset($movedown)) {
if ($movecourse = get_record("course", "id", $movedown)) {
$choosenext = false;
foreach ($courses as $course) {
if ($choosenext) {
$swapcourse = $course;
break;
}
if ($course->id == $movecourse->id) {
$choosenext = true;
}
}
}
}
if ($swapcourse and $movecourse) { // Renumber everything for robustness
$count=0;
foreach ($courses as $course) {
$count++;
if ($course->id == $swapcourse->id) {
$course = $movecourse;
} else if ($course->id == $movecourse->id) {
$course = $swapcourse;
}
if (! set_field("course", "sortorder", $count, "id", $course->id)) {
notify("Could not update that course!");
}
}
}
}
fix_course_sortorder();
} // End of editing stuff
/// Print out all the sub-categories
if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
$firstentry = true;
foreach ($subcategories as $subcategory) {
if ($subcategory->visible or iscreator()) {
if ($firstentry) {
echo '';
echo ''.get_string('subcategories').' |
';
echo '';
$firstentry = false;
}
$catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
echo ''.
$subcategory->name.' ';
}
}
if (!$firstentry) {
echo " |
";
echo "
";
}
}
/// Print out all the courses
unset($course); // To avoid unwanted language effects later
$courses = get_courses_page($category->id, "c.sortorder ASC", "c.*", $totalcount, $page*$perpage, $perpage);
$numcourses = count($courses);
if (!$courses) {
print_heading(get_string("nocoursesyet"));
} else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$creatorediting) {
print_courses($category, "80%");
} else {
print_paging_bar($totalcount, $page, $perpage, "category.php?id=$category->id&perpage=$perpage&");
$strcourses = get_string("courses");
$strselect = get_string("select");
$stredit = get_string("edit");
$strdelete = get_string("delete");
$strbackup = get_string("backup");
$strrestore = get_string("restore");
$strmoveup = get_string("moveup");
$strmovedown = get_string("movedown");
$strupdate = get_string("update");
$strhide = get_string("hide");
$strshow = get_string("show");
$strsummary = get_string("summary");
$strsettings = get_string("settings");
$strassignteachers = get_string("assignteachers");
$strallowguests = get_string("allowguests");
$strrequireskey = get_string("requireskey");
if (empty($THEME->custompix)) {
$pixpath = "$CFG->wwwroot/pix";
} else {
$pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
}
echo '";
echo "
";
}
echo "";
if (isadmin() and $numcourses > 1) { /// Print button to re-sort courses by name
unset($options);
$options["id"] = $category->id;
$options["resort"] = "name";
print_single_button("category.php", $options, get_string("resortcoursesbyname"), "get");
}
if (iscreator()) { /// Print button to create a new course
unset($options);
$options["category"] = $category->id;
print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
echo "
";
}
if (isadmin()) { /// Print form to rename the category
$strrename= get_string("rename");
echo '";
echo "
";
}
echo "";
print_footer();
?>