Merge branch 'MDL-57476_file_callbacks' of https://github.com/gthomas2/moodle

This commit is contained in:
Andrew Nicols 2017-07-17 10:31:28 +08:00
commit 5a842a92b3
2 changed files with 58 additions and 3 deletions

View File

@ -1020,6 +1020,27 @@ class file_storage {
return $dir_info;
}
/**
* Add new file record to database and handle callbacks.
*
* @param stdClass $newrecord
*/
protected function create_file($newrecord) {
global $DB;
$newrecord->id = $DB->insert_record('files', $newrecord);
if ($newrecord->filename !== '.') {
// Callback for file created.
if ($pluginsfunction = get_plugins_with_function('after_file_created')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($newrecord);
}
}
}
}
}
/**
* Add new local file based on existing local file.
*
@ -1134,7 +1155,7 @@ class file_storage {
}
try {
$newrecord->id = $DB->insert_record('files', $newrecord);
$this->create_file($newrecord);
} catch (dml_exception $e) {
throw new stored_file_creation_exception($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid,
$newrecord->filepath, $newrecord->filename, $e->debuginfo);
@ -1302,7 +1323,7 @@ class file_storage {
$newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);
try {
$newrecord->id = $DB->insert_record('files', $newrecord);
$this->create_file($newrecord);
} catch (dml_exception $e) {
if ($newfile) {
$this->move_to_trash($newrecord->contenthash);
@ -1421,7 +1442,7 @@ class file_storage {
$newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);
try {
$newrecord->id = $DB->insert_record('files', $newrecord);
$this->create_file($newrecord);
} catch (dml_exception $e) {
if ($newfile) {
$this->move_to_trash($newrecord->contenthash);

View File

@ -139,6 +139,7 @@ class stored_file {
global $DB;
$updatereferencesneeded = false;
$keys = array_keys((array)$this->file_record);
$filepreupdate = clone($this->file_record);
foreach ($dataobject as $field => $value) {
if (in_array($field, $keys)) {
if ($field == 'contextid' and (!is_number($value) or $value < 1)) {
@ -216,6 +217,17 @@ class stored_file {
// Either filesize or contenthash of this file have changed. Update all files that reference to it.
$this->fs->update_references_to_storedfile($this);
}
// Callback for file update.
if (!$this->is_directory()) {
if ($pluginsfunction = get_plugins_with_function('after_file_updated')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($this->file_record, $filepreupdate);
}
}
}
}
}
/**
@ -375,6 +387,17 @@ class stored_file {
$DB->delete_records('files', array('id'=>$this->file_record->id));
$transaction->allow_commit();
if (!$this->is_directory()) {
// Callback for file deletion.
if ($pluginsfunction = get_plugins_with_function('after_file_deleted')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($this->file_record);
}
}
}
}
}
// Move pool file to trash if content not needed any more.
@ -815,9 +838,20 @@ class stored_file {
* @return int
*/
public function set_sortorder($sortorder) {
$oldorder = $this->file_record->sortorder;
$filerecord = new stdClass;
$filerecord->sortorder = $sortorder;
$this->update($filerecord);
if (!$this->is_directory()) {
// Callback for file sort order change.
if ($pluginsfunction = get_plugins_with_function('after_file_sorted')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($this->file_record, $oldorder, $sortorder);
}
}
}
}
}
/**