| 1 | efrain | 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_contentbank\contentbank;
 | 
        
           |  |  | 20 | use core_external\external_api;
 | 
        
           |  |  | 21 | use core_external\external_function_parameters;
 | 
        
           |  |  | 22 | use core_external\external_single_structure;
 | 
        
           |  |  | 23 | use core_external\external_value;
 | 
        
           |  |  | 24 | use core_external\external_warnings;
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | /**
 | 
        
           |  |  | 27 |  * This is the external method for copying a content.
 | 
        
           |  |  | 28 |  *
 | 
        
           |  |  | 29 |  * @package    core_contentbank
 | 
        
           |  |  | 30 |  * @copyright  2023 Daniel Neis Araujo
 | 
        
           |  |  | 31 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 32 |  */
 | 
        
           |  |  | 33 | class copy_content extends external_api {
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |     /**
 | 
        
           |  |  | 36 |      * copy_content parameters.
 | 
        
           |  |  | 37 |      *
 | 
        
           |  |  | 38 |      * @since  Moodle 4.3
 | 
        
           |  |  | 39 |      * @return external_function_parameters
 | 
        
           |  |  | 40 |      */
 | 
        
           |  |  | 41 |     public static function execute_parameters(): external_function_parameters {
 | 
        
           |  |  | 42 |         return new external_function_parameters(
 | 
        
           |  |  | 43 |             [
 | 
        
           |  |  | 44 |                 'contentid' => new external_value(PARAM_INT, 'The content id to copy', VALUE_REQUIRED),
 | 
        
           |  |  | 45 |                 'name' => new external_value(PARAM_RAW, 'The new name for the content', VALUE_REQUIRED),
 | 
        
           |  |  | 46 |             ]
 | 
        
           |  |  | 47 |         );
 | 
        
           |  |  | 48 |     }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     /**
 | 
        
           |  |  | 51 |      * Copy content from the contentbank.
 | 
        
           |  |  | 52 |      *
 | 
        
           |  |  | 53 |      * @since  Moodle 4.3
 | 
        
           |  |  | 54 |      * @param  int $contentid The content id to copy.
 | 
        
           |  |  | 55 |      * @param  string $name The new name.
 | 
        
           |  |  | 56 |      * @return array Id of the new content; false and the warning, otherwise.
 | 
        
           |  |  | 57 |      */
 | 
        
           |  |  | 58 |     public static function execute(int $contentid, string $name): array {
 | 
        
           |  |  | 59 |         global $DB;
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |         $id = 0;
 | 
        
           |  |  | 62 |         $warnings = [];
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |         $params = self::validate_parameters(self::execute_parameters(), [
 | 
        
           |  |  | 65 |             'contentid' => $contentid,
 | 
        
           |  |  | 66 |             'name' => $name,
 | 
        
           |  |  | 67 |         ]);
 | 
        
           |  |  | 68 |         $params['name'] = clean_param($params['name'], PARAM_TEXT);
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |         // If name is empty don't try to copy and return a more detailed message.
 | 
        
           |  |  | 71 |         if (trim($params['name']) === '') {
 | 
        
           |  |  | 72 |             $warnings[] = [
 | 
        
           |  |  | 73 |                 'item' => $params['contentid'],
 | 
        
           |  |  | 74 |                 'warningcode' => 'emptynamenotallowed',
 | 
        
           |  |  | 75 |                 'message' => get_string('emptynamenotallowed', 'core_contentbank'),
 | 
        
           |  |  | 76 |             ];
 | 
        
           |  |  | 77 |         } else {
 | 
        
           |  |  | 78 |             try {
 | 
        
           |  |  | 79 |                 $record = $DB->get_record('contentbank_content', ['id' => $params['contentid']], '*', MUST_EXIST);
 | 
        
           |  |  | 80 |                 $cb = new contentbank();
 | 
        
           |  |  | 81 |                 $content = $cb->get_content_from_id($record->id);
 | 
        
           |  |  | 82 |                 $contenttype = $content->get_content_type_instance();
 | 
        
           |  |  | 83 |                 $context = \context::instance_by_id($record->contextid, MUST_EXIST);
 | 
        
           |  |  | 84 |                 self::validate_context($context);
 | 
        
           |  |  | 85 |                 // Check capability.
 | 
        
           |  |  | 86 |                 if ($contenttype->can_copy($content)) {
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 |                     // This content can be copied.
 | 
        
           |  |  | 89 |                     $crecord = $content->get_content();
 | 
        
           |  |  | 90 |                     unset($crecord->id);
 | 
        
           |  |  | 91 |                     $crecord->name = $params['name'];
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |                     if ($content = $contenttype->create_content($crecord)) {
 | 
        
           | 1441 | ariadna | 94 |   | 
        
           |  |  | 95 |                         $handler = \core_contentbank\customfield\content_handler::create();
 | 
        
           |  |  | 96 |                         $handler->instance_form_before_set_data($record);
 | 
        
           |  |  | 97 |                         $record->id = $content->get_id();
 | 
        
           |  |  | 98 |                         $handler->instance_form_save($record);
 | 
        
           |  |  | 99 |   | 
        
           | 1 | efrain | 100 |                         $fs = get_file_storage();
 | 
        
           |  |  | 101 |                         $files = $fs->get_area_files($context->id, 'contentbank', 'public', $params['contentid'], 'itemid, filepath,
 | 
        
           |  |  | 102 |                             filename', false);
 | 
        
           |  |  | 103 |                         if (!empty($files)) {
 | 
        
           |  |  | 104 |                             $file = reset($files);
 | 
        
           |  |  | 105 |                             $content->import_file($file);
 | 
        
           |  |  | 106 |                         }
 | 
        
           |  |  | 107 |                         $id = $content->get_id();
 | 
        
           |  |  | 108 |                     } else {
 | 
        
           |  |  | 109 |                         $warnings[] = [
 | 
        
           |  |  | 110 |                             'item' => $params['contentid'],
 | 
        
           |  |  | 111 |                             'warningcode' => 'contentnotcopied',
 | 
        
           |  |  | 112 |                             'message' => get_string('contentnotcopied', 'core_contentbank'),
 | 
        
           |  |  | 113 |                         ];
 | 
        
           |  |  | 114 |                     }
 | 
        
           |  |  | 115 |                 } else {
 | 
        
           |  |  | 116 |                     // The user has no permission to manage this content.
 | 
        
           |  |  | 117 |                     $warnings[] = [
 | 
        
           |  |  | 118 |                         'item' => $params['contentid'],
 | 
        
           |  |  | 119 |                         'warningcode' => 'nopermissiontomanage',
 | 
        
           |  |  | 120 |                         'message' => get_string('nopermissiontocopy', 'core_contentbank'),
 | 
        
           |  |  | 121 |                     ];
 | 
        
           |  |  | 122 |                 }
 | 
        
           |  |  | 123 |             } catch (\moodle_exception $e) {
 | 
        
           |  |  | 124 |                 // The content or the context don't exist.
 | 
        
           |  |  | 125 |                 $warnings[] = [
 | 
        
           |  |  | 126 |                     'item' => $params['contentid'],
 | 
        
           |  |  | 127 |                     'warningcode' => 'exception',
 | 
        
           |  |  | 128 |                     'message' => $e->getMessage(),
 | 
        
           |  |  | 129 |                 ];
 | 
        
           |  |  | 130 |             }
 | 
        
           |  |  | 131 |         }
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 |         return [
 | 
        
           |  |  | 134 |             'id' => $id,
 | 
        
           |  |  | 135 |             'warnings' => $warnings,
 | 
        
           |  |  | 136 |         ];
 | 
        
           |  |  | 137 |     }
 | 
        
           |  |  | 138 |   | 
        
           |  |  | 139 |     /**
 | 
        
           |  |  | 140 |      * copy_content return.
 | 
        
           |  |  | 141 |      *
 | 
        
           |  |  | 142 |      * @since  Moodle 4.3
 | 
        
           |  |  | 143 |      * @return external_single_structure
 | 
        
           |  |  | 144 |      */
 | 
        
           |  |  | 145 |     public static function execute_returns(): external_single_structure {
 | 
        
           |  |  | 146 |         return new external_single_structure([
 | 
        
           |  |  | 147 |             'id' => new external_value(PARAM_INT, 'The id of the new content'),
 | 
        
           |  |  | 148 |             'warnings' => new external_warnings(),
 | 
        
           |  |  | 149 |         ]);
 | 
        
           |  |  | 150 |     }
 | 
        
           |  |  | 151 | }
 |