mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 17:06:53 +02:00
Merge branch 'MDL-57476_file_callbacks' of https://github.com/gthomas2/moodle
This commit is contained in:
commit
5a842a92b3
2 changed files with 58 additions and 3 deletions
|
@ -1020,6 +1020,27 @@ class file_storage {
|
||||||
return $dir_info;
|
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.
|
* Add new local file based on existing local file.
|
||||||
*
|
*
|
||||||
|
@ -1134,7 +1155,7 @@ class file_storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$newrecord->id = $DB->insert_record('files', $newrecord);
|
$this->create_file($newrecord);
|
||||||
} catch (dml_exception $e) {
|
} catch (dml_exception $e) {
|
||||||
throw new stored_file_creation_exception($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid,
|
throw new stored_file_creation_exception($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid,
|
||||||
$newrecord->filepath, $newrecord->filename, $e->debuginfo);
|
$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);
|
$newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$newrecord->id = $DB->insert_record('files', $newrecord);
|
$this->create_file($newrecord);
|
||||||
} catch (dml_exception $e) {
|
} catch (dml_exception $e) {
|
||||||
if ($newfile) {
|
if ($newfile) {
|
||||||
$this->move_to_trash($newrecord->contenthash);
|
$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);
|
$newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$newrecord->id = $DB->insert_record('files', $newrecord);
|
$this->create_file($newrecord);
|
||||||
} catch (dml_exception $e) {
|
} catch (dml_exception $e) {
|
||||||
if ($newfile) {
|
if ($newfile) {
|
||||||
$this->move_to_trash($newrecord->contenthash);
|
$this->move_to_trash($newrecord->contenthash);
|
||||||
|
|
|
@ -139,6 +139,7 @@ class stored_file {
|
||||||
global $DB;
|
global $DB;
|
||||||
$updatereferencesneeded = false;
|
$updatereferencesneeded = false;
|
||||||
$keys = array_keys((array)$this->file_record);
|
$keys = array_keys((array)$this->file_record);
|
||||||
|
$filepreupdate = clone($this->file_record);
|
||||||
foreach ($dataobject as $field => $value) {
|
foreach ($dataobject as $field => $value) {
|
||||||
if (in_array($field, $keys)) {
|
if (in_array($field, $keys)) {
|
||||||
if ($field == 'contextid' and (!is_number($value) or $value < 1)) {
|
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.
|
// Either filesize or contenthash of this file have changed. Update all files that reference to it.
|
||||||
$this->fs->update_references_to_storedfile($this);
|
$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));
|
$DB->delete_records('files', array('id'=>$this->file_record->id));
|
||||||
|
|
||||||
$transaction->allow_commit();
|
$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.
|
// Move pool file to trash if content not needed any more.
|
||||||
|
@ -815,9 +838,20 @@ class stored_file {
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function set_sortorder($sortorder) {
|
public function set_sortorder($sortorder) {
|
||||||
|
$oldorder = $this->file_record->sortorder;
|
||||||
$filerecord = new stdClass;
|
$filerecord = new stdClass;
|
||||||
$filerecord->sortorder = $sortorder;
|
$filerecord->sortorder = $sortorder;
|
||||||
$this->update($filerecord);
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue