MDL-10929:
[moodle-linuxchix.git] / course / index.php
blob6ac5a79ef634dee5cd0c14909374858a2d557027
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", build_navigation(array(array('name'=>$strcourses,'link'=>'','type'=>'misc'))),
75 $strcategories, '', '', true, update_categories_button());
76 print_heading($strcategories);
77 print_box_start('categorybox');
78 print_whole_category_list();
79 print_box_end();
80 print_course_search();
81 } else {
82 $strfulllistofcourses = get_string('fulllistofcourses');
83 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses,
84 build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))),
85 '', '', true, update_categories_button());
86 print_box_start('courseboxes');
87 print_courses(0);
88 print_box_end();
91 /// I am not sure this context in the next has_capability call is correct.
92 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
93 print_single_button('request.php', NULL, get_string('courserequest'), 'get');
95 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Print link to create a new course
96 /// Get the 1st available category
97 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
98 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
100 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) {
101 print_single_button('pending.php',NULL, get_string('coursespending'),'get');
103 print_footer();
104 exit;
107 /// From now on is all the admin/course creator functions
109 /// Print headings
111 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
112 require_once($CFG->libdir.'/adminlib.php');
113 admin_externalpage_setup('coursemgmt');
114 admin_externalpage_print_header();
115 } else {
116 print_header("$site->shortname: $strcategories", build_navigation(array(array('name'=>$strcourses,'link'=>'','type'=>'misc'))),
117 $strcategories, '', '', true, update_categories_button());
120 print_heading($strcategories);
122 /// Delete a category if necessary
124 if (!empty($delete) and confirm_sesskey()) {
126 // context is coursecat, if not present admins should have it set in site level
127 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
128 if ($deletecat = get_record('course_categories', 'id', $delete) and has_capability('moodle/category:delete', $context)) {
129 if (!empty($sure) && $sure == md5($deletecat->timemodified)) {
130 /// Send the children categories to live with their grandparent
131 if ($childcats = get_records('course_categories', 'parent', $deletecat->id)) {
132 foreach ($childcats as $childcat) {
133 if (! set_field('course_categories', 'parent', $deletecat->parent, 'id', $childcat->id)) {
134 error('Could not update a child category!', 'index.php');
139 /// If the grandparent is a valid (non-zero) category, then
140 /// send the children courses to live with their grandparent as well
141 if ($deletecat->parent) {
142 if ($childcourses = get_records('course', 'category', $deletecat->id)) {
143 foreach ($childcourses as $childcourse) {
144 if (! set_field('course', 'category', $deletecat->parent, 'id', $childcourse->id)) {
145 error('Could not update a child course!', 'index.php');
151 /// Finally delete the category itself
152 if (delete_records('course_categories', 'id', $deletecat->id)) {
153 notify(get_string('categorydeleted', '', format_string($deletecat->name)));
154 // MLD-9983
155 events_trigger('category_deleted', $deletecat);
158 else {
159 $strdeletecategorycheck = get_string('deletecategorycheck','', format_string($deletecat->name));
160 notice_yesno($strdeletecategorycheck,
161 "index.php?delete=$delete&amp;sure=".md5($deletecat->timemodified)."&amp;sesskey=$USER->sesskey",
162 "index.php?sesskey=$USER->sesskey");
164 print_footer();
165 exit();
171 /// Create a default category if necessary
172 if (!$categories = get_categories()) { /// No category yet!
173 // Try and make one
174 unset($tempcat);
175 $tempcat->name = get_string('miscellaneous');
176 if (!$tempcat->id = insert_record('course_categories', $tempcat)) {
177 error('Serious error: Could not create a default category!');
182 /// Move a category to a new parent if required
184 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
185 if ($tempcat = get_record('course_categories', 'id', $move)) {
186 if ($tempcat->parent != $moveto) {
187 if (! set_field('course_categories', 'parent', $moveto, 'id', $tempcat->id)) {
188 notify('Could not update that category!');
189 } else {
190 rebuild_context_rel(get_context_instance(CONTEXT_COURSECAT, $move));
197 /// Hide or show a category
198 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
199 if (!empty($hide)) {
200 $tempcat = get_record('course_categories', 'id', $hide);
201 $visible = 0;
202 } else {
203 $tempcat = get_record('course_categories', 'id', $show);
204 $visible = 1;
206 if ($tempcat) {
207 if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id)) {
208 notify('Could not update that category!');
210 if (! set_field('course', 'visible', $visible, 'category', $tempcat->id)) {
211 notify('Could not hide/show any courses in this category !');
217 /// Move a category up or down
219 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
221 $swapcategory = NULL;
222 $movecategory = NULL;
224 if (!empty($moveup)) {
225 if ($movecategory = get_record('course_categories', 'id', $moveup)) {
226 $categories = get_categories($movecategory->parent);
228 foreach ($categories as $category) {
229 if ($category->id == $movecategory->id) {
230 break;
232 $swapcategory = $category;
236 if (!empty($movedown)) {
237 if ($movecategory = get_record('course_categories', 'id', $movedown)) {
238 $categories = get_categories($movecategory->parent);
240 $choosenext = false;
241 foreach ($categories as $category) {
242 if ($choosenext) {
243 $swapcategory = $category;
244 break;
246 if ($category->id == $movecategory->id) {
247 $choosenext = true;
252 if ($swapcategory and $movecategory) { // Renumber everything for robustness
253 $count=0;
254 foreach ($categories as $category) {
255 $count++;
256 if ($category->id == $swapcategory->id) {
257 $category = $movecategory;
258 } else if ($category->id == $movecategory->id) {
259 $category = $swapcategory;
261 if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id)) {
262 notify('Could not update that category!');
268 /// Find the default category (the one with the lowest ID)
269 $categories = get_categories();
270 $default = 99999;
271 foreach ($categories as $category) {
272 if ($category->id < $default) {
273 $default = $category->id;
277 /// Find any orphan courses that don't yet have a valid category and set to default
278 if ($courses = get_courses(NULL,NULL,'c.id, c.category, c.sortorder, c.visible')) {
279 foreach ($courses as $course) {
280 if ($course->category and !isset($categories[$course->category])) {
281 set_field('course', 'category', $default, 'id', $course->id);
286 fix_course_sortorder();
288 /// Print form for creating new categories
290 if (has_capability('moodle/category:create', get_context_instance(CONTEXT_SYSTEM))) {
291 $mform->display();
294 /// Print out the categories with all the knobs
296 $strcategories = get_string('categories');
297 $strcourses = get_string('courses');
298 $strmovecategoryto = get_string('movecategoryto');
299 $stredit = get_string('edit');
301 $displaylist = array();
302 $parentlist = array();
304 $displaylist[0] = get_string('top');
305 make_categories_list($displaylist, $parentlist, '');
307 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
308 echo '<th class="header" scope="col">'.$strcategories.'</th>';
309 echo '<th class="header" scope="col">'.$strcourses.'</th>';
310 echo '<th class="header" scope="col">'.$stredit.'</th>';
311 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
312 echo '</tr>';
314 print_category_edit(NULL, $displaylist, $parentlist);
316 echo '</table>';
318 echo '<div class="buttons">';
319 /// Print link to create a new course
320 if (has_capability('moodle/course:create', $context)) {
321 unset($options);
322 $options['category'] = $category->id;
323 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
326 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) {
327 print_single_button('pending.php',NULL, get_string('coursespending'), 'get');
329 // admin page does not allow custom buttons in the navigation bar
330 echo '<div class="singlebutton">';
331 echo update_categories_button();
332 echo '</div></div>';
334 print_footer();
337 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
338 /// Recursive function to print all the categories ready for editing
340 global $CFG, $USER;
342 static $str = '';
344 if (empty($str)) {
345 $str->delete = get_string('delete');
346 $str->moveup = get_string('moveup');
347 $str->movedown = get_string('movedown');
348 $str->edit = get_string('editthiscategory');
349 $str->hide = get_string('hide');
350 $str->show = get_string('show');
353 if ($category) {
355 $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
357 echo '<tr><td align="left" class="name">';
358 for ($i=0; $i<$depth;$i++) {
359 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
361 $linkcss = $category->visible ? '' : ' class="dimmed" ';
362 echo '<a '.$linkcss.' title="'.$str->edit.'" '.
363 ' href="category.php?id='.$category->id.'&amp;categoryedit=on&amp;sesskey='.sesskey().'">'.
364 format_string($category->name).'</a>';
365 echo '</td>';
367 echo '<td class="count">'.$category->coursecount.'</td>';
369 echo '<td class="icons">'; /// Print little icons
371 if (has_capability('moodle/category:delete', $context)) {
372 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
373 ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$str->delete.'" /></a> ';
376 if (has_capability('moodle/category:visibility', $context)) {
377 if (!empty($category->visible)) {
378 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
379 ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$str->hide.'" /></a> ';
380 } else {
381 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
382 ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$str->show.'" /></a> ';
386 if ($up) {
387 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
388 ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
390 if ($down) {
391 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
392 ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
394 echo '</td>';
396 echo '<td align="left">';
397 $tempdisplaylist = $displaylist;
398 unset($tempdisplaylist[$category->id]);
399 foreach ($parentslist as $key => $parents) {
400 if (in_array($category->id, $parents)) {
401 unset($tempdisplaylist[$key]);
404 popup_form ("index.php?move=$category->id&amp;sesskey=$USER->sesskey&amp;moveto=", $tempdisplaylist, "moveform$category->id", $category->parent, '', '', '', false);
405 echo '</td>';
406 echo '</tr>';
407 } else {
408 $category->id = '0';
411 if ($categories = get_categories($category->id)) { // Print all the children recursively
412 $countcats = count($categories);
413 $count = 0;
414 $first = true;
415 $last = false;
416 foreach ($categories as $cat) {
417 $count++;
418 if ($count == $countcats) {
419 $last = true;
421 $up = $first ? false : true;
422 $down = $last ? false : true;
423 $first = false;
425 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);