From 2d8a275baba858ae2ae350aa8b5553a09fb528cf Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 21 Feb 2013 11:22:20 +1100 Subject: [PATCH] MDL-38147 deprecated get_course_category(), change usage to coursecat --- course/lib.php | 36 ++++++++++++++++++++-------- course/pending.php | 13 ++-------- lib/datalib.php | 42 --------------------------------- lib/deprecatedlib.php | 55 +++++++++++++++++++++++++++++++++++++++++++ lib/upgrade.txt | 2 +- 5 files changed, 84 insertions(+), 64 deletions(-) diff --git a/course/lib.php b/course/lib.php index eb8d6a43d5d..3e363a874ec 100644 --- a/course/lib.php +++ b/course/lib.php @@ -3265,6 +3265,31 @@ class course_request { return $this->properties->collision; } + /** + * Returns the category where this course request should be created + * + * Note that we don't check here that user has a capability to view + * hidden categories if he has capabilities 'moodle/site:approvecourse' and + * 'moodle/course:changecategory' + * + * @return coursecat + */ + public function get_category() { + global $CFG; + require_once($CFG->libdir.'/coursecatlib.php'); + // If the category is not set, if the current user does not have the rights to change the category, or if the + // category does not exist, we set the default category to the course to be approved. + // The system level is used because the capability moodle/site:approvecourse is based on a system level. + if (empty($this->properties->category) || !has_capability('moodle/course:changecategory', context_system::instance()) || + (!$category = coursecat::get($this->properties->category, IGNORE_MISSING, true))) { + $category = coursecat::get($CFG->defaultrequestcategory, IGNORE_MISSING, true); + } + if (!$category) { + $category = coursecat::get_default(); + } + return $category; + } + /** * This function approves the request turning it into a course * @@ -3287,18 +3312,9 @@ class course_request { unset($data->reason); unset($data->requester); - // If the category is not set, if the current user does not have the rights to change the category, or if the - // category does not exist, we set the default category to the course to be approved. - // The system level is used because the capability moodle/site:approvecourse is based on a system level. - if (empty($data->category) || !has_capability('moodle/course:changecategory', context_system::instance()) || - (!$category = get_course_category($data->category))) { - $category = get_course_category($CFG->defaultrequestcategory); - } - // Set category + $category = $this->get_category(); $data->category = $category->id; - $data->sortorder = $category->sortorder; // place as the first in category - // Set misc settings $data->requested = 1; diff --git a/course/pending.php b/course/pending.php index 3d6cb18c716..eaf4c285fc1 100644 --- a/course/pending.php +++ b/course/pending.php @@ -110,23 +110,14 @@ if (empty($pending)) { // Check here for shortname collisions and warn about them. $course->check_shortname_collision(); - // Retreiving category name. - // If the category was not set (can happen after upgrade) or if the user does not have the capability - // to change the category, we fallback on the default one. - // Else, the category proposed is fetched, but we fallback on the default one if we can't find it. - // It is just a matter of displaying the right information because the logic when approving the category - // proceeds the same way. The system context level is used as moodle/site:approvecourse uses it. - if (empty($course->category) || !has_capability('moodle/course:changecategory', context_system::instance()) || - (!$category = get_course_category($course->category))) { - $category = get_course_category($CFG->defaultrequestcategory); - } + $category = $course->get_category(); $row = array(); $row[] = format_string($course->shortname); $row[] = format_string($course->fullname); $row[] = fullname($course->get_requester()); $row[] = $course->summary; - $row[] = format_string($category->name); + $row[] = $category->get_formatted_name(); $row[] = format_string($course->reason); $row[] = $OUTPUT->single_button(new moodle_url($baseurl, array('approve' => $course->id, 'sesskey' => sesskey())), get_string('approve'), 'get') . $OUTPUT->single_button(new moodle_url($baseurl, array('reject' => $course->id)), get_string('rejectdots'), 'get'); diff --git a/lib/datalib.php b/lib/datalib.php index a8281f478d1..f75be8dad67 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1026,48 +1026,6 @@ function get_all_subcategories($catid) { return $subcats; } -/** - * Return specified category, default if given does not exist - * - * @global object - * @uses MAX_COURSES_IN_CATEGORY - * @uses CONTEXT_COURSECAT - * @uses SYSCONTEXTID - * @param int $catid course category id - * @return object caregory - */ -function get_course_category($catid=0) { - global $DB; - - $category = false; - - if (!empty($catid)) { - $category = $DB->get_record('course_categories', array('id'=>$catid)); - } - - if (!$category) { - // the first category is considered default for now - if ($category = $DB->get_records('course_categories', null, 'sortorder', '*', 0, 1)) { - $category = reset($category); - - } else { - $cat = new stdClass(); - $cat->name = get_string('miscellaneous'); - $cat->depth = 1; - $cat->sortorder = MAX_COURSES_IN_CATEGORY; - $cat->timemodified = time(); - $catid = $DB->insert_record('course_categories', $cat); - // make sure category context exists - context_coursecat::instance($catid); - mark_context_dirty('/'.SYSCONTEXTID); - fix_course_sortorder(); // Required to build course_categories.depth and .path. - $category = $DB->get_record('course_categories', array('id'=>$catid)); - } - } - - return $category; -} - /** * Fixes course category and course sortorder, also verifies category and course parents and paths. * (circular references are not fixed) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index d31fe9185db..3277d43ec54 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -3616,3 +3616,58 @@ function course_category_show($category) { coursecat::get($category->id)->show(); } + +/** + * Return specified category, default if given does not exist + * + * This function is deprecated. + * To get the category with the specified it please use: + * coursecat::get($catid, IGNORE_MISSING); + * or + * coursecat::get($catid, MUST_EXIST); + * + * To get the first available category please use + * coursecat::get_default(); + * + * class coursecat will also make sure that at least one category exists in DB + * + * @deprecated since 2.5 + * @see coursecat::get() + * @see coursecat::get_default() + * + * @param int $catid course category id + * @return object caregory + */ +function get_course_category($catid=0) { + global $DB; + + debugging('Function get_course_category() is deprecated. Please use coursecat::get(), see phpdocs for more details'); + + $category = false; + + if (!empty($catid)) { + $category = $DB->get_record('course_categories', array('id'=>$catid)); + } + + if (!$category) { + // the first category is considered default for now + if ($category = $DB->get_records('course_categories', null, 'sortorder', '*', 0, 1)) { + $category = reset($category); + + } else { + $cat = new stdClass(); + $cat->name = get_string('miscellaneous'); + $cat->depth = 1; + $cat->sortorder = MAX_COURSES_IN_CATEGORY; + $cat->timemodified = time(); + $catid = $DB->insert_record('course_categories', $cat); + // make sure category context exists + context_coursecat::instance($catid); + mark_context_dirty('/'.SYSCONTEXTID); + fix_course_sortorder(); // Required to build course_categories.depth and .path. + $category = $DB->get_record('course_categories', array('id'=>$catid)); + } + } + + return $category; +} diff --git a/lib/upgrade.txt b/lib/upgrade.txt index ed1721dcbf3..d61587668d5 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -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() + course_category_show(), get_course_category() YUI changes: * M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp