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");
8 $categoryedit = optional_param('categoryedit', -1,PARAM_BOOL
);
9 $delete = optional_param('delete',0,PARAM_INT
);
10 $hide = optional_param('hide',0,PARAM_INT
);
11 $show = optional_param('show',0,PARAM_INT
);
12 $sure = optional_param('sure','',PARAM_ALPHANUM
);
13 $move = optional_param('move',0,PARAM_INT
);
14 $moveto = optional_param('moveto',-1,PARAM_INT
);
15 $moveup = optional_param('moveup',0,PARAM_INT
);
16 $movedown = optional_param('movedown',0,PARAM_INT
);
18 $sysctx = get_context_instance(CONTEXT_SYSTEM
);
21 if (!$site = get_site()) {
22 error('Site isn\'t defined!');
25 if ($CFG->forcelogin
) {
29 if (has_capability('moodle/category:update', $sysctx)) {
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 /// Unless it's an editing admin, just print the regular listing of courses/categories
51 /// Print form for creating new categories
53 $countcategories = count_records('course_categories');
55 if ($countcategories > 1 ||
($countcategories == 1 && count_records('course') > 200)) {
56 $strcourses = get_string('courses');
57 $strcategories = get_string('categories');
60 $navlinks[] = array('name'=>$strcategories,'link'=>'','type'=>'misc');
61 $navigation = build_navigation($navlinks);
62 print_header("$site->shortname: $strcategories", $strcourses, $navigation, '', '', true, update_categories_button());
63 print_heading($strcategories);
64 echo skip_main_destination();
65 print_box_start('categorybox');
66 print_whole_category_list();
68 print_course_search();
70 $strfulllistofcourses = get_string('fulllistofcourses');
71 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses,
72 build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))),
73 '', '', true, update_categories_button());
74 echo skip_main_destination();
75 print_box_start('courseboxes');
80 /// I am not sure this context in the next has_capability call is correct.
81 if (isloggedin() and !isguest() and !has_capability('moodle/course:create', $sysctx) and $CFG->enablecourserequests
) { // Print link to request a new course
82 print_single_button('request.php', NULL, get_string('courserequest'), 'get');
84 if (has_capability('moodle/course:create', $sysctx)) { // Print link to create a new course
85 /// Get the 1st available category
86 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
87 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
89 if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests
)) {
90 print_single_button('pending.php',NULL, get_string('coursespending'),'get');
96 /// From now on is all the admin/course creator functions
100 if (has_capability('moodle/site:config', $sysctx)) {
101 require_once($CFG->libdir
.'/adminlib.php');
102 admin_externalpage_setup('coursemgmt');
103 admin_externalpage_print_header();
105 print_header("$site->shortname: $strcategories", $strcourses,
106 build_navigation(array(array('name'=>$strcategories,'link'=>'','type'=>'misc'))), '', '', true, update_categories_button());
109 print_heading($strcategories);
111 /// Delete a category if necessary
113 if (!empty($delete) and confirm_sesskey()) {
115 // context is coursecat, if not present admins should have it set in site level
116 $context = get_context_instance(CONTEXT_COURSECAT
, $delete);
117 if ($deletecat = get_record('course_categories', 'id', $delete) and has_capability('moodle/category:delete', $context)) {
118 if (!empty($sure) && $sure == md5($deletecat->timemodified
)) {
119 /// Send the children categories to live with their grandparent
120 if ($childcats = get_records('course_categories', 'parent', $deletecat->id
)) {
121 foreach ($childcats as $childcat) {
122 if (! set_field('course_categories', 'parent', $deletecat->parent
, 'id', $childcat->id
)) {
123 error('Could not update a child category!', 'index.php');
128 /// If the grandparent is a valid (non-zero) category, then
129 /// send the children courses to live with their grandparent as well
130 if ($deletecat->parent
) {
131 if ($childcourses = get_records('course', 'category', $deletecat->id
)) {
132 foreach ($childcourses as $childcourse) {
133 if (! set_field('course', 'category', $deletecat->parent
, 'id', $childcourse->id
)) {
134 error('Could not update a child course!', 'index.php');
140 /// Finally delete the category itself
141 if (delete_records('course_categories', 'id', $deletecat->id
)) {
142 notify(get_string('categorydeleted', '', format_string($deletecat->name
)));
144 events_trigger('category_deleted', $deletecat);
148 $strdeletecategorycheck = get_string('deletecategorycheck','', format_string($deletecat->name
));
149 notice_yesno($strdeletecategorycheck,
150 "index.php?delete=$delete&sure=".md5($deletecat->timemodified
)."&sesskey=$USER->sesskey",
151 "index.php?sesskey=$USER->sesskey");
160 /// Create a default category if necessary
161 if (!$categories = get_categories()) { /// No category yet!
164 $tempcat->name
= get_string('miscellaneous');
165 if (!$tempcat->id
= insert_record('course_categories', $tempcat)) {
166 error('Serious error: Could not create a default category!');
168 $tempcat->context
= get_context_instance(CONTEXT_COURSECAT
, $tempcat->id
);
169 mark_context_dirty('/'.SYSCONTEXTID
);
173 /// Move a category to a new parent if required
175 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
176 if ($tempcat = get_record('course_categories', 'id', $move)) {
177 if ($tempcat->parent
!= $moveto) {
178 $newp = get_record('course_categories', 'id', $moveto);
179 if (! move_category($tempcat, $newp)) {
180 notify('Could not update that category!');
187 /// Hide or show a category
188 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
190 $tempcat = get_record('course_categories', 'id', $hide);
193 $tempcat = get_record('course_categories', 'id', $show);
197 if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id
)) {
198 notify('Could not update that category!');
200 if (! set_field('course', 'visible', $visible, 'category', $tempcat->id
)) {
201 notify('Could not hide/show any courses in this category !');
207 /// Move a category up or down
209 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
211 $swapcategory = NULL;
212 $movecategory = NULL;
214 if (!empty($moveup)) {
215 if ($movecategory = get_record('course_categories', 'id', $moveup)) {
216 $categories = get_categories($movecategory->parent
);
218 foreach ($categories as $category) {
219 if ($category->id
== $movecategory->id
) {
222 $swapcategory = $category;
227 if (!empty($movedown)) {
228 if ($movecategory = get_record('course_categories', 'id', $movedown)) {
229 $categories = get_categories($movecategory->parent
);
232 foreach ($categories as $category) {
234 $swapcategory = $category;
237 if ($category->id
== $movecategory->id
) {
244 if ($swapcategory and $movecategory) { // Renumber everything for robustness
246 foreach ($categories as $category) {
248 if ($category->id
== $swapcategory->id
) {
249 $category = $movecategory;
250 } else if ($category->id
== $movecategory->id
) {
251 $category = $swapcategory;
253 if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id
)) {
254 notify('Could not update that category!');
261 /// Find any orphan courses that don't yet have a valid category and set to default
262 fix_coursecategory_orphans();
264 /// Should be a no-op 99% of the cases
265 fix_course_sortorder();
267 /// Print out the categories with all the knobs
269 $strcategories = get_string('categories');
270 $strcourses = get_string('courses');
271 $strmovecategoryto = get_string('movecategoryto');
272 $stredit = get_string('edit');
274 $displaylist = array();
275 $parentlist = array();
277 $displaylist[0] = get_string('top');
278 make_categories_list($displaylist, $parentlist, '');
280 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
281 echo '<th class="header" scope="col">'.$strcategories.'</th>';
282 echo '<th class="header" scope="col">'.$strcourses.'</th>';
283 echo '<th class="header" scope="col">'.$stredit.'</th>';
284 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
287 print_category_edit(NULL, $displaylist, $parentlist);
291 echo '<div class="buttons">';
293 if (!empty($category->id
)) {
294 // Print link to create a new course in current category
295 if (has_capability('moodle/course:create', $context)) {
297 $options['category'] = $category->id
;
298 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
301 if (has_capability('moodle/course:create', $sysctx)) {
302 // print create course link to first category
304 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
305 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
309 // Print button for creating new categories
310 if (has_capability('moodle/category:create', $context)) {
312 if (!empty($category->id
)) {
313 $options['id'] = $category->id
;
317 $options['categoryadd'] = 1;
318 print_single_button('editcategory.php', $options, get_string('addnewcategory'), 'get');
321 if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests
)) {
322 print_single_button('pending.php',NULL, get_string('coursespending'), 'get');
324 // admin page does not allow custom buttons in the navigation bar
325 echo '<div class="singlebutton">';
326 echo update_categories_button();
331 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
332 /// Recursive function to print all the categories ready for editing
339 $str->edit
= get_string('edit');
340 $str->delete
= get_string('delete');
341 $str->moveup
= get_string('moveup');
342 $str->movedown
= get_string('movedown');
343 $str->edit
= get_string('editthiscategory');
344 $str->hide
= get_string('hide');
345 $str->show
= get_string('show');
350 if (!isset($category->context
)) {
351 $category->context
= get_context_instance(CONTEXT_COURSECAT
, $category->id
);
354 echo '<tr><td align="left" class="name">';
355 for ($i=0; $i<$depth;$i++
) {
356 echo ' ';
358 $linkcss = $category->visible ?
'' : ' class="dimmed" ';
359 echo '<a '.$linkcss.' title="'.$str->edit
.'" '.
360 ' href="category.php?id='.$category->id
.'&categoryedit=on&sesskey='.sesskey().'">'.
361 format_string($category->name
).'</a>';
364 echo '<td class="count">'.$category->coursecount
.'</td>';
366 echo '<td class="icons">'; /// Print little icons
368 if (has_capability('moodle/category:update', $category->context
)) {
369 echo '<a title="'.$str->edit
.'" href="editcategory.php?id='.$category->id
.'&sesskey='.sesskey().'"><img'.
370 ' src="'.$CFG->pixpath
.'/t/edit.gif" class="iconsmall" alt="'.$str->edit
.'" /></a> ';
373 if (has_capability('moodle/category:delete', $category->context
)) {
374 echo '<a title="'.$str->delete
.'" href="index.php?delete='.$category->id
.'&sesskey='.sesskey().'"><img'.
375 ' src="'.$CFG->pixpath
.'/t/delete.gif" class="iconsmall" alt="'.$str->delete
.'" /></a> ';
378 if (has_capability('moodle/category:visibility', $category->context
)) {
379 if (!empty($category->visible
)) {
380 echo '<a title="'.$str->hide
.'" href="index.php?hide='.$category->id
.'&sesskey='.sesskey().'"><img'.
381 ' src="'.$CFG->pixpath
.'/t/hide.gif" class="iconsmall" alt="'.$str->hide
.'" /></a> ';
383 echo '<a title="'.$str->show
.'" href="index.php?show='.$category->id
.'&sesskey='.sesskey().'"><img'.
384 ' src="'.$CFG->pixpath
.'/t/show.gif" class="iconsmall" alt="'.$str->show
.'" /></a> ';
389 echo '<a title="'.$str->moveup
.'" href="index.php?moveup='.$category->id
.'&sesskey='.sesskey().'"><img'.
390 ' src="'.$CFG->pixpath
.'/t/up.gif" class="iconsmall" alt="'.$str->moveup
.'" /></a> ';
393 echo '<a title="'.$str->movedown
.'" href="index.php?movedown='.$category->id
.'&sesskey='.sesskey().'"><img'.
394 ' src="'.$CFG->pixpath
.'/t/down.gif" class="iconsmall" alt="'.$str->movedown
.'" /></a> ';
398 echo '<td align="left">';
399 $tempdisplaylist = $displaylist;
400 unset($tempdisplaylist[$category->id
]);
401 foreach ($parentslist as $key => $parents) {
402 if (in_array($category->id
, $parents)) {
403 unset($tempdisplaylist[$key]);
406 popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", $category->parent
, '', '', '', false);
413 if ($categories = get_categories($category->id
)) { // Print all the children recursively
414 $countcats = count($categories);
418 foreach ($categories as $cat) {
420 if ($count == $countcats) {
423 $up = $first ?
false : true;
424 $down = $last ?
false : true;
427 print_category_edit($cat, $displaylist, $parentslist, $depth+
1, $up, $down);