MDL-53345 forum: Add attachmentareaid to support attachments in api

This commit is contained in:
Ben Kelada 2016-03-31 14:13:37 +11:00
parent 41182118ae
commit e881c4f5c3
2 changed files with 59 additions and 15 deletions

View File

@ -962,6 +962,7 @@ class mod_forum_external extends external_api {
'The allowed keys (value format) are:
discussionsubscribe (bool); subscribe to the discussion?, default to true
itemid (int); the draft file area id for inline attachments
attachmentsid (int); the draft file area id for attachments
'),
'value' => new external_value(PARAM_RAW, 'the value of the option,
this param is validated in the external function.'
@ -997,7 +998,8 @@ class mod_forum_external extends external_api {
// Validate options.
$options = array(
'discussionsubscribe' => true,
'itemid' => 0
'itemid' => 0,
'attachmentsid' => null
);
foreach ($params['options'] as $option) {
$name = trim($option['name']);
@ -1008,6 +1010,9 @@ class mod_forum_external extends external_api {
case 'itemid':
$value = clean_param($option['value'], PARAM_INT);
break;
case 'attachmentsid':
$value = clean_param($option['value'], PARAM_INT);
break;
default:
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
@ -1047,8 +1052,9 @@ class mod_forum_external extends external_api {
$post->messageformat = FORMAT_HTML; // Force formatting for now.
$post->messagetrust = trusttext_trusted($context);
$post->itemid = $options['itemid'];
if ($postid = forum_add_new_post($post, null)) {
$post->attachments = $options['attachmentsid'];
$fakemform = $post->attachments;
if ($postid = forum_add_new_post($post, $fakemform)) {
$post->id = $postid;
@ -1123,6 +1129,7 @@ class mod_forum_external extends external_api {
discussionsubscribe (bool); subscribe to the discussion?, default to true
discussionpinned (bool); is the discussion pinned, default to false
itemid (int); the draft file area id for inline attachments
attachmentsid (int); the draft file area id for attachments
'),
'value' => new external_value(PARAM_RAW, 'The value of the option,
This param is validated in the external function.'
@ -1161,7 +1168,8 @@ class mod_forum_external extends external_api {
$options = array(
'discussionsubscribe' => true,
'discussionpinned' => false,
'itemid' => 0
'itemid' => 0,
'attachmentsid' => null
);
foreach ($params['options'] as $option) {
$name = trim($option['name']);
@ -1175,6 +1183,9 @@ class mod_forum_external extends external_api {
case 'itemid':
$value = clean_param($option['value'], PARAM_INT);
break;
case 'attachmentsid':
$value = clean_param($option['value'], PARAM_INT);
break;
default:
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
@ -1226,13 +1237,15 @@ class mod_forum_external extends external_api {
$discussion->name = $discussion->subject;
$discussion->timestart = 0;
$discussion->timeend = 0;
$discussion->attachments = $options['attachmentsid'];
if (has_capability('mod/forum:pindiscussions', $context) && $options['discussionpinned']) {
$discussion->pinned = FORUM_DISCUSSION_PINNED;
} else {
$discussion->pinned = FORUM_DISCUSSION_UNPINNED;
}
if ($discussionid = forum_add_discussion($discussion)) {
$fakemform = $options['attachmentsid'];
if ($discussionid = forum_add_discussion($discussion, $fakemform)) {
$discussion->id = $discussionid;

View File

@ -782,7 +782,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
* Test add_discussion_post
*/
public function test_add_discussion_post() {
global $CFG, $USER;
global $CFG;
$this->resetAfterTest(true);
@ -838,16 +838,17 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
}
$this->assertTrue($tested);
// Test inline attachment in post
// Test inline and regular attachment in post
// Create a file in a draft area for inline attachments.
$draftidinlineattach = file_get_unused_draft_itemid();
$draftidattach = file_get_unused_draft_itemid();
self::setUser($user);
$usercontext = context_user::instance($user->id);
$filepath = '/';
$filearea = 'draft';
$component = 'user';
$filenameimg = 'shouldbeanimage.txt';
$filerecord = array(
$filerecordinline = array(
'contextid' => $usercontext->id,
'component' => $component,
'filearea' => $filearea,
@ -856,9 +857,17 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
'filename' => $filenameimg,
);
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, 'image contents (not really)');
$options = array(array('name' => 'itemid', 'value' => $draftidinlineattach));
// Create a file in a draft area for regular attachments.
$filerecordattach = $filerecordinline;
$attachfilename = 'attachment.txt';
$filerecordattach['filename'] = $attachfilename;
$filerecordattach['itemid'] = $draftidattach;
$fs->create_file_from_string($filerecordinline, 'image contents (not really)');
$fs->create_file_from_string($filerecordattach, 'simple text attachment');
$options = array(array('name' => 'itemid', 'value' => $draftidinlineattach),
array('name' => 'attachmentsid', 'value' => $draftidattach));
$dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot
. "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}"
. '" alt="inlineimage">.';
@ -874,9 +883,12 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
foreach ($posts['posts'] as $thispost) {
if ($createdpost['postid'] == $thispost['id']) {
$this->assertEquals($createdpost['postid'], $thispost['id']);
$this->assertNotContains('draftfile.php', $thispost['message']);
$this->assertEquals($thispost['attachment'], 1, "There should be a non-inline attachment");
$this->assertCount(1, $thispost['attachments'], "There should be 1 attachment");
$this->assertEquals($thispost['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
$this->assertContains('pluginfile.php', $thispost['message']);
$postfound = true;
break;
}
}
@ -954,9 +966,15 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$this->assertCount(3, $discussions['discussions']);
$this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']);
// Test inline attachment in new discussion
// Test inline and regular attachment in new discussion
// Create a file in a draft area for inline attachments.
$fs = get_file_storage();
$draftidinlineattach = file_get_unused_draft_itemid();
$draftidattach = file_get_unused_draft_itemid();
$usercontext = context_user::instance($USER->id);
$filepath = '/';
$filearea = 'draft';
@ -970,13 +988,22 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
'filepath' => $filepath,
'filename' => $filenameimg,
);
$fs = get_file_storage();
// Create a file in a draft area for regular attachments.
$filerecordattach = $filerecord;
$attachfilename = 'attachment.txt';
$filerecordattach['filename'] = $attachfilename;
$filerecordattach['itemid'] = $draftidattach;
$fs->create_file_from_string($filerecord, 'image contents (not really)');
$fs->create_file_from_string($filerecordattach, 'simple text attachment');
$dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot .
"/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" .
'" alt="inlineimage">.';
$options = array(array('name' => 'itemid', 'value' => $draftidinlineattach));
$options = array(array('name' => 'itemid', 'value' => $draftidinlineattach),
array('name' => 'attachmentsid', 'value' => $draftidattach));
$createddiscussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject',
$dummytext, -1, $options);
$createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
@ -990,9 +1017,13 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$postfound = false;
foreach ($discussions['discussions'] as $thisdiscussion) {
if ($createddiscussion['discussionid'] == $thisdiscussion['discussion']) {
$this->assertEquals($thisdiscussion['attachment'], 1, "There should be a non-inline attachment");
$this->assertCount(1, $thisdiscussion['attachments'], "There should be 1 attachment");
$this->assertEquals($thisdiscussion['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
$this->assertNotContains('draftfile.php', $thisdiscussion['message']);
$this->assertContains('pluginfile.php', $thisdiscussion['message']);
$postfound = true;
break;
}
}