*/
require_once('../config.php');
-require_once('lib.php');
-require_once('editcategory_form.php');
+require_once($CFG->dirroot.'/course/lib.php');
+require_once($CFG->dirroot.'/course/editcategory_form.php');
+require_once($CFG->libdir.'/coursecatlib.php');
require_login();
if ($newcategory->parent != $category->parent) {
// check category manage capability if parent changed
require_capability('moodle/category:manage', get_category_or_system_context((int)$newcategory->parent));
- $parent_cat = $DB->get_record('course_categories', array('id' => $newcategory->parent));
- move_category($newcategory, $parent_cat);
+ coursecat::get($newcategory->id)->move($newcategory->parent);
}
} else {
// Create a new category.
public static function update_categories($categories) {
global $CFG, $DB;
require_once($CFG->dirroot . "/course/lib.php");
+ require_once($CFG->libdir . "/coursecatlib.php");
// Validate parameters.
$params = self::validate_parameters(self::update_categories_parameters(), array('categories' => $categories));
$category->theme = $cat['theme'];
}
if (!empty($cat['parent']) && ($category->parent != $cat['parent'])) {
- // First check if parent exists.
- if (!$parent_cat = $DB->get_record('course_categories', array('id' => $cat['parent']))) {
- throw new moodle_exception('unknowcategory');
+ $coursecat = coursecat::get($category->id);
+ if ($coursecat->can_move($cat['parent'])) {
+ self::validate_context(get_category_or_system_context((int)$cat['parent']));
+ $coursecat->move($cat['parent']);
+ $coursecat = coursecat::get($category->id);
+ $category->parent = (int)$cat['parent'];
+ // prevent automaticaly calculated fields from accidental update in DB
+ unset($category->path);
+ unset($category->depth);
+ unset($category->sortorder);
+ } else {
+ throw new moodle_exception('nopermissions', '', '', '');
}
- // Then check if we have capability.
- self::validate_context(get_category_or_system_context((int)$cat['parent']));
- require_capability('moodle/category:manage', get_category_or_system_context((int)$cat['parent']));
- // Finally move the category.
- move_category($category, $parent_cat);
- $category->parent = $cat['parent'];
- // Get updated path by move_category().
- $category->path = $DB->get_field('course_categories', 'path',
- array('id' => $category->id));
}
$DB->update_record('course_categories', $category);
}
return true;
}
-/**
- * Hide course category and child course and subcategories
- * @param stdClass $category
- * @return void
- */
-function course_category_hide($category) {
- global $DB;
-
- $category->visible = 0;
- $DB->set_field('course_categories', 'visible', 0, array('id'=>$category->id));
- $DB->set_field('course_categories', 'visibleold', 0, array('id'=>$category->id));
- $DB->execute("UPDATE {course} SET visibleold = visible WHERE category = ?", array($category->id)); // store visible flag so that we can return to it if we immediately unhide
- $DB->set_field('course', 'visible', 0, array('category' => $category->id));
- // get all child categories and hide too
- if ($subcats = $DB->get_records_select('course_categories', "path LIKE ?", array("$category->path/%"))) {
- foreach ($subcats as $cat) {
- $DB->set_field('course_categories', 'visibleold', $cat->visible, array('id'=>$cat->id));
- $DB->set_field('course_categories', 'visible', 0, array('id'=>$cat->id));
- $DB->execute("UPDATE {course} SET visibleold = visible WHERE category = ?", array($cat->id));
- $DB->set_field('course', 'visible', 0, array('category' => $cat->id));
- }
- }
- add_to_log(SITEID, "category", "hide", "editcategory.php?id=$category->id", $category->id);
-}
-
-/**
- * Show course category and child course and subcategories
- * @param stdClass $category
- * @return void
- */
-function course_category_show($category) {
- global $DB;
-
- $category->visible = 1;
- $DB->set_field('course_categories', 'visible', 1, array('id'=>$category->id));
- $DB->set_field('course_categories', 'visibleold', 1, array('id'=>$category->id));
- $DB->execute("UPDATE {course} SET visible = visibleold WHERE category = ?", array($category->id));
- // get all child categories and unhide too
- if ($subcats = $DB->get_records_select('course_categories', "path LIKE ?", array("$category->path/%"))) {
- foreach ($subcats as $cat) {
- if ($cat->visibleold) {
- $DB->set_field('course_categories', 'visible', 1, array('id'=>$cat->id));
- }
- $DB->execute("UPDATE {course} SET visible = visibleold WHERE category = ?", array($cat->id));
- }
- }
- add_to_log(SITEID, "category", "show", "editcategory.php?id=$category->id", $category->id);
-}
-
-/**
- * Efficiently moves a category - NOTE that this can have
- * a huge impact access-control-wise...
- *
- * @param stdClass|coursecat $category
- * @param stdClass|coursecat $newparentcat
- */
-function move_category($category, $newparentcat) {
- global $CFG, $DB;
-
- $context = context_coursecat::instance($category->id);
-
- $hidecat = false;
- if (empty($newparentcat->id)) {
- $DB->set_field('course_categories', 'parent', 0, array('id' => $category->id));
- $newparent = context_system::instance();
- } else {
- $DB->set_field('course_categories', 'parent', $newparentcat->id, array('id' => $category->id));
- $newparent = context_coursecat::instance($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));
-
- // Log action.
- add_to_log(SITEID, "category", "move", "editcategory.php?id=$category->id", $category->id);
-
- // and fix the sortorders
- fix_course_sortorder();
-
- if ($hidecat) {
- course_category_hide($category);
- }
-}
-
/**
* Returns the display name of the given section that the course prefers
*
if (!empty($movecat) and ($movetocat >= 0) and confirm_sesskey()) {
// Move a category to a new parent if required.
- if ($cattomove = $DB->get_record('course_categories', array('id' => $movecat))) {
- require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent));
- if ($cattomove->parent != $movetocat) {
- $newparent = $DB->get_record('course_categories', array('id' => $movetocat));
- require_capability('moodle/category:manage', get_category_or_system_context($movetocat));
- move_category($cattomove, $newparent);
+ $cattomove = coursecat::get($movecat);
+ if ($cattomove->parent != $movetocat) {
+ if ($cattomove->can_change_parent($movetocat)) {
+ $cattomove->change_parent($movetocat);
+ } else {
+ print_error('cannotmovecategory');
}
}
}
// Hide or show a category.
if ($hidecat and confirm_sesskey()) {
- if ($tempcat = $DB->get_record('course_categories', array('id' => $hidecat))) {
- require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
- if ($tempcat->visible == 1) {
- course_category_hide($tempcat);
- }
- }
+ $coursecat = coursecat::get($hidecat);
+ require_capability('moodle/category:manage', get_category_or_system_context($coursecat->parent));
+ $coursecat->hide();
} else if ($showcat and confirm_sesskey()) {
- if ($tempcat = $DB->get_record('course_categories', array('id' => $showcat))) {
- require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent));
- if ($tempcat->visible == 0) {
- course_category_show($tempcat);
- }
- }
+ $coursecat = coursecat::get($showcat);
+ require_capability('moodle/category:manage', get_category_or_system_context($coursecat->parent));
+ $coursecat->show();
}
if ((!empty($moveupcat) or !empty($movedowncat)) and confirm_sesskey()) {
return coursecat::get($category->id)->delete_full($showfeedback);
}
+
+/**
+ * Efficiently moves a category - NOTE that this can have
+ * a huge impact access-control-wise...
+ *
+ * This function is deprecated. Please use
+ * $coursecat = coursecat::get($category->id);
+ * if ($coursecat->can_change_parent($newparentcat->id)) {
+ * $coursecat->change_parent($newparentcat->id);
+ * }
+ *
+ * Alternatively you can use
+ * $coursecat->update(array('parent' => $newparentcat->id));
+ *
+ * Function update() also updates field course_categories.timemodified
+ *
+ * @see coursecat::change_parent()
+ * @see coursecat::update()
+ * @deprecated since 2.5
+ *
+ * @param stdClass|coursecat $category
+ * @param stdClass|coursecat $newparentcat
+ */
+function move_category($category, $newparentcat) {
+ global $CFG;
+ require_once($CFG->libdir.'/coursecatlib.php');
+
+ debugging('Function move_category() is deprecated. Please use coursecat::change_parent() instead.');
+
+ return coursecat::get($category->id)->change_parent($newparentcat->id);
+}
+
+/**
+ * Hide course category and child course and subcategories
+ *
+ * This function is deprecated. Please use
+ * coursecat::get($category->id)->hide();
+ *
+ * @see coursecat::hide()
+ * @deprecated since 2.5
+ *
+ * @param stdClass $category
+ * @return void
+ */
+function course_category_hide($category) {
+ global $CFG;
+ require_once($CFG->libdir.'/coursecatlib.php');
+
+ debugging('Function course_category_hide() is deprecated. Please use coursecat::hide() instead.');
+
+ coursecat::get($category->id)->hide();
+}
+
+/**
+ * Show course category and child course and subcategories
+ *
+ * This function is deprecated. Please use
+ * coursecat::get($category->id)->show();
+ *
+ * @see coursecat::show()
+ * @deprecated since 2.5
+ *
+ * @param stdClass $category
+ * @return void
+ */
+function course_category_show($category) {
+ global $CFG;
+ require_once($CFG->libdir.'/coursecatlib.php');
+
+ debugging('Function course_category_show() is deprecated. Please use coursecat::show() instead.');
+
+ coursecat::get($category->id)->show();
+}
format_string() with the passed options.
* Functions responsible for managing and accessing course categories are moved to class coursecat
in lib/coursecatlib.php. The following global functions are deprecated: make_categories_list(),
- category_delete_move(), category_delete_full()
+ category_delete_move(), category_delete_full(), move_category(), course_category_hide(),
+ course_category_show()
YUI changes:
* M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp