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
 * Library of functions, classes and constants for module subcourse
19
 *
20
 * @package     mod_subcourse
21
 * @copyright   2008 David Mudrak <david@moodle.com>
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Returns the information if the module supports a feature
27
 *
28
 * @see plugin_supports() in lib/moodlelib.php
29
 * @param string $feature FEATURE_xx constant for requested feature
30
 * @return mixed true if the feature is supported, null if unknown
31
 */
32
function subcourse_supports($feature) {
33
 
34
    if (defined('FEATURE_MOD_PURPOSE')) {
35
        if ($feature === FEATURE_MOD_PURPOSE) {
36
            return MOD_PURPOSE_CONTENT;
37
        }
38
    }
39
 
40
    switch($feature) {
41
        case FEATURE_GRADE_HAS_GRADE:
42
            return true;
43
        case FEATURE_MOD_INTRO:
44
            return true;
45
        case FEATURE_SHOW_DESCRIPTION:
46
            return true;
47
        case FEATURE_GROUPS:
48
            return true;
49
        case FEATURE_GROUPINGS:
50
            return true;
51
        case FEATURE_GROUPMEMBERSONLY:
52
            return true;
53
        case FEATURE_BACKUP_MOODLE2:
54
            return true;
55
        case FEATURE_COMPLETION_TRACKS_VIEWS:
56
            return true;
57
        case FEATURE_COMPLETION_HAS_RULES:
58
            return true;
59
        default:
60
            return null;
61
    }
62
}
63
 
64
/**
65
 * Given an object containing all the necessary data, (defined by the form)
66
 * this function will create a new instance and return the id number of the new
67
 * instance.
68
 *
69
 * @param stdClass $subcourse
70
 * @return int The id of the newly inserted subcourse record
71
 */
72
function subcourse_add_instance(stdClass $subcourse) {
73
    global $CFG, $DB;
74
    require_once($CFG->dirroot.'/mod/subcourse/locallib.php');
75
 
76
    $subcourse->timecreated = time();
77
 
78
    if (empty($subcourse->instantredirect)) {
79
        $subcourse->instantredirect = 0;
80
    }
81
 
82
    if (empty($subcourse->blankwindow)) {
83
        $subcourse->blankwindow = 0;
84
    }
85
 
86
    if (empty($subcourse->coursepageprintprogress)) {
87
        $subcourse->coursepageprintprogress = 0;
88
    }
89
 
90
    if (empty($subcourse->coursepageprintgrade)) {
91
        $subcourse->coursepageprintgrade = 0;
92
    }
93
 
94
    $newid = $DB->insert_record("subcourse", $subcourse);
95
 
96
    if (!empty($subcourse->refcourse)) {
97
        // Create grade_item but do not fetch grades.
98
        // The context does not exist yet and we can't get users by capability.
99
        subcourse_grades_update($subcourse->course, $newid, $subcourse->refcourse, $subcourse->name, true);
100
    }
101
 
102
    if (!empty($subcourse->completionexpected)) {
103
        \core_completion\api::update_completion_date_event($subcourse->coursemodule, 'subcourse', $newid,
104
            $subcourse->completionexpected);
105
    }
106
 
107
    return $newid;
108
}
109
 
110
/**
111
 * Given an object containing all the necessary data, (defined by the form)
112
 * this function will update an existing instance with new data.
113
 *
114
 * @param stdClass $subcourse
115
 * @return boolean success/failure
116
 */
