New help translation
[moodle.git] / course / index.php
blobc83991dae7b941b1dd73072da78bd45386750315
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");
9 if (!$site = get_site()) {
10 error("Site isn't defined!");
13 if (isadmin()) {
14 if (isset($_GET['edit'])) {
15 if ($edit == "on") {
16 $USER->editing = true;
17 } else if ($edit == "off") {
18 $USER->editing = false;
23 $adminediting = (isadmin() and !empty($USER->editing));
26 /// Unless it's an editing admin, just print the regular listing of courses/categories
28 if (!$adminediting) {
29 $countcategories = count_records("course_categories");
31 if ($countcategories > 1) {
32 $strcourses = get_string("courses");
33 print_header($strcourses, $strcourses, $strcourses, "", "", true, update_categories_button());
34 print_heading(get_string("categories"));
35 print_simple_box_start("center", "50%", "#FFFFFF", 5, "categorybox");
36 print_whole_category_list();
37 print_simple_box_end();
38 print_course_search();
39 } else {
40 $strfulllistofcourses = get_string("fulllistofcourses");
41 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, $strfulllistofcourses);
42 print_courses(0, "80%");
45 if (iscreator()) { // Print link to create a new course
46 echo "<center><p>";
47 unset($options);
48 $option["category"] = $category->id;
49 print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
50 echo "</p></center>";
53 print_footer();
54 exit;
57 /// From now on is all the admin functions
59 require_login();
61 if (!isadmin()) {
62 error("Only administrators can use this page!");
65 /// Print headings
67 $stradministration = get_string("administration");
68 $strcategories = get_string("categories");
69 $strcategory = get_string("category");
70 $strcourses = get_string("courses");
71 $stredit = get_string("edit");
72 $strdelete = get_string("delete");
73 $straction = get_string("action");
74 $straddnewcategory = get_string("addnewcategory");
76 print_header("$site->shortname: $strcategories", "$site->fullname",
77 "<a href=\"../$CFG->admin/index.php\">$stradministration</a> -> $strcategories",
78 "addform.addcategory", "", true, update_categories_button());
80 print_heading($strcategories);
83 /// If data for a new category was submitted, then add it
84 if ($form = data_submitted()) {
85 if (!empty($form->addcategory)) {
86 unset($newcategory);
87 $newcategory->name = $form->addcategory;
88 $newcategory->sortorder = 999;
89 if (!insert_record("course_categories", $newcategory)) {
90 notify("Could not insert the new category '$newcategory->name'");
91 } else {
92 notify(get_string("categoryadded", "", $newcategory->name));
98 /// Delete a category if necessary
100 if (isset($delete)) {
101 if ($tempcat = get_record("course_categories", "id", $delete)) {
102 if (delete_records("course_categories", "id", $tempcat->id)) {
103 notify(get_string("categorydeleted", "", $tempcat->name));
105 if ($children = get_records("course_categories", "parent", $tempcat->id)) {
106 // Send the children to live with their grandparent
107 foreach ($children as $childcat) {
108 if (! set_field("course_categories", "parent", $tempcat->parent, "id", $childcat->id)) {
109 notify("Could not update a child category!");
117 /// Create a default category if necessary
118 if (!$categories = get_categories()) { /// No category yet!
119 // Try and make one
120 unset($tempcat);
121 $tempcat->name = get_string("miscellaneous");
122 if (!$tempcat->id = insert_record("course_categories", $tempcat)) {
123 error("Serious error: Could not create a default category!");
128 /// Move a category to a new parent if required
130 if (isset($move) and isset($moveto)) {
131 if ($tempcat = get_record("course_categories", "id", $move)) {
132 if ($tempcat->parent != $moveto) {
133 if (! set_field("course_categories", "parent", $moveto, "id", $tempcat->id)) {
134 notify("Could not update that category!");
141 /// Hide or show a category
142 if (isset($hide) or isset($show)) {
143 if (isset($hide)) {
144 $tempcat = get_record("course_categories", "id", $hide);
145 $visible = 0;
146 } else {
147 $tempcat = get_record("course_categories", "id", $show);
148 $visible = 1;
150 if ($tempcat) {
151 if (! set_field("course_categories", "visible", $visible, "id", $tempcat->id)) {
152 notify("Could not update that category!");
154 if (! set_field("course", "visible", $visible, "category", $tempcat->id)) {
155 notify("Could not hide/show any courses in this category !");
161 /// Move a category up or down
163 if (isset($moveup) or isset($movedown)) {
165 $swapcategory = NULL;
166 $movecategory = NULL;
168 if (isset($moveup)) {
169 if ($movecategory = get_record("course_categories", "id", $moveup)) {
170 $categories = get_categories("$movecategory->parent");
172 foreach ($categories as $category) {
173 if ($category->id == $movecategory->id) {
174 break;
176 $swapcategory = $category;
180 if (isset($movedown)) {
181 if ($movecategory = get_record("course_categories", "id", $movedown)) {
182 $categories = get_categories("$movecategory->parent");
184 $choosenext = false;
185 foreach ($categories as $category) {
186 if ($choosenext) {
187 $swapcategory = $category;
188 break;
190 if ($category->id == $movecategory->id) {
191 $choosenext = true;
196 if ($swapcategory and $movecategory) { // Renumber everything for robustness
197 $count=0;
198 foreach ($categories as $category) {
199 $count++;
200 if ($category->id == $swapcategory->id) {
201 $category = $movecategory;
202 } else if ($category->id == $movecategory->id) {
203 $category = $swapcategory;
205 if (! set_field("course_categories", "sortorder", $count, "id", $category->id)) {
206 notify("Could not update that category!");
212 /// Find the default category (the one with the lowest ID)
213 $categories = get_categories();
214 $default = 99999;
215 foreach ($categories as $category) {
216 fix_course_sortorder($category->id);
217 if ($category->id < $default) {
218 $default = $category->id;
222 /// Find any orphan courses that don't yet have a valid category and set to default
223 if ($courses = get_courses()) {
224 $foundorphans = false;
225 foreach ($courses as $course) {
226 if ($course->category and !isset($categories[$course->category])) {
227 set_field("course", "category", $default, "id", $course->id);
228 $foundorphans = true;
231 if ($foundorphans) {
232 fix_course_sortorder($default);
236 /// Print form for creating new categories
238 echo "<center>";
239 echo "<form name=\"addform\" action=\"index.php\" method=\"post\">";
240 echo "<input type=\"text\" size=30 name=\"addcategory\">";
241 echo "<input type=\"submit\" value=\"$straddnewcategory\">";
242 echo "</form>";
243 echo "</center>";
245 echo "<br />";
248 /// Print out the categories with all the knobs
250 $strcategories = get_string("categories");
251 $strcourses = get_string("courses");
252 $strmovecategoryto = get_string("movecategoryto");
253 $stredit = get_string("edit");
255 $displaylist = array();
256 $parentlist = array();
258 $displaylist[0] = get_string("top");
259 make_categories_list($displaylist, $parentlist, "");
261 echo "<table align=\"center\" border=0 cellspacing=2 cellpadding=5 class=\"generalbox\"><tr>";
262 echo "<th>$strcategories</th>";
263 echo "<th>$strcourses</th>";
264 echo "<th>$stredit</th>";
265 echo "<th>$strmovecategoryto</th>";
267 print_category_edit(NULL, $displaylist, $parentlist);
269 echo "</table>";
270 echo "<br />";
272 /// Print link to create a new course
273 echo "<center>";
274 unset($options);
275 $options["category"] = $category->id;
276 print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
277 echo "<br />";
278 echo "</center>";
280 print_footer();
284 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
285 /// Recursive function to print all the categories ready for editing
287 global $THEME, $CFG;
289 static $str = '';
290 static $pixpath = '';
292 if (empty($str)) {
293 $str->delete = get_string("delete");
294 $str->moveup = get_string("moveup");
295 $str->movedown = get_string("movedown");
296 $str->edit = get_string("editthiscategory");
297 $str->hide = get_string("hide");
298 $str->show = get_string("show");
301 if (empty($pixpath)) {
302 if (empty($THEME->custompix)) {
303 $pixpath = "$CFG->wwwroot/pix";
304 } else {
305 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
309 if ($category) {
310 echo "<tr><td align=\"left\" nowrap=\"nowrap\" bgcolor=\"$THEME->cellcontent\">";
311 echo "<p>";
312 for ($i=0; $i<$depth;$i++) {
313 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
315 $linkcss = $category->visible ? "" : " class=\"dimmed\" ";
316 echo "<a $linkcss title=\"$str->edit\" href=\"category.php?id=$category->id&edit=on\">$category->name</a>";
317 echo "</p>";
318 echo "</td>";
320 echo "<td align=\"right\">$category->coursecount</td>";
322 echo "<td nowrap=\"nowrap\">"; /// Print little icons
324 echo "<a title=\"$str->delete\" href=\"index.php?delete=$category->id\"><img".
325 " src=\"$pixpath/t/delete.gif\" height=11 width=11 border=0></a> ";
327 if (!empty($category->visible)) {
328 echo "<a title=\"$str->hide\" href=\"index.php?hide=$category->id\"><img".
329 " src=\"$pixpath/t/hide.gif\" height=11 width=11 border=0></a> ";
330 } else {
331 echo "<a title=\"$str->show\" href=\"index.php?show=$category->id\"><img".
332 " src=\"$pixpath/t/show.gif\" height=11 width=11 border=0></a> ";
335 if ($up) {
336 echo "<a title=\"$str->moveup\" href=\"index.php?moveup=$category->id\"><img".
337 " src=\"$pixpath/t/up.gif\" height=11 width=11 border=0></a> ";
339 if ($down) {
340 echo "<a title=\"$str->movedown\" href=\"index.php?movedown=$category->id\"><img".
341 " src=\"$pixpath/t/down.gif\" height=11 width=11 border=0></a> ";
343 echo "</td>";
345 echo "<td align=\"left\" width=\"0\">";
346 $tempdisplaylist = $displaylist;
347 unset($tempdisplaylist[$category->id]);
348 foreach ($parentslist as $key => $parents) {
349 if (in_array($category->id, $parents)) {
350 unset($tempdisplaylist[$key]);
353 popup_form ("index.php?move=$category->id&moveto=", $tempdisplaylist, "moveform$category->id", "$category->parent", "", "", "", false);
354 echo "</td>";
355 echo "</tr>";
356 } else {
357 $category->id = "0";
360 if ($categories = get_categories($category->id)) { // Print all the children recursively
361 $countcats = count($categories);
362 $count = 0;
363 $first = true;
364 $last = false;
365 foreach ($categories as $cat) {
366 $count++;
367 if ($count == $countcats) {
368 $last = true;
370 $up = $first ? false : true;
371 $down = $last ? false : true;
372 $first = false;
374 print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);