From c2cb45451f8317e9e670b3b2cae2a8af44966c64 Mon Sep 17 00:00:00 2001 From: moodler Date: Wed, 30 Jul 2003 13:02:45 +0000 Subject: [PATCH] COURSE DISPLAY REVAMP OK, some big changes here to the front end, particularly in course categories and course display. Course categories can now be nested (to any level). Courses and course categories can now be manually sorted any way required. There is a groovy front end for managing these, and a better range of options for formatting the front page. It all still needs some polishing, which I'll be doing over the next couple of days, including better auto-sorting. I would not use this on production systems just yet. --- course/categories.php | 317 ++++++++++++++++++++++------ course/category.php | 220 +++++++++++++++++++ course/edit.html | 4 +- course/edit.php | 2 + course/index.php | 57 +++-- course/lib.php | 480 ++++++++++++++++++++++++++---------------- course/unenrol.php | 2 +- index.php | 96 +++++---- lang/en/moodle.php | 13 +- lib/datalib.php | 56 ++++- lib/db/mysql.php | 9 + lib/db/mysql.sql | 6 + lib/db/postgres7.php | 9 + lib/db/postgres7.sql | 6 + lib/weblib.php | 17 +- version.php | 2 +- 16 files changed, 963 insertions(+), 333 deletions(-) create mode 100644 course/category.php diff --git a/course/categories.php b/course/categories.php index 762130ed35..668466dc8b 100644 --- a/course/categories.php +++ b/course/categories.php @@ -7,7 +7,7 @@ require_login(); if (!isadmin()) { - error("Only administrators can use this course!"); + error("Only administrators can use this page!"); } if (!$site = get_site()) { @@ -24,80 +24,146 @@ $stredit = get_string("edit"); $strdelete = get_string("delete"); $straction = get_string("action"); - $stradd = get_string("add"); + $straddnewcategory = get_string("addnewcategory"); print_header("$site->shortname: $strcategories", "$site->fullname", - "admin/index.php\">$stradministration -> $strcategories"); + "admin/index.php\">$stradministration -> $strcategories", + "addform.addcategory"); - print_heading($strcategories); -/// If data submitted, then process and store. +/// If data for a new category was submitted, then add it + if ($form = data_submitted()) { + 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)); + } + } + } - if ($form = data_submitted()) { - $categories = array(); +/// Delete a category if necessary - // Peel out all the data from variable names. - foreach ($form as $key => $val) { - if ($key == "new") { - if (!empty($val)) { - if (get_records("course_categories", "name", $val)) { - notify(get_string("categoryduplicate", "", $val)); - } else { - $cat->name = $val; - if (!insert_record("course_categories", $cat)) { - error("Could not insert the new category '$val'"); - } else { - notify(get_string("categoryadded", "", $val)); - } - } - } - - } else { - $cat->id = substr($key,1); - $cat->name = $val; - - if ($existingcats = get_records("course_categories", "name", $val)) { - foreach($existingcats as $existingcat) { - if ($existingcat->id != $cat->id) { - notify(get_string("categoryduplicate", "", $val)); - continue 2; - } + if (isset($delete)) { + if ($tempcat = get_record("course_categories", "id", $delete)) { + if (delete_records("course_categories", "id", $tempcat->id)) { + notify(get_string("categorydeleted", "", $tempcat->name)); + } + if ($children = get_records("course_categories", "parent", $tempcat->id)) { + foreach ($children as $childcat) { + if (! set_field("course_categories", "parent", $tempcat->parent, "id", $childcat->id)) { + notify("Could not update a child category!"); } } - - if (!update_record("course_categories", $cat)) { - error("Could not update the category '$val'"); - } } } - } + } -/// Get the existing categories - if (!$categories = get_categories()) { +/// Create a default category if necessary + if (!$categories = get_categories()) { /// No category yet! // Try and make one - $cat->name = get_string("miscellaneous"); - if ($cat->id = insert_record("course_categories", $cat)) { - $categories[$cat->id] = $cat; - } else { + unset($tempcat); + $tempcat->name = get_string("miscellaneous"); + if (!$tempcat->id = insert_record("course_categories", $tempcat)) { error("Serious error: Could not create a default category!"); } } -/// Delete category if the user wants to delete it - if (isset($delete)) { - if (delete_records("course_categories", "id", $delete)) { - notify(get_string("categorydeleted", "", $categories[$delete]->name)); - unset($categories[$delete]); + +/// Move a category to a new parent if required + + if (isset($move) and isset($moveto)) { + 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 + if (isset($hide) or isset($show)) { + if (isset($hide)) { + $tempcat = get_record("course_categories", "id", $hide); + $visible = 0; } else { - error("An error occurred while trying to delete a category"); + $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 + + if (isset($moveup) or isset($movedown)) { + + $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"); + + $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 lowest ID category - this is the default category +/// Find the default category (the one with the lowest ID) + $categories = get_categories(); $default = 99999; foreach ($categories as $category) { + fix_category_courses($category->id); if ($category->id < $default) { $default = $category->id; } @@ -112,30 +178,141 @@ } } +/// Print form for creating new categories -/// Print the table of all categories - $table->head = array ($strcategory, $strcourses, $straction); - $table->align = array ("LEFT", "CENTER", "CENTER"); - $table->size = array ("80", "50", "50"); - $table->width = 100; + print_simple_box_start("center"); + echo "
"; + echo "
"; + echo ""; + echo ""; + echo "
"; + echo "
"; + print_simple_box_end(); - echo "
"; - foreach ($categories as $category) { - $count = count_records("course", "category", $category->id); - if ($category->id == $default) { - $delete = ""; // Can't delete default category + echo "
"; + + +/// Print out the categories with all the knobs + + $strcategories = get_string("categories"); + $strmovecategoryto = get_string("movecategoryto"); + $stredit = get_string("edit"); + + $displaylist = array(); + $parentlist = array(); + + $displaylist[0] = get_string("top"); + make_categories_list($displaylist, $parentlist, ""); + + echo ""; + echo ""; + echo ""; + echo ""; + + print_category_edit(NULL, $displaylist, $parentlist); + + echo "
$strcategories$stredit$strmovecategoryto
"; + + + 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 + + global $THEME, $CFG; + + static $str = ''; + static $pixpath = ''; + + 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"); + } + + if (empty($pixpath)) { + if (empty($THEME->custompix)) { + $pixpath = "$CFG->wwwroot/pix"; } else { - $delete = "id\">$strdelete"; + $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix"; } - $table->data[] = array ("id\" VALUE=\"$category->name\" SIZE=30>", - "id\">$count", $delete); } - $table->data[] = array ("", "", "$stradd"); - print_table($table); - echo "

