Differences Between: [Versions 311 and 402] [Versions 311 and 403]
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 defined('MOODLE_INTERNAL') || die(); 20 21 global $CFG; 22 require_once($CFG->libdir . '/externallib.php'); 23 24 use external_api; 25 use external_function_parameters; 26 use external_single_structure; 27 use external_value; 28 use external_warnings; 29 30 /** 31 * External API to set the visibility of content bank content. 32 * 33 * @package core_contentbank 34 * @copyright 2020 François Moreau 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class set_content_visibility extends external_api { 38 /** 39 * set_content_visibility parameters. 40 * 41 * @since Moodle 3.11 42 * @return external_function_parameters 43 */ 44 public static function execute_parameters(): external_function_parameters { 45 return new external_function_parameters( 46 [ 47 'contentid' => new external_value(PARAM_INT, 'The content id to rename', VALUE_REQUIRED), 48 'visibility' => new external_value(PARAM_INT, 'The new visibility for the content', VALUE_REQUIRED), 49 ] 50 ); 51 } 52 53 /** 54 * Set visibility of a content from the contentbank. 55 * 56 * @since Moodle 3.11 57 * @param int $contentid The content id to rename. 58 * @param int $visibility The new visibility. 59 * @return array 60 */ 61 public static function execute(int $contentid, int $visibility): array { 62 global $DB; 63 64 $result = false; 65 $warnings = []; 66 67 $params = self::validate_parameters(self::execute_parameters(), [ 68 'contentid' => $contentid, 69 'visibility' => $visibility, 70 ]); 71 72 try { 73 $record = $DB->get_record('contentbank_content', ['id' => $params['contentid']], '*', MUST_EXIST); 74 $contenttypeclass = "\\$record->contenttype\\contenttype"; 75 if (class_exists($contenttypeclass)) { 76 $context = \context::instance_by_id($record->contextid, MUST_EXIST); 77 self::validate_context($context); 78 $contenttype = new $contenttypeclass($context); 79 $contentclass = "\\$record->contenttype\\content"; 80 $content = new $contentclass($record); 81 // Check capability. 82 if ($contenttype->can_manage($content)) { 83 // This content's visibility can be changed. 84 if ($content->set_visibility($params['visibility'])) { 85 $result = true; 86 } else { 87 $warnings[] = [ 88 'item' => $params['contentid'], 89 'warningcode' => 'contentvisibilitynotset', 90 'message' => get_string('contentvisibilitynotset', 'core_contentbank') 91 ]; 92 } 93 94 } else { 95 // The user has no permission to manage this content. 96 $warnings[] = [ 97 'item' => $params['contentid'], 98 'warningcode' => 'nopermissiontomanage', 99 'message' => get_string('nopermissiontomanage', 'core_contentbank') 100 ]; 101 } 102 } 103 } catch (\moodle_exception $e) { 104 // The content or the context don't exist. 105 $warnings[] = [ 106 'item' => $params['contentid'], 107 'warningcode' => 'exception', 108 'message' => $e->getMessage() 109 ]; 110 } 111 112 return [ 113 'result' => $result, 114 'warnings' => $warnings 115 ]; 116 } 117 118 /** 119 * set_content_visibility return. 120 * 121 * @since Moodle 3.11 122 * @return external_single_structure 123 */ 124 public static function execute_returns(): external_single_structure { 125 return new external_single_structure([ 126 'result' => new external_value(PARAM_BOOL, 'The processing result'), 127 'warnings' => new external_warnings() 128 ]); 129 } 130 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body