MDL-65071 forum: Include starred discussions info in the web service
authorMihail Geshoski <mihail@moodle.com>
Wed, 1 May 2019 07:56:33 +0000 (15:56 +0800)
committerMihail Geshoski <mihail@moodle.com>
Wed, 1 May 2019 14:10:12 +0000 (22:10 +0800)
mod/forum/externallib.php

index c346151..100a50d 100644 (file)
@@ -594,11 +594,6 @@ class mod_forum_external extends external_api {
             $canlock = has_capability('moodle/course:manageactivities', $modcontext, $USER);
             $replies = forum_count_discussion_replies($forumid, $sort, -1, $page, $perpage, $canseeprivatereplies);
 
-            if (isloggedin()) {
-                $usercontext = context_user::instance($USER->id);
-                $ufservice = core_favourites\service_factory::get_service_for_user_context($usercontext);
-            }
-            $canfavourite = has_capability('mod/forum:cantogglefavourite', $modcontext, $USER);
             foreach ($alldiscussions as $discussion) {
 
                 // This function checks for qanda forums.
@@ -647,10 +642,7 @@ class mod_forum_external extends external_api {
 
                 $discussion->locked = forum_discussion_is_locked($forum, $discussion);
                 $discussion->canlock = $canlock;
-                $discussion->starred = !empty($ufservice) ? $ufservice->favourite_exists('mod_forum', 'discussions',
-                    $discussionrec->id, $modcontext) : false;
                 $discussion->canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
-                $discussion->canfavourite = $canfavourite;
 
                 if (forum_is_author_hidden($discussion, $forum)) {
                     $discussion->userid = null;
@@ -741,10 +733,8 @@ class mod_forum_external extends external_api {
                                 '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'),
-                                'starred' => new external_value(PARAM_BOOL, 'Is the discussion starred'),
                                 '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'),
-                                'canfavourite' => new external_value(PARAM_BOOL, 'Can the user star the discussion'),
                             ), 'post'
                         )
                     ),
@@ -880,6 +870,11 @@ class mod_forum_external extends external_api {
 
             $canlock = $capabilitymanager->can_manage_forum($USER);
 
+            $usercontext = context_user::instance($USER->id);
+            $ufservice = core_favourites\service_factory::get_service_for_user_context($usercontext);
+
+            $canfavourite = has_capability('mod/forum:cantogglefavourite', $modcontext, $USER);
+
             foreach ($alldiscussions as $discussionsummary) {
                 $discussion = $discussionsummary->get_discussion();
                 $firstpostauthor = $discussionsummary->get_first_post_author();
@@ -938,7 +933,10 @@ class mod_forum_external extends external_api {
 
                 $discussionobject->locked = $forum->is_discussion_locked($discussion);
                 $discussionobject->canlock = $canlock;
+                $discussionobject->starred = !empty($ufservice) ? $ufservice->favourite_exists('mod_forum', 'discussions',
+                    $discussion->get_id(), $modcontext) : false;
                 $discussionobject->canreply = $capabilitymanager->can_post_in_discussion($USER, $discussion);
+                $discussionobject->canfavourite = $canfavourite;
 
                 if (forum_is_author_hidden($discussionobject, $forumrecord)) {
                     $discussionobject->userid = null;
@@ -1011,8 +1009,10 @@ class mod_forum_external extends external_api {
                             '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'),
+                            'starred' => new external_value(PARAM_BOOL, 'Is the discussion starred'),
                             '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'),
+                            'canfavourite' => new external_value(PARAM_BOOL, 'Can the user star the discussion'),
                         ), 'post'
                     )
                 ),