/**
  * Gets all users within this forum who are supposed to be 'monitored'
  * (that means users who are in the monitorroles setting).
  *
  * Note: In Moodle 2 we should be able to replace this with getting the
  * enrolled users for the course, I think?
  * @param int $groupid Group ID or ALL_GROUPS/NO_GROUPS to get all users
  */
 function get_monitored_users($groupid)
 {
     global $CFG;
     if ($groupid > 0) {
         //Get all users from the chosen group
         $sql = "SELECT u.id, u.lastname, u.firstname, u.username, gm.groupid\n                FROM " . $CFG->prefix . "groups_members gm\n                JOIN " . $CFG->prefix . "user u ON u.id = gm.userid\n                WHERE gm.groupid = {$groupid} \n                ORDER BY u.lastname, u.firstname";
         if ($users = get_records_sql($sql)) {
             return $users;
         }
     } else {
         // Get roleids from the monitor roles setting
         if (!($roleids = forum_utils::safe_explode(',', $CFG->forumng_monitorroles))) {
             return array();
         }
         $roleidfind = forum_utils::in_or_equals($roleids);
         $context = $this->get_context();
         $contextids = forum_utils::safe_explode('/', $context->path);
         $contextidfind = forum_utils::in_or_equals($contextids);
         $sql = "SELECT u.id, u.lastname, u.firstname, u.username\n                    FROM " . $CFG->prefix . "role_assignments ra\n                    JOIN " . $CFG->prefix . "user u ON u.id = ra.userid\n                    WHERE ra.roleid {$roleidfind} AND ra.contextid {$contextidfind}\n                    ORDER BY u.lastname, u.firstname";
         if ($users = get_records_sql($sql)) {
             return $users;
         }
     }
     return array();
 }