Skip to content

Commit

Permalink
MDL-38147 deprecated make_categories_list(), 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 b28bb7e commit 4e0b602
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 175 deletions.
6 changes: 3 additions & 3 deletions cohort/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public function validation($data, $files) {
}

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)) {
Expand Down
5 changes: 2 additions & 3 deletions course/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,9 +70,7 @@
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');
Expand Down
5 changes: 2 additions & 3 deletions course/completion_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ function definition() {
}

// 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();
Expand Down
5 changes: 2 additions & 3 deletions course/delete_category_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/questionlib.php');
require_once($CFG->libdir. '/coursecatlib.php');

class delete_category_form extends moodleform {

Expand Down Expand Up @@ -62,10 +63,8 @@ function definition() {
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.
Expand Down
11 changes: 4 additions & 7 deletions course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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;
Expand Down Expand Up @@ -48,9 +49,7 @@ function definition() {
// 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);
Expand All @@ -61,12 +60,10 @@ function definition() {
}
} 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');
Expand Down
6 changes: 3 additions & 3 deletions course/editcategory_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
}

require_once ($CFG->dirroot.'/course/moodleform_mod.php');
require_once ($CFG->libdir.'/coursecatlib.php');
class editcategory_form extends moodleform {

// form definition
Expand All @@ -18,17 +19,16 @@ function definition() {
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');
}

Expand Down
116 changes: 10 additions & 106 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 5 additions & 18 deletions course/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,6 @@
$PAGE->set_button(print_course_search('', true, 'navbar'));
}

$parentlist = array();
$displaylist = array();
make_categories_list($displaylist, $parentlist);
$displaylist[0] = get_string('top');

// Start output.
Expand All @@ -350,7 +347,7 @@
);
$table->data = array();

print_category_edit($table, null, $displaylist, $parentlist);
print_category_edit($table, null);

echo html_writer::table($table);
} else {
Expand Down Expand Up @@ -552,9 +549,7 @@
}

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();
Expand Down Expand Up @@ -615,13 +610,11 @@
*
* @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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
5 changes: 2 additions & 3 deletions course/request_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
}

require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/coursecatlib.php');

/**
* A form for a user to request a course.
Expand Down Expand Up @@ -69,9 +70,7 @@ function definition() {
$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');
Expand Down
15 changes: 4 additions & 11 deletions course/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,14 +39,8 @@
// 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) {
Expand Down Expand Up @@ -109,9 +104,7 @@
}
}

$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist);
$displaylist = coursecat::make_categories_list();

$strcourses = new lang_string("courses");
$strsearch = new lang_string("search");
Expand Down
Loading

0 comments on commit 4e0b602

Please sign in to comment.