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

This commit is contained in:
Marina Glancy 2013-02-21 10:37:52 +11:00
parent b28bb7e8c7
commit 4e0b6025ad
15 changed files with 117 additions and 175 deletions

View File

@ -85,9 +85,9 @@ class cohort_edit_form extends moodleform {
}
protected function get_category_options($currentcontextid) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage');
global $CFG;
require_once($CFG->libdir. '/coursecatlib.php');
$displaylist = coursecat::make_categories_list('moodle/cohort:manage');
$options = array();
$syscontext = context_system::instance();
if (has_capability('moodle/cohort:manage', $syscontext)) {

View File

@ -26,6 +26,7 @@
require_once("../config.php");
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->libdir.'/textlib.class.php');
require_once($CFG->libdir. '/coursecatlib.php');
$id = required_param('id', PARAM_INT); // Category id
$page = optional_param('page', 0, PARAM_INT); // which page to show
@ -69,9 +70,7 @@ $PAGE->set_button(print_course_search('', true, 'navbar'));
echo $OUTPUT->header();
/// Print the category selector
$displaylist = array();
$notused = array();
make_categories_list($displaylist, $notused);
$displaylist = coursecat::make_categories_list();
echo '<div class="categorypicker">';
$select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');

View File

@ -97,9 +97,8 @@ class course_completion_form extends moodleform {
}
// Get category list
$list = array();
$parents = array();
make_categories_list($list, $parents);
require_once($CFG->libdir. '/coursecatlib.php');
$list = coursecat::make_categories_list();
// Get course list for select box
$selectbox = array();

View File