"; - echo "
"; - echo "
"; - print_footer(); + if ($category) { + echo "cellcontent\">"; + echo "

"; + for ($i=0; $i<$depth;$i++) { + echo "      "; + } + $linkcss = $category->visible ? "" : " class=\"dimmed\" "; + echo "edit\" href=\"category.php?id=$category->id\">$category->name"; + echo "

"; + echo ""; + + echo ""; /// Print little icons + + if (!empty($category->visible)) { + echo "hide\" href=\"categories.php?hide=$category->id\"> "; + } else { + echo "show\" href=\"categories.php?show=$category->id\"> "; + } + + echo "delete\" href=\"categories.php?delete=$category->id\"> "; + + //echo "update\" href=\"category.php?id=$category->id\"> "; + + if ($up) { + echo "moveup\" href=\"categories.php?moveup=$category->id\"> "; + } + if ($down) { + echo "movedown\" href=\"categories.php?movedown=$category->id\"> "; + } + echo ""; + + echo ""; + $tempdisplaylist = $displaylist; + unset($tempdisplaylist[$category->id]); + foreach ($parentslist as $key => $parents) { + if (in_array($category->id, $parents)) { + unset($tempdisplaylist[$key]); + } + } + popup_form ("categories.php?move=$category->id&moveto=", $tempdisplaylist, "moveform$category->id", "$category->parent", "", "", "", false); + echo ""; + echo ""; + } else { + $category->id = "0"; + } + + 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; + + print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down); + } + } +} + ?> diff --git a/course/category.php b/course/category.php new file mode 100644 index 0000000000..35a6d68044 --- /dev/null +++ b/course/category.php @@ -0,0 +1,220 @@ +shortname: $strcategory", "$site->fullname", + "admin/index.php\">$stradministration -> ". + "$strcategories -> $strcategory"); + + +/// Rename the category + + if (!empty($rename)) { + $category->name = $rename; + if (! set_field("course_categories", "name", $category->name, "id", $category->id)) { + notify("An error occurred while renaming the category"); + } else { + notify("The category was renamed"); + } + } + +/// Print the category selector + + $displaylist = array(); + $parentlist = array(); + + make_categories_list($displaylist, $parentlist, ""); + + echo "
"; + popup_form("category.php?id=", $displaylist, "switchcategory", "$category->id", "", "", "", false); + echo "

