MDL-38147 deprecated create_course_category(), change usage to coursecat. Also avoid...
authorMarina Glancy <marina@moodle.com>
Thu, 21 Feb 2013 00:33:15 +0000 (11:33 +1100)
committerMarina Glancy <marina@moodle.com>
Mon, 25 Mar 2013 02:22:26 +0000 (13:22 +1100)
changed to usage of
- coursecat::create()
- coursecat::update()

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

index a1aeb50..355aca1 100644 (file)
@@ -93,41 +93,16 @@ if ($mform->is_cancelled()) {
         redirect($CFG->wwwroot .'/course/manage.php');
     }
 } else if ($data = $mform->get_data()) {
-    $newcategory = new stdClass();
-    $newcategory->name = $data->name;
-    $newcategory->idnumber = $data->idnumber;
-    $newcategory->description_editor = $data->description_editor;
-    $newcategory->parent = $data->parent; // if $data->parent = 0, the new category will be a top-level category
-
-    if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
-        $newcategory->theme = $data->theme;
-    }
-
-    $logaction = 'update';
     if ($id) {
-        // Update an existing category.
-        $newcategory->id = $category->id;
-        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));
-            coursecat::get($newcategory->id)->move($newcategory->parent);
+        $newcategory = coursecat::get($id);
+        if ($data->parent != $category->parent && !$newcategory->can_change_parent($data->parent)) {
+            print_error('cannotmovecategory');
         }
+        $newcategory->update($data, $editoroptions);
     } else {
-        // Create a new category.
-        $newcategory->description = $data->description_editor['text'];
-
-        // Don't overwrite the $newcategory object as it'll be processed by file_postupdate_standard_editor in a moment
-        $category = create_course_category($newcategory);
-        $newcategory->id = $category->id;
-        $categorycontext = $category->context;
-        $logaction = 'add';
+        $newcategory = coursecat::create($data, $editoroptions);
     }
 
-    $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext, 'coursecat', 'description', 0);
-    $DB->update_record('course_categories', $newcategory);
-    add_to_log(SITEID, "category", $logaction, "editcategory.php?id=$newcategory->id", $newcategory->id);
-    fix_course_sortorder();
-
     redirect('manage.php?id='.$newcategory->id);
 }
 
