-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
369 lines (294 loc) · 13 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?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/>.
/**
* @package mod_palestra
* @copyright 2021 Interlegis (https://www.interlegis.leg.br)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
function palestra_supports($feature) {
switch($feature) {
case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_ASSIGNMENT;
case FEATURE_GROUPS: return false;
case FEATURE_GROUPINGS: return false;
case FEATURE_MOD_INTRO: return true;
case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
case FEATURE_COMPLETION_HAS_RULES: return true;
case FEATURE_GRADE_HAS_GRADE: return true;
case FEATURE_BACKUP_MOODLE2: return true;
case FEATURE_SHOW_DESCRIPTION: return true;
default: return null;
}
}
function palestra_reset_userdata($data) {
// Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
// See MDL-9367.
return array();
}
function palestra_get_view_actions() {
return array('view','view all');
}
function palestra_get_post_actions() {
return array('update', 'add');
}
function palestra_add_instance($palestra, $mform = null) {
global $CFG, $DB;
require_once("$CFG->libdir/resourcelib.php");
$cmid = $palestra->coursemodule;
$palestra->timemodified = time();
$palestra->needsupdate = 1;
$palestra->id = $DB->insert_record('palestra', $palestra);
$DB->set_field('course_modules', 'instance', $palestra->id, array('id'=>$cmid));
$context = context_module::instance($cmid);
palestra_grade_item_update($palestra);
$completiontimeexpected = !empty($palestra->completionexpected) ? $palestra->completionexpected : null;
\core_completion\api::update_completion_date_event($cmid, 'palestra', $palestra->id, $completiontimeexpected);
return $palestra->id;
}
function palestra_update_instance($palestra, $mform) {
global $CFG, $DB;
require_once("$CFG->libdir/resourcelib.php");
$cmid = $palestra->coursemodule;
$palestra->timemodified = time();
$palestra->needsupdate = 1;
$palestra->id = $palestra->instance;
$DB->update_record('palestra', $palestra);
palestra_grade_item_update($palestra);
$completiontimeexpected = !empty($palestra->completionexpected) ? $palestra->completionexpected : null;
\core_completion\api::update_completion_date_event($cmid, 'palestra', $palestra->id, $completiontimeexpected);
return true;
}
function palestra_delete_instance($id) {
global $DB;
if (!$palestra = $DB->get_record('palestra', array('id'=>$id))) {
return false;
}
$cm = get_coursemodule_from_instance('palestra', $id);
\core_completion\api::update_completion_date_event($cm->id, 'palestra', $id, null);
$DB->delete_records('palestra', array('id'=>$palestra->id));
$DB->delete_records('palestra_presence', array('palestraid'=>$palestra->id));
return true;
}
function palestra_get_coursemodule_info($coursemodule) {
global $CFG, $DB;
require_once("$CFG->libdir/resourcelib.php");
if (!$palestra = $DB->get_record('palestra', array('id'=>$coursemodule->instance))) {
return NULL;
}
$info = new cached_cm_info();
$info->name = $palestra->name;
if ($coursemodule->showdescription) {
// Convert intro to html. Do not filter cached version, filters run at display time.
$info->content = format_module_intro('palestra', $palestra, $coursemodule->id, false);
}
return $info;
}
function palestra_view($palestra, $course, $cm, $context) {
$params = array(
'context' => $context,
'objectid' => $palestra->id
);
$event = \mod_palestra\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('palestra', $palestra);
$event->trigger();
// Completion.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
}
function mod_palestra_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory, $userid = 0) {
global $USER;
if (empty($userid)) {
$userid = $USER->id;
}
$cm = get_fast_modinfo($event->courseid, $userid)->instances['palestra'][$event->instance];
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false, $userid);
if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
return null;
}
return $factory->create_instance(
get_string('view'),
new \moodle_url('/mod/palestra/view.php', ['id' => $cm->id]),
1,
true
);
}
function palestra_grade_item_update($palestra, $grades=NULL) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
}
$params = array('itemname'=>$palestra->name, 'idnumber'=>$palestra->cmidnumber, 'needsupdate'=>1);
if ($palestra->grade == 0) {
$params['gradetype'] = GRADE_TYPE_NONE;
} else if ($palestra->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $palestra->grade;
$params['grademin'] = 0;
} else if ($palestra->grade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$palestra->grade;
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = NULL;
}
return grade_update('mod/palestra', $palestra->course, 'mod', 'palestra', $palestra->id, 0, $grades, $params);
}
function palestra_update_grades($palestra, $userid=0, $nullifnone=true) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
$now = time();
if ($userid) {
$presences = $DB->get_records("palestra_presence", array("palestraid"=>$palestra->id, "userid"=>$userid), 'id, userid, starttime, lastcheck');
} else {
$presences = $DB->get_records_select("palestra_presence", "palestraid=? and lastcheck+? < ?", [$palestra->id, $palestra->duration*60, $now], 'userid, starttime', 'id, userid, starttime, lastcheck');
}
$audiences = array();
$minimal = $palestra->checkinterval * 60;
$user = null;
foreach($presences as $presence) {
# Compute only after palestra starts
if ($presence->starttime < $palestra->startdate) {
$presence->starttime = $palestra->startdate;
}
if ($user != $presence->userid) {
if ($user) {
$audiences[$user] = (isset($audiences[$user])?$audiences[$user]:0) + (max($end - $start, $minimal)/60);
}
$user = $presence->userid;
$start = $presence->starttime;
$end = $presence->lastcheck;
continue;
}
if ($presence->starttime < ($start + $minimal)) {
if ($end < $presence->lastcheck) {
$end = $presence->lastcheck;
}
} else {
$audiences[$user] = (isset($audiences[$user])?$audiences[$user]:0) + (max($end - $start, $minimal)/60);
$start = $presence->starttime;
$end = $presence->lastcheck;
}
}
if ($user) {
$audiences[$user] = (isset($audiences[$user])?$audiences[$user]:0) + (max($end - $start, $minimal)/60);
}
$grades = array();
foreach($audiences as $userid=>$presence_time) {
$presence_rate = $presence_time / $palestra->duration;
if ($presence_rate > 1.0) {
$presence_rate = 1.0;
}
$grade = new stdclass();
$grade->userid = $userid;
$grade->rawgrade = grade_floatval($palestra->grade * $presence_rate);
$grade->datesubmitted = $palestra->startdate;
$grade->dategraded = $palestra->startdate;
$grades[$userid] = $grade;
}
palestra_grade_item_update($palestra, $grades);
$DB->set_field('palestra', 'needsupdate', 0, array('id'=>$palestra->id));
}
function palestra_reset_gradebook($courseid, $type='') {
global $CFG, $DB;
$wheresql = '';
$params = array($courseid);
if ($type) {
$wheresql = "AND p.type=?";
$params[] = $type;
}
$sql = "SELECT p.*, cm.idnumber as cmidnumber, p.course as courseid
FROM {palestra} p, {course_modules} cm, {modules} m
WHERE m.name='palestra' AND m.id=cm.module AND cm.instance=p.id AND p.course=? $wheresql";
if ($palestras = $DB->get_records_sql($sql, $params)) {
foreach ($palestras as $palestra) {
palestra_grade_item_update($palestra, 'reset');
}
}
}
function palestra_get_user_grades($palestra, $userid = 0) {
global $CFG;
require_once($CFG->dirroot.'/rating/lib.php');
$ratingoptions = new stdClass;
$ratingoptions->component = 'mod_palestra';
$ratingoptions->ratingarea = 'post';
//need these to work backwards to get a context id. Is there a better way to get contextid from a module instance?
$ratingoptions->modulename = 'palestra';
$ratingoptions->moduleid = $palestra->id;
$ratingoptions->userid = $userid;
$ratingoptions->aggregationmethod = $palestra->assessed;
$ratingoptions->scaleid = $palestra->scale;
$ratingoptions->itemtable = 'palestra_posts';
$ratingoptions->itemtableusercolumn = 'userid';
$rm = new rating_manager();
return $rm->get_user_grades($ratingoptions);
}
function palestra_extend_settings_navigation($settings, $palestranode) {
global $PAGE, $CFG;
// We want to add these new nodes after the Edit settings node, and before the
// Locally assigned roles node. Of course, both of those are controlled by capabilities.
$keys = $palestranode->get_children_key_list();
$beforekey = null;
$i = array_search('modedit', $keys);
if ($i === false and array_key_exists(0, $keys)) {
$beforekey = $keys[0];
} else if (array_key_exists($i + 1, $keys)) {
$beforekey = $keys[$i + 1];
}
if (has_capability('mod/palestra:viewpresences', $PAGE->cm->context)) {
$url = new moodle_url('/mod/palestra/presences.php', array('cmid'=>$PAGE->cm->id));
$node = navigation_node::create(get_string('view_presences', 'palestra'),
$url,
navigation_node::TYPE_SETTING, null, 'mod_palestra_viewpresences');
$palestranode->add_node($node, $beforekey);
}
// if (has_capability('mod/quiz:manage', $PAGE->cm->context)) {
// $node = navigation_node::create(get_string('editquiz', 'quiz'),
// new moodle_url('/mod/quiz/edit.php', array('cmid'=>$PAGE->cm->id)),
// navigation_node::TYPE_SETTING, null, 'mod_quiz_edit',
// new pix_icon('t/edit', ''));
// $quiznode->add_node($node, $beforekey);
// }
// if (has_capability('mod/quiz:preview', $PAGE->cm->context)) {
// $url = new moodle_url('/mod/quiz/startattempt.php',
// array('cmid'=>$PAGE->cm->id, 'sesskey'=>sesskey()));
// $node = navigation_node::create(get_string('preview', 'quiz'), $url,
// navigation_node::TYPE_SETTING, null, 'mod_quiz_preview',
// new pix_icon('i/preview', ''));
// $quiznode->add_node($node, $beforekey);
// }
// if (has_any_capability(array('mod/quiz:viewreports', 'mod/quiz:grade'), $PAGE->cm->context)) {
// require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
// $reportlist = quiz_report_list($PAGE->cm->context);
// $url = new moodle_url('/mod/quiz/report.php',
// array('id' => $PAGE->cm->id, 'mode' => reset($reportlist)));
// $reportnode = $quiznode->add_node(navigation_node::create(get_string('results', 'quiz'), $url,
// navigation_node::TYPE_SETTING,
// null, null, new pix_icon('i/report', '')), $beforekey);
// foreach ($reportlist as $report) {
// $url = new moodle_url('/mod/quiz/report.php',
// array('id' => $PAGE->cm->id, 'mode' => $report));
// $reportnode->add_node(navigation_node::create(get_string($report, 'quiz_'.$report), $url,
// navigation_node::TYPE_SETTING,
// null, 'quiz_report_' . $report, new pix_icon('i/item', '')));
// }
// }
// question_extend_settings_navigation($quiznode, $PAGE->cm->context)->trim_if_empty();
}