Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core_contentbank\external; 18 19 use core_external\external_api; 20 use core_external\external_function_parameters; 21 use core_external\external_single_structure; 22 use core_external\external_value; 23 use core_external\external_warnings; 24 25 /** 26 * External API to set the visibility of content bank content. 27 * 28 * @package core_contentbank 29 * @copyright 2020 François Moreau 30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 31 */ 32 class set_content_visibility extends external_api { 33 /** 34 * set_content_visibility parameters. 35 * 36 * @since Moodle 3.11 37 * @return external_function_parameters 38 */ 39 public static function execute_parameters(): external_function_parameters { 40 return new external_function_parameters( 41 [ 42 'contentid' => new external_value(PARAM_INT, 'The content id to rename', VALUE_REQUIRED), 43 'visibility' => new external_value(PARAM_INT, 'The new visibility for the content', VALUE_REQUIRED), 44 ] 45 ); 46 } 47 48 /** 49 * Set visibility of a content from the contentbank. 50 * 51 * @since Moodle 3.11 52 * @param int $contentid The content id to rename. 53 * @param int $visibility The new visibility. 54 * @return array 55 */ 56 public static function execute(int $contentid, int $visibility): array { 57 global $DB; 58 59 $result = false; 60 $warnings = []; 61 62 $params = self::validate_parameters(self::execute_parameters(), [ 63 'contentid' => $contentid, 64 'visibility' => $visibility, 65 ]); 66 67 try { 68 $record = $DB->get_record('contentbank_content', ['id' => $params['contentid']], '*', MUST_EXIST); 69 $contenttypeclass = "\\$record->contenttype\\contenttype"; 70 if (class_exists($contenttypeclass)) { 71 $context = \context::instance_by_id($record->contextid, MUST_EXIST); 72 self::validate_context($context); 73 $contenttype = new $contenttypeclass($context); 74 $contentclass = "\\$record->contenttype\\content"; 75 $content = new $contentclass($record); 76 // Check capability. 77 if ($contenttype->can_manage($content)) { 78 // This content's visibility can be changed. 79 if ($content->set_visibility($params['visibility'])) { 80 $result = true; 81 } else { 82 $warnings[] = [ 83 'item' => $params['contentid'], 84 'warningcode' => 'contentvisibilitynotset', 85 'message' => get_string('contentvisibilitynotset', 'core_contentbank') 86 ]; 87 } 88 89 } else { 90 // The user has no permission to manage this content. 91 $warnings[] = [ 92 'item' => $params['contentid'], 93 'warningcode' => 'nopermissiontomanage', 94 'message' => get_string('nopermissiontomanage', 'core_contentbank') 95 ]; 96 } 97 } 98 } catch (\moodle_exception $e) { 99 // The content or the context don't exist. 100 $warnings[] = [ 101 'item' => $params['contentid'], 102 'warningcode' => 'exception', 103 'message' => $e->getMessage() 104 ]; 105 } 106 107 return [ 108 'result' => $result, 109 'warnings' => $warnings 110 ]; 111 } 112 113 /** 114 * set_content_visibility return. 115 * 116 * @since Moodle 3.11 117 * @return external_single_structure 118 */ 119 public static function execute_returns(): external_single_structure { 120 return new external_single_structure([ 121 'result' => new external_value(PARAM_BOOL, 'The processing result'), 122 'warnings' => new external_warnings() 123 ]); 124 } 125 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body