MDL-57390 mod_forum: Return more permissions in can_add_discussion

When adding new discussions we should know if:
- Pinned discussions can be created
- Attachments are supported
This commit is contained in:
Juan Leyva 2017-03-14 15:10:16 +01:00
parent 1034421264
commit 581e75bf3c
6 changed files with 42 additions and 5 deletions

View File

@ -173,8 +173,7 @@ class reply_handler extends \core\message\inbound\handler {
// Add attachments to the post. // Add attachments to the post.
if (!empty($messagedata->attachments['attachment']) && count($messagedata->attachments['attachment'])) { if (!empty($messagedata->attachments['attachment']) && count($messagedata->attachments['attachment'])) {
$attachmentcount = count($messagedata->attachments['attachment']); $attachmentcount = count($messagedata->attachments['attachment']);
if (empty($forum->maxattachments) || $forum->maxbytes == 1 || if (!forum_can_create_attachment($forum, $modcontext)) {
!has_capability('mod/forum:createattachment', $modcontext)) {
// Attachments are not allowed. // Attachments are not allowed.
mtrace("--> User does not have permission to attach files in this forum. Rejecting e-mail."); mtrace("--> User does not have permission to attach files in this forum. Rejecting e-mail.");

View File

@ -133,7 +133,7 @@ class mod_forum_post_form extends moodleform {
$mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum'); $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
} }
if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all if (forum_can_create_attachment($forum, $modcontext)) {
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum)); $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
$mform->addHelpButton('attachments', 'attachment', 'forum'); $mform->addHelpButton('attachments', 'attachment', 'forum');
} }

View File

@ -1129,6 +1129,8 @@ class mod_forum_external extends external_api {
$result = array(); $result = array();
$result['status'] = $status; $result['status'] = $status;
$result['canpindiscussions'] = has_capability('mod/forum:pindiscussions', $context);
$result['cancreateattachment'] = forum_can_create_attachment($forum, $context);
$result['warnings'] = $warnings; $result['warnings'] = $warnings;
return $result; return $result;
} }
@ -1143,6 +1145,10 @@ class mod_forum_external extends external_api {
return new external_single_structure( return new external_single_structure(
array( array(
'status' => new external_value(PARAM_BOOL, 'True if the user can add discussions, false otherwise.'), 'status' => new external_value(PARAM_BOOL, 'True if the user can add discussions, false otherwise.'),
'canpindiscussions' => new external_value(PARAM_BOOL, 'True if the user can pin discussions, false otherwise.',
VALUE_OPTIONAL),
'cancreateattachment' => new external_value(PARAM_BOOL, 'True if the user can add attachments, false otherwise.',
VALUE_OPTIONAL),
'warnings' => new external_warnings() 'warnings' => new external_warnings()
) )
); );

View File

@ -8078,3 +8078,19 @@ function forum_check_updates_since(cm_info $cm, $from, $filter = array()) {
return $updates; return $updates;
} }
/**
* Check if the user can create attachments in a forum.
* @param stdClass $forum forum object
* @param stdClass $context context object
* @return bool true if the user can create attachments, false otherwise
* @since Moodle 3.3
*/
function forum_can_create_attachment($forum, $context) {
// If maxbytes == 1 it means no attachments at all.
if (empty($forum->maxattachments) || $forum->maxbytes == 1 ||
!has_capability('mod/forum:createattachment', $context)) {
return false;
}
return true;
}

View File

@ -1001,7 +1001,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
* Test can_add_discussion. A basic test since all the API functions are already covered by unit tests. * Test can_add_discussion. A basic test since all the API functions are already covered by unit tests.
*/ */
public function test_can_add_discussion() { public function test_can_add_discussion() {
global $DB;
$this->resetAfterTest(true); $this->resetAfterTest(true);
// Create courses to add the modules. // Create courses to add the modules.
@ -1022,11 +1022,24 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$result = mod_forum_external::can_add_discussion($forum->id); $result = mod_forum_external::can_add_discussion($forum->id);
$result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
$this->assertFalse($result['status']); $this->assertFalse($result['status']);
$this->assertFalse($result['canpindiscussions']);
$this->assertTrue($result['cancreateattachment']);
// Disable attachments.
$DB->set_field('forum', 'maxattachments', 0, array('id' => $forum->id));
$result = mod_forum_external::can_add_discussion($forum->id);
$result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
$this->assertFalse($result['status']);
$this->assertFalse($result['canpindiscussions']);
$this->assertFalse($result['cancreateattachment']);
$DB->set_field('forum', 'maxattachments', 1, array('id' => $forum->id)); // Enable attachments again.
self::setAdminUser(); self::setAdminUser();
$result = mod_forum_external::can_add_discussion($forum->id); $result = mod_forum_external::can_add_discussion($forum->id);
$result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
$this->assertTrue($result['status']); $this->assertTrue($result['status']);
$this->assertTrue($result['canpindiscussions']);
$this->assertTrue($result['cancreateattachment']);
} }

View File

@ -4,9 +4,12 @@ information provided here is intended especially for developers.
=== 3.3 === === 3.3 ===
* External function get_forums_by_courses now returns and additional field "istracked" that indicates if the user * External function get_forums_by_courses now returns and additional field "istracked" that indicates if the user
is tracking the related forum. is tracking the related forum.
* The legacy forum.js file has been removed, this includes the js functions: * The legacy forum.js file has been removed, this includes the js functions:
forum_produce_subscribe_link, forum_produce_tracking_link, lockoptions_timetoitems, forum_produce_subscribe_link, forum_produce_tracking_link, lockoptions_timetoitems,
lockoptions_timefromitems, lockoptions, lockoption, unlockoption lockoptions_timefromitems, lockoptions, lockoption, unlockoption
* External function can_add_discussion now returns two additional fields:
"canpindiscussions" that indicates if the user can add pinned discussions.
"cancreateattachment" that indicates if the user can add attachments to the discussion.
=== 3.2 === === 3.2 ===
* The setting $CFG->forum_replytouser has been removed in favour of a centralized noreplyaddress setting. * The setting $CFG->forum_replytouser has been removed in favour of a centralized noreplyaddress setting.