From 861efb1937be51e559d4360f785780bb61da6ecb Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Thu, 1 Sep 2005 04:48:22 +0000 Subject: [PATCH] Allow moving courses and editing courses and stuff from the course results search page (now has turn editing on button) --- course/category.php | 38 +++--------- course/lib.php | 56 ++++++++++++++++- course/search.php | 146 ++++++++++++++++++++++++++++++++++++++++---- lang/en/moodle.php | 2 + lib/javascript.php | 7 +++ lib/weblib.php | 31 +++++++++- 6 files changed, 236 insertions(+), 44 deletions(-) diff --git a/course/category.php b/course/category.php index 897442f45a8..ffb68d5c854 100644 --- a/course/category.php +++ b/course/category.php @@ -132,35 +132,14 @@ error("Error finding the category"); } - unset($data->moveto); - unset($data->id); - unset($data->sesskey); - - if ($data) { - foreach ($data as $code => $junk) { - - $courseid = substr($code, 1); - - if (! record_exists('course', 'id', $courseid)) { - notify('Error finding a course'); - } else { - // figure out a sortorder that we can use in the destination category - $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min - FROM ' . $CFG->prefix . 'course WHERE category=' . $destcategory->id) || 1000; - - $newcourse = new stdClass; - $newcourse->id = $courseid; - $newcourse->category = $destcategory->id; - $newcourse->sortorder = $sortorder; - - if (!update_record('course', $newcourse)) { - notify("An error occurred - course not moved!"); - } - fix_course_sortorder(); - } + + $courses = array(); + foreach ( $data as $key => $value ) { + if (preg_match('/^c\d+$/', $key)) { + array_push($courses, substr($key, 1)); } - $category = get_record('course_categories', 'id', $category->id); // Refresh it - } + } + move_courses($courses, $data->moveto); } /// Hide or show a course @@ -430,6 +409,9 @@ echo ''; echo ""; echo "
"; + + print_course_search(); + } echo ""; diff --git a/course/lib.php b/course/lib.php index 6e6faf32fd2..a796ee5126c 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1400,7 +1400,7 @@ function print_courses($category, $width="100%") { function print_course($course, $width="100%") { - global $CFG; + global $CFG, $USER; static $enrol; @@ -1962,4 +1962,58 @@ function course_allowed_module($course,$mod) { return (record_exists("course_allowed_modules","course",$course->id,"module",$modid)); } +/*** + *** Efficiently moves many courses around while maintaining + *** sortorder in order. + *** + *** $courseids is an array of course ids + *** + **/ + +function move_courses ($courseids, $categoryid) { + + global $CFG; + + if (!empty($courseids)) { + + $courseids = array_reverse($courseids); + + foreach ($courseids as $courseid) { + + if (! $course = get_record("course", "id", $courseid)) { + notify("Error finding course $courseid"); + } else { + // figure out a sortorder that we can use in the destination category + $sortorder = get_field_sql('SELECT MIN(sortorder)-1 AS min + FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid); + if ($sortorder === false) { + // the category is empty + // rather than let the db default to 0 + // set it to > 100 and avoid extra work in fix_coursesortorder() + $sortorder = 200; + } else if ($sortorder < 10) { + fix_course_sortorder($categoryid); + } + + $course->category = $categoryid; + $course->sortorder = $sortorder; + $course->fullname = addslashes($course->fullname); + $course->shortname = addslashes($course->shortname); + $course->summary = addslashes($course->summary); + $course->password = addslashes($course->password); + $course->teacher = addslashes($course->teacher); + $course->teachers = addslashes($course->teachers); + $course->student = addslashes($course->student); + $course->students = addslashes($course->students); + + if (!update_record('course', $course)) { + notify("An error occurred - course not moved!"); + } + } + } + fix_course_sortorder(); + } + return true; +} + ?> diff --git a/course/search.php b/course/search.php index 8846ecd2b02..dd0cce5b73b 100644 --- a/course/search.php +++ b/course/search.php @@ -27,6 +27,26 @@ require_login(); } + if (iscreator()) { + if (isset($_GET['edit']) and confirm_sesskey()) { + if ($edit == "on") { + $USER->categoriessearchediting = true; + } else if ($edit == "off") { + $USER->categoriessearchediting = false; + } + + print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&perpage=$perpage&"); + + } + + $creatorediting = !empty($USER->categoriessearchediting); + $adminediting = (isadmin() and $creatorediting); + } + + if ($adminediting && $perpage != 99999) { + $perpage = 30; + } + $displaylist = array(); $parentlist = array(); make_categories_list($displaylist, $parentlist, ""); @@ -35,6 +55,10 @@ $strsearch = get_string("search"); $strsearchresults = get_string("searchresults"); $strcategory = get_string("category"); + $strselect = get_string("select"); + $strselectall = get_string("selectall"); + $strdeselectall = get_string("deselectall"); + $stredit = get_string("edit"); if (!$search) { print_header("$site->fullname : $strsearch", $site->fullname, @@ -52,32 +76,128 @@ exit; } + if (isset($moveto) and $data = data_submitted() and confirm_sesskey()) { // Some courses are being moved + + if (! $destcategory = get_record("course_categories", "id", $data->moveto)) { + error("Error finding the category"); + } + + $courses = array(); + foreach ( $data as $key => $value ) { + if (preg_match('/^c\d+$/', $key)) { + array_push($courses, substr($key, 1)); + } + } + move_courses($courses, $data->moveto); + } + + $courses = get_courses_search($searchterms, "fullname ASC", + $page*$perpage, $perpage, $totalcount); + $searchform = print_course_search($search, true, "navbar"); + if (!empty($courses) && iscreator()) { + $searchform .= update_categories_search_button($search,$page,$perpage); + } + + print_header("$site->fullname : $strsearchresults", $site->fullname, "$strcourses -> $strsearch -> '$search'", "", "", "", $searchform); $lastcategory = -1; - if ($courses = get_courses_search($searchterms, "fullname ASC", - $page*$perpage, $perpage, $totalcount)) { + if ($courses) { print_heading("$strsearchresults: $totalcount"); - print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&perpage=$perpage&"); + if (!$adminediting) { + foreach ($courses as $course) { + $course->fullname = highlight("$search", $course->fullname); + $course->summary = highlight("$search", $course->summary); + $course->summary .= "

"; + $course->summary .= "$strcategory: category\">"; + $course->summary .= $displaylist[$course->category]; + $course->summary .= "

"; + print_course($course); + print_spacer(5,5); + } + } else { // slightly more sophisticated + + $oldperpage = ($adminediting) ? 30 : 10; + print_paging_bar($totalcount, $page, $oldperpage, "search.php?search=$search&perpage=$perpage&",($perpage == 99999)); + + if ($perpage != 99999 && $totalcount > $perpage) { + echo "

"; + echo "".get_string("showall", "", $totalcount).""; + echo "

"; + } + echo "
"; + echo "sesskey\">"; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + foreach ($courses as $course) { + $course->fullname = highlight("$search", $course->fullname); + $linkcss = $course->visible ? "" : " class=\"dimmed\" "; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + echo ""; + echo "
$strcourses$strcategory$strselect$stredit
id\">$course->fullname".$displaylist[$course->category].""; + echo "id\">"; + echo ""; + if (empty($THEME->custompix)) { + $pixpath = "$CFG->wwwroot/pix"; + } else { + $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix"; + } + echo "wwwroot/course/edit.php?id=$course->id\"> "; + echo "wwwroot/course/teacher.php?id=$course->id\"> "; + echo "id\"> "; + if (!empty($course->visible)) { + echo "category&hide=$course->id&sesskey=$USER->sesskey\"> "; + } else { + echo "category&show=$course->id&sesskey=$USER->sesskey\"> "; + } + + echo "id\"> "; + + echo "id&wdir=/backupdata\"> "; + echo "
"; + echo "
"; + echo "\n"; + echo "\n"; + choose_from_menu ($displaylist, "moveto", "", get_string("moveselectedcoursesto"), "javascript:document.movecourses.submit()"); + echo "
"; + + print_paging_bar($totalcount, $page, $oldperpage, "search.php?search=$search&perpage=30&",($perpage == 99999)); + + + if ($perpage != 99999 && $totalcount > $perpage) { + echo "

"; + echo "".get_string("showall", "", $totalcount).""; + echo "

"; + } - foreach ($courses as $course) { - $course->fullname = highlight("$search", $course->fullname); - $course->summary = highlight("$search", $course->summary); - $course->summary .= "

"; - $course->summary .= "$strcategory: category\">"; - $course->summary .= $displaylist[$course->category]; - $course->summary .= "

"; - print_course($course); - print_spacer(5,5); } - print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&perpage=$perpage&"); + + } else { print_heading(get_string("nocoursesfound", "", $search)); diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 015984dd08f..0b4e0f4a08d 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -309,6 +309,7 @@ $string['deletingexistingcoursedata'] = 'Deleting existing course data'; $string['deletingolddata'] = 'Deleting old data'; $string['department'] = 'Department'; $string['description'] = 'Description'; +$string['deselectall'] = 'Deselect all'; $string['detailedless'] = 'Less detailed'; $string['detailedmore'] = 'More detailed'; $string['directorypaths'] = 'Directory Paths'; @@ -985,6 +986,7 @@ $string['section'] = 'Section'; $string['sections'] = 'Sections'; $string['seealsostats'] = 'See also: stats'; $string['select'] = 'Select'; +$string['selectall'] = 'Select all'; $string['selectacountry'] = 'Select a country'; $string['selectednowmove'] = '$a files selected for moving. Now go to the destination and press \'Move files to here\''; $string['senddetails'] = 'Send my details via email'; diff --git a/lib/javascript.php b/lib/javascript.php index f7488c57789..ceafa522e75 100644 --- a/lib/javascript.php +++ b/lib/javascript.php @@ -33,6 +33,13 @@ function openpopup(url,name,options,fullscreen) { return false; } +function uncheckall() { + void(d=document); + void(el=d.getElementsByTagName('INPUT')); + for(i=0;iinserttextform)) { diff --git a/lib/weblib.php b/lib/weblib.php index b809428aaed..4cb493136ce 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -3210,6 +3210,33 @@ function update_categories_button() { } } +/** + * Prints the editing button on search results listing + * For bulk move courses to another category + */ + +function update_categories_search_button($search,$page,$perpage) { + global $CFG, $USER; + + if (isadmin()) { + if (!empty($USER->categoriessearchediting)) { + $string = get_string("turneditingoff"); + $edit = "off"; + $perpage = 30; + } else { + $string = get_string("turneditingon"); + $edit = "on"; + } + return "framename\" method=\"get\" action=\"$CFG->wwwroot/course/search.php\">". + "". + "sesskey\" />". + "". + "". + "". + ""; + } +} + /** * Prints the editing button on group page * @@ -4022,7 +4049,7 @@ function obfuscate_mailto($email, $label='', $dimmed=false) { var an equal sign, then the page number. * @param string $pagevar This is the variable name that you use for the page number in your code (ie. 'tablepage', 'blogpage', etc) */ -function print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar='page') { +function print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar='page',$nocurr=false) { $maxdisplay = 18; @@ -4044,7 +4071,7 @@ function print_paging_bar($totalcount, $page, $perpage, $baseurl, $pagevar='page $displaycount = 0; while ($displaycount < $maxdisplay and $currpage < $lastpage) { $displaypage = $currpage+1; - if ($page == $currpage) { + if ($page == $currpage && empty($nocurr)) { echo '  '. $displaypage; } else { echo '  '. $displaypage .'';