Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions amd/build/images_list.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/images_list.min.js.map

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

92 changes: 92 additions & 0 deletions amd/src/images_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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/>.

/**
* Report builder images list management
*
* @module core_imagebuilder/images_list
* @copyright 2021 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

"use strict";

import {dispatchEvent} from 'core/event_dispatcher';
import Notification from 'core/notification';
import Pending from 'core/pending';
import {prefetchStrings} from 'core/prefetch';
import {getString} from 'core/str';
import {add as addToast} from 'core/toast';
import * as reportEvents from 'core_reportbuilder/local/events';
import Ajax from 'core/ajax';

/**
* Delete given image
*
* @param {Number} imageId
* @return {Promise}
*/
export const deleteImage = imageId => {
const request = {
methodname: 'mod_scheduler_delete_image',
args: {imageid: imageId}
};

return Ajax.call([request])[0];
};

/**
* Initialise module
*/
export const init = () => {
prefetchStrings('mod_scheduler', [
'deleteimage',
'deleteimageconfirm',
'imagedeleted',
]);

prefetchStrings('core', [
'delete',
]);

document.addEventListener('click', event => {
const imageDelete = event.target.closest('[data-action="image-delete"]');
if (imageDelete) {
event.preventDefault();

// Use triggerElement to return focus to the action menu toggle.
const triggerElement = imageDelete.closest('.dropdown').querySelector('.dropdown-toggle');
Notification.saveCancelPromise(
getString('deleteimage', 'mod_scheduler'),
getString('deleteimageconfirm', 'mod_scheduler', imageDelete.dataset.imageName),
getString('delete', 'core'),
{triggerElement}
).then(() => {
const pendingPromise = new Pending('mod_scheduler/image:delete');
const reportElement = event.target.closest('[data-region="core_reportbuilder/report"]');

return deleteImage(imageDelete.dataset.imageId)
.then(() => addToast(getString('imagedeleted', 'mod_scheduler')))
.then(() => {
dispatchEvent(reportEvents.tableReload, {preservePagination: true}, reportElement);
return pendingPromise.resolve();
})
.catch(Notification.exception);
}).catch(() => {
return;
});
}
});
};
12 changes: 6 additions & 6 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

require_once("$CFG->libdir/externallib.php");

use external_api;
use external_function_parameters;
use external_value;
use external_single_structure;
use external_multiple_structure;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_value;
use core_external\external_single_structure;
use core_external\external_multiple_structure;

use \mod_scheduler\model\scheduler;
use mod_scheduler\model\scheduler;

/**
* This is the external API for this component.
Expand Down
89 changes: 89 additions & 0 deletions classes/external/delete_image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
// 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/>.

/**
* This is the external API for this component.
*
* @package mod_scheduler
* @copyright 2022 University of Glasgow
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_scheduler\external;

defined('MOODLE_INTERNAL') || die();

use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_value;
use core_external\external_single_structure;
use core_external\external_multiple_structure;

use \mod_scheduler\model\scheduler;

/**
* This is the external API for this component.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_image extends external_api {

/**
* Delete image parameters
*
* @return external_function_parameters
*/
public static function execute_parameters() {
return new external_function_parameters([
'imageid' => new external_value(PARAM_INT, 'The image id', VALUE_REQUIRED),
]);
}

public static function execute(int $imageid): bool {
global $DB;

[
'imageid' => $imageid,
] = self::validate_parameters(self::execute_parameters(), [
'imageid' => $imageid,
]);

$filerecord = $DB->get_record('files', ['id' => $imageid]);
if ($filerecord->component !== 'mod_scheduler') {
return false;
}
if ($filerecord->filearea !== 'message') {
return false;
}
$ctx = \context::instance_by_id($filerecord->contextid);
if (!has_any_capability(['mod/scheduler:manage', 'mod/scheduler:manageallappointments'], $ctx)) {
return false;
}
$fs = get_file_storage();
$file = $fs->get_file($filerecord->contextid, 'mod_scheduler', 'message', $filerecord->itemid, $filerecord->filepath, $filerecord->filename);
$file->delete();
return true;
}

/**
* External method return value
*
* @return external_value
*/
public static function execute_returns(): external_value {
return new external_value(PARAM_BOOL, 'Success');
}
}
Loading
Loading