Skip to content

Commit

Permalink
MDL-38147 deprecated get_course_category(), change usage to coursecat
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Mar 25, 2013
1 parent 6e1d1ee commit 2d8a275
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 64 deletions.
36 changes: 26 additions & 10 deletions course/lib.php
Expand Up @@ -3265,6 +3265,31 @@ public function check_shortname_collision($shortnamemark = '[*]') {
return $this->properties->collision; 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 * This function approves the request turning it into a course
* *
Expand All @@ -3287,18 +3312,9 @@ public function approve() {
unset($data->reason); unset($data->reason);
unset($data->requester); 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 // Set category
$category = $this->get_category();
$data->category = $category->id; $data->category = $category->id;
$data->sortorder = $category->sortorder; // place as the first in category

// Set misc settings // Set misc settings
$data->requested = 1; $data->requested = 1;


Expand Down
13 changes: 2 additions & 11 deletions course/pending.php
Expand Up @@ -110,23 +110,14 @@
// Check here for shortname collisions and warn about them. // Check here for shortname collisions and warn about them.
$course->check_shortname_collision(); $course->check_shortname_collision();


// Retreiving category name. $category = $course->get_category();
// 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);
}


$row = array(); $row = array();
$row[] = format_string($course->shortname); $row[] = format_string($course->shortname);
$row[] = format_string($course->fullname); $row[] = format_string($course->fullname);
$row[] = fullname($course->get_requester()); $row[] = fullname($course->get_requester());
$row[] = $course->summary; $row[] = $course->summary;
$row[] = format_string($category->name); $row[] = $category->get_formatted_name();
$row[] = format_string($course->reason); $row[] = format_string($course->reason);
$row[] = $OUTPUT->single_button(new moodle_url($baseurl, array('approve' => $course->id, 'sesskey' => sesskey())), get_string('approve'), 'get') . $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'); $OUTPUT->single_button(new moodle_url($baseurl, array('reject' => $course->id)), get_string('rejectdots'), 'get');
Expand Down
42 changes: 0 additions & 42 deletions lib/datalib.php
Expand Up @@ -1026,48 +1026,6 @@ function get_all_subcategories($catid) {
return $subcats; 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. * Fixes course category and course sortorder, also verifies category and course parents and paths.
* (circular references are not fixed) * (circular references are not fixed)
Expand Down
55 changes: 55 additions & 0 deletions lib/deprecatedlib.php
Expand Up @@ -3616,3 +3616,58 @@ function course_category_show($category) {


coursecat::get($category->id)->show(); 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;
}
2 changes: 1 addition & 1 deletion lib/upgrade.txt
Expand Up @@ -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 * 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(), in lib/coursecatlib.php. The following global functions are deprecated: make_categories_list(),
category_delete_move(), category_delete_full(), move_category(), course_category_hide(), category_delete_move(), category_delete_full(), move_category(), course_category_hide(),
course_category_show() course_category_show(), get_course_category()


YUI changes: YUI changes:
* M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp * M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp
Expand Down

0 comments on commit 2d8a275

Please sign in to comment.