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();
$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.
// 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()) {
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.
$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'));
}
// 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';
);
$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'));
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..
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(
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);
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'));
}
* 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;
$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(' ', $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)),
);
}
// 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)
);
}
$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");
// 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;
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)
}
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;
+}