function testReorderSections() { $sections = array(20 => 0, 21 => 1, 22 => 2, 23 => 3, 24 => 4, 25 => 5); $this->assertFalse(reorder_sections(1, 3, 4)); $newsections = reorder_sections($sections, 2, 4); $newsections_flipped = array_flip($newsections); $this->assertEqual(20, reset($newsections_flipped)); $this->assertEqual(21, next($newsections_flipped)); $this->assertEqual(23, next($newsections_flipped)); $this->assertEqual(24, next($newsections_flipped)); $this->assertEqual(22, next($newsections_flipped)); $this->assertEqual(25, next($newsections_flipped)); $newsections = reorder_sections($sections, 4, 0); $newsections_flipped = array_flip($newsections); $this->assertEqual(20, reset($newsections_flipped)); $this->assertEqual(24, next($newsections_flipped)); $this->assertEqual(21, next($newsections_flipped)); $this->assertEqual(22, next($newsections_flipped)); $this->assertEqual(23, next($newsections_flipped)); $this->assertEqual(25, next($newsections_flipped)); $newsections = reorder_sections($sections, 1, 5); $newsections_flipped = array_flip($newsections); $this->assertEqual(20, reset($newsections_flipped)); $this->assertEqual(22, next($newsections_flipped)); $this->assertEqual(23, next($newsections_flipped)); $this->assertEqual(24, next($newsections_flipped)); $this->assertEqual(25, next($newsections_flipped)); $this->assertEqual(21, next($newsections_flipped)); }
public function test_reorder_sections() { global $DB; $this->resetAfterTest(true); $this->getDataGenerator()->create_course(array('numsections' => 5), array('createsections' => true)); $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true)); $oldsections = array(); $sections = array(); foreach ($DB->get_records('course_sections', array('course' => $course->id)) as $section) { $oldsections[$section->section] = $section->id; $sections[$section->id] = $section->section; } ksort($oldsections); $neworder = reorder_sections($sections, 2, 4); $neworder = array_keys($neworder); $this->assertEquals($oldsections[0], $neworder[0]); $this->assertEquals($oldsections[1], $neworder[1]); $this->assertEquals($oldsections[2], $neworder[4]); $this->assertEquals($oldsections[3], $neworder[2]); $this->assertEquals($oldsections[4], $neworder[3]); $this->assertEquals($oldsections[5], $neworder[5]); $this->assertEquals($oldsections[6], $neworder[6]); $neworder = reorder_sections(1, 2, 4); $this->assertFalse($neworder); }
/** * Moves a section within a course, from a position to another. * Be very careful: $section and $destination refer to section number, * not id!. * * @param object $course * @param int $section Section number (not id!!!) * @param int $destination * @return boolean Result */ function move_section_to($course, $section, $destination) { /// Moves a whole course section up and down within the course global $USER, $DB; if (!$destination && $destination != 0) { return true; } if ($destination > $course->numsections) { return false; } // Get all sections for this course and re-order them (2 of them should now share the same section number) if (!($sections = $DB->get_records_menu('course_sections', array('course' => $course->id), 'section ASC, id ASC', 'id, section'))) { return false; } $movedsections = reorder_sections($sections, $section, $destination); // Update all sections. Do this in 2 steps to avoid breaking database // uniqueness constraint $transaction = $DB->start_delegated_transaction(); foreach ($movedsections as $id => $position) { if ($sections[$id] !== $position) { $DB->set_field('course_sections', 'section', -$position, array('id' => $id)); } } foreach ($movedsections as $id => $position) { if ($sections[$id] !== $position) { $DB->set_field('course_sections', 'section', $position, array('id' => $id)); } } // Adjust destination to reflect the actual section $moveup = false; if ($section > $destination) { $destination++; $moveup = true; } // If we move the highlighted section itself, then just highlight the destination. // Adjust the higlighted section location if we move something over it either direction. if ($section == $course->marker) { course_set_marker($course, $destination); } elseif ($moveup && $section > $course->marker && $course->marker >= $destination) { course_set_marker($course, $course->marker + 1); } elseif (!$moveup && $section < $course->marker && $course->marker <= $destination) { course_set_marker($course, $course->marker - 1); } // if the focus is on the section that is being moved, then move the focus along if (course_get_display($course->id) == $section) { course_set_display($course->id, $destination); } $transaction->allow_commit(); return true; }
/** * Moves a section within a course, from a position to another. * Be very careful: $section and $destination refer to section number, * not id!. * * @param object $course * @param int $section Section number (not id!!!) * @param int $destination * @return boolean Result */ function move_section_to($course, $section, $destination) { /// Moves a whole course section up and down within the course global $USER, $DB; if (!$destination && $destination != 0) { return true; } // compartibility with course formats using field 'numsections' $courseformatoptions = course_get_format($course)->get_format_options(); if (array_key_exists('numsections', $courseformatoptions) && $destination > $courseformatoptions['numsections'] || $destination < 1) { return false; } // Get all sections for this course and re-order them (2 of them should now share the same section number) if (!($sections = $DB->get_records_menu('course_sections', array('course' => $course->id), 'section ASC, id ASC', 'id, section'))) { return false; } $movedsections = reorder_sections($sections, $section, $destination); // Update all sections. Do this in 2 steps to avoid breaking database // uniqueness constraint $transaction = $DB->start_delegated_transaction(); foreach ($movedsections as $id => $position) { if ($sections[$id] !== $position) { $DB->set_field('course_sections', 'section', -$position, array('id' => $id)); } } foreach ($movedsections as $id => $position) { if ($sections[$id] !== $position) { $DB->set_field('course_sections', 'section', $position, array('id' => $id)); } } // If we move the highlighted section itself, then just highlight the destination. // Adjust the higlighted section location if we move something over it either direction. if ($section == $course->marker) { course_set_marker($course->id, $destination); } elseif ($section > $course->marker && $course->marker >= $destination) { course_set_marker($course->id, $course->marker + 1); } elseif ($section < $course->marker && $course->marker <= $destination) { course_set_marker($course->id, $course->marker - 1); } $transaction->allow_commit(); rebuild_course_cache($course->id, true); return true; }
/** * Moves a section within a course, from a position to another. * Be very careful: $section and $destination refer to section number, * not id!. * * @param object $course * @param int $section Section number (not id!!!) * @param int $destination * @return boolean Result */ function move_section_to($course, $section, $destination) { /// Moves a whole course section up and down within the course global $USER; if (!$destination && $destination != 0) { return true; } if ($destination > $course->numsections) { return false; } // Get all sections for this course and re-order them (2 of them should now share the same section number) if (!($sections = get_records_menu('course_sections', 'course', $course->id, 'section ASC, id ASC', 'id, section'))) { return false; } $sections = reorder_sections($sections, $section, $destination); // Update all sections foreach ($sections as $id => $position) { set_field('course_sections', 'section', $position, 'id', $id); } // if the focus is on the section that is being moved, then move the focus along if (isset($USER->display[$course->id]) and $USER->display[$course->id] == $section) { course_set_display($course->id, $destination); } return true; }