MDL-38147 deprecated get_categories(), change usage to coursecat

This commit is contained in:
Marina Glancy
2013-03-25 13:23:06 +11:00
parent 8db5dcb7cd
commit e1d54562ce
5 changed files with 129 additions and 133 deletions
+3 -2
View File
@@ -1,6 +1,7 @@
<?php
include_once($CFG->dirroot . '/course/lib.php');
include_once($CFG->libdir . '/coursecatlib.php');
class block_course_list extends block_list {
function init() {
@@ -53,11 +54,11 @@ class block_course_list extends block_list {
}
}
$categories = get_categories("0"); // Parent = 0 ie top-level categories only
$categories = coursecat::get(0)->get_children(); // Parent = 0 ie top-level categories only
if ($categories) { //Check we have categories
if (count($categories) > 1 || (count($categories) == 1 && $DB->count_records('course') > 200)) { // Just print top level category links
foreach ($categories as $category) {
$categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id)));
$categoryname = $category->get_formatted_name();
$linkcss = $category->visible ? "" : " class=\"dimmed\" ";
$this->content->items[]="<a $linkcss href=\"$CFG->wwwroot/course/category.php?id=$category->id\">".$icon . $categoryname . "</a>";
}
+34 -61
View File
@@ -52,18 +52,17 @@ $movedowncat = optional_param('movedowncat', 0, PARAM_INT);
require_login();
// Retrieve coursecat object
// This will also make sure that category is accessible and create default category if missing
$coursecat = coursecat::get($id);
if ($id) {
$PAGE->set_category_by_id($id);
$PAGE->set_url(new moodle_url('/course/manage.php', array('id' => $id)));
// This is sure to be the category context.
$context = $PAGE->context;
// And the object has been loaded for us no need for another DB call.
$category = $PAGE->category;
if (!can_edit_in_category($category->id)) {
redirect(new moodle_url('/course/category.php', array('id' => $category->id)));
}
if (!$category->visible) {
require_capability('moodle/category:viewhiddencategories', $context);
if (!can_edit_in_category($coursecat->id)) {
redirect(new moodle_url('/course/category.php', array('id' => $coursecat->id)));
}
} else {
$context = context_system::instance();
@@ -76,22 +75,6 @@ if ($id) {
$canmanage = has_capability('moodle/category:manage', $context);
// Check the default category exists.
if (!$id && !$DB->record_exists('course_categories', array('parent' => 0))) {
// No category yet! Try and make one.
$tempcat = new stdClass;
$tempcat->name = get_string('miscellaneous');
$tempcat->id = $DB->insert_record('course_categories', $tempcat);
// Fetch the context to ensure it is created.
context_coursecat::instance($tempcat->id);
mark_context_dirty('/'.SYSCONTEXTID);
// Required to build course_categories.depth and categories.path.
fix_course_sortorder();
set_config('defaultrequestcategory', $tempcat->id);
// Unset the temp category. We no longer need it.
unset($tempcat);
}
// Process any category actions.
if (!empty($deletecat) and confirm_sesskey()) {
// Delete a category.
@@ -153,13 +136,13 @@ if (!empty($movecat) and ($movetocat >= 0) and confirm_sesskey()) {
// Hide or show a category.
if ($hidecat and confirm_sesskey()) {
$coursecat = coursecat::get($hidecat);
require_capability('moodle/category:manage', get_category_or_system_context($coursecat->parent));
$coursecat->hide();
$cattohide = coursecat::get($hidecat);
require_capability('moodle/category:manage', get_category_or_system_context($cattohide->parent));
$cattohide->hide();
} else if ($showcat and confirm_sesskey()) {
$coursecat = coursecat::get($showcat);
require_capability('moodle/category:manage', get_category_or_system_context($coursecat->parent));
$coursecat->show();
$cattoshow = coursecat::get($showcat);
require_capability('moodle/category:manage', get_category_or_system_context($cattoshow->parent));
$cattoshow->show();
}
if ((!empty($moveupcat) or !empty($movedowncat)) and confirm_sesskey()) {
@@ -194,13 +177,13 @@ if ((!empty($moveupcat) or !empty($movedowncat)) and confirm_sesskey()) {
fix_course_sortorder();
}
if (isset($category) && $canmanage && $resort && confirm_sesskey()) {
if ($coursecat->id && $canmanage && $resort && confirm_sesskey()) {
// Resort the category.
if ($courses = get_courses($category->id, '', 'c.id,c.fullname,c.sortorder')) {
if ($courses = get_courses($coursecat->id, '', 'c.id,c.fullname,c.sortorder')) {
collatorlib::asort_objects_by_property($courses, 'fullname', collatorlib::SORT_NATURAL);
$i = 1;
foreach ($courses as $course) {
$DB->set_field('course', 'sortorder', $category->sortorder + $i, array('id' => $course->id));
$DB->set_field('course', 'sortorder', $coursecat->sortorder + $i, array('id' => $course->id));
$i++;
}
// This should not be needed but we do it just to be safe.
@@ -307,11 +290,8 @@ if (can_edit_in_category()) {
$PAGE->navbar->add($settingsnode->text, $settingsnode->action);
}
} else {
// If we get here then they must have arrived here using a specific category
// within which they can manage.
// We can safetly assume $category is set.
$site = get_site();
$PAGE->set_title("$site->shortname: $category->name");
$PAGE->set_title("$site->shortname: $coursecat->name");
$PAGE->set_heading($site->fullname);
$PAGE->set_button(print_course_search('', true, 'navbar'));
}
@@ -321,7 +301,7 @@ $displaylist[0] = get_string('top');
// Start output.
echo $OUTPUT->header();
if (!isset($category)) {
if (!$coursecat->id) {
// Print out the categories with all the knobs.
$table = new html_table;
$table->id = 'coursecategories';
@@ -340,12 +320,12 @@ if (!isset($category)) {
);
$table->data = array();
print_category_edit($table, null);
print_category_edit($table, $coursecat);
echo html_writer::table($table);
} else {
// Print the category selector.
$select = new single_select(new moodle_url('/course/manage.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
$select = new single_select(new moodle_url('/course/manage.php'), 'id', $displaylist, $coursecat->id, null, 'switchcategory');
$select->set_label(get_string('categories').':');
echo html_writer::start_tag('div', array('class' => 'categorypicker'));
@@ -372,7 +352,7 @@ if ($canmanage) {
echo $OUTPUT->container_end();
}
if (isset($category)) {
if ($coursecat->id) {
// Print out all the sub-categories (plain mode).
// In order to view hidden subcategories the user must have the viewhiddencategories.
// capability in the current category..
@@ -391,7 +371,7 @@ if (isset($category)) {
ctx.contextlevel = :contextlevel
$categorywhere
ORDER BY cc.sortorder ASC";
$subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id, 'contextlevel' => CONTEXT_COURSECAT));
$subcategories = $DB->get_recordset_sql($sql, array('parentid' => $coursecat->id, 'contextlevel' => CONTEXT_COURSECAT));
// Prepare a table to display the sub categories.
$table = new html_table;
$table->attributes = array(
@@ -420,7 +400,7 @@ if (isset($category)) {
echo html_writer::table($table);
}
$courses = get_courses_page($category->id, 'c.sortorder ASC',
$courses = get_courses_page($coursecat->id, 'c.sortorder ASC',
'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
$totalcount, $page*$perpage, $perpage);
$numcourses = count($courses);
@@ -581,8 +561,8 @@ if ($canmanage and $numcourses > 1) {
if (has_capability('moodle/course:create', $context)) {
// Print button to create a new course.
$url = new moodle_url('/course/edit.php');
if (isset($category)) {
$url->params(array('category' => $category->id, 'returnto' => 'catmanage'));
if ($coursecat->id) {
$url->params(array('category' => $coursecat->id, 'returnto' => 'catmanage'));
} else {
$url->params(array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcatmanage'));
}
@@ -602,12 +582,12 @@ echo $OUTPUT->footer();
* Recursive function to print all the categories ready for editing.
*
* @param html_table $table The table to add data to.
* @param stdClass $category The category to render
* @param coursecat $category The category to render
* @param int $depth The depth of the category.
* @param bool $up True if this category can be moved up.
* @param bool $down True if this category can be moved down.
*/
function print_category_edit(html_table $table, $category, $depth=-1, $up=false, $down=false) {
function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false) {
global $OUTPUT;
static $str = null;
@@ -625,22 +605,20 @@ function print_category_edit(html_table $table, $category, $depth=-1, $up=false,
$str->spacer = $OUTPUT->spacer().' ';
}
if (!empty($category)) {
if ($category->id) {
if (!isset($category->context)) {
$category->context = context_coursecat::instance($category->id);
}
$categorycontext = context_coursecat::instance($category->id);
$attributes = array();
$attributes['class'] = $category->visible ? '' : 'dimmed';
$attributes['title'] = $str->edit;
$categoryurl = new moodle_url('/course/manage.php', array('id' => $category->id, 'sesskey' => sesskey()));
$categoryname = format_string($category->name, true, array('context' => $category->context));
$categoryname = $category->get_formatted_name();
$categorypadding = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $depth);
$categoryname = $categorypadding . html_writer::link($categoryurl, $categoryname, $attributes);
$icons = array();
if (has_capability('moodle/category:manage', $category->context)) {
if (has_capability('moodle/category:manage', $categorycontext)) {
// Edit category.
$icons[] = $OUTPUT->action_icon(
new moodle_url('/course/editcategory.php', array('id' => $category->id)),
@@ -668,9 +646,9 @@ function print_category_edit(html_table $table, $category, $depth=-1, $up=false,
);
}
// Cohorts.
if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $category->context)) {
if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $categorycontext)) {
$icons[] = $OUTPUT->action_icon(
new moodle_url('/cohort/index.php', array('contextid' => $category->context->id)),
new moodle_url('/cohort/index.php', array('contextid' => $categorycontext->id)),
new pix_icon('t/cohort', $str->cohorts, 'moodle', array('class' => 'iconsmall')),
null, array('title' => $str->cohorts)
);
@@ -697,7 +675,7 @@ function print_category_edit(html_table $table, $category, $depth=-1, $up=false,
}
$actions = '';
if (has_capability('moodle/category:manage', $category->context)) {
if (has_capability('moodle/category:manage', $categorycontext)) {
$popupurl = new moodle_url("manage.php?movecat=$category->id&sesskey=".sesskey());
$tempdisplaylist = array(0 => get_string('top')) + coursecat::make_categories_list('moodle/category:manage', $category->id);
$select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id");
@@ -715,14 +693,9 @@ function print_category_edit(html_table $table, $category, $depth=-1, $up=false,
// Actions.
new html_table_cell($actions)
));
// Get the subcategories to be printed.
$categories = get_categories($category->id);
} else {
$categories = get_categories(0);
}
if ($categories) {
if ($categories = $category->get_children()) {
// Print all the children recursively.
$countcats = count($categories);
$count = 0;
-69
View File
@@ -934,75 +934,6 @@ function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $record
return $courses;
}
/**
* Returns a sorted list of categories. Each category object has a context
* property that is a context object.
*
* When asking for $parent='none' it will return all the categories, regardless
* of depth. Wheen asking for a specific parent, the default is to return
* a "shallow" resultset. Pass false to $shallow and it will return all
* the child categories as well.
*
* @global object
* @uses CONTEXT_COURSECAT
* @param string $parent The parent category if any
* @param string $sort the sortorder
* @param bool $shallow - set to false to get the children too
* @return array of categories
*/
function get_categories($parent='none', $sort=NULL, $shallow=true) {
global $DB;
if ($sort === NULL) {
$sort = 'ORDER BY cc.sortorder ASC';
} elseif ($sort ==='') {
// leave it as empty
} else {
$sort = "ORDER BY $sort";
}
list($ccselect, $ccjoin) = context_instance_preload_sql('cc.id', CONTEXT_COURSECAT, 'ctx');
if ($parent === 'none') {
$sql = "SELECT cc.* $ccselect
FROM {course_categories} cc
$ccjoin
$sort";
$params = array();
} elseif ($shallow) {
$sql = "SELECT cc.* $ccselect
FROM {course_categories} cc
$ccjoin
WHERE cc.parent=?
$sort";
$params = array($parent);
} else {
$sql = "SELECT cc.* $ccselect
FROM {course_categories} cc
$ccjoin
JOIN {course_categories} ccp
ON ((cc.parent = ccp.id) OR (cc.path LIKE ".$DB->sql_concat('ccp.path',"'/%'")."))
WHERE ccp.id=?
$sort";
$params = array($parent);
}
$categories = array();
$rs = $DB->get_recordset_sql($sql, $params);
foreach($rs as $cat) {
context_instance_preload($cat);
$catcontext = context_coursecat::instance($cat->id);
if ($cat->visible || has_capability('moodle/category:viewhiddencategories', $catcontext)) {
$categories[$cat->id] = $cat;
}
}
$rs->close();
return $categories;
}
/**
* Fixes course category and course sortorder, also verifies category and course parents and paths.
* (circular references are not fixed)
+91
View File
@@ -3786,3 +3786,94 @@ function get_child_categories($parentid) {
}
return $rv;
}
/**
* Returns a sorted list of categories.
*
* When asking for $parent='none' it will return all the categories, regardless
* of depth. Wheen asking for a specific parent, the default is to return
* a "shallow" resultset. Pass false to $shallow and it will return all
* the child categories as well.
*
* @deprecated since 2.5
*
* This function is deprecated. Use appropriate functions from class coursecat.
* Examples:
*
* coursecat::get($categoryid)->get_children()
* - returns all children of the specified category as instances of class
* coursecat, which means on each of them method get_children() can be called again
*
* coursecat::get($categoryid)->get_children(array('recursive' => true))
* - returns all children of the specified category and all subcategories
*
* coursecat::get(0)->get_children(array('recursive' => true))
* - returns all categories defined in the system
*
* Sort fields can be specified, see phpdocs to {@link coursecat::get_children()}
*
* Also see functions {@link coursecat::get_children_count()}, {@link coursecat::count_all()},
* {@link coursecat::get_default()}
*
* The code of this deprecated function is left as it is because coursecat::get_children()
* returns categories as instances of coursecat and not stdClass
*
* @param string $parent The parent category if any
* @param string $sort the sortorder
* @param bool $shallow - set to false to get the children too
* @return array of categories
*/
function get_categories($parent='none', $sort=NULL, $shallow=true) {
global $DB;
debugging('Function get_categories() is deprecated. Please use coursecat::get_children(). See phpdocs for more details',
DEBUG_DEVELOPER);
if ($sort === NULL) {
$sort = 'ORDER BY cc.sortorder ASC';
} elseif ($sort ==='') {
// leave it as empty
} else {
$sort = "ORDER BY $sort";
}
list($ccselect, $ccjoin) = context_instance_preload_sql('cc.id', CONTEXT_COURSECAT, 'ctx');
if ($parent === 'none') {
$sql = "SELECT cc.* $ccselect
FROM {course_categories} cc
$ccjoin
$sort";
$params = array();
} elseif ($shallow) {
$sql = "SELECT cc.* $ccselect
FROM {course_categories} cc
$ccjoin
WHERE cc.parent=?
$sort";
$params = array($parent);
} else {
$sql = "SELECT cc.* $ccselect
FROM {course_categories} cc
$ccjoin
JOIN {course_categories} ccp
ON ((cc.parent = ccp.id) OR (cc.path LIKE ".$DB->sql_concat('ccp.path',"'/%'")."))
WHERE ccp.id=?
$sort";
$params = array($parent);
}
$categories = array();
$rs = $DB->get_recordset_sql($sql, $params);
foreach($rs as $cat) {
context_instance_preload($cat);
$catcontext = context_coursecat::instance($cat->id);
if ($cat->visible || has_capability('moodle/category:viewhiddencategories', $catcontext)) {
$categories[$cat->id] = $cat;
}
}
$rs->close();
return $categories;
}
+1 -1
View File
@@ -38,7 +38,7 @@ information provided here is intended especially for developers.
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(), create_course_category(), get_all_subcategories(),
get_child_categories()
get_child_categories(), get_categories()
YUI changes:
* M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp