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
 * The mod_hvp content user data.
19
 *
20
 * @package    mod_hvp
21
 * @since      Moodle 2.7
22
 * @copyright  2016 Joubel AS
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace mod_hvp;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
/**
31
 * Class content_user_data handles user data and corresponding db operations.
32
 *
33
 * @package mod_hvp
34
 * @copyright   2018 Joubel AS <contact@joubel.com>
35
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class content_user_data {
38
 
39
    /**
40
     * Retrieves ajax parameters for content and update or delete
41
     * user data depending on params.
42
     *
43
     * @throws \coding_exception
44
     * @throws \dml_exception
45
     */
46
    public static function handle_ajax() {
47
        // Query String Parameters.
48
        $contentid = required_param('content_id', PARAM_INT);
49
        $dataid = required_param('data_type', PARAM_RAW);
50
        $subcontentid = required_param('sub_content_id', PARAM_INT);
51
 
52
        // Form Data.
53
        $data = optional_param('data', null, PARAM_RAW);
54
        $preload = optional_param('preload', null, PARAM_INT);
55
        $invalidate = optional_param('invalidate', null, PARAM_INT);
56
 
57
        if ($contentid === null || $dataid === null || $subcontentid === null) {
58
            // Missing parameters.
59
            \H5PCore::ajaxError(get_string('missingparameters', 'hvp'));
60
            return;
61
        }
62
 
63
        // Saving data.
64
        if ($data !== null && $preload !== null && $invalidate !== null) {
65
            self::store_data($contentid, $subcontentid, $dataid, $data, $preload, $invalidate);
66
        } else {
67
            self::fetch_existing_data($contentid, $subcontentid, $dataid);
68
        }
69
    }
70
 
71
 
72
    /**
73
     * Stores content user data
74
     *
75
     * @param $contentid
76
     * @param $subcontentid
77
     * @param $dataid
78
     * @param $data
79
     * @param $preload
80
     * @param $invalidate
81
     *
82
     * @throws \coding_exception
83
     * @throws \dml_exception
84
     */
85
    private static function store_data($contentid, $subcontentid, $dataid, $data, $preload, $invalidate) {
86
        // Validate token.
87
        if (!\H5PCore::validToken('contentuserdata', required_param('token', PARAM_RAW))) {
88
            \H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
89
            return;
90
        }
91
 
92
        if ($contentid === 0) {
93
            $context = \context::instance_by_id(required_param('contextId', PARAM_RAW));
94
        } else {
95
            // Load course module for content to get context.
96
            $cm = get_coursemodule_from_instance('hvp', $contentid);
97
            if (!$cm) {
98
                \H5PCore::ajaxError('No such content');
99
                http_response_code(404);
100
                return;
101
            }
102
            $context = \context_module::instance($cm->id);
103
        }
104
 
105
        // Check permissions.
106
        if (!has_capability('mod/hvp:savecontentuserdata', $context)) {
107
            \H5PCore::ajaxError(get_string('nopermissiontosavecontentuserdata', 'hvp'));
108
            http_response_code(403);
109
            return;
110
        }
111
 
112
        if ($data === '0') {
113
            // Delete user data.
114
            self::delete_user_data($contentid, $subcontentid, $dataid);
115
        } else {
116
            // Save user data.
117
            self::save_user_data($contentid, $subcontentid, $dataid, $preload, $invalidate, $data);
118
        }
119
        \H5PCore::ajaxSuccess();
120
    }
121
 
122
    /**
123
     * Return existing content user data
124
     *
125
     * @param $contentid
126
     * @param $subcontentid
127
     * @param $dataid
128
     *
129
     * @throws \dml_exception
130
     */
131
    private static function fetch_existing_data($contentid, $subcontentid, $dataid) {
132
        // Fetch user data.
133
        $userdata = self::get_user_data($contentid, $subcontentid, $dataid);
134
        \H5PCore::ajaxSuccess($userdata ? $userdata->data : null);
135
    }
136
 
137
    /**
138
     * Get user data for content.
139
     *
140
     * @param $contentid
141
     * @param $subcontentid
142
     * @param $dataid
143
     *
144
     * @return mixed
145
     * @throws \dml_exception
146
     */
147
    public static function get_user_data($contentid, $subcontentid, $dataid) {
148
        global $DB, $USER;
149
 
150
        $result = $DB->get_record('hvp_content_user_data', array(
151
                'user_id' => $USER->id,
152
                'hvp_id' => $contentid,
153
                'sub_content_id' => $subcontentid,
154
                'data_id' => $dataid
155
            )
156
        );
157
 
158
        return $result;
159
    }
160
 
161
    /**
162
     * Save user data for specific content in database.
163
     *
164
     * @param $contentid
165
     * @param $subcontentid
166
     * @param $dataid
167
     * @param $preload
168
     * @param $invalidate
169
     * @param $data
170
     *
171
     * @throws \dml_exception
172
     */
173
    public static function save_user_data($contentid, $subcontentid, $dataid, $preload, $invalidate, $data) {
174
        global $DB, $USER;
175
 
176
        // Determine if we should update or insert.
177
        $update = self::get_user_data($contentid, $subcontentid, $dataid);
178
 
179
        // Wash values to ensure 0 or 1.
180
        $preload = ($preload === '0' || $preload === 0) ? 0 : 1;
181
        $invalidate = ($invalidate === '0' || $invalidate === 0) ? 0 : 1;
182
 
183
        // New data to be inserted.
184
        $newdata = (object)array(
185
            'user_id' => $USER->id,
186
            'hvp_id' => $contentid,
187
            'sub_content_id' => $subcontentid,
188
            'data_id' => $dataid,
189
            'data' => $data,
190
            'preloaded' => $preload,
191
            'delete_on_content_change' => $invalidate
192
        );
193
 
194
        // Does not exist.
195
        if ($update === false) {
196
            // Insert new data.
197
            $DB->insert_record('hvp_content_user_data', $newdata);
198
        } else {
199
            // Get old data id.
200
            $newdata->id = $update->id;
201
 
202
            // Update old data.
203
            $DB->update_record('hvp_content_user_data', $newdata);
204
        }
205
    }
206
 
207
    /**
208
     * Delete user data with specific content from database
209
     *
210
     * @param $contentid
211
     * @param $subcontentid
212
     * @param $dataid
213
     *
214
     * @throws \dml_exception
215
     */
216
    public static function delete_user_data($contentid, $subcontentid, $dataid) {
217
        global $DB, $USER;
218
 
219
        $DB->delete_records('hvp_content_user_data', array(
220
            'user_id' => $USER->id,
221
            'hvp_id' => $contentid,
222
            'sub_content_id' => $subcontentid,
223
            'data_id' => $dataid
224
        ));
225
    }
226
 
227
    /**
228
     * Load user data for specific content
229
     *
230
     * @param $contentid
231
     *
232
     * @return array User data for specific content if found, else null
233
     * @throws \dml_exception
234
     */
235
    public static function load_pre_loaded_user_data($contentid) {
236
        global $DB, $USER;
237
 
238
        $preloadeduserdata = array(
239
            'state' => '{}'
240
        );
241
 
242
        $results = $DB->get_records('hvp_content_user_data', array(
243
            'user_id' => $USER->id,
244
            'hvp_id' => $contentid,
245
            'sub_content_id' => 0,
246
            'preloaded' => 1
247
        ));
248
 
249
        // Get data for data ids.
250
        foreach ($results as $contentuserdata) {
251
            $preloadeduserdata[$contentuserdata->data_id] = $contentuserdata->data;
252
        }
253
 
254
        return $preloadeduserdata;
255
    }
256
}