Fixed apostrophe problem in MDL-8416
[moodle.git] / course / index.php
CommitLineData
24f2b65a 1<?php // $Id$
d2b6ba70 2 // For most people, just lists the course categories
3 // Allows the admin to create, delete and rename course categories
f9903ed0 4
6bc502cc 5 require_once("../config.php");
6 require_once("lib.php");
f9903ed0 7
7ff99cb6 8 $categoryedit = optional_param('categoryedit', -1,PARAM_BOOL);
9d6a11f8 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);
bbbf2d40 17
18 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
7168423c 19
d2b6ba70 20 if (!$site = get_site()) {
21 error("Site isn't defined!");
22 }
23
29aab4ac 24 if ($CFG->forcelogin) {
25 require_login();
26 }
27
51792df0 28 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
7ff99cb6 29 if ($categoryedit !== -1) {
30 $USER->categoryediting = $categoryedit;
d2b6ba70 31 }
9d6a11f8 32 $adminediting = !empty($USER->categoryediting);
33 } else {
34 $adminediting = false;
d2b6ba70 35 }
36
d2b6ba70 37
38/// Unless it's an editing admin, just print the regular listing of courses/categories
39
40 if (!$adminediting) {
41 $countcategories = count_records("course_categories");
42
765d3c7d 43 if ($countcategories > 1 || ($countcategories == 1 && count_records('course') > 200)) {
d2b6ba70 44 $strcourses = get_string("courses");
c0b85daf 45 $strcategories = get_string("categories");
46 print_header("$site->shortname: $strcategories", $strcourses,
47 $strcategories, "", "", true, update_categories_button());
48 print_heading($strcategories);
44b843b6 49 print_box_start('categorybox');
d2b6ba70 50 print_whole_category_list();
44b843b6 51 print_box_end();
38a10939 52 print_course_search();
d2b6ba70 53 } else {
54 $strfulllistofcourses = get_string("fulllistofcourses");
10403748 55 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, $strfulllistofcourses,
56 '', '', true, update_categories_button());
d2b6ba70 57 print_courses(0, "80%");
58 }
c432fd32 59
51792df0 60 /// I am not sure this context in the next has_capability call is correct.
61 if (isloggedin() and !isguest() and !has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Print link to request a new course
d1a66080 62 print_single_button("request.php", NULL, get_string("courserequest"), "get");
63 }
ae9e4c06 64 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Print link to create a new course
dd506ec8 65 print_single_button("edit.php", NULL, get_string("addnewcourse"), "get");
c432fd32 66 }
a13d2157 67 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) {
a8ff614f 68 print_single_button('pending.php',NULL, get_string('coursespending'),"get");
0705ff84 69 }
d2b6ba70 70 print_footer();
71 exit;
72 }
73
74/// From now on is all the admin functions
75
d2b6ba70 76
6e4dc10f 77 require_once($CFG->libdir.'/adminlib.php');
78 $adminroot = admin_get_root();
79 admin_externalpage_setup('coursemgmt', $adminroot);
7ff99cb6 80
d2b6ba70 81
82/// Print headings
83
84 $stradministration = get_string("administration");
0a263205 85 $strcategories = get_string("categories");
d2b6ba70 86 $strcategory = get_string("category");
87 $strcourses = get_string("courses");
d2b6ba70 88 $stredit = get_string("edit");
89 $strdelete = get_string("delete");
90 $straction = get_string("action");
91 $straddnewcategory = get_string("addnewcategory");
ba2e5d73 92
7ff99cb6 93
94
6e4dc10f 95 admin_externalpage_print_header($adminroot);
ba2e5d73 96
d2b6ba70 97 print_heading($strcategories);
98
99
100/// If data for a new category was submitted, then add it
0468976c 101 if ($form = data_submitted() and confirm_sesskey() and has_capability('moodle/category:create', $context)) {
d2b6ba70 102 if (!empty($form->addcategory)) {
103 unset($newcategory);
104 $newcategory->name = $form->addcategory;
105 $newcategory->sortorder = 999;
106 if (!insert_record("course_categories", $newcategory)) {
107 notify("Could not insert the new category '$newcategory->name'");
108 } else {
109 notify(get_string("categoryadded", "", $newcategory->name));
110 }
111 }
112 }
113
114
115/// Delete a category if necessary
116
7168423c 117 if (!empty($delete) and confirm_sesskey()) {
61240489 118
119 // context is coursecat, if not present admins should have it set in site level
120 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
121 if ($deletecat = get_record("course_categories", "id", $delete) and has_capability('moodle/category:delete', $context)) {
ff84f654 122 if (!empty($sure) && $sure == md5($deletecat->timemodified)) {
123 /// Send the children categories to live with their grandparent
124 if ($childcats = get_records("course_categories", "parent", $deletecat->id)) {
125 foreach ($childcats as $childcat) {
126 if (! set_field("course_categories", "parent", $deletecat->parent, "id", $childcat->id)) {
127 error("Could not update a child category!", "index.php");
128 }
d2b6ba70 129 }
130 }
ff84f654 131
132 /// If the grandparent is a valid (non-zero) category, then
133 /// send the children courses to live with their grandparent as well
134 if ($deletecat->parent) {
135 if ($childcourses = get_records("course", "category", $deletecat->id)) {
136 foreach ($childcourses as $childcourse) {
137 if (! set_field("course", "category", $deletecat->parent, "id", $childcourse->id)) {
138 error("Could not update a child course!", "index.php");
139 }
17c4a011 140 }
54e7130e 141 }
142 }
ff84f654 143
144 /// Finally delete the category itself
145 if (delete_records("course_categories", "id", $deletecat->id)) {
146 notify(get_string("categorydeleted", "", $deletecat->name));
147 }
54e7130e 148 }
ff84f654 149 else {
150 $strdeletecategorycheck = get_string("deletecategorycheck","",$deletecat->name);
151 notice_yesno("$strdeletecategorycheck",
152 "index.php?delete=$delete&amp;sure=".md5($deletecat->timemodified)."&amp;sesskey=$USER->sesskey",
153 "index.php?sesskey=$USER->sesskey");
d4a18e34 154
155 admin_externalpage_print_footer($adminroot);
ff84f654 156 exit();
17c4a011 157 }
d2b6ba70 158 }
ba2e5d73 159 }
0a263205 160
ba2e5d73 161
d2b6ba70 162/// Create a default category if necessary
163 if (!$categories = get_categories()) { /// No category yet!
164 // Try and make one
165 unset($tempcat);
166 $tempcat->name = get_string("miscellaneous");
167 if (!$tempcat->id = insert_record("course_categories", $tempcat)) {
168 error("Serious error: Could not create a default category!");
169 }
170 }
171
172
173/// Move a category to a new parent if required
174
7168423c 175 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
d2b6ba70 176 if ($tempcat = get_record("course_categories", "id", $move)) {
177 if ($tempcat->parent != $moveto) {
178 if (! set_field("course_categories", "parent", $moveto, "id", $tempcat->id)) {
179 notify("Could not update that category!");
180 }
181 }
182 }
183 }
184
185
186/// Hide or show a category
7168423c 187 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
188 if (!empty($hide)) {
d2b6ba70 189 $tempcat = get_record("course_categories", "id", $hide);
190 $visible = 0;
191 } else {
192 $tempcat = get_record("course_categories", "id", $show);
193 $visible = 1;
194 }
195 if ($tempcat) {
196 if (! set_field("course_categories", "visible", $visible, "id", $tempcat->id)) {
197 notify("Could not update that category!");
198 }
199 if (! set_field("course", "visible", $visible, "category", $tempcat->id)) {
200 notify("Could not hide/show any courses in this category !");
201 }
202 }
203 }
204
205
206/// Move a category up or down
207
7168423c 208 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
d2b6ba70 209
210 $swapcategory = NULL;
211 $movecategory = NULL;
212
7168423c 213 if (!empty($moveup)) {
d2b6ba70 214 if ($movecategory = get_record("course_categories", "id", $moveup)) {
215 $categories = get_categories("$movecategory->parent");
216
217 foreach ($categories as $category) {
218 if ($category->id == $movecategory->id) {
219 break;
220 }
221 $swapcategory = $category;
222 }
223 }
224 }
7168423c 225 if (!empty($movedown)) {
d2b6ba70 226 if ($movecategory = get_record("course_categories", "id", $movedown)) {
227 $categories = get_categories("$movecategory->parent");
ba2e5d73 228
d2b6ba70 229 $choosenext = false;
230 foreach ($categories as $category) {
231 if ($choosenext) {
232 $swapcategory = $category;
233 break;
234 }
235 if ($category->id == $movecategory->id) {
236 $choosenext = true;
237 }
238 }
239 }
240 }
241 if ($swapcategory and $movecategory) { // Renumber everything for robustness
242 $count=0;
243 foreach ($categories as $category) {
244 $count++;
245 if ($category->id == $swapcategory->id) {
246 $category = $movecategory;
247 } else if ($category->id == $movecategory->id) {
248 $category = $swapcategory;
249 }
250 if (! set_field("course_categories", "sortorder", $count, "id", $category->id)) {
251 notify("Could not update that category!");
252 }
253 }
254 }
255 }
256
257/// Find the default category (the one with the lowest ID)
c2cb4545 258 $categories = get_categories();
d2b6ba70 259 $default = 99999;
260 foreach ($categories as $category) {
d2b6ba70 261 if ($category->id < $default) {
262 $default = $category->id;
263 }
264 }
265
266/// Find any orphan courses that don't yet have a valid category and set to default
09575480 267 if ($courses = get_courses(NULL,NULL,'c.id, c.category, c.sortorder, c.visible')) {
d2b6ba70 268 foreach ($courses as $course) {
7a7ac154 269 if ($course->category and !isset($categories[$course->category])) {
d2b6ba70 270 set_field("course", "category", $default, "id", $course->id);
271 }
272 }
273 }
6bc502cc 274
275 fix_course_sortorder();
d2b6ba70 276
277/// Print form for creating new categories
61240489 278 if (has_capability('moodle/category:create', $context)) {
44b843b6 279 echo '<div class="addcategory">';
b7dc2256 280 echo "<form id=\"addform\" action=\"index.php\" method=\"post\">";
44b843b6 281 echo '<fieldset class="invisiblefieldset">';
61240489 282 echo "<input type=\"text\" size=\"30\" alt=\"$straddnewcategory\" name=\"addcategory\" />";
283 echo "<input type=\"submit\" value=\"$straddnewcategory\" />";
284 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
44b843b6 285 echo "</fieldset>";
61240489 286 echo "</form>";
44b843b6 287 echo "</div>";
61240489 288 }
d2b6ba70 289
290/// Print out the categories with all the knobs
291
292 $strcategories = get_string("categories");
293 $strcourses = get_string("courses");
294 $strmovecategoryto = get_string("movecategoryto");
295 $stredit = get_string("edit");
296
297 $displaylist = array();
298 $parentlist = array();
299
300 $displaylist[0] = get_string("top");
301 make_categories_list($displaylist, $parentlist, "");
302
44b843b6 303 echo "<table class=\"generalbox\"><tr>";
54926e78 304 echo "<th scope=\"col\">$strcategories</th>";
305 echo "<th scope=\"col\">$strcourses</th>";
306 echo "<th scope=\"col\">$stredit</th>";
307 echo "<th scope=\"col\">$strmovecategoryto</th>";
cc57a111 308 echo "</tr>";
d2b6ba70 309
310 print_category_edit(NULL, $displaylist, $parentlist);
311
312 echo "</table>";
f2bb0045 313 echo "<br />";
d2b6ba70 314
f2bb0045 315 /// Print link to create a new course
c432fd32 316 echo "<center>";
f2bb0045 317 unset($options);
a8990295 318 $options["category"] = $category->id;
bbbf2d40 319
320
0468976c 321 if (has_capability('moodle/course:create', $context)) {
61240489 322 print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
bbbf2d40 323 }
0705ff84 324 print_single_button('pending.php',NULL, get_string('coursespending'),"get");
f2bb0045 325 echo "<br />";
326 echo "</center>";
c2cb4545 327
6e4dc10f 328 admin_externalpage_print_footer($adminroot);
d2b6ba70 329
330
331
332function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
333/// Recursive function to print all the categories ready for editing
334
810393c8 335 global $CFG, $USER;
d2b6ba70 336
337 static $str = '';
d2b6ba70 338
339 if (empty($str)) {
340 $str->delete = get_string("delete");
341 $str->moveup = get_string("moveup");
342 $str->movedown = get_string("movedown");
343 $str->edit = get_string("editthiscategory");
344 $str->hide = get_string("hide");
345 $str->show = get_string("show");
346 }
c2cb4545 347
d2b6ba70 348 if ($category) {
61240489 349 $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
350
810393c8 351 echo "<tr><td align=\"left\" nowrap=\"nowrap\">";
d2b6ba70 352 for ($i=0; $i<$depth;$i++) {
353 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
354 }
355 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
7ff99cb6 356 echo "<a $linkcss title=\"$str->edit\" href=\"category.php?id=$category->id&amp;categoryedit=on&amp;sesskey=$USER->sesskey\">$category->name</a>";
d2b6ba70 357 echo "</td>";
358
9936fe81 359 echo "<td align=\"right\">$category->coursecount</td>";
d2b6ba70 360
361 echo "<td nowrap=\"nowrap\">"; /// Print little icons
362
61240489 363 if (has_capability('moodle/category:delete', $context)) {
364 echo "<a title=\"$str->delete\" href=\"index.php?delete=$category->id&amp;sesskey=$USER->sesskey\"><img".
0d905d9f 365 " src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$str->delete\" /></a> ";
61240489 366 }
367
368 if (has_capability('moodle/category:visibility', $context)) {
369 if (!empty($category->visible)) {
370 echo "<a title=\"$str->hide\" href=\"index.php?hide=$category->id&amp;sesskey=$USER->sesskey\"><img".
0d905d9f 371 " src=\"$CFG->pixpath/t/hide.gif\" class=\"iconsmall\" alt=\"$str->hide\" /></a> ";
61240489 372 } else {
373 echo "<a title=\"$str->show\" href=\"index.php?show=$category->id&amp;sesskey=$USER->sesskey\"><img".
0d905d9f 374 " src=\"$CFG->pixpath/t/show.gif\" class=\"iconsmall\" alt=\"$str->show\" /></a> ";
61240489 375 }
376 }
d2b6ba70 377
378 if ($up) {
d99ceac2 379 echo "<a title=\"$str->moveup\" href=\"index.php?moveup=$category->id&amp;sesskey=$USER->sesskey\"><img".
0d905d9f 380 " src=\"$CFG->pixpath/t/up.gif\" class=\"iconsmall\" alt=\"$str->moveup\" /></a> ";
d2b6ba70 381 }
382 if ($down) {
d99ceac2 383 echo "<a title=\"$str->movedown\" href=\"index.php?movedown=$category->id&amp;sesskey=$USER->sesskey\"><img".
0d905d9f 384 " src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$str->movedown\" /></a> ";
d2b6ba70 385 }
386 echo "</td>";
387
388 echo "<td align=\"left\" width=\"0\">";
389 $tempdisplaylist = $displaylist;
390 unset($tempdisplaylist[$category->id]);
391 foreach ($parentslist as $key => $parents) {
392 if (in_array($category->id, $parents)) {
393 unset($tempdisplaylist[$key]);
394 }
395 }
d99ceac2 396 popup_form ("index.php?move=$category->id&amp;sesskey=$USER->sesskey&amp;moveto=", $tempdisplaylist, "moveform$category->id", "$category->parent", "", "", "", false);
d2b6ba70 397 echo "</td>";
398 echo "</tr>";
c2cb4545 399 } else {
d2b6ba70 400 $category->id = "0";
c2cb4545 401 }
f9903ed0 402
d2b6ba70 403 if ($categories = get_categories($category->id)) { // Print all the children recursively
404 $countcats = count($categories);
405 $count = 0;
406 $first = true;
407 $last = false;
408 foreach ($categories as $cat) {
409 $count++;
410 if ($count == $countcats) {
411 $last = true;
412 }
413 $up = $first ? false : true;
414 $down = $last ? false : true;
415 $first = false;
f9903ed0 416
d2b6ba70 417 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);
418 }
419 }
420}
f9903ed0 421
422
d2b6ba70 423?>