MDL-65608 forum: check if group exists in discussion exporter
authorRyan Wyllie <ryan@moodle.com>
Wed, 15 May 2019 05:57:09 +0000 (13:57 +0800)
committerRyan Wyllie <ryan@moodle.com>
Thu, 16 May 2019 01:50:38 +0000 (09:50 +0800)
mod/forum/classes/local/exporters/discussion.php

index 690a24f..16d682a 100644 (file)
@@ -166,23 +166,31 @@ class discussion extends exporter {
         $discussion = $this->discussion;
 
         $groupdata = null;
         $discussion = $this->discussion;
 
         $groupdata = null;
-        if ($discussion->has_group() && $group = $this->related['groupsbyid'][$discussion->get_group_id()]) {
-            $groupdata = [
-                'name' => $group->name,
-                'urls' => [],
-            ];
-            $canviewparticipants = $capabilitymanager->can_view_participants($user, $discussion);
-            if (!$group->hidepicture) {
-                $url = get_group_picture_url($group, $forum->get_course_id());
-                if (!empty($url)) {
-                    $groupdata['urls']['picture'] = $url;
+        if ($discussion->has_group()) {
+            $groupsbyid = $this->related['groupsbyid'];
+            $group = $groupsbyid[$discussion->get_group_id()] ?? null;
+
+            // We may not have received the group if the caller doesn't want to include it in the export
+            // or if it's been deleted and the discussion record hasn't been updated.
+            if ($group) {
+                $groupdata = [
+                    'name' => $group->name,
+                    'urls' => [],
+                ];
+
+                if (!$group->hidepicture) {
+                    $url = get_group_picture_url($group, $forum->get_course_id());
+                    if (!empty($url)) {
+                        $groupdata['urls']['picture'] = $url;
+                    }
+                }
+
+                if ($capabilitymanager->can_view_participants($user, $discussion)) {
+                    $groupdata['urls']['userlist'] = (new \moodle_url('/user/index.php', [
+                        'id' => $forum->get_course_id(),
+                        'group' => $group->id,
+                    ]));
                 }
                 }
-            }
-            if ($canviewparticipants) {
-                $groupdata['urls']['userlist'] = (new \moodle_url('/user/index.php', [
-                    'id' => $forum->get_course_id(),
-                    'group' => $group->id,
-                ]));
             }
         }
 
             }
         }