diff --git a/mod/forum/amd/build/repository.min.js b/mod/forum/amd/build/repository.min.js index 16de8615ee1..40df235d3ae 100644 --- a/mod/forum/amd/build/repository.min.js +++ b/mod/forum/amd/build/repository.min.js @@ -1 +1 @@ -define(["core/ajax"],function(a){var b=function(b,c,d){var e={methodname:"mod_forum_set_subscription_state",args:{forumid:b,discussionid:c,targetstate:d}};return a.call([e])[0]},c=function(b,c,d,e,f,g){var h={methodname:"mod_forum_add_discussion_post",args:{postid:b,message:d,messageformat:e,subject:c,options:[{name:"private",value:f},{name:"tohtml",value:g}]}};return a.call([h])[0]},d=function(b,c,d){var e={methodname:"mod_forum_toggle_favourite_state",args:{discussionid:c,targetstate:d}};return a.call([e])[0]},e=function(b,c,d){var e={methodname:"mod_forum_set_lock_state",args:{forumid:b,discussionid:c,targetstate:d}};return a.call([e])[0]},f=function(b,c,d){var e={methodname:"mod_forum_set_pin_state",args:{discussionid:c,targetstate:d}};return a.call([e])[0]};return{setDiscussionSubscriptionState:b,addDiscussionPost:c,setDiscussionLockState:e,setFavouriteDiscussionState:d,setPinDiscussionState:f}}); \ No newline at end of file +define(["core/ajax"],function(a){var b=function(b,c,d){var e={methodname:"mod_forum_set_subscription_state",args:{forumid:b,discussionid:c,targetstate:d}};return a.call([e])[0]},c=function(b,c,d,e,f,g){var h={methodname:"mod_forum_add_discussion_post",args:{postid:b,message:d,messageformat:e,subject:c,options:[{name:"private",value:f},{name:"topreferredformat",value:g}]}};return a.call([h])[0]},d=function(b,c,d){var e={methodname:"mod_forum_toggle_favourite_state",args:{discussionid:c,targetstate:d}};return a.call([e])[0]},e=function(b,c,d){var e={methodname:"mod_forum_set_lock_state",args:{forumid:b,discussionid:c,targetstate:d}};return a.call([e])[0]},f=function(b,c,d){var e={methodname:"mod_forum_set_pin_state",args:{discussionid:c,targetstate:d}};return a.call([e])[0]};return{setDiscussionSubscriptionState:b,addDiscussionPost:c,setDiscussionLockState:e,setFavouriteDiscussionState:d,setPinDiscussionState:f}}); \ No newline at end of file diff --git a/mod/forum/amd/src/inpage_reply.js b/mod/forum/amd/src/inpage_reply.js index 7b9edd3b20f..014e64669ce 100644 --- a/mod/forum/amd/src/inpage_reply.js +++ b/mod/forum/amd/src/inpage_reply.js @@ -94,7 +94,7 @@ define([ // In the future, other formats should be supported, letting users to use their preferred editor and format. var messageformat = CONTENT_FORMATS.MOODLE; // The message post will be converted from messageformat to FORMAT_HTML. - var tohtml = true; + var topreferredformat = true; var postid = form.elements.reply.value; var subject = form.elements.subject.value; var currentRoot = submitButton.parents(Selectors.post.forumContent); @@ -106,7 +106,7 @@ define([ showSubmitButtonLoadingIcon(submitButton); allButtons.prop('disabled', true); - Repository.addDiscussionPost(postid, subject, message, messageformat, isprivatereply, tohtml) + Repository.addDiscussionPost(postid, subject, message, messageformat, isprivatereply, topreferredformat) .then(function(context) { var message = context.messages.reduce(function(carry, message) { if (message.type == 'success') { diff --git a/mod/forum/amd/src/repository.js b/mod/forum/amd/src/repository.js index e63fb48387a..1e8e29d694d 100644 --- a/mod/forum/amd/src/repository.js +++ b/mod/forum/amd/src/repository.js @@ -43,7 +43,7 @@ define(['core/ajax'], function(Ajax) { return Ajax.call([request])[0]; }; - var addDiscussionPost = function(postid, subject, message, messageformat, isprivatereply, tohtml) { + var addDiscussionPost = function(postid, subject, message, messageformat, isprivatereply, topreferredformat) { var request = { methodname: 'mod_forum_add_discussion_post', args: { @@ -55,8 +55,8 @@ define(['core/ajax'], function(Ajax) { name: "private", value: isprivatereply, }, { - name: "tohtml", - value: tohtml, + name: "topreferredformat", + value: topreferredformat, }] } }; diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 6f5b6613d09..37f09d7b9dd 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -1188,7 +1188,7 @@ class mod_forum_external extends external_api { private (bool); make this reply private to the author of the parent post, default to false. inlineattachmentsid (int); the draft file area id for inline attachments attachmentsid (int); the draft file area id for attachments - tohtml (bool); convert the message & messageformat to FORMAT_HTML, defaults to false + topreferredformat (bool); convert the message & messageformat to FORMAT_HTML, defaults to false '), 'value' => new external_value(PARAM_RAW, 'the value of the option, this param is validated in the external function.' @@ -1263,7 +1263,7 @@ class mod_forum_external extends external_api { 'private' => false, 'inlineattachmentsid' => 0, 'attachmentsid' => null, - 'tohtml' => false + 'topreferredformat' => false ); foreach ($params['options'] as $option) { $name = trim($option['name']); @@ -1284,7 +1284,7 @@ class mod_forum_external extends external_api { $value = 0; } break; - case 'tohtml': + case 'topreferredformat': $value = clean_param($option['value'], PARAM_BOOL); break; default: @@ -1300,10 +1300,15 @@ class mod_forum_external extends external_api { $thresholdwarning = forum_check_throttling($forumrecord, $cm); forum_check_blocking_threshold($thresholdwarning); - // If we want to force a conversion to HTML, let's do it now. - if ($options['tohtml'] && $params['messageformat'] != FORMAT_HTML) { - $params['message'] = format_text($params['message'], $params['messageformat'], ['context' => $context]); - $params['messageformat'] = FORMAT_HTML; + // If we want to force a conversion to the preferred format, let's do it now. + if ($options['topreferredformat']) { + // We always are going to honor the preferred format. We are creating a new post. + $preferredformat = editors_get_preferred_format(); + // If the post is not HTML and the preferred format is HTML, convert to it. + if ($params['messageformat'] != FORMAT_HTML and $preferredformat == FORMAT_HTML) { + $params['message'] = format_text($params['message'], $params['messageformat'], ['context' => $context]); + } + $params['messageformat'] = $preferredformat; } // Create the post. diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 35696addc04..5e903acbee5 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -1601,9 +1601,10 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $this->assertEquals($format, $dbformat); } - // Now let's try the 'tohtml' option. That should end with the content - // transformed and the format being FORMAT_HTML always. - $options = [['name' => 'tohtml', 'value' => true]]; + // Now let's try the 'topreferredformat' option. That should end with the content + // transformed and the format being FORMAT_HTML (when, like in this case, user preferred + // format is HTML, inferred from editor in preferences). + $options = [['name' => 'topreferredformat', 'value' => true]]; $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'interesting subject', 'with some https://example.com link', $options, FORMAT_MOODLE); $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost); diff --git a/mod/forum/upgrade.txt b/mod/forum/upgrade.txt index 50f0f2a263c..6f5c9b9df41 100644 --- a/mod/forum/upgrade.txt +++ b/mod/forum/upgrade.txt @@ -14,7 +14,7 @@ information provided here is intended especially for developers. * New external function mod_forum_external::get_forum_discussions returns a list of forum discussions optionally sorted and paginated. * External function mod_forum_external::get_forum_discussions_paginated has been deprecated. Use mod_forum_external::get_forum_discussions instead. - * External function mod_forum_external::add_discussion_post() has a new 'messageformat' param to be able to handle other formats different from FORMAT_HTML (that continues being the default one). Also a new 'tohtml' option enables the automatic conversion between any format and FORMAT_HTML before contents are saved. + * External function mod_forum_external::add_discussion_post() has a new 'messageformat' param to be able to handle other formats different from FORMAT_HTML (that continues being the default one). Also a new 'topreferredformat' option enables the automatic conversion between any format and the preferred format (given the editor preference) before contents are saved. === 3.6 ===