forcelogin) { require_login(); } if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if ($categoryedit !== -1) { $USER->categoryediting = $categoryedit; } $adminediting = !empty($USER->categoryediting); } else { $adminediting = false; } $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'); /// If data for a new category was submitted, then add it $mform = new category_add_form(); if ($form = $mform->get_data() and has_capability('moodle/category:create', $context)) { if (!empty($form->addcategory)) { unset($newcategory); $newcategory->name = stripslashes_safe($form->addcategory); $newcategory->description = $form->description; $newcategory->sortorder = 999; if (!insert_record('course_categories', $newcategory)) { notify("Could not insert the new category '" . format_string($newcategory->name) . "'"); } else { notify(get_string('categoryadded', '', format_string($newcategory->name))); } } } /// Unless it's an editing admin, just print the regular listing of courses/categories if (!$adminediting) { /// Print form for creating new categories $countcategories = count_records('course_categories'); if ($countcategories > 1 || ($countcategories == 1 && count_records('course') > 200)) { $strcourses = get_string('courses'); $strcategories = get_string('categories'); $navlinks = array(); $navlinks[] = array('name'=>$strcategories,'link'=>'','type'=>'misc'); $navigation = build_navigation($navlinks); print_header("$site->shortname: $strcategories", $strcourses, $navigation, '', '', true, update_categories_button()); print_heading($strcategories); print_box_start('categorybox'); print_whole_category_list(); print_box_end(); print_course_search(); } else { $strfulllistofcourses = get_string('fulllistofcourses'); print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))), '', '', true, update_categories_button()); print_box_start('courseboxes'); print_courses(0); print_box_end(); } /// I am not sure this context in the next has_capability call is correct. if (isloggedin() and !isguest() and !has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $CFG->enablecourserequests) { // Print link to request a new course print_single_button('request.php', NULL, get_string('courserequest'), 'get'); } if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Print link to create a new course /// Get the 1st available category $options = array('category' => get_field('course_categories', 'id', 'parent', '0')); print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); } if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) { print_single_button('pending.php',NULL, get_string('coursespending'),'get'); } print_footer(); exit; } /// From now on is all the admin/course creator functions /// Print headings if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { require_once($CFG->libdir.'/adminlib.php'); admin_externalpage_setup('coursemgmt'); admin_externalpage_print_header(); } else { print_header("$site->shortname: $strcategories", $strcourses, build_navigation(array(array('name'=>$strcategories,'link'=>'','type'=>'misc'))), '', '', true, update_categories_button()); } print_heading($strcategories); /// Delete a category if necessary if (!empty($delete) and confirm_sesskey()) { // context is coursecat, if not present admins should have it set in site level $context = get_context_instance(CONTEXT_COURSECAT, $delete); if ($deletecat = get_record('course_categories', 'id', $delete) and has_capability('moodle/category:delete', $context)) { if (!empty($sure) && $sure == md5($deletecat->timemodified)) { /// Send the children categories to live with their grandparent if ($childcats = get_records('course_categories', 'parent', $deletecat->id)) { foreach ($childcats as $childcat) { if (! set_field('course_categories', 'parent', $deletecat->parent, 'id', $childcat->id)) { error('Could not update a child category!', 'index.php'); } } } /// If the grandparent is a valid (non-zero) category, then /// send the children courses to live with their grandparent as well if ($deletecat->parent) { if ($childcourses = get_records('course', 'category', $deletecat->id)) { foreach ($childcourses as $childcourse) { if (! set_field('course', 'category', $deletecat->parent, 'id', $childcourse->id)) { error('Could not update a child course!', 'index.php'); } } } } /// Finally delete the category itself if (delete_records('course_categories', 'id', $deletecat->id)) { notify(get_string('categorydeleted', '', format_string($deletecat->name))); // MLD-9983 events_trigger('category_deleted', $deletecat); } } else { $strdeletecategorycheck = get_string('deletecategorycheck','', format_string($deletecat->name)); notice_yesno($strdeletecategorycheck, "index.php?delete=$delete&sure=".md5($deletecat->timemodified)."&sesskey=$USER->sesskey", "index.php?sesskey=$USER->sesskey"); print_footer(); exit(); } } } /// Create a default category if necessary if (!$categories = get_categories()) { /// No category yet! // Try and make one unset($tempcat); $tempcat->name = get_string('miscellaneous'); if (!$tempcat->id = insert_record('course_categories', $tempcat)) { error('Serious error: Could not create a default category!'); } } /// Move a category to a new parent if required if (!empty($move) and ($moveto>=0) and confirm_sesskey()) { if ($tempcat = get_record('course_categories', 'id', $move)) { if ($tempcat->parent != $moveto) { if (! set_field('course_categories', 'parent', $moveto, 'id', $tempcat->id)) { notify('Could not update that category!'); } else { rebuild_context_rel(get_context_instance(CONTEXT_COURSECAT, $move)); } } } } /// Hide or show a category if ((!empty($hide) or !empty($show)) and confirm_sesskey()) { if (!empty($hide)) { $tempcat = get_record('course_categories', 'id', $hide); $visible = 0; } else { $tempcat = get_record('course_categories', 'id', $show); $visible = 1; } if ($tempcat) { if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id)) { notify('Could not update that category!'); } if (! set_field('course', 'visible', $visible, 'category', $tempcat->id)) { notify('Could not hide/show any courses in this category !'); } } } /// Move a category up or down if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) { $swapcategory = NULL; $movecategory = NULL; if (!empty($moveup)) { if ($movecategory = get_record('course_categories', 'id', $moveup)) { $categories = get_categories($movecategory->parent); foreach ($categories as $category) { if ($category->id == $movecategory->id) { break; } $swapcategory = $category; } } } if (!empty($movedown)) { if ($movecategory = get_record('course_categories', 'id', $movedown)) { $categories = get_categories($movecategory->parent); $choosenext = false; foreach ($categories as $category) { if ($choosenext) { $swapcategory = $category; break; } if ($category->id == $movecategory->id) { $choosenext = true; } } } } if ($swapcategory and $movecategory) { // Renumber everything for robustness $count=0; foreach ($categories as $category) { $count++; if ($category->id == $swapcategory->id) { $category = $movecategory; } else if ($category->id == $movecategory->id) { $category = $swapcategory; } if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id)) { notify('Could not update that category!'); } } } } /// Find the default category (the one with the lowest ID) $categories = get_categories(); $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(NULL,NULL,'c.id, c.category, c.sortorder, c.visible')) { foreach ($courses as $course) { if ($course->category and !isset($categories[$course->category])) { set_field('course', 'category', $default, 'id', $course->id); } } } fix_course_sortorder(); /// Print form for creating new categories if (has_capability('moodle/category:create', get_context_instance(CONTEXT_SYSTEM))) { $mform->display(); } /// Print out the categories with all the knobs $strcategories = get_string('categories'); $strcourses = get_string('courses'); $strmovecategoryto = get_string('movecategoryto'); $stredit = get_string('edit'); $displaylist = array(); $parentlist = array(); $displaylist[0] = get_string('top'); make_categories_list($displaylist, $parentlist, ''); echo '
'.$strcategories.' | '; echo ''.$strcourses.' | '; echo ''.$stredit.' | '; echo ''.$strmovecategoryto.' | '; echo '
---|