Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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)) {
94
                        $fs = get_file_storage();
95
                        $files = $fs->get_area_files($context->id, 'contentbank', 'public', $params['contentid'], 'itemid, filepath,
96
                            filename', false);
97
                        if (!empty($files)) {
98
                            $file = reset($files);
99
                            $content->import_file($file);
100
                        }
101
                        $id = $content->get_id();
102
                    } else {
103
                        $warnings[] = [
104
                            'item' => $params['contentid'],
105
                            'warningcode' => 'contentnotcopied',
106
                            'message' => get_string('contentnotcopied', 'core_contentbank'),
107
                        ];
108
                    }
109
                } else {
110
                    // The user has no permission to manage this content.
111
                    $warnings[] = [
112
                        'item' => $params['contentid'],
113
                        'warningcode' => 'nopermissiontomanage',
114
                        'message' => get_string('nopermissiontocopy', 'core_contentbank'),
115
                    ];
116
                }
117
            } catch (\moodle_exception $e) {
118
                // The content or the context don't exist.
119
                $warnings[] = [
120
                    'item' => $params['contentid'],
121
                    'warningcode' => 'exception',
122
                    'message' => $e->getMessage(),
123
                ];
124
            }
125
        }
126
 
127
        return [
128
            'id' => $id,
129
            'warnings' => $warnings,
130
        ];
131
    }
132
 
133
    /**
134
     * copy_content return.
135
     *
136
     * @since  Moodle 4.3
137
     * @return external_single_structure
138
     */
139
    public static function execute_returns(): external_single_structure {
140
        return new external_single_structure([
141
            'id' => new external_value(PARAM_INT, 'The id of the new content'),
142
            'warnings' => new external_warnings(),
143
        ]);
144
    }
145
}