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
 * Class containing the external API functions functions for the Policy tool.
19
 *
20
 * @package    tool_policy
21
 * @copyright  2018 Sara Arjona (sara@moodle.com)
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace tool_policy;
26
 
27
use context_system;
28
use core_external\external_api;
29
use core_external\external_function_parameters;
30
use core_external\external_single_structure;
31
use core_external\external_value;
32
use core_external\external_warnings;
33
use moodle_exception;
34
use tool_policy\form\accept_policy;
35
 
36
/**
37
 * Class external.
38
 *
39
 * The external API for the Policy tool.
40
 *
41
 * @copyright   2018 Sara Arjona (sara@moodle.com)
42
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class external extends external_api {
45
 
46
    /**
47
     * Parameter description for get_policy_version_parameters().
48
     *
49
     * @return external_function_parameters
50
     */
51
    public static function get_policy_version_parameters() {
52
        return new external_function_parameters([
53
            'versionid' => new external_value(PARAM_INT, 'The policy version ID', VALUE_REQUIRED),
54
            'behalfid' => new external_value(PARAM_INT, 'The id of user on whose behalf the user is viewing the policy',
55
                VALUE_DEFAULT, 0)
56
        ]);
57
    }
58
 
59
    /**
60
     * Fetch the details of a policy version.
61
     *
62
     * @param int $versionid The policy version ID.
63
     * @param int $behalfid The id of user on whose behalf the user is viewing the policy.
64
     * @return array
65
     * @throws coding_exception
66
     * @throws dml_exception
67
     * @throws invalid_parameter_exception
68
     * @throws restricted_context_exception
69
     * @throws moodle_exception
70
     */
71
    public static function get_policy_version($versionid, $behalfid = null) {
72
        global $PAGE;
73
 
74
        $result = [];
75
        $warnings = [];
76
        $params = external_api::validate_parameters(self::get_policy_version_parameters(), [
77
            'versionid' => $versionid,
78
            'behalfid' => $behalfid
79
        ]);
80
        $versionid = $params['versionid'];
81
        $behalfid = $params['behalfid'];
82
 
83
        $context = context_system::instance();
84
        $PAGE->set_context($context);
85
 
86
        try {
87
            // Validate if the user has access to the policy version.
88
            $version = api::get_policy_version($versionid);
89
            if (!api::can_user_view_policy_version($version, $behalfid)) {
90
                $warnings[] = [
91
                    'item' => $versionid,
92
                    'warningcode' => 'errorusercantviewpolicyversion',
93
                    'message' => get_string('errorusercantviewpolicyversion', 'tool_policy')
94
                ];
95
            } else if (!empty($version)) {
96
                $version = api::get_policy_version($versionid);
97
                $policy['name'] = $version->name;
98
                $policy['versionid'] = $versionid;
99
                list($policy['content'], $notusedformat) = \core_external\util::format_text(
100
                    $version->content,
101
                    $version->contentformat,
102
                    \context_system::instance(),
103
                    'tool_policy',
104
                    'policydocumentcontent',
105
                    $version->id
106
                );
107
                $result['policy'] = $policy;
108
            }
109
        } catch (moodle_exception $e) {
110
            $warnings[] = [
111
                'item' => $versionid,
112
                'warningcode' => 'errorpolicyversionnotfound',
113
                'message' => get_string('errorpolicyversionnotfound', 'tool_policy')
114
            ];
115
        }
116
 
117
        return [
118
            'result' => $result,
119
            'warnings' => $warnings
120
        ];
121
    }
122
 
123
    /**
124
     * Parameter description for get_policy_version().
125
     *
126
     * @return \core_external\external_description
127
     */
128
    public static function get_policy_version_returns() {
129
        return new external_single_structure([
130
            'result' => new external_single_structure([
131
                            'policy' => new external_single_structure([
132
                                    'name' => new external_value(PARAM_RAW, 'The policy version name', VALUE_OPTIONAL),
133
                                    'versionid' => new external_value(PARAM_INT, 'The policy version id', VALUE_OPTIONAL),
134
                                    'content' => new external_value(PARAM_RAW, 'The policy version content', VALUE_OPTIONAL)
135
                                    ], 'Policy information', VALUE_OPTIONAL)
136
                            ]),
137
            'warnings' => new external_warnings()
138
        ]);
139
    }
140
 
141
    /**
142
     * Describes the parameters for submit_create_group_form webservice.
143
     * @return external_function_parameters
144
     */
145
    public static function submit_accept_on_behalf_parameters() {
146
        return new external_function_parameters(
147
            array(
148
                'jsonformdata' => new external_value(PARAM_RAW, 'The data from the create group form, encoded as a json array')
149
            )
150
        );
151
    }
152
 
153
    /**
154
     * Submit the create group form.
155
     *
156
     * @param string $jsonformdata The data from the form, encoded as a json array.
157
     * @return int new group id.
158
     */
159
    public static function submit_accept_on_behalf($jsonformdata) {
160
        // We always must pass webservice params through validate_parameters.
161
        $params = self::validate_parameters(self::submit_accept_on_behalf_parameters(),
162
            ['jsonformdata' => $jsonformdata]);
163
 
164
        self::validate_context(context_system::instance());
165
 
166
        $serialiseddata = json_decode($params['jsonformdata']);
167
 
168
        $data = array();
169
        parse_str($serialiseddata, $data);
170
 
171
        // The last param is the ajax submitted data.
172
        $mform = new accept_policy(null, $data, 'post', '', null, true, $data);
173
 
174
        // Do the action.
175
        $mform->process();
176
 
177
        return true;
178
    }
179
 
180
    /**
181
     * Returns description of method result value.
182
     *
183
     * @return \core_external\external_description
184
     * @since Moodle 3.0
185
     */
186
    public static function submit_accept_on_behalf_returns() {
187
        return new external_value(PARAM_BOOL, 'success');
188
    }
189
}