"; + + +/// Move a specified course to a new category + + if (isset($move) and isset($moveto)) { + if (! $course = get_record("course", "id", $move)) { + notify("Error finding the course"); + } else if (! $destcategory = get_record("course_categories", "id", $moveto)) { + notify("Error finding the category"); + } else { + if (!set_field("course", "category", $destcategory->id, "id", $course->id)) { + notify("An error occurred - course not moved!"); + } + fix_category_courses($destcategory->id); + fix_category_courses($category->id); + $category = get_record("course_categories", "id", $category->id); + } + } + + +/// Hide or show a course + + if (isset($hide) or isset($show)) { + if (isset($hide)) { + $course = get_record("course", "id", $hide); + $visible = 0; + } else { + $course = get_record("course", "id", $show); + $visible = 1; + } + if ($course) { + if (! set_field("course", "visible", $visible, "id", $course->id)) { + notify("Could not update that course!"); + } + } + } + + +/// Move a course up or down + + if (isset($moveup) or isset($movedown)) { + + + $movecourse = isset($moveup) ? $moveup : $movedown; + + fix_category_courses($category->id); + if (!$category = get_record("course_categories", "id", $category->id)) { // Fresh copy + error("Category not known!"); + } + + $courses = explode(',', $category->courseorder); + $key = array_search($movecourse, $courses); + if ($key === NULL or $key === false) { + notify("Could not find that course in the category list!"); + + } else { + if (isset($moveup)) { + $swapkey = $key-1; + } else { + $swapkey = $key+1; + } + $courses[$key] = $courses[$swapkey]; + $courses[$swapkey] = $movecourse; + $category->courseorder = implode(",", $courses); + + if (! set_field("course_categories", "courseorder", $category->courseorder, "id", $category->id)) { + notify("Database error while trying to update the category!"); + } + } + } + + +/// Print out the courses with all the knobs + + fix_category_courses($category->id); + + if (!$courses = get_courses($category)) { + print_heading(get_string("nocoursesyet")); + + } else { + + $strcourses = get_string("courses"); + $strmovecourseto = get_string("movecourseto"); + $stredit = get_string("edit"); + $strdelete = get_string("delete"); + $strmoveup = get_string("moveup"); + $strmovedown = get_string("movedown"); + $strupdate = get_string("update"); + $strhide = get_string("hide"); + $strshow = get_string("show"); + + if (empty($THEME->custompix)) { + $pixpath = "$CFG->wwwroot/pix"; + } else { + $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix"; + } + + echo ""; + echo ""; + echo ""; + echo ""; + + $numcourses = count($courses); + $count = 0; + + foreach ($courses as $course) { + $count++; + $up = ($count == 1) ? false : true; + $down = ($count == $numcourses) ? false : true; + + $linkcss = $course->visible ? "" : " class=\"dimmed\" "; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + + echo "
$strcourses$stredit$strmovecourseto
id\">$course->fullname"; + if (!empty($course->visible)) { + echo "id&hide=$course->id\"> "; + } else { + echo "id&show=$course->id\"> "; + } + echo "id\"> "; + + if ($up) { + echo "id&moveup=$course->id\"> "; + } + + if ($down) { + echo "id&movedown=$course->id\"> "; + } + + echo ""; + popup_form ("category.php?id=$category->id&move=$course->id&moveto=", $displaylist, + "moveform$course->id", "$course->category", "", "", "", false); + echo "
"; + echo "
"; + } + +/// Print form to rename the category + + $strrename= get_string("rename"); + + print_simple_box_start("center"); + echo "
"; + echo "
"; + echo "id\">"; + echo "name\">"; + echo ""; + echo "
"; + echo "
"; + print_simple_box_end(); + + print_footer(); + +?> diff --git a/course/edit.html b/course/edit.html index 584731387e..336747e432 100644 --- a/course/edit.html +++ b/course/edit.html @@ -52,7 +52,9 @@

: categories, "category", "$form->category", ""); + $displaylist = array(); + make_categories_list($displaylist); + choose_from_menu($displaylist, "category", "$form->category", ""); helpbutton("coursecategory", get_string("category")); ?> diff --git a/course/edit.php b/course/edit.php index 7c130d70c4..36c5cb3248 100644 --- a/course/edit.php +++ b/course/edit.php @@ -44,6 +44,8 @@ $form->timemodified = time(); + fix_category_courses($form->category); + if (!empty($course)) { if (update_record("course", $form)) { add_to_log($course->id, "course", "update", "edit.php?id=$id", ""); diff --git a/course/index.php b/course/index.php index 82ad3ee987..946772e3fe 100644 --- a/course/index.php +++ b/course/index.php @@ -4,53 +4,44 @@ require_once("../config.php"); require_once("lib.php"); - optional_variable($category, ""); + optional_variable($category, "0"); $strcourses = get_string("courses"); $strcategories = get_string("categories"); $strmycourses = get_string("mycourses"); $strfulllistofcourses = get_string("fulllistofcourses"); - if (!$categories = get_categories()) { - error("Could not find any course categories!"); - } + if ($category = get_record("course_categories", "id", $category)) { + print_header($strcourses, $strcourses, "$strcourses -> $category->name", + "", "", true, update_category_button($category->id)); - if ($category == "all") { - $title = $strfulllistofcourses; - $navigation = "$strcourses -> $title"; - } else if ($category == "my") { - $title = $strmycourses; - $navigation = "$strcourses -> $title"; - } else if (isset($categories[$category])) { - $title = $categories[$category]->name; - $navigation = "$strcourses -> $title"; } else { - $navigation = $strcourses; + print_header($strcourses, $strcourses, $strcourses); + $category->id = 0; } - print_header($strcourses, $strcourses, $navigation); - $showcategories = (count($categories) > 1); - if ($showcategories) { - echo ""; - echo ""; diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 4b2ca830ff..04c5f045ec 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -16,6 +16,7 @@ $string['add'] = "Add"; $string['added'] = "Added \$a"; $string['addinganew'] = "Adding a new \$a"; $string['addinganewto'] = "Adding a new \$a->what to \$a->to"; +$string['addnewcategory'] = "Add new category"; $string['addnewcourse'] = "Add a new course"; $string['addnewuser'] = "Add a new user"; $string['address'] = "Address"; @@ -186,6 +187,7 @@ $string['editinga'] = "Editing a \$a"; $string['editmyprofile'] = "Edit profile"; $string['editsummary'] = "Edit summary"; $string['editthisactivity'] = "Edit this activity"; +$string['editthiscategory'] = "Edit this category"; $string['edituser'] = "Edit user accounts"; $string['email'] = "Email address"; $string['emailformat'] = "Email format"; @@ -276,8 +278,11 @@ $string['formattopics'] = "Topics format"; $string['formatweeks'] = "Weekly format"; $string['formatwiki'] = "Wiki format"; $string['from'] = "From"; +$string['frontpagecategorynames'] = "Show a list of categories"; +$string['frontpagecourselist'] = "Show a list of courses"; $string['frontpagedescription'] = "Front page description"; $string['frontpageformat'] = "Front page format"; +$string['frontpagenews'] = "Show news items"; $string['fulllistofcourses'] = "All courses"; $string['fullprofile'] = "Full profile"; $string['fullname'] = "Full name"; @@ -441,6 +446,8 @@ $string['modulesuccess'] = "\$a tables have been set up correctly"; $string['moodleversion'] = "Moodle Version"; $string['mostrecently'] = "most recently"; $string['move'] = "Move"; +$string['movecategoryto'] = "Move category to:"; +$string['movecourseto'] = "Move course to:"; $string['movedown'] = "Move down"; $string['movefull'] = "Move \$a to this location"; $string['movehere'] = "Move to here"; @@ -609,7 +616,6 @@ $string['showonlytopic'] = "Show only topic \$a"; $string['showonlyweek'] = "Show only week \$a"; $string['showrecent'] = "Show recent activity"; $string['showtheselogs'] = "Show these logs"; -$string['socialheadline'] = "Social forum - latest topics"; $string['showallcourses'] = "Show all courses"; $string['site'] = "Site"; $string['sites'] = "Sites"; @@ -617,6 +623,7 @@ $string['sitelogs'] = "Site logs"; $string['sitenews'] = "Site news"; $string['sitesettings'] = "Site settings"; $string['size'] = "Size"; +$string['socialheadline'] = "Social forum - latest topics"; $string['someallowguest'] = "Some courses may allow guest access"; $string['someerrorswerefound'] = "Some information was missing or incorrect. Look below for details."; $string['startdate'] = "Course start date"; @@ -635,6 +642,7 @@ $string['strftimerecentfull'] = "%%a, %%d %%b %%Y, %%I:%%M %%p"; $string['strftimetime'] = "%%I:%%M %%p"; $string['stringsnotset'] = "The following strings are not defined in \$a"; $string['studentnotallowed'] = "Sorry, but you can not enter this course as '\$a'"; +$string['subcategories'] = "Sub-categories"; $string['success'] = "Success"; $string['summary'] = "Summary"; $string['summaryof'] = "Summary of \$a"; @@ -652,13 +660,14 @@ $string['to'] = "To"; $string['today'] = "Today"; $string['todaylogs'] = "Today's logs"; $string['toomanytoshow'] = "There are too many users to show"; +$string['top'] = "Top"; $string['topic'] = "Topic"; $string['topichide'] = "Hide this topic from \$a"; $string['topicoutline'] = "Topic outline"; $string['topicshow'] = "Show this topic to \$a"; +$string['total'] = "Total"; $string['turneditingoff'] = "Turn editing off"; $string['turneditingon'] = "Turn editing on"; -$string['total'] = "Total"; $string['undecided'] = "Undecided"; $string['unenrol'] = "Unenrol"; $string['unenrolme'] = "Unenrol me from \$a"; diff --git a/lib/datalib.php b/lib/datalib.php index 2554687764..06e5d16927 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -83,6 +83,9 @@ function table_column($table, $oldfield, $field, $type="integer", $size="10", case "mysqlt": switch (strtolower($type)) { + case "text": + $type = "TEXT"; + break; case "integer": $type = "INTEGER($size)"; break; @@ -821,16 +824,24 @@ function get_site () { function get_courses($category=0, $sort="fullname ASC") { -/// Returns list of courses +/// Returns list of courses, for whole site, or category - if ($category > 0) { // Return all courses in one category - $courses = get_records("course", "category", $category, $sort); + if ($category === 0) { // Return all courses, except site + $courses = get_records_select("course", "category > 0", $sort); - } else if ($category < 0) { // Return all courses, even the site + } else if ($category === -1) { // Return all courses, even the site $courses = get_records("course", "", "", $sort); - } else { // Return all courses, except site - $courses = get_records_select("course", "category > 0", $sort); + } else { // $category is an object + $courses = get_records("course", "category", $category->id); + if ($courses) { // Reorder them + $courselist = explode(',', $category->courseorder); + $outcourses = array(); + foreach ($courselist as $courseid) { + $outcourses[] = $courses[$courseid]; + } + $courses = $outcourses; + } } if ($courses) { /// Remove unavailable courses from the list @@ -845,8 +856,37 @@ function get_courses($category=0, $sort="fullname ASC") { return $courses; } -function get_categories() { - return get_records("course_categories", "", "", "name"); +function get_my_courses($userid, $sort="c.fullname ASC") { + global $CFG; + + return get_records_sql("SELECT c.* + FROM {$CFG->prefix}course c, + {$CFG->prefix}user_students s, + {$CFG->prefix}user_teachers t + WHERE (s.userid = '$userid' AND s.course = c.id) + OR (t.userid = '$userid' AND t.course = c.id) + GROUP BY c.id + ORDER BY $sort"); +} + + +function get_categories($parent="none", $sort="sortorder ASC") { + if ($parent == "none") { + $categories = get_records("course_categories", "", "", $sort); + } else { + $categories = get_records("course_categories", "parent", $parent, $sort); + } + if ($categories) { /// Remove unavailable categories from the list + $admin = isadmin(); + foreach ($categories as $key => $category) { + if (!$category->visible) { + if (!$admin) { + unset($categories[$key]); + } + } + } + } + return $categories; } diff --git a/lib/db/mysql.php b/lib/db/mysql.php index 33ea8caaf5..a2058df9b6 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -413,6 +413,15 @@ function main_upgrade($oldversion=0) { execute_sql(" ALTER TABLE `{$CFG->prefix}user_teachers` ADD INDEX courseuserid (course,userid) "); } + if ($oldversion < 2003072803) { + table_column("course_categories", "", "description", "text", "", "", ""); + table_column("course_categories", "", "parent", "integer", "10", "unsigned"); + table_column("course_categories", "", "sortorder", "integer", "10", "unsigned"); + table_column("course_categories", "", "courseorder", "text", "", "", ""); + table_column("course_categories", "", "visible", "integer", "1", "unsigned", "1"); + table_column("course_categories", "", "timemodified", "integer", "10", "unsigned"); + } + return $result; } diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index ece942f6f2..66c0a33bd8 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -60,6 +60,12 @@ CREATE TABLE `prefix_course` ( CREATE TABLE `prefix_course_categories` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(255) NOT NULL default '', + `description` text NOT NULL, + `parent` int(10) unsigned NOT NULL default '0', + `sortorder` int(10) unsigned NOT NULL default '0', + `courseorder` text NOT NULL, + `visible` tinyint(1) NOT NULL default '1', + `timemodified` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) TYPE=MyISAM COMMENT='Course categories'; diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index 653423fef6..8e3c95986c 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -184,6 +184,15 @@ function main_upgrade($oldversion=0) { execute_sql(" CREATE INDEX {$CFG->prefix}user_teachers_courseuserid_idx ON {$CFG->prefix}user_teachers (course,userid) "); } + if ($oldversion < 2003072802) { + table_column("course_categories", "", "description", "text", "", "", ""); + table_column("course_categories", "", "parent", "integer", "10", "unsigned"); + table_column("course_categories", "", "sortorder", "integer", "10", "unsigned"); + table_column("course_categories", "", "courseorder", "text", "", "", ""); + table_column("course_categories", "", "visible", "integer", "1", "unsigned", "1"); + table_column("course_categories", "", "timemodified", "integer", "10", "unsigned"); + } + return $result; } ?> diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index 4f689cd567..3a8630f108 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -31,6 +31,12 @@ CREATE TABLE prefix_course ( CREATE TABLE prefix_course_categories ( id SERIAL PRIMARY KEY, name varchar(255) NOT NULL default '' + description text NOT NULL default '', + parent integer NOT NULL default '0', + sortorder integer NOT NULL default '0', + courseorder text NOT NULL default '', + visible integer NOT NULL default '1', + timemodified` integer NOT NULL default '0' ); CREATE TABLE prefix_course_display ( diff --git a/lib/weblib.php b/lib/weblib.php index f00c7e86fe..0121cd3dbb 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -725,7 +725,7 @@ function print_footer ($course=NULL) { $homelink = "framename}\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname"; } } else { - $homelink = "framename}\" href=\"$CFG->wwwroot\">".get_string("home").""; + $homelink = "framename}\" href=\"$CFG->wwwroot/\">".get_string("home").""; $course = get_site(); } @@ -1079,6 +1079,17 @@ function update_module_button($moduleid, $courseid, $string) { } } +function update_category_button($categoryid) { +// Prints the editing button on a module "view" page + global $CFG; + + if (isadmin()) { + $string = get_string("editthiscategory"); + return "wwwroot/course/category.php\">". + "". + ""; + } +} function navmenu($course, $cm=NULL) { // Given a course and a (current) coursemodule @@ -1184,7 +1195,7 @@ function error ($message, $link="") { $link = "$SESSION->fromurl"; unset($SESSION->fromurl); } else { - $link = "$CFG->wwwroot"; + $link = "$CFG->wwwroot/"; } } print_continue($link); @@ -1245,7 +1256,7 @@ function notice ($message, $link="") { if (!empty($_SERVER["HTTP_REFERER"])) { $link = $_SERVER["HTTP_REFERER"]; } else { - $link = $CFG->wwwroot; + $link = "$CFG->wwwroot/"; } } diff --git a/version.php b/version.php index abe5209d52..87c589dcff 100644 --- a/version.php +++ b/version.php @@ -5,7 +5,7 @@ // database to determine whether upgrades should // be performed (see lib/db/*.php) -$version = 2003072800; // The current version is a date (YYYYMMDDXX) +$version = 2003072803; // The current version is a date (YYYYMMDDXX) $release = "1.1 development"; // User-friendly version number -- 2.39.5
"; - print_course_categories($categories, $category, 200); - echo ""; - } else { - echo "
"; - $category="all"; - unset($title); - } +/// Print the category selector - if ($category) { - if (isset($title)) { - print_heading_block($title); - } - echo "
"; - print_all_courses($category); + $categories = get_categories(); + $multicategories = count($categories) > 1; + + if (count($categories) > 1) { + $parentlist = array(); + $displaylist = array(); + $displaylist["0"] = $strfulllistofcourses; + make_categories_list($displaylist, $parentlist, ""); + + echo "
"; + popup_form("index.php?category=", $displaylist, "switchcategory", "$category->id", "", "", "", false); + echo "

"; } - echo "
"; + if (empty($category->id)) { + print_courses(0, "80%"); + } else { + print_courses($category, "80%"); + } print_footer(); diff --git a/course/lib.php b/course/lib.php index 9a7c9733c3..fc1f9fada7 100644 --- a/course/lib.php +++ b/course/lib.php @@ -14,6 +14,9 @@ define('COURSE_LIVELOG_REFRESH', 60); // Seconds define('COURSE_MAX_RECENT_PERIOD', 604800); // A week, in seconds +define("FRONTPAGENEWS", 0); +define("FRONTPAGECOURSELIST", 1); +define("FRONTPAGECATEGORYNAMES", 2); function print_log_selector_form($course, $selecteduser=0, $selecteddate="today") { @@ -189,114 +192,16 @@ function print_log($course, $user=0, $date=0, $order="ORDER BY l.time ASC") { } -function print_all_courses($category="all", $style="full", $maxcount=999, $width=180) { - global $CFG, $THEME, $USER; - - if ($category == "all") { - $courses = get_courses(); - - } else if ($category == "my") { - if (isset($USER->id)) { - if ($courses = get_courses()) { - foreach ($courses as $key => $course) { - if (!isteacher($course->id) and !isstudent($course->id)) { - unset($courses[$key]); - } - } - } - } - - } else { - $courses = get_courses($category); - } - - if ($style == "minimal") { - $count = 0; - if (empty($THEME->custompix)) { - $icon = "wwwroot/pix/i/course.gif\" height=16 width=16 alt=\"".get_string("course")."\">"; - } else { - $icon = "wwwroot/theme/$CFG->theme/pix/i/course.gif\" height=16 width=16 alt=\"".get_string("course")."\">"; - } - if ($courses) { - foreach ($courses as $course) { - $moddata[]="shortname\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname"; - $modicon[]=$icon; - if ($count++ >= $maxcount) { - break; - } - } - $fulllist = "

wwwroot/course/\">".get_string("fulllistofcourses")."..."; - } else { - $moddata = array(); - $modicon = array(); - $fulllist = get_string("nocoursesyet"); - } - print_side_block(get_string("courses"), "", $moddata, $modicon, $fulllist, $width); - - } else if ($courses) { - foreach ($courses as $course) { - print_course($course); - echo "
\n"; - } - +function print_log_graph($course, $userid=0, $type="course.png", $date=0) { + global $CFG; + if (empty($CFG->gdversion)) { + echo "(".get_string("gdneed").")"; } else { - echo "

".get_string("nocoursesyet")."

"; + echo "wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">"; } } -function print_course($course) { - - global $CFG, $THEME; - - if (! $site = get_site()) { - error("Could not find a site!"); - } - - if (empty($THEME->custompix)) { - $pixpath = "$CFG->wwwroot/pix"; - } else { - $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix"; - } - - print_simple_box_start("CENTER", "100%"); - - echo ""; - echo ""; - echo ""; - echo "
"; - echo "

wwwroot/course/view.php?id=$course->id\">$course->fullname

"; - if ($teachers = get_course_teachers($course->id)) { - echo "

\n"; - foreach ($teachers as $teacher) { - if ($teacher->authority > 0) { - if (!$teacher->role) { - $teacher->role = $course->teacher; - } - echo "$teacher->role: wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname
"; - } - } - echo "

"; - } - if ($course->guest) { - $strallowguests = get_string("allowguests"); - echo "wwwroot/course/view.php?id=$course->id\">"; - echo "\"$strallowguests\"  "; - } - if ($course->password) { - $strrequireskey = get_string("requireskey"); - echo "wwwroot/course/view.php?id=$course->id\">"; - echo "\"$strrequireskey\""; - } - - - echo "
"; - echo "

".text_to_html($course->summary)."

"; - echo "
"; - - print_simple_box_end(); -} function print_recent_activity($course) { // $course is an object @@ -593,7 +498,7 @@ function print_section_block($heading, $course, $section, $mods, $modnames, $mod } if ($mod->visible or $isteacher) { $instancename = urldecode($modinfo[$modnumber]->name); - $link_css = $mod->visible ? "" : " class=\"dimmed\" "; + $linkcss = $mod->visible ? "" : " class=\"dimmed\" "; if (!empty($modinfo[$modnumber]->extra)) { $extra = urldecode($modinfo[$modnumber]->extra); } else { @@ -602,7 +507,7 @@ function print_section_block($heading, $course, $section, $mods, $modnames, $mod $modicon[] = "wwwroot/mod/$mod->modname/icon.gif\"". " height=\"16\" width=\"16\" alt=\"$mod->modfullname\">"; - $moddata[] = "modfullname\" $link_css $extra". + $moddata[] = "modfullname\" $linkcss $extra". "href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename". "
$editbuttons"; } @@ -670,10 +575,10 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, } else { $extra = ""; } - $link_css = $mod->visible ? "" : " class=\"dimmed\" "; + $linkcss = $mod->visible ? "" : " class=\"dimmed\" "; echo "wwwroot/mod/$mod->modname/icon.gif\"". " height=16 width=16 alt=\"$mod->modfullname\">". - " modfullname\" $link_css $extra". + " modfullname\" $linkcss $extra". " href=\"$CFG->wwwroot/mod/$mod->modname/view.php?id=$mod->id\">$instancename"; } if (isediting($course->id)) { @@ -893,9 +798,127 @@ function print_course_admin_links($course, $width=180) { print_side_block(get_string("administration"), "", $admindata, $adminicon, "", $width); } -function print_course_categories($categories, $selected="none", $width=180) { - global $CFG, $THEME, $USER; - + +function make_categories_list(&$list, &$parents, $category=NULL, $path="") { +/// Given an empty array, this function recursively travels the +/// categories, building up a nice list for display. It also makes +/// an array that list all the parents for each category. + + if ($category) { + if ($path) { + $path = "$path / $category->name"; + } else { + $path = "$category->name"; + } + $list[$category->id] = $path; + } else { + $category->id = 0; + } + + if ($categories = get_categories("$category->id")) { // Print all the children recursively + foreach ($categories as $cat) { + if (!empty($category->id)) { + $parents[$cat->id] = $parents[$category->id]; + $parents[$cat->id][] = $category->id; + } + make_categories_list($list, &$parents, $cat, $path); + } + } +} + + +function fix_category_courses($categoryid) { +/// Given a category, this function makes sure the courseorder +/// variable reflects the real world. + + if (!$category = get_record("course_categories", "id", $categoryid)) { + return false; + } + + $catcourseschanged = false; + + if (trim($category->courseorder)) { + $catcourses = explode(',', $category->courseorder); + } else { + $catcourses = array(); + } + $courses = get_records("course", "category", $category->id); + + if ($catcourses) { + foreach ($catcourses as $key => $catcourse) { // Look for missing courses + if (!isset($courses[$catcourse])) { + $catcourseschanged = true; + unset($catcourses[$key]); + } + } + } + if ($courses) { + foreach ($courses as $course) { + if (!in_array($course->id, $catcourses)) { + $catcourseschanged = true; + $catcourses[] = $course->id; + } + } + } + if ($catcourseschanged) { + $category->courseorder = implode(',', $catcourses); + return set_field("course_categories", "courseorder", $category->courseorder, "id", $category->id); + } + return true; +} + + + +function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1) { +/// Recursive function to print out all the categories in a nice format +/// with or without courses included + + if (!$displaylist) { + make_categories_list(&$displaylist, &$parentslist); + } + + if ($category) { + if ($category->visible or isadmin()) { + print_category_box($category, $depth); + } else { + return; // Don't bother printing children of invisible categories + } + + } else { + print_simple_box_start("center", "100%"); + $category->id = "0"; + } + + 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; + + print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1); + } + } + + if ($category->id == "0") { + print_simple_box_end(); + } +} + + + +function print_category_box($category, $depth) { +/// Prints the category box in indented fashion + + global $CFG; + $strallowguests = get_string("allowguests"); $strrequireskey = get_string("requireskey"); @@ -905,84 +928,189 @@ function print_course_categories($categories, $selected="none", $width=180) { $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix"; } - if ($selected == "index") { // Print comprehensive index of categories with courses - if ($courses = get_courses()) { - if (isset($USER->id) and !isadmin()) { - print_simple_box_start("CENTER", "100%", $THEME->cellheading); - print_heading("".get_string("mycourses")."", "left"); - $some = false; - echo ""; - print_simple_box_end(); - print_spacer(8,1); - } - foreach ($categories as $category) { - print_simple_box_start("CENTER", "100%"); - print_heading("id\">$category->name", "left"); - $some = false; - echo "
    "; - foreach ($courses as $key => $course) { - if ($course->category == $category->id) { - echo "wwwroot/course/view.php?id=$course->id\">$course->fullname"; - echo "  "; - unset($courses[$key]); - if ($course->guest ) { - echo "wwwroot/course/view.php?id=$course->id\">"; - echo "\"\""; - } - if ($course->password) { - echo "wwwroot/course/view.php?id=$course->id\">"; - echo "\"\""; - } - echo "
    "; - $some = true; - } + $size = $depth * 40; + + $catlinkcss = $category->visible ? "" : " class=\"dimmed\" "; + + echo ""; + echo ""; + echo ""; + echo ""; + echo "
    "; + echo print_spacer(1, $size); + echo ""; + echo "wwwroot/course/index.php?category=$category->id\">$category->name"; + //echo "$category->name"; + if ($CFG->frontpage == FRONTPAGECOURSELIST) { + if ($courses = get_courses($category)) { + echo ""; - print_simple_box_end(); - print_spacer(8,1); } + echo ""; } + } + echo "
    "; - } else { // Print short list of categories only - foreach ($categories as $cat) { - $caticon[]=""; - if ($cat->id == $selected) { - $catdata[]="$cat->name"; - } else { - $catdata[]="wwwroot/course/index.php?category=$cat->id\">$cat->name"; - } +} + +function print_courses_sideblock($category=0, $width="100%") { + global $CFG, $THEME; + + if (empty($THEME->custompix)) { + $icon = "wwwroot/pix/i/course.gif\"". + " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">"; + } else { + $icon = "wwwroot/theme/$CFG->theme/pix/i/course.gif\"". + " height=\"16\" width=\"16\" alt=\"".get_string("course")."\">"; + } + + $categories = get_categories(0); // Parent = 0 ie top-level categories only + if (count($categories) > 1) { // Just print top level category links + foreach ($categories as $category) { + $linkcss = $category->visible ? "" : " class=\"dimmed\" "; + $moddata[]="wwwroot/course/index.php?category=$category->id\">$category->name"; + $modicon[]=$icon; } - $catdata[] = "wwwroot/course/index.php?category=all\">".get_string("fulllistofcourses").""; - $caticon[] = ""; - if (isset($USER->id)) { - $catdata[] = "wwwroot/course/index.php?category=my\">".get_string("mycourses").""; - $caticon[] = ""; + } else { // Just print course names of single category + $courses = get_courses($category); + if ($courses) { + foreach ($courses as $course) { + $linkcss = $course->visible ? "" : " class=\"dimmed\" "; + $moddata[]="shortname\" ". + "href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->fullname"; + $modicon[]=$icon; + } + $fulllist = "

    wwwroot/course/\">".get_string("fulllistofcourses")."..."; + } else { + $moddata = array(); + $modicon = array(); + $fulllist = get_string("nocoursesyet"); } - print_side_block(get_string("categories"), "", $catdata, $caticon, "", $width); } + + print_side_block(get_string("courses"), "", $moddata, $modicon, $fulllist, $width); } -function print_log_graph($course, $userid=0, $type="course.png", $date=0) { - global $CFG; - if (empty($CFG->gdversion)) { - echo "(".get_string("gdneed").")"; + +function print_courses($category, $width="100%") { +/// Category is 0 (for all courses) or an object + + global $CFG, $THEME; + + if (empty($category)) { + $categories = NULL; + $courses = get_courses(0); } else { - echo "wwwroot/course/loggraph.php?id=$course->id&user=$userid&type=$type&date=$date\">"; + $categories = get_categories($category->id); // sub categories + $courses = get_courses($category); + } + + if ($categories) { + print_simple_box_start("center"); + print_heading(get_string("subcategories")); + foreach ($categories as $category) { + $linkcss = $category->visible ? "" : " class=\"dimmed\" "; + echo "

    wwwroot/course/index.php?category=$category->id\">$category->name

    "; + } + print_simple_box_end(); + } + + if ($courses) { + foreach ($courses as $course) { + print_course($course, $width); + echo "
    \n"; + } + } else { + print_heading(get_string("nocoursesyet")); + } + +} + + +function print_course($course, $width="100%") { + + global $CFG, $THEME; + + if (! $site = get_site()) { + error("Could not find a site!"); + } + + if (empty($THEME->custompix)) { + $pixpath = "$CFG->wwwroot/pix"; + } else { + $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix"; + } + + print_simple_box_start("center", "$width"); + + echo ""; + echo ""; + echo ""; + echo "
    "; + echo "

    wwwroot/course/view.php?id=$course->id\">$course->fullname

    "; + if ($teachers = get_course_teachers($course->id)) { + echo "

    \n"; + foreach ($teachers as $teacher) { + if ($teacher->authority > 0) { + if (!$teacher->role) { + $teacher->role = $course->teacher; + } + echo "$teacher->role: wwwroot/user/view.php?id=$teacher->id&course=$site->id\">$teacher->firstname $teacher->lastname
    "; + } + } + echo "

    "; + } + if ($course->guest) { + $strallowguests = get_string("allowguests"); + echo "wwwroot/course/view.php?id=$course->id\">"; + echo "\"$strallowguests\"  "; + } + if ($course->password) { + $strrequireskey = get_string("requireskey"); + echo "wwwroot/course/view.php?id=$course->id\">"; + echo "\"$strrequireskey\""; + } + + + echo "
    "; + echo "

    ".text_to_html($course->summary)."

    "; + echo "
    "; + + print_simple_box_end(); +} + + +function print_my_moodle() { +/// Prints custom user information on the home page. +/// Over time this can include all sorts of information + + global $USER, $CFG; + + if (!isset($USER->id)) { + error("It shouldn't be possible to see My Moodle without being logged in."); + } + + if ($courses = get_my_courses($USER->id)) { + foreach ($courses as $course) { + print_course($course, "100%"); + echo "
    \n"; + } } + echo "

    wwwroot/course/\">".get_string("fulllistofcourses")."...

    "; } diff --git a/course/unenrol.php b/course/unenrol.php index 3c32377dd4..d9eb241244 100644 --- a/course/unenrol.php +++ b/course/unenrol.php @@ -34,7 +34,7 @@ if ($user->id == $USER->id) { unset($USER->student["$id"]); - redirect("$CFG->wwwroot"); + redirect("$CFG->wwwroot/"); } redirect("$CFG->wwwroot/user/index.php?id=$course->id"); diff --git a/index.php b/index.php index ed7a34dc50..f48298d147 100644 --- a/index.php +++ b/index.php @@ -61,14 +61,23 @@ echo "
    "; } - if ($site->newsitems > 0 ) { - $categories = get_categories(); - if (count($categories) > 1) { - print_course_categories($categories, "none", $side); - } else { - $category = array_shift($categories); - print_all_courses($category->id, "minimal", 10, $side); - } + switch ($CFG->frontpage) { + case FRONTPAGENEWS: // print news links on the side + print_courses_sideblock(0, "$side"); + break; + + case FRONTPAGECOURSELIST: + case FRONTPAGECATEGORYNAMES: + if ($site->newsitems) { + if ($news = forum_get_course_forum($site->id, "news")) { + print_side_block_start(get_string("latestnews"), $side, "sideblocklatestnews"); + echo ""; + forum_print_latest_discussions($news->id, $site->newsitems, "minimal", "", false); + echo ""; + print_side_block_end(); + } + } + break; } print_spacer(1,$side); } @@ -86,38 +95,49 @@ } echo "
"; - if ($site->newsitems == 0 ) { - print_heading_block(get_string("availablecourses")); - print_spacer(8,1); - $categories = get_categories(); - if (count($categories) > 1) { - print_course_categories($categories, "index"); - } else { - print_all_courses("all"); - } - - } else { - if (! $newsforum = forum_get_course_forum($site->id, "news")) { - error("Could not find or create a main news forum for the site"); - } - - if (isset($USER->id)) { - $SESSION->fromdiscussion = "$CFG->wwwroot"; - if (forum_is_subscribed($USER->id, $newsforum->id)) { - $subtext = get_string("unsubscribe", "forum"); + switch ($CFG->frontpage) { /// Display the main part of the front page. + case FRONTPAGENEWS: + if (! $newsforum = forum_get_course_forum($site->id, "news")) { + error("Could not find or create a main news forum for the site"); + } + + if (isset($USER->id)) { + $SESSION->fromdiscussion = "$CFG->wwwroot"; + if (forum_is_subscribed($USER->id, $newsforum->id)) { + $subtext = get_string("unsubscribe", "forum"); + } else { + $subtext = get_string("subscribe", "forum"); + } + $headertext = " + +
$newsforum->name + id\">$subtext +
"; } else { - $subtext = get_string("subscribe", "forum"); + $headertext = $newsforum->name; } - $headertext = " -
- id\">$subtext -
$newsforum->name"; - } else { - $headertext = $newsforum->name; - } - print_heading_block($headertext); - print_spacer(8,1); - forum_print_latest_discussions($newsforum->id, $site->newsitems); + print_heading_block($headertext); + print_spacer(8,1); + forum_print_latest_discussions($newsforum->id, $site->newsitems); + break; + + case FRONTPAGECOURSELIST: + case FRONTPAGECATEGORYNAMES: + if (isset($USER->id) and !isset($USER->admin)) { + print_heading_block(get_string("mycourses")); + print_spacer(8,1); + print_my_moodle(); + } else { + print_heading_block(get_string("availablecourses")); + print_spacer(8,1); + if (count_records("course_categories") > 1) { + print_whole_category_list(); + } else { + print_courses(0, "100%"); + } + } + break; + } echo "