Ejemplo n.º 1
0
/**
 * Update the module info.
 * This function doesn't check the user capabilities. It updates the course module and the module instance.
 * Then execute common action to create/update module process (trigger event, rebuild cache, save plagiarism settings...).
 *
 * @param object $cm course module
 * @param object $moduleinfo module info
 * @param object $course course of the module
 * @param object $mform - the mform is required by some specific module in the function MODULE_update_instance(). This is due to a hack in this function.
 * @return array list of course module and module info.
 */
function update_moduleinfo($cm, $moduleinfo, $course, $mform = null)
{
    global $DB, $CFG;
    // Attempt to include module library before we make any changes to DB.
    include_modulelib($moduleinfo->modulename);
    $moduleinfo->course = $course->id;
    $moduleinfo = set_moduleinfo_defaults($moduleinfo);
    if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
        $moduleinfo->groupmode = $cm->groupmode;
        // Keep original.
    }
    // Update course module first.
    $cm->groupmode = $moduleinfo->groupmode;
    if (isset($moduleinfo->groupingid)) {
        $cm->groupingid = $moduleinfo->groupingid;
    }
    if (isset($moduleinfo->groupmembersonly)) {
        $cm->groupmembersonly = $moduleinfo->groupmembersonly;
    }
    $completion = new completion_info($course);
    if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
        // Update completion settings.
        $cm->completion = $moduleinfo->completion;
        $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
        $cm->completionview = $moduleinfo->completionview;
        $cm->completionexpected = $moduleinfo->completionexpected;
    }
    if (!empty($CFG->enableavailability)) {
        $cm->availablefrom = $moduleinfo->availablefrom;
        $cm->availableuntil = $moduleinfo->availableuntil;
        $cm->showavailability = $moduleinfo->showavailability;
        condition_info::update_cm_from_form($cm, $moduleinfo, true);
    }
    if (isset($moduleinfo->showdescription)) {
        $cm->showdescription = $moduleinfo->showdescription;
    } else {
        $cm->showdescription = 0;
    }
    $DB->update_record('course_modules', $cm);
    $modcontext = context_module::instance($moduleinfo->coursemodule);
    // Update embedded links and save files.
    if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
        $moduleinfo->intro = file_save_draft_area_files($moduleinfo->introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $moduleinfo->introeditor['text']);
        $moduleinfo->introformat = $moduleinfo->introeditor['format'];
        unset($moduleinfo->introeditor);
    }
    $updateinstancefunction = $moduleinfo->modulename . "_update_instance";
    if (!$updateinstancefunction($moduleinfo, $mform)) {
        print_error('cannotupdatemod', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
    }
    // Make sure visibility is set correctly (in particular in calendar).
    if (has_capability('moodle/course:activityvisibility', $modcontext)) {
        set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
    }
    if (isset($moduleinfo->cmidnumber)) {
        // Label.
        // Set cm idnumber - uniqueness is already verified by form validation.
        set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
    }
    // Now that module is fully updated, also update completion data if required.
    // (this will wipe all user completion data and recalculate it)
    if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) {
        $completion->reset_all_state($cm);
    }
    // Trigger event based on the action we did.
    $event = \core\event\course_module_updated::create(array('courseid' => $course->id, 'context' => $modcontext, 'objectid' => $moduleinfo->coursemodule, 'other' => array('modulename' => $moduleinfo->modulename, 'name' => $moduleinfo->name, 'instanceid' => $moduleinfo->instance)));
    $event->trigger();
    add_to_log($course->id, $moduleinfo->modulename, "update", "view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->instance}", $moduleinfo->coursemodule);
    $moduleinfo = edit_module_post_actions($moduleinfo, $course);
    return array($cm, $moduleinfo);
}
Ejemplo n.º 2
0
 /**
  * Tests for event validations related to course module update.
  */
 public function test_course_module_updated_event_exceptions()
 {
     $this->resetAfterTest();
     // Generate data.
     $modinfo = $this->create_specific_module_test('assign');
     $context = context_module::instance($modinfo->coursemodule);
     // Test not setting instanceid.
     try {
         $event = \core\event\course_module_updated::create(array('courseid' => $modinfo->course, 'context' => $context, 'objectid' => $modinfo->coursemodule, 'other' => array('modulename' => 'assign', 'name' => 'My assignment')));
         $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without\n                    other['instanceid']");
     } catch (coding_exception $e) {
         $this->assertContains("The 'instanceid' value must be set in other.", $e->getMessage());
     }
     // Test not setting modulename.
     try {
         $event = \core\event\course_module_updated::create(array('courseid' => $modinfo->course, 'context' => $context, 'objectid' => $modinfo->coursemodule, 'other' => array('instanceid' => $modinfo->instance, 'name' => 'My assignment')));
         $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without\n                    other['modulename']");
     } catch (coding_exception $e) {
         $this->assertContains("The 'modulename' value must be set in other.", $e->getMessage());
     }
     // Test not setting name.
     try {
         $event = \core\event\course_module_updated::create(array('courseid' => $modinfo->course, 'context' => $context, 'objectid' => $modinfo->coursemodule, 'other' => array('modulename' => 'assign', 'instanceid' => $modinfo->instance)));
         $this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without\n                    other['name']");
     } catch (coding_exception $e) {
         $this->assertContains("The 'name' value must be set in other.", $e->getMessage());
     }
 }
