Gitlab CSE Unil

externallib.php 16.7 KB
Newer Older
M. Chardon's avatar
M. Chardon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * External assign API
 *
 * @package    assignfeedback_editpdfplus
 * @copyright  2017 Université de Lausanne
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
defined('MOODLE_INTERNAL') || die;

require_once("$CFG->libdir/externallib.php");
require_once("$CFG->dirroot/user/externallib.php");
require_once("$CFG->dirroot/mod/assign/locallib.php");
require_once("locallib.php");
require_once("locallib_admin.php");

use \assignfeedback_editpdfplus\form\axis_form;
34
use \assignfeedback_editpdfplus\form\axis_del_form;
M. Chardon's avatar
M. Chardon committed
35
use \assignfeedback_editpdfplus\form\axis_import_form;
M. Chardon's avatar
M. Chardon committed
36
37
38
39
use \assignfeedback_editpdfplus\admin_editor;

class assignfeedback_editpdfplus_external extends external_api {

40
41
42
43
    const PLUGINNAME = "assignfeedback_editpdfplus";
    const DATAJSON = 'jsonformdata';
    const MESSAGELIB = 'message';
    const COURSELIB = "course";
44
    const CONTEXTID = "contextid";
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    const AXEID = "axeid";
    const AXEIDDESC = "Axe ID";
    const AXELIB = "axelabel";
    const AXELIBDESC = "Axe label";
    const TOOLID = "toolid";
    const TOOLIDDESC = "Tool ID";
    const TOOLLIBDESC = "Tool label";
    const TOOLTYPE = "typetool";
    const TOOLTYPEDESC = "Type of tool";
    const TOOLSELECTED = "selecttool";
    const BOUTONLIBTOOL = "button";
    const ENABLETOOL = "enable";
    const ENABLETOOLDESC = "Tool is enabled";

M. Chardon's avatar
M. Chardon committed
59
    /**
60
61
     * Returns description of method parameters for general calling
     * @return \external_function_parameters
M. Chardon's avatar
M. Chardon committed
62
     */
63
64
    public static function submit_generic_form_parameters() {
        $message = 'The data from the grading form, encoded as a json array';
M. Chardon's avatar
M. Chardon committed
65
66
        return new external_function_parameters(
                array(
67
            self::DATAJSON => new external_value(PARAM_RAW, $message)
M. Chardon's avatar
M. Chardon committed
68
69
70
71
                )
        );
    }

M. Chardon's avatar
M. Chardon committed
72
    /**
73
74
75
     * 
     * Form return generic structure
     * @return \external_single_structure
M. Chardon's avatar
M. Chardon committed
76
     */
77
78
79
80
81
82
83
    public static function submit_generic_form_returns() {
        return new external_single_structure(
                array(
            self::MESSAGELIB => new external_value(PARAM_TEXT, self::MESSAGELIB, VALUE_OPTIONAL)
                )
        );
    }
M. Chardon's avatar
M. Chardon committed
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    /**
     * Form return tool structure
     * @return \external_multiple_structure
     */
    public static function submit_tool_form_returns() {
        return new external_multiple_structure(
                new external_single_structure(
                array(
            self::AXEID => new external_value(PARAM_INT, self::AXEIDDESC),
            self::TOOLSELECTED => new external_value(PARAM_INT, self::TOOLIDDESC),
            self::ENABLETOOL => new external_value(PARAM_INT, self::ENABLETOOLDESC, VALUE_OPTIONAL),
            self::TOOLID => new external_value(PARAM_INT, self::TOOLIDDESC),
            self::TOOLTYPE => new external_value(PARAM_INT, self::TOOLTYPEDESC, VALUE_OPTIONAL),
            self::BOUTONLIBTOOL => new external_value(PARAM_TEXT, self::TOOLLIBDESC, VALUE_OPTIONAL),
            self::MESSAGELIB => new external_value(PARAM_TEXT, self::MESSAGELIB, VALUE_OPTIONAL)
                )
                )
        );
    }
M. Chardon's avatar
M. Chardon committed
104

105
106
107
108
109
110
111
112
113
114
115
    /**
     * Extract and parse json data string into an array
     * @param external_function_parameters $externalFunctionParameter
     * @param String $jsonformdata
     * @return array decoded data
     */
    public static function getParseData($externalFunctionParameter, $jsonformdata) {
        $params = self::validate_parameters($externalFunctionParameter, array(
                    self::DATAJSON => $jsonformdata
        ));
        $serialiseddata = json_decode($params[self::DATAJSON]);
M. Chardon's avatar
M. Chardon committed
116
117
        $data = array();
        parse_str($serialiseddata, $data);
118
119
        return $data;
    }
M. Chardon's avatar
M. Chardon committed
120

121
122
123
124
    /**
     * Set Page context from the course id given. It returns the context found.
     * @global $DB
     * @global $PAGE
125
     * @param int $contextid Current context id
126
127
     * @return context course's context 
     */
128
129
130
    public static function setPageContext($contextid) {
        global $PAGE;
        $context = context::instance_by_id($contextid, MUST_EXIST);
M. Chardon's avatar
M. Chardon committed
131
        $PAGE->set_context($context);
132
133
134
135
136
137
138
        return $context;
    }