117
function subcourse_update_instance(stdClass $subcourse) {
118
    global $CFG, $DB;
119
    require_once($CFG->dirroot.'/mod/subcourse/locallib.php');
120
 
121
    $cmid = $subcourse->coursemodule;
122
 
123
    $subcourse->timemodified = time();
124
    $subcourse->id = $subcourse->instance;
125
 
126
    if (!empty($subcourse->refcoursecurrent)) {
127
        unset($subcourse->refcourse);
128
    }
129
 
130
    if (empty($subcourse->instantredirect)) {
131
        $subcourse->instantredirect = 0;
132
    }
133
 
134
    if (empty($subcourse->blankwindow)) {
135
        $subcourse->blankwindow = 0;
136
    }
137
 
138
    if (empty($subcourse->coursepageprintprogress)) {
139
        $subcourse->coursepageprintprogress = 0;
140
    }
141
 
142
    if (empty($subcourse->coursepageprintgrade)) {
143
        $subcourse->coursepageprintgrade = 0;
144
    }
145
 
146
    $DB->update_record('subcourse', $subcourse);
147
 
148
    if (!empty($subcourse->refcourse)) {
149
        if (has_capability('mod/subcourse:fetchgrades', context_module::instance($cmid))) {
150
            subcourse_grades_update($subcourse->course, $subcourse->id, $subcourse->refcourse, $subcourse->name,
151
                false, false, [], $subcourse->fetchpercentage);
152
            subcourse_update_timefetched($subcourse->id);
153
        }
154
    }
155
 
156
    \core_completion\api::update_completion_date_event($cmid, 'subcourse', $subcourse->id, $subcourse->completionexpected);
157
 
158
    return true;
159
}
160
 
161
/**
162
 * Given an ID of an instance of this module,
163
 * this function will permanently delete the instance
164
 * and any data that depends on it.
165
 *
166
 * @param int $id Id of the module instance
167
 * @return boolean success/failure
168
 */
169
function subcourse_delete_instance($id) {
170
    global $CFG, $DB;
171
    require_once($CFG->libdir.'/gradelib.php');
172
 
173
    // Check the instance exists.
174
    if (!$subcourse = $DB->get_record("subcourse", ["id" => $id])) {
175
        return false;
176
    }
177
 
178
    // Remove the instance record.
179
    $DB->delete_records("subcourse", ["id" => $subcourse->id]);
180
 
181
    // Clean up the gradebook items.
182
    grade_update('mod/subcourse', $subcourse->course, 'mod', 'subcourse', $subcourse->id, 0, null, ['deleted' => true]);
183
 
184
    return true;
185
}
186
 
187
/**
188
 * Must return an array of user records (all data) who are participants
189
 * for a given instance of subcourse. Must include every user involved
190
 * in the instance, independient of his role (student, teacher, admin...)
191
 * See other modules as example.
192
 *
193
 * @param int $subcourseid ID of an instance of this module
194
 * @return mixed boolean/array of students
195
 */
196
function subcourse_get_participants($subcourseid) {
197
    return false;
198
}
199
 
200
/**
201
 * Return a small object with summary information about what a
202
 * user has done with a given particular instance of this module
203
 * Used for user activity reports.
204
 * $return->time = the time they did it
205
 * $return->info = a short text description
206
 *
207
 * @param stdClass $course The course record.
208
 * @param stdClass $user The user record.
209
 * @param cm_info|stdClass $mod The course module info object or record.
210
 * @param stdClass $subcourse The subcourse instance record.
211
 * @return null
212
 */
213
function subcourse_user_outline($course, $user, $mod, $subcourse) {
214
    return true;
215
}
216
 
217
/**
218
 * Print a detailed representation of what a user has done with
219
 * a given particular instance of this module, for user activity reports.
220
 *
221
 * @param stdClass $course The course record.
222
 * @param stdClass $user The user record.
223
 * @param cm_info|stdClass $mod The course module info object or record.
224
 * @param stdClass $subcourse The subcourse instance record.
225
 * @return boolean
226
 */
227
function subcourse_user_complete($course, $user, $mod, $subcourse) {
228
    return true;
229
}
230
 
231
/**
232
 * Given a course and a time, this module should find recent activity
233
 * that has occurred in subcourse activities and print it out.
234
 * Return true if there was output, or false is there was none.
235
 *
236
 * @param stdClass $course
237
 * @param bool $viewfullnames
238
 * @param int $timestart
239
 * @return boolean true if anything was printed, otherwise false
240
 */
