MDL-10873 If both site default and user pref are empty for studentsperpage, we assume...
[moodle-pu.git] / course / index.php
blob5ab89eb36b7b72a8727ceacf5596a23d01868a0b
1 <?php // $Id$
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) {
26 require_login();
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);
34 } else {
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)) {
51 unset($newcategory);
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) . "'");
57 } else {
58 notify(get_string('categoryadded', '', format_string($newcategory->name)));
63 /// Unless it's an editing admin, just print the regular listing of courses/categories
65 if (!$adminediting) {
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');
74 print_header("$site->shortname: $strcategories", $strcourses,
75 $strcategories, '', '', true, update_categories_button());
76 print_heading($strcategories);
77 print_box_start('categorybox');
78 print_category_create_form();
79 print_whole_category_list();
80 print_box_end();
81 print_course_search();
82 } else {
83 $strfulllistofcourses = get_string('fulllistofcourses');
84 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, $strfulllistofcourses,
85 '', '', true, update_categories_button());
86 print_box_start('courseboxes');
87 print_category_create_form();
88 print_courses(0);
89 print_box_end();
92 /// I am not sure this context in the next has_capability call is correct.
93 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
94 print_single_button('request.php', NULL, get_string('courserequest'), 'get');
96 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Print link to create a new course
97 /// Get the 1st available category
98 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
99 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
101 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) {
102 print_single_button('pending.php',NULL, get_string('coursespending'),'get');
104 print_footer();
105 exit;
108 /// From now on is all the admin/course creator functions
110 /// Print headings
112 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
113 require_once($CFG->libdir.'/adminlib.php');
114 admin_externalpage_setup('coursemgmt');
115 admin_externalpage_print_header();
116 } else {
117 print_header("$site->shortname: $strcategories", $strcourses,
118 $strcategories, '', '', true, update_categories_button());
121 print_heading($strcategories);
123 /// Delete a category if necessary
125 if (!empty($delete) and confirm_sesskey()) {
127 // context is coursecat, if not present admins should have it set in site level
128 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
129 if ($deletecat = get_record('course_categories', 'id', $delete) and has_capability('moodle/category:delete', $context)) {
130 if (!empty($sure) && $sure == md5($deletecat->timemodified)) {
131 /// Send the children categories to live with their grandparent
132 if ($childcats = get_records('course_categories', 'parent', $deletecat->id)) {
133 foreach ($childcats as $childcat) {
134 if (! set_field('course_categories', 'parent', $deletecat->parent, 'id', $childcat->id)) {
135 error('Could not update a child category!', 'index.php');
140 /// If the grandparent is a valid (non-zero) category, then
141 /// send the children courses to live with their grandparent as well
142 if ($deletecat->parent) {
143 if ($childcourses = get_records('course', 'category', $deletecat->id)) {
144 foreach ($childcourses as $childcourse) {
145 if (! set_field('course', 'category', $deletecat->parent, 'id', $childcourse->id)) {
146 error('Could not update a child course!', 'index.php');
152 /// Finally delete the category itself
153 if (delete_records('course_categories', 'id', $deletecat->id)) {
154 notify(get_string('categorydeleted', '', format_string($deletecat->name)));
155 // MLD-9983
156 events_trigger('category_deleted', $deletecat);
159 else {
160 $strdeletecategorycheck = get_string('deletecategorycheck','', format_string($deletecat->name));
161 notice_yesno($strdeletecategorycheck,
162 "index.php?delete=$delete&amp;sure=".md5($deletecat->timemodified)."&amp;sesskey=$USER->sesskey",
163 "index.php?sesskey=$USER->sesskey");
165 print_footer();
166 exit();
172 /// Create a default category if necessary
173 if (!$categories = get_categories()) { /// No category yet!
174 // Try and make one
175 unset($tempcat);
176 $tempcat->name = get_string('miscellaneous');
177 if (!$tempcat->id = insert_record('course_categories', $tempcat)) {
178 error('Serious error: Could not create a default category!');
183 /// Move a category to a new parent if required
185 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
186 if ($tempcat = get_record('course_categories', 'id', $move)) {
187 if ($tempcat->parent != $moveto) {
188 if (! set_field('course_categories', 'parent', $moveto, 'id', $tempcat->id)) {
189 notify('Could not update that category!');
190 } else {
191 rebuild_context_rel(get_context_instance(CONTEXT_COURSECAT, $move));
198 /// Hide or show a category
199 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
200 if (!empty($hide)) {
201 $tempcat = get_record('course_categories', 'id', $hide);
202 $visible = 0;
203 } else {
204 $tempcat = get_record('course_categories', 'id', $show);
205 $visible = 1;
207 if ($tempcat) {
208 if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id)) {
209 notify('Could not update that category!');
211 if (! set_field('course', 'visible', $visible, 'category', $tempcat->id)) {
212 notify('Could not hide/show any courses in this category !');
218 /// Move a category up or down
220 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
222 $swapcategory = NULL;
223 $movecategory = NULL;
225 if (!empty($moveup)) {
226 if ($movecategory = get_record('course_categories', 'id', $moveup)) {
227 $categories = get_categories($movecategory->parent);
229 foreach ($categories as $category) {
230 if ($category->id == $movecategory->id) {
231 break;
233 $swapcategory = $category;
237 if (!empty($movedown)) {
238 if ($movecategory = get_record('course_categories', 'id', $movedown)) {
239 $categories = get_categories($movecategory->parent);
241 $choosenext = false;
242 foreach ($categories as $category) {
243 if ($choosenext) {
244 $swapcategory = $category;
245 break;
247 if ($category->id == $movecategory->id) {
248 $choosenext = true;
253 if ($swapcategory and $movecategory) { // Renumber everything for robustness
254 $count=0;
255 foreach ($categories as $category) {
256 $count++;
257 if ($category->id == $swapcategory->id) {
258 $category = $movecategory;
259 } else if ($category->id == $movecategory->id) {
260 $category = $swapcategory;
262 if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id)) {
263 notify('Could not update that category!');
269 /// Find the default category (the one with the lowest ID)
270 $categories = get_categories();
271 $default = 99999;
272 foreach ($categories as $category) {
273 if ($category->id < $default) {
274 $default = $category->id;
278 /// Find any orphan courses that don't yet have a valid category and set to default
279 if ($courses = get_courses(NULL,NULL,'c.id, c.category, c.sortorder, c.visible')) {
280 foreach ($courses as $course) {
281 if ($course->category and !isset($categories[$course->category])) {
282 set_field('course', 'category', $default, 'id', $course->id);
287 fix_course_sortorder();
289 /// Print form for creating new categories
291 if (has_capability('moodle/category:create', get_context_instance(CONTEXT_SYSTEM))) {
292 $mform->display();
295 /// Print out the categories with all the knobs
297 $strcategories = get_string('categories');
298 $strcourses = get_string('courses');
299 $strmovecategoryto = get_string('movecategoryto');
300 $stredit = get_string('edit');
302 $displaylist = array();
303 $parentlist = array();
305 $displaylist[0] = get_string('top');
306 make_categories_list($displaylist, $parentlist, '');
308 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
309 echo '<th class="header" scope="col">'.$strcategories.'</th>';
310 echo '<th class="header" scope="col">'.$strcourses.'</th>';
311 echo '<th class="header" scope="col">'.$stredit.'</th>';
312 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
313 echo '</tr>';
315 print_category_edit(NULL, $displaylist, $parentlist);
317 echo '</table>';
319 echo '<div class="buttons">';
320 /// Print link to create a new course
321 if (has_capability('moodle/course:create', $context)) {
322 unset($options);
323 $options['category'] = $category->id;
324 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
327 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) {
328 print_single_button('pending.php',NULL, get_string('coursespending'), 'get');
330 // admin page does not allow custom buttons in the navigation bar
331 echo '<div class="singlebutton">';
332 echo update_categories_button();
333 echo '</div></div>';
335 print_footer();
338 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
339 /// Recursive function to print all the categories ready for editing
341 global $CFG, $USER;
343 static $str = '';
345 if (empty($str)) {
346 $str->delete = get_string('delete');
347 $str->moveup = get_string('moveup');
348 $str->movedown = get_string('movedown');
349 $str->edit = get_string('editthiscategory');
350 $str->hide = get_string('hide');
351 $str->show = get_string('show');
354 if ($category) {
356 $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
358 echo '<tr><td align="left" class="name">';
359 for ($i=0; $i<$depth;$i++) {
360 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
362 $linkcss = $category->visible ? '' : ' class="dimmed" ';
363 echo '<a '.$linkcss.' title="'.$str->edit.'" '.
364 ' href="category.php?id='.$category->id.'&amp;categoryedit=on&amp;sesskey='.sesskey().'">'.
365 format_string($category->name).'</a>';
366 echo '</td>';
368 echo '<td class="count">'.$category->coursecount.'</td>';
370 echo '<td class="icons">'; /// Print little icons
372 if (has_capability('moodle/category:delete', $context)) {
373 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
374 ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$str->delete.'" /></a> ';
377 if (has_capability('moodle/category:visibility', $context)) {
378 if (!empty($category->visible)) {
379 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
380 ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$str->hide.'" /></a> ';
381 } else {
382 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
383 ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$str->show.'" /></a> ';
387 if ($up) {
388 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
389 ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
391 if ($down) {
392 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
393 ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
395 echo '</td>';
397 echo '<td align="left">';
398 $tempdisplaylist = $displaylist;
399 unset($tempdisplaylist[$category->id]);
400 foreach ($parentslist as $key => $parents) {
401 if (in_array($category->id, $parents)) {
402 unset($tempdisplaylist[$key]);
405 popup_form ("index.php?move=$category->id&amp;sesskey=$USER->sesskey&amp;moveto=", $tempdisplaylist, "moveform$category->id", $category->parent, '', '', '', false);
406 echo '</td>';
407 echo '</tr>';
408 } else {
409 $category->id = '0';
412 if ($categories = get_categories($category->id)) { // Print all the children recursively
413 $countcats = count($categories);
414 $count = 0;
415 $first = true;
416 $last = false;
417 foreach ($categories as $cat) {
418 $count++;
419 if ($count == $countcats) {
420 $last = true;
422 $up = $first ? false : true;
423 $down = $last ? false : true;
424 $first = false;
426 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);