MDL-67790 core_contentbank: Add Rename content option

This commit is contained in:
Amaia Anabitarte 2020-04-17 13:40:55 +02:00
parent cd391f9922
commit 448012c6b6
13 changed files with 417 additions and 21 deletions

View File

@ -1,2 +1,2 @@
define ("core_contentbank/actions",["jquery","core/ajax","core/notification","core/str","core/templates","core/url","core/modal_factory","core/modal_events"],function(a,b,c,d,e,f,g,h){var j={DELETE_CONTENT:"[data-action=\"deletecontent\"]"},k=function(){this.registerEvents()};k.prototype.registerEvents=function(){a(j.DELETE_CONTENT).click(function(b){b.preventDefault();var e=a(this).data("contentname"),f=a(this).data("contentid"),j=a(this).data("contextid"),k="";d.get_strings([{key:"deletecontent",component:"core_contentbank"},{key:"deletecontentconfirm",component:"core_contentbank",param:{name:e}},{key:"delete",component:"core"}]).then(function(a){var b=a[0],c=a[1];k=a[2];return g.create({title:b,body:c,type:g.types.SAVE_CANCEL,large:!0})}).done(function(a){a.setSaveButtonText(k);a.getRoot().on(h.save,function(){return i(f,j)});a.getRoot().on(h.hidden,function(){a.destroy()});a.show()}).catch(c.exception)})};function i(a,e){var g="success";b.call([{methodname:"core_contentbank_delete_content",args:{contentids:{contentid:a}}}])[0].then(function(a){if(a.result){return d.get_string("contentdeleted","core_contentbank")}g="error";return d.get_string("contentnotdeleted","core_contentbank")}).done(function(a){var b={contextid:e};if("success"==g){b.statusmsg=a}else{b.errormsg=a}window.location.href=f.relativeUrl("contentbank/index.php",b,!1)}).fail(c.exception)}return{init:function init(){return new k}}});
define ("core_contentbank/actions",["jquery","core/ajax","core/notification","core/str","core/templates","core/url","core/modal_factory","core/modal_events"],function(a,b,c,d,e,f,g,h){var k={DELETE_CONTENT:"[data-action=\"deletecontent\"]",RENAME_CONTENT:"[data-action=\"renamecontent\"]"},l=function(){this.registerEvents()};l.prototype.registerEvents=function(){a(k.DELETE_CONTENT).click(function(b){b.preventDefault();var e=a(this).data("contentname"),f=a(this).data("contentid"),j=a(this).data("contextid"),k="";d.get_strings([{key:"deletecontent",component:"core_contentbank"},{key:"deletecontentconfirm",component:"core_contentbank",param:{name:e}},{key:"delete",component:"core"}]).then(function(a){var b=a[0],c=a[1];k=a[2];return g.create({title:b,body:c,type:g.types.SAVE_CANCEL,large:!0})}).done(function(a){a.setSaveButtonText(k);a.getRoot().on(h.save,function(){return i(f,j)});a.getRoot().on(h.hidden,function(){a.destroy()});a.show()}).catch(c.exception)});a(k.RENAME_CONTENT).click(function(b){b.preventDefault();var f=a(this).data("contentname"),i=a(this).data("contentid"),k="";d.get_strings([{key:"renamecontent",component:"core_contentbank"},{key:"rename",component:"core_contentbank"}]).then(function(a){var b=a[0];k=a[1];return g.create({title:b,body:e.render("core_contentbank/renamecontent",{contentid:i,name:f}),type:g.types.SAVE_CANCEL})}).then(function(b){b.setSaveButtonText(k);b.getRoot().on(h.save,function(){var b=a("#newname").val();return j(i,b)});b.getRoot().on(h.hidden,function(){b.destroy()});b.show()}).catch(c.exception)})};function i(a,e){var g="success";b.call([{methodname:"core_contentbank_delete_content",args:{contentids:{contentid:a}}}])[0].then(function(a){if(a.result){return d.get_string("contentdeleted","core_contentbank")}g="error";return d.get_string("contentnotdeleted","core_contentbank")}).done(function(a){var b={contextid:e};if("success"==g){b.statusmsg=a}else{b.errormsg=a}window.location.href=f.relativeUrl("contentbank/index.php",b,!1)}).fail(c.exception)}function j(a,e){var g="success";b.call([{methodname:"core_contentbank_rename_content",args:{contentid:a,name:e}}])[0].then(function(a){if(a){return d.get_string("contentrenamed","core_contentbank")}g="error";return d.get_string("contentnotrenamed","core_contentbank")}).then(function(b){var d=null;if("success"==g){d={id:a,statusmsg:b};window.location.href=f.relativeUrl("contentbank/view.php",d,!1)}else{c.addNotification({message:b,type:"error"});c.fetchNotifications()}}).catch(c.exception)}return{init:function init(){return new l}}});
//# sourceMappingURL=actions.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -39,6 +39,7 @@ function($, Ajax, Notification, Str, Templates, Url, ModalFactory, ModalEvents)
*/
var ACTIONS = {
DELETE_CONTENT: '[data-action="deletecontent"]',
RENAME_CONTENT: '[data-action="renamecontent"]',
};
/**
@ -108,6 +109,54 @@ function($, Ajax, Notification, Str, Templates, Url, ModalFactory, ModalEvents)
return;
}).catch(Notification.exception);
});
$(ACTIONS.RENAME_CONTENT).click(function(e) {
e.preventDefault();
var contentname = $(this).data('contentname');
var contentid = $(this).data('contentid');
var strings = [
{
key: 'renamecontent',
component: 'core_contentbank'
},
{
key: 'rename',
component: 'core_contentbank'
},
];
var saveButtonText = '';
Str.get_strings(strings).then(function(langStrings) {
var modalTitle = langStrings[0];
saveButtonText = langStrings[1];
return ModalFactory.create({
title: modalTitle,
body: Templates.render('core_contentbank/renamecontent', {'contentid': contentid, 'name': contentname}),
type: ModalFactory.types.SAVE_CANCEL
});
}).then(function(modal) {
modal.setSaveButtonText(saveButtonText);
modal.getRoot().on(ModalEvents.save, function() {
// The action is now confirmed, sending an action for it.
var newname = $("#newname").val();
return renameContent(contentid, newname);
});
// Handle hidden event.
modal.getRoot().on(ModalEvents.hidden, function() {
// Destroy when hidden.
modal.destroy();
});
// Show the modal.
modal.show();
return;
}).catch(Notification.exception);
});
};
/**
@ -146,6 +195,49 @@ function($, Ajax, Notification, Str, Templates, Url, ModalFactory, ModalEvents)
}).fail(Notification.exception);
}
/**
* Rename content in the content bank.
*
* @param {int} contentid The content to rename.
* @param {string} name The new name for the content.
*/
function renameContent(contentid, name) {
var request = {
methodname: 'core_contentbank_rename_content',
args: {
contentid: contentid,
name: name
}
};
var requestType = 'success';
Ajax.call([request])[0].then(function(data) {
if (data) {
return Str.get_string('contentrenamed', 'core_contentbank');
}
requestType = 'error';
return Str.get_string('contentnotrenamed', 'core_contentbank');
}).then(function(message) {
var params = null;
if (requestType == 'success') {
params = {
id: contentid,
statusmsg: message
};
// Redirect to the content view page and display the message as a notification.
window.location.href = Url.relativeUrl('contentbank/view.php', params, false);
} else {
// Fetch error notifications.
Notification.addNotification({
message: message,
type: 'error'
});
Notification.fetchNotifications();
}
return;
}).catch(Notification.exception);
}
return /** @alias module:core_contentbank/actions */ {
// Public variables and functions.

View File

@ -24,6 +24,7 @@
namespace core_contentbank;
use core_text;
use stored_file;
use stdClass;
use coding_exception;
@ -102,6 +103,33 @@ abstract class content {
return $DB->update_record('contentbank_content', $this->content);
}
/**
* Set a new name to the content.
*
* @param string $name The name of the content.
* @return bool True if the content has been succesfully updated. False otherwise.
* @throws \coding_exception if not loaded.
*/
public function set_name(string $name): bool {
if (empty($name)) {
return false;
}
// Clean name.
$name = clean_param($name, PARAM_TEXT);
if (core_text::strlen($name) > 255) {
$name = core_text::substr($name, 0, 255);
}
$oldname = $this->content->name;
$this->content->name = $name;
$updated = $this->update_content();
if (!$updated) {
$this->content->name = $oldname;
}
return $updated;
}
/**
* Returns the name of the content.
*

View File

@ -24,7 +24,6 @@
namespace core_contentbank;
use coding_exception;
use moodle_url;
/**
@ -39,7 +38,7 @@ abstract class contenttype {
/** Plugin implements uploading feature */
const CAN_UPLOAD = 'upload';
/** @var context This content's context. **/
/** @var context This contenttype's context. **/
protected $context = null;
/**
@ -99,6 +98,18 @@ abstract class contenttype {
return $DB->delete_records('contentbank_content', ['id' => $content->get_id()]);
}
/**
* Rename this content from the content_bank.
* This method can be overwritten by the plugins if they need to change some other specific information.
*
* @param content $content The content to rename.
* @param string $name The name of the content.
* @return boolean true if the content has been renamed; false otherwise.
*/
public function rename_content(content $content, string $name): bool {
return $content->set_name($name);
}
/**
* Returns the contenttype name of this content.
*
@ -240,6 +251,41 @@ abstract class contenttype {
return true;
}
/**
* Check if the user can managed this content.
*
* @param content $content The content to be managed.
* @return bool True if content could be managed. False otherwise.
*/
public final function can_manage(content $content): bool {
global $USER;
if ($this->context->id != $content->get_content()->contextid) {
// The content has to have exactly the same context as this contenttype.
return false;
}
// Check main contentbank management permission.
$hascapability = has_capability('moodle/contentbank:manageanycontent', $this->context);
if ($content->get_content()->usercreated == $USER->id) {
// This content has been created by the current user; check if they can manage their content.
$hascapability = $hascapability || has_capability('moodle/contentbank:manageowncontent', $this->context);
}
return $hascapability && $this->is_manage_allowed($content);
}
/**
* Returns if content allows managing.
*
* @param content $content The content to be managed.
* @return bool True if content allows uploading. False otherwise.
*/
protected function is_manage_allowed(content $content): bool {
// Plugins can overwrite this function to add any check they need.
return true;
}
/**
* Returns the plugin supports the feature.
*

View File

@ -0,0 +1,136 @@
<?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/>.
/**
* External API to rename content bank content.
*
* @package core_contentbank
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_contentbank\external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/externallib.php');
use external_api;
use external_function_parameters;
use external_single_structure;
use external_value;
use external_warnings;
/**
* This is the external method for renaming a content.
*
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rename_content extends external_api {
/**
* rename_content parameters.
*
* @since Moodle 3.9
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'contentid' => new external_value(PARAM_INT, 'The content id to rename', VALUE_REQUIRED),
'name' => new external_value(PARAM_RAW, 'The new name for the content', VALUE_REQUIRED),
]
);
}
/**
* Rename content from the contentbank.
*
* @since Moodle 3.9
* @param int $contentid The content id to rename.
* @param string $name The new name.
* @return array True if the content has been renamed; false and the warning, otherwise.
*/
public static function execute(int $contentid, string $name): array {
global $DB;
$result = false;
$warnings = [];
$params = self::validate_parameters(self::execute_parameters(), [
'contentid' => $contentid,
'name' => $name,
]);
$params['name'] = clean_param($params['name'], PARAM_TEXT);
try {
$record = $DB->get_record('contentbank_content', ['id' => $contentid], '*', MUST_EXIST);
$contenttypeclass = "\\$record->contenttype\\contenttype";
if (class_exists($contenttypeclass)) {
$context = \context::instance_by_id($record->contextid, MUST_EXIST);
self::validate_context($context);
$contenttype = new $contenttypeclass($context);
$contentclass = "\\$record->contenttype\\content";
$content = new $contentclass($record);
// Check capability.
if ($contenttype->can_manage($content)) {
// This content can be renamed.
if ($contenttype->rename_content($content, $params['name'])) {
$result = true;
} else {
$warnings[] = [
'item' => $contentid,
'warningcode' => 'contentnotrenamed',
'message' => get_string('contentnotrenamed', 'core_contentbank')
];
}
} else {
// The user has no permission to manage this content.
$warnings[] = [
'item' => $contentid,
'warningcode' => 'nopermissiontomanage',
'message' => get_string('nopermissiontomanage', 'core_contentbank')
];
}
}
} catch (\moodle_exception $e) {
// The content or the context don't exist.
$warnings[] = [
'item' => $contentid,
'warningcode' => 'exception',
'message' => $e->getMessage()
];
}
return [
'result' => $result,
'warnings' => $warnings
];
}
/**
* rename_content return.
*
* @since Moodle 3.9
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'result' => new external_value(PARAM_BOOL, 'The processing result'),
'warnings' => new external_warnings()
]);
}
}

View File

@ -0,0 +1,30 @@
{{!
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 core_contentbank/renamecontent
Example context (json):
{
"contentid": 17,
"name": "Accordion"
}
}}
<div class="form-check w-100 justify-content-start">
<label for="newname">{{#str}}contentname, core_contentbank{{/str}}</label>
<input type="text" size="5" id="newname" name="newname" value="{{{ name }}}" class="form-control text-ltr">
</div>

View File

@ -35,7 +35,10 @@ $record = $DB->get_record('contentbank_content', ['id' => $id], '*', MUST_EXIST)
$context = context::instance_by_id($record->contextid, MUST_EXIST);
require_capability('moodle/contentbank:access', $context);
$returnurl = new \moodle_url('/contentbank/index.php');
$statusmsg = optional_param('statusmsg', '', PARAM_RAW);
$errormsg = optional_param('errormsg', '', PARAM_RAW);
$returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
$plugin = core_plugin_manager::instance()->get_plugin_info($record->contenttype);
if (!$plugin || !$plugin->is_enabled()) {
print_error('unsupported', 'core_contentbank', $returnurl);
@ -46,7 +49,6 @@ $title = get_string('contentbank');
if ($PAGE->course) {
require_login($PAGE->course->id);
}
$returnurl = new \moodle_url('/contentbank/index.php', ['contextid' => $context->id]);
$PAGE->set_url(new \moodle_url('/contentbank/view.php', ['id' => $id]));
$PAGE->set_context($context);
@ -57,13 +59,32 @@ $PAGE->set_title($title);
$PAGE->set_pagetype('contenbank');
$contenttypeclass = "\\$record->contenttype\\contenttype";
$contenttype = new $contenttypeclass($context);
$contentclass = "\\$record->contenttype\\content";
if (!class_exists($contenttypeclass) || !class_exists($contentclass)) {
print_error('contenttypenotfound', 'error', $returnurl, $record->contenttype);
}
$contenttype = new $contenttypeclass($context);
$content = new $contentclass($record);
// Create the cog menu with all the secondary actions, such as delete, rename...
$actionmenu = new action_menu();
$actionmenu->set_alignment(action_menu::TR, action_menu::BR);
if ($contenttype->can_manage($content)) {
// Add the rename content item to the menu.
$attributes = [
'data-action' => 'renamecontent',
'data-contentname' => $content->get_name(),
'data-contentid' => $content->get_id(),
];
$actionmenu->add_secondary_action(new action_menu_link(
new moodle_url('#'),
new pix_icon('e/styleparagraph', get_string('rename')),
get_string('rename'),
false,
$attributes
));
}
if ($contenttype->can_delete($content)) {
// Create the cog menu with all the secondary actions, such as delete, rename...
$actionmenu = new action_menu();
$actionmenu->set_alignment(action_menu::TR, action_menu::BR);
// Add the delete content item to the menu.
$attributes = [
'data-action' => 'deletecontent',
@ -78,18 +99,24 @@ if ($contenttype->can_delete($content)) {
false,
$attributes
));
// Add the cog menu to the header.
$PAGE->add_header_action(html_writer::div(
$OUTPUT->render($actionmenu),
'd-print-none',
['id' => 'region-main-settings-menu']
));
}
// Add the cog menu to the header.
$PAGE->add_header_action(html_writer::div(
$OUTPUT->render($actionmenu),
'd-print-none',
['id' => 'region-main-settings-menu']
));
echo $OUTPUT->header();
echo $OUTPUT->box_start('generalbox');
// If needed, display notifications.
if ($errormsg !== '') {
echo $OUTPUT->notification($errormsg);
} else if ($statusmsg !== '') {
echo $OUTPUT->notification($statusmsg, 'notifysuccess');
}
if ($contenttype->can_access()) {
echo $contenttype->get_view_content($record);
}

View File

@ -24,23 +24,27 @@
$string['author'] = 'Author';
$string['contentdeleted'] = 'The content has been deleted.';
$string['contentname'] = 'Content name';
$string['contentnotdeleted'] = 'An error was encountered while trying to delete the content.';
$string['contentnotrenamed'] = 'An error was encountered while trying to rename the content.';
$string['contentrenamed'] = 'The content has been renamed.';
$string['deletecontent'] = 'Delete content';
$string['deletecontentconfirm'] = 'Are you sure you want to delete the content <em>\'{$a->name}\'</em> and all associated files? This action cannot be undone.';
$string['file'] = 'Upload content';
$string['file_help'] = 'Files may be stored in the content bank for use in courses. Only files used by content types enabled on the site may be uploaded.';
$string['name'] = 'Content';
$string['nopermissiontodelete'] = 'You do not have permission to delete content.';
$string['nopermissiontomanage'] = 'You do not have permission to manage content.';
$string['privacy:metadata:content:contenttype'] = 'The contenttype plugin of the content in the content bank.';
$string['privacy:metadata:content:name'] = 'Name of the content in the content bank.';
$string['privacy:metadata:content:timecreated'] = 'The time when the content was created.';
$string['privacy:metadata:content:timemodified'] = 'The time when the content was modified.';
$string['privacy:metadata:content:usercreated'] = 'The user has created the content.';
$string['privacy:metadata:content:usercreated'] = 'The user who created the content.';
$string['privacy:metadata:content:usermodified'] = 'Last user has modified the content.';
$string['privacy:metadata:content:usermodified'] = 'The last user who modified the content.';
$string['privacy:metadata:contentbankcontent'] = 'Stores the content of the content bank.';
$string['privacy:metadata:userid'] = 'The ID of the user creating or modifying content bank content.';
$string['rename'] = 'Rename';
$string['renamecontent'] = 'Rename content';
$string['timecreated'] = 'Time created';
$string['unsupported'] = 'This content type is not supported.';
$string['upload'] = 'Upload';

View File

@ -153,7 +153,9 @@ $string['confirmunassignno'] = 'Cancel';
$string['contentbank:access'] = 'Access the content bank';
$string['contentbank:deleteanycontent'] = 'Delete any content from the content bank';
$string['contentbank:deleteowncontent'] = 'Delete content from own content bank';
$string['contentbank:upload'] = 'Upload content to the content bank';
$string['contentbank:manageanycontent'] = 'Manage any content from the content bank (rename, move, publish, share, etc.)';
$string['contentbank:manageowncontent'] = 'Manage content from own content bank (rename, move, publish, share, etc.)';
$string['contentbank:upload'] = 'Upload new content in the content bank';
$string['context'] = 'Context';
$string['course:activityvisibility'] = 'Hide/show activities';
$string['course:bulkmessaging'] = 'Send a message to many people';

View File

@ -2522,4 +2522,26 @@ $capabilities = array(
'user' => CAP_ALLOW,
]
],
// Manage (rename, move, publish, share, etc.) any content from the content bank.
'moodle/contentbank:manageanycontent' => [
'riskbitmask' => RISK_DATALOSS,
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
'coursecreator' => CAP_ALLOW,
)
],
// Manage (rename, move, publish, share, etc.) content created by yourself.
'moodle/contentbank:manageowncontent' => [
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
'coursecreator' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
)
],
);

View File

@ -2773,6 +2773,15 @@ $functions = array(
'ajax' => 'true',
'capabilities' => 'moodle/contentbank:deleteanycontent',
],
'core_contentbank_rename_content' => [
'classname' => 'core_contentbank\external\rename_content',
'methodname' => 'execute',
'classpath' => '',
'description' => 'Rename a content in the content bank.',
'type' => 'write',
'ajax' => 'true',
'capabilities' => 'moodle/contentbank:manageowncontent',
],
);
$services = array(

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2020050200.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2020050200.02; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '3.9dev+ (Build: 20200502)'; // Human-friendly version name