original_string: public static function subscribe_user_to_discussion($userid, $discussion, $context = null) {
          global $DB;
  
          // First check whether the user is subscribed to the discussion already.
          $subscription = $DB->get_record('forum_discussion_subs', array('userid' => $userid, 'discussion' => $discussion->id));
          if ($subscription) {
              if ($subscription->preference != self::FORUM_DISCUSSION_UNSUBSCRIBED) {
                  // The user is already subscribed to the discussion. Ignore.
                  return false;
              }
          }
          // No discussion-level subscription. Check for a forum level subscription.
          if ($DB->record_exists('forum_subscriptions', array('userid' => $userid, 'forum' => $discussion->forum))) {
              if ($subscription && $subscription->preference == self::FORUM_DISCUSSION_UNSUBSCRIBED) {
                  // The user is subscribed to the forum, but unsubscribed from the discussion, delete the discussion preference.
                  $DB->delete_records('forum_discussion_subs', array('id' => $subscription->id));
                  unset(self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id]);
              } else {
                  // The user is already subscribed to the forum. Ignore.
                  return false;
              }
          } else {
              if ($subscription) {
                  $subscription->preference = time();
                  $DB->update_record('forum_discussion_subs', $subscription);
              } else {
                  $subscription = new \stdClass();
                  $subscription->userid  = $userid;
                  $subscription->forum = $discussion->forum;
                  $subscription->discussion = $discussion->id;
                  $subscription->preference = time();
  
                  $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
                  self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id] = $subscription->preference;
              }
          }
  
          $context = forum_get_context($discussion->forum, $context);
          $params = array(
              'context' => $context,
              'objectid' => $subscription->id,
              'relateduserid' => $userid,
              'other' => array(
                  'forumid' => $discussion->forum,
                  'discussion' => $discussion->id,
              ),
  
          );
          $event  = event\discussion_subscription_created::create($params);
          $event->trigger();
  
          return true;
      }
code: public static function subscribe_user_to_discussion($userid, $discussion, $context = null) {
          global $DB;
  
          // First check whether the user is subscribed to the discussion already.
          $subscription = $DB->get_record('forum_discussion_subs', array('userid' => $userid, 'discussion' => $discussion->id));
          if ($subscription) {
              if ($subscription->preference != self::FORUM_DISCUSSION_UNSUBSCRIBED) {
                  // The user is already subscribed to the discussion. Ignore.
                  return false;
              }
          }
          // No discussion-level subscription. Check for a forum level subscription.
          if ($DB->record_exists('forum_subscriptions', array('userid' => $userid, 'forum' => $discussion->forum))) {
              if ($subscription && $subscription->preference == self::FORUM_DISCUSSION_UNSUBSCRIBED) {
                  // The user is subscribed to the forum, but unsubscribed from the discussion, delete the discussion preference.
                  $DB->delete_records('forum_discussion_subs', array('id' => $subscription->id));
                  unset(self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id]);
              } else {
                  // The user is already subscribed to the forum. Ignore.
                  return false;
              }
          } else {
              if ($subscription) {
                  $subscription->preference = time();
                  $DB->update_record('forum_discussion_subs', $subscription);
              } else {
                  $subscription = new \stdClass();
                  $subscription->userid  = $userid;
                  $subscription->forum = $discussion->forum;
                  $subscription->discussion = $discussion->id;
                  $subscription->preference = time();
  
                  $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
                  self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id] = $subscription->preference;
              }
          }
  
          $context = forum_get_context($discussion->forum, $context);
          $params = array(
              'context' => $context,
              'objectid' => $subscription->id,
              'relateduserid' => $userid,
              'other' => array(
                  'forumid' => $discussion->forum,
                  'discussion' => $discussion->id,
              ),
  
          );
          $event  = event\discussion_subscription_created::create($params);
          $event->trigger();
  
          return true;
      }
code_tokens: public static function subscribe_user_to_discussion ( $ userid , $ discussion , $ context = null ) { global $ DB ; // First check whether the user is subscribed to the discussion already. $ subscription = $ DB -> get_record ( 'forum_discussion_subs' , array ( 'userid' => $ userid , 'discussion' => $ discussion -> id ) ) ; if ( $ subscription ) { if ( $ subscription -> preference != self :: FORUM_DISCUSSION_UNSUBSCRIBED ) { // The user is already subscribed to the discussion. Ignore. return false ; } } // No discussion-level subscription. Check for a forum level subscription. if ( $ DB -> record_exists ( 'forum_subscriptions' , array ( 'userid' => $ userid , 'forum' => $ discussion -> forum ) ) ) { if ( $ subscription && $ subscription -> preference == self :: FORUM_DISCUSSION_UNSUBSCRIBED ) { // The user is subscribed to the forum, but unsubscribed from the discussion, delete the discussion preference. $ DB -> delete_records ( 'forum_discussion_subs' , array ( 'id' => $ subscription -> id ) ) ; unset ( self :: $ forumdiscussioncache [ $ userid ] [ $ discussion -> forum ] [ $ discussion -> id ] ) ; } else { // The user is already subscribed to the forum. Ignore. return false ; } } else { if ( $ subscription ) { $ subscription -> preference = time ( ) ; $ DB -> update_record ( 'forum_discussion_subs' , $ subscription ) ; } else { $ subscription = new \ stdClass ( ) ; $ subscription -> userid = $ userid ; $ subscription -> forum = $ discussion -> forum ; $ subscription -> discussion = $ discussion -> id ; $ subscription -> preference = time ( ) ; $ subscription -> id = $ DB -> insert_record ( 'forum_discussion_subs' , $ subscription ) ; self :: $ forumdiscussioncache [ $ userid ] [ $ discussion -> forum ] [ $ discussion -> id ] = $ subscription -> preference ; } } $ context = forum_get_context ( $ discussion -> forum , $ context ) ; $ params = array ( 'context' => $ context , 'objectid' => $ subscription -> id , 'relateduserid' => $ userid , 'other' => array ( 'forumid' => $ discussion -> forum , 'discussion' => $ discussion -> id , ) , ) ; $ event = event \ discussion_subscription_created :: create ( $ params ) ; $ event -> trigger ( ) ; return true ; }
docstring: Subscribes the user to the specified discussion.
  
  @param int $userid The userid of the user being subscribed
  @param \stdClass $discussion The discussion to subscribe to
  @param \context_module|null $context Module context, may be omitted if not known or if called for the current
  module set in page.
  @return boolean Whether a change was made
docstring_tokens: Subscribes the user to the specified discussion .