diff --git a/mod/forum/classes/message/inbound/reply_handler.php b/mod/forum/classes/message/inbound/reply_handler.php index d8375694e90..874626a3f76 100644 --- a/mod/forum/classes/message/inbound/reply_handler.php +++ b/mod/forum/classes/message/inbound/reply_handler.php @@ -173,8 +173,7 @@ class reply_handler extends \core\message\inbound\handler { // Add attachments to the post. if (!empty($messagedata->attachments['attachment']) && count($messagedata->attachments['attachment'])) { $attachmentcount = count($messagedata->attachments['attachment']); - if (empty($forum->maxattachments) || $forum->maxbytes == 1 || - !has_capability('mod/forum:createattachment', $modcontext)) { + if (!forum_can_create_attachment($forum, $modcontext)) { // Attachments are not allowed. mtrace("--> User does not have permission to attach files in this forum. Rejecting e-mail."); diff --git a/mod/forum/classes/post_form.php b/mod/forum/classes/post_form.php index 6ddbaee7472..6ee9586b1a6 100644 --- a/mod/forum/classes/post_form.php +++ b/mod/forum/classes/post_form.php @@ -133,7 +133,7 @@ class mod_forum_post_form extends moodleform { $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->addHelpButton('attachments', 'attachment', 'forum'); } diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 1efe238ad4f..04cde18a1d8 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -1129,6 +1129,8 @@ class mod_forum_external extends external_api { $result = array(); $result['status'] = $status; + $result['canpindiscussions'] = has_capability('mod/forum:pindiscussions', $context); + $result['cancreateattachment'] = forum_can_create_attachment($forum, $context); $result['warnings'] = $warnings; return $result; } @@ -1143,6 +1145,10 @@ class mod_forum_external extends external_api { return new external_single_structure( array( '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() ) ); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 970396cf3c8..d3c457b0c39 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -8078,3 +8078,19 @@ function forum_check_updates_since(cm_info $cm, $from, $filter = array()) { 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; +} \ No newline at end of file diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 3a02d7303b9..ea1ca6d0354 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -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. */ public function test_can_add_discussion() { - + global $DB; $this->resetAfterTest(true); // 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 = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $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(); $result = mod_forum_external::can_add_discussion($forum->id); $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result); $this->assertTrue($result['status']); + $this->assertTrue($result['canpindiscussions']); + $this->assertTrue($result['cancreateattachment']); } diff --git a/mod/forum/upgrade.txt b/mod/forum/upgrade.txt index 966d9afa65f..4b730e49fa8 100644 --- a/mod/forum/upgrade.txt +++ b/mod/forum/upgrade.txt @@ -4,9 +4,12 @@ information provided here is intended especially for developers. === 3.3 === * External function get_forums_by_courses now returns and additional field "istracked" that indicates if the user 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, 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 === * The setting $CFG->forum_replytouser has been removed in favour of a centralized noreplyaddress setting.