Index: course/lib.php
===================================================================
--- course/lib.php	(revision 4)
+++ course/lib.php	(working copy)
@@ -3124,40 +3124,39 @@
     global $CFG;
 
     if (!empty($courseids)) {
+        $newparent = get_context_instance(CONTEXT_COURSECAT, $categoryid);
 
-            $courseids = array_reverse($courseids);
+        // 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 (is_null($sortorder) || $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;
+        }
 
-            foreach ($courseids as $courseid) {
+        $courseids = array_reverse($courseids);
 
-                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 (is_null($sortorder) || $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);
-                    }
+        foreach ($courseids as $courseid) {
 
-                    $course->category  = $categoryid;
-                    $course->sortorder = $sortorder;
+            $course            = new stdClass;
+            $course->id        = $courseid;
+            $course->category  = $categoryid;
+            $course->sortorder = $sortorder;
 
-                    if (!update_record('course', addslashes_recursive($course))) {
-                        notify("An error occurred - course not moved!");
-                    }
-
-                    $context   = get_context_instance(CONTEXT_COURSE, $course->id);
-                    $newparent = get_context_instance(CONTEXT_COURSECAT, $course->category);
-                    context_moved($context, $newparent);
-                }
+            if (!update_record('course', addslashes_recursive($course))) {
+                notify("An error occurred - course not moved!");
+                continue;
             }
-            fix_course_sortorder();
+
+            $context   = get_context_instance(CONTEXT_COURSE, $course->id);
+            context_moved($context, $newparent);
+
+            $sortorder--;
         }
+        fix_course_sortorder();
+    }
     return true;
 }
 
