/**
  * Obtains the list of people who are forced to subscribe to the forum
  * (if forced) or are by default subscribed (if initial).
  * @param int $groupid If specified, restricts list to this group id
  * @return array Array of partial user objects (with enough info to send
  *   email and display them)
  */
 public function get_auto_subscribers($groupid = forum::ALL_GROUPS)
 {
     global $CFG;
     switch ($this->get_effective_subscription_option()) {
         case self::SUBSCRIPTION_FORCED:
         case self::SUBSCRIPTION_INITIALLY_SUBSCRIBED:
             break;
         default:
             return array();
     }
     $groups = $this->get_permitted_groups();
     $context = $this->get_context();
     // Get all users (limited to the specified groups if applicable)
     // who are allowed to view discussions in this forum
     $fields = '';
     foreach (forum_utils::get_username_fields(true) as $field) {
         if ($fields) {
             $fields .= ',';
         }
         $fields .= 'u.' . $field;
     }
     $users = get_users_by_capability($context, 'mod/forumng:viewdiscussion', $fields, '', '', '', $groups, '', false);
     $users = $users ? $users : array();
     // Now filter list to include only people in the subscriberoles
     // list. Big IN clauses can be slow, so rather than doing that,
     // let's just get the list of people on those roles, and then
     // intersect in PHP. It's a shame you can't add
     // joins/restrictions to get_users_by_capability :(
     $roleids = forum_utils::safe_explode(',', $CFG->forumng_subscriberoles);
     $roleidcheck = forum_utils::in_or_equals($roleids);
     $contextids = forum_utils::safe_explode('/', $context->path);
     $contextidcheck = forum_utils::in_or_equals($contextids);
     if ($groupid == forum::ALL_GROUPS || $groupid == forum::NO_GROUPS) {
         $groupcheck = '';
     } else {
         $groupcheck = "INNER JOIN {$CFG->prefix}groups_members gm ON gm.userid=ra.userid AND gm.groupid={$groupid}";
     }
     $rs = get_recordset_sql("\nSELECT\n    ra.userid\nFROM\n    {$CFG->prefix}role_assignments ra\n    {$groupcheck}\nWHERE\n    contextid {$contextidcheck} AND roleid {$roleidcheck}");
     if (!$rs) {
         throw new forum_exception('Failed to get users with subscribe role');
     }
     $allowedusers = array();
     while ($rec = rs_fetch_next_record($rs)) {
         $allowedusers[$rec->userid] = true;
     }
     rs_close($rs);
     foreach ($users as $id => $user) {
         if (!array_key_exists($id, $allowedusers)) {
             unset($users[$id]);
         }
     }
     return $users;
 }