241
function subcourse_print_recent_activity($course, $viewfullnames, $timestart) {
242
    return false;
243
}
244
 
245
/**
246
 * Is a scale used by the given subcourse instance?
247
 *
248
 * The subcourse itself does not generate grades so we always return
249
 * false here in order not to block the scale removal.
250
 *
251
 * @param int $subcourseid id of an instance of this module
252
 * @param int $scaleid
253
 * @return bool
254
 */
255
function subcourse_scale_used($subcourseid, $scaleid) {
256
    return false;
257
}
258
 
259
/**
260
 * Is a scale used by some subcourse instance?
261
 *
262
 * The subcourse itself does not generate grades so we always return
263
 * false here in order not to block the scale removal.
264
 *
265
 * @param int $scaleid
266
 * @return boolean True if the scale is used by any subcourse
267
 */
268
function subcourse_scale_used_anywhere($scaleid) {
269
    return false;
270
}
271
 
272
/**
273
 * This will provide summary info about the user's grade in the subcourse below the link on
274
 * the course/view.php page
275
 *
276
 * @param cm_info $cm
277
 * @return void
278
 */
279
function mod_subcourse_cm_info_view(cm_info $cm) {
280
    global $CFG, $USER, $DB;
281
 
282
    if (isset($cm->customdata->coursepageprintgrade) && isset($cm->customdata->coursepageprintprogress)) {
283
        $displayoptions = (object) [
284
            'coursepageprintgrade' => $cm->customdata->coursepageprintgrade,
285
            'coursepageprintprogress' => $cm->customdata->coursepageprintprogress,
286
        ];
287
 
288
    } else {
289
        // This is unexpected - the customdata should be set in {@see subcourse_get_coursemodule_info()}.
290
        $displayoptions = $DB->get_record('subcourse', ['id' => $cm->instance], 'coursepageprintgrade, coursepageprintprogress');
291
    }
292
 
293
    $html = '';
294
 
295
    if ($displayoptions->coursepageprintprogress) {
296
        $sql = "SELECT r.*
297
                  FROM {course} r
298
                  JOIN {subcourse} s ON s.refcourse = r.id
299
                 WHERE s.id = :subcourseid";
300
 
301
        $refcourse = $DB->get_record_sql($sql, ['subcourseid' => $cm->instance], IGNORE_MISSING);
302
        $percentage = null;
303
        if ($refcourse) {
304
            $percentage = \core_completion\progress::get_course_progress_percentage($refcourse);
305
        }
306
        if ($percentage !== null) {
307
            $percentage = floor($percentage);
308
            $html .= html_writer::tag('div', get_string('currentprogress', 'subcourse', $percentage),
309
                ['class' => 'contentafterlink']);
310
        }
311
    }
312
 
313
    if ($displayoptions->coursepageprintgrade) {
314
        require_once($CFG->libdir.'/gradelib.php');
315
 
316
        $grades = grade_get_grades($cm->course, 'mod', 'subcourse', $cm->instance, $USER->id);
317
        $currentgrade = (empty($grades->items[0]->grades)) ? null : reset($grades->items[0]->grades);
318
 
319
        if (($currentgrade !== null) && isset($currentgrade->grade) && !($currentgrade->hidden)) {
320
            $strgrade = $currentgrade->str_grade;
321
            $html .= html_writer::tag('div', get_string('currentgrade', 'subcourse', $strgrade),
322
                ['class' => 'contentafterlink']);
323
        }
324
    }
325
 
326
    if ($html !== '') {
327
        $cm->set_after_link($html);
328
    }
329
}
330
 
331
/**
332
 * Return the action associated with the given calendar event, or null if there is none.
333
 *
334
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
335
 * is not displayed on the block.
336
 *
337
 * @param calendar_event $event
338
 * @param \core_calendar\action_factory $factory
339
 * @return \core_calendar\local\event\entities\action_interface|null
340
 */
