MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / course / index.php
blob5d698b4f594af12e86962914a298c3cd84957d75
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 $sysctx = get_context_instance(CONTEXT_SYSTEM);
20 $context = $sysctx;
22 if (!$site = get_site()) {
23 error('Site isn\'t defined!');
26 if ($CFG->forcelogin) {
27 require_login();
30 if (has_capability('moodle/category:update', $sysctx)) {
31 if ($categoryedit !== -1) {
32 $USER->categoryediting = $categoryedit;
34 $adminediting = !empty($USER->categoryediting);
35 } else {
36 $adminediting = false;
39 $stradministration = get_string('administration');
40 $strcategories = get_string('categories');
41 $strcategory = get_string('category');
42 $strcourses = get_string('courses');
43 $stredit = get_string('edit');
44 $strdelete = get_string('delete');
45 $straction = get_string('action');
48 /// If data for a new category was submitted, then add it
49 $mform = new category_add_form();
50 if ($form = $mform->get_data() and has_capability('moodle/category:create', $context)) {
51 if (!empty($form->addcategory)) {
52 unset($newcategory);
53 $newcategory->name = stripslashes_safe($form->addcategory);
54 $newcategory->description = $form->description;
55 $newcategory->sortorder = 999;
56 if (!$newcategory->id = insert_record('course_categories', $newcategory)) {
57 notify("Could not insert the new category '" . format_string($newcategory->name) . "'");
58 } else {
59 $newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
60 mark_context_dirty($newcategory->context->path);
61 notify(get_string('categoryadded', '', format_string($newcategory->name)));
66 /// Unless it's an editing admin, just print the regular listing of courses/categories
68 if (!$adminediting) {
70 /// Print form for creating new categories
72 $countcategories = count_records('course_categories');
74 if ($countcategories > 1 || ($countcategories == 1 && count_records('course') > 200)) {
75 $strcourses = get_string('courses');
76 $strcategories = get_string('categories');
78 $navlinks = array();
79 $navlinks[] = array('name'=>$strcategories,'link'=>'','type'=>'misc');
80 $navigation = build_navigation($navlinks);
81 print_header("$site->shortname: $strcategories", $strcourses, $navigation, '', '', true, update_categories_button());
82 print_heading($strcategories);
83 print_box_start('categorybox');
84 print_whole_category_list();
85 print_box_end();
86 print_course_search();
87 } else {
88 $strfulllistofcourses = get_string('fulllistofcourses');
89 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses,
90 build_navigation(array(array('name'=>$strfulllistofcourses, 'link'=>'','type'=>'misc'))),
91 '', '', true, update_categories_button());
92 print_box_start('courseboxes');
93 print_courses(0);
94 print_box_end();
97 /// I am not sure this context in the next has_capability call is correct.
98 if (isloggedin() and !isguest() and !has_capability('moodle/course:create', $sysctx) and $CFG->enablecourserequests) { // Print link to request a new course
99 print_single_button('request.php', NULL, get_string('courserequest'), 'get');
101 if (has_capability('moodle/course:create', $sysctx)) { // Print link to create a new course
102 /// Get the 1st available category
103 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
104 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
106 if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests)) {
107 print_single_button('pending.php',NULL, get_string('coursespending'),'get');
109 print_footer();
110 exit;
113 /// From now on is all the admin/course creator functions
115 /// Print headings
117 if (has_capability('moodle/site:config', $sysctx)) {
118 require_once($CFG->libdir.'/adminlib.php');
119 admin_externalpage_setup('coursemgmt');
120 admin_externalpage_print_header();
121 } else {
122 print_header("$site->shortname: $strcategories", $strcourses,
123 build_navigation(array(array('name'=>$strcategories,'link'=>'','type'=>'misc'))), '', '', true, update_categories_button());
126 print_heading($strcategories);
128 /// Delete a category if necessary
130 if (!empty($delete) and confirm_sesskey()) {
132 // context is coursecat, if not present admins should have it set in site level
133 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
134 if ($deletecat = get_record('course_categories', 'id', $delete) and has_capability('moodle/category:delete', $context)) {
135 if (!empty($sure) && $sure == md5($deletecat->timemodified)) {
136 /// Send the children categories to live with their grandparent
137 if ($childcats = get_records('course_categories', 'parent', $deletecat->id)) {
138 foreach ($childcats as $childcat) {
139 if (! set_field('course_categories', 'parent', $deletecat->parent, 'id', $childcat->id)) {
140 error('Could not update a child category!', 'index.php');
145 /// If the grandparent is a valid (non-zero) category, then
146 /// send the children courses to live with their grandparent as well
147 if ($deletecat->parent) {
148 if ($childcourses = get_records('course', 'category', $deletecat->id)) {
149 foreach ($childcourses as $childcourse) {
150 if (! set_field('course', 'category', $deletecat->parent, 'id', $childcourse->id)) {
151 error('Could not update a child course!', 'index.php');
157 /// Finally delete the category itself
158 if (delete_records('course_categories', 'id', $deletecat->id)) {
159 notify(get_string('categorydeleted', '', format_string($deletecat->name)));
160 // MLD-9983
161 events_trigger('category_deleted', $deletecat);
164 else {
165 $strdeletecategorycheck = get_string('deletecategorycheck','', format_string($deletecat->name));
166 notice_yesno($strdeletecategorycheck,
167 "index.php?delete=$delete&amp;sure=".md5($deletecat->timemodified)."&amp;sesskey=$USER->sesskey",
168 "index.php?sesskey=$USER->sesskey");
170 print_footer();
171 exit();
177 /// Create a default category if necessary
178 if (!$categories = get_categories()) { /// No category yet!
179 // Try and make one
180 unset($tempcat);
181 $tempcat->name = get_string('miscellaneous');
182 if (!$tempcat->id = insert_record('course_categories', $tempcat)) {
183 error('Serious error: Could not create a default category!');
185 $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id);
186 mark_context_dirty('/'.SYSCONTEXTID);
190 /// Move a category to a new parent if required
192 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
193 if ($tempcat = get_record('course_categories', 'id', $move)) {
194 if ($tempcat->parent != $moveto) {
195 $newp = get_record('course_categories', 'id', $moveto);
196 if (! move_category($tempcat, $newp)) {
197 notify('Could not update that category!');
204 /// Hide or show a category
205 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
206 if (!empty($hide)) {
207 $tempcat = get_record('course_categories', 'id', $hide);
208 $visible = 0;
209 } else {
210 $tempcat = get_record('course_categories', 'id', $show);
211 $visible = 1;
213 if ($tempcat) {
214 if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id)) {
215 notify('Could not update that category!');
217 if (! set_field('course', 'visible', $visible, 'category', $tempcat->id)) {
218 notify('Could not hide/show any courses in this category !');
224 /// Move a category up or down
226 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
228 $swapcategory = NULL;
229 $movecategory = NULL;
231 if (!empty($moveup)) {
232 if ($movecategory = get_record('course_categories', 'id', $moveup)) {
233 $categories = get_categories($movecategory->parent);
235 foreach ($categories as $category) {
236 if ($category->id == $movecategory->id) {
237 break;
239 $swapcategory = $category;
241 unset($category);
244 if (!empty($movedown)) {
245 if ($movecategory = get_record('course_categories', 'id', $movedown)) {
246 $categories = get_categories($movecategory->parent);
248 $choosenext = false;
249 foreach ($categories as $category) {
250 if ($choosenext) {
251 $swapcategory = $category;
252 break;
254 if ($category->id == $movecategory->id) {
255 $choosenext = true;
258 unset($category);
261 if ($swapcategory and $movecategory) { // Renumber everything for robustness
262 $count=0;
263 foreach ($categories as $category) {
264 $count++;
265 if ($category->id == $swapcategory->id) {
266 $category = $movecategory;
267 } else if ($category->id == $movecategory->id) {
268 $category = $swapcategory;
270 if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id)) {
271 notify('Could not update that category!');
274 unset($category);
278 /// Find any orphan courses that don't yet have a valid category and set to default
279 fix_coursecategory_orphans();
281 /// Should be a no-op 99% of the cases
282 fix_course_sortorder();
284 /// Print form for creating new categories
286 if (has_capability('moodle/category:create', get_context_instance(CONTEXT_SYSTEM))) {
287 $mform->display();
290 /// Print out the categories with all the knobs
292 $strcategories = get_string('categories');
293 $strcourses = get_string('courses');
294 $strmovecategoryto = get_string('movecategoryto');
295 $stredit = get_string('edit');
297 $displaylist = array();
298 $parentlist = array();
300 $displaylist[0] = get_string('top');
301 make_categories_list($displaylist, $parentlist, '');
303 echo '<table class="generalbox editcourse boxaligncenter"><tr class="header">';
304 echo '<th class="header" scope="col">'.$strcategories.'</th>';
305 echo '<th class="header" scope="col">'.$strcourses.'</th>';
306 echo '<th class="header" scope="col">'.$stredit.'</th>';
307 echo '<th class="header" scope="col">'.$strmovecategoryto.'</th>';
308 echo '</tr>';
310 print_category_edit(NULL, $displaylist, $parentlist);
312 echo '</table>';
314 echo '<div class="buttons">';
315 /// Print link to create a new course
316 if (has_capability('moodle/course:create', $context)) {
317 unset($options);
318 if (!empty($category->id)) {
319 $options['category'] = $category->id;
320 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
324 if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests)) {
325 print_single_button('pending.php',NULL, get_string('coursespending'), 'get');
327 // admin page does not allow custom buttons in the navigation bar
328 echo '<div class="singlebutton">';
329 echo update_categories_button();
330 echo '</div></div>';
332 print_footer();
334 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
335 /// Recursive function to print all the categories ready for editing
337 global $CFG, $USER;
339 static $str = '';
341 if (empty($str)) {
342 $str->delete = get_string('delete');
343 $str->moveup = get_string('moveup');
344 $str->movedown = get_string('movedown');
345 $str->edit = get_string('editthiscategory');
346 $str->hide = get_string('hide');
347 $str->show = get_string('show');
350 if ($category) {
352 if (!isset($category->context)) {
353 $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id);
356 echo '<tr><td align="left" class="name">';
357 for ($i=0; $i<$depth;$i++) {
358 echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
360 $linkcss = $category->visible ? '' : ' class="dimmed" ';
361 echo '<a '.$linkcss.' title="'.$str->edit.'" '.
362 ' href="category.php?id='.$category->id.'&amp;categoryedit=on&amp;sesskey='.sesskey().'">'.
363 format_string($category->name).'</a>';
364 echo '</td>';
366 echo '<td class="count">'.$category->coursecount.'</td>';
368 echo '<td class="icons">'; /// Print little icons
370 if (has_capability('moodle/category:delete', $category->context)) {
371 echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
372 ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$str->delete.'" /></a> ';
375 if (has_capability('moodle/category:visibility', $category->context)) {
376 if (!empty($category->visible)) {
377 echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
378 ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$str->hide.'" /></a> ';
379 } else {
380 echo '<a title="'.$str->show.'" href="index.php?show='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
381 ' src="'.$CFG->pixpath.'/t/show.gif" class="iconsmall" alt="'.$str->show.'" /></a> ';
385 if ($up) {
386 echo '<a title="'.$str->moveup.'" href="index.php?moveup='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
387 ' src="'.$CFG->pixpath.'/t/up.gif" class="iconsmall" alt="'.$str->moveup.'" /></a> ';
389 if ($down) {
390 echo '<a title="'.$str->movedown.'" href="index.php?movedown='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
391 ' src="'.$CFG->pixpath.'/t/down.gif" class="iconsmall" alt="'.$str->movedown.'" /></a> ';
393 echo '</td>';
395 echo '<td align="left">';
396 $tempdisplaylist = $displaylist;
397 unset($tempdisplaylist[$category->id]);
398 foreach ($parentslist as $key => $parents) {
399 if (in_array($category->id, $parents)) {
400 unset($tempdisplaylist[$key]);
403 popup_form ("index.php?move=$category->id&amp;sesskey=$USER->sesskey&amp;moveto=", $tempdisplaylist, "moveform$category->id", $category->parent, '', '', '', false);
404 echo '</td>';
405 echo '</tr>';
406 } else {
407 $category->id = '0';
410 if ($categories = get_categories($category->id)) { // Print all the children recursively
411 $countcats = count($categories);
412 $count = 0;
413 $first = true;
414 $last = false;
415 foreach ($categories as $cat) {
416 $count++;
417 if ($count == $countcats) {
418 $last = true;
420 $up = $first ? false : true;
421 $down = $last ? false : true;
422 $first = false;
424 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);