Ejemplo n.º 3
0
 /**
  * Updates Dataform settings.
  *
  * @return bool
  */
 public function update($params, $notify = '')
 {
     global $CFG, $DB, $COURSE;
     $updatedf = false;
     $params = (object) $params;
     $data = $this->data;
     $data->activityicon = 0;
     // Check for record properties update.
     foreach ($params as $key => $value) {
         if (!property_exists($data, $key)) {
             continue;
         }
         if ($value != $data->{$key}) {
             $data->{$key} = $value;
             $updatedf = true;
         }
     }
     // Grade update.
     if (!$data->grade) {
         if ($data->gradeitems) {
             $data->gradeitems = null;
             $updatedf = true;
         }
     }
     // Max entries.
     $cfgmaxentries = $CFG->dataform_maxentries;
     if ($cfgmaxentries == 0) {
         $data->maxentries = 0;
     } else {
         if ($cfgmaxentries > 0) {
             if ($data->maxentries > $cfgmaxentries or $data->maxentries < 0) {
                 $data->maxentries = $cfgmaxentries;
             }
         }
     }
     if ($data->maxentries < -1) {
         $data->maxentries = -1;
     }
     if (!$updatedf) {
         return true;
     }
     $data->timemodified = time();
     if (!$DB->update_record('dataform', $data)) {
         // Something went wrong at updating.
         if ($notify === true) {
             $note = array('dfupdatefailed' => get_string('dfupdatefailed', 'dataform'));
             $this->notifications = array('problem' => $note);
         } else {
             if ($notify) {
                 $this->notifications = array('problem' => array('' => $notify));
             }
         }
         return false;
     }
     $this->_data = $data;
     // Calendar.
     $data->coursemodule = $this->cm->id;
     $data->module = $this->cm->module;
     \mod_dataform\helper\calendar_event::update_event_timeavailable($data);
     \mod_dataform\helper\calendar_event::update_event_timedue($data);
     // Activity icon.
     if (!empty($data->activityicon)) {
         $options = array('subdirs' => 0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1, 'accepted_types' => array('image'));
         file_save_draft_area_files($data->activityicon, $this->context->id, 'mod_dataform', 'activityicon', 0, $options);
         $updatedf = true;
     }
     // Grading.
     $grademan = $this->grade_manager;
     if (!$data->grade) {
         $grademan->delete_grade_items();
     } else {
         $gradedata = array('grade' => $data->grade);
         // Update name if there is only one grade item.
         if (!($gradeitems = $grademan->grade_items) or count($gradeitems) < 2) {
             $gradedata['name'] = $this->name;
         }
         $itemparams = $grademan->get_grade_item_params_from_data($gradedata);
         $this->grade_manager->update_grade_item(0, $itemparams);
     }
     // Trigger event.
     $eventparams = array('objectid' => $this->cm->id, 'context' => $this->context, 'other' => array('modulename' => 'dataform', 'name' => $this->name, 'instanceid' => $this->id));
     $event = \core\event\course_module_updated::create($eventparams);
     $event->add_record_snapshot('course_modules', $this->cm);
     $event->add_record_snapshot('course', $this->course);
     $event->add_record_snapshot('dataform', $data);
     $event->trigger();
     // Process notification message.
     if ($notify and $notify !== true) {
         $this->notifications = array('success' => array('' => $notify));
     }
     return true;
 }