/// Resort the category if requested
if (!empty($_GET['resort']) and confirm_sesskey()) {
- if ($courses = get_courses($category->id, "fullname ASC")) {
- $count = 0;
+ if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
+ // move it off the range
+ $count = get_record_sql('SELECT MAX(sortorder) AS max, 1
+ FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
+ $count = $count->max + 100;
+ begin_sql();
foreach ($courses as $course) {
set_field('course', 'sortorder', $count, 'id', $course->id);
$count++;
}
- fix_course_sortorder();
+ commit_sql();
+ fix_course_sortorder($category->id);
}
}
}
$movecourse = NULL;
$swapcourse = NULL;
- $courses = get_courses($category->id,'c.sortorder ASC', 'c.id,c.sortorder');
-
+ // ensure the course order has no gaps
+ // and isn't at 0
+ fix_course_sortorder($category->id);
+
+ // we are going to need to know the range
+ $max = get_record_sql('SELECT MAX(sortorder) AS max, 1
+ FROM ' . $CFG->prefix . 'course WHERE category=' . $category->id);
+ $max = $max->max + 100;
+
if (isset($moveup)) {
- if ($movecourse = get_record("course", "id", $moveup)) {
- foreach ($courses as $course) {
- if ($course->id == $movecourse->id) {
- break;
- }
- $swapcourse = $course;
- }
- }
- }
- if (isset($movedown)) {
- if ($movecourse = get_record("course", "id", $movedown)) {
- $choosenext = false;
- foreach ($courses as $course) {
- if ($choosenext) {
- $swapcourse = $course;
- break;
- }
- if ($course->id == $movecourse->id) {
- $choosenext = true;
- }
- }
- }
+ $movecourse = get_record('course', 'id', $moveup);
+ $swapcourse = get_record('course',
+ 'category', $category->id,
+ 'sortorder', $movecourse->sortorder - 1);
+ } else {
+ $movecourse = get_record('course', 'id', $movedown);
+ $swapcourse = get_record('course',
+ 'category', $category->id,
+ 'sortorder', $movecourse->sortorder + 1);
}
+
if ($swapcourse and $movecourse) { // Renumber everything for robustness
- $count=0;
begin_sql();
- foreach ($courses as $course) {
- $count++;
- if ($course->id == $swapcourse->id) {
- $course = $movecourse;
- } else if ($course->id == $movecourse->id) {
- $course = $swapcourse;
- }
- if (! set_field("course", "sortorder", $count, "id", $course->id)) {
- notify("Could not update that course!");
- }
+ if (!( set_field("course", "sortorder", $max, "id", $swapcourse->id)
+ && set_field("course", "sortorder", $swapcourse->sortorder, "id", $movecourse->id)
+ && set_field("course", "sortorder", $movecourse->sortorder, "id", $swapcourse->id)
+ )) {
+ notify("Could not update that course!");
}
commit_sql();
}
- }
- fix_course_sortorder();
+ }
} // End of editing stuff
echo '<a title="'.$strdelete.'" href="delete.php?id='.$acourse->id.'">'.
'<img src="'.$pixpath.'/t/delete.gif" height="11" width="11" border="0" alt="" /></a> ';
if (!empty($acourse->visible)) {
- echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.
+ echo '<a title="'.$strhide.'" href="category.php?id='.$category->id.'&page='.$page.
'&hide='.$acourse->id.'&sesskey='.$USER->sesskey.'">'.
'<img src="'.$pixpath.'/t/hide.gif" height="11" width="11" border="0" alt="" /></a> ';
} else {
- echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.
+ echo '<a title="'.$strshow.'" href="category.php?id='.$category->id.'&page='.$page.
'&show='.$acourse->id.'&sesskey='.$USER->sesskey.'">'.
'<img src="'.$pixpath.'/t/show.gif" height="11" width="11" border="0" alt="" /></a> ';
}
'<img src="'.$pixpath.'/t/restore.gif" height="11" width="11" border="0" alt="" /></a> ';
if ($up) {
- echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.
+ echo '<a title="'.$strmoveup.'" href="category.php?id='.$category->id.'&page='.$page.
'&moveup='.$acourse->id.'&sesskey='.$USER->sesskey.'">'.
'<img src="'.$pixpath.'/t/up.gif" height="11" width="11" border="0" alt="" /></a> ';
} else {
}
if ($down) {
- echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.
+ echo '<a title="'.$strmovedown.'" href="category.php?id='.$category->id.'&page='.$page.
'&movedown='.$acourse->id.'&sesskey='.$USER->sesskey.'">'.
'<img src="'.$pixpath.'/t/down.gif" height="11" width="11" border="0" alt="" /></a> ';
} else {
/**
- * This recursive function makes sure that the courseorder is consecutive
- *
- * @param int $categoryid ?
- * @param int $n ?
- * @return ?
- * @todo Finish documenting this function
- */
-function fix_course_sortorder($categoryid=0, $n=0) {
+* This recursive function makes sure that the courseorder is consecutive
+*
+* @param type description
+*
+* $n is the starting point, offered only for compatilibity -- will be ignored!
+* $safe (bool) prevents it from assuming category-sortorder is unique, used to upgrade
+* safely from 1.4 to 1.5
+*/
+function fix_course_sortorder($categoryid=0, $n=0, $safe=0) {
+
+ global $CFG;
$count = 0;
- if ($courses = get_courses($categoryid)) {
- foreach ($courses as $course) {
- set_field('course', 'sortorder', $n, 'id', $course->id);
- $n++;
- $count++;
- }
+
+ $n=100;
+
+ // get some basic info
+ $info = get_record_sql('SELECT MIN(sortorder) AS min,
+ MAX(sortorder) AS max,
+ COUNT(sortorder) AS count
+ FROM ' . $CFG->prefix . 'course
+ WHERE category=' . $categoryid);
+ if (is_object($info)) { // no courses?
+ $max = $info->max;
+ $count = $info->count;
+ $min = $info->min;
+ unset($info);
+ }
+
+ // actually sort only if there are courses,
+ // and we meet one ofthe triggers:
+ // - safe flag
+ // - they are not in a continuos block
+ // - they are too close to the 'bottom'
+ if ($count && ( $safe
+ || ($max-$min+1!=$count)
+ || $min < 10 ) ) {
+ if ($courses = get_courses($categoryid, 'c.sortorder ASC', 'c.id,c.sortorder')) {
+ begin_sql();
+
+ // find the ideal starting point
+ if ( ($min<$n&&$n<$max) || ($n+$count>=$min) || ($min<10) ) {
+
+ $n = $max+100; // this is usually the ideal solution
+
+ // if we are aiming way too high, try to bring it back to earth
+ if ($n > 100+3*$count) {
+ if ($min > 100+$count){
+ $n = 100;
+ }
+ }
+ }
+
+ foreach ($courses as $course) {
+ if ($course->sortorder != $n ) { // save db traffic
+ set_field('course', 'sortorder', $n, 'id', $course->id);
+ }
+ $n++;
+ }
+ commit_sql();
+ }
}
set_field('course_categories', 'coursecount', $count, 'id', $categoryid);
+ $n=0;
if ($categories = get_categories($categoryid)) {
foreach ($categories as $category) {
$n = fix_course_sortorder($category->id, $n);