3 * Displays a category and all its sub-categories.
4 * In editing mode, allows admins to move/delete/hide categories
7 require_once("../config.php");
8 require_once("lib.php");
9 require_once('editcategory_form.php');
11 $id = optional_param('id', 0, PARAM_INT
); // Category id: if not given, show Add a Category form. If given and 'categoryupdate': show edit form
12 $page = optional_param('page', 0, PARAM_INT
); // which page to show
13 $perpage = optional_param('perpage', $CFG->coursesperpage
, PARAM_INT
); // how many per page
14 $hide = optional_param('hide', 0, PARAM_INT
);
15 $show = optional_param('show', 0, PARAM_INT
);
16 $moveup = optional_param('moveup', 0, PARAM_INT
);
17 $movedown = optional_param('movedown', 0, PARAM_INT
);
18 $moveto = optional_param('moveto', 0, PARAM_INT
);
19 $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL
); // Enables Move/Delete/Hide icons near each category in the list
20 $categoryadd = optional_param('categoryadd', 0, PARAM_BOOL
); // Enables the Add Category form
21 $categoryupdate = optional_param('categoryupdate', 0, PARAM_BOOL
); // Enables the Edit Category form
22 $resort = optional_param('resort', 0, PARAM_BOOL
);
24 if (!$site = get_site()) {
25 error("Site isn't defined!");
28 if ($categoryadd) { // Show Add category form: if $id is given, it is used as the parent category
29 $strtitle = get_string("addnewcategory");
30 $context = get_context_instance(CONTEXT_SYSTEM
);
32 } elseif (!is_null($id) && !$categoryadd) { // Show Edit category form: $id is given as the identifier of the category being edited
33 $strtitle = get_string("editcategorysettings");
34 $context = get_context_instance(CONTEXT_COURSECAT
, $id);
35 if (!$category = get_record("course_categories", "id", $id)) {
36 error("Category not known!");
40 $mform = new editcategory_form('editcategory.php', compact(array('category', 'id')));
42 if (!empty($category)) {
43 $mform->set_data($category);
44 } elseif (!is_null($id)) {
45 $data = new stdClass();
47 $data->categoryadd
= 1;
48 $mform->set_data($data);
51 if ($mform->is_cancelled()){
52 if (empty($category)) {
53 redirect($CFG->wwwroot
.'/course/index.php?categoryedit=on');
55 redirect($CFG->wwwroot
.'/course/category.php?categoryedit=on&id='.$category->id
);
57 } else if (($data = $mform->get_data())) {
58 $newcategory = new stdClass();
59 $newcategory->name
= $data->name
;
60 $newcategory->description
= $data->description
;
61 $newcategory->sortorder
= 999;
62 $newcategory->parent
= $data->parent
; // if $id = 0, the new category will be a top-level category
64 if (!empty($data->theme
) && !empty($CFG->allowcategorythemes
)) {
65 $newcategory->theme
= $data->theme
;
69 if (empty($category) && has_capability('moodle/category:create', $context)) { // Create a new category
70 if (!$newcategory->id
= insert_record('course_categories', $newcategory)) {
71 notify( "Could not insert the new category '$newcategory->name' ");
73 $newcategory->context
= get_context_instance(CONTEXT_COURSECAT
, $newcategory->id
);
74 mark_context_dirty($newcategory->context
->path
);
75 redirect('index.php?categoryedit=on');
77 } elseif (has_capability('moodle/category:update', $context)) {
78 $newcategory->id
= $category->id
;
80 if ($newcategory->parent
!= $category->parent
) {
81 $parent_cat = get_record('course_categories', 'id', $newcategory->parent
);
82 move_category($newcategory, $parent_cat);
85 if (!update_record('course_categories', $newcategory)) {
86 error( "Could not update the category '$newcategory->name' ");
88 if ($newcategory->parent
== 0) {
89 $redirect_link = 'index.php?categoryedit=on';
91 $redirect_link = 'category.php?id='.$newcategory->id
.'&categoryedit=on';
93 fix_course_sortorder();
94 redirect($redirect_link);
101 // If id is given, but not categoryadd or categoryupdate, we show the category with its list of subcategories
102 if ($id && !$categoryadd && !$categoryupdate && false) {
105 if ($CFG->forcelogin) {
109 // Determine whether to allow user to see this category
110 if (has_capability('moodle/course:create', $context)) {
111 if ($categoryedit !== -1) {
112 $USER->categoryediting = $categoryedit;
114 $navbaritem = update_category_button($category->id);
115 $creatorediting = !empty($USER->categoryediting);
116 $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) and $creatorediting);
119 if (!$category->visible) {
120 print_error('notavailable', 'error');
122 $navbaritem = print_course_search("", true, "navbar");
123 $adminediting = false;
124 $creatorediting = false;
127 // Resort the category if requested
128 if ($resort and confirm_sesskey()) {
129 if ($courses = get_courses($id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
130 // move it off the range
131 $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
132 FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
133 $count = $count->max + 100;
135 foreach ($courses as $course) {
136 set_field('course', 'sortorder', $count, 'id', $course->id);
140 fix_course_sortorder($category->id);
145 $numcategories = count_records("course_categories");
147 $stradministration = get_string("administration");
148 $strcategories = get_string("categories");
149 $strcategory = get_string("category");
150 $strcourses = get_string("courses");
153 $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc');
154 $navlinks[] = array('name' => $category->name, 'link' => null, 'type' => 'misc');
155 $navigation = build_navigation($navlinks);
157 if ($creatorediting) {
159 // modify this to treat this as an admin page
161 require_once($CFG->libdir.'/adminlib.php');
162 admin_externalpage_setup('categorymgmt');
163 admin_externalpage_print_header();
165 print_header("$site->shortname: $category->name", "$site->fullname: $strcategories", $navigation, "", "", true, $navbaritem);
168 print_header("$site->shortname: $category->name", "$site->fullname: $strcategories", $navigation, "", "", true, $navbaritem);
171 // Print button to turn editing off
173 echo '<div class="categoryediting button" align="right">'.update_category_button($category->id).'</div>';
176 // Print link to roles
178 if (has_capability('moodle/role:assign', $context)) {
179 echo '<div class="rolelink"><a href="'.$CFG->wwwroot.'/'.$CFG->admin.'/roles/assign.php?contextid='.
180 $context->id.'">'.get_string('assignroles','role').'</a></div>';
183 // Print the category selector
185 $displaylist = array();
186 $parentlist = array();
188 make_categories_list($displaylist, $parentlist, "");
190 echo '<div class="categorypicker">';
191 popup_form('category.php?id=', $displaylist, 'switchcategory', $category->id, '', '', '', false, 'self', $strcategories.':');
194 // Print current category description
195 if ($category->description) {
197 print_heading(get_string('description'));
198 echo $category->description;
203 if ($creatorediting) {
204 // Move a specified category to a new category
206 if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved
208 // user must have category update in both cats to perform this
209 require_capability('moodle/category:update', $context);
210 require_capability('moodle/category:update', get_context_instance(CONTEXT_COURSECAT, $moveto));
212 if (!$destcategory = get_record("course_categories", "id", $data->moveto)) {
213 error("Error finding the destination category");
215 // TODO function to move the category
218 // Hide or show a category
219 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
220 require_capability('moodle/category:visibility', $context);
222 $category = get_record("course_categories", "id", $hide);
225 $category = get_record("course_categories", "id", $show);
229 if (! set_field("course_categories", "visible", $visible, "id", $category->id)) {
230 notify("Could not update that category!");
236 // Move a category up or down
237 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
238 require_capability('moodle/category:update', $context);
239 $movecategory = NULL;
240 $swapcategory = NULL;
242 // TODO something like fix_course_sortorder() ?
244 // we are going to need to know the range
245 $max = get_record_sql('SELECT MAX(sortorder) AS max, 1 FROM ' . $CFG->prefix . 'course_categories WHERE id=' . $category->id);
246 $max = $max->max + 100;
248 if (!empty($moveup)) {
249 $movecategory = get_record('course_categories', 'id', $moveup);
250 $swapcategory = get_record('course_categories',
251 'category', $category->id,
252 'sortorder', $movecategory->sortorder - 1);
254 $movecategory = get_record('course_categories', 'id', $movedown);
255 $swapcategory = get_record('course_categories',
256 'category', $category->id,
257 'sortorder', $movecategory->sortorder + 1);
260 if ($swapcourse and $movecourse) { // Renumber everything for robustness
262 if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id)
263 && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
264 && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
266 notify("Could not update that course!");
273 } // End of editing stuff
275 // Print out all the sub-categories
276 if ($subcategories = get_records("course_categories", "parent", $category->id, "sortorder ASC")) {
278 foreach ($subcategories as $subcategory) {
279 if ($subcategory->visible or has_capability('moodle/course:create', $context)) {
280 $subcategorieswereshown = true;
282 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter">';
283 echo '<tr><th scope="col">'.get_string('subcategories').'</th></tr>';
284 echo '<tr><td style="white-space: nowrap">';
287 $catlinkcss = $subcategory->visible ? "" : " class=\"dimmed\" ";
288 echo '<a '.$catlinkcss.' href="category.php?id='.$subcategory->id.'">'.
289 format_string($subcategory->name).'</a><br />';
293 echo "</td></tr></table>";
298 // print option to add a subcategory
299 if (has_capability('moodle/category:create', $context) && $creatorediting) {
301 $mform->set_data($cat);
310 $straddnewcategory = get_string("addnewcategory");
311 $stradministration = get_string("administration");
312 $strcategories = get_string("categories");
315 if (!empty($category->name
)) {
316 $navlinks[] = array('name' => $strtitle,
320 $fullname = $category->name
;
322 $navlinks[] = array('name' => $stradministration,
323 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
325 $navlinks[] = array('name' => $strcategories,
326 'link' => 'index.php',
328 $navlinks[] = array('name' => $straddnewcategory,
331 $title = "$site->shortname: $straddnewcategory";
332 $fullname = $site->fullname
;
335 $navigation = build_navigation($navlinks);
336 print_header($title, $fullname, $navigation, $mform->focus());
337 print_heading($strtitle);