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
 * External stickynotes API
19
 *
20
 * @package    mod_stickynotes
21
 * @copyright  2021 Sébastien Mehr <sebmehr.fr>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die;
26
 
27
require_once("$CFG->libdir/externallib.php");
28
 
29
/**
30
 * Stickynotes functions
31
 * @copyright 2021 Sébastien Mehr <sebmehr.fr>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class mod_stickynotes_external extends external_api {
35
 
36
    public static function changing_note_position($noteid, $oldcolumnid, $newcolumnid, $oldindex, $newindex) {
37
        global $DB;
38
 
39
        $params = self::validate_parameters(self::changing_note_position_parameters(),
40
                array('noteid' => $noteid, 'oldcolumnid' => $oldcolumnid, 'newcolumnid' => $newcolumnid,
41
        'oldindex' => $oldindex, 'newindex' => $newindex));
42
 
43
        $newdata = new stdClass();
44
        $newdata->id = $noteid;
45
        $newdata->stickycolid = $newcolumnid;
46
        $newdata->ordernote = $newindex;
47
 
48
        try {
49
            $transaction = $DB->start_delegated_transaction();
50
 
51
            $paramdb = array($newcolumnid, $newindex);
52
            $DB->execute("UPDATE {stickynotes_note} SET ordernote = ordernote + 1
53
                            WHERE stickycolid = ? AND ordernote >= ?", $paramdb);
54
 
55
            $DB->update_record('stickynotes_note', $newdata);
56
 
57
            if ($oldcolumnid != $newcolumnid) {
58
                $paramdb = array($oldcolumnid, $oldindex);
59
                $DB->execute("UPDATE {stickynotes_note} SET ordernote = ordernote - 1
60
                                WHERE stickycolid = ? AND ordernote > ?", $paramdb);
61
            }
62
 
63
            $transaction->allow_commit();
64
 
65
        } catch (Exception $e) {
66
            $transaction->rollback($e);
67
        }
68
 
69
        $sql = 'SELECT id, stickycolid FROM {stickynotes_note} WHERE id = ?';
70
        $paramsdb = array($noteid);
71
        $dbresult = $DB->get_records_sql($sql, $paramsdb);
72
 
73
        return $dbresult;
74
    }
75
 
76
    public static function changing_note_position_parameters() {
77
        return new external_function_parameters(
78
            array(
79
                'noteid' => new external_value(PARAM_INT, VALUE_REQUIRED),
80
                'oldcolumnid' => new external_value(PARAM_INT, VALUE_REQUIRED),
81
                'newcolumnid' => new external_value(PARAM_INT, VALUE_REQUIRED),
82
                'oldindex' => new external_value(PARAM_INT, VALUE_REQUIRED),
83
                'newindex' => new external_value(PARAM_INT, VALUE_REQUIRED),
84
            )
85
        );
86
    }
87
 
88
    public static function changing_note_position_returns() {
89
        return new external_multiple_structure(
90
            new external_single_structure(
91
                array(
92
                    'id' => new external_value(PARAM_INT, VALUE_REQUIRED),
93
                    'stickycolid' => new external_value(PARAM_INT, VALUE_REQUIRED),
94
                )
95
            )
96
        );
97
 
98
    }
99
 
100
    /**
101
     * Returns welcome message
102
     * @return array = array('' => , ); welcome message
103
     */
104
    public static function get_notes_column_select($id) {
105
        global $USER;
106
        global $DB;
107
        global $CFG;
108
 
109
        $params = self::validate_parameters(
110
            self::get_notes_column_select_parameters(),
111
                array('id' => $id)
112
        );
113
 
114
        $sql = 'SELECT id, ordernote, message FROM {stickynotes_note} WHERE stickycolid = ? ORDER BY ordernote';
115
        $paramsdb = array($id);
116
        $dbresult = $DB->get_records_sql($sql, $paramsdb);
117
 
118
        $return[0] = array(
119
            'ordernote' => '1',
120
            'message' => get_string('firstplace', 'stickynotes')
121
        );
122
 
123
        foreach ($dbresult as $move) {
124
            $neworder = $move->ordernote + 1;
125
            $return[] = array('message' => get_string('after', 'stickynotes')." '".$move->message."'", 'ordernote' => $neworder);
126
        }
127
        return json_encode(array_values($return));
128
    }
129
 
130
    /**
131
     * Returns description of method parameters
132
     * @return external_function_parameters
133
     */
134
    public static function get_notes_column_select_parameters() {
135
        return new external_function_parameters(
136
          array(
137
              'id' => new external_value(PARAM_INT, "id")
138
              )
139
        );
140
    }
141
 
142
    /**
143
     * Returns description of method result value
144
     * @return external_description
145
     */
146
    public static function get_notes_column_select_returns() {
147
        return new external_value(PARAM_RAW, 'The updated JSON output');
148
    }
149
}