Skip to content
Permalink
Browse files

MDL-57476 file: Add CUD callbacks

  • Loading branch information...
gthomas2
gthomas2 committed Jun 22, 2017
1 parent 350700b commit 023037dc9e6748897b63e3babf6e4469e4b94163
Showing with 58 additions and 3 deletions.
  1. +24 −3 lib/filestorage/file_storage.php
  2. +34 −0 lib/filestorage/stored_file.php
@@ -1020,6 +1020,27 @@ public function create_directory($contextid, $component, $filearea, $itemid, $fi
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 @@ public function create_file_from_storedfile($filerecord, $fileorid) {
}
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 @@ public function create_file_from_pathname($filerecord, $pathname) {
$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 @@ public function create_file_from_string($filerecord, $content) {
$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);
@@ -139,6 +139,7 @@ protected function update($dataobject) {
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 @@ protected function update($dataobject) {
// 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 @@ public function delete() {
$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 @@ public function get_sortorder() {
* @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);
}
}
}
}
}
/**

0 comments on commit 023037d

Please sign in to comment.
You can’t perform that action at this time.