/** * Make sure up-to-date events are created for all Dataform instances * * This standard function will check all instances of this module * and make sure there are up-to-date events created for each of them. * If courseid = 0, then every instance event in the site is checked, else * only instance events belonging to the course specified are checked. * This function is used, in its new format, by restore_refresh_events() * * @param $courseid int optional If zero then all module instances for all courses are covered * @return boolean Always returns true */ function dataform_refresh_events($courseid = 0) { global $DB; if ($courseid == 0) { if (!($dataforms = $DB->get_records('dataform'))) { return true; } } else { if (!($dataforms = $DB->get_records('dataform', array('course' => $courseid)))) { return true; } } $moduleid = $DB->get_field('modules', 'id', array('name' => 'dataform')); foreach ($dataforms as $data) { $data->module = $moduleid; $cm = get_coursemodule_from_instance('dataform', $data->id, $courseid, false, MUST_EXIST); $data->coursemodule = $cm->id; \mod_dataform\helper\calendar_event::update_event_timeavailable($data); \mod_dataform\helper\calendar_event::update_event_timedue($data); } return true; }
/** * 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; }