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
Expand Up @@ -85,9 +85,9 @@ public function validation($data, $files) {
} }


protected function get_category_options($currentcontextid) { protected function get_category_options($currentcontextid) {
$displaylist = array(); global $CFG;
$parentlist = array(); require_once($CFG->libdir. '/coursecatlib.php');
make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage'); $displaylist = coursecat::make_categories_list('moodle/cohort:manage');
$options = array(); $options = array();
$syscontext = context_system::instance(); $syscontext = context_system::instance();
if (has_capability('moodle/cohort:manage', $syscontext)) { if (has_capability('moodle/cohort:manage', $syscontext)) {
Expand Down
5 changes: 2 additions & 3 deletions course/category.php
Expand Up @@ -26,6 +26,7 @@
require_once("../config.php"); require_once("../config.php");
require_once($CFG->dirroot.'/course/lib.php'); require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->libdir.'/textlib.class.php'); require_once($CFG->libdir.'/textlib.class.php');
require_once($CFG->libdir. '/coursecatlib.php');


$id = required_param('id', PARAM_INT); // Category id $id = required_param('id', PARAM_INT); // Category id
$page = optional_param('page', 0, PARAM_INT); // which page to show $page = optional_param('page', 0, PARAM_INT); // which page to show
Expand Down Expand Up @@ -69,9 +70,7 @@
echo $OUTPUT->header(); echo $OUTPUT->header();


/// Print the category selector /// Print the category selector
$displaylist = array(); $displaylist = coursecat::make_categories_list();
$notused = array();
make_categories_list($displaylist, $notused);


echo '<div class="categorypicker">'; echo '<div class="categorypicker">';
$select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory'); $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
Expand Up @@ -97,9 +97,8 @@ function definition() {
} }


// Get category list // Get category list
$list = array(); require_once($CFG->libdir. '/coursecatlib.php');
$parents = array(); $list = coursecat::make_categories_list();
make_categories_list($list, $parents);


// Get course list for select box // Get course list for select box
$selectbox = array(); $selectbox = array();
Expand Down
5 changes: 2 additions & 3 deletions course/delete_category_form.php
Expand Up @@ -6,6 +6,7 @@


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


class delete_category_form extends moodleform { class delete_category_form extends moodleform {


Expand Down Expand Up @@ -62,10 +63,8 @@ function definition() {
if ($containscategories || $containsquestions) { if ($containscategories || $containsquestions) {
$testcaps[] = 'moodle/category:manage'; $testcaps[] = 'moodle/category:manage';
} }
$displaylist = array();
$notused = array();
if (!empty($testcaps)) { if (!empty($testcaps)) {
make_categories_list($displaylist, $notused, $testcaps, $category->id); $displaylist = coursecat::make_categories_list($testcaps, $category->id);
} }


/// Now build the options. /// Now build the options.
Expand Down
11 changes: 4 additions & 7 deletions course/edit_form.php
Expand Up @@ -4,6 +4,7 @@


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


class course_edit_form extends moodleform { class course_edit_form extends moodleform {
protected $course; protected $course;
Expand Down Expand Up @@ -48,9 +49,7 @@ function definition() {
// verify permissions to change course category or keep current // verify permissions to change course category or keep current
if (empty($course->id)) { if (empty($course->id)) {
if (has_capability('moodle/course:create', $categorycontext)) { if (has_capability('moodle/course:create', $categorycontext)) {
$displaylist = array(); $displaylist = coursecat::make_categories_list('moodle/course:create');
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/course:create');
$mform->addElement('select', 'category', get_string('category'), $displaylist); $mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->addHelpButton('category', 'category'); $mform->addHelpButton('category', 'category');
$mform->setDefault('category', $category->id); $mform->setDefault('category', $category->id);
Expand All @@ -61,12 +60,10 @@ function definition() {
} }
} else { } else {
if (has_capability('moodle/course:changecategory', $coursecontext)) { if (has_capability('moodle/course:changecategory', $coursecontext)) {
$displaylist = array(); $displaylist = coursecat::make_categories_list('moodle/course:create');
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/course:create');
if (!isset($displaylist[$course->category])) { if (!isset($displaylist[$course->category])) {
//always keep current //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->addElement('select', 'category', get_string('category'), $displaylist);
$mform->addHelpButton('category', 'category'); $mform->addHelpButton('category', 'category');
Expand Down
6 changes: 3 additions & 3 deletions course/editcategory_form.php
Expand Up @@ -4,6 +4,7 @@
} }


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


// form definition // form definition
Expand All @@ -18,17 +19,16 @@ function definition() {
if (has_capability('moodle/category:manage', get_system_context()) || $category->parent == 0) { if (has_capability('moodle/category:manage', get_system_context()) || $category->parent == 0) {
$options[0] = get_string('top'); $options[0] = get_string('top');
} }
$parents = array();
if ($category->id) { if ($category->id) {
// Editing an existing category. // 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])) { if (empty($options[$category->parent])) {
$options[$category->parent] = $DB->get_field('course_categories', 'name', array('id'=>$category->parent)); $options[$category->parent] = $DB->get_field('course_categories', 'name', array('id'=>$category->parent));
} }
$strsubmit = get_string('savechanges'); $strsubmit = get_string('savechanges');
} else { } else {
// Making a new category // Making a new category
make_categories_list($options, $parents, 'moodle/category:manage'); $options += coursecat::make_categories_list('moodle/category:manage');
$strsubmit = get_string('createcategory'); $strsubmit = get_string('createcategory');
} }


Expand Down
116 changes: 10 additions & 106 deletions course/lib.php
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. * 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; return;
} }


if (!$displaylist) {
make_categories_list($displaylist, $parentslist);
}

if (!$categorycourses) { if (!$categorycourses) {
if ($category) { if ($category) {
$categorycourses = get_category_courses_array($category->id); $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() { 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) { foreach ($cats as $key => $value) {
if (array_key_exists($key,$parents)) { $cats[$key] = str_repeat('&nbsp;', coursecat::get($key)->depth - 1). $value;
if ($indent = count($parents[$key])) {
for ($i = 0; $i < $indent; $i++) {
$cats[$key] = '&nbsp;'.$cats[$key];
}
}
}
} }
return $cats; return $cats;
} }
Expand Down
23 changes: 5 additions & 18 deletions course/manage.php
Expand Up @@ -323,9 +323,6 @@
$PAGE->set_button(print_course_search('', true, 'navbar')); $PAGE->set_button(print_course_search('', true, 'navbar'));
} }


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


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


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


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


if ($abletomovecourses) { if ($abletomovecourses) {
$movetocategories = array(); $movetocategories = coursecat::make_categories_list('moodle/category:manage');
$notused = array();
make_categories_list($movetocategories, $notused, 'moodle/category:manage');
$movetocategories[$id] = get_string('moveselectedcoursesto'); $movetocategories[$id] = get_string('moveselectedcoursesto');


$cell = new html_table_cell(); $cell = new html_table_cell();
Expand Down Expand Up @@ -615,13 +610,11 @@
* *
* @param html_table $table The table to add data to. * @param html_table $table The table to add data to.
* @param stdClass $category The category to render * @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 int $depth The depth of the category.
* @param bool $up True if this category can be moved up. * @param bool $up True if this category can be moved up.
* @param bool $down True if this category can be moved down. * @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; global $OUTPUT;


static $str = null; static $str = null;
Expand Down Expand Up @@ -712,14 +705,8 @@ function print_category_edit(html_table $table, $category, $displaylist, $parent


$actions = ''; $actions = '';
if (has_capability('moodle/category:manage', $category->context)) { 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()); $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 = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id");
$select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide')); $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
$actions = $OUTPUT->render($select); $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; $down = $last ? false : true;
$first = false; $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
Expand Up @@ -36,6 +36,7 @@
} }


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


/** /**
* A form for a user to request a course. * A form for a user to request a course.
Expand Down Expand Up @@ -69,9 +70,7 @@ function definition() {
$mform->setType('shortname', PARAM_TEXT); $mform->setType('shortname', PARAM_TEXT);


if (!empty($CFG->requestcategoryselection)) { if (!empty($CFG->requestcategoryselection)) {
$displaylist = array(); $displaylist = coursecat::make_categories_list();
$parentlist = array();
make_categories_list($displaylist, $parentlist, '');
$mform->addElement('select', 'category', get_string('category'), $displaylist); $mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->setDefault('category', $CFG->defaultrequestcategory); $mform->setDefault('category', $CFG->defaultrequestcategory);
$mform->addHelpButton('category', 'category'); $mform->addHelpButton('category', 'category');
Expand Down
15 changes: 4 additions & 11 deletions course/search.php
Expand Up @@ -24,6 +24,7 @@


require_once("../config.php"); require_once("../config.php");
require_once($CFG->dirroot.'/course/lib.php'); require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->libdir.'/coursecatlib.php');


$search = optional_param('search', '', PARAM_RAW); // search words $search = optional_param('search', '', PARAM_RAW); // search words
$page = optional_param('page', 0, PARAM_INT); // which page to show $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 // List of minimum capabilities which user need to have for editing/moving course
$capabilities = array('moodle/course:create', 'moodle/category:manage'); $capabilities = array('moodle/course:create', 'moodle/category:manage');


// List of category id's in which current user has course:create and category:manage capability. // Populate usercatlist with list of category id's with course:create and category:manage capabilities.
$usercatlist = array(); $usercatlist = coursecat::make_categories_list($capabilities);

// 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);


$search = trim(strip_tags($search)); // trim & clean raw searched string $search = trim(strip_tags($search)); // trim & clean raw searched string
if ($search) { if ($search) {
Expand Down Expand Up @@ -109,9 +104,7 @@
} }
} }


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


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

0 comments on commit 4e0b602

Please sign in to comment.