341
function mod_subcourse_core_calendar_provide_event_action(calendar_event $event, \core_calendar\action_factory $factory) {
342
 
343
    $cm = get_fast_modinfo($event->courseid)->instances['subcourse'][$event->instance];
344
 
345
    return $factory->create_instance(
346
        get_string('view'),
347
        new \moodle_url('/mod/subcourse/view.php', ['id' => $cm->id]),
348
        1,
349
        true
350
    );
351
}
352
 
353
/**
354
 * Given a course_module object, this function returns any
355
 * "extra" information that may be needed when printing
356
 * this activity in a course listing.
357
 *
358
 * See {@see get_array_of_activities()} in course/lib.php
359
 *
360
 * @param object $coursemodule
361
 * @return cached_cm_info info
362
 */
363
function subcourse_get_coursemodule_info($coursemodule) {
364
    global $CFG, $DB;
365
 
366
    $subcourse = $DB->get_record('subcourse', ['id' => $coursemodule->instance],
367
        'id, name, intro, introformat, instantredirect, blankwindow, coursepageprintgrade, coursepageprintprogress');
368
 
369
    if (!$subcourse) {
370
        return null;
371
    }
372
 
373
    $info = new cached_cm_info();
374
    $info->name = $subcourse->name;
375
    $info->customdata = (object) [
376
        'coursepageprintgrade' => $subcourse->coursepageprintgrade,
377
        'coursepageprintprogress' => $subcourse->coursepageprintprogress,
378
    ];
379
 
380
    if ($subcourse->instantredirect && $subcourse->blankwindow) {
381
        $url = new moodle_url('/mod/subcourse/view.php', ['id' => $coursemodule->id, 'isblankwindow' => 1]);
382
        $info->onclick = "window.open('".$url->out(false)."'); return false;";
383
    }
384
 
385
    if ($coursemodule->showdescription) {
386
        // Set content from intro and introformat. Filters are disabled because we filter with format_text at display time.
387
        $info->content = format_module_intro('subcourse', $subcourse, $coursemodule->id, false);
388
    }
389
 
390
    return $info;
391
}
392
 
393
 
394
/**
395
 * Create or update the grade item for given subcourse
396
 *
397
 * @category grade
398
 * @param object $subcourse object
399
 * @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
400
 * @return int 0 if ok, error code otherwise
401
 */
402
function subcourse_grade_item_update($subcourse, $grades = null) {
403
    global $CFG;
404
    require_once($CFG->dirroot . '/mod/subcourse/locallib.php');
405
 
406
    $reset = false;
407
    if ($grades === 'reset') {
408
        $reset = true;
409
    }
410
    $gradeitemonly = true;
411
    if (!empty($grades)) {
412
        $gradeitemonly = false;
413
    }
414
    return subcourse_grades_update($subcourse->course, $subcourse->id, $subcourse->refcourse,
415
        $subcourse->name, $gradeitemonly, $reset);
416
}
417
 
418
/**
419
 * Update activity grades.
420
 *
421
 * @param stdClass $subcourse subcourse record
422
 * @param int $userid specific user only, 0 means all
423
 * @param bool $nullifnone - not used
424
 */
425
function subcourse_update_grades($subcourse, $userid=0, $nullifnone=true) {
426
    global $CFG;
427
    require_once($CFG->dirroot . '/mod/subcourse/locallib.php');
428
    require_once($CFG->libdir.'/gradelib.php');
429
 
430
    $refgrades = subcourse_fetch_refgrades($subcourse->id, $subcourse->refcourse, false, $userid, false);
431
 
432
    if ($refgrades && $refgrades->grades) {
433
        if (!empty($refgrades->localremotescale)) {
434
            // Unable to fetch remote grades - local scale is used in the remote course.
435
            return GRADE_UPDATE_FAILED;
436
        }
437
        return subcourse_grade_item_update($subcourse, $refgrades->grades);
438
    } else {
439
        return subcourse_grade_item_update($subcourse);
440
    }
441
}