2004-09-12 12:21:27 +00:00
|
|
|
<?php // $Id$
|
2003-08-07 16:01:31 +00:00
|
|
|
// For most people, just lists the course categories
|
|
|
|
// Allows the admin to create, delete and rename course categories
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-05-30 00:33:45 +00:00
|
|
|
require_once("../config.php");
|
|
|
|
require_once("lib.php");
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
if (!$site = get_site()) {
|
|
|
|
error("Site isn't defined!");
|
|
|
|
}
|
|
|
|
|
2004-01-14 08:27:40 +00:00
|
|
|
if ($CFG->forcelogin) {
|
|
|
|
require_login();
|
|
|
|
}
|
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
if (isadmin()) {
|
2004-10-09 13:22:59 +00:00
|
|
|
if (isset($_GET['edit']) and confirm_sesskey()) {
|
2003-08-07 16:01:31 +00:00
|
|
|
if ($edit == "on") {
|
2003-12-30 17:18:06 +00:00
|
|
|
$USER->categoriesediting = true;
|
2003-08-07 16:01:31 +00:00
|
|
|
} else if ($edit == "off") {
|
2003-12-30 17:18:06 +00:00
|
|
|
$USER->categoriesediting = false;
|
2003-08-07 16:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-30 17:18:06 +00:00
|
|
|
$adminediting = (isadmin() and !empty($USER->categoriesediting));
|
2003-08-07 16:01:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Unless it's an editing admin, just print the regular listing of courses/categories
|
|
|
|
|
|
|
|
if (!$adminediting) {
|
|
|
|
$countcategories = count_records("course_categories");
|
|
|
|
|
|
|
|
if ($countcategories > 1) {
|
|
|
|
$strcourses = get_string("courses");
|
2004-02-20 12:56:17 +00:00
|
|
|
$strcategories = get_string("categories");
|
|
|
|
print_header("$site->shortname: $strcategories", $strcourses,
|
|
|
|
$strcategories, "", "", true, update_categories_button());
|
|
|
|
print_heading($strcategories);
|
2003-09-03 15:25:08 +00:00
|
|
|
print_simple_box_start("center", "50%", "#FFFFFF", 5, "categorybox");
|
2003-08-07 16:01:31 +00:00
|
|
|
print_whole_category_list();
|
|
|
|
print_simple_box_end();
|
2003-08-11 09:45:50 +00:00
|
|
|
print_course_search();
|
2003-08-07 16:01:31 +00:00
|
|
|
} else {
|
|
|
|
$strfulllistofcourses = get_string("fulllistofcourses");
|
2005-01-11 07:01:04 +00:00
|
|
|
print_header("$site->shortname: $strfulllistofcourses", $strfulllistofcourses, $strfulllistofcourses,
|
|
|
|
'', '', true, update_categories_button());
|
2003-08-07 16:01:31 +00:00
|
|
|
print_courses(0, "80%");
|
|
|
|
}
|
2003-08-22 12:15:35 +00:00
|
|
|
|
|
|
|
if (iscreator()) { // Print link to create a new course
|
2004-09-23 12:25:49 +00:00
|
|
|
echo "<center>";
|
2004-03-10 07:20:42 +00:00
|
|
|
print_single_button("edit.php", NULL, get_string("addnewcourse"), "get");
|
2004-09-23 12:25:49 +00:00
|
|
|
echo "</center>";
|
2003-08-22 12:15:35 +00:00
|
|
|
}
|
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
print_footer();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// From now on is all the admin functions
|
|
|
|
|
|
|
|
require_login();
|
|
|
|
|
|
|
|
if (!isadmin()) {
|
|
|
|
error("Only administrators can use this page!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print headings
|
|
|
|
|
|
|
|
$stradministration = get_string("administration");
|
2002-09-09 12:37:34 +00:00
|
|
|
$strcategories = get_string("categories");
|
2003-08-07 16:01:31 +00:00
|
|
|
$strcategory = get_string("category");
|
|
|
|
$strcourses = get_string("courses");
|
|
|
|
$stredit = get_string("edit");
|
|
|
|
$strdelete = get_string("delete");
|
|
|
|
$straction = get_string("action");
|
|
|
|
$straddnewcategory = get_string("addnewcategory");
|
2002-09-09 11:48:11 +00:00
|
|
|
|
2004-05-30 00:33:45 +00:00
|
|
|
print_header("$site->shortname: $strcategories", "$site->fullname",
|
2003-08-09 16:53:30 +00:00
|
|
|
"<a href=\"../$CFG->admin/index.php\">$stradministration</a> -> $strcategories",
|
2003-08-07 16:01:31 +00:00
|
|
|
"addform.addcategory", "", true, update_categories_button());
|
2002-09-09 11:48:11 +00:00
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
print_heading($strcategories);
|
|
|
|
|
|
|
|
|
|
|
|
/// If data for a new category was submitted, then add it
|
2004-10-09 13:22:59 +00:00
|
|
|
if ($form = data_submitted() and confirm_sesskey()) {
|
2003-08-07 16:01:31 +00:00
|
|
|
if (!empty($form->addcategory)) {
|
|
|
|
unset($newcategory);
|
|
|
|
$newcategory->name = $form->addcategory;
|
|
|
|
$newcategory->sortorder = 999;
|
|
|
|
if (!insert_record("course_categories", $newcategory)) {
|
|
|
|
notify("Could not insert the new category '$newcategory->name'");
|
|
|
|
} else {
|
|
|
|
notify(get_string("categoryadded", "", $newcategory->name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Delete a category if necessary
|
|
|
|
|
2004-10-09 13:22:59 +00:00
|
|
|
if (isset($delete) and confirm_sesskey()) {
|
2004-01-09 16:54:10 +00:00
|
|
|
if ($deletecat = get_record("course_categories", "id", $delete)) {
|
2004-11-16 02:21:31 +00:00
|
|
|
if (!empty($sure) && $sure == md5($deletecat->timemodified)) {
|
|
|
|
/// Send the children categories to live with their grandparent
|
|
|
|
if ($childcats = get_records("course_categories", "parent", $deletecat->id)) {
|
|
|
|
foreach ($childcats as $childcat) {
|
|
|
|
if (! set_field("course_categories", "parent", $deletecat->parent, "id", $childcat->id)) {
|
|
|
|
error("Could not update a child category!", "index.php");
|
|
|
|
}
|
2003-08-07 16:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-16 02:21:31 +00:00
|
|
|
|
|
|
|
/// If the grandparent is a valid (non-zero) category, then
|
|
|
|
/// send the children courses to live with their grandparent as well
|
|
|
|
if ($deletecat->parent) {
|
|
|
|
if ($childcourses = get_records("course", "category", $deletecat->id)) {
|
|
|
|
foreach ($childcourses as $childcourse) {
|
|
|
|
if (! set_field("course", "category", $deletecat->parent, "id", $childcourse->id)) {
|
|
|
|
error("Could not update a child course!", "index.php");
|
|
|
|
}
|
2004-01-09 16:54:10 +00:00
|
|
|
}
|
2003-12-21 05:10:13 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-16 02:21:31 +00:00
|
|
|
|
|
|
|
/// Finally delete the category itself
|
|
|
|
if (delete_records("course_categories", "id", $deletecat->id)) {
|
|
|
|
notify(get_string("categorydeleted", "", $deletecat->name));
|
|
|
|
}
|
2003-12-21 05:10:13 +00:00
|
|
|
}
|
2004-11-16 02:21:31 +00:00
|
|
|
else {
|
|
|
|
$strdeletecategorycheck = get_string("deletecategorycheck","",$deletecat->name);
|
|
|
|
notice_yesno("$strdeletecategorycheck",
|
|
|
|
"index.php?delete=$delete&sure=".md5($deletecat->timemodified)."&sesskey=$USER->sesskey",
|
|
|
|
"index.php?sesskey=$USER->sesskey");
|
|
|
|
exit();
|
2004-01-09 16:54:10 +00:00
|
|
|
}
|
2003-08-07 16:01:31 +00:00
|
|
|
}
|
2002-09-09 11:48:11 +00:00
|
|
|
}
|
2002-09-09 12:37:34 +00:00
|
|
|
|
2002-09-09 11:48:11 +00:00
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
/// Create a default category if necessary
|
|
|
|
if (!$categories = get_categories()) { /// No category yet!
|
|
|
|
// Try and make one
|
|
|
|
unset($tempcat);
|
|
|
|
$tempcat->name = get_string("miscellaneous");
|
|
|
|
if (!$tempcat->id = insert_record("course_categories", $tempcat)) {
|
|
|
|
error("Serious error: Could not create a default category!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Move a category to a new parent if required
|
|
|
|
|
2004-10-09 13:22:59 +00:00
|
|
|
if (isset($move) and isset($moveto) and confirm_sesskey()) {
|
2003-08-07 16:01:31 +00:00
|
|
|
if ($tempcat = get_record("course_categories", "id", $move)) {
|
|
|
|
if ($tempcat->parent != $moveto) {
|
|
|
|
if (! set_field("course_categories", "parent", $moveto, "id", $tempcat->id)) {
|
|
|
|
notify("Could not update that category!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Hide or show a category
|
2004-10-09 13:22:59 +00:00
|
|
|
if ((isset($hide) or isset($show)) and confirm_sesskey()) {
|
2003-08-07 16:01:31 +00:00
|
|
|
if (isset($hide)) {
|
|
|
|
$tempcat = get_record("course_categories", "id", $hide);
|
|
|
|
$visible = 0;
|
|
|
|
} else {
|
|
|
|
$tempcat = get_record("course_categories", "id", $show);
|
|
|
|
$visible = 1;
|
|
|
|
}
|
|
|
|
if ($tempcat) {
|
|
|
|
if (! set_field("course_categories", "visible", $visible, "id", $tempcat->id)) {
|
|
|
|
notify("Could not update that category!");
|
|
|
|
}
|
|
|
|
if (! set_field("course", "visible", $visible, "category", $tempcat->id)) {
|
|
|
|
notify("Could not hide/show any courses in this category !");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Move a category up or down
|
|
|
|
|
2004-10-09 13:22:59 +00:00
|
|
|
if ((isset($moveup) or isset($movedown)) and confirm_sesskey()) {
|
2003-08-07 16:01:31 +00:00
|
|
|
|
|
|
|
$swapcategory = NULL;
|
|
|
|
$movecategory = NULL;
|
|
|
|
|
|
|
|
if (isset($moveup)) {
|
|
|
|
if ($movecategory = get_record("course_categories", "id", $moveup)) {
|
|
|
|
$categories = get_categories("$movecategory->parent");
|
|
|
|
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if ($category->id == $movecategory->id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$swapcategory = $category;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($movedown)) {
|
|
|
|
if ($movecategory = get_record("course_categories", "id", $movedown)) {
|
|
|
|
$categories = get_categories("$movecategory->parent");
|
2002-09-09 11:48:11 +00:00
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
$choosenext = false;
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if ($choosenext) {
|
|
|
|
$swapcategory = $category;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($category->id == $movecategory->id) {
|
|
|
|
$choosenext = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($swapcategory and $movecategory) { // Renumber everything for robustness
|
|
|
|
$count=0;
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
$count++;
|
|
|
|
if ($category->id == $swapcategory->id) {
|
|
|
|
$category = $movecategory;
|
|
|
|
} else if ($category->id == $movecategory->id) {
|
|
|
|
$category = $swapcategory;
|
|
|
|
}
|
|
|
|
if (! set_field("course_categories", "sortorder", $count, "id", $category->id)) {
|
|
|
|
notify("Could not update that category!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the default category (the one with the lowest ID)
|
2003-07-30 13:02:45 +00:00
|
|
|
$categories = get_categories();
|
2003-08-07 16:01:31 +00:00
|
|
|
$default = 99999;
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
if ($category->id < $default) {
|
|
|
|
$default = $category->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find any orphan courses that don't yet have a valid category and set to default
|
2004-11-17 05:05:14 +00:00
|
|
|
if ($courses = get_courses(NULL,NULL,'c.category,c.id,c.sortorder')) {
|
2003-08-07 16:01:31 +00:00
|
|
|
foreach ($courses as $course) {
|
2003-08-22 12:49:37 +00:00
|
|
|
if ($course->category and !isset($categories[$course->category])) {
|
2003-08-07 16:01:31 +00:00
|
|
|
set_field("course", "category", $default, "id", $course->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-05-30 00:33:45 +00:00
|
|
|
|
|
|
|
fix_course_sortorder();
|
2003-08-07 16:01:31 +00:00
|
|
|
|
|
|
|
/// Print form for creating new categories
|
|
|
|
|
|
|
|
echo "<center>";
|
|
|
|
echo "<form name=\"addform\" action=\"index.php\" method=\"post\">";
|
2004-09-23 12:25:49 +00:00
|
|
|
echo "<input type=\"text\" size=\"30\" alt=\"$straddnewcategory\" name=\"addcategory\" />";
|
2004-09-12 12:21:27 +00:00
|
|
|
echo "<input type=\"submit\" value=\"$straddnewcategory\" />";
|
2004-10-09 13:22:59 +00:00
|
|
|
echo "<input type=\"hidden\" name=\"sesskey\" value=\"$USER->sesskey\" />";
|
2003-08-07 16:01:31 +00:00
|
|
|
echo "</form>";
|
|
|
|
echo "</center>";
|
|
|
|
|
|
|
|
echo "<br />";
|
|
|
|
|
|
|
|
|
|
|
|
/// Print out the categories with all the knobs
|
|
|
|
|
|
|
|
$strcategories = get_string("categories");
|
|
|
|
$strcourses = get_string("courses");
|
|
|
|
$strmovecategoryto = get_string("movecategoryto");
|
|
|
|
$stredit = get_string("edit");
|
|
|
|
|
|
|
|
$displaylist = array();
|
|
|
|
$parentlist = array();
|
|
|
|
|
|
|
|
$displaylist[0] = get_string("top");
|
|
|
|
make_categories_list($displaylist, $parentlist, "");
|
|
|
|
|
2004-09-12 12:21:27 +00:00
|
|
|
echo "<table align=\"center\" border=\"0\" cellspacing=\"2\" cellpadding=\"5\" class=\"generalbox\"><tr>";
|
2003-08-07 16:01:31 +00:00
|
|
|
echo "<th>$strcategories</th>";
|
|
|
|
echo "<th>$strcourses</th>";
|
|
|
|
echo "<th>$stredit</th>";
|
|
|
|
echo "<th>$strmovecategoryto</th>";
|
2004-09-23 12:25:49 +00:00
|
|
|
echo "</tr>";
|
2003-08-07 16:01:31 +00:00
|
|
|
|
|
|
|
print_category_edit(NULL, $displaylist, $parentlist);
|
|
|
|
|
|
|
|
echo "</table>";
|
2003-08-09 16:53:30 +00:00
|
|
|
echo "<br />";
|
2003-08-07 16:01:31 +00:00
|
|
|
|
2003-08-09 16:53:30 +00:00
|
|
|
/// Print link to create a new course
|
2003-08-22 12:15:35 +00:00
|
|
|
echo "<center>";
|
2003-08-09 16:53:30 +00:00
|
|
|
unset($options);
|
2003-09-08 12:59:04 +00:00
|
|
|
$options["category"] = $category->id;
|
2003-08-09 16:53:30 +00:00
|
|
|
print_single_button("edit.php", $options, get_string("addnewcourse"), "get");
|
|
|
|
echo "<br />";
|
|
|
|
echo "</center>";
|
2003-07-30 13:02:45 +00:00
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
print_footer();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
|
|
|
|
/// Recursive function to print all the categories ready for editing
|
|
|
|
|
2005-01-25 14:03:43 +00:00
|
|
|
global $CFG, $USER;
|
2003-08-07 16:01:31 +00:00
|
|
|
|
|
|
|
static $str = '';
|
|
|
|
|
|
|
|
if (empty($str)) {
|
|
|
|
$str->delete = get_string("delete");
|
|
|
|
$str->moveup = get_string("moveup");
|
|
|
|
$str->movedown = get_string("movedown");
|
|
|
|
$str->edit = get_string("editthiscategory");
|
|
|
|
$str->hide = get_string("hide");
|
|
|
|
$str->show = get_string("show");
|
|
|
|
}
|
2003-07-30 13:02:45 +00:00
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
if ($category) {
|
2005-01-25 14:03:43 +00:00
|
|
|
echo "<tr><td align=\"left\" nowrap=\"nowrap\">";
|
2003-08-07 16:01:31 +00:00
|
|
|
for ($i=0; $i<$depth;$i++) {
|
|
|
|
echo " ";
|
|
|
|
}
|
|
|
|
$linkcss = $category->visible ? "" : " class=\"dimmed\" ";
|
2004-10-08 16:52:20 +00:00
|
|
|
echo "<a $linkcss title=\"$str->edit\" href=\"category.php?id=$category->id&edit=on&sesskey=$USER->sesskey\">$category->name</a>";
|
2003-08-07 16:01:31 +00:00
|
|
|
echo "</td>";
|
|
|
|
|
2003-08-19 09:26:36 +00:00
|
|
|
echo "<td align=\"right\">$category->coursecount</td>";
|
2003-08-07 16:01:31 +00:00
|
|
|
|
|
|
|
echo "<td nowrap=\"nowrap\">"; /// Print little icons
|
|
|
|
|
2004-10-09 13:22:59 +00:00
|
|
|
echo "<a title=\"$str->delete\" href=\"index.php?delete=$category->id&sesskey=$USER->sesskey\"><img".
|
2005-01-25 14:03:43 +00:00
|
|
|
" src=\"$CFG->pixpath/t/delete.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
|
2003-08-07 16:01:31 +00:00
|
|
|
|
|
|
|
if (!empty($category->visible)) {
|
2004-10-09 13:22:59 +00:00
|
|
|
echo "<a title=\"$str->hide\" href=\"index.php?hide=$category->id&sesskey=$USER->sesskey\"><img".
|
2005-01-25 14:03:43 +00:00
|
|
|
" src=\"$CFG->pixpath/t/hide.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
|
2003-08-07 16:01:31 +00:00
|
|
|
} else {
|
2004-10-09 13:22:59 +00:00
|
|
|
echo "<a title=\"$str->show\" href=\"index.php?show=$category->id&sesskey=$USER->sesskey\"><img".
|
2005-01-25 14:03:43 +00:00
|
|
|
" src=\"$CFG->pixpath/t/show.gif\" height=\"11\" width=\"11\" border=\"0\"alt=\"\" /></a> ";
|
2003-08-07 16:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($up) {
|
2004-10-09 13:22:59 +00:00
|
|
|
echo "<a title=\"$str->moveup\" href=\"index.php?moveup=$category->id&sesskey=$USER->sesskey\"><img".
|
2005-01-25 14:03:43 +00:00
|
|
|
" src=\"$CFG->pixpath/t/up.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"\" /></a> ";
|
2003-08-07 16:01:31 +00:00
|
|
|
}
|
|
|
|
if ($down) {
|
2004-10-09 13:22:59 +00:00
|
|
|
echo "<a title=\"$str->movedown\" href=\"index.php?movedown=$category->id&sesskey=$USER->sesskey\"><img".
|
2005-01-25 14:03:43 +00:00
|
|
|
" src=\"$CFG->pixpath/t/down.gif\" height=\"11\" width=\"11\" border=\"0\"alt=\"\" /></a> ";
|
2003-08-07 16:01:31 +00:00
|
|
|
}
|
|
|
|
echo "</td>";
|
|
|
|
|
|
|
|
echo "<td align=\"left\" width=\"0\">";
|
|
|
|
$tempdisplaylist = $displaylist;
|
|
|
|
unset($tempdisplaylist[$category->id]);
|
|
|
|
foreach ($parentslist as $key => $parents) {
|
|
|
|
if (in_array($category->id, $parents)) {
|
|
|
|
unset($tempdisplaylist[$key]);
|
|
|
|
}
|
|
|
|
}
|
2004-10-09 13:22:59 +00:00
|
|
|
popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", "$category->parent", "", "", "", false);
|
2003-08-07 16:01:31 +00:00
|
|
|
echo "</td>";
|
|
|
|
echo "</tr>";
|
2003-07-30 13:02:45 +00:00
|
|
|
} else {
|
2003-08-07 16:01:31 +00:00
|
|
|
$category->id = "0";
|
2003-07-30 13:02:45 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
if ($categories = get_categories($category->id)) { // Print all the children recursively
|
|
|
|
$countcats = count($categories);
|
|
|
|
$count = 0;
|
|
|
|
$first = true;
|
|
|
|
$last = false;
|
|
|
|
foreach ($categories as $cat) {
|
|
|
|
$count++;
|
|
|
|
if ($count == $countcats) {
|
|
|
|
$last = true;
|
|
|
|
}
|
|
|
|
$up = $first ? false : true;
|
|
|
|
$down = $last ? false : true;
|
|
|
|
$first = false;
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
|
2003-08-07 16:01:31 +00:00
|
|
|
?>
|