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 $move = optional_param('move',0,PARAM_INT
);
13 $moveto = optional_param('moveto',-1,PARAM_INT
);
14 $moveup = optional_param('moveup',0,PARAM_INT
);
15 $movedown = optional_param('movedown',0,PARAM_INT
);
17 $sysctx = get_context_instance(CONTEXT_SYSTEM
);
20 if (!$site = get_site()) {
21 error('Site isn\'t defined!');
24 if ($CFG->forcelogin
) {
28 if (has_capability('moodle/category:update', $sysctx)) {
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 /// Unless it's an editing admin, just print the regular listing of courses/categories
50 /// Print form for creating new categories
52 $countcategories = count_records('course_categories');
54 if ($countcategories > 1 ||
($countcategories == 1 && count_records('course') > 200)) {
55 $strcourses = get_string('courses');
56 $strcategories = get_string('categories');
59 $navlinks[] = array('name'=>$strcategories,'link'=>'','type'=>'misc');
60 $navigation = build_navigation($navlinks);
61 print_header("$site->shortname: $strcategories", $strcourses, $navigation, '', '', true, update_categories_button());
62 print_heading($strcategories);
63 echo skip_main_destination();
64 print_box_start('categorybox');
65 print_whole_category_list();
67 print_course_search();
69 $strfulllistofcourses = get_string('fulllistofcourses');
70 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses,
71 build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))),
72 '', '', true, update_categories_button());
73 echo skip_main_destination();
74 print_box_start('courseboxes');
79 /// I am not sure this context in the next has_capability call is correct.
80 if (isloggedin() and !isguest() and !has_capability('moodle/course:create', $sysctx) and $CFG->enablecourserequests
) { // Print link to request a new course
81 print_single_button('request.php', NULL, get_string('courserequest'), 'get');
83 if (has_capability('moodle/course:create', $sysctx)) { // Print link to create a new course
84 /// Get the 1st available category
85 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
86 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
88 if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests
)) {
89 print_single_button('pending.php',NULL, get_string('coursespending'),'get');
95 /// From now on is all the admin/course creator functions
97 /// Delete a category if necessary
99 if (!empty($delete) and confirm_sesskey()) {
100 require_once('delete_category_form.php');
102 if (!$deletecat = get_record('course_categories', 'id', $delete)) {
103 error('Incorrect category id', 'index.php');
106 $heading = get_string('deletecategory', '', format_string($deletecat->name
));
108 $context = get_context_instance(CONTEXT_COURSECAT
, $delete);
109 require_capability('moodle/category:delete', $context);
111 $mform = new delete_category_form(null, $deletecat);
112 $mform->set_data(array('delete'=>$delete));
114 if ($mform->is_cancelled()) {
115 redirect('index.php');
117 } else if (!$data= $mform->get_data(false)) {
118 require_once($CFG->libdir
. '/questionlib.php');
119 print_category_edit_header();
120 print_heading($heading);
121 print_box(get_string('deletecategorycheck2'), 'generalbox boxwidthnormal boxaligncenter');
122 if (question_context_has_any_questions($context)) {
123 print_box(get_string('deletecoursecategorywithquestions', 'question'),
124 'generalbox boxwidthnormal boxaligncenter');
131 print_category_edit_header();
132 print_heading($heading);
134 if ($data->fulldelete
) {
135 category_delete_full($deletecat, true);
137 category_delete_move($deletecat, $data->newparent
, true);
140 print_continue('index.php');
147 print_category_edit_header();
148 print_heading($strcategories);
151 /// Create a default category if necessary
152 if (!$categories = get_categories()) { /// No category yet!
155 $tempcat->name
= get_string('miscellaneous');
156 if (!$tempcat->id
= insert_record('course_categories', $tempcat)) {
157 error('Serious error: Could not create a default category!');
159 $tempcat->context
= get_context_instance(CONTEXT_COURSECAT
, $tempcat->id
);
160 mark_context_dirty('/'.SYSCONTEXTID
);
164 /// Move a category to a new parent if required
166 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
167 if ($tempcat = get_record('course_categories', 'id', $move)) {
168 if ($tempcat->parent
!= $moveto) {
169 $newp = get_record('course_categories', 'id', $moveto);
170 if (! move_category($tempcat, $newp)) {
171 notify('Could not update that category!');
178 /// Hide or show a category
179 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
181 $tempcat = get_record('course_categories', 'id', $hide);
184 $tempcat = get_record('course_categories', 'id', $show);
188 if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id
)) {
189 notify('Could not update that category!');
191 if (! set_field('course', 'visible', $visible, 'category', $tempcat->id
)) {
192 notify('Could not hide/show any courses in this category !');
198 /// Move a category up or down
200 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
202 $swapcategory = NULL;
203 $movecategory = NULL;
205 if (!empty($moveup)) {
206 if ($movecategory = get_record('course_categories', 'id', $moveup)) {
207 $categories = get_categories($movecategory->parent
);
209 foreach ($categories as $category) {
210 if ($category->id
== $movecategory->id
) {
213 $swapcategory = $category;
218 if (!empty($movedown)) {
219 if ($movecategory = get_record('course_categories', 'id', $movedown)) {
220 $categories = get_categories($movecategory->parent
);
223 foreach ($categories as $category) {
225 $swapcategory = $category;
228 if ($category->id
== $movecategory->id
) {
235 if ($swapcategory and $movecategory) { // Renumber everything for robustness
237 foreach ($categories as $category) {
239 if ($category->id
== $swapcategory->id
) {
240 $category = $movecategory;
241 } else if ($category->id
== $movecategory->id
) {
242 $category = $swapcategory;
244 if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id
)) {
245 notify('Could not update that category!');
252 /// Find any orphan courses that don't yet have a valid category and set to default
253 fix_coursecategory_orphans();
255 /// Should be a no-op 99% of the cases
256 fix_course_sortorder();
258 /// Print out the categories with all the knobs
260 $strcategories = get_string('categories');
261 $strcourses = get_string('courses');
262 $strmovecategoryto = get_string('movecategoryto');
263 $stredit = get_string('edit');
265 $displaylist = array();
266 $parentlist = array();
268 $displaylist[0] = get_string('top');
269 make_categories_list($displaylist, $parentlist, '');
271 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
272 echo '<th class="header" scope="col">'.$strcategories.'</th>';
273 echo '<th class="header" scope="col">'.$strcourses.'</th>';
274 echo '<th class="header" scope="col">'.$stredit.'</th>';
275 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
278 print_category_edit(NULL, $displaylist, $parentlist);
282 echo '<div class="buttons">';
284 if (!empty($category->id
)) {
285 // Print link to create a new course in current category
286 if (has_capability('moodle/course:create', $context)) {
288 $options['category'] = $category->id
;
289 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
292 if (has_capability('moodle/course:create', $sysctx)) {
293 // print create course link to first category
295 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
296 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
300 // Print button for creating new categories
301 if (has_capability('moodle/category:create', $context)) {
303 if (!empty($category->id
)) {
304 $options['id'] = $category->id
;
308 $options['categoryadd'] = 1;
309 print_single_button('editcategory.php', $options, get_string('addnewcategory'), 'get');
312 if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests
)) {
313 print_single_button('pending.php',NULL, get_string('coursespending'), 'get');
315 // admin page does not allow custom buttons in the navigation bar
316 echo '<div class="singlebutton">';
317 echo update_categories_button();
322 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
323 /// Recursive function to print all the categories ready for editing
330 $str->edit
= get_string('edit');
331 $str->delete
= get_string('delete');
332 $str->moveup
= get_string('moveup');
333 $str->movedown
= get_string('movedown');
334 $str->edit
= get_string('editthiscategory');
335 $str->hide
= get_string('hide');
336 $str->show
= get_string('show');
341 if (!isset($category->context
)) {
342 $category->context
= get_context_instance(CONTEXT_COURSECAT
, $category->id
);
345 echo '<tr><td align="left" class="name">';
346 for ($i=0; $i<$depth;$i++
) {
347 echo ' ';
349 $linkcss = $category->visible ?
'' : ' class="dimmed" ';
350 echo '<a '.$linkcss.' title="'.$str->edit
.'" '.
351 ' href="category.php?id='.$category->id
.'&categoryedit=on&sesskey='.sesskey().'">'.
352 format_string($category->name
).'</a>';
355 echo '<td class="count">'.$category->coursecount
.'</td>';
357 echo '<td class="icons">'; /// Print little icons
359 if (has_capability('moodle/category:update', $category->context
)) {
360 echo '<a title="'.$str->edit
.'" href="editcategory.php?id='.$category->id
.'&sesskey='.sesskey().'"><img'.
361 ' src="'.$CFG->pixpath
.'/t/edit.gif" class="iconsmall" alt="'.$str->edit
.'" /></a> ';
364 if (has_capability('moodle/category:delete', $category->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', $category->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 function print_category_edit_header() {
427 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
428 require_once($CFG->libdir
.'/adminlib.php');
429 admin_externalpage_setup('coursemgmt');
430 admin_externalpage_print_header();
432 print_header("$SITE->shortname:". get_string('categories'), get_string('courses'),
433 build_navigation(array(array('name'=>get_string('categories'),'link'=>'','type'=>'misc'))), '', '', true, update_categories_button());