Gitlab CSE Unil

externallib.php 16.8 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    const PLUGINNAME = "assignfeedback_editpdfplus";
    const DATAJSON = 'jsonformdata';
    const MESSAGELIB = 'message';
    const COURSELIB = "course";
    const COURSEID = "courseid";
    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
125
126
127
128
129
130
    /**
     * Set Page context from the course id given. It returns the context found.
     * @global $DB
     * @global $PAGE
     * @param int $courseid Current course id
     * @return context course's context 
     */
    public static function setPageContext($courseid) {
        global $DB, $PAGE;
        $course = $DB->get_record(self::COURSELIB, array('id' => $courseid), '*', MUST_EXIST);
M. Chardon's avatar
M. Chardon committed
131
132
        $context = context_course::instance($course->id, MUST_EXIST);
        $PAGE->set_context($context);
133
134
135
136
137
138
139
        return $context;
    }

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

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

148
149
150
151
152
153
154
155
156
    /**
     * 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);

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

        $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);
            }
172
            return array(array(self::AXEID => $axeid, self::AXELIB => $validateddata->label));
M. Chardon's avatar
M. Chardon committed
173
        }
174
        return array(self::getMessageError());
M. Chardon's avatar
M. Chardon committed
175
176
    }

M. Chardon's avatar
M. Chardon committed
177
178
179
180
    /**
     * Form return structure
     * @return \external_multiple_structure
     */
M. Chardon's avatar
M. Chardon committed
181
182
183
184
    public static function submit_axis_form_returns() {
        return new external_multiple_structure(
                new external_single_structure(
                array(
185
186
187
            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
188
189
190
191
192
                )
                )
        );
    }

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

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

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

        $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();

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

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

M. Chardon's avatar
M. Chardon committed
238
    /**
239
240
241
     * 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
242
243
     * @return array
     */
244
245
    public static function submit_tool_form($jsonformdata, $mode) {
        $data = self::getParseData(self::submit_axis_form_parameters(), $jsonformdata);
M. Chardon's avatar
M. Chardon committed
246

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

        $customdata = (object) $data;

        $sessionkey = sesskey();
252
253
254
255
256
257
258
        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
259
            if ($tool) {
M. Chardon's avatar
M. Chardon committed
260
                $tools = admin_editor::get_tools_by_axis($tool->axis);
M. Chardon's avatar
M. Chardon committed
261
262
                $res = array();
                foreach ($tools as $toolTmp) {
263
                    $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
264
265
266
267
                }
                return $res;
            }
        }
268
269
        return array(self::getMessageError());
    }
M. Chardon's avatar
M. Chardon committed
270

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    /**
     * 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
286
287
    }

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

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

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

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

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

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

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

        $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);
347
348
                if (sizeof($tools) > 0) {
                    foreach ($tools as $toolTmp) {
349
                        $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 => '');
350
351
                    }
                } else {
352
                    $res[] = array(self::AXEID => $axisid, self::TOOLSELECTED => -1, self::TOOLID => -1, self::MESSAGELIB => '1');
M. Chardon's avatar
M. Chardon committed
353
354
355
356
                }
                return $res;
            }
        }
357
        return array(self::getMessageError());
M. Chardon's avatar
M. Chardon committed
358
359
    }

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

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

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

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

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

        $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) {
406
                        $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
407
408
                    }
                } else {
409
                    $res = array(array(self::AXEID => $axeNew, self::AXELIB => $axeToImport->label, self::MESSAGELIB => ""));
M. Chardon's avatar
M. Chardon committed
410
411
412
413
414
                }

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

M. Chardon's avatar
M. Chardon committed
418
419
420
421
    /**
     * Form return structure
     * @return \external_multiple_structure
     */
M. Chardon's avatar
M. Chardon committed
422
423
424
425
    public static function submit_axis_import_form_returns() {
        return new external_multiple_structure(
                new external_single_structure(
                array(
426
427
428
429
430
431
432
            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
433
434
435
436
437
                )
                )
        );
    }

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

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

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

        $customdata = (object) $data;

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

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

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