MDL-57924 mod_data: Move update record code to new function

This commit is contained in:
Juan Leyva 2017-02-14 10:11:43 +01:00 committed by Eloy Lafuente (stronk7)
parent 61c640c13a
commit df0c549f88
2 changed files with 41 additions and 28 deletions

View file

@ -1226,3 +1226,43 @@ function data_add_fields_contents_to_new_record($data, $context, $recordid, $fie
$event->add_record_snapshot('data', $data);
$event->trigger();
}
/**
* Updates the fields contents of an existing record.
*
* @param stdClass $data database object
* @param stdClass $record record to update object
* @param stdClass $context context object
* @param stdClass $datarecord the submitted data
* @param stdClass $processeddata pre-processed submitted fields
* @since Moodle 3.3
*/
function data_update_record_fields_contents($data, $record, $context, $datarecord, $processeddata) {
global $DB;
// Reset the approved flag after edit if the user does not have permission to approve their own entries.
if (!has_capability('mod/data:approve', $context)) {
$record->approved = 0;
}
// Update the parent record.
$record->timemodified = time();
$DB->update_record('data_records', $record);
// Update all content.
foreach ($processeddata->fields as $fieldname => $field) {
$field->update_content($record->id, $datarecord->$fieldname, $fieldname);
}
// Trigger an event for updating this record.
$event = \mod_data\event\record_updated::create(array(
'objectid' => $record->id,
'context' => $context,
'courseid' => $data->course,
'other' => array(
'dataid' => $data->id
)
));
$event->add_record_snapshot('data', $data);
$event->trigger();
}