if (!empty($move) and $moveto >= 0 and confirm_sesskey()) { if ($cattomove = $DB->get_record('course_categories', array('id' => $move))) { require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent)); if ($cattomove->parent != $moveto) { $newparent = $DB->get_record('course_categories', array('id' => $moveto)); require_capability('moodle/category:manage', get_category_or_system_context($moveto)); move_category($cattomove, $newparent); } } } /// Hide or show a category if ($hide and confirm_sesskey()) { if ($tempcat = $DB->get_record('course_categories', array('id' => $hide))) { require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent)); if ($tempcat->visible == 1) { course_category_hide($tempcat); } } } else { if ($show and confirm_sesskey()) { if ($tempcat = $DB->get_record('course_categories', array('id' => $show))) { require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent)); if ($tempcat->visible == 0) { course_category_show($tempcat); } } } } /// Move a category up or down if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) { fix_course_sortorder();
/** * Efficiently moves a category - NOTE that this can have * a huge impact access-control-wise... */ function move_category($category, $newparentcat) { global $CFG, $DB; $context = get_context_instance(CONTEXT_COURSECAT, $category->id); $hidecat = false; if (empty($newparentcat->id)) { $DB->set_field('course_categories', 'parent', 0, array('id' => $category->id)); $newparent = get_context_instance(CONTEXT_SYSTEM); } else { $DB->set_field('course_categories', 'parent', $newparentcat->id, array('id' => $category->id)); $newparent = get_context_instance(CONTEXT_COURSECAT, $newparentcat->id); if (!$newparentcat->visible and $category->visible) { // better hide category when moving into hidden category, teachers may unhide afterwards and the hidden children will be restored properly $hidecat = true; } } context_moved($context, $newparent); // now make it last in new category $DB->set_field('course_categories', 'sortorder', MAX_COURSES_IN_CATEGORY * MAX_COURSE_CATEGORIES, array('id' => $category->id)); // and fix the sortorders fix_course_sortorder(); if ($hidecat) { course_category_hide($category); } }