original_string: public static function toggle_favourite_state($discussionid, $targetstate) {
          global $DB, $PAGE, $USER;
  
          $params = self::validate_parameters(self::toggle_favourite_state_parameters(), [
              'discussionid' => $discussionid,
              'targetstate' => $targetstate
          ]);
  
          $vaultfactory = mod_forum\local\container::get_vault_factory();
          // Get the discussion vault and the corresponding discussion entity.
          $discussionvault = $vaultfactory->get_discussion_vault();
          $discussion = $discussionvault->get_from_id($params['discussionid']);
  
          $forumvault = $vaultfactory->get_forum_vault();
          $forum = $forumvault->get_from_id($discussion->get_forum_id());
          $forumcontext = $forum->get_context();
          self::validate_context($forumcontext);
  
          $managerfactory = mod_forum\local\container::get_manager_factory();
          $capabilitymanager = $managerfactory->get_capability_manager($forum);
  
          // Does the user have the ability to favourite the discussion?
          if (!$capabilitymanager->can_favourite_discussion($USER)) {
              throw new moodle_exception('cannotfavourite', 'forum');
          }
          $usercontext = context_user::instance($USER->id);
          $ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
          $isfavourited = $ufservice->favourite_exists('mod_forum', 'discussions', $discussion->get_id(), $forumcontext);
  
          $favouritefunction = $targetstate ? 'create_favourite' : 'delete_favourite';
          if ($isfavourited != (bool) $params['targetstate']) {
              $ufservice->{$favouritefunction}('mod_forum', 'discussions', $discussion->get_id(), $forumcontext);
          }
  
          $exporterfactory = mod_forum\local\container::get_exporter_factory();
          $builder = mod_forum\local\container::get_builder_factory()->get_exported_discussion_builder();
          $favourited = ($builder->is_favourited($discussion, $forumcontext, $USER) ? [$discussion->get_id()] : []);
          $exporter = $exporterfactory->get_discussion_exporter($USER, $forum, $discussion, [], $favourited);
          return $exporter->export($PAGE->get_renderer('mod_forum'));
      }
code: public static function toggle_favourite_state($discussionid, $targetstate) {
          global $DB, $PAGE, $USER;
  
          $params = self::validate_parameters(self::toggle_favourite_state_parameters(), [
              'discussionid' => $discussionid,
              'targetstate' => $targetstate
          ]);
  
          $vaultfactory = mod_forum\local\container::get_vault_factory();
          // Get the discussion vault and the corresponding discussion entity.
          $discussionvault = $vaultfactory->get_discussion_vault();
          $discussion = $discussionvault->get_from_id($params['discussionid']);
  
          $forumvault = $vaultfactory->get_forum_vault();
          $forum = $forumvault->get_from_id($discussion->get_forum_id());
          $forumcontext = $forum->get_context();
          self::validate_context($forumcontext);
  
          $managerfactory = mod_forum\local\container::get_manager_factory();
          $capabilitymanager = $managerfactory->get_capability_manager($forum);
  
          // Does the user have the ability to favourite the discussion?
          if (!$capabilitymanager->can_favourite_discussion($USER)) {
              throw new moodle_exception('cannotfavourite', 'forum');
          }
          $usercontext = context_user::instance($USER->id);
          $ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
          $isfavourited = $ufservice->favourite_exists('mod_forum', 'discussions', $discussion->get_id(), $forumcontext);
  
          $favouritefunction = $targetstate ? 'create_favourite' : 'delete_favourite';
          if ($isfavourited != (bool) $params['targetstate']) {
              $ufservice->{$favouritefunction}('mod_forum', 'discussions', $discussion->get_id(), $forumcontext);
          }
  
          $exporterfactory = mod_forum\local\container::get_exporter_factory();
          $builder = mod_forum\local\container::get_builder_factory()->get_exported_discussion_builder();
          $favourited = ($builder->is_favourited($discussion, $forumcontext, $USER) ? [$discussion->get_id()] : []);
          $exporter = $exporterfactory->get_discussion_exporter($USER, $forum, $discussion, [], $favourited);
          return $exporter->export($PAGE->get_renderer('mod_forum'));
      }
code_tokens: public static function toggle_favourite_state ( $ discussionid , $ targetstate ) { global $ DB , $ PAGE , $ USER ; $ params = self :: validate_parameters ( self :: toggle_favourite_state_parameters ( ) , [ 'discussionid' => $ discussionid , 'targetstate' => $ targetstate ] ) ; $ vaultfactory = mod_forum \ local \ container :: get_vault_factory ( ) ; // Get the discussion vault and the corresponding discussion entity. $ discussionvault = $ vaultfactory -> get_discussion_vault ( ) ; $ discussion = $ discussionvault -> get_from_id ( $ params [ 'discussionid' ] ) ; $ forumvault = $ vaultfactory -> get_forum_vault ( ) ; $ forum = $ forumvault -> get_from_id ( $ discussion -> get_forum_id ( ) ) ; $ forumcontext = $ forum -> get_context ( ) ; self :: validate_context ( $ forumcontext ) ; $ managerfactory = mod_forum \ local \ container :: get_manager_factory ( ) ; $ capabilitymanager = $ managerfactory -> get_capability_manager ( $ forum ) ; // Does the user have the ability to favourite the discussion? if ( ! $ capabilitymanager -> can_favourite_discussion ( $ USER ) ) { throw new moodle_exception ( 'cannotfavourite' , 'forum' ) ; } $ usercontext = context_user :: instance ( $ USER -> id ) ; $ ufservice = \ core_favourites \ service_factory :: get_service_for_user_context ( $ usercontext ) ; $ isfavourited = $ ufservice -> favourite_exists ( 'mod_forum' , 'discussions' , $ discussion -> get_id ( ) , $ forumcontext ) ; $ favouritefunction = $ targetstate ? 'create_favourite' : 'delete_favourite' ; if ( $ isfavourited != ( bool ) $ params [ 'targetstate' ] ) { $ ufservice -> { $ favouritefunction } ( 'mod_forum' , 'discussions' , $ discussion -> get_id ( ) , $ forumcontext ) ; } $ exporterfactory = mod_forum \ local \ container :: get_exporter_factory ( ) ; $ builder = mod_forum \ local \ container :: get_builder_factory ( ) -> get_exported_discussion_builder ( ) ; $ favourited = ( $ builder -> is_favourited ( $ discussion , $ forumcontext , $ USER ) ? [ $ discussion -> get_id ( ) ] : [ ] ) ; $ exporter = $ exporterfactory -> get_discussion_exporter ( $ USER , $ forum , $ discussion , [ ] , $ favourited ) ; return $ exporter -> export ( $ PAGE -> get_renderer ( 'mod_forum' ) ) ; }
docstring: Toggle the favouriting value for the discussion provided
  
  @param int $discussionid The discussion we need to favourite
  @param bool $targetstate The state of the favourite value
  @return array The exported discussion
docstring_tokens: Toggle the favouriting value for the discussion provided