public function test_move_section() { 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(); foreach ($DB->get_records('course_sections', array('course' => $course->id)) as $section) { $oldsections[$section->section] = $section->id; } ksort($oldsections); move_section_to($course, 2, 4); $sections = array(); foreach ($DB->get_records('course_sections', array('course' => $course->id)) as $section) { $sections[$section->section] = $section->id; } ksort($sections); $this->assertEquals($oldsections[0], $sections[0]); $this->assertEquals($oldsections[1], $sections[1]); $this->assertEquals($oldsections[2], $sections[4]); $this->assertEquals($oldsections[3], $sections[2]); $this->assertEquals($oldsections[4], $sections[3]); $this->assertEquals($oldsections[5], $sections[5]); $this->assertEquals($oldsections[6], $sections[6]); }
/** * Create a section. */ private function create_section($course, $section, $prepend = false) { global $DB; $maxid = $DB->get_field('course_sections', 'MAX(section)', array('course' => $course->id)); $section->section = $maxid + 1; \cattarget_section\section::create_section($course, $section); if ($prepend) { move_section_to($course, $section->section, 1); } }
function testMoveSection() { global $DB; $course = new stdClass(); $course->numsections = 10; $course->id = 1; $sections = array(20 => 0, 21 => 1, 22 => 2, 23 => 3, 24 => 4, 25 => 5); $DB->setReturnValueAt(0, 'get_records_menu', $sections); $DB->expectAt(0, 'set_field', array('course_sections', 'section', 0, array('id' => 20))); $DB->expectAt(1, 'set_field', array('course_sections', 'section', 1, array('id' => 21))); $DB->expectAt(2, 'set_field', array('course_sections', 'section', 2, array('id' => 23))); $DB->expectAt(3, 'set_field', array('course_sections', 'section', 3, array('id' => 24))); $DB->expectAt(4, 'set_field', array('course_sections', 'section', 4, array('id' => 22))); $DB->expectAt(5, 'set_field', array('course_sections', 'section', 5, array('id' => 25))); move_section_to($course, 2, 4); $DB->setReturnValueAt(1, 'get_records_menu', $sections); $DB->expectAt(6, 'set_field', array('course_sections', 'section', 0, array('id' => 20))); $DB->expectAt(7, 'set_field', array('course_sections', 'section', 1, array('id' => 24))); $DB->expectAt(8, 'set_field', array('course_sections', 'section', 2, array('id' => 21))); $DB->expectAt(9, 'set_field', array('course_sections', 'section', 3, array('id' => 22))); $DB->expectAt(10, 'set_field', array('course_sections', 'section', 4, array('id' => 23))); $DB->expectAt(11, 'set_field', array('course_sections', 'section', 5, array('id' => 25))); move_section_to($course, 4, 0); }
/** * Deletes a section * * Do not call this function directly, instead call {@link course_delete_section()} * * @param int|stdClass|section_info $section * @param bool $forcedeleteifnotempty if set to false section will not be deleted if it has modules in it. * @return bool whether section was deleted */ public function delete_section($section, $forcedeleteifnotempty = false) { global $DB; if (!$this->uses_sections()) { // Not possible to delete section if sections are not used. return false; } if (!is_object($section)) { $section = $DB->get_record('course_sections', array('course' => $this->get_courseid(), 'section' => $section), 'id,section,sequence,summary'); } if (!$section || !$section->section) { // Not possible to delete 0-section. return false; } if (!$forcedeleteifnotempty && (!empty($section->sequence) || !empty($section->summary))) { return false; } $course = $this->get_course(); // Remove the marker if it points to this section. if ($section->section == $course->marker) { course_set_marker($course->id, 0); } $lastsection = $DB->get_field_sql('SELECT max(section) from {course_sections} WHERE course = ?', array($course->id)); // Find out if we need to descrease the 'numsections' property later. $courseformathasnumsections = array_key_exists('numsections', $this->get_format_options()); $decreasenumsections = $courseformathasnumsections && $section->section <= $course->numsections; // Move the section to the end. move_section_to($course, $section->section, $lastsection, true); // Delete all modules from the section. foreach (preg_split('/,/', $section->sequence, -1, PREG_SPLIT_NO_EMPTY) as $cmid) { course_delete_module($cmid); } // Delete section and it's format options. $DB->delete_records('course_format_options', array('sectionid' => $section->id)); $DB->delete_records('course_sections', array('id' => $section->id)); rebuild_course_cache($course->id, true); // Descrease 'numsections' if needed. if ($decreasenumsections) { $this->update_course_format_options(array('numsections' => $course->numsections - 1)); } return true; }
switch ($requestmethod) { case 'POST': switch ($class) { case 'section': if (!$DB->record_exists('course_sections', array('course' => $course->id, 'section' => $id))) { throw new moodle_exception('AJAX commands.php: Bad Section ID ' . $id); } switch ($field) { case 'visible': require_capability('moodle/course:sectionvisibility', $coursecontext); $resourcestotoggle = set_section_visible($course->id, $id, $value); echo json_encode(array('resourcestotoggle' => $resourcestotoggle)); break; case 'move': require_capability('moodle/course:movesections', $coursecontext); move_section_to($course, $id, $value); // See if format wants to do something about it $response = course_get_format($course)->ajax_section_move(); if ($response !== null) { echo json_encode($response); } break; } break; case 'resource': switch ($field) { case 'visible': require_capability('moodle/course:activityvisibility', $modcontext); set_coursemodule_visible($cm->id, $value); \core\event\course_module_updated::create_from_cm($cm, $modcontext)->trigger(); break;
set_user_preference('usemodchooser', $modchooser); } } if (has_capability('moodle/course:sectionvisibility', $context)) { if ($hide && confirm_sesskey()) { set_section_visible($course->id, $hide, '0'); redirect($PAGE->url); } if ($show && confirm_sesskey()) { set_section_visible($course->id, $show, '1'); redirect($PAGE->url); } } if (!empty($section) && !empty($move) && has_capability('moodle/course:movesections', $context) && confirm_sesskey()) { $destsection = $section + $move; if (move_section_to($course, $section, $destsection)) { if ($course->id == SITEID) { redirect($CFG->wwwroot . '/?redirect=0'); } else { redirect(course_get_url($course)); } } else { echo $OUTPUT->notification('An error occurred while moving a section'); } } } else { $USER->editing = 0; /** * Hide blocks * @author André Rodrigues <Math> * @date 2015/12/19
public function test_move_section_marker() { 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)); // Set course marker to the section we are going to move.. course_set_marker($course->id, 2); // Verify that the course marker is set correctly. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(2, $course->marker); // Test move the marked section down.. move_section_to($course, 2, 4); // Verify that the coruse marker has been moved along with the section.. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(4, $course->marker); // Test move the marked section up.. move_section_to($course, 4, 3); // Verify that the course marker has been moved along with the section.. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(3, $course->marker); // Test moving a non-marked section above the marked section.. move_section_to($course, 4, 2); // Verify that the course marker has been moved down to accomodate.. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(4, $course->marker); // Test moving a non-marked section below the marked section.. move_section_to($course, 3, 6); // Verify that the course marker has been up to accomodate.. $course = $DB->get_record('course', array('id' => $course->id)); $this->assertEquals(3, $course->marker); }
/** * Moves a section up or down by 1. CANNOT BE USED DIRECTLY BY AJAX! * * @param object $course course object * @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; if (!$move) { return true; } $sectiondest = $section + $move; // compartibility with course formats using field 'numsections' $courseformatoptions = course_get_format($course)->get_format_options(); if (array_key_exists('numsections', $courseformatoptions) && $sectiondest > $courseformatoptions['numsections'] or $sectiondest < 1) { return false; } $retval = move_section_to($course, $section, $sectiondest); return $retval; }
/** * Moves a section up or down by 1. CANNOT BE USED DIRECTLY BY AJAX! * * @param object $course course object * @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; if (!$move) { return true; } $sectiondest = $section + $move; if ($sectiondest > $course->numsections or $sectiondest < 1) { return false; } $retval = move_section_to($course, $section, $sectiondest); // If section moved, then rebuild course cache. if ($retval) { rebuild_course_cache($course->id, true); } return $retval; }
print_error('cantcreatesection', 'error', null, $course->fullname); return; } $pbar->update_full(5, get_string('creating_section', 'format_onetopic')); //Assign same section info $data = new stdClass(); $data->course = $sectioninfo->course; $data->section = $num_newsection; //The name is not duplicated //$data->name = $sectioninfo->name; $data->summary = $sectioninfo->summary; $data->summaryformat = $sectioninfo->summaryformat; $data->visible = $sectioninfo->visible; $data->availability = $sectioninfo->availability; $new_section_id = $DB->insert_record('course_sections', $data, true); $moved = move_section_to($course, $num_newsection, $section + 1); if ($moved) { $num_newsection = $section + 1; } $format_options = $course_format->get_format_options($section); if (is_array($format_options) && count($format_options) > 0) { $format_options['id'] = $new_section_id; $course_format->update_section_format_options($format_options); } // Trigger an event for course section update. $event = \core\event\course_section_updated::create(array('objectid' => $new_section_id, 'courseid' => $course->id, 'context' => $context, 'other' => array('sectionnum' => $num_newsection))); $event->trigger(); $pbar->update_full(10, get_string('rebuild_course_cache', 'format_onetopic')); //rebuild_course_cache($course->id, true); $new_sectioninfo = $modinfo->get_section_info($num_newsection); $modules = array();
/** * Delete a section. */ public function delete($courses) { global $DB; // Get the section identifier. $sectionident = $this->get_identifier(); if ($sectionident == 0) { throw new \moodle_exception("Cannot delete section 0."); } // We have a section number, delete that section. foreach ($courses as $course) { // Get the last section id. $lastid = $DB->get_field('course_sections', 'MAX(section)', array('course' => $course->id)); // Move this to be the last section. move_section_to($course, $sectionident, $lastid + 1, true); // Get section info, the number of sections won't have changed so we are now $lastid. $modinfo = get_fast_modinfo($course); $section = $modinfo->get_section_info($lastid); // Delete it. course_delete_section($course, $section); } }