Automatic installer.php lang files by installer_builder (20080623)
[moodle.git] / course / index.php
blob1d1d7da848897638d55fd942f10fcf847bc32693
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");
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 $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);
18 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
20 if (!$site = get_site()) {
21 error("Site isn't defined!");
24 if ($CFG->forcelogin) {
25 require_login();
28 if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
29 if ($categoryedit !== -1) {
30 $USER->categoryediting = $categoryedit;
32 $adminediting = !empty($USER->categoryediting);
33 } else {
34 $adminediting = false;
38 /// Unless it's an editing admin, just print the regular listing of courses/categories
40 if (!$adminediting) {
41 $countcategories = count_records("course_categories");
43 if ($countcategories > 1 || ($countcategories == 1 && count_records('course') > 200)) {
44 $strcourses = get_string("courses");
45 $strcategories = get_string("categories");
46 print_header("$site->shortname: $strcategories", $strcourses,
47 $strcategories, "", "", true, update_categories_button());
48 print_heading($strcategories);
49 print_simple_box_start("center", "50%", "#FFFFFF", 5, "categorybox");
50 print_whole_category_list();
51 print_simple_box_end();
52 print_course_search();
53 } else {
54 $strfulllistofcourses = get_string("fulllistofcourses");
55 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, $strfulllistofcourses,
56 '', '', true, update_categories_button());
57 print_courses(0, "80%");
60 echo "<center>";
62 /// I am not sure this context in the next has_capability call is correct.
63 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
64 print_single_button("request.php", NULL, get_string("courserequest"), "get");
66 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Print link to create a new course
67 /// Get the 1st available category
68 $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
69 print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
71 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) {
72 print_single_button('pending.php',NULL, get_string('coursespending'),"get");
74 echo "</center>";
75 print_footer();
76 exit;
79 /// From now on is all the admin functions
82 require_once($CFG->libdir.'/adminlib.php');
83 $adminroot = admin_get_root();
84 admin_externalpage_setup('coursemgmt', $adminroot);
87 /// Print headings
89 $stradministration = get_string("administration");
90 $strcategories = get_string("categories");
91 $strcategory = get_string("category");
92 $strcourses = get_string("courses");
93 $stredit = get_string("edit");
94 $strdelete = get_string("delete");
95 $straction = get_string("action");
96 $straddnewcategory = get_string("addnewcategory");
100 admin_externalpage_print_header($adminroot);
102 print_heading($strcategories);
105 /// If data for a new category was submitted, then add it
106 if ($form = data_submitted() and confirm_sesskey() and has_capability('moodle/category:create', $context)) {
107 if (!empty($form->addcategory)) {
108 unset($newcategory);
109 $newcategory->name = $form->addcategory;
110 $newcategory->sortorder = 999;
111 if (!insert_record("course_categories", $newcategory)) {
112 notify("Could not insert the new category '$newcategory->name'");
113 } else {
114 notify(get_string("categoryadded", "", $newcategory->name));
120 /// Delete a category if necessary
122 if (!empty($delete) and confirm_sesskey()) {
124 // context is coursecat, if not present admins should have it set in site level
125 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
126 if ($deletecat = get_record("course_categories", "id", $delete) and has_capability('moodle/category:delete', $context)) {
127 if (!empty($sure) && $sure == md5($deletecat->timemodified)) {
128 /// Send the children categories to live with their grandparent
129 if ($childcats = get_records("course_categories", "parent", $deletecat->id)) {
130 foreach ($childcats as $childcat) {
131 if (! set_field("course_categories", "parent", $deletecat->parent, "id", $childcat->id)) {
132 error("Could not update a child category!", "index.php");
137 /// If the grandparent is a valid (non-zero) category, then
138 /// send the children courses to live with their grandparent as well
139 if ($deletecat->parent) {
140 if ($childcourses = get_records("course", "category", $deletecat->id)) {
141 foreach ($childcourses as $childcourse) {
142 if (! set_field("course", "category", $deletecat->parent, "id", $childcourse->id)) {
143 error("Could not update a child course!", "index.php");
149 /// Finally delete the category itself
150 if (delete_records("course_categories", "id", $deletecat->id)) {
151 notify(get_string("categorydeleted", "", $deletecat->name));
154 else {
155 $strdeletecategorycheck = get_string("deletecategorycheck","",$deletecat->name);
156 notice_yesno("$strdeletecategorycheck",
157 "index.php?delete=$delete&amp;sure=".md5($deletecat->timemodified)."&amp;sesskey=$USER->sesskey",
158 "index.php?sesskey=$USER->sesskey");
159 exit();
165 /// Create a default category if necessary
166 if (!$categories = get_categories()) { /// No category yet!
167 // Try and make one
168 unset($tempcat);
169 $tempcat->name = get_string("miscellaneous");
170 if (!$tempcat->id = insert_record("course_categories", $tempcat)) {
171 error("Serious error: Could not create a default category!");
176 /// Move a category to a new parent if required
178 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
179 if ($tempcat = get_record("course_categories", "id", $move)) {
180 if ($tempcat->parent != $moveto) {
181 if (! set_field("course_categories", "parent", $moveto, "id", $tempcat->id)) {
182 notify("Could not update that category!");
189 /// Hide or show a category
190 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
191 if (!empty($hide)) {
192 $tempcat = get_record("course_categories", "id", $hide);
193 $visible = 0;
194 } else {
195 $tempcat = get_record("course_categories", "id", $show);
196 $visible = 1;
198 if ($tempcat) {
199 if (! set_field("course_categories", "visible", $visible, "id", $tempcat->id)) {
200 notify("Could not update that category!");
202 if (! set_field("course", "visible", $visible, "category", $tempcat->id)) {
203 notify("Could not hide/show any courses in this category !");
209 /// Move a category up or down
211 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
213 $swapcategory = NULL;
214 $movecategory = NULL;
216 if (!empty($moveup)) {
217 if ($movecategory = get_record("course_categories", "id", $moveup)) {
218 $categories = get_categories("$movecategory->parent");
220 foreach ($categories as $category) {
221 if ($category->id == $movecategory->id) {
222 break;
224 $swapcategory = $category;
228 if (!empty($movedown)) {
229 if ($movecategory = get_record("course_categories", "id", $movedown)) {
230 $categories = get_categories("$movecategory->parent");
232 $choosenext = false;
233 foreach ($categories as $category) {
234 if ($choosenext) {
235 $swapcategory = $category;
236 break;
238 if ($category->id == $movecategory->id) {
239 $choosenext = true;
244 if ($swapcategory and $movecategory) { // Renumber everything for robustness
245 $count=0;
246 foreach ($categories as $category) {
247 $count++;
248 if ($category->id == $swapcategory->id) {
249 $category = $movecategory;
250 } else if ($category->id == $movecategory->id) {
251 $category = $swapcategory;
253 if (! set_field("course_categories", "sortorder", $count, "id", $category->id)) {
254 notify("Could not update that category!");
260 /// Find the default category (the one with the lowest ID)
261 $categories = get_categories();
262 $default = 99999;
263 foreach ($categories as $category) {
264 if ($category->id < $default) {
265 $default = $category->id;
269 /// Find any orphan courses that don't yet have a valid category and set to default
270 if ($courses = get_courses(NULL,NULL,'c.id, c.category, c.sortorder, c.visible')) {
271 foreach ($courses as $course) {
272 if ($course->category and !isset($categories[$course->category])) {
273 set_field("course", "category", $default, "id", $course->id);
278 fix_course_sortorder();
280 /// Print form for creating new categories
281 if (has_capability('moodle/category:create', $context)) {
282 echo "<center>";
283 echo "<form name=\"addform\" action=\"index.php\" method=\"post\">";
284 echo "<input type=\"text\" size=\"30\" alt=\"$straddnewcategory\" name=\"addcategory\" />";
285 echo "<input type=\"submit\" value=\"$straddnewcategory\" />";
286 echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
287 echo "</form>";
288 echo "</center>";
290 echo "<br />";
293 /// Print out the categories with all the knobs
295 $strcategories = get_string("categories");
296 $strcourses = get_string("courses");
297 $strmovecategoryto = get_string("movecategoryto");
298 $stredit = get_string("edit");
300 $displaylist = array();
301 $parentlist = array();
303 $displaylist[0] = get_string("top");
304 make_categories_list($displaylist, $parentlist, "");
306 echo "<table align=\"center\" border=\"0\" cellspacing=\"2\" cellpadding=\"5\" class=\"generalbox\"><tr>";
307 echo "<th>$strcategories</th>";
308 echo "<th>$strcourses</th>";
309 echo "<th>$stredit</th>";
310 echo "<th>$strmovecategoryto</th>";
311 echo "</tr>";
313 print_category_edit(NULL, $displaylist, $parentlist);
315 echo "</table>";
316 echo "<br />";
318 /// Print link to create a new course
319 echo "<center>";
320 unset($options);
321 $options["category"] = $category->id;
324 if (has_capability('moodle/course:create', $context)) {
325 print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
327 print_single_button('pending.php',NULL, get_string('coursespending'),"get");
328 echo "<br />";
329 echo "</center>";
331 admin_externalpage_print_footer($adminroot);
335 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
336 /// Recursive function to print all the categories ready for editing
338 global $CFG, $USER;
340 static $str = '';
342 if (empty($str)) {
343 $str->delete = get_string("delete");
344 $str->moveup = get_string("moveup");
345 $str->movedown = get_string("movedown");
346 $str->edit = get_string("editthiscategory");
347 $str->hide = get_string("hide");
348 $str->show = get_string("show");
351 if ($category) {
352 $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
354 echo "<tr><td align=\"left\" nowrap=\"nowrap\">";
355 for ($i=0; $i<$depth;$i++) {
356 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
358 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
359 echo "<a $linkcss title=\"$str->edit\" href=\"category.php?id=$category->id&amp;categoryedit=on&amp;sesskey=$USER->sesskey\">$category->name</a>";
360 echo "</td>";
362 echo "<td align=\"right\">$category->coursecount</td>";
364 echo "<td nowrap=\"nowrap\">"; /// Print little icons
366 if (has_capability('moodle/category:delete', $context)) {
367 echo "<a title=\"$str->delete\" href=\"index.php?delete=$category->id&amp;sesskey=$USER->sesskey\"><img".
368 " src=\"$CFG->pixpath/t/delete.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
371 if (has_capability('moodle/category:visibility', $context)) {
372 if (!empty($category->visible)) {
373 echo "<a title=\"$str->hide\" href=\"index.php?hide=$category->id&amp;sesskey=$USER->sesskey\"><img".
374 " src=\"$CFG->pixpath/t/hide.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
375 } else {
376 echo "<a title=\"$str->show\" href=\"index.php?show=$category->id&amp;sesskey=$USER->sesskey\"><img".
377 " src=\"$CFG->pixpath/t/show.gif\" height=\"11\" width=\"11\" border=\"0\"alt=\"\" /></a> ";
381 if ($up) {
382 echo "<a title=\"$str->moveup\" href=\"index.php?moveup=$category->id&amp;sesskey=$USER->sesskey\"><img".
383 " src=\"$CFG->pixpath/t/up.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"$str->moveup\" /></a> ";
385 if ($down) {
386 echo "<a title=\"$str->movedown\" href=\"index.php?movedown=$category->id&amp;sesskey=$USER->sesskey\"><img".
387 " src=\"$CFG->pixpath/t/down.gif\" height=\"11\" width=\"11\" border=\"0\"alt=\"$str->movedown\" /></a> ";
389 echo "</td>";
391 echo "<td align=\"left\" width=\"0\">";
392 $tempdisplaylist = $displaylist;
393 unset($tempdisplaylist[$category->id]);
394 foreach ($parentslist as $key => $parents) {
395 if (in_array($category->id, $parents)) {
396 unset($tempdisplaylist[$key]);
399 popup_form ("index.php?move=$category->id&amp;sesskey=$USER->sesskey&amp;moveto=", $tempdisplaylist, "moveform$category->id", "$category->parent", "", "", "", false);
400 echo "</td>";
401 echo "</tr>";
402 } else {
403 $category->id = "0";
406 if ($categories = get_categories($category->id)) { // Print all the children recursively
407 $countcats = count($categories);
408 $count = 0;
409 $first = true;
410 $last = false;
411 foreach ($categories as $cat) {
412 $count++;
413 if ($count == $countcats) {
414 $last = true;
416 $up = $first ? false : true;
417 $down = $last ? false : true;
418 $first = false;
420 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);