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
/**
18
 * This is the external method for updating a glossary entry.
19
 *
20
 * @package    mod_glossary
21
 * @since      Moodle 3.10
22
 * @copyright  2020 Juan Leyva <juan@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_glossary\external;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
global $CFG;
31
require_once($CFG->dirroot . '/mod/glossary/lib.php');
32
 
33
use core_external\external_api;
34
use core_external\external_format_value;
35
use core_external\external_function_parameters;
36
use core_external\external_multiple_structure;
37
use core_external\external_single_structure;
38
use core_external\external_value;
39
use core_external\external_warnings;
40
use core_text;
41
use moodle_exception;
42
 
43
/**
44
 * This is the external method for updating a glossary entry.
45
 *
46
 * @copyright  2020 Juan Leyva <juan@moodle.com>
47
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 */
49
class update_entry extends external_api {
50
    /**
51
     * Parameters.
52
     *
53
     * @return external_function_parameters
54
     */
55
    public static function execute_parameters(): external_function_parameters {
56
        return new external_function_parameters([
57
            'entryid' => new external_value(PARAM_INT, 'Glossary entry id to update'),
58
            'concept' => new external_value(PARAM_TEXT, 'Glossary concept'),
59
            'definition' => new external_value(PARAM_RAW, 'Glossary concept definition'),
60
            'definitionformat' => new external_format_value('definition'),
61
            'options' => new external_multiple_structure (
62
                new external_single_structure(
63
                    [
64
                        'name' => new external_value(PARAM_ALPHANUM,
65
                            'The allowed keys (value format) are:
66
                            inlineattachmentsid (int); the draft file area id for inline attachments
67
                            attachmentsid (int); the draft file area id for attachments
68
                            categories (comma separated int); comma separated category ids
69
                            aliases (comma separated str); comma separated aliases
70
                            usedynalink (bool); whether the entry should be automatically linked.
71
                            casesensitive (bool); whether the entry is case sensitive.
72
                            fullmatch (bool); whether to match whole words only.'),
73
                        'value' => new external_value(PARAM_RAW, 'the value of the option (validated inside the function)')
74
                    ]
75
                ), 'Optional settings', VALUE_DEFAULT, []
76
            )
77
        ]);
78
    }
79
 
80
    /**
81
     * Update the indicated glossary entry.
82
     *
83
     * @param  int $entryid The entry to update
84
     * @param string $concept    the glossary concept
85
     * @param string $definition the concept definition
86
     * @param int $definitionformat the concept definition format
87
     * @param array  $options    additional settings
88
     * @return array with result and warnings
89
     * @throws moodle_exception
90
     */
91
    public static function execute(int $entryid, string $concept, string $definition, int $definitionformat,
92
            array $options = []): array {
93
 
94
        global $DB;
95
 
96
        $params = self::validate_parameters(self::execute_parameters(), compact('entryid', 'concept', 'definition',
97
            'definitionformat', 'options'));
98
        $id = $params['entryid'];
99
 
100
        // Get and validate the glossary entry.
101
        $entry = $DB->get_record('glossary_entries', ['id' => $id], '*', MUST_EXIST);
102
        list($glossary, $context, $course, $cm) = \mod_glossary_external::validate_glossary($entry->glossaryid);
103
 
104
        // Check if the user can update the entry.
105
        mod_glossary_can_update_entry($entry, $glossary, $context, $cm, false);
106
 
107
        // Check for duplicates if the concept changes.
108
        if (!$glossary->allowduplicatedentries &&
109
                core_text::strtolower($entry->concept) != core_text::strtolower(trim($params['concept']))) {
110
 
111
            if (glossary_concept_exists($glossary, $params['concept'])) {
112
                throw new moodle_exception('errconceptalreadyexists', 'glossary');
113
            }
114
        }
115
 
116
        // Prepare the entry object.
117
        $entry->aliases = '';
118
        $entry = mod_glossary_prepare_entry_for_edition($entry);
119
        $entry->concept = $params['concept'];
120
        $entry->definition_editor = [
121
            'text' => $params['definition'],
122
            'format' => $params['definitionformat'],
123
        ];
124
        // Options.
125
        foreach ($params['options'] as $option) {
126
            $name = trim($option['name']);
127
            switch ($name) {
128
                case 'inlineattachmentsid':
129
                    $entry->definition_editor['itemid'] = clean_param($option['value'], PARAM_INT);
130
                    break;
131
                case 'attachmentsid':
132
                    $entry->attachment_filemanager = clean_param($option['value'], PARAM_INT);
133
                    break;
134
                case 'categories':
135
                    $entry->categories = clean_param($option['value'], PARAM_SEQUENCE);
136
                    $entry->categories = explode(',', $entry->categories);
137
                    break;
138
                case 'aliases':
139
                    $entry->aliases = clean_param($option['value'], PARAM_NOTAGS);
140
                    // Convert to the expected format.
141
                    $entry->aliases = str_replace(",", "\n", $entry->aliases);
142
                    break;
143
                case 'usedynalink':
144
                case 'casesensitive':
145
                case 'fullmatch':
146
                    // Only allow if linking is enabled.
147
                    if ($glossary->usedynalink) {
148
                        $entry->{$name} = clean_param($option['value'], PARAM_BOOL);
149
                    }
150
                    break;
151
                default:
152
                    throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
153
            }
154
        }
155
 
156
        $entry = glossary_edit_entry($entry, $course, $cm, $glossary, $context);
157
 
158
        return [
159
            'result' => true,
160
            'warnings' => [],
161
        ];
162
    }
163
 
164
    /**
165
     * Return.
166
     *
167
     * @return external_single_structure
168
     */
169
    public static function execute_returns(): external_single_structure {
170
        return new external_single_structure([
171
            'result' => new external_value(PARAM_BOOL, 'The update result'),
172
            'warnings' => new external_warnings()
173
        ]);
174
    }
175
}