diff --git a/block_opencast.php b/block_opencast.php
index 80579525..e708163d 100644
--- a/block_opencast.php
+++ b/block_opencast.php
@@ -156,22 +156,13 @@ public function get_content() {
}
/**
- * Deletes the series mappings when a block is deleted.
- * @return bool
- * @throws coding_exception
+ * Perform actions when the block instance is deleting.
+ * @see block_opencast_pre_block_delete method in lib.php, by which completes this function purpose by providing
+ * a new confirmation message.
+ * @return void
*/
public function instance_delete() {
- global $COURSE;
- $success = true;
-
- $mappings = seriesmapping::get_records(['courseid' => $COURSE->id]);
- foreach ($mappings as $mapping) {
- if (!$mapping->delete()) {
- $success = false;
- }
- }
-
- return $success;
+ // Please see block_opencast_pre_block_delete before implementing anything here.
}
/**
@@ -196,4 +187,93 @@ public function instance_create() {
}
return true;
}
+
+ /**
+ * Return a block_contents object representing the full contents of this block.
+ *
+ * This internally calls ->get_content(), and then adds the editing controls etc.
+ *
+ * Overwritten method from parent class (block_base)
+ *
+ * @param \core_renderer $output
+ * @return block_contents a representation of the block, for rendering.
+ */
+ public function get_content_for_output($output) {
+ global $COURSE;
+ // Get the block_contents object from parent class.
+ $bc = parent::get_content_for_output($output);
+
+ // We prepare the data to use and replace the existing action link contents.
+ $title = $this->title;
+ $defaultdeletestr = get_string('deleteblock', 'block', $this->title);
+
+ // Check if the block_contents has controls.
+ if (!empty($bc->controls)) {
+
+ // We filter the controls to find the delete action link.
+ $deleteactionfiltered = array_filter($bc->controls, function ($actionlink) use ($defaultdeletestr, $title) {
+ // Get the text from action link.
+ $actionlinktext = $actionlink->text;
+ // Get the text if it is a type of lang_string via __toString.
+ if ($actionlinktext instanceof \lang_string) {
+ $actionlinktext = $actionlinktext->__toString();
+ }
+ return $actionlinktext === $defaultdeletestr;
+ });
+
+ // In case the delete action link could be found, we try to replace its properties.
+ if (!empty($deleteactionfiltered)) {
+ $index = key($deleteactionfiltered);
+ $deleteaction = reset($deleteactionfiltered);
+ // Replace the action link's text.
+ if (isset($deleteaction->text)) {
+ $deleteaction->text = get_string('delete_block_action_item_keepseriesmapping_text', 'block_opencast');
+ }
+ if (isset($deleteaction->attributes)) {
+ if (isset($deleteaction->attributes['data-modal-title-str'])) {
+ $deleteaction->attributes['data-modal-title-str'] = json_encode(
+ ['deletecheck_keepseriesmapping_title_modal', 'block_opencast']
+ );
+ }
+ if (isset($deleteaction->attributes['data-modal-content-str'])) {
+ $deleteaction->attributes['data-modal-content-str'] = json_encode(
+ ['deletecheck_keepseriesmapping_content_modal', 'block_opencast']
+ );
+ }
+ }
+ $bc->controls[$index] = $deleteaction;
+ }
+
+ // A new action link item that is meant to delete the seariemapping as well,
+ // only by passing the 'removeseriesmapping' flag set to 1.
+ $deleteurl = new moodle_url('/course/view.php',
+ [
+ 'id' => $COURSE->id,
+ 'sesskey' => sesskey(),
+ 'bui_deleteid' => $this->instance->id,
+ 'bui_confirm' => 1,
+ 'removeseriesmapping' => 1
+ ]
+ );
+ $str = get_string('delete_block_action_item_noseriesmapping_text', 'block_opencast');
+ $deleteseriesmappinglink = new action_menu_link_secondary(
+ $deleteurl,
+ new pix_icon('t/delete', $str, 'moodle', array('class' => 'iconsmall', 'title' => '')),
+ $str,
+ [
+ 'class' => 'editing_delete',
+ 'data-modal' => 'confirmation',
+ 'data-modal-title-str' => json_encode(['deletecheck_noseriesmapping_title_modal', 'block_opencast']),
+ 'data-modal-content-str' => json_encode(['deletecheck_noseriesmapping_content_modal', 'block_opencast']),
+ 'data-modal-yes-button-str' => json_encode(['delete', 'core']),
+ 'data-modal-toast' => 'true',
+ 'data-modal-toast-confirmation-str' => json_encode(['deleteblockinprogress', 'block', $title]),
+ 'data-modal-destination' => $deleteurl->out(false),
+ ]
+ );
+
+ $bc->controls[] = $deleteseriesmappinglink;
+ }
+ return $bc;
+ }
}
diff --git a/lang/en/block_opencast.php b/lang/en/block_opencast.php
index d5aa5ad4..db8d92a8 100644
--- a/lang/en/block_opencast.php
+++ b/lang/en/block_opencast.php
@@ -891,5 +891,12 @@
$string['directaccess_copy_success'] = 'The direct access link has been successfully copied to clipboard.';
$string['directaccess_copytoclipboard_unavialable'] = 'It seems that your browser does not support the copy to clipboard functionality, try to copy the link manually from the dropdown item.';
$string['opencast:autocompleteteacherroles'] = 'Teacher roles to be extracted and provided in the autocomplete list';
+$string['delete_block_action_item_keepseriesmapping_text'] = 'Remove Opencast Block (keep series mapping)';
+$string['deletecheck_keepseriesmapping_title_modal'] = 'Remove Opencast Block (keep series mapping)?';
+$string['deletecheck_keepseriesmapping_content_modal'] = 'Are you sure you want to remove the Opencast block from the course and keep the series mapping to the course?
With this option you are able to re-add the block to this course and have all series and events in place again';
+$string['delete_block_action_item_noseriesmapping_text'] = 'Remove Opencast Block (remove series mapping)';
+$string['deletecheck_noseriesmapping_title_modal'] = 'Remove Opencast Block (remove series mapping)?';
+$string['deletecheck_noseriesmapping_content_modal'] = 'Are you sure you want to remove the Opencast block from the course and also remove all the mapping to the series and events?
You are NOT able to retrieve the series connected to this course, in case you want to add the block back.';
+$string['error_block_delete_seriesmapping'] = 'Unfortunately, there was an error during course series mapping deletion, please contact the system administrator.';
// Deprecated since version 2021062300.
$string['video_already_uploaded'] = 'Video already uploaded';
diff --git a/lib.php b/lib.php
index 43c7e421..15896824 100644
--- a/lib.php
+++ b/lib.php
@@ -105,3 +105,40 @@ function block_opencast_pre_course_delete(stdClass $course) {
$mapping->delete();
}
}
+
+
+/**
+ * Pre-delete block hook to show a confirmation message or to perform cleaup the related series and videos.
+ *
+ * @param object $instance a row from the block_instances table
+ *
+ * @throws moodle_exception
+ */
+function block_opencast_pre_block_delete($instance) {
+
+ // We make sure if the deleting block is Opencast block, otherwise we do nothing!
+ if ($instance->blockname !== 'opencast') {
+ return;
+ }
+
+ // Get the course and context base don block instance parentcontextid.
+ list($context, $course, $cm) = get_context_info_array($instance->parentcontextid);
+
+ // We get the flag 'removeseriesmapping' to decide whether to delete the series mapping.
+ $removeseriesmapping = optional_param('removeseriesmapping', null, PARAM_INT);
+ // We only perform the series mapping deletion if the flag is set to 1.
+ if ($removeseriesmapping === 1) {
+ $success = true;
+ $mappings = seriesmapping::get_records(['courseid' => $course->id]);
+ foreach ($mappings as $mapping) {
+ if (!$mapping->delete()) {
+ $success = false;
+ }
+ }
+ if (!$success) {
+ throw new moodle_exception('error_block_delete_seriesmapping', 'block_opencast');
+ }
+ }
+ // We let the process continue if the flag 'removeseriesmapping' is set to 0,
+ // which means it is decided not to delete series mapping.
+}
diff --git a/tests/behat/block_opencast_manageseries.feature b/tests/behat/block_opencast_manageseries.feature
index b49fabbe..384a35f0 100644
--- a/tests/behat/block_opencast_manageseries.feature
+++ b/tests/behat/block_opencast_manageseries.feature
@@ -105,3 +105,21 @@ Feature: Manage series as Teacher
And I click on "Import series" "button" in the ".modal" "css_element"
And I wait "2" seconds
Then I should not see "The series could not be imported"
+
+ @javascript
+ Scenario: When manually deleting a block, teacher is able to decide whether to keep or to delete the series mapping by two different action items
+ When I open the action menu in "Opencast Videos" "block"
+ And I click on "Remove Opencast Block (keep series mapping)" "link"
+ And I click on "Delete" "button" in the "Remove Opencast Block (keep series mapping)?" "dialogue"
+ Then I add the "Opencast Videos" block
+ And I click on "Go to overview..." "link"
+ And I click on "Manage series" "link"
+ Then I should see "Test series"
+ When I am on "Course 1" course homepage with editing mode on
+ When I open the action menu in "Opencast Videos" "block"
+ And I click on "Remove Opencast Block (remove series mapping)" "link"
+ And I click on "Delete" "button" in the "Remove Opencast Block (remove series mapping)?" "dialogue"
+ Then I add the "Opencast Videos" block
+ And I click on "Go to overview..." "link"
+ And I click on "Manage series" "link"
+ Then I should see "No series is defined yet."