Skip to content

Commit

Permalink
MDL-65252 mod_forum: removal of get_forum_discussion_posts ws
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze committed Jul 1, 2021
1 parent 8baf7d6 commit 1530035
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 684 deletions.
10 changes: 0 additions & 10 deletions mod/forum/db/services.php
Expand Up @@ -48,16 +48,6 @@
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),

'mod_forum_get_forum_discussion_posts' => array(
'classname' => 'mod_forum_external',
'methodname' => 'get_forum_discussion_posts',
'classpath' => 'mod/forum/externallib.php',
'description' => 'Returns a list of forum posts for a discussion.',
'type' => 'read',
'capabilities' => 'mod/forum:viewdiscussion, mod/forum:viewqandawithoutposting',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),

'mod_forum_get_forum_discussions_paginated' => array(
'classname' => 'mod_forum_external',
'methodname' => 'get_forum_discussions_paginated',
Expand Down
237 changes: 0 additions & 237 deletions mod/forum/externallib.php
Expand Up @@ -267,243 +267,6 @@ public static function get_discussion_posts_returns() {
]);
}

/**
* Describes the parameters for get_forum_discussion_posts.
*
* @return external_function_parameters
* @since Moodle 2.7
*/
public static function get_forum_discussion_posts_parameters() {
return new external_function_parameters (
array(
'discussionid' => new external_value(PARAM_INT, 'discussion ID', VALUE_REQUIRED),
'sortby' => new external_value(PARAM_ALPHA,
'sort by this element: id, created or modified', VALUE_DEFAULT, 'created'),
'sortdirection' => new external_value(PARAM_ALPHA, 'sort direction: ASC or DESC', VALUE_DEFAULT, 'DESC')
)
);
}

/**
* Returns a list of forum posts for a discussion
*
* @param int $discussionid the post ids
* @param string $sortby sort by this element (id, created or modified)
* @param string $sortdirection sort direction: ASC or DESC
*
* @return array the forum post details
* @since Moodle 2.7
* @todo MDL-65252 This will be removed in Moodle 3.11
*/
public static function get_forum_discussion_posts($discussionid, $sortby = "created", $sortdirection = "DESC") {
global $CFG, $DB, $USER, $PAGE;

$posts = array();
$warnings = array();

// Validate the parameter.
$params = self::validate_parameters(self::get_forum_discussion_posts_parameters(),
array(
'discussionid' => $discussionid,
'sortby' => $sortby,
'sortdirection' => $sortdirection));

// Compact/extract functions are not recommended.
$discussionid = $params['discussionid'];
$sortby = $params['sortby'];
$sortdirection = $params['sortdirection'];

$sortallowedvalues = array('id', 'created', 'modified');
if (!in_array($sortby, $sortallowedvalues)) {
throw new invalid_parameter_exception('Invalid value for sortby parameter (value: ' . $sortby . '),' .
'allowed values are: ' . implode(',', $sortallowedvalues));
}

$sortdirection = strtoupper($sortdirection);
$directionallowedvalues = array('ASC', 'DESC');
if (!in_array($sortdirection, $directionallowedvalues)) {
throw new invalid_parameter_exception('Invalid value for sortdirection parameter (value: ' . $sortdirection . '),' .
'allowed values are: ' . implode(',', $directionallowedvalues));
}

$discussion = $DB->get_record('forum_discussions', array('id' => $discussionid), '*', MUST_EXIST);
$forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);

// Validate the module context. It checks everything that affects the module visibility (including groupings, etc..).
$modcontext = context_module::instance($cm->id);
self::validate_context($modcontext);

// This require must be here, see mod/forum/discuss.php.
require_once($CFG->dirroot . "/mod/forum/lib.php");

// Check they have the view forum capability.
require_capability('mod/forum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum');

if (! $post = forum_get_post_full($discussion->firstpost)) {
throw new moodle_exception('notexists', 'forum');
}

// This function check groups, qanda, timed discussions, etc.
if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
throw new moodle_exception('noviewdiscussionspermission', 'forum');
}

$canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);

// We will add this field in the response.
$canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);

$forumtracked = forum_tp_is_tracked($forum);

$sort = 'p.' . $sortby . ' ' . $sortdirection;
$allposts = forum_get_all_discussion_posts($discussion->id, $sort, $forumtracked);

foreach ($allposts as $post) {
if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm, false)) {
$warning = array();
$warning['item'] = 'post';
$warning['itemid'] = $post->id;
$warning['warningcode'] = '1';
$warning['message'] = 'You can\'t see this post';
$warnings[] = $warning;
continue;
}

// Function forum_get_all_discussion_posts adds postread field.
// Note that the value returned can be a boolean or an integer. The WS expects a boolean.
if (empty($post->postread)) {
$post->postread = false;
} else {
$post->postread = true;
}

