-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
313 lines (269 loc) · 10.3 KB
/
lib.php
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?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/>.
/**
* Library of interface functions and constants.
*
* @package mod_cobra
* @copyright 2016 onwards - Cellule TICE - University of Namur
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_cobra\local\helper;
/**
* Return if the plugin supports $feature.
*
* @param string $feature Constant representing the feature.
* @return true | null True if the feature is supported, null otherwise.
*/
function cobra_supports($feature) {
switch($feature) {
case FEATURE_MOD_ARCHETYPE :
return MOD_ARCHETYPE_RESOURCE;
case FEATURE_MOD_INTRO :
return true;
case FEATURE_GROUPS:
return false;
case FEATURE_GROUPINGS:
return false;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_MOD_PURPOSE:
return MOD_PURPOSE_CONTENT;
default:
return null;
}
}
/**
* Saves a new instance of the mod_cobra into the database.
*
* Given an object containing all the necessary data, (defined by the form
* in mod_form.php) this function will create a new instance and return the id
* number of the instance.
*
* @param object $cobra An object from the form.
* @return int The id of the newly inserted record.
*/
function cobra_add_instance($cobra) {
global $DB;
$cobra->timecreated = time();
$cobra->id = $DB->insert_record('cobra', $cobra);
$completiontimeexpected = !empty($cobra->completionexpected) ? $cobra->completionexpected : null;
\core_completion\api::update_completion_date_event($cobra->coursemodule, 'cobra', $cobra->id, $completiontimeexpected);
return $cobra->id;
}
/**
* Updates an instance of the mod_cobra in the database.
*
* Given an object containing all the necessary data (defined in mod_form.php),
* this function will update an existing instance with new data.
*
* @param object $cobra An object from the form in mod_form.php.
* @return bool True if successful, false otherwise.
*/
function cobra_update_instance($cobra) {
global $DB;
$cobra->timemodified = time();
$cobra->id = $cobra->instance;
$completiontimeexpected = !empty($cobra->completionexpected) ? $cobra->completionexpected : null;
\core_completion\api::update_completion_date_event($cobra->coursemodule, 'cobra', $cobra->id, $completiontimeexpected);
return $DB->update_record('cobra', $cobra);
}
/**
* Removes an instance of the mod_cobra from the database.
*
* @param int $id Id of the module instance.
* @return bool True if successful, false on failure.
*/
function cobra_delete_instance($id) {
global $DB;
$exists = $DB->get_record('cobra', ['id' => $id]);
if (!$exists) {
return false;
}
$DB->delete_records('cobra', ['id' => $id]);
return true;
}
/**
* Extends the course navigation with mod_cobra nodes.
*
* @param navigation_node $parentnode main course navigation node
* @param stdClass $course
* @param context_course $context
*/
function cobra_extend_navigation_course(navigation_node $parentnode, stdClass $course, context_course $context) {
global $DB;
if ($DB->record_exists('cobra', ['course' => $course->id])) {
global $CFG;
if (has_capability('mod/cobra:addinstance', $context)) {
$cobranode = $parentnode->add(get_string('glossary', 'mod_cobra') . ' ' . get_string('cobra', 'mod_cobra'));
$params = ['id' => $context->instanceid, 'cmd' => 'rqexport'];
$cobranode->add(get_string('glossary', 'mod_cobra') . ' ' . get_string('cobra', 'mod_cobra'),
new moodle_url($CFG->wwwroot .'/mod/cobra/glossary.php', $params),
navigation_node::TYPE_SETTING, null, 'mod_cobra_export_glossary');
$params = ['id' => $context->instanceid, 'cmd' => 'rqcompare'];
$cobranode->add(get_string('comparetextwithglossary', 'mod_cobra'),
new moodle_url($CFG->wwwroot .'/mod/cobra/glossary.php', $params),
navigation_node::TYPE_SETTING, null, 'mod_cobra_compare_glossary');
}
}
}
/**
* Hook for plugins to take action when a module is created or updated.
* Here to keep only one instance as defaut for corpus order and display preferences
*
* @param stdClass $moduleinfo the module info
* @param stdClass $course the course of the module
*
* @return stdClass moduleinfo updated by plugins.
*/
function cobra_coursemodule_edit_post_actions($moduleinfo, $course) {
global $DB;
if ($moduleinfo->modulename != 'cobra') {
return $moduleinfo;
}
if (!PHPUNIT_TEST) {
if (empty($moduleinfo->id)) {
$cobraid = $DB->get_field_sql('SELECT MAX(id) FROM {cobra} WHERE course = :course', ['course' => $course->id]);
} else {
$cobraid = $moduleinfo->id;
}
if ($moduleinfo->isdefaultdisplayprefs) {
$statement = "UPDATE {cobra}
SET isdefaultdisplayprefs = 0
WHERE course = :course
AND id != :newid";
$DB->execute($statement, ['newid' => $cobraid, 'course' => $course->id]);
}
if ($moduleinfo->isdefaultcorpusorder) {
$statement = "UPDATE {cobra}
SET isdefaultcorpusorder = 0
WHERE course = :course
AND id != :newid";
$DB->execute($statement, ['newid' => $cobraid, 'course' => $course->id]);
}
}
return $moduleinfo;
}
/**
* This function receives a calendar event and returns the action associated with it, or null if there is none.
*
* This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
* is not displayed on the block.
*
* @param calendar_event $event
* @param \core_calendar\action_factory $factory
* @return \core_calendar\local\event\entities\action_interface|null
*/
function mod_cobra_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['cobra'][$event->instance];
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);
if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
return null;
}
return $factory->create_instance(
get_string('view'),
new \moodle_url('/mod/cobra/view.php', ['id' => $cm->id]),
1,
true
);
}
/**
* Actual implementation of the reset course functionality, clear all
* personal glossaries and clear course level default cobra preferences for
* course $data->courseid.
*
* @param object $data the data submitted from the reset course.
* @return array status array
*/
function cobra_reset_userdata($data) {
global $DB;
$componentstr = get_string('modulename', 'cobra');
$status = [];
if (!empty($data->reset_cobra_defaults)) {
$sql = "UPDATE {cobra}
SET isdefaultdisplayprefs = 0,
isdefaultcorpusorder = 0
WHERE course=:course";
$params = ['course' => $data->courseid];
$success = $DB->execute($sql, $params);
$status[] = [
'component' => $componentstr,
'item' => get_string('resetdefaults', 'cobra'),
'error' => !$success,
];
}
if (!empty($data->reset_cobra_click_history) && !empty($data->reset_cobra_personal_glossaries)) {
$params = ['course' => $data->courseid];
$success = $DB->delete_records('cobra_click', $params);
$status[] = [
'component' => $componentstr,
'item' => get_string('resetglossaries', 'cobra'),
'error' => !$success,
];
$status[] = [
'component' => $componentstr,
'item' => get_string('resetclichistory', 'cobra'),
'error' => !$success,
];
} else if (!empty($data->reset_cobra_personal_glossaries)) {
$sql = "UPDATE {cobra_click}
SET inglossary = 0
WHERE course = :course";
$params = ['course' => $data->courseid];
$success = $DB->execute($sql, $params);
$status[] = [
'component' => $componentstr,
'item' => get_string('resetglossaries', 'cobra'),
'error' => !$success,
];
}
return $status;
}
/**
* Called by course/reset.php
* Module reset form elements.
* @param moodleform $mform form passed by reference
*/
function cobra_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'cobraheader', get_string('modulename', 'cobra'));
$mform->addElement('checkbox', 'reset_cobra_defaults', get_string('resetdefaults', 'cobra'));
$mform->addElement('checkbox', 'reset_cobra_personal_glossaries', get_string('resetglossaries', 'cobra'));
$mform->addElement('checkbox', 'reset_cobra_click_history', get_string('resetclichistory', 'cobra'));
$mform->disabledIf('reset_cobra_click_history', 'reset_cobra_personal_glossaries', 'notchecked');
}
/**
* Course reset form defaults.
* @param stdClass $course
* @return array
*/
function cobra_reset_course_form_defaults($course) {
return [
'reset_cobra_defaults' => 0,
'reset_cobra_personal_glossaries' => 1,
'reset_cobra_click_history' => 0,
];
}
/**
* Fill in cached text info and glossary entries.
*/
function cobra_fill_cache_tables() {
helper::fill_cache_tables();
}