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 $context = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
20 if (!$site = get_site()) {
21 error('Site isn\'t defined!');
24 if ($CFG->forcelogin
) {
28 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
29 if ($categoryedit !== -1) {
30 $USER->categoryediting
= $categoryedit;
32 $adminediting = !empty($USER->categoryediting
);
34 $adminediting = false;
37 $stradministration = get_string('administration');
38 $strcategories = get_string('categories');
39 $strcategory = get_string('category');
40 $strcourses = get_string('courses');
41 $stredit = get_string('edit');
42 $strdelete = get_string('delete');
43 $straction = get_string('action');
46 /// If data for a new category was submitted, then add it
47 if ($form = data_submitted() and confirm_sesskey() and has_capability('moodle/category:create', $context)) {
48 if (!empty($form->addcategory
)) {
50 $newcategory->name
= stripslashes_safe($form->addcategory
);
51 $newcategory->sortorder
= 999;
52 if (!insert_record('course_categories', $newcategory)) {
53 notify("Could not insert the new category '" . format_string($newcategory->name
) . "'");
55 notify(get_string('categoryadded', '', format_string($newcategory->name
)));
60 /// Unless it's an editing admin, just print the regular listing of courses/categories
64 /// Print form for creating new categories
66 $countcategories = count_records('course_categories');
68 if ($countcategories > 1 ||
($countcategories == 1 && count_records('course') > 200)) {
69 $strcourses = get_string('courses');
70 $strcategories = get_string('categories');
71 print_header("$site->shortname: $strcategories", $strcourses,
72 $strcategories, '', '', true, update_categories_button());
73 print_heading($strcategories);
74 print_box_start('categorybox');
75 print_category_create_form();
76 print_whole_category_list();
78 print_course_search();
80 $strfulllistofcourses = get_string('fulllistofcourses');
81 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, $strfulllistofcourses,
82 '', '', true, update_categories_button());
83 print_box_start('courseboxes');
84 print_category_create_form();
89 /// I am not sure this context in the next has_capability call is correct.
90 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
91 print_single_button('request.php', NULL, get_string('courserequest'), 'get');
93 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) { // Print link to create a new course
94 /// Get the 1st available category
95 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
96 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
98 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM
, SITEID
)) and !empty($CFG->enablecourserequests
)) {
99 print_single_button('pending.php',NULL, get_string('coursespending'),'get');
105 /// From now on is all the admin/course creator functions
109 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
110 require_once($CFG->libdir
.'/adminlib.php');
111 admin_externalpage_setup('coursemgmt');
112 admin_externalpage_print_header();
114 print_header("$site->shortname: $strcategories", $strcourses,
115 $strcategories, '', '', true, update_categories_button());
118 print_heading($strcategories);
120 /// Delete a category if necessary
122 if (!empty($delete) and confirm_sesskey()) {
124 // context is coursecat, if not present admins should have it set in site level
125 $context = get_context_instance(CONTEXT_COURSECAT
, $delete);
126 if ($deletecat = get_record('course_categories', 'id', $delete) and has_capability('moodle/category:delete', $context)) {
127 if (!empty($sure) && $sure == md5($deletecat->timemodified
)) {
128 /// Send the children categories to live with their grandparent
129 if ($childcats = get_records('course_categories', 'parent', $deletecat->id
)) {
130 foreach ($childcats as $childcat) {
131 if (! set_field('course_categories', 'parent', $deletecat->parent
, 'id', $childcat->id
)) {
132 error('Could not update a child category!', 'index.php');
137 /// If the grandparent is a valid (non-zero) category, then
138 /// send the children courses to live with their grandparent as well
139 if ($deletecat->parent
) {
140 if ($childcourses = get_records('course', 'category', $deletecat->id
)) {
141 foreach ($childcourses as $childcourse) {
142 if (! set_field('course', 'category', $deletecat->parent
, 'id', $childcourse->id
)) {
143 error('Could not update a child course!', 'index.php');
149 /// Finally delete the category itself
150 if (delete_records('course_categories', 'id', $deletecat->id
)) {
151 notify(get_string('categorydeleted', '', format_string($deletecat->name
)));
153 events_trigger('category_deleted', $deletecat);
157 $strdeletecategorycheck = get_string('deletecategorycheck','', format_string($deletecat->name
));
158 notice_yesno($strdeletecategorycheck,
159 "index.php?delete=$delete&sure=".md5($deletecat->timemodified
)."&sesskey=$USER->sesskey",
160 "index.php?sesskey=$USER->sesskey");
169 /// Create a default category if necessary
170 if (!$categories = get_categories()) { /// No category yet!
173 $tempcat->name
= get_string('miscellaneous');
174 if (!$tempcat->id
= insert_record('course_categories', $tempcat)) {
175 error('Serious error: Could not create a default category!');
180 /// Move a category to a new parent if required
182 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
183 if ($tempcat = get_record('course_categories', 'id', $move)) {
184 if ($tempcat->parent
!= $moveto) {
185 if (! set_field('course_categories', 'parent', $moveto, 'id', $tempcat->id
)) {
186 notify('Could not update that category!');
193 /// Hide or show a category
194 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
196 $tempcat = get_record('course_categories', 'id', $hide);
199 $tempcat = get_record('course_categories', 'id', $show);
203 if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id
)) {
204 notify('Could not update that category!');
206 if (! set_field('course', 'visible', $visible, 'category', $tempcat->id
)) {
207 notify('Could not hide/show any courses in this category !');
213 /// Move a category up or down
215 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
217 $swapcategory = NULL;
218 $movecategory = NULL;
220 if (!empty($moveup)) {
221 if ($movecategory = get_record('course_categories', 'id', $moveup)) {
222 $categories = get_categories($movecategory->parent
);
224 foreach ($categories as $category) {
225 if ($category->id
== $movecategory->id
) {
228 $swapcategory = $category;
232 if (!empty($movedown)) {
233 if ($movecategory = get_record('course_categories', 'id', $movedown)) {
234 $categories = get_categories($movecategory->parent
);
237 foreach ($categories as $category) {
239 $swapcategory = $category;
242 if ($category->id
== $movecategory->id
) {
248 if ($swapcategory and $movecategory) { // Renumber everything for robustness
250 foreach ($categories as $category) {
252 if ($category->id
== $swapcategory->id
) {
253 $category = $movecategory;
254 } else if ($category->id
== $movecategory->id
) {
255 $category = $swapcategory;
257 if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id
)) {
258 notify('Could not update that category!');
264 /// Find the default category (the one with the lowest ID)
265 $categories = get_categories();
267 foreach ($categories as $category) {
268 if ($category->id
< $default) {
269 $default = $category->id
;
273 /// Find any orphan courses that don't yet have a valid category and set to default
274 if ($courses = get_courses(NULL,NULL,'c.id, c.category, c.sortorder, c.visible')) {
275 foreach ($courses as $course) {
276 if ($course->category
and !isset($categories[$course->category
])) {
277 set_field('course', 'category', $default, 'id', $course->id
);
282 fix_course_sortorder();
284 /// Print form for creating new categories
285 print_category_create_form();
287 /// Print out the categories with all the knobs
289 $strcategories = get_string('categories');
290 $strcourses = get_string('courses');
291 $strmovecategoryto = get_string('movecategoryto');
292 $stredit = get_string('edit');
294 $displaylist = array();
295 $parentlist = array();
297 $displaylist[0] = get_string('top');
298 make_categories_list($displaylist, $parentlist, '');
300 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
301 echo '<th class="header" scope="col">'.$strcategories.'</th>';
302 echo '<th class="header" scope="col">'.$strcourses.'</th>';
303 echo '<th class="header" scope="col">'.$stredit.'</th>';
304 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
307 print_category_edit(NULL, $displaylist, $parentlist);
311 echo '<div class="buttons">';
312 /// Print link to create a new course
313 if (has_capability('moodle/course:create', $context)) {
315 $options['category'] = $category->id
;
316 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
319 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM
, SITEID
)) and !empty($CFG->enablecourserequests
)) {
320 print_single_button('pending.php',NULL, get_string('coursespending'), 'get');
322 // admin page does not allow custom buttons in the navigation bar
323 echo '<div class="singlebutton">';
324 echo update_categories_button();
330 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
331 /// Recursive function to print all the categories ready for editing
338 $str->delete
= get_string('delete');
339 $str->moveup
= get_string('moveup');
340 $str->movedown
= get_string('movedown');
341 $str->edit
= get_string('editthiscategory');
342 $str->hide
= get_string('hide');
343 $str->show
= get_string('show');
348 $context = get_context_instance(CONTEXT_COURSECAT
, $category->id
);
350 echo '<tr><td align="left" class="name">';
351 for ($i=0; $i<$depth;$i++
) {
352 echo ' ';
354 $linkcss = $category->visible ?
'' : ' class="dimmed" ';
355 echo '<a '.$linkcss.' title="'.$str->edit
.'" '.
356 ' href="category.php?id='.$category->id
.'&categoryedit=on&sesskey='.sesskey().'">'.
357 format_string($category->name
).'</a>';
360 echo '<td class="count">'.$category->coursecount
.'</td>';
362 echo '<td class="icons">'; /// Print little icons
364 if (has_capability('moodle/category:delete', $context)) {
365 echo '<a title="'.$str->delete
.'" href="index.php?delete='.$category->id
.'&sesskey='.sesskey().'"><img'.
366 ' src="'.$CFG->pixpath
.'/t/delete.gif" class="iconsmall" alt="'.$str->delete
.'" /></a> ';
369 if (has_capability('moodle/category:visibility', $context)) {
370 if (!empty($category->visible
)) {
371 echo '<a title="'.$str->hide
.'" href="index.php?hide='.$category->id
.'&sesskey='.sesskey().'"><img'.
372 ' src="'.$CFG->pixpath
.'/t/hide.gif" class="iconsmall" alt="'.$str->hide
.'" /></a> ';
374 echo '<a title="'.$str->show
.'" href="index.php?show='.$category->id
.'&sesskey='.sesskey().'"><img'.
375 ' src="'.$CFG->pixpath
.'/t/show.gif" class="iconsmall" alt="'.$str->show
.'" /></a> ';
380 echo '<a title="'.$str->moveup
.'" href="index.php?moveup='.$category->id
.'&sesskey='.sesskey().'"><img'.
381 ' src="'.$CFG->pixpath
.'/t/up.gif" class="iconsmall" alt="'.$str->moveup
.'" /></a> ';
384 echo '<a title="'.$str->movedown
.'" href="index.php?movedown='.$category->id
.'&sesskey='.sesskey().'"><img'.
385 ' src="'.$CFG->pixpath
.'/t/down.gif" class="iconsmall" alt="'.$str->movedown
.'" /></a> ';
389 echo '<td align="left">';
390 $tempdisplaylist = $displaylist;
391 unset($tempdisplaylist[$category->id
]);
392 foreach ($parentslist as $key => $parents) {
393 if (in_array($category->id
, $parents)) {
394 unset($tempdisplaylist[$key]);
397 popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", $category->parent
, '', '', '', false);
404 if ($categories = get_categories($category->id
)) { // Print all the children recursively
405 $countcats = count($categories);
409 foreach ($categories as $cat) {
411 if ($count == $countcats) {
414 $up = $first ?
false : true;
415 $down = $last ?
false : true;
418 print_category_edit($cat, $displaylist, $parentslist, $depth+
1, $up, $down);
423 /** prints the add new category text input field and form */
424 function print_category_create_form() {
426 $straddnewcategory = get_string('addnewcategory');
428 if (has_capability('moodle/category:create', get_context_instance(CONTEXT_SYSTEM
))) {
429 echo '<div class="addcategory">';
430 echo '<form id="addform" action="index.php" method="post">';
431 echo '<fieldset class="invisiblefieldset">';
432 echo '<input type="text" size="30" alt="'.$straddnewcategory.'" name="addcategory" />';
433 echo '<input type="submit" value="'.$straddnewcategory.'" />';
434 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';