Skip to content

Commit

Permalink
MDL-69089 core_contentbank: Empty content names are not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Aug 17, 2020
1 parent a0fc902 commit 5ea98dc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion contentbank/amd/build/actions.min.js

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

2 changes: 1 addition & 1 deletion contentbank/amd/build/actions.min.js.map

Large diffs are not rendered by default.

26 changes: 21 additions & 5 deletions contentbank/amd/src/actions.js
Expand Up @@ -139,10 +139,26 @@ function($, Ajax, Notification, Str, Templates, Url, ModalFactory, ModalEvents)
});
}).then(function(modal) {
modal.setSaveButtonText(saveButtonText);
modal.getRoot().on(ModalEvents.save, function() {
modal.getRoot().on(ModalEvents.save, function(e) {
// The action is now confirmed, sending an action for it.
var newname = $("#newname").val();
return renameContent(contentid, newname);
var newname = $("#newname").val().trim();
if (newname) {
renameContent(contentid, newname);
} else {
var errorStrings = [
{
key: 'error',
},
{
key: 'emptynamenotallowed',
component: 'core_contentbank',
},
];
Str.get_strings(errorStrings).then(function(langStrings) {
Notification.alert(langStrings[0], langStrings[1]);
}).catch(Notification.exception);
e.preventDefault();
}
});

// Handle hidden event.
Expand Down Expand Up @@ -211,11 +227,11 @@ function($, Ajax, Notification, Str, Templates, Url, ModalFactory, ModalEvents)
};
var requestType = 'success';
Ajax.call([request])[0].then(function(data) {
if (data) {
if (data.result) {
return 'contentrenamed';
}
requestType = 'error';
return 'contentnotrenamed';
return data.warnings[0].message;

}).then(function(message) {
var params = null;
Expand Down
1 change: 1 addition & 0 deletions contentbank/classes/content.php
Expand Up @@ -127,6 +127,7 @@ public function update_content(): bool {
* @throws \coding_exception if not loaded.
*/
public function set_name(string $name): bool {
$name = trim($name);
if (empty($name)) {
return false;
}
Expand Down
21 changes: 15 additions & 6 deletions contentbank/classes/external/rename_content.php
Expand Up @@ -87,15 +87,24 @@ public static function execute(int $contentid, string $name): array {
$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 {
if (empty(trim($name))) {
// If name is empty don't try to rename and return a more detailed message.
$warnings[] = [
'item' => $contentid,
'warningcode' => 'contentnotrenamed',
'message' => get_string('contentnotrenamed', 'core_contentbank')
'warningcode' => 'emptynamenotallowed',
'message' => get_string('emptynamenotallowed', 'core_contentbank')
];
} else {
// 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.
Expand Down
1 change: 1 addition & 0 deletions lang/en/contentbank.php
Expand Up @@ -33,6 +33,7 @@
$string['contentsmoved'] = 'Content bank contents moved to {$a}.';
$string['contenttypenoaccess'] = 'You cannot view this {$a} instance.';
$string['contenttypenoedit'] = 'You can not edit this content';
$string['emptynamenotallowed'] = 'Empty name is not allowed';
$string['eventcontentcreated'] = 'Content created';
$string['eventcontentdeleted'] = 'Content deleted';
$string['eventcontentupdated'] = 'Content updated';
Expand Down

0 comments on commit 5ea98dc

Please sign in to comment.