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!");
14 if (isset($_GET['edit'])) {
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
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();
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
48 $option["category"] = $category->id
;
49 print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
57 /// From now on is all the admin functions
62 error("Only administrators can use this page!");
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
)) {
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'");
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!
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)) {
144 $tempcat = get_record("course_categories", "id", $hide);
147 $tempcat = get_record("course_categories", "id", $show);
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
) {
176 $swapcategory = $category;
180 if (isset($movedown)) {
181 if ($movecategory = get_record("course_categories", "id", $movedown)) {
182 $categories = get_categories("$movecategory->parent");
185 foreach ($categories as $category) {
187 $swapcategory = $category;
190 if ($category->id
== $movecategory->id
) {
196 if ($swapcategory and $movecategory) { // Renumber everything for robustness
198 foreach ($categories as $category) {
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();
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;
232 fix_course_sortorder($default);
236 /// Print form for creating new categories
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\">";
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);
272 /// Print link to create a new course
275 $options["category"] = $category->id
;
276 print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
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
290 static $pixpath = '';
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";
305 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
310 echo "<tr><td align=\"left\" nowrap=\"nowrap\" bgcolor=\"$THEME->cellcontent\">";
312 for ($i=0; $i<$depth;$i++
) {
313 echo " ";
315 $linkcss = $category->visible ?
"" : " class=\"dimmed\" ";
316 echo "<a $linkcss title=\"$str->edit\" href=\"category.php?id=$category->id&edit=on\">$category->name</a>";
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> ";
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> ";
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> ";
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> ";
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);
360 if ($categories = get_categories($category->id
)) { // Print all the children recursively
361 $countcats = count($categories);
365 foreach ($categories as $cat) {
367 if ($count == $countcats) {
370 $up = $first ?
false : true;
371 $down = $last ?
false : true;
374 print_category_edit($cat, $displaylist, $parentslist, $depth+
1, $up, $down);