Repository navigation
Expand file tree
/
Copy pathlib.php
More file actions
351 lines (298 loc) · 12.6 KB
/
Copy pathlib.php
File metadata and controls
351 lines (298 loc) · 12.6 KB
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
<?php
/**
* Library functions for Self Discovery Tracker module
*
* @package mod_selfdiscoverytracker
* @copyright 2026 SAVIO - Sistema de Aprendizaje Virtual Interactivo (UTB)
* @author SAVIO Development Team
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Supports the module.
*
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, false if not, null if doesn't know
*/
function selfdiscoverytracker_supports($feature) {
switch($feature) {
case FEATURE_MOD_INTRO: return true;
case FEATURE_SHOW_DESCRIPTION: 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_GRADE_OUTCOMES: return true;
case FEATURE_BACKUP_MOODLE2: return true;
default: return null;
}
}
/**
* Dynamically updates course-module information for the current user.
*
* We use this to suppress completion UI for teacher/admin users so they don't
* see student-oriented "to do"/"done" completion prompts in the course page.
*
* @param cm_info $cm
* @return void
*/
function selfdiscoverytracker_cm_info_dynamic(cm_info $cm): void {
$coursecontext = context_course::instance($cm->course);
$isteacheroradmin = has_capability('moodle/course:update', $coursecontext) ||
has_capability('moodle/course:manageactivities', $coursecontext) ||
has_capability('moodle/course:viewhiddenactivities', $coursecontext) ||
has_capability('moodle/site:config', $coursecontext);
if (!$isteacheroradmin) {
// Students should see this activity embedded without a view link.
$cm->set_no_view_link();
}
}
/**
* Prefered callback name (Moodle checks mod_{modname}_* first).
*
* @param cm_info $cm
* @return void
*/
function mod_selfdiscoverytracker_cm_info_dynamic(cm_info $cm): void {
selfdiscoverytracker_cm_info_dynamic($cm);
}
/**
* Adds per-user view-only data for the course page.
*
* We can't reliably alter completion tracking flags per-user (they are cached/static),
* but we can add an extra CSS class and hide the completion UI in the course page.
*
* @param cm_info $cm
* @return void
*/
function selfdiscoverytracker_cm_info_view(cm_info $cm): void {
global $USER;
$coursecontext = context_course::instance($cm->course);
$isteacheroradmin = has_capability('moodle/course:update', $coursecontext) ||
has_capability('moodle/course:manageactivities', $coursecontext) ||
has_capability('moodle/course:viewhiddenactivities', $coursecontext) ||
has_capability('moodle/site:config', $coursecontext);
if ($isteacheroradmin) {
$existing = trim($cm->extraclasses ?? '');
$cm->set_extra_classes(trim($existing . ' sdt-hidecompletion'));
} else {
global $PAGE, $CFG;
// For students, we check completion status dynamically on course view
// to ensure it updates immediately without page refresh.
if ($cm->completion == COMPLETION_TRACKING_AUTOMATIC && isloggedin() && !isguestuser()) {
// We use the class loader or include checking to stay safe
if (class_exists('\mod_selfdiscoverytracker\tracker_helper')) {
\mod_selfdiscoverytracker\tracker_helper::check_and_update_completion($cm, $USER->id);
} else {
require_once($CFG->dirroot . '/mod/selfdiscoverytracker/classes/tracker_helper.php');
\mod_selfdiscoverytracker\tracker_helper::check_and_update_completion($cm, $USER->id);
}
}
// Render dashboard inline for students on the course page.
if ($cm->uservisible && has_capability('mod/selfdiscoverytracker:view', $cm->context) &&
isset($PAGE) && $PAGE->context && $PAGE->context->contextlevel == CONTEXT_COURSE &&
$PAGE->context->instanceid == $cm->course) {
if (!class_exists('\mod_selfdiscoverytracker\tracker_helper')) {
require_once($CFG->dirroot . '/mod/selfdiscoverytracker/classes/tracker_helper.php');
}
try {
$progress_data = \mod_selfdiscoverytracker\tracker_helper::get_all_tests_progress($USER->id);
$course = $cm->get_modinfo()->get_course();
$output = $PAGE->get_renderer('mod_selfdiscoverytracker');
$dashboard = new \mod_selfdiscoverytracker\output\dashboard($course, $cm, $progress_data, false);
// Inject the dashboard HTML into the content area of the module on the course page.
$cm->set_content($output->render($dashboard), true);
$cm->set_extra_classes(trim(($cm->extraclasses ?? '') . ' sdt-inline-dashboard'));
} catch (Exception $e) {
// Fail silently to avoid breaking the course page.
}
}
}
}
/**
* Prefered callback name (Moodle checks mod_{modname}_* first).
*
* @param cm_info $cm
* @return void
*/
function mod_selfdiscoverytracker_cm_info_view(cm_info $cm): void {
selfdiscoverytracker_cm_info_view($cm);
}
/**
* Adds a new instance of the selfdiscoverytracker activity.
*
* @param stdClass $selfdiscoverytracker The object containing the data to be inserted
* @return int The id of the new instance
*/
function selfdiscoverytracker_add_instance($selfdiscoverytracker) {
global $DB;
// Institutional grading policy: this activity can only award 0 or 5.
$selfdiscoverytracker->grade = 5;
$selfdiscoverytracker->timecreated = time();
$selfdiscoverytracker->timemodified = time();
$selfdiscoverytracker->id = $DB->insert_record('selfdiscoverytracker', $selfdiscoverytracker);
selfdiscoverytracker_grade_item_update($selfdiscoverytracker);
return $selfdiscoverytracker->id;
}
/**
* Updates an instance of the selfdiscoverytracker activity.
*
* @param stdClass $selfdiscoverytracker The object containing the data to be updated
* @return boolean True on success, false on failure
*/
function selfdiscoverytracker_update_instance($selfdiscoverytracker) {
global $DB;
// Keep the activity's scale fixed even if a form submission is altered.
$selfdiscoverytracker->grade = 5;
$selfdiscoverytracker->timemodified = time();
$selfdiscoverytracker->id = $selfdiscoverytracker->instance;
if (!$DB->update_record('selfdiscoverytracker', $selfdiscoverytracker)) {
return false;
}
selfdiscoverytracker_grade_item_update($selfdiscoverytracker);
selfdiscoverytracker_update_grades($selfdiscoverytracker);
return true;
}
/**
* Deletes an instance of the selfdiscoverytracker activity.
*
* @param int $id The id of the instance to be deleted
* @return boolean True on success, false on failure
*/
function selfdiscoverytracker_delete_instance($id) {
global $DB;
if (!$selfdiscoverytracker = $DB->get_record('selfdiscoverytracker', array('id' => $id))) {
return false;
}
$DB->delete_records('selfdiscoverytracker', array('id' => $selfdiscoverytracker->id));
selfdiscoverytracker_grade_item_delete($selfdiscoverytracker);
return true;
}
/**
* Obtains the automatic completion state for this module based on any conditions
* in selfdiscoverytracker settings.
*
* @param stdClass $course Course
* @param stdClass $cm Course-module
* @param int $userid User ID
* @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
* @return bool True if completed, false if not, $type if conditions not set.
*/
function selfdiscoverytracker_get_completion_state($course, $cm, $userid, $type) {
global $DB, $CFG;
$selfdiscoverytracker = $DB->get_record('selfdiscoverytracker', array('id' => $cm->instance));
if ($selfdiscoverytracker && $selfdiscoverytracker->completionalltests) {
require_once($CFG->dirroot . '/mod/selfdiscoverytracker/classes/tracker_helper.php');
$all_completed = \mod_selfdiscoverytracker\tracker_helper::are_all_tests_completed($userid);
return $all_completed;
}
return $type;
}
/**
* Create or update the grade item for given selfdiscoverytracker instance.
*
* @param stdClass $selfdiscoverytracker
* @param mixed $grades Optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function selfdiscoverytracker_grade_item_update($selfdiscoverytracker, $grades=null) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
$params = array('itemname' => $selfdiscoverytracker->name, 'idnumber' => $selfdiscoverytracker->course);
// The institutional scale is binary: 0 until every exploration is complete,
// then 5. Keep the gradebook item fixed to that maximum for every instance.
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = 5;
$params['grademin'] = 0;
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update('mod/selfdiscoverytracker', $selfdiscoverytracker->course, 'mod', 'selfdiscoverytracker', $selfdiscoverytracker->id, 0, $grades, $params);
}
/**
* Delete grade item for given selfdiscoverytracker instance.
*
* @param stdClass $selfdiscoverytracker
* @return int 0 if ok, error code otherwise
*/
function selfdiscoverytracker_grade_item_delete($selfdiscoverytracker) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
return grade_update('mod/selfdiscoverytracker', $selfdiscoverytracker->course, 'mod', 'selfdiscoverytracker', $selfdiscoverytracker->id, 0, null, array('deleted'=>1));
}
/**
* Update grades in gradebook.
*
* @param stdClass $selfdiscoverytracker
* @param int $userid Specific user only, 0 means all
* @param bool $nullifnone If true and user has no grade, set grade to null
*/
function selfdiscoverytracker_update_grades($selfdiscoverytracker, $userid=0, $nullifnone=true) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->dirroot.'/mod/selfdiscoverytracker/classes/tracker_helper.php');
$grades = array();
if ($userid) {
$users = array($userid);
} else {
// This can be very slow if many users.
// We get all users capable of being graded.
// We need cmid. If not in object, try to find it.
$cm = get_coursemodule_from_instance('selfdiscoverytracker', $selfdiscoverytracker->id, $selfdiscoverytracker->course);
$context = context_module::instance($cm->id);
$users = get_enrolled_users($context, 'mod/selfdiscoverytracker:view', 0, 'u.id');
$users = array_keys($users);
}
foreach ($users as $uid) {
// Partial completion never receives a partial grade. A student obtains
// the maximum grade only after completing all five explorations.
$final_grade = \mod_selfdiscoverytracker\tracker_helper::are_all_tests_completed($uid) ? 5 : 0;
$grades[$uid] = new stdClass();
$grades[$uid]->userid = $uid;
$grades[$uid]->rawgrade = $final_grade;
}
selfdiscoverytracker_grade_item_update($selfdiscoverytracker, $grades);
}
/**
* Extends the settings navigation with the selfdiscoverytracker settings.
*
* @param settings_navigation $settingsnav
* @param navigation_node $selfdiscoverytrackernode
* @return void
*/
function selfdiscoverytracker_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $selfdiscoverytrackernode) {
global $PAGE, $DB;
// El enlace conduce a progreso sensible de estudiantes.
$coursecontext = context_course::instance($PAGE->course->id);
if (!has_capability('mod/selfdiscoverytracker:viewstudentdata', $coursecontext)) {
return;
}
$cm = $PAGE->cm;
if (!$cm) {
return;
}
// Ensure we have the instance.
$grade_item = $DB->get_record('grade_items', array(
'itemtype' => 'mod',
'itemmodule' => 'selfdiscoverytracker',
'iteminstance' => $cm->instance,
'courseid' => $cm->course
));
if ($grade_item) {
// Link to the Single View grade report for this item.
$url = new moodle_url('/grade/report/singleview/index.php', array(
'id' => $cm->course,
'item' => 'grade',
'itemid' => $grade_item->id
));
// In Moodle 4.0+ boosting theme, adding to this node creates a secondary nav tab.
$selfdiscoverytrackernode->add(
get_string('grades'),
$url,
navigation_node::TYPE_SETTING,
null,
'selfdiscoverytrackergrades',
new pix_icon('i/grades', '')
);
}
}