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
) {
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
);
34 $adminediting = false;
38 /// Unless it's an editing admin, just print the regular listing of courses/categories
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();
54 $strfulllistofcourses = get_string("fulllistofcourses");
55 print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, $strfulllistofcourses,
56 '', '', true, update_categories_button());
57 print_courses(0, "80%");
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
))) { // 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 print_single_button("edit.php", NULL, get_string("addnewcourse"), "get");
69 if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM
, SITEID
)) and !empty($CFG->enablecourserequests
)) {
70 print_single_button('pending.php',NULL, get_string('coursespending'),"get");
77 /// From now on is all the admin functions
80 require_once($CFG->libdir
.'/adminlib.php');
81 $adminroot = admin_get_root();
82 admin_externalpage_setup('coursemgmt', $adminroot);
87 $stradministration = get_string("administration");
88 $strcategories = get_string("categories");
89 $strcategory = get_string("category");
90 $strcourses = get_string("courses");
91 $stredit = get_string("edit");
92 $strdelete = get_string("delete");
93 $straction = get_string("action");
94 $straddnewcategory = get_string("addnewcategory");
98 admin_externalpage_print_header($adminroot);
100 print_heading($strcategories);
103 /// If data for a new category was submitted, then add it
104 if ($form = data_submitted() and confirm_sesskey() and has_capability('moodle/category:create', $context)) {
105 if (!empty($form->addcategory
)) {
107 $newcategory->name
= $form->addcategory
;
108 $newcategory->sortorder
= 999;
109 if (!insert_record("course_categories", $newcategory)) {
110 notify("Could not insert the new category '$newcategory->name'");
112 notify(get_string("categoryadded", "", $newcategory->name
));
118 /// Delete a category if necessary
120 if (!empty($delete) and confirm_sesskey()) {
122 // context is coursecat, if not present admins should have it set in site level
123 $context = get_context_instance(CONTEXT_COURSECAT
, $delete);
124 if ($deletecat = get_record("course_categories", "id", $delete) and has_capability('moodle/category:delete', $context)) {
125 if (!empty($sure) && $sure == md5($deletecat->timemodified
)) {
126 /// Send the children categories to live with their grandparent
127 if ($childcats = get_records("course_categories", "parent", $deletecat->id
)) {
128 foreach ($childcats as $childcat) {
129 if (! set_field("course_categories", "parent", $deletecat->parent
, "id", $childcat->id
)) {
130 error("Could not update a child category!", "index.php");
135 /// If the grandparent is a valid (non-zero) category, then
136 /// send the children courses to live with their grandparent as well
137 if ($deletecat->parent
) {
138 if ($childcourses = get_records("course", "category", $deletecat->id
)) {
139 foreach ($childcourses as $childcourse) {
140 if (! set_field("course", "category", $deletecat->parent
, "id", $childcourse->id
)) {
141 error("Could not update a child course!", "index.php");
147 /// Finally delete the category itself
148 if (delete_records("course_categories", "id", $deletecat->id
)) {
149 notify(get_string("categorydeleted", "", $deletecat->name
));
153 $strdeletecategorycheck = get_string("deletecategorycheck","",$deletecat->name
);
154 notice_yesno("$strdeletecategorycheck",
155 "index.php?delete=$delete&sure=".md5($deletecat->timemodified
)."&sesskey=$USER->sesskey",
156 "index.php?sesskey=$USER->sesskey");
163 /// Create a default category if necessary
164 if (!$categories = get_categories()) { /// No category yet!
167 $tempcat->name
= get_string("miscellaneous");
168 if (!$tempcat->id
= insert_record("course_categories", $tempcat)) {
169 error("Serious error: Could not create a default category!");
174 /// Move a category to a new parent if required
176 if (!empty($move) and ($moveto>=0) and confirm_sesskey()) {
177 if ($tempcat = get_record("course_categories", "id", $move)) {
178 if ($tempcat->parent
!= $moveto) {
179 if (! set_field("course_categories", "parent", $moveto, "id", $tempcat->id
)) {
180 notify("Could not update that category!");
187 /// Hide or show a category
188 if ((!empty($hide) or !empty($show)) and confirm_sesskey()) {
190 $tempcat = get_record("course_categories", "id", $hide);
193 $tempcat = get_record("course_categories", "id", $show);
197 if (! set_field("course_categories", "visible", $visible, "id", $tempcat->id
)) {
198 notify("Could not update that category!");
200 if (! set_field("course", "visible", $visible, "category", $tempcat->id
)) {
201 notify("Could not hide/show any courses in this category !");
207 /// Move a category up or down
209 if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
211 $swapcategory = NULL;
212 $movecategory = NULL;
214 if (!empty($moveup)) {
215 if ($movecategory = get_record("course_categories", "id", $moveup)) {
216 $categories = get_categories("$movecategory->parent");
218 foreach ($categories as $category) {
219 if ($category->id
== $movecategory->id
) {
222 $swapcategory = $category;
226 if (!empty($movedown)) {
227 if ($movecategory = get_record("course_categories", "id", $movedown)) {
228 $categories = get_categories("$movecategory->parent");
231 foreach ($categories as $category) {
233 $swapcategory = $category;
236 if ($category->id
== $movecategory->id
) {
242 if ($swapcategory and $movecategory) { // Renumber everything for robustness
244 foreach ($categories as $category) {
246 if ($category->id
== $swapcategory->id
) {
247 $category = $movecategory;
248 } else if ($category->id
== $movecategory->id
) {
249 $category = $swapcategory;
251 if (! set_field("course_categories", "sortorder", $count, "id", $category->id
)) {
252 notify("Could not update that category!");
258 /// Find the default category (the one with the lowest ID)
259 $categories = get_categories();
261 foreach ($categories as $category) {
262 if ($category->id
< $default) {
263 $default = $category->id
;
267 /// Find any orphan courses that don't yet have a valid category and set to default
268 if ($courses = get_courses(NULL,NULL,'c.id, c.category, c.sortorder, c.visible')) {
269 foreach ($courses as $course) {
270 if ($course->category
and !isset($categories[$course->category
])) {
271 set_field("course", "category", $default, "id", $course->id
);
276 fix_course_sortorder();
278 /// Print form for creating new categories
279 if (has_capability('moodle/category:create', $context)) {
281 echo "<form name=\"addform\" action=\"index.php\" method=\"post\">";
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\" />";
291 /// Print out the categories with all the knobs
293 $strcategories = get_string("categories");
294 $strcourses = get_string("courses");
295 $strmovecategoryto = get_string("movecategoryto");
296 $stredit = get_string("edit");
298 $displaylist = array();
299 $parentlist = array();
301 $displaylist[0] = get_string("top");
302 make_categories_list($displaylist, $parentlist, "");
304 echo "<table align=\"center\" border=\"0\" cellspacing=\"2\" cellpadding=\"5\" class=\"generalbox\"><tr>";
305 echo "<th>$strcategories</th>";
306 echo "<th>$strcourses</th>";
307 echo "<th>$stredit</th>";
308 echo "<th>$strmovecategoryto</th>";
311 print_category_edit(NULL, $displaylist, $parentlist);
316 /// Print link to create a new course
319 $options["category"] = $category->id
;
322 if (has_capability('moodle/course:create', $context)) {
323 print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
325 print_single_button('pending.php',NULL, get_string('coursespending'),"get");
329 admin_externalpage_print_footer($adminroot);
333 function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
334 /// Recursive function to print all the categories ready for editing
341 $str->delete
= get_string("delete");
342 $str->moveup
= get_string("moveup");
343 $str->movedown
= get_string("movedown");
344 $str->edit
= get_string("editthiscategory");
345 $str->hide
= get_string("hide");
346 $str->show
= get_string("show");
350 $context = get_context_instance(CONTEXT_COURSECAT
, $category->id
);
352 echo "<tr><td align=\"left\" nowrap=\"nowrap\">";
353 for ($i=0; $i<$depth;$i++
) {
354 echo " ";
356 $linkcss = $category->visible ?
"" : " class=\"dimmed\" ";
357 echo "<a $linkcss title=\"$str->edit\" href=\"category.php?id=$category->id&categoryedit=on&sesskey=$USER->sesskey\">$category->name</a>";
360 echo "<td align=\"right\">$category->coursecount</td>";
362 echo "<td nowrap=\"nowrap\">"; /// Print little icons
364 if (has_capability('moodle/category:delete', $context)) {
365 echo "<a title=\"$str->delete\" href=\"index.php?delete=$category->id&sesskey=$USER->sesskey\"><img".
366 " src=\"$CFG->pixpath/t/delete.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
369 if (has_capability('moodle/category:visibility', $context)) {
370 if (!empty($category->visible
)) {
371 echo "<a title=\"$str->hide\" href=\"index.php?hide=$category->id&sesskey=$USER->sesskey\"><img".
372 " src=\"$CFG->pixpath/t/hide.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
374 echo "<a title=\"$str->show\" href=\"index.php?show=$category->id&sesskey=$USER->sesskey\"><img".
375 " src=\"$CFG->pixpath/t/show.gif\" height=\"11\" width=\"11\" border=\"0\"alt=\"\" /></a> ";
380 echo "<a title=\"$str->moveup\" href=\"index.php?moveup=$category->id&sesskey=$USER->sesskey\"><img".
381 " src=\"$CFG->pixpath/t/up.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"$str->moveup\" /></a> ";
384 echo "<a title=\"$str->movedown\" href=\"index.php?movedown=$category->id&sesskey=$USER->sesskey\"><img".
385 " src=\"$CFG->pixpath/t/down.gif\" height=\"11\" width=\"11\" border=\"0\"alt=\"$str->movedown\" /></a> ";
389 echo "<td align=\"left\" width=\"0\">";
390 $tempdisplaylist = $displaylist;
391 unset($tempdisplaylist[$category->id
]);
392 foreach ($parentslist as $key => $parents) {
393 if (in_array($category->id
, $parents)) {
394 unset($tempdisplaylist[$key]);
397 popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", "$category->parent", "", "", "", false);
404 if ($categories = get_categories($category->id
)) { // Print all the children recursively
405 $countcats = count($categories);
409 foreach ($categories as $cat) {
411 if ($count == $countcats) {
414 $up = $first ?
false : true;
415 $down = $last ?
false : true;
418 print_category_edit($cat, $displaylist, $parentslist, $depth+
1, $up, $down);