@ -6,6 +6,7 @@ if (!defined('MOODLE_INTERNAL')) {
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/questionlib.php');
require_once($CFG->libdir. '/coursecatlib.php');
class delete_category_form extends moodleform {
@ -62,10 +63,8 @@ class delete_category_form extends moodleform {
if ($containscategories || $containsquestions) {
$testcaps[] = 'moodle/category:manage';
}
$displaylist = array();
$notused = array();
if (!empty($testcaps)) {
make_categories_list($displaylist, $notused, $testcaps, $category->id);
$displaylist = coursecat::make_categories_list($testcaps, $category->id);
}
/// Now build the options.

View File

@ -4,6 +4,7 @@ defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->libdir. '/coursecatlib.php');
class course_edit_form extends moodleform {
protected $course;
@ -48,9 +49,7 @@ class course_edit_form extends moodleform {
// verify permissions to change course category or keep current
if (empty($course->id)) {
if (has_capability('moodle/course:create', $categorycontext)) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/course:create');
$displaylist = coursecat::make_categories_list('moodle/course:create');
$mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->addHelpButton('category', 'category');
$mform->setDefault('category', $category->id);
@ -61,12 +60,10 @@ class course_edit_form extends moodleform {
}
} else {
if (has_capability('moodle/course:changecategory', $coursecontext)) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/course:create');
$displaylist = coursecat::make_categories_list('moodle/course:create');
if (!isset($displaylist[$course->category])) {
//always keep current
$displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
$displaylist[$course->category] = coursecat::get($course->category)->get_formatted_name();
}
$mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->addHelpButton('category', 'category');

View File

@ -4,6 +4,7 @@ if (!defined('MOODLE_INTERNAL')) {
}
require_once ($CFG->dirroot.'/course/moodleform_mod.php');
require_once ($CFG->libdir.'/coursecatlib.php');
class editcategory_form extends moodleform {
// form definition
@ -18,17 +19,16 @@ class editcategory_form extends moodleform {
if (has_capability('moodle/category:manage', get_system_context()) || $category->parent == 0) {
$options[0] = get_string('top');
}
$parents = array();
if ($category->id) {
// Editing an existing category.
make_categories_list($options, $parents, 'moodle/category:manage', $category->id);
$options += coursecat::make_categories_list('moodle/category:manage', $category->id);
if (empty($options[$category->parent])) {
$options[$category->parent] = $DB->get_field('course_categories', 'name', array('id'=>$category->parent));
}
$strsubmit = get_string('savechanges');
} else {
// Making a new category
make_categories_list($options, $parents, 'moodle/category:manage');
$options += coursecat::make_categories_list('moodle/category:manage');
$strsubmit = get_string('createcategory');
}

View File

@ -1249,99 +1249,6 @@ function get_child_categories($parentid) {
}
}
/**
* This function recursively travels the categories, building up a nice list
* for display. It also makes an array that list all the parents for each
* category.
*
* For example, if you have a tree of categories like:
* Miscellaneous (id = 1)
* Subcategory (id = 2)
* Sub-subcategory (id = 4)
* Other category (id = 3)
* Then after calling this function you will have
* $list = array(1 => 'Miscellaneous', 2 => 'Miscellaneous / Subcategory',
* 4 => 'Miscellaneous / Subcategory / Sub-subcategory',
* 3 => 'Other category');
* $parents = array(2 => array(1), 4 => array(1, 2));
*
* If you specify $requiredcapability, then only categories where the current
* user has that capability will be added to $list, although all categories
* will still be added to $parents, and if you only have $requiredcapability
* in a child category, not the parent, then the child catgegory will still be
* included.
*
* If you specify the option $excluded, then that category, and all its children,
* are omitted from the tree. This is useful when you are doing something like
* moving categories, where you do not want to allow people to move a category
* to be the child of itself.
*
* @param array $list For output, accumulates an array categoryid => full category path name
* @param array $parents For output, accumulates an array categoryid => list of parent category ids.
* @param string/array $requiredcapability if given, only categories where the current
* user has this capability will be added to $list. Can also be an array of capabilities,
* in which case they are all required.
* @param integer $excludeid Omit this category and its children from the lists built.
* @param object $category Build the tree starting at this category - otherwise starts at the top level.
* @param string $path For internal use, as part of recursive calls.
*/
function make_categories_list(&$list, &$parents, $requiredcapability = '',
$excludeid = 0, $category = NULL, $path = "") {
// initialize the arrays if needed
if (!is_array($list)) {
$list = array();
}
if (!is_array($parents)) {
$parents = array();
}
if (empty($category)) {
// Start at the top level.
$category = new stdClass;
$category->id = 0;
} else {
// This is the excluded category, don't include it.
if ($excludeid > 0 && $excludeid == $category->id) {
return;
}
$context = context_coursecat::instance($category->id);
$categoryname = format_string($category->name, true, array('context' => $context));
// Update $path.
if ($path) {
$path = $path.' / '.$categoryname;
} else {
$path = $categoryname;
}
// Add this category to $list, if the permissions check out.
if (empty($requiredcapability)) {
$list[$category->id] = $path;
} else {
$requiredcapability = (array)$requiredcapability;
if (has_all_capabilities($requiredcapability, $context)) {
$list[$category->id] = $path;
}
}
}
// Add all the children recursively, while updating the parents array.
if ($categories = get_child_categories($category->id)) {
foreach ($categories as $cat) {
if (!empty($category->id)) {
if (isset($parents[$category->id])) {
$parents[$cat->id] = $parents[$category->id];
}
$parents[$cat->id][] = $category->id;
}
make_categories_list($list, $parents, $requiredcapability, $excludeid, $cat, $path);
}
}
}
/**
* This function generates a structured array of courses and categories.
*
@ -1423,10 +1330,6 @@ function print_whole_category_list($category=NULL, $displaylist=NULL, $parentsli
return;
}
if (!$displaylist) {
make_categories_list($displaylist, $parentslist);
}
if (!$categorycourses) {
if ($category) {
$categorycourses = get_category_courses_array($category->id);
@ -1497,18 +1400,19 @@ function get_category_courses_array_recursively(array &$flattened, $category) {
}
/**
* This function will return $options array for html_writer::select(), with whitespace to denote nesting.
* Returns full course categories trees to be used in html_writer::select()
*
* Calls {@link coursecat::make_categories_list()} to build the tree and
* adds whitespace to denote nesting
*
* @return array array mapping coursecat id to the display name
*/
function make_categories_options() {
make_categories_list($cats,$parents);
global $CFG;
require_once($CFG->libdir. '/coursecatlib.php');
$cats = coursecat::make_categories_list();
foreach ($cats as $key => $value) {
if (array_key_exists($key,$parents)) {
if ($indent = count($parents[$key])) {
for ($i = 0; $i < $indent; $i++) {
$cats[$key] = '&nbsp;'.$cats[$key];
}
}
}
$cats[$key] = str_repeat('&nbsp;', coursecat::get($key)->depth - 1). $value;
}
return $cats;
}

View File

@ -323,9 +323,6 @@ if (can_edit_in_category()) {
$PAGE->set_button(print_course_search('', true, 'navbar'));
}
$parentlist = array();
$displaylist = array();
make_categories_list($displaylist, $parentlist);
$displaylist[0] = get_string('top');
// Start output.
@ -350,7 +347,7 @@ if (!isset($category)) {
);
$table->data = array();
print_category_edit($table, null, $displaylist, $parentlist);
print_category_edit($table, null);
echo html_writer::table($table);
} else {
@ -552,9 +549,7 @@ if (!$courses) {
}
if ($abletomovecourses) {
$movetocategories = array();
$notused = array();
make_categories_list($movetocategories, $notused, 'moodle/category:manage');
$movetocategories = coursecat::make_categories_list('moodle/category:manage');
$movetocategories[$id] = get_string('moveselectedcoursesto');
$cell = new html_table_cell();
@ -615,13 +610,11 @@ echo $OUTPUT->footer();
*
* @param html_table $table The table to add data to.
* @param stdClass $category The category to render
* @param array $displaylist The categories this can be moved to.
* @param array $parentslist An array of categories.
* @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, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
function print_category_edit(html_table $table, $category, $depth=-1, $up=false, $down=false) {
global $OUTPUT;
static $str = null;
@ -712,14 +705,8 @@ function print_category_edit(html_table $table, $category, $displaylist, $parent
$actions = '';
if (has_capability('moodle/category:manage', $category->context)) {
$tempdisplaylist = $displaylist;
unset($tempdisplaylist[$category->id]);
foreach ($parentslist as $key => $parents) {
if (in_array($category->id, $parents)) {
unset($tempdisplaylist[$key]);
}
}
$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");
$select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
$actions = $OUTPUT->render($select);
@ -757,7 +744,7 @@ function print_category_edit(html_table $table, $category, $displaylist, $parent
$down = $last ? false : true;
$first = false;
print_category_edit($table, $cat, $displaylist, $parentslist, $depth+1, $up, $down);
print_category_edit($table, $cat, $depth+1, $up, $down);
}
}
}

View File

@ -36,6 +36,7 @@ if (!defined('MOODLE_INTERNAL')) {
}
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/coursecatlib.php');
/**
* A form for a user to request a course.
@ -69,9 +70,7 @@ class course_request_form extends moodleform {
$mform->setType('shortname', PARAM_TEXT);
if (!empty($CFG->requestcategoryselection)) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, '');
$displaylist = coursecat::make_categories_list();
$mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->setDefault('category', $CFG->defaultrequestcategory);
$mform->addHelpButton('category', 'category');

View File

@ -24,6 +24,7 @@
require_once("../config.php");
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->libdir.'/coursecatlib.php');
$search = optional_param('search', '', PARAM_RAW); // search words
$page = optional_param('page', 0, PARAM_INT); // which page to show
@ -38,14 +39,8 @@ $modulelist= optional_param('modulelist', '', PARAM_PLUGIN);
// List of minimum capabilities which user need to have for editing/moving course
$capabilities = array('moodle/course:create', 'moodle/category:manage');
// List of category id's in which current user has course:create and category:manage capability.
$usercatlist = array();
// List of parent category id's
$catparentlist = array();
// Populate usercatlist with list of category id's with required capabilities.
make_categories_list($usercatlist, $catparentlist, $capabilities);
// Populate usercatlist with list of category id's with course:create and category:manage capabilities.
$usercatlist = coursecat::make_categories_list($capabilities);
$search = trim(strip_tags($search)); // trim & clean raw searched string
if ($search) {
@ -109,9 +104,7 @@ if (has_capability('moodle/course:visibility', context_system::instance())) {
}
}
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist);
$displaylist = coursecat::make_categories_list();
$strcourses = new lang_string("courses");
$strsearch = new lang_string("search");

View File

@ -108,12 +108,7 @@ if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configtext('enrol_database/newcoursecategory', get_string('newcoursecategory', 'enrol_database'), '', ''));
if (!during_initial_install()) {
require_once($CFG->dirroot.'/course/lib.php');
$options = array();
$parentlist = array();
make_categories_list($options, $parentlist);
$settings->add(new admin_setting_configselect('enrol_database/defaultcategory', get_string('defaultcategory', 'enrol_database'), get_string('defaultcategory_desc', 'enrol_database'), 1, $options));
unset($parentlist);
$settings->add(new admin_setting_configselect('enrol_database/defaultcategory', get_string('defaultcategory', 'enrol_database'), get_string('defaultcategory_desc', 'enrol_database'), 1, make_categories_options()));
}
$settings->add(new admin_setting_configtext('enrol_database/templatecourse', get_string('templatecourse', 'enrol_database'), get_string('templatecourse_desc', 'enrol_database'), ''));

View File

@ -94,10 +94,7 @@ if ($ADMIN->fulltree) {
$options = $yesno;
$settings->add(new admin_setting_configselect('enrol_ldap/autocreate', get_string('autocreate_key', 'enrol_ldap'), get_string('autocreate', 'enrol_ldap'), 0, $options));
if (!during_initial_install()) {
require_once($CFG->dirroot.'/course/lib.php');
$parentlist = array();
$options = array();
make_categories_list($options, $parentlist);
$options = make_categories_options();
$settings->add(new admin_setting_configselect('enrol_ldap/category', get_string('category_key', 'enrol_ldap'), get_string('category', 'enrol_ldap'), key($options), $options));
}
$settings->add(new admin_setting_configtext_trim_lower('enrol_ldap/template', get_string('template_key', 'enrol_ldap'), get_string('template', 'enrol_ldap'), ''));

View File

@ -3425,3 +3425,77 @@ function update_category_button($categoryid = 0) {
}
return $OUTPUT->single_button(new moodle_url('/course/' . $page, $options), $label, 'get');
}
/**
* This function recursively travels the categories, building up a nice list
* for display. It also makes an array that list all the parents for each
* category.
*
* For example, if you have a tree of categories like:
* Miscellaneous (id = 1)
* Subcategory (id = 2)
* Sub-subcategory (id = 4)
* Other category (id = 3)
* Then after calling this function you will have
* $list = array(1 => 'Miscellaneous', 2 => 'Miscellaneous / Subcategory',
* 4 => 'Miscellaneous / Subcategory / Sub-subcategory',
* 3 => 'Other category');
* $parents = array(2 => array(1), 4 => array(1, 2));
*
* If you specify $requiredcapability, then only categories where the current
* user has that capability will be added to $list, although all categories
* will still be added to $parents, and if you only have $requiredcapability
* in a child category, not the parent, then the child catgegory will still be
* included.
*
* If you specify the option $excluded, then that category, and all its children,
* are omitted from the tree. This is useful when you are doing something like
* moving categories, where you do not want to allow people to move a category
* to be the child of itself.
*
* This function is deprecated! For list of categories use
* coursecat::make_all_categories($requiredcapability, $excludeid, $separator)
* For parents of one particular category use
* coursecat::get($id)->get_parents()
*
* @deprecated since 2.5
*
* @param array $list For output, accumulates an array categoryid => full category path name
* @param array $parents For output, accumulates an array categoryid => list of parent category ids.
* @param string/array $requiredcapability if given, only categories where the current
* user has this capability will be added to $list. Can also be an array of capabilities,
* in which case they are all required.
* @param integer $excludeid Omit this category and its children from the lists built.
* @param object $category Not used
* @param string $path Not used
*/
function make_categories_list(&$list, &$parents, $requiredcapability = '',
$excludeid = 0, $category = NULL, $path = "") {
global $CFG, $DB;
require_once($CFG->libdir.'/coursecatlib.php');
debugging('Global function make_categories_list() is deprecated. Please use '.
'coursecat::make_categories_list() and coursecat::get_parents()',
DEBUG_DEVELOPER);
// For categories list use just this one function:
if (empty($list)) {
$list = array();
}
$list += coursecat::make_categories_list($requiredcapability, $excludeid);
// Building the list of all parents of all categories in the system is highly undesirable and hardly ever needed.
// Usually user needs only parents for one particular category, in which case should be used:
// coursecat::get($categoryid)->get_parents()
if (empty($parents)) {
$parents = array();
}
$all = $DB->get_records_sql('SELECT id, parent FROM {course_categories} ORDER BY sortorder');
foreach ($all as $record) {
if ($record->parent) {
$parents[$record->id] = array_merge($parents[$record->parent], array($record->parent));
} else {
$parents[$record->id] = array();
}
}
}

View File

@ -34,6 +34,8 @@ information provided here is intended especially for developers.
* condition_info:get_condition_user_fields($formatoptions) now accepts the optional
param $formatoptions, that will determine if the field names are processed by
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()
YUI changes:
* M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp

View File

@ -32,11 +32,8 @@ class user_filter_courserole extends user_filter_type {
*/
function get_course_categories() {
global $CFG;
require_once($CFG->dirroot.'/course/lib.php');
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist);
return array(0=> get_string('anycategory', 'filters')) + $displaylist;
require_once($CFG->libdir.'/coursecatlib.php');
return array(0=> get_string('anycategory', 'filters')) + coursecat::make_categories_list();
}
/**