Example #1
0
/**
 * Updates the course section
 *
 * This function does not check permissions or clean values - this has to be done prior to calling it.
 *
 * @param int|stdClass $course
 * @param stdClass $section record from course_sections table - it will be updated with the new values
 * @param array|stdClass $data
 */
function course_update_section($course, $section, $data)
{
    global $DB;
    $courseid = is_object($course) ? $course->id : (int) $course;
    // Some fields can not be updated using this method.
    $data = array_diff_key((array) $data, array('id', 'course', 'section', 'sequence'));
    $changevisibility = array_key_exists('visible', $data) && (bool) $data['visible'] != (bool) $section->visible;
    if (array_key_exists('name', $data) && \core_text::strlen($data['name']) > 255) {
        throw new moodle_exception('maximumchars', 'moodle', '', 255);
    }
    // Update record in the DB and course format options.
    $data['id'] = $section->id;
    $DB->update_record('course_sections', $data);
    rebuild_course_cache($courseid, true);
    course_get_format($courseid)->update_section_format_options($data);
    // Update fields of the $section object.
    foreach ($data as $key => $value) {
        if (property_exists($section, $key)) {
            $section->{$key} = $value;
        }
    }
    // Trigger an event for course section update.
    $event = \core\event\course_section_updated::create(array('objectid' => $section->id, 'courseid' => $courseid, 'context' => context_course::instance($courseid), 'other' => array('sectionnum' => $section->section)));
    $event->trigger();
    // If section visibility was changed, hide the modules in this section too.
    if ($changevisibility && !empty($section->sequence)) {
        $modules = explode(',', $section->sequence);
        foreach ($modules as $moduleid) {
            if ($cm = get_coursemodule_from_id(null, $moduleid, $courseid)) {
                if ($data['visible']) {
                    // As we unhide the section, we use the previously saved visibility stored in visibleold.
                    set_coursemodule_visible($moduleid, $cm->visibleold);
                } else {
                    // We hide the section, so we hide the module but we store the original state in visibleold.
                    set_coursemodule_visible($moduleid, 0);
                    $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id' => $moduleid));
                }
                \core\event\course_module_updated::create_from_cm($cm)->trigger();
            }
        }
    }
}
Example #2
0
/**
 * For a given course section, marks it visible or hidden,
 * and does the same for every activity in that section
 *
 * @param int $courseid course id
 * @param int $sectionnumber The section number to adjust
 * @param int $visibility The new visibility
 * @return array A list of resources which were hidden in the section
 */
function set_section_visible($courseid, $sectionnumber, $visibility)
{
    global $DB;
    $resourcestotoggle = array();
    if ($section = $DB->get_record("course_sections", array("course" => $courseid, "section" => $sectionnumber))) {
        $DB->set_field("course_sections", "visible", "{$visibility}", array("id" => $section->id));
        $event = \core\event\course_section_updated::create(array('context' => context_course::instance($courseid), 'objectid' => $section->id, 'other' => array('sectionnum' => $sectionnumber)));
        $event->add_record_snapshot('course_sections', $section);
        $event->trigger();
        if (!empty($section->sequence)) {
            $modules = explode(",", $section->sequence);
            foreach ($modules as $moduleid) {
                if ($cm = get_coursemodule_from_id(null, $moduleid, $courseid)) {
                    if ($visibility) {
                        // As we unhide the section, we use the previously saved visibility stored in visibleold.
                        set_coursemodule_visible($moduleid, $cm->visibleold);
                    } else {
                        // We hide the section, so we hide the module but we store the original state in visibleold.
                        set_coursemodule_visible($moduleid, 0);
                        $DB->set_field('course_modules', 'visibleold', $cm->visible, array('id' => $moduleid));
                    }
                    \core\event\course_module_updated::create_from_cm($cm)->trigger();
                }
            }
        }
        rebuild_course_cache($courseid, true);
        // Determine which modules are visible for AJAX update
        if (!empty($modules)) {
            list($insql, $params) = $DB->get_in_or_equal($modules);
            $select = 'id ' . $insql . ' AND visible = ?';
            array_push($params, $visibility);
            if (!$visibility) {
                $select .= ' AND visibleold = 1';
            }
            $resourcestotoggle = $DB->get_fieldset_select('course_modules', 'id', $select, $params);
        }
    }
    return $resourcestotoggle;
}
Example #3
0
 /**
  * Test that triggering a course_section_updated event works as expected.
  */
 public function test_course_section_updated_event()
 {
     global $DB;
     $this->resetAfterTest();
     // Create the course with sections.
     $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
     $sections = $DB->get_records('course_sections', array('course' => $course->id));
     $coursecontext = context_course::instance($course->id);
     $section = array_pop($sections);
     $section->name = 'Test section';
     $section->summary = 'Test section summary';
     $DB->update_record('course_sections', $section);
     // Trigger an event for course section update.
     $event = \core\event\course_section_updated::create(array('objectid' => $section->id, 'courseid' => $course->id, 'context' => context_course::instance($course->id), 'other' => array('sectionnum' => $section->section)));
     $event->add_record_snapshot('course_sections', $section);
     // Trigger and catch event.
     $sink = $this->redirectEvents();
     $event->trigger();
     $events = $sink->get_events();
     $sink->close();
     // Validate the event.
     $event = $events[0];
     $this->assertInstanceOf('\\core\\event\\course_section_updated', $event);
     $this->assertEquals('course_sections', $event->objecttable);
     $this->assertEquals($section->id, $event->objectid);
     $this->assertEquals($course->id, $event->courseid);
     $this->assertEquals($coursecontext->id, $event->contextid);
     $this->assertEquals($section->section, $event->other['sectionnum']);
     $expecteddesc = "The user with id '{$event->userid}' updated section number '{$event->other['sectionnum']}' for the course with id '{$event->courseid}'";
     $this->assertEquals($expecteddesc, $event->get_description());
     $url = new moodle_url('/course/editsection.php', array('id' => $event->objectid));
     $this->assertEquals($url, $event->get_url());
     $this->assertEquals($section, $event->get_record_snapshot('course_sections', $event->objectid));
     $id = $section->id;
     $sectionnum = $section->section;
     $expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
     $this->assertEventLegacyLogData($expectedlegacydata, $event);
     $this->assertEventContextNotUsed($event);
 }
                $data->availability = null;
            }
        }
        $DB->update_record('course_sections', $data);
        rebuild_course_cache($course->id, true);
        if (isset($data->section)) {
            // Usually edit form does not change relative section number but just in case.
            $sectionnum = $data->section;
        }
        course_get_format($course->id)->update_section_format_options($data);
        // Set section info, as this might not be present in form_data.
        if (!isset($data->section)) {
            $data->section = $sectionnum;
        }
        // Trigger an event for course section update.
        $event = \core\event\course_section_updated::create(array('objectid' => $data->id, 'courseid' => $course->id, 'context' => $context, 'other' => array('sectionnum' => $data->section)));
        $event->trigger();
        $PAGE->navigation->clear_cache();
        redirect(course_get_url($course, $section, array('sr' => $sectionreturn)));
    }
}
// The edit form is displayed for the first time or if there was validation error on the previous step.
$sectionname = get_section_name($course, $sectionnum);
$stredit = get_string('edita', '', " {$sectionname}");
$strsummaryof = get_string('summaryof', '', " {$sectionname}");
$PAGE->set_title($stredit);
$PAGE->set_heading($course->fullname);
$PAGE->navbar->add($stredit);
echo $OUTPUT->header();
echo $OUTPUT->heading($strsummaryof);
$mform->display();