Merge branch 'MDL-69089-master' of git://github.com/aanabit/moodle

This commit is contained in:
Andrew Nicols 2020-08-19 08:18:16 +08:00
commit 7cd63bde9f
9 changed files with 67 additions and 34 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 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,d){var e="success";b.call([{methodname:"core_contentbank_delete_content",args:{contentids:{contentid:a}}}])[0].then(function(a){if(a.result){return"contentdeleted"}e="error";return"contentnotdeleted"}).done(function(a){var b={contextid:d};if("success"==e){b.statusmsg=a}else{b.errormsg=a}window.location.href=f.relativeUrl("contentbank/index.php",b,!1)}).fail(c.exception)}function j(a,d){var e="success";b.call([{methodname:"core_contentbank_rename_content",args:{contentid:a,name:d}}])[0].then(function(a){if(a){return"contentrenamed"}e="error";return"contentnotrenamed"}).then(function(b){var d=null;if("success"==e){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}}});
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(b){var e=a("#newname").val().trim();if(e){j(i,e)}else{d.get_strings([{key:"error"},{key:"emptynamenotallowed",component:"core_contentbank"}]).then(function(a){c.alert(a[0],a[1])}).catch(c.exception);b.preventDefault()}});b.getRoot().on(h.hidden,function(){b.destroy()});b.show()}).catch(c.exception)})};function i(a,d){var e="success";b.call([{methodname:"core_contentbank_delete_content",args:{contentids:{contentid:a}}}])[0].then(function(a){if(a.result){return"contentdeleted"}e="error";return"contentnotdeleted"}).done(function(a){var b={contextid:d};if("success"==e){b.statusmsg=a}else{b.errormsg=a}window.location.href=f.relativeUrl("contentbank/index.php",b,!1)}).fail(c.exception)}function j(a,d){var e="success";b.call([{methodname:"core_contentbank_rename_content",args:{contentid:a,name:d}}])[0].then(function(a){if(a.result){return"contentrenamed"}e="error";return a.warnings[0].message}).then(function(b){var d=null;if("success"==e){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

@ -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.
@ -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;

View File

@ -127,6 +127,7 @@ abstract class content {
* @throws \coding_exception if not loaded.
*/
public function set_name(string $name): bool {
$name = trim($name);
if (empty($name)) {
return false;
}

View File

@ -87,15 +87,24 @@ class rename_content extends external_api {
$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.

View File

@ -81,7 +81,9 @@ class core_contenttype_content_testcase extends \advanced_testcase {
'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'],
'Name with tags' => ['This is <b>bold</b>', 'This is bold'],
'Long name' => [str_repeat('a', 100), str_repeat('a', 100)],
'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)]
'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)],
'Empty name' => ['', 'Old name'],
'Blanks only' => [' ', 'Old name'],
];
}

View File

@ -375,12 +375,14 @@ class core_contenttype_contenttype_testcase extends \advanced_testcase {
*/
public function rename_content_provider() {
return [
'Standard name' => ['New name', 'New name'],
'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'],
'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'],
'Name with tags' => ['This is <b>bold</b>', 'This is bold'],
'Long name' => [str_repeat('a', 100), str_repeat('a', 100)],
'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)]
'Standard name' => ['New name', 'New name', true],
'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017', true],
'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle', true],
'Name with tags' => ['This is <b>bold</b>', 'This is bold', true],
'Long name' => [str_repeat('a', 100), str_repeat('a', 100), true],
'Too long name' => [str_repeat('a', 300), str_repeat('a', 255), true],
'Empty name' => ['', 'Test content ', false],
'Blanks only' => [' ', 'Test content ', false],
];
}
@ -390,10 +392,11 @@ class core_contenttype_contenttype_testcase extends \advanced_testcase {
* @dataProvider rename_content_provider
* @param string $newname The name to set
* @param string $expected The name result
* @param bool $result The bolean result expected when renaming
*
* @covers ::rename_content
*/
public function test_rename_content(string $newname, string $expected) {
public function test_rename_content(string $newname, string $expected, bool $result) {
global $DB;
$this->resetAfterTest();
@ -414,9 +417,8 @@ class core_contenttype_contenttype_testcase extends \advanced_testcase {
// Check the content is renamed as expected by a user with permission.
$renamed = $contenttype->rename_content($content, $newname);
$this->assertTrue($renamed);
$this->assertEquals($result, $renamed);
$record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
$this->assertNotEquals($oldname, $record->name);
$this->assertEquals($expected, $record->name);
}

View File

@ -52,12 +52,14 @@ class rename_content_testcase extends \externallib_advanced_testcase {
*/
public function rename_content_provider() {
return [
'Standard name' => ['New name', 'New name'],
'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'],
'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'],
'Name with tags' => ['This is <b>bold</b>', 'This is bold'],
'Long name' => [str_repeat('a', 100), str_repeat('a', 100)],
'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)]
'Standard name' => ['New name', 'New name', true],
'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017', true],
'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle', true],
'Name with tags' => ['This is <b>bold</b>', 'This is bold', true],
'Long name' => [str_repeat('a', 100), str_repeat('a', 100), true],
'Too long name' => [str_repeat('a', 300), str_repeat('a', 255), true],
'Empty name' => ['', 'Test content ', false],
'Blanks only' => [' ', 'Test content ', false],
];
}
@ -66,11 +68,12 @@ class rename_content_testcase extends \externallib_advanced_testcase {
*
* @dataProvider rename_content_provider
* @param string $newname The name to set
* @param string $expected The name result
* @param string $expectedname The name result
* @param bool $expectedresult The bolean result expected when renaming
*
* @covers ::execute
*/
public function test_rename_content_with_permission(string $newname, string $expected) {
public function test_rename_content_with_permission(string $newname, string $expectedname, bool $expectedresult) {
global $DB;
$this->resetAfterTest();
@ -91,10 +94,9 @@ class rename_content_testcase extends \externallib_advanced_testcase {
// Call the WS and check the content is renamed as expected.
$result = rename_content::execute($content->get_id(), $newname);
$result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
$this->assertTrue($result['result']);
$this->assertEquals($expectedresult, $result['result']);
$record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
$this->assertNotEquals($oldname, $record->name);
$this->assertEquals($expected, $record->name);
$this->assertEquals($expectedname, $record->name);
// Call the WS using an unexisting contentid and check an error is thrown.
$this->expectException(\invalid_response_exception::class);

View File

@ -33,6 +33,7 @@ $string['contentrenamed'] = 'The content has been renamed.';
$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';