From d60ebf4bf475280d9a97ef1058c2b64527045efa Mon Sep 17 00:00:00 2001 From: Daniel Thies Date: Thu, 11 Apr 2024 19:41:17 -0500 Subject: [PATCH] MED-101: Add remove resource functionality --- classes/event/resource_created.php | 2 +- classes/event/resource_deleted.php | 87 ++++++++++++++++++ classes/event/resource_updated.php | 88 +++++++++++++++++++ classes/form/delete_resource.php | 51 +++++++++++ classes/media_manager.php | 2 + index.php | 3 + lang/en/tool_mediatime.php | 3 + .../classes/event/resource_deleted.php | 38 ++++++++ .../classes/event/resource_updated.php | 38 ++++++++ .../streamio/classes/form/delete_resource.php | 66 ++++++++++++++ source/streamio/classes/manager.php | 46 +++++++++- .../lang/en/mediatimesrc_streamio.php | 10 ++- .../templates/media_resource.mustache | 1 + .../classes/event/resource_deleted.php | 38 ++++++++ .../classes/event/resource_updated.php | 38 ++++++++ .../classes/form/delete_resource.php | 58 ++++++++++++ source/videotime/classes/manager.php | 45 +++++++++- .../lang/en/mediatimesrc_videotime.php | 4 +- .../templates/media_resource.mustache | 1 + templates/media_manager.mustache | 1 + templates/media_resource.mustache | 1 + 21 files changed, 612 insertions(+), 9 deletions(-) create mode 100644 classes/event/resource_deleted.php create mode 100644 classes/event/resource_updated.php create mode 100644 classes/form/delete_resource.php create mode 100644 source/streamio/classes/event/resource_deleted.php create mode 100644 source/streamio/classes/event/resource_updated.php create mode 100644 source/streamio/classes/form/delete_resource.php create mode 100644 source/videotime/classes/event/resource_deleted.php create mode 100644 source/videotime/classes/event/resource_updated.php create mode 100644 source/videotime/classes/form/delete_resource.php diff --git a/classes/event/resource_created.php b/classes/event/resource_created.php index 42ea813..0c90a6e 100644 --- a/classes/event/resource_created.php +++ b/classes/event/resource_created.php @@ -74,7 +74,7 @@ public function get_url() { * @return array */ public static function get_objectid_mapping() { - return array('db' => 'tool_mediatime', 'restore' => \core\event\base::NOT_MAPPED); + return ['db' => 'tool_mediatime', 'restore' => \core\event\base::NOT_MAPPED]; } /** diff --git a/classes/event/resource_deleted.php b/classes/event/resource_deleted.php new file mode 100644 index 0000000..e9d67a4 --- /dev/null +++ b/classes/event/resource_deleted.php @@ -0,0 +1,87 @@ +. + +namespace tool_mediatime\event; + +use core\event\base; + +/** + * The resource_deleted event class. + * + * @package tool_mediatime + * @category event + * @copyright 2024 bdecent gmbh + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class resource_deleted extends base { + + // For more information about the Events API please visit {@link https://docs.moodle.org/dev/Events_API}. + + /** + * Initialise the event. + */ + protected function init() { + $this->data['objecttable'] = 'tool_mediatime'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->data['crud'] = 'd'; + } + + /** + * Returns event name. + * + * @return string + */ + public static function get_name() { + return get_string('event_resource_deleted', 'tool_mediatime'); + } + + /** + * Get the event description. + * + * @return string + */ + public function get_description() { + return "The user with id '{$this->userid}' deleted a mediatime resource with id '{$this->objectid}'."; + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/admin/tool/mediatime/index.php', [ + ]); + } + + /** + * Get the object ID mapping. + * + * @return array + */ + public static function get_objectid_mapping() { + return ['db' => 'tool_mediatime', 'restore' => \core\event\base::NOT_MAPPED]; + } + + /** + * No mapping required for this event because this event is not backed up. + * + * @return bool + */ + public static function get_other_mapping() { + return false; + } +} diff --git a/classes/event/resource_updated.php b/classes/event/resource_updated.php new file mode 100644 index 0000000..19bcb56 --- /dev/null +++ b/classes/event/resource_updated.php @@ -0,0 +1,88 @@ +. + +namespace tool_mediatime\event; + +use core\event\base; + +/** + * The resource_updated event class. + * + * @package tool_mediatime + * @category event + * @copyright 2024 bdecent gmbh + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class resource_updated extends base { + + // For more information about the Events API please visit {@link https://docs.moodle.org/dev/Events_API}. + + /** + * Initialise the event. + */ + protected function init() { + $this->data['objecttable'] = 'tool_mediatime'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->data['crud'] = 'u'; + } + + /** + * Returns event name. + * + * @return string + */ + public static function get_name() { + return get_string('event_resource_updated', 'tool_mediatime'); + } + + /** + * Get the event description. + * + * @return string + */ + public function get_description() { + return "The user with id '{$this->userid}' deleted a mediatime resource with id '{$this->objectid}'."; + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/admin/tool/mediatime/index.php', [ + 'id' => $this->objectid, + ]); + } + + /** + * Get the object ID mapping. + * + * @return array + */ + public static function get_objectid_mapping() { + return ['db' => 'tool_mediatime', 'restore' => \core\event\base::NOT_MAPPED]; + } + + /** + * No mapping required for this event because this event is not backed up. + * + * @return bool + */ + public static function get_other_mapping() { + return false; + } +} diff --git a/classes/form/delete_resource.php b/classes/form/delete_resource.php new file mode 100644 index 0000000..2075edc --- /dev/null +++ b/classes/form/delete_resource.php @@ -0,0 +1,51 @@ +. + +/** + * Media Time media edit form + * + * @package tool_mediatime + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_mediatime\form; + +use context_system; +use moodleform; + +/** + * Media Time media edit form + * + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class delete_resource extends moodleform { + + /** + * Definition + */ + public function definition() { + $mform = $this->_form; + require_capability('tool/mediatime:manage', context_system::instance()); + + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); + + $mform->addElement('hidden', 'source'); + $mform->setType('source', PARAM_TEXT); + } +} diff --git a/classes/media_manager.php b/classes/media_manager.php index 3f16ae9..2cd7f0c 100644 --- a/classes/media_manager.php +++ b/classes/media_manager.php @@ -130,11 +130,13 @@ public function export_for_template(renderer_base $output): array { foreach ($this->media as $record) { $resource = new output\media_resource($record); $url = new moodle_url('/admin/tool/mediatime/index.php', ['id' => $record->id]); + $removeurl = new moodle_url('/admin/tool/mediatime/index.php', ['delete' => $record->id]); $media[] = [ 'imageurl' => $resource->image_url($output), 'tags' => $resource->tags($output), 'url' => $url->out(), 'content' => $record->content, + 'removeurl' => $removeurl->out(), ]; } diff --git a/index.php b/index.php index 85d4041..bf05b4d 100644 --- a/index.php +++ b/index.php @@ -31,6 +31,7 @@ $source = optional_param('source', '', PARAM_ALPHANUMEXT); $id = optional_param('id', null, PARAM_INT); +$delete = optional_param('delete', null, PARAM_INT); $edit = optional_param('edit', null, PARAM_INT); admin_externalpage_setup('mediatimelibrary'); @@ -38,6 +39,8 @@ $PAGE->set_heading(get_string('pluginname', 'tool_mediatime')); if ($id) { $record = $DB->get_record('tool_mediatime', ['id' => $id]); +} else if ($delete) { + $record = $DB->get_record('tool_mediatime', ['id' => $delete]); } else if ($edit) { $record = $DB->get_record('tool_mediatime', ['id' => $edit]); } else { diff --git a/lang/en/tool_mediatime.php b/lang/en/tool_mediatime.php index ff6c42a..041ab61 100644 --- a/lang/en/tool_mediatime.php +++ b/lang/en/tool_mediatime.php @@ -28,6 +28,9 @@ $string['pluginname'] = 'Media Time'; $string['addnewcontent'] = 'Add new content'; $string['event_resource_created'] = 'Resource created'; +$string['event_resource_deleted'] = 'Resource deleted'; +$string['event_resource_updated'] = 'Resource updated'; +$string['filelinks'] = 'File links'; $string['library'] = 'Library'; $string['keyword'] = 'Keyword'; $string['mediatime:create'] = 'Create media in library'; diff --git a/source/streamio/classes/event/resource_deleted.php b/source/streamio/classes/event/resource_deleted.php new file mode 100644 index 0000000..132d9aa --- /dev/null +++ b/source/streamio/classes/event/resource_deleted.php @@ -0,0 +1,38 @@ +. + +namespace mediatimesrc_streamio\event; + +use core\event\base; + +/** + * The resource_deleted event class. + * + * @package mediatimesrc_streamio + * @category event + * @copyright 2024 bdecent gmbh + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class resource_deleted extends \tool_mediatime\event\resource_deleted { + /** + * Returns event name. + * + * @return string + */ + public static function get_name() { + return get_string('event_resource_deleted', 'mediatimesrc_streamio'); + } +} diff --git a/source/streamio/classes/event/resource_updated.php b/source/streamio/classes/event/resource_updated.php new file mode 100644 index 0000000..8f8e640 --- /dev/null +++ b/source/streamio/classes/event/resource_updated.php @@ -0,0 +1,38 @@ +. + +namespace mediatimesrc_streamio\event; + +use core\event\base; + +/** + * The resource_updated event class. + * + * @package mediatimesrc_streamio + * @category event + * @copyright 2024 bdecent gmbh + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class resource_updated extends \tool_mediatime\event\resource_updated { + /** + * Returns event name. + * + * @return string + */ + public static function get_name() { + return get_string('event_resource_updated', 'mediatimesrc_streamio'); + } +} diff --git a/source/streamio/classes/form/delete_resource.php b/source/streamio/classes/form/delete_resource.php new file mode 100644 index 0000000..64fbfab --- /dev/null +++ b/source/streamio/classes/form/delete_resource.php @@ -0,0 +1,66 @@ +. + +/** + * Media Time media edit form + * + * @package mediatimesrc_streamio + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mediatimesrc_streamio\form; + +use context_system; +use moodleform; +use mediatimesrc_streamio\api; +use mediatimesrc_streamio\output\media_resource; + +/** + * Media Time media edit form + * + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class delete_resource extends \tool_mediatime\form\delete_resource { + + /** + * Definition + */ + public function definition() { + $mform = $this->_form; + + $mform->addElement('hidden', 'source'); + $mform->setType('source', PARAM_TEXT); + $mform->setDefault('source', 'streamio'); + + $mform->addElement('static', 'name', get_string('resourcename', 'tool_mediatime'), $this->_customdata['name']); + $mform->setType('name', PARAM_TEXT); + + $mform->addElement('hidden', 'delete'); + $mform->setType('delete', PARAM_INT); + + $action = [ + $mform->createElement('radio', 'action', '', get_string('keepstreamiofiles', 'mediatimesrc_streamio'), 1, []), + $mform->createElement('radio', 'action', '', get_string('removestreamiofiles', 'mediatimesrc_streamio'), 2, []), + ]; + $mform->addGroup($action, 'streamiofileaction', get_string('streamiofileaction', 'mediatimesrc_streamio'), [' '], false); + $mform->setType('action', PARAM_INT); + $mform->addHelpButton('streamiofileaction', 'streamiofileaction', 'mediatimesrc_streamio'); + + $this->add_action_buttons(true, get_string('delete')); + } +} diff --git a/source/streamio/classes/manager.php b/source/streamio/classes/manager.php index 53a7d24..4bc35a6 100644 --- a/source/streamio/classes/manager.php +++ b/source/streamio/classes/manager.php @@ -101,7 +101,13 @@ public function __construct($record = null) { redirect($redirect); } - $this->form = new form\edit_resource(); + if ($delete = optional_param('delete', null, PARAM_INT)) { + $this->form = new form\delete_resource(null, (array)$this->record); + $this->form->set_data(['delete' => $delete]); + } else { + $this->form = new form\edit_resource(); + } + if ($record) { $this->content = json_decode($record->content); } @@ -114,6 +120,14 @@ public function __construct($record = null) { ] + (array)$this->content); } if ($this->form->is_cancelled()) { + $redirect = new moodle_url('/admin/tool/mediatime/index.php'); + redirect($redirect); + } else if ( + optional_param('delete', null, PARAM_INT) + && ($data = $this->form->get_data()) + ) { + $this->delete_resource($data->action == 2); + $redirect = new moodle_url('/admin/tool/mediatime/index.php'); redirect($redirect); } else if (($data = $this->form->get_data()) && empty($data->newfile)) { @@ -144,6 +158,12 @@ public function __construct($record = null) { $video->name = $data->name; $data->content = json_encode($video); $DB->update_record('tool_mediatime', $data); + + $event = \mediatimesrc_streamio\event\resource_updated::create([ + 'contextid' => SYSCONTEXTID, + 'objectid' => $this->record->id, + ]); + $event->trigger(); } $this->save_file($data->edit, $video); @@ -241,4 +261,28 @@ protected function save_file($id, $video) { curl_close($ch); } } + + /** + * Delete resource + * + * @param bool $removestreamiofile Whether to delete server resource at Streamio + */ + public function delete_resource(bool $removestreamiofile = false) { + global $DB; + + if ($removestreamiofile) { + $id = $this->content->id; + $this->api->request("/videos/$id", [], 'DELETE'); + } + + $DB->delete_records('tool_mediatime', [ + 'id' => $this->record->id, + ]); + + $event = \mediatimesrc_streamio\event\resource_deleted::create([ + 'contextid' => SYSCONTEXTID, + 'objectid' => $this->record->id, + ]); + $event->trigger(); + } } diff --git a/source/streamio/lang/en/mediatimesrc_streamio.php b/source/streamio/lang/en/mediatimesrc_streamio.php index 3780ce0..6a0d15d 100644 --- a/source/streamio/lang/en/mediatimesrc_streamio.php +++ b/source/streamio/lang/en/mediatimesrc_streamio.php @@ -27,14 +27,20 @@ $string['pluginname'] = 'Streamio'; $string['event_resource_created'] = 'Streamio resource created'; +$string['event_resource_deleted'] = 'Streamio resource deleted'; +$string['event_resource_updated'] = 'Streamio resource updated'; +$string['keepstreamiofiles'] = 'Keep Streamio files'; $string['password'] = 'Password'; $string['password_help'] = 'Streamio password for administrative access'; +$string['removestreamiofiles'] = 'Remove Streamio files'; $string['selectexistingfile'] = 'Select existing file'; +$string['streamiofileaction'] = 'Streamio file action'; +$string['streamiofileaction_help'] = 'Select whether the file should be deleted on Streamio when the local content is deleted. If the file is used in other local resources, it will no longer be available to users.'; +$string['streamio:upload'] = 'Upload files to Streamio'; +$string['streamio:viewall'] = 'View all files available'; $string['uploadnewfile'] = 'Upload new file'; $string['username'] = 'Username'; $string['username_help'] = 'Streamio user name for administrative access found in Streamio settings'; -$string['streamio:upload'] = 'Upload files to Streamio'; -$string['streamio:viewall'] = 'View all files available'; $string['videofile'] = 'Video file'; $string['videofile_help'] = 'Choose to upload a new video, or choose to use an existing one that you select'; $string['privacy:metadata'] = 'The Streamio plugin for Media Time does not store personal data. It does transmit files associated with educational video content that normally does not contain user data. If it is used for personal data, a separate agreement with the provider may be required.'; diff --git a/source/streamio/templates/media_resource.mustache b/source/streamio/templates/media_resource.mustache index e27beb5..085b466 100644 --- a/source/streamio/templates/media_resource.mustache +++ b/source/streamio/templates/media_resource.mustache @@ -40,6 +40,7 @@ {{# canedit }} + {{/ canedit }} {{# libraryhome }} {{# str }}library, tool_mediatime {{/ str }} diff --git a/source/videotime/classes/event/resource_deleted.php b/source/videotime/classes/event/resource_deleted.php new file mode 100644 index 0000000..6249888 --- /dev/null +++ b/source/videotime/classes/event/resource_deleted.php @@ -0,0 +1,38 @@ +. + +namespace mediatimesrc_videotime\event; + +use core\event\base; + +/** + * The resource_deleted event class. + * + * @package mediatimesrc_videotime + * @category event + * @copyright 2024 bdecent gmbh + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class resource_deleted extends \tool_mediatime\event\resource_deleted { + /** + * Returns event name. + * + * @return string + */ + public static function get_name() { + return get_string('event_resource_deleted', 'mediatimesrc_videotime'); + } +} diff --git a/source/videotime/classes/event/resource_updated.php b/source/videotime/classes/event/resource_updated.php new file mode 100644 index 0000000..22cfeca --- /dev/null +++ b/source/videotime/classes/event/resource_updated.php @@ -0,0 +1,38 @@ +. + +namespace mediatimesrc_videotime\event; + +use core\event\base; + +/** + * The resource_updated event class. + * + * @package mediatimesrc_videotime + * @category event + * @copyright 2024 bdecent gmbh + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class resource_updated extends \tool_mediatime\event\resource_updated { + /** + * Returns event name. + * + * @return string + */ + public static function get_name() { + return get_string('event_resource_updated', 'mediatimesrc_videotime'); + } +} diff --git a/source/videotime/classes/form/delete_resource.php b/source/videotime/classes/form/delete_resource.php new file mode 100644 index 0000000..567b712 --- /dev/null +++ b/source/videotime/classes/form/delete_resource.php @@ -0,0 +1,58 @@ +. + +/** + * Media Time delete resource form + * + * @package mediatimesrc_videotime + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mediatimesrc_videotime\form; + +use context_system; +use moodleform; +use mediatimesrc_videotime\api; +use mediatimesrc_videotime\output\media_resource; + +/** + * Media Time delete resource form + * + * @copyright 2024 bdecent gmbh + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class delete_resource extends \tool_mediatime\form\delete_resource { + + /** + * Definition + */ + public function definition() { + $mform = $this->_form; + + $mform->addElement('hidden', 'source'); + $mform->setType('source', PARAM_TEXT); + $mform->setDefault('source', 'videotime'); + + $mform->addElement('static', 'name', get_string('resourcename', 'tool_mediatime'), $this->_customdata['name']); + $mform->setType('name', PARAM_TEXT); + + $mform->addElement('hidden', 'delete'); + $mform->setType('delete', PARAM_INT); + + $this->add_action_buttons(true, get_string('delete')); + } +} diff --git a/source/videotime/classes/manager.php b/source/videotime/classes/manager.php index 1235e4f..b69f86d 100644 --- a/source/videotime/classes/manager.php +++ b/source/videotime/classes/manager.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Manage Streamio source files + * Manage Video Time resource files * * @package mediatimesrc_videotime * @copyright 2024 bdecent gmbh @@ -34,13 +34,13 @@ use templatable; /** - * Manage Streamio source files + * Manage Video Time resource files * * @copyright 2024 bdecent gmbh * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class manager implements renderable, templatable { - /** @var $content Cached Streamio video record */ + /** @var $content Cached content object */ protected ?stdClass $content = null; /** @var $record Media time record for resource */ @@ -60,7 +60,13 @@ public function __construct($record = null) { $context = \context_system::instance(); $this->record = $record; - $this->form = new form\edit_resource(); + if ($delete = optional_param('delete', null, PARAM_INT)) { + $this->form = new form\delete_resource(null, (array)$this->record); + $this->form->set_data(['delete' => $delete]); + } else { + $this->form = new form\edit_resource(); + } + if ($record) { $this->content = json_decode($record->content); } @@ -102,6 +108,14 @@ public function __construct($record = null) { ] + (array)$this->content); } if ($this->form->is_cancelled()) { + $redirect = new moodle_url('/admin/tool/mediatime/index.php'); + redirect($redirect); + } else if ( + optional_param('delete', null, PARAM_INT) + && ($data = $this->form->get_data()) + ) { + $this->delete_resource(); + $redirect = new moodle_url('/admin/tool/mediatime/index.php'); redirect($redirect); } else if ($data = $this->form->get_data()) { @@ -122,6 +136,12 @@ public function __construct($record = null) { $data->id = $data->edit; $data->content = json_encode($data); $DB->update_record('tool_mediatime', $data); + + $event = event\resource_updated::create([ + 'contextid' => SYSCONTEXTID, + 'objectid' => $this->record->id, + ]); + $event->trigger(); } file_save_draft_area_files( @@ -182,4 +202,21 @@ public function export_for_template(renderer_base $output) { 'form' => $this->form->render(), ]; } + + /** + * Delete resource + */ + public function delete_resource() { + global $DB; + + $DB->delete_records('tool_mediatime', [ + 'id' => $this->record->id, + ]); + + $event = \mediatimesrc_videotime\event\resource_deleted::create([ + 'contextid' => SYSCONTEXTID, + 'objectid' => $this->record->id, + ]); + $event->trigger(); + } } diff --git a/source/videotime/lang/en/mediatimesrc_videotime.php b/source/videotime/lang/en/mediatimesrc_videotime.php index d8c736d..f67d0ef 100644 --- a/source/videotime/lang/en/mediatimesrc_videotime.php +++ b/source/videotime/lang/en/mediatimesrc_videotime.php @@ -27,9 +27,11 @@ $string['pluginname'] = 'Video Time resource'; $string['event_resource_created'] = 'Video Time resource created'; +$string['event_resource_deleted'] = 'Video Time resource deleted'; +$string['event_resource_updated'] = 'Video Time resource updated'; +$string['maxbytes'] = 'Maximum size of file uploads'; $string['posterimage'] = 'Poster image'; $string['posterimage_help'] = 'Select an image to displayed before the video is played or as a thumbnail image.'; -$string['maxbytes'] = 'Maximum size of file uploads'; $string['videofile'] = 'Video file'; $string['videofile_help'] = 'Video file to be displayed'; $string['configmaxbytes'] = 'Default maximum file size for all files uploaded'; diff --git a/source/videotime/templates/media_resource.mustache b/source/videotime/templates/media_resource.mustache index f035cf6..30bf3a5 100644 --- a/source/videotime/templates/media_resource.mustache +++ b/source/videotime/templates/media_resource.mustache @@ -40,6 +40,7 @@ {{# canedit }} + {{/ canedit }} {{# libraryhome }} {{# str }}library, tool_mediatime {{/ str }} diff --git a/templates/media_manager.mustache b/templates/media_manager.mustache index 44ba420..8ac3ad5 100644 --- a/templates/media_manager.mustache +++ b/templates/media_manager.mustache @@ -50,6 +50,7 @@ {{^ content.name }} {{ content.title }} {{/ content.name }} + diff --git a/templates/media_resource.mustache b/templates/media_resource.mustache index cba217c..f6d9452 100644 --- a/templates/media_resource.mustache +++ b/templates/media_resource.mustache @@ -31,6 +31,7 @@
{{# canedit }} + {{/ canedit }} {{# str }}library, tool_mediatime {{/ str }}