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 API for this tool.
19
 *
20
 * @package    mod_customcert
21
 * @copyright  2016 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace mod_customcert;
25
 
26
use core_external\external_api;
27
use core_external\external_value;
28
use core_external\external_single_structure;
29
use core_external\external_multiple_structure;
30
use core_external\external_function_parameters;
31
 
32
/**
33
 * This is the external API for this tool.
34
 *
35
 * @copyright  2016 Mark Nelson <markn@moodle.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class external extends external_api {
39
 
40
    /**
41
     * Returns the save_element() parameters.
42
     *
43
     * @return external_function_parameters
44
     */
45
    public static function save_element_parameters() {
46
        return new external_function_parameters(
47
            [
48
                'templateid' => new external_value(PARAM_INT, 'The template id'),
49
                'elementid' => new external_value(PARAM_INT, 'The element id'),
50
                'values' => new external_multiple_structure(
51
                    new external_single_structure(
52
                        [
53
                            'name' => new external_value(PARAM_ALPHANUMEXT, 'The field to update'),
54
                            'value' => new external_value(PARAM_RAW, 'The value of the field'),
55
                        ]
56
                    )
57
                ),
58
            ]
59
        );
60
    }
61
 
62
    /**
63
     * Handles saving element data.
64
     *
65
     * @param int $templateid The template id.
66
     * @param int $elementid The element id.
67
     * @param array $values The values to save
68
     * @return array
69
     */
70
    public static function save_element($templateid, $elementid, $values) {
71
        global $DB;
72
 
73
        $params = [
74
            'templateid' => $templateid,
75
            'elementid' => $elementid,
76
            'values' => $values,
77
        ];
78
        self::validate_parameters(self::save_element_parameters(), $params);
79
 
80
        $template = $DB->get_record('customcert_templates', ['id' => $templateid], '*', MUST_EXIST);
81
        $element = $DB->get_record('customcert_elements', ['id' => $elementid], '*', MUST_EXIST);
82
 
83
        // Set the template.
84
        $template = new \mod_customcert\template($template);
85
 
86
        // Perform checks.
87
        if ($cm = $template->get_cm()) {
88
            self::validate_context(\context_module::instance($cm->id));
89
        } else {
90
            self::validate_context(\context_system::instance());
91
        }
92
        // Make sure the user has the required capabilities.
93
        $template->require_manage();
94
 
95
        // Set the values we are going to save.
96
        $data = new \stdClass();
97
        $data->id = $element->id;
98
        $data->name = $element->name;
99
        foreach ($values as $value) {
100
            $field = $value['name'];
101
            $data->$field = $value['value'];
102
        }
103
 
104
        // Get an instance of the element class.
105
        if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
106
            return $e->save_form_elements($data);
107
        }
108
 
109
        return false;
110
    }
111
 
112
    /**
113
     * Returns the save_element result value.
114
     *
115
     * @return external_value
116
     */
117
    public static function save_element_returns() {
118
        return new external_value(PARAM_BOOL, 'True if successful, false otherwise');
119
    }
120
 
121
    /**
122
     * Returns get_element() parameters.
123
     *
124
     * @return external_function_parameters
125
     */
126
    public static function get_element_html_parameters() {
127
        return new external_function_parameters(
128
            [
129
                'templateid' => new external_value(PARAM_INT, 'The template id'),
130
                'elementid' => new external_value(PARAM_INT, 'The element id'),
131
            ]
132
        );
133
    }
134
 
135
    /**
136
     * Handles return the element's HTML.
137
     *
138
     * @param int $templateid The template id
139
     * @param int $elementid The element id.
140
     * @return string
141
     */
142
    public static function get_element_html($templateid, $elementid) {
143
        global $DB;
144
 
145
        $params = [
146
            'templateid' => $templateid,
147
            'elementid' => $elementid,
148
        ];
149
        self::validate_parameters(self::get_element_html_parameters(), $params);
150
 
151
        $template = $DB->get_record('customcert_templates', ['id' => $templateid], '*', MUST_EXIST);
152
        $element = $DB->get_record('customcert_elements', ['id' => $elementid], '*', MUST_EXIST);
153
 
154
        // Set the template.
155
        $template = new \mod_customcert\template($template);
156
 
157
        // Perform checks.
158
        if ($cm = $template->get_cm()) {
159
            self::validate_context(\context_module::instance($cm->id));
160
        } else {
161
            self::validate_context(\context_system::instance());
162
        }
163
 
164
        // Get an instance of the element class.
165
        if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
166
            return $e->render_html();
167
        }
168
 
169
        return '';
170
    }
171
 
172
    /**
173
     * Returns the get_element result value.
174
     *
175
     * @return external_value
176
     */
177
    public static function get_element_html_returns() {
178
        return new external_value(PARAM_RAW, 'The HTML');
179
    }
180
 
181
    /**
182
     * Returns the delete_issue() parameters.
183
     *
184
     * @return external_function_parameters
185
     */
186
    public static function delete_issue_parameters() {
187
        return new external_function_parameters(
188
            [
189
                'certificateid' => new external_value(PARAM_INT, 'The certificate id'),
190
                'issueid' => new external_value(PARAM_INT, 'The issue id'),
191
            ]
192
        );
193
    }
194
 
195
    /**
196
     * Handles deleting a customcert issue.
197
     *
198
     * @param int $certificateid The certificate id.
199
     * @param int $issueid The issue id.
200
     * @return bool
201
     */
202
    public static function delete_issue($certificateid, $issueid) {
203
        global $DB;
204
 
205
        $params = [
206
            'certificateid' => $certificateid,
207
            'issueid' => $issueid,
208
        ];
209
        self::validate_parameters(self::delete_issue_parameters(), $params);
210
 
211
        $certificate = $DB->get_record('customcert', ['id' => $certificateid], '*', MUST_EXIST);
212
        $issue = $DB->get_record('customcert_issues', ['id' => $issueid, 'customcertid' => $certificateid], '*', MUST_EXIST);
213
 
214
        $cm = get_coursemodule_from_instance('customcert', $certificate->id, 0, false, MUST_EXIST);
215
 
216
        // Make sure the user has the required capabilities.
217
        $context = \context_module::instance($cm->id);
218
        self::validate_context($context);
219
        require_capability('mod/customcert:manage', $context);
220
 
221
        // Delete the issue.
222
        return $DB->delete_records('customcert_issues', ['id' => $issue->id]);
223
    }
224
 
225
    /**
226
     * Returns the delete_issue result value.
227
     *
228
     * @return external_value
229
     */
230
    public static function delete_issue_returns() {
231
        return new external_value(PARAM_BOOL, 'True if successful, false otherwise');
232
    }
233
}