-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.php
321 lines (283 loc) · 10.1 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
314
315
316
317
318
319
320
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Library of interface functions and constants.
*
* @package mod_debate
* @copyright 2021 Safat Shahin <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* 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 debate_supports($feature) {
switch($feature) {
case FEATURE_BACKUP_MOODLE2:
case FEATURE_SHOW_DESCRIPTION:
case FEATURE_COMPLETION_TRACKS_VIEWS:
case FEATURE_MOD_INTRO:
return true;
default:
return null;
}
}
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the debate response.
*
* @param moodleform $mform the course reset form that is being built.
*/
function debate_reset_course_form_definition($mform) {
$mform->addElement('header', 'debateheader', get_string('modulenameplural', 'mod_debate'));
$mform->addElement('advcheckbox', 'reset_debate_attempts',
get_string('reset_debate_attempts', 'mod_debate'));
$mform->addElement('advcheckbox', 'reset_debate_teams',
get_string('reset_debate_teams', 'mod_debate'));
}
/**
* Course reset form defaults.
*
* @param object $course
* @return array the defaults.
*/
function debate_reset_course_form_defaults($course) {
return array(
'reset_debate_attempts' => 0,
'reset_debate_teams' => 0
);
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
*
* @param stdClass $data
* @return array status array
*/
function debate_reset_userdata($data) {
global $DB;
$componentstr = get_string('modulenameplural', 'mod_debate');
$status = array();
if (!empty($data->reset_debate_attempts)) {
$DB->delete_records_select('debate_response', '', array($data->courseid));
$status[] = array(
'component' => $componentstr,
'item' => get_string('attemptsdeleted', 'mod_debate'),
'error' => false);
}
if (!empty($data->reset_debate_teams)) {
$DB->delete_records_select('debate_teams', '', array($data->courseid));
$status[] = array(
'component' => $componentstr,
'item' => get_string('teamsdeleted', 'mod_debate'),
'error' => false);
}
return $status;
}
/**
* List the actions that correspond to a view of this module.
* This is used by the participation report.
*
* Note: This is not used by new logging system. Event with
* crud = 'r' and edulevel = LEVEL_PARTICIPATING will
* be considered as view action.
*
* @return array
*/
function debate_get_view_actions() {
return array('view', 'view all');
}
/**
* List the actions that correspond to a post of this module.
* This is used by the participation report.
*
* Note: This is not used by new logging system. Event with
* crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
* will be considered as post action.
*
* @return array
*/
function debate_get_post_actions() {
return array('update', 'add');
}
/**
* Saves a new instance of the mod_debate 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 $moduleinstance An object from the form.
* @param mod_debate_mod_form $mform The form.
* @return int The id of the newly inserted record.
*/
function debate_add_instance($moduleinstance, $mform = null) {
global $CFG, $DB;
require_once("$CFG->libdir/resourcelib.php");
$cmid = $moduleinstance->coursemodule;
$moduleinstance->timecreated = time();
$id = $DB->insert_record('debate', $moduleinstance);
$moduleinstance->id = $id;
$DB->set_field('course_modules', 'instance', $id, array('id' => $cmid));
$completiontimeexpected = !empty($moduleinstance->completionexpected) ? $moduleinstance->completionexpected : null;
\core_completion\api::update_completion_date_event($cmid, 'debate', $id, $completiontimeexpected);
return $id;
}
/**
* Updates an instance of the mod_debate 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 $moduleinstance An object from the form in mod_form.php.
* @param mod_debate_mod_form $mform The form.
* @return bool True if successful, false otherwise.
*/
function debate_update_instance($moduleinstance, $mform = null) {
global $CFG, $DB;
require_once("$CFG->libdir/resourcelib.php");
$cmid = $moduleinstance->coursemodule;
$moduleinstance->timemodified = time();
$moduleinstance->id = $moduleinstance->instance;
$DB->update_record('debate', $moduleinstance);
$completiontimeexpected = !empty($moduleinstance->completionexpected) ? $moduleinstance->completionexpected : null;
\core_completion\api::update_completion_date_event($cmid, 'debate', $moduleinstance->id, $completiontimeexpected);
return true;
}
/**
* Removes an instance of the mod_debate from the database.
*
* @param int $id Id of the module instance.
* @return bool True if successful, false on failure.
*/
function debate_delete_instance($id) {
global $DB;
if (!$debate = $DB->get_record('debate', array('id' => $id))) {
return false;
}
if (!$cm = get_coursemodule_from_instance('debate', $debate->id)) {
return false;
}
if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
return false;
}
$context = context_module::instance($cm->id);
// Now get rid of all files.
$fs = get_file_storage();
$fs->delete_area_files($context->id);
\core_completion\api::update_completion_date_event($cm->id, 'debate', $debate->id, null);
$DB->delete_records('debate_response', array('debateid' => $id));
$DB->delete_records('debate', array('id' => $id));
return true;
}
/**
* Returns the lists of all browsable file areas within the given module context.
*
* The file area 'intro' for the activity introduction field is added automatically
* by {@see file_browser::get_file_info_context_module()}.
*
* @package mod_debate
* @category files
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @return string[]
*/
function debate_get_file_areas($course, $cm, $context) {
return array();
}
/**
* File browsing support for mod_debate file areas.
*
* @package mod_debate
* @category files
*
* @param file_browser $browser
* @param array $areas
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info Instance or null if not found
*/
function debate_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
return null;
}
/**
* Serves the files from the mod_debate file areas.
*
* @package mod_debate
* @category files
*
* @param stdClass $course The course object.
* @param stdClass $cm The course module object.
* @param stdClass $context The mod_debate's context.
* @param string $filearea The name of the file area.
* @param array $args Extra arguments (itemid, path).
* @param bool $forcedownload Whether or not force download.
* @param array $options Additional options affecting the file serving.
*/
function debate_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options = array()) {
global $DB, $CFG;
if ($context->contextlevel != CONTEXT_MODULE) {
send_file_not_found();
}
require_login($course, true, $cm);
send_file_not_found();
}
/**
* Mark the activity completed (if required) and trigger the course_module_viewed event.
*
* @param stdClass $debate debate object
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @since Moodle 3.0
*/
function debate_view($debate, $course, $cm, $context) {
global $DB, $USER;
// Trigger course_module_viewed event.
$params = array(
'context' => $context,
'objectid' => $debate->id
);
$event = \mod_debate\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('debate', $debate);
$event->trigger();
// Completion.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
$userresponsecount = $DB->count_records_select('debate_response',
'debateid = :debateid AND courseid = :courseid AND userid = :userid',
array('debateid' => (int)$debate->id, 'courseid' => (int)$course->id, 'userid' => $USER->id), 'COUNT("id")');
if ($userresponsecount >= (int)$debate->debateresponsecomcount) {
$completion->update_state($cm, COMPLETION_COMPLETE, $USER->id);
} else {
$current = $completion->get_data($cm, false, $USER->id);
$current->completionstate = COMPLETION_INCOMPLETE;
$current->timemodified = time();
$current->overrideby = null;
$completion->internal_set_data($cm, $current);
}
}