Skip to content

Commit

Permalink
Merge pull request #355 from ferishili/issue-351-confirmation
Browse files Browse the repository at this point in the history
Proposal: (delete block) removing seriesmapping with confirmation
  • Loading branch information
NinaHerrmann authored Jan 15, 2024
2 parents 94dd66c + ece0a5c commit 8c00eec
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 19 deletions.
10 changes: 10 additions & 0 deletions amd/build/block_delete_handler.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/block_delete_handler.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions amd/src/block_delete_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Javascript to add a custom block_delete handler
*
* @module block_opencast/block_delete_handler
* @copyright 2024 Justus Dieckmann, University of Münster
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

import ModalFactory from 'core/modal_factory';
import Ajax from 'core/ajax';
import Templates from 'core/templates';
import Prefetch from "core/prefetch";
import {get_string} from 'core/str';
import Notification from "core/notification";

export const init = (contextid, deleteurl) => {
Prefetch.prefetchTemplate('block_opencast/delete_block_modal');
Prefetch.prefetchString('block_opencast', 'deletecheck_title_modal');
const deleteButton = document.querySelector('.block_opencast a.dropdown-item.block_opencast_delete');
deleteButton.onclick = async(e) => {
e.preventDefault();

const html = await Templates.render('block_opencast/delete_block_modal', {
deleteblockurl: deleteurl
});

const modal = await ModalFactory.create({
type: ModalFactory.types.CANCEL,
body: html,
title: await get_string('deletecheck_title_modal', 'block_opencast'),
large: true
});
await modal.show();
modal.body[0].querySelector('.block_opencast-delete-mapping').onclick = async() => {
try {
await Ajax.call([{
methodname: 'block_opencast_unlink_series',
args: {contextid: contextid, ocinstanceid: -1, seriesid: 'all'}
}])[0];
window.location = deleteurl;
} catch (e) {
Notification.exception(e);
}
};
};
};
76 changes: 62 additions & 14 deletions block_opencast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

/**
Expand All @@ -196,4 +187,61 @@ 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);

// Check whether the user can manually delete series mapping.
if (!has_capability('block/opencast:manageseriesforcourse', $this->context)) {
return $bc;
}

// 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) {
return str_contains($actionlink->attributes['class'], 'editing_delete');
});

// In case the delete action link could be found, we try to replace its properties.
if (!empty($deleteactionfiltered)) {
$index = key($deleteactionfiltered);
$deleteaction = reset($deleteactionfiltered);

// Delete all modal-related attributes.
if (isset($deleteaction->attributes)) {
foreach ($deleteaction->attributes as $k => $v) {
if (str_starts_with($k, 'data-modal')) {
unset($deleteaction->attributes[$k]);
}
}
$deleteaction->attributes['class'] .= ' block_opencast_delete';
}
$bc->controls[$index] = $deleteaction;
$deleteurl = new moodle_url('/course/view.php', [
'id' => $COURSE->id,
'bui_deleteid' => $this->instance->id,
'bui_confirm' => 1,
'sesskey' => sesskey()
]);
$this->page->requires->js_call_amd('block_opencast/block_delete_handler', 'init',
[$this->context->id, $deleteurl->out(false)]);
}
}
return $bc;
}
}
14 changes: 9 additions & 5 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,21 @@ public static function unlink_series(int $contextid, int $ocinstanceid, string $

list($unused, $course, $cm) = get_context_info_array($context->id);

$mapping = seriesmapping::get_record(['ocinstanceid' => $params['ocinstanceid'], 'courseid' => $course->id,
'series' => $params['seriesid'], ], true);
if ($params['seriesid'] === 'all') {
$mappings = seriesmapping::get_records(['courseid' => $course->id]);
} else {
$mappings = seriesmapping::get_records(['ocinstanceid' => $params['ocinstanceid'], 'courseid' => $course->id,
'series' => $params['seriesid']]);
}

if ($mapping) {
foreach ($mappings as $mapping) {
if (!$mapping->delete()) {
throw new moodle_exception('delete_series_failed', 'block_opencast');
}

// Unlinking series from course.
$apibridge = apibridge::get_instance($params['ocinstanceid']);
$seriesunlinked = $apibridge->unlink_series_from_course($course->id, $params['seriesid']);
$apibridge = apibridge::get_instance($mapping->get('ocinstanceid'));
$seriesunlinked = $apibridge->unlink_series_from_course($course->id, $mapping->get('series'));

if (!$seriesunlinked) {
throw new moodle_exception('delete_series_failed', 'block_opencast');
Expand Down
8 changes: 8 additions & 0 deletions lang/en/block_opencast.php
Original file line number Diff line number Diff line change
Expand Up @@ -890,5 +890,13 @@
$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';
// Strings for delete block modal.
$string['deletecheck_title_modal'] = 'Remove Opencast Block?';
$string['error_block_delete_seriesmapping'] = 'Unfortunately, there was an error during course series mapping deletion, please contact the system administrator.';

$string['delete_mapping_explanation'] = 'The Opencast Block tracks which Opencast Series is mapped to the course.<br> You can choose whether to delete the mapping.<br> If you delete it, the series will no longer appear when you create the opencast block again.';
$string['only_delete_block'] = 'Delete block, but keep series mapping';
$string['delete_block_and_mapping'] = 'Delete block and series mapping';

// Deprecated since version 2021062300.
$string['video_already_uploaded'] = 'Video already uploaded';
35 changes: 35 additions & 0 deletions templates/delete_block_modal.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template block_opencast/delete_block_modal
This template renders the delete modal
Example context (json):
{
"deleteblockurl": "https://moodle.local/blocks/opencast/managedefaults.php?courseid=3&ocinstanceid=1"
}
}}
{{#str}} delete_mapping_explanation, block_opencast {{/str}}
<div class="d-flex justify-content-end mt-3">
<div class="btn btn-primary mx-1 block_opencast-delete-mapping">
{{#str}} delete_block_and_mapping, block_opencast {{/str}}
</div>
<a class='btn btn-primary mx-1 block_opencast-keep-mapping' href="{{{deleteblockurl}}}">
{{#str}} only_delete_block, block_opencast {{/str}}
</a>
</div>
20 changes: 20 additions & 0 deletions tests/behat/block_opencast_manageseries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,23 @@ 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 will be asked to decide whether to delete seriesmapping in a confirmation.
When I open the action menu in "Opencast Videos" "block"
And I click on "Delete Opencast Videos block" "link"
Then I should see "Remove Opencast Block?"
When I click on "Delete block, but keep series mapping" "link"
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 "Delete Opencast Videos block" "link"
When I click on "Delete block and series mapping" "text"
And I wait to be redirected
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."

0 comments on commit 8c00eec

Please sign in to comment.