diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 6a5853e1efb..12daa8fc864 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -1617,6 +1617,43 @@ class mod_forum_external extends external_api { $discussion->toggle_locked_state($lockedvalue); $response = $discussionvault->update_discussion($discussion); $discussion = !$response ? $response : $discussion; + $exporterfactory = mod_forum\local\container::get_exporter_factory(); + $exporter = $exporterfactory->get_discussion_exporter($USER, $forum, $discussion); + return $exporter->export($PAGE->get_renderer('mod_forum')); + } + + /** + * Set the pin state. + * + * @param int $forumid + * @param int $discussionid + * @param bool $targetstate + * @return \stdClass + */ + public static function set_pin_state($forumid, $discussionid, $targetstate) { + global $PAGE, $USER; + $params = self::validate_parameters(self::set_pin_state_parameters(), [ + 'forumid' => $forumid, + 'discussionid' => $discussionid, + 'targetstate' => $targetstate, + ]); + $vaultfactory = mod_forum\local\container::get_vault_factory(); + $forumvault = $vaultfactory->get_forum_vault(); + $discussionvault = $vaultfactory->get_discussion_vault(); + $forum = $forumvault->get_from_id($params['forumid']); + + self::validate_context($forum->get_context()); + if (!$forum->can_subscribe()) { + // Nothing to do. We won't actually output any content here though. + throw new \moodle_exception('cannotsubscribe', 'mod_forum'); + } + + $discussion = $discussionvault->get_from_id($params['discussionid']); + $discussion->set_pinned($targetstate); + + $legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory(); + $discussionrecord = $legacydatamapperfactory->get_discussion_data_mapper()->to_legacy_object($discussion); + $discussionvault->update_discussion($discussion); $exporterfactory = mod_forum\local\container::get_exporter_factory(); $exporter = $exporterfactory->get_discussion_exporter($USER, $forum, $discussion); @@ -1638,6 +1675,21 @@ class mod_forum_external extends external_api { ); } + /** + * Returns description of method parameters. + * + * @return external_function_parameters + */ + public static function set_pin_state_parameters() { + return new external_function_parameters( + [ + 'forumid' => new external_value(PARAM_INT, 'Forum that the discussion is in', VALUE_REQUIRED, null, NULL_NOT_ALLOWED), + 'discussionid' => new external_value(PARAM_INT, 'The discussion to pin or unpin', VALUE_REQUIRED, null, NULL_NOT_ALLOWED), + 'targetstate' => new external_value(PARAM_INT, 'The target state', VALUE_REQUIRED, null, NULL_NOT_ALLOWED), + ] + ); + } + /** * Returns description of method result value. * @@ -1645,11 +1697,20 @@ class mod_forum_external extends external_api { */ public static function set_lock_state_returns() { return new external_single_structure([ - 'id' => new external_value(PARAM_INT, 'The discussion we are locking.'), - 'locked' => new external_value(PARAM_BOOL, 'The locked state of the discussion.'), - 'times' => new external_single_structure([ - 'locked' => new external_value(PARAM_INT, 'The locked time of the discussion.'), - ]) + 'id' => new external_value(PARAM_INT, 'The discussion we are locking.'), + 'locked' => new external_value(PARAM_BOOL, 'The locked state of the discussion.'), + 'times' => new external_single_structure([ + 'locked' => new external_value(PARAM_INT, 'The locked time of the discussion.'), + ]) ]); } + + /** + * Returns description of method result value. + * + * @return external_single_structure + */ + public static function set_pin_state_returns() { + return \mod_forum\local\exporters\discussion::get_read_structure(); + } }