MDL-38147 deprecated move_category(), course_category_hide(), course_category_show...
authorMarina Glancy <marina@moodle.com>
Wed, 20 Feb 2013 23:55:35 +0000 (10:55 +1100)
committerMarina Glancy <marina@moodle.com>
Mon, 25 Mar 2013 02:21:09 +0000 (13:21 +1100)
All usages in core replaced with:
- coursecat::change_parent()
- coursecat::hide()
- coursecat::show()

course/editcategory.php
course/externallib.php
course/lib.php
course/manage.php
lib/deprecatedlib.php
lib/upgrade.txt

index b7184b7..a1aeb50 100644 (file)
@@ -27,8 +27,9 @@
  */
 
 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();
 
@@ -109,8 +110,7 @@ if ($mform->is_cancelled()) {
         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.
index d7a3a6e..df4da93 100644 (file)
@@ -1765,6 +1765,7 @@ class core_course_external extends external_api {
     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));
@@ -1800,19 +1801,19 @@ class core_course_external extends external_api {
                 $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);
         }
index 5c7f99b..eb8d6a4 100644 (file)
@@ -2684,97 +2684,6 @@ function move_courses($courseids, $categoryid) {
     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
  *
index 9721c4a..ad33d41 100644 (file)
@@ -141,31 +141,25 @@ if (!empty($deletecat) and confirm_sesskey()) {
 
 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()) {
index 70d3188..d31fe91 100644 (file)
@@ -3543,3 +3543,76 @@ function category_delete_full($category, $showfeedback=true) {
 
     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();
+}
index 0d0c081..ed1721d 100644 (file)
@@ -36,7 +36,8 @@ information provided here is intended especially for developers.
   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