Exemplo n.º 1
0
/**
 * This method will delete a course section and may delete all modules inside it.
 *
 * No permissions are checked here, use {@link course_can_delete_section()} to
 * check if section can actually be deleted.
 *
 * @param int|stdClass $course
 * @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
 */
function course_delete_section($course, $section, $forcedeleteifnotempty = true)
{
    global $DB;
    // Prepare variables.
    $courseid = is_object($course) ? $course->id : (int) $course;
    $sectionnum = is_object($section) ? $section->section : (int) $section;
    $section = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $sectionnum));
    if (!$section) {
        // No section exists, can't proceed.
        return false;
    }
    $format = course_get_format($course);
    $sectionname = $format->get_section_name($section);
    // Delete section.
    $result = $format->delete_section($section, $forcedeleteifnotempty);
    // Trigger an event for course section deletion.
    if ($result) {
        $context = context_course::instance($courseid);
        $event = \core\event\course_section_deleted::create(array('objectid' => $section->id, 'courseid' => $courseid, 'context' => $context, 'other' => array('sectionnum' => $section->section, 'sectionname' => $sectionname)));
        $event->add_record_snapshot('course_sections', $section);
        $event->trigger();
    }
    return $result;
}
Exemplo n.º 2
0
/**
 * Course section deletion, using an adhoc task for deletion of the modules it contains.
 * 1. Schedule all modules within the section for adhoc removal.
 * 2. Move all modules to course section 0.
 * 3. Delete the resulting empty section.
 *
 * @param \stdClass $section the section to schedule for deletion.
 * @param bool $forcedeleteifnotempty whether to force section deletion if it contains modules.
 * @return bool true if the section was scheduled for deletion, false otherwise.
 */
function course_delete_section_async($section, $forcedeleteifnotempty = true)
{
    global $DB, $USER;
    // Objects only, and only valid ones.
    if (!is_object($section) || empty($section->id)) {
        return false;
    }
    // Does the object currently exist in the DB for removal (check for stale objects).
    $section = $DB->get_record('course_sections', array('id' => $section->id));
    if (!$section || !$section->section) {
        // No section exists, or the section is 0. Can't proceed.
        return false;
    }
    // Check whether the section can be removed.
    if (!$forcedeleteifnotempty && (!empty($section->sequence) || !empty($section->summary))) {
        return false;
    }
    $format = course_get_format($section->course);
    $sectionname = $format->get_section_name($section);
    // Flag those modules having no existing deletion flag. Some modules may have been scheduled for deletion manually, and we don't
    // want to create additional adhoc deletion tasks for these. Moving them to section 0 will suffice.
    $affectedmods = $DB->get_records_select('course_modules', 'course = ? AND section = ? AND deletioninprogress <> ?', [$section->course, $section->id, 1], '', 'id');
    $DB->set_field('course_modules', 'deletioninprogress', '1', ['course' => $section->course, 'section' => $section->id]);
    // Move all modules to section 0.
    $modules = $DB->get_records('course_modules', ['section' => $section->id], '');
    $sectionzero = $DB->get_record('course_sections', ['course' => $section->course, 'section' => '0']);
    foreach ($modules as $mod) {
        moveto_module($mod, $sectionzero);
    }
    // Create and queue an adhoc task for the deletion of the modules.
    $removaltask = new \core_course\task\course_delete_modules();
    $data = array('cms' => $affectedmods, 'userid' => $USER->id, 'realuserid' => \core\session\manager::get_realuser()->id);
    $removaltask->set_custom_data($data);
    \core\task\manager::queue_adhoc_task($removaltask);
    // Delete the now empty section, passing in only the section number, which forces the function to fetch a new object.
    // The refresh is needed because the section->sequence is now stale.
    $result = $format->delete_section($section->section, $forcedeleteifnotempty);
    // Trigger an event for course section deletion.
    if ($result) {
        $context = \context_course::instance($section->course);
        $event = \core\event\course_section_deleted::create(array('objectid' => $section->id, 'courseid' => $section->course, 'context' => $context, 'other' => array('sectionnum' => $section->section, 'sectionname' => $sectionname)));
        $event->add_record_snapshot('course_sections', $section);
        $event->trigger();
    }
    rebuild_course_cache($section->course, true);
    return $result;
}