original_string: public static function subscribe_user($userid, $forum, $context = null, $userrequest = false) {
          global $DB;
  
          if (self::is_subscribed($userid, $forum)) {
              return true;
          }
  
          $sub = new \stdClass();
          $sub->userid  = $userid;
          $sub->forum = $forum->id;
  
          $result = $DB->insert_record("forum_subscriptions", $sub);
  
          if ($userrequest) {
              $discussionsubscriptions = $DB->get_recordset('forum_discussion_subs', array('userid' => $userid, 'forum' => $forum->id));
              $DB->delete_records_select('forum_discussion_subs',
                      'userid = :userid AND forum = :forumid AND preference <> :preference', array(
                          'userid' => $userid,
                          'forumid' => $forum->id,
                          'preference' => self::FORUM_DISCUSSION_UNSUBSCRIBED,
                      ));
  
              // Reset the subscription caches for this forum.
              // We know that the there were previously entries and there aren't any more.
              if (isset(self::$forumdiscussioncache[$userid]) && isset(self::$forumdiscussioncache[$userid][$forum->id])) {
                  foreach (self::$forumdiscussioncache[$userid][$forum->id] as $discussionid => $preference) {
                      if ($preference != self::FORUM_DISCUSSION_UNSUBSCRIBED) {
                          unset(self::$forumdiscussioncache[$userid][$forum->id][$discussionid]);
                      }
                  }
              }
          }
  
          // Reset the cache for this forum.
          self::$forumcache[$userid][$forum->id] = true;
  
          $context = forum_get_context($forum->id, $context);
          $params = array(
              'context' => $context,
              'objectid' => $result,
              'relateduserid' => $userid,
              'other' => array('forumid' => $forum->id),
  
          );
          $event  = event\subscription_created::create($params);
          if ($userrequest && $discussionsubscriptions) {
              foreach ($discussionsubscriptions as $subscription) {
                  $event->add_record_snapshot('forum_discussion_subs', $subscription);
              }
              $discussionsubscriptions->close();
          }
          $event->trigger();
  
          return $result;
      }
code: public static function subscribe_user($userid, $forum, $context = null, $userrequest = false) {
          global $DB;
  
          if (self::is_subscribed($userid, $forum)) {
              return true;
          }
  
          $sub = new \stdClass();
          $sub->userid  = $userid;
          $sub->forum = $forum->id;
  
          $result = $DB->insert_record("forum_subscriptions", $sub);
  
          if ($userrequest) {
              $discussionsubscriptions = $DB->get_recordset('forum_discussion_subs', array('userid' => $userid, 'forum' => $forum->id));
              $DB->delete_records_select('forum_discussion_subs',
                      'userid = :userid AND forum = :forumid AND preference <> :preference', array(
                          'userid' => $userid,
                          'forumid' => $forum->id,
                          'preference' => self::FORUM_DISCUSSION_UNSUBSCRIBED,
                      ));
  
              // Reset the subscription caches for this forum.
              // We know that the there were previously entries and there aren't any more.
              if (isset(self::$forumdiscussioncache[$userid]) && isset(self::$forumdiscussioncache[$userid][$forum->id])) {
                  foreach (self::$forumdiscussioncache[$userid][$forum->id] as $discussionid => $preference) {
                      if ($preference != self::FORUM_DISCUSSION_UNSUBSCRIBED) {
                          unset(self::$forumdiscussioncache[$userid][$forum->id][$discussionid]);
                      }
                  }
              }
          }
  
          // Reset the cache for this forum.
          self::$forumcache[$userid][$forum->id] = true;
  
          $context = forum_get_context($forum->id, $context);
          $params = array(
              'context' => $context,
              'objectid' => $result,
              'relateduserid' => $userid,
              'other' => array('forumid' => $forum->id),
  
          );
          $event  = event\subscription_created::create($params);
          if ($userrequest && $discussionsubscriptions) {
              foreach ($discussionsubscriptions as $subscription) {
                  $event->add_record_snapshot('forum_discussion_subs', $subscription);
              }
              $discussionsubscriptions->close();
          }
          $event->trigger();
  
          return $result;
      }
code_tokens: public static function subscribe_user ( $ userid , $ forum , $ context = null , $ userrequest = false ) { global $ DB ; if ( self :: is_subscribed ( $ userid , $ forum ) ) { return true ; } $ sub = new \ stdClass ( ) ; $ sub -> userid = $ userid ; $ sub -> forum = $ forum -> id ; $ result = $ DB -> insert_record ( "forum_subscriptions" , $ sub ) ; if ( $ userrequest ) { $ discussionsubscriptions = $ DB -> get_recordset ( 'forum_discussion_subs' , array ( 'userid' => $ userid , 'forum' => $ forum -> id ) ) ; $ DB -> delete_records_select ( 'forum_discussion_subs' , 'userid = :userid AND forum = :forumid AND preference <> :preference' , array ( 'userid' => $ userid , 'forumid' => $ forum -> id , 'preference' => self :: FORUM_DISCUSSION_UNSUBSCRIBED , ) ) ; // Reset the subscription caches for this forum. // We know that the there were previously entries and there aren't any more. if ( isset ( self :: $ forumdiscussioncache [ $ userid ] ) && isset ( self :: $ forumdiscussioncache [ $ userid ] [ $ forum -> id ] ) ) { foreach ( self :: $ forumdiscussioncache [ $ userid ] [ $ forum -> id ] as $ discussionid => $ preference ) { if ( $ preference != self :: FORUM_DISCUSSION_UNSUBSCRIBED ) { unset ( self :: $ forumdiscussioncache [ $ userid ] [ $ forum -> id ] [ $ discussionid ] ) ; } } } } // Reset the cache for this forum. self :: $ forumcache [ $ userid ] [ $ forum -> id ] = true ; $ context = forum_get_context ( $ forum -> id , $ context ) ; $ params = array ( 'context' => $ context , 'objectid' => $ result , 'relateduserid' => $ userid , 'other' => array ( 'forumid' => $ forum -> id ) , ) ; $ event = event \ subscription_created :: create ( $ params ) ; if ( $ userrequest && $ discussionsubscriptions ) { foreach ( $ discussionsubscriptions as $ subscription ) { $ event -> add_record_snapshot ( 'forum_discussion_subs' , $ subscription ) ; } $ discussionsubscriptions -> close ( ) ; } $ event -> trigger ( ) ; return $ result ; }
docstring: Adds user to the subscriber list.
  
  @param int $userid The ID of the user to subscribe
  @param \stdClass $forum The forum record for this forum.
  @param \context_module|null $context Module context, may be omitted if not known or if called for the current
  module set in page.
  @param boolean $userrequest Whether the user requested this change themselves. This has an effect on whether
  discussion subscriptions are removed too.
  @return bool|int Returns true if the user is already subscribed, or the forum_subscriptions ID if the user was
  successfully subscribed.
docstring_tokens: Adds user to the subscriber list .