    public static function getMessageError() {
        return array(self::MESSAGELIB => get_string("admin_messageko", self::PLUGINNAME));
    }

M. Chardon's avatar
M. Chardon committed
139
140
141
142
143
144
145
146
    /**
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function submit_axis_form_parameters() {
        return self::submit_generic_form_parameters();
    }

147
148
149
150
151
152
153
154
    /**
     * Submit axis form for adding or edditing
     * @param String $jsonformdata
     * @return array
     */
    public static function submit_axis_form($jsonformdata) {
        $data = self::getParseData(self::submit_axis_form_parameters(), $jsonformdata);

155
        $context = self::setPageContext($data[self::CONTEXTID]);
M. Chardon's avatar
M. Chardon committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

        $customdata = (object) $data;
        $formparams = array($customdata);

        // Data is injected into the form by the last param for the constructor.
        $mform = new axis_form(null, $formparams, 'post', '', null, true, $data);
        $validateddata = $mform->get_data();

        if ($validateddata) {
            if ($validateddata->axeid) {
                admin_editor::edit_axis($validateddata->axeid, $validateddata->label);
                $axeid = $validateddata->axeid;
            } else {
                $axeid = admin_editor::add_axis($validateddata->label, $context->id);
            }
171
            return array(array(self::AXEID => $axeid, self::AXELIB => $validateddata->label));
M. Chardon's avatar
M. Chardon committed
172
        }
173
        return array(self::getMessageError());
M. Chardon's avatar
M. Chardon committed
174
175
    }

M. Chardon's avatar
M. Chardon committed
176
177
178
179
    /**
     * Form return structure
     * @return \external_multiple_structure
     */
M. Chardon's avatar
M. Chardon committed
180
181
182
183
    public static function submit_axis_form_returns() {
        return new external_multiple_structure(
                new external_single_structure(
                array(
184
185
186
            self::AXEID => new external_value(PARAM_INT, self::AXEIDDESC),
            self::AXELIB => new external_value(PARAM_TEXT, self::AXELIBDESC),
            self::MESSAGELIB => new external_value(PARAM_TEXT, self::MESSAGELIB, VALUE_OPTIONAL)
M. Chardon's avatar
M. Chardon committed
187
188
189
190
191
                )
                )
        );
    }

192
193
194
195
196
    /**
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function submit_axis_del_form_parameters() {
197
        return self::submit_generic_form_parameters();
198
199
    }

M. Chardon's avatar
M. Chardon committed
200
201
202
203
204
    /**
     * Submit axis form for deleting
     * @param String $jsonformdata
     * @return array
     */
205
    public static function submit_axis_del_form($jsonformdata) {
206
        $data = self::getParseData(self::submit_axis_form_parameters(), $jsonformdata);
207

208
        self::setPageContext($data[self::CONTEXTID]);
209
210
211
212
213
214
215
216

        $customdata = (object) $data;
        $formparams = array($customdata);

        // Data is injected into the form by the last param for the constructor.
        $mform = new axis_del_form(null, $formparams, 'post', '', null, true, $data);
        $validateddata = $mform->get_data();

217
218
219
        if ($validateddata && $validateddata->axeid && admin_editor::del_axis($validateddata->axeid)) {
            $message = "1";
            return array(array(self::MESSAGELIB => $message));
220
        }
221
222
223
        $warnings = array();
        $message = get_string('admindeltool_messageko', self::PLUGINNAME);
        $warnings[] = array(self::MESSAGELIB => $message);
224
225
226
        return $warnings;
    }

M. Chardon's avatar
M. Chardon committed
227
228
229
230
    /**
     * Form return structure
     * @return \external_multiple_structure
     */
231
232
    public static function submit_axis_del_form_returns() {
        return new external_multiple_structure(
233
                self::submit_generic_form_returns()
234
235
236
        );
    }

M. Chardon's avatar
M. Chardon committed
237
    /**
238
239
240
     * Submit tool form for adding or edditing
     * @param String $jsonformdata json tool
     * @param String $mode add or edit the tool
M. Chardon's avatar
M. Chardon committed
241
242
     * @return array
     */
243
244
    public static function submit_tool_form($jsonformdata, $mode) {
        $data = self::getParseData(self::submit_axis_form_parameters(), $jsonformdata);
M. Chardon's avatar
M. Chardon committed
245

246
        $context = self::setPageContext($data[self::CONTEXTID]);
M. Chardon's avatar
M. Chardon committed
247
248
249
250

        $customdata = (object) $data;

        $sessionkey = sesskey();
251
252
253
254
255
256
257
        if ($sessionkey == $customdata->sesskey && $mode) {
            $tool = null;
            if ($mode == "add") {
                $tool = admin_editor::add_tool($customdata, $context->id);
            } elseif ($mode == "edit") {
                $tool = admin_editor::edit_tool($customdata);
            }
M. Chardon's avatar
M. Chardon committed
258
            if ($tool) {
M. Chardon's avatar
M. Chardon committed
259
                $tools = admin_editor::get_tools_by_axis($tool->axis);
M. Chardon's avatar
M. Chardon committed
260
261
                $res = array();
                foreach ($tools as $toolTmp) {
262
                    $res[] = array(self::AXEID => $tool->axis, self::TOOLSELECTED => $tool->id, self::ENABLETOOL => $toolTmp->enabled, self::TOOLID => $toolTmp->id, self::TOOLTYPE => $toolTmp->type, self::BOUTONLIBTOOL => $toolTmp->label, self::MESSAGELIB => '');
M. Chardon's avatar
M. Chardon committed
263
264
265
266
                }
                return $res;
            }
        }
267
268
        return array(self::getMessageError());
    }
M. Chardon's avatar
M. Chardon committed
269

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    /**
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function submit_tool_edit_form_parameters() {
        return self::submit_generic_form_parameters();
    }

    /**
     * Submit tool form for edditing
     * @param String $jsonformdata
     * @return array
     */
    public static function submit_tool_edit_form($jsonformdata) {
        return self::submit_tool_form($jsonformdata, "edit");
M. Chardon's avatar
M. Chardon committed
285
286
    }

M. Chardon's avatar
M. Chardon committed
287
288
289
290
    /**
     * Form return structure
     * @return \external_multiple_structure
     */
M. Chardon's avatar
M. Chardon committed
291
    public static function submit_tool_edit_form_returns() {
292
        return self::submit_tool_form_returns();
M. Chardon's avatar
M. Chardon committed
293
294
295
296
297
298
299
    }

