2 // For most people, just lists the course categories
3 // Allows the admin to create, delete and rename course categories
5 require_once("../config.php");
6 require_once("lib.php");
7 require_once('category_add_form.php');
9 $categoryedit = optional_param('categoryedit', -1,PARAM_BOOL
);
10 $delete = optional_param('delete',0,PARAM_INT
);
11 $hide = optional_param('hide',0,PARAM_INT
);
12 $show = optional_param('show',0,PARAM_INT
);
13 $sure = optional_param('sure','',PARAM_ALPHANUM
);
14 $move = optional_param('move',0,PARAM_INT
);
15 $moveto = optional_param('moveto',-1,PARAM_INT
);
16 $moveup = optional_param('moveup',0,PARAM_INT
);
17 $movedown = optional_param('movedown',0,PARAM_INT
);
19 $context = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
21 if (!$site = get_site()) {
22 error('Site isn\'t defined!');
25 if ($CFG->forcelogin
) {
29 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
30 if ($categoryedit !== -1) {
31 $USER->categoryediting
= $categoryedit;
33 $adminediting = !empty($USER->categoryediting
);
35 $adminediting = false;
38 $stradministration = get_string('administration');
39 $strcategories = get_string('categories');
40 $strcategory = get_string('category');
41 $strcourses = get_string('courses');
42 $stredit = get_string('edit');
43 $strdelete = get_string('delete');
44 $straction = get_string('action');
47 /// If data for a new category was submitted, then add it
48 $mform = new category_add_form();
49 if ($form = $mform->get_data() and has_capability('moodle/category:create', $context)) {
50 if (!empty($form->addcategory
)) {
52 $newcategory->name
= stripslashes_safe($form->addcategory
);
53 $newcategory->description
= $form->description
;
54 $newcategory->sortorder
= 999;
55 if (!insert_record('course_categories', $newcategory)) {
56 notify("Could not insert the new category '" . format_string($newcategory->name
) . "'");
58 notify(get_string('categoryadded', '', format_string($newcategory->name
)));
63 /// Unless it's an editing admin, just print the regular listing of courses/categories
67 /// Print form for creating new categories
69 $countcategories = count_records('course_categories');
71 if ($countcategories > 1 ||
($countcategories == 1 && count_records('course') > 200)) {
72 $strcourses = get_string('courses');
73 $strcategories = get_string('categories');
76 $navlinks[] = array('name'=>$strcategories,'link'=>'','type'=>'misc');
77 $navigation = build_navigation($navlinks);
78 print_header("$site->shortname: $strcategories", $strcourses, $navigation, '', '', true, update_categories_button());
79 print_heading($strcategories);
80 print_box_start('categorybox');
81 print_whole_category_list();
83 print_course_search();
85 $strfulllistofcourses = get_string('fulllistofcourses');
86 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses,
87 build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))),
88 '', '', true, update_categories_button());
89 print_box_start('courseboxes');
94 /// I am not sure this context in the next has_capability call is correct.
95 if (isloggedin() and !isguest() and !has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM
, SITEID
)) and $CFG->enablecourserequests
) { // Print link to request a new course
96 print_single_button('request.php', NULL, get_string('courserequest'), 'get');
98 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) { // Print link to create a new course
99 /// Get the 1st available category
100 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
101 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
103 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM
, SITEID
)) and !empty($CFG->enablecourserequests
)) {
104 print_single_button('pending.php',NULL, get_string('coursespending'),'get');
110 /// From now on is all the admin/course creator functions
114 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
115 require_once($CFG->libdir
.'/adminlib.php');
116 admin_externalpage_setup('coursemgmt');
117 admin_externalpage_print_header();
119 print_header("$site->shortname: $strcategories", $strcourses,
120 build_navigation(array(array('name'=>$strcategories,'link'=>'','type'=>'misc'))), '', '', true, update_categories_button());
123 print_heading($strcategories);
125 /// Delete a category if necessary
127 if (!empty($delete) and confirm_sesskey()) {
129 // context is coursecat, if not present admins should have it set in site level
130 $context = get_context_instance(CONTEXT_COURSECAT
, $delete);
131 if ($deletecat = get_record('course_categories', 'id', $delete) and has_capability('moodle/category:delete', $context)) {
132 if (!empty($sure) && $sure == md5($deletecat->timemodified
)) {
133 /// Send the children categories to live with their grandparent
134 if ($childcats = get_records('course_categories', 'parent', $deletecat->id
)) {
135 foreach ($childcats as $childcat) {
136 if (! set_field('course_categories', 'parent', $deletecat->parent
, 'id', $childcat->id
)) {
137 error('Could not update a child category!', 'index.php');
142 /// If the grandparent is a valid (non-zero) category, then
143 /// send the children courses to live with their grandparent as well
144 if ($deletecat->parent
) {
145 if ($childcourses = get_records('course', 'category', $deletecat->id
)) {
146 foreach ($childcourses as $childcourse) {
147 if (! set_field('course', 'category', $deletecat->parent
, 'id', $childcourse->id
)) {
148 error('Could not update a child course!', 'index.php');
154 /// Finally delete the category itself
155 if (delete_records('course_categories', 'id', $deletecat->id
)) {
156 notify(get_string('categorydeleted', '', format_string($deletecat->name
)));
158 events_trigger('category_deleted', $deletecat);
162 $strdeletecategorycheck = get_string('deletecategorycheck','', format_string($deletecat->name
));
163 notice_yesno($strdeletecategorycheck,
164 "index.php?delete=$delete&sure=".md5($deletecat->timemodified
)."&sesskey=$USER->sesskey",
165 "index.php?sesskey=$USER->sesskey");
174 /// Create a default category if necessary
175 if (!$categories = get_categories()) { /// No category yet!
178 $tempcat->name
= get_string('miscellaneous');
179 if (!$tempcat->id
= insert_record('course_categories', $tempcat)) {
180 error('Serious error: Could not create a default category!');
185 /// Move a category to a new parent if required
187 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
188 if ($tempcat = get_record('course_categories', 'id', $move)) {
189 if ($tempcat->parent
!= $moveto) {
190 if (! set_field('course_categories', 'parent', $moveto, 'id', $tempcat->id
)) {
191 notify('Could not update that category!');
193 rebuild_context_rel(get_context_instance(CONTEXT_COURSECAT
, $move));
200 /// Hide or show a category
201 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
203 $tempcat = get_record('course_categories', 'id', $hide);
206 $tempcat = get_record('course_categories', 'id', $show);
210 if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id
)) {
211 notify('Could not update that category!');
213 if (! set_field('course', 'visible', $visible, 'category', $tempcat->id
)) {
214 notify('Could not hide/show any courses in this category !');
220 /// Move a category up or down
222 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
224 $swapcategory = NULL;
225 $movecategory = NULL;
227 if (!empty($moveup)) {
228 if ($movecategory = get_record('course_categories', 'id', $moveup)) {
229 $categories = get_categories($movecategory->parent
);
231 foreach ($categories as $category) {
232 if ($category->id
== $movecategory->id
) {
235 $swapcategory = $category;
239 if (!empty($movedown)) {
240 if ($movecategory = get_record('course_categories', 'id', $movedown)) {
241 $categories = get_categories($movecategory->parent
);
244 foreach ($categories as $category) {
246 $swapcategory = $category;
249 if ($category->id
== $movecategory->id
) {
255 if ($swapcategory and $movecategory) { // Renumber everything for robustness
257 foreach ($categories as $category) {
259 if ($category->id
== $swapcategory->id
) {
260 $category = $movecategory;
261 } else if ($category->id
== $movecategory->id
) {
262 $category = $swapcategory;
264 if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id
)) {
265 notify('Could not update that category!');
271 /// Find the default category (the one with the lowest ID)
272 $categories = get_categories();
274 foreach ($categories as $category) {
275 if ($category->id
< $default) {
276 $default = $category->id
;
280 /// Find any orphan courses that don't yet have a valid category and set to default
281 if ($courses = get_courses(NULL,NULL,'c.id, c.category, c.sortorder, c.visible')) {
282 foreach ($courses as $course) {
283 if ($course->category
and !isset($categories[$course->category
])) {
284 set_field('course', 'category', $default, 'id', $course->id
);
289 fix_course_sortorder();
291 /// Print form for creating new categories
293 if (has_capability('moodle/category:create', get_context_instance(CONTEXT_SYSTEM
))) {
297 /// Print out the categories with all the knobs
299 $strcategories = get_string('categories');
300 $strcourses = get_string('courses');
301 $strmovecategoryto = get_string('movecategoryto');
302 $stredit = get_string('edit');
304 $displaylist = array();
305 $parentlist = array();
307 $displaylist[0] = get_string('top');
308 make_categories_list($displaylist, $parentlist, '');
310 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
311 echo '<th class="header" scope="col">'.$strcategories.'</th>';
312 echo '<th class="header" scope="col">'.$strcourses.'</th>';
313 echo '<th class="header" scope="col">'.$stredit.'</th>';
314 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
317 print_category_edit(NULL, $displaylist, $parentlist);
321 echo '<div class="buttons">';
322 /// Print link to create a new course
323 if (has_capability('moodle/course:create', $context)) {
325 $options['category'] = $category->id
;
326 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
329 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM
, SITEID
)) and !empty($CFG->enablecourserequests
)) {
330 print_single_button('pending.php',NULL, get_string('coursespending'), 'get');
332 // admin page does not allow custom buttons in the navigation bar
333 echo '<div class="singlebutton">';
334 echo update_categories_button();
340 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
341 /// Recursive function to print all the categories ready for editing
348 $str->delete
= get_string('delete');
349 $str->moveup
= get_string('moveup');
350 $str->movedown
= get_string('movedown');
351 $str->edit
= get_string('editthiscategory');
352 $str->hide
= get_string('hide');
353 $str->show
= get_string('show');
358 $context = get_context_instance(CONTEXT_COURSECAT
, $category->id
);
360 echo '<tr><td align="left" class="name">';
361 for ($i=0; $i<$depth;$i++
) {
362 echo ' ';
364 $linkcss = $category->visible ?
'' : ' class="dimmed" ';
365 echo '<a '.$linkcss.' title="'.$str->edit
.'" '.
366 ' href="category.php?id='.$category->id
.'&categoryedit=on&sesskey='.sesskey().'">'.
367 format_string($category->name
).'</a>';
370 echo '<td class="count">'.$category->coursecount
.'</td>';
372 echo '<td class="icons">'; /// Print little icons
374 if (has_capability('moodle/category:delete', $context)) {
375 echo '<a title="'.$str->delete
.'" href="index.php?delete='.$category->id
.'&sesskey='.sesskey().'"><img'.
376 ' src="'.$CFG->pixpath
.'/t/delete.gif" class="iconsmall" alt="'.$str->delete
.'" /></a> ';
379 if (has_capability('moodle/category:visibility', $context)) {
380 if (!empty($category->visible
)) {
381 echo '<a title="'.$str->hide
.'" href="index.php?hide='.$category->id
.'&sesskey='.sesskey().'"><img'.
382 ' src="'.$CFG->pixpath
.'/t/hide.gif" class="iconsmall" alt="'.$str->hide
.'" /></a> ';
384 echo '<a title="'.$str->show
.'" href="index.php?show='.$category->id
.'&sesskey='.sesskey().'"><img'.
385 ' src="'.$CFG->pixpath
.'/t/show.gif" class="iconsmall" alt="'.$str->show
.'" /></a> ';
390 echo '<a title="'.$str->moveup
.'" href="index.php?moveup='.$category->id
.'&sesskey='.sesskey().'"><img'.
391 ' src="'.$CFG->pixpath
.'/t/up.gif" class="iconsmall" alt="'.$str->moveup
.'" /></a> ';
394 echo '<a title="'.$str->movedown
.'" href="index.php?movedown='.$category->id
.'&sesskey='.sesskey().'"><img'.
395 ' src="'.$CFG->pixpath
.'/t/down.gif" class="iconsmall" alt="'.$str->movedown
.'" /></a> ';
399 echo '<td align="left">';
400 $tempdisplaylist = $displaylist;
401 unset($tempdisplaylist[$category->id
]);
402 foreach ($parentslist as $key => $parents) {
403 if (in_array($category->id
, $parents)) {
404 unset($tempdisplaylist[$key]);
407 popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", $category->parent
, '', '', '', false);
414 if ($categories = get_categories($category->id
)) { // Print all the children recursively
415 $countcats = count($categories);
419 foreach ($categories as $cat) {
421 if ($count == $countcats) {
424 $up = $first ?
false : true;
425 $down = $last ?
false : true;
428 print_category_edit($cat, $displaylist, $parentslist, $depth+
1, $up, $down);