$post->isprivatereply = !empty($post->privatereplyto);

$post->canreply = $canreply;
if (!empty($post->children)) {
$post->children = array_keys($post->children);
} else {
$post->children = array();
}

if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
// The post is available, but has been marked as deleted.
// It will still be available but filled with a placeholder.
$post->userid = null;
$post->userfullname = null;
$post->userpictureurl = null;

$post->subject = get_string('privacy:request:delete:post:subject', 'mod_forum');
$post->message = get_string('privacy:request:delete:post:message', 'mod_forum');

$post->deleted = true;
$posts[] = $post;

continue;
}
$post->deleted = false;

if (forum_is_author_hidden($post, $forum)) {
$post->userid = null;
$post->userfullname = null;
$post->userpictureurl = null;
} else {
$user = new stdclass();
$user->id = $post->userid;
$user = username_load_fields_from_object($user, $post, null, array('picture', 'imagealt', 'email'));
$post->userfullname = fullname($user, $canviewfullname);

$userpicture = new user_picture($user);
$userpicture->size = 1; // Size f1.
$post->userpictureurl = $userpicture->get_url($PAGE)->out(false);
}

$post->subject = external_format_string($post->subject, $modcontext->id);
// Rewrite embedded images URLs.
$options = array('trusted' => $post->messagetrust);
list($post->message, $post->messageformat) =
external_format_text($post->message, $post->messageformat, $modcontext->id, 'mod_forum', 'post', $post->id,
$options);

// List attachments.
if (!empty($post->attachment)) {
$post->attachments = external_util::get_area_files($modcontext->id, 'mod_forum', 'attachment', $post->id);
}
$messageinlinefiles = external_util::get_area_files($modcontext->id, 'mod_forum', 'post', $post->id);
if (!empty($messageinlinefiles)) {
$post->messageinlinefiles = $messageinlinefiles;
}
// Post tags.
$post->tags = \core_tag\external\util::get_item_tags('mod_forum', 'forum_posts', $post->id);

$posts[] = $post;
}

$result = array();
$result['posts'] = $posts;
$result['ratinginfo'] = \core_rating\external\util::get_rating_info($forum, $modcontext, 'mod_forum', 'post', $posts);
$result['warnings'] = $warnings;
return $result;
}

/**
* Describes the get_forum_discussion_posts return value.
*
* @return external_single_structure
* @since Moodle 2.7
*/
public static function get_forum_discussion_posts_returns() {
return new external_single_structure(
array(
'posts' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Post id'),
'discussion' => new external_value(PARAM_INT, 'Discussion id'),
'parent' => new external_value(PARAM_INT, 'Parent id'),
'userid' => new external_value(PARAM_INT, 'User id'),
'created' => new external_value(PARAM_INT, 'Creation time'),
'modified' => new external_value(PARAM_INT, 'Time modified'),
'mailed' => new external_value(PARAM_INT, 'Mailed?'),
'subject' => new external_value(PARAM_RAW, 'The post subject'),
'message' => new external_value(PARAM_RAW, 'The post message'),
'messageformat' => new external_format_value('message'),
'messagetrust' => new external_value(PARAM_INT, 'Can we trust?'),
'messageinlinefiles' => new external_files('post message inline files', VALUE_OPTIONAL),
'attachment' => new external_value(PARAM_RAW, 'Has attachments?'),
'attachments' => new external_files('attachments', VALUE_OPTIONAL),
'totalscore' => new external_value(PARAM_INT, 'The post message total score'),
'mailnow' => new external_value(PARAM_INT, 'Mail now?'),
'children' => new external_multiple_structure(new external_value(PARAM_INT, 'children post id')),
'canreply' => new external_value(PARAM_BOOL, 'The user can reply to posts?'),
'postread' => new external_value(PARAM_BOOL, 'The post was read'),
'userfullname' => new external_value(PARAM_TEXT, 'Post author full name'),
'userpictureurl' => new external_value(PARAM_URL, 'Post author picture.', VALUE_OPTIONAL),
'deleted' => new external_value(PARAM_BOOL, 'This post has been removed.'),
'isprivatereply' => new external_value(PARAM_BOOL, 'The post is a private reply'),
'tags' => new external_multiple_structure(
\core_tag\external\tag_item_exporter::get_read_structure(), 'Tags', VALUE_OPTIONAL
),
), 'post'
)
),
'ratinginfo' => \core_rating\external\util::external_ratings_structure(),
'warnings' => new external_warnings()
)
);
}

/**
* Mark the get_forum_discussion_posts web service as deprecated.
*
* @return bool
*/
public static function get_forum_discussion_posts_is_deprecated() {
return true;
}

/**
* Mark the get_forum_discussions_paginated web service as deprecated.
*
Expand Down

0 comments on commit 1530035

Please sign in to comment.