    /**
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function submit_tool_add_form_parameters() {
300
        return self::submit_generic_form_parameters();
M. Chardon's avatar
M. Chardon committed
301
302
    }

M. Chardon's avatar
M. Chardon committed
303
304
305
306
307
    /**
     * Submit tool form for adding
     * @param String $jsonformdata
     * @return array
     */
M. Chardon's avatar
M. Chardon committed
308
    public static function submit_tool_add_form($jsonformdata) {
309
        return self::submit_tool_form($jsonformdata, "add");
M. Chardon's avatar
M. Chardon committed
310
311
    }

M. Chardon's avatar
M. Chardon committed
312
313
314
315
    /**
     * Form return structure
     * @return \external_multiple_structure
     */
M. Chardon's avatar
M. Chardon committed
316
    public static function submit_tool_add_form_returns() {
317
        return self::submit_tool_form_returns();
M. Chardon's avatar
M. Chardon committed
318
319
    }

M. Chardon's avatar
M. Chardon committed
320
321
322
323
324
    /**
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function submit_tool_del_form_parameters() {
325
        return self::submit_generic_form_parameters();
M. Chardon's avatar
M. Chardon committed
326
327
    }

M. Chardon's avatar
M. Chardon committed
328
329
330
331
332
    /**
     * Submit tool form for deleting
     * @param String $jsonformdata
     * @return array
     */
M. Chardon's avatar
M. Chardon committed
333
    public static function submit_tool_del_form($jsonformdata) {
334
        $data = self::getParseData(self::submit_axis_form_parameters(), $jsonformdata);
M. Chardon's avatar
M. Chardon committed
335

336
        $context = self::setPageContext($data[self::CONTEXTID]);
M. Chardon's avatar
M. Chardon committed
337
338
339
340
341
342
343
344
345

        $customdata = (object) $data;

        $sessionkey = sesskey();
        if ($sessionkey == $customdata->sesskey) {
            $axisid = $customdata->axisid;
            if (admin_editor::del_tool($customdata, $context->id)) {
                $res = array();
                $tools = admin_editor::get_tools_by_axis($axisid);
346
347
                if (sizeof($tools) > 0) {
                    foreach ($tools as $toolTmp) {
348
                        $res[] = array(self::AXEID => $axisid, self::TOOLSELECTED => -1, self::ENABLETOOL => $toolTmp->enabled, self::TOOLID => $toolTmp->id, self::TOOLTYPE => $toolTmp->type, self::BOUTONLIBTOOL => $toolTmp->label, self::MESSAGELIB => '');
349
350
                    }
                } else {
351
                    $res[] = array(self::AXEID => $axisid, self::TOOLSELECTED => -1, self::TOOLID => -1, self::MESSAGELIB => '1');
M. Chardon's avatar
M. Chardon committed
352
353
354
355
                }
                return $res;
            }
        }
356
        return array(self::getMessageError());
M. Chardon's avatar
M. Chardon committed
357
358
    }

M. Chardon's avatar
M. Chardon committed
359
360
361
362
    /**
     * Form return structure
     * @return \external_multiple_structure
     */
M. Chardon's avatar
M. Chardon committed
363
    public static function submit_tool_del_form_returns() {
364
        return self::submit_tool_form_returns();
M. Chardon's avatar
M. Chardon committed
365
366
    }

M. Chardon's avatar
M. Chardon committed
367
368
369
370
371
    /**
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function submit_axis_import_form_parameters() {
372
        return self::submit_generic_form_parameters();
M. Chardon's avatar
M. Chardon committed
373
374
    }

M. Chardon's avatar
M. Chardon committed
375
376
377
378
379
    /**
     * Submit axis form for importing
     * @param String $jsonformdata
     * @return array
     */
M. Chardon's avatar
M. Chardon committed
380
381
    public static function submit_axis_import_form($jsonformdata) {

382
        $data = self::getParseData(self::submit_axis_import_form_parameters(), $jsonformdata);
M. Chardon's avatar
M. Chardon committed
383

384
        $context = self::setPageContext($data[self::CONTEXTID]);
M. Chardon's avatar
M. Chardon committed
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

        $customdata = (object) $data;
        $formparams = array($customdata);

        // Data is injected into the form by the last param for the constructor.
        $mform = new axis_import_form(null, $formparams, 'post', '', null, true, $data);
        $validateddata = $mform->get_data();

        if ($validateddata && $validateddata->axeid) {
            $axeToImport = admin_editor::getAxisById($validateddata->axeid);
            $axeNew = admin_editor::import_axis($axeToImport, $context->id);
            if ($axeNew) {
                $tools = admin_editor::get_tools_by_axis($axeToImport->id);
                foreach ($tools as $toolToImport) {
                    admin_editor::import_tool($toolToImport, $axeNew, $context->id);
                }
                $res = array();
                $toolsNew = admin_editor::get_tools_by_axis($axeNew);
                if (sizeof($toolsNew) > 0) {
                    foreach ($toolsNew as $tool) {
405
                        $res[] = array(self::AXEID => $axeNew, self::AXELIB => $axeToImport->label, self::MESSAGELIB => "", self::ENABLETOOL => $tool->enabled, self::TOOLID => $tool->id, self::TOOLTYPE => $tool->type, self::BOUTONLIBTOOL => $tool->label, self::MESSAGELIB => '');
M. Chardon's avatar
M. Chardon committed
406
407
                    }
                } else {
408
                    $res = array(array(self::AXEID => $axeNew, self::AXELIB => $axeToImport->label, self::MESSAGELIB => ""));
M. Chardon's avatar
M. Chardon committed
409
410
411
412
413
                }

                return $res;
            }
        }
414
        return array(self::getMessageError());
M. Chardon's avatar
M. Chardon committed
415
416
    }

M. Chardon's avatar
M. Chardon committed
417
418
419
420
    /**
     * Form return structure
     * @return \external_multiple_structure
     */
M. Chardon's avatar
M. Chardon committed
421
422
423
424
    public static function submit_axis_import_form_returns() {
        return new external_multiple_structure(
                new external_single_structure(
                array(
425
426
427
428
429
430
431
            self::AXEID => new external_value(PARAM_INT, self::AXEIDDESC, VALUE_OPTIONAL),
            self::AXELIB => new external_value(PARAM_TEXT, self::AXELIBDESC, VALUE_OPTIONAL),
            self::MESSAGELIB => new external_value(PARAM_TEXT, self::MESSAGELIB),
            self::ENABLETOOL => new external_value(PARAM_INT, self::ENABLETOOLDESC, VALUE_OPTIONAL),
            self::TOOLID => new external_value(PARAM_INT, self::TOOLIDDESC, VALUE_OPTIONAL),
            self::TOOLTYPE => new external_value(PARAM_INT, self::TOOLTYPEDESC, VALUE_OPTIONAL),
            self::BOUTONLIBTOOL => new external_value(PARAM_TEXT, self::TOOLLIBDESC, VALUE_OPTIONAL)
M. Chardon's avatar
M. Chardon committed
432
433
434
435
436
                )
                )
        );
    }

437
438
439
440
441
    /**
     * Returns description of method parameters
     * @return external_function_parameters
     */
    public static function submit_tool_order_form_parameters() {
442
        return self::submit_generic_form_parameters();
443
444
445
446
447
448
449
450
    }

    /**
     * Submit tool form for changing order
     * @param String $jsonformdata
     * @return array
     */
    public static function submit_tool_order_form($jsonformdata) {
451
        $data = self::getParseData(self::submit_tool_order_form_parameters(), $jsonformdata);
452

453
        self::setPageContext($data[self::CONTEXTID]);
454
455
456
457
458
459

        $customdata = (object) $data;

        $sessionkey = sesskey();
        if ($sessionkey == $customdata->sesskey && $customdata->toolid) {
            admin_editor::edit_tool_order($customdata);
460
            return array(self::MESSAGELIB => 'ok');
461
        }
462
        return self::getMessageError();
463
464
465
466
467
468
469
    }

    /**
     * Form return structure
     * @return \external_multiple_structure
     */
    public static function submit_tool_order_form_returns() {
470
        return self::submit_generic_form_returns();
471
472
    }

M. Chardon's avatar
M. Chardon committed
473
}