Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
declare(strict_types=1);
18
 
19
namespace mod_feedback\external\questions;
20
 
21
use core_external\external_api;
22
use core_external\external_value;
23
use core_external\external_function_parameters;
24
use context_module;
25
 
26
/**
27
 * External method for reordering feedback questions.
28
 *
29
 * @package     mod_feedback
30
 * @copyright   2024 Mikel Martín <mikel@moodle.com>
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class reorder extends external_api {
34
 
35
    /**
36
     * Describes the parameters for reorder.
37
     *
38
     * @return external_function_parameters
39
     */
40
    public static function execute_parameters(): external_function_parameters {
41
        return new external_function_parameters(
42
            [
43
                'cmid' => new external_value(PARAM_INT, 'Feedback course module id'),
44
                'itemorder' => new external_value(PARAM_SEQUENCE, 'Feedback order by sequence of question item ids'),
45
            ]
46
        );
47
    }
48
 
49
    /**
50
     * External function to reorder feedback questions.
51
     *
52
     * @param int $cmid
53
     * @param string $itemorder
54
     * @return bool
55
     */
56
    public static function execute(int $cmid, string $itemorder): bool {
57
        global $DB;
58
        [
59
            'cmid' => $cmid,
60
            'itemorder' => $itemorder,
61
        ] = self::validate_parameters(self::execute_parameters(), [
62
            'cmid' => $cmid,
63
            'itemorder' => $itemorder,
64
        ]);
65
 
66
        $cm = get_coursemodule_from_id('feedback', $cmid, 0, false, MUST_EXIST);
67
        $feedback = $DB->get_record('feedback', ['id' => $cm->instance], '*', MUST_EXIST);
68
        $context = context_module::instance($cm->id);
69
 
70
        self::validate_context($context);
71
        require_capability('mod/feedback:edititems', $context);
72
 
73
        $itemlist = explode(',', trim($itemorder, ',')) ?: [];
74
        if (count($itemlist) > 0) {
75
            return feedback_ajax_saveitemorder($itemlist, $feedback);
76
        }
77
 
78
        return false;
79
    }
80
 
81
    /**
82
     * Describes the data returned from the external function.
83
     *
84
     * @return external_value
85
     */
86
    public static function execute_returns(): external_value {
87
        return new external_value(PARAM_BOOL, '', VALUE_REQUIRED);
88
    }
89
}