index df4da93..362f928 100644 (file)
@@ -1650,7 +1650,7 @@ class core_course_external extends external_api {
      */
     public static function create_categories($categories) {
         global $CFG, $DB;
-        require_once($CFG->dirroot . "/course/lib.php");
+        require_once($CFG->libdir . "/coursecatlib.php");
 
         $params = self::validate_parameters(self::create_categories_parameters(),
                         array('categories' => $categories));
@@ -1670,38 +1670,10 @@ class core_course_external extends external_api {
             self::validate_context($context);
             require_capability('moodle/category:manage', $context);
 
-            // Check name.
-            if (textlib::strlen($category['name'])>255) {
-                throw new moodle_exception('categorytoolong');
-            }
-
-            $newcategory = new stdClass();
-            $newcategory->name = $category['name'];
-            $newcategory->parent = $category['parent'];
-            // Format the description.
-            if (!empty($category['description'])) {
-                $newcategory->description = $category['description'];
-            }
-            $newcategory->descriptionformat = external_validate_format($category['descriptionformat']);
-            if (isset($category['theme']) and !empty($CFG->allowcategorythemes)) {
-                $newcategory->theme = $category['theme'];
-            }
-            // Check id number.
-            if (!empty($category['idnumber'])) { // Same as in course/editcategory_form.php .
-                if (textlib::strlen($category['idnumber'])>100) {
-                    throw new moodle_exception('idnumbertoolong');
-                }
-                if ($existing = $DB->get_record('course_categories', array('idnumber' => $category['idnumber']))) {
-                    if ($existing->id) {
-                        throw new moodle_exception('idnumbertaken');
-                    }
-                }
-                $newcategory->idnumber = $category['idnumber'];
-            }
+            // this will validate format and throw an exception if there are errors
+            external_validate_format($category['descriptionformat']);
 
-            $newcategory = create_course_category($newcategory);
-            // Populate special fields.
-            fix_course_sortorder();
+            $newcategory = coursecat::create($category);
 
             $createdcategories[] = array('id' => $newcategory->id, 'name' => $newcategory->name);
         }
@@ -1764,7 +1736,6 @@ 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.
@@ -1773,49 +1744,16 @@ class core_course_external extends external_api {
         $transaction = $DB->start_delegated_transaction();
 
         foreach ($params['categories'] as $cat) {
-            if (!$category = $DB->get_record('course_categories', array('id' => $cat['id']))) {
-                throw new moodle_exception('unknowcategory');
-            }
+            $category = coursecat::get($cat['id']);
 
             $categorycontext = context_coursecat::instance($cat['id']);
             self::validate_context($categorycontext);
             require_capability('moodle/category:manage', $categorycontext);
 
-            if (!empty($cat['name'])) {
-                if (textlib::strlen($cat['name'])>255) {
-                     throw new moodle_exception('categorytoolong');
-                }
-                $category->name = $cat['name'];
-            }
-            if (!empty($cat['idnumber'])) {
-                if (textlib::strlen($cat['idnumber'])>100) {
-                    throw new moodle_exception('idnumbertoolong');
-                }
-                $category->idnumber = $cat['idnumber'];
-            }
-            if (!empty($cat['description'])) {
-                $category->description = $cat['description'];
-                $category->descriptionformat = external_validate_format($cat['descriptionformat']);
-            }
-            if (!empty($cat['theme'])) {
-                $category->theme = $cat['theme'];
-            }
-            if (!empty($cat['parent']) && ($category->parent != $cat['parent'])) {
-                $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', '', '', '');
-                }
-            }
-            $DB->update_record('course_categories', $category);
+            // this will throw an exception if descriptionformat is not valid
+            external_validate_format($cat['descriptionformat']);
+
+            $category->update($cat);
         }
 
         $transaction->allow_commit();
index 3e363a8..62c8644 100644 (file)
@@ -2877,32 +2877,6 @@ function create_course($data, $editoroptions = NULL) {
     return $course;
 }
 
-/**
- * Create a new course category and marks the context as dirty
- *
- * This function does not set the sortorder for the new category and
- * @see{fix_course_sortorder} should be called after creating a new course
- * category
- *
- * Please note that this function does not verify access control.
- *
- * @param object $category All of the data required for an entry in the course_categories table
- * @return object new course category
- */
-function create_course_category($category) {
-    global $DB;
-
-    $category->timemodified = time();
-    $category->id = $DB->insert_record('course_categories', $category);
-    $category = $DB->get_record('course_categories', array('id' => $category->id));
-
-    // We should mark the context as dirty
-    $category->context = context_coursecat::instance($category->id);
-    $category->context->mark_dirty();
-
-    return $category;
-}
-
 /**
  * Update a course.
  *
index 3277d43..816edd4 100644 (file)
@@ -3671,3 +3671,36 @@ function get_course_category($catid=0) {
 
     return $category;
 }
+
+/**
+ * Create a new course category and marks the context as dirty
+ *
+ * This function does not set the sortorder for the new category and
+ * {@link fix_course_sortorder()} should be called after creating a new course
+ * category
+ *
+ * Please note that this function does not verify access control.
+ *
+ * This function is deprecated. It is replaced with the method create() in class coursecat.
+ * {@link coursecat::create()} also verifies the data, fixes sortorder and logs the action
+ *
+ * @deprecated since 2.5
+ *
+ * @param object $category All of the data required for an entry in the course_categories table
+ * @return object new course category
+ */
+function create_course_category($category) {
+    global $DB;
+
+    debugging('Function create_course_category() is deprecated. Please use coursecat::create(), see phpdocs for more details', DEBUG_DEVELOPER);
+
+    $category->timemodified = time();
+    $category->id = $DB->insert_record('course_categories', $category);
+    $category = $DB->get_record('course_categories', array('id' => $category->id));
+
+    // We should mark the context as dirty
+    $category->context = context_coursecat::instance($category->id);
+    $category->context->mark_dirty();
+
+    return $category;
+}
index d615876..6ca9915 100644 (file)
@@ -37,7 +37,7 @@ information provided here is intended especially for developers.
 * 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(), move_category(), course_category_hide(),
-  course_category_show(), get_course_category()
+  course_category_show(), get_course_category(), create_course_category()
 
 YUI changes:
 * M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp