Merge branch 'wip-mdl-33367' of git://github.com/rajeshtaneja/moodle
authorSam Hemelryk <sam@moodle.com>
Wed, 6 Jun 2012 03:19:49 +0000 (15:19 +1200)
committerSam Hemelryk <sam@moodle.com>
Wed, 6 Jun 2012 03:19:49 +0000 (15:19 +1200)
course/lib.php
course/tests/courselib_test.php
course/view.php
course/yui/dragdrop/dragdrop.js

index c45056b..26fea72 100644 (file)
@@ -2953,8 +2953,11 @@ function delete_mod_from_section($mod, $section) {
  * @param int $section Section number (not id!!!)
  * @param int $move (-1 or 1)
  * @return boolean true if section moved successfully
+ * @todo MDL-33379 remove this function in 2.5
  */
 function move_section($course, $section, $move) {
+    debugging('This function will be removed before 2.5 is released please use move_section_to', DEBUG_DEVELOPER);
+
 /// Moves a whole course section up and down within the course
     global $USER, $DB;
 
@@ -2968,41 +2971,12 @@ function move_section($course, $section, $move) {
         return false;
     }
 
-    if (!$sectionrecord = $DB->get_record("course_sections", array("course"=>$course->id, "section"=>$section))) {
-        return false;
-    }
-
-    if (!$sectiondestrecord = $DB->get_record("course_sections", array("course"=>$course->id, "section"=>$sectiondest))) {
-        return false;
-    }
-
-    // Three-step change ensures that the section always remains unique (there is
-    // a unique index now)
-    $DB->set_field("course_sections", "section", -$sectiondest, array("id"=>$sectionrecord->id));
-    $DB->set_field("course_sections", "section", $section, array("id"=>$sectiondestrecord->id));
-    $DB->set_field("course_sections", "section", $sectiondest, array("id"=>$sectionrecord->id));
-
-    // Update highlighting if the move affects highlighted section
-    if ($course->marker == $section) {
-        course_set_marker($course->id, $sectiondest);
-    } elseif ($course->marker == $sectiondest) {
-        course_set_marker($course->id, $section);
+    $retval = move_section_to($course, $section, $sectiondest);
+    // If section moved, then rebuild course cache.
+    if ($retval) {
+        rebuild_course_cache($course->id, true);
     }
-
-
-    // Fix order if needed. The database prevents duplicate sections, but it is
-    // possible there could be a gap in the numbering.
-    $sections = $DB->get_records('course_sections', array('course'=>$course->id), 'section ASC');
-    $n = 0;
-    foreach ($sections as $section) {
-        if ($section->section != $n) {
-            $DB->set_field('course_sections', 'section', $n, array('id'=>$section->id));
-        }
-        $n++;
-    }
-    // After moving section, rebuild course cache.
-    rebuild_course_cache($course->id, true);
-    return true;
+    return $retval;
 }
 
 /**
@@ -3023,7 +2997,7 @@ function move_section_to($course, $section, $destination) {
         return true;
     }
 
-    if ($destination > $course->numsections) {
+    if (($destination > $course->numsections) || ($destination < 1)) {
         return false;
     }
 
@@ -3112,6 +3086,10 @@ function reorder_sections($sections, $origin_position, $target_position) {
             unset($sections[$id]);
         }
         if ($position == $target_position) {
+            if ($target_position < $origin_position) {
+                $append_array[$id] = $position;
+                unset($sections[$id]);
+            }
             $found = true;
         }
     }
index f7f4f80..2274e6d 100644 (file)
@@ -52,6 +52,16 @@ class courselib_testcase extends advanced_testcase {
         $this->assertEquals($oldsections[5], $neworder[5]);
         $this->assertEquals($oldsections[6], $neworder[6]);
 
+        $neworder = reorder_sections($sections, 4, 2);
+        $neworder = array_keys($neworder);
+        $this->assertEquals($oldsections[0], $neworder[0]);
+        $this->assertEquals($oldsections[1], $neworder[1]);
+        $this->assertEquals($oldsections[2], $neworder[3]);
+        $this->assertEquals($oldsections[3], $neworder[4]);
+        $this->assertEquals($oldsections[4], $neworder[2]);
+        $this->assertEquals($oldsections[5], $neworder[5]);
+        $this->assertEquals($oldsections[6], $neworder[6]);
+
         $neworder = reorder_sections(1, 2, 4);
         $this->assertFalse($neworder);
     }
index 2d4d347..2b6a908 100644 (file)
 
             if (!empty($section)) {
                 if (!empty($move) and confirm_sesskey()) {
-                    if (move_section($course, $section, $move)) {
+                    $destsection = $section + $move;
+                    if (move_section_to($course, $section, $destsection)) {
+                        // Rebuild course cache, after moving section
+                        rebuild_course_cache($course->id, true);
                         if ($course->id == SITEID) {
                             redirect($CFG->wwwroot . '/?redirect=0');
                         } else {
index 080cace..2b492ad 100644 (file)
@@ -135,12 +135,10 @@ YUI.add('moodle-course-dragdrop', function(Y) {
             var dragnodeid = Number(this.get_section_id(dragnode));
             var dropnodeid = Number(this.get_section_id(dropnode));
 
-            var targetoffset = 0;
             var loopstart = dragnodeid;
             var loopend = dropnodeid;
 
             if (this.goingup) {
-                targetoffset = 1;
                 loopstart = dropnodeid;
                 loopend = dragnodeid;
             }
@@ -166,7 +164,7 @@ YUI.add('moodle-course-dragdrop', function(Y) {
             params['class'] = 'section';
             params.field = 'move';
             params.id = dragnodeid;
-            params.value = dropnodeid - targetoffset;
+            params.value = dropnodeid;
 
             // Do AJAX request
             var uri = M.cfg.wwwroot + this.get('ajaxurl');