Skip to content

Commit

Permalink
Merge branch 'MDL-70483-main' of https://github.com/kevpercy/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Feb 1, 2024
2 parents 08a4c5f + a0890de commit 13c8b7e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 487 deletions.
11 changes: 0 additions & 11 deletions mod/forum/db/services.php
Expand Up @@ -48,17 +48,6 @@
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),

'mod_forum_get_forum_discussions_paginated' => array(
'classname' => 'mod_forum_external',
'methodname' => 'get_forum_discussions_paginated',
'classpath' => 'mod/forum/externallib.php',
'description' => '** DEPRECATED ** Please do not call this function any more.
Returns a list of forum discussions optionally sorted and paginated.',
'type' => 'read',
'capabilities' => 'mod/forum:viewdiscussion, mod/forum:viewqandawithoutposting',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),

'mod_forum_get_forum_discussions' => array(
'classname' => 'mod_forum_external',
'methodname' => 'get_forum_discussions',
Expand Down
260 changes: 0 additions & 260 deletions mod/forum/externallib.php
Expand Up @@ -287,266 +287,6 @@ public static function get_discussion_posts_returns() {
]);
}

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

/**
* Describes the parameters for get_forum_discussions_paginated.
*
* @deprecated since 3.7
* @return external_function_parameters
* @since Moodle 2.8
*/
public static function get_forum_discussions_paginated_parameters() {
return new external_function_parameters (
array(
'forumid' => new external_value(PARAM_INT, 'forum instance id', VALUE_REQUIRED),
'sortby' => new external_value(PARAM_ALPHA,
'sort by this element: id, timemodified, timestart or timeend', VALUE_DEFAULT, 'timemodified'),
'sortdirection' => new external_value(PARAM_ALPHA, 'sort direction: ASC or DESC', VALUE_DEFAULT, 'DESC'),
'page' => new external_value(PARAM_INT, 'current page', VALUE_DEFAULT, -1),
'perpage' => new external_value(PARAM_INT, 'items per page', VALUE_DEFAULT, 0),
)
);
}

/**
* Returns a list of forum discussions optionally sorted and paginated.
*
* @deprecated since 3.7
* @param int $forumid the forum instance id
* @param string $sortby sort by this element (id, timemodified, timestart or timeend)
* @param string $sortdirection sort direction: ASC or DESC
* @param int $page page number
* @param int $perpage items per page
*
* @return array the forum discussion details including warnings
* @since Moodle 2.8
*/
public static function get_forum_discussions_paginated($forumid, $sortby = 'timemodified', $sortdirection = 'DESC',
$page = -1, $perpage = 0) {
global $CFG, $DB, $USER, $PAGE;

require_once($CFG->dirroot . "/mod/forum/lib.php");

$warnings = array();
$discussions = array();

$params = self::validate_parameters(self::get_forum_discussions_paginated_parameters(),
array(
'forumid' => $forumid,
'sortby' => $sortby,
'sortdirection' => $sortdirection,
'page' => $page,
'perpage' => $perpage
)
);

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

$sortallowedvalues = array('id', 'timemodified', 'timestart', 'timeend');
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));
}

$forum = $DB->get_record('forum', array('id' => $forumid), '*', 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);

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

$sort = 'd.pinned DESC, d.' . $sortby . ' ' . $sortdirection;
$alldiscussions = forum_get_discussions($cm, $sort, true, -1, -1, true, $page, $perpage, FORUM_POSTS_ALL_USER_GROUPS);

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

// Get the unreads array, this takes a forum id and returns data for all discussions.
$unreads = array();
if ($cantrack = forum_tp_can_track_forums($forum)) {
if ($forumtracked = forum_tp_is_tracked($forum)) {
$unreads = forum_get_discussions_unread($cm);
}
}
// The forum function returns the replies for all the discussions in a given forum.
$canseeprivatereplies = has_capability('mod/forum:readprivatereplies', $modcontext);
$canlock = has_capability('moodle/course:manageactivities', $modcontext, $USER);
$replies = forum_count_discussion_replies($forumid, $sort, -1, $page, $perpage, $canseeprivatereplies);

foreach ($alldiscussions as $discussion) {

// This function checks for qanda forums.
// Note that the forum_get_discussions returns as id the post id, not the discussion id so we need to do this.
$discussionrec = clone $discussion;
$discussionrec->id = $discussion->discussion;
if (!forum_user_can_see_discussion($forum, $discussionrec, $modcontext)) {
$warning = array();
// Function forum_get_discussions returns forum_posts ids not forum_discussions ones.
$warning['item'] = 'post';
$warning['itemid'] = $discussion->id;
$warning['warningcode'] = '1';
$warning['message'] = 'You can\'t see this discussion';
$warnings[] = $warning;
continue;
}

$discussion->numunread = 0;
if ($cantrack && $forumtracked) {
if (isset($unreads[$discussion->discussion])) {
$discussion->numunread = (int) $unreads[$discussion->discussion];
}
}

$discussion->numreplies = 0;
if (!empty($replies[$discussion->discussion])) {
$discussion->numreplies = (int) $replies[$discussion->discussion]->replies;
}

$discussion->name = \core_external\util::format_string($discussion->name, $modcontext);
$discussion->subject = \core_external\util::format_string($discussion->subject, $modcontext);
// Rewrite embedded images URLs.
$options = array('trusted' => $discussion->messagetrust);
list($discussion->message, $discussion->messageformat) =
\core_external\util::format_text($discussion->message, $discussion->messageformat,
$modcontext, 'mod_forum', 'post', $discussion->id, $options);

// List attachments.
if (!empty($discussion->attachment)) {
$discussion->attachments = external_util::get_area_files($modcontext->id, 'mod_forum', 'attachment',
$discussion->id);
}
$messageinlinefiles = external_util::get_area_files($modcontext->id, 'mod_forum', 'post', $discussion->id);
if (!empty($messageinlinefiles)) {
$discussion->messageinlinefiles = $messageinlinefiles;
}

$discussion->locked = forum_discussion_is_locked($forum, $discussion);
$discussion->canlock = $canlock;
$discussion->canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);

if (forum_is_author_hidden($discussion, $forum)) {
$discussion->userid = null;
$discussion->userfullname = null;
$discussion->userpictureurl = null;

$discussion->usermodified = null;
$discussion->usermodifiedfullname = null;
$discussion->usermodifiedpictureurl = null;
} else {
$picturefields = explode(',', implode(',', \core_user\fields::get_picture_fields()));

// Load user objects from the results of the query.
$user = new stdclass();
$user->id = $discussion->userid;
$user = username_load_fields_from_object($user, $discussion, null, $picturefields);
// Preserve the id, it can be modified by username_load_fields_from_object.
$user->id = $discussion->userid;
$discussion->userfullname = fullname($user, $canviewfullname);

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

$usermodified = new stdclass();
$usermodified->id = $discussion->usermodified;
$usermodified = username_load_fields_from_object($usermodified, $discussion, 'um', $picturefields);
// Preserve the id (it can be overwritten due to the prefixed $picturefields).
$usermodified->id = $discussion->usermodified;
$discussion->usermodifiedfullname = fullname($usermodified, $canviewfullname);

$userpicture = new user_picture($usermodified);
$userpicture->size = 1; // Size f1.
$discussion->usermodifiedpictureurl = $userpicture->get_url($PAGE)->out(false);
}

$discussions[] = $discussion;
}
}

$result = array();
$result['discussions'] = $discussions;
$result['warnings'] = $warnings;
return $result;

}

/**
* Describes the get_forum_discussions_paginated return value.
*
* @deprecated since 3.7
* @return external_single_structure
* @since Moodle 2.8
*/
public static function get_forum_discussions_paginated_returns() {
return new external_single_structure(
array(
'discussions' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Post id'),
'name' => new external_value(PARAM_RAW, 'Discussion name'),
'groupid' => new external_value(PARAM_INT, 'Group id'),
'timemodified' => new external_value(PARAM_INT, 'Time modified'),
'usermodified' => new external_value(PARAM_INT, 'The id of the user who last modified'),
'timestart' => new external_value(PARAM_INT, 'Time discussion can start'),
'timeend' => new external_value(PARAM_INT, 'Time discussion ends'),
'discussion' => new external_value(PARAM_INT, 'Discussion id'),
'parent' => new external_value(PARAM_INT, 'Parent id'),
'userid' => new external_value(PARAM_INT, 'User who started the discussion 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?'),
'userfullname' => new external_value(PARAM_TEXT, 'Post author full name'),
'usermodifiedfullname' => new external_value(PARAM_TEXT, 'Post modifier full name'),
'userpictureurl' => new external_value(PARAM_URL, 'Post author picture.'),
'usermodifiedpictureurl' => new external_value(PARAM_URL, 'Post modifier picture.'),
'numreplies' => new external_value(PARAM_INT, 'The number of replies in the discussion'),
'numunread' => new external_value(PARAM_INT, 'The number of unread discussions.'),
'pinned' => new external_value(PARAM_BOOL, 'Is the discussion pinned'),
'locked' => new external_value(PARAM_BOOL, 'Is the discussion locked'),
'canreply' => new external_value(PARAM_BOOL, 'Can the user reply to the discussion'),
'canlock' => new external_value(PARAM_BOOL, 'Can the user lock the discussion'),
), 'post'
)
),
'warnings' => new external_warnings()
)
);
}

/**
* Describes the parameters for get_forum_discussions.
*
Expand Down

0 comments on commit 13c8b7e

Please sign in to comment.