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
 * Activity progress reports
19
 *
20
 * @package    report
21
 * @subpackage progress
22
 * @copyright  2008 Sam Marshall
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
use core\report_helper;
27
use \report_progress\local\helper;
28
 
29
require('../../config.php');
30
require_once($CFG->libdir . '/completionlib.php');
31
 
32
// Get course
33
$id = required_param('course',PARAM_INT);
34
$course = $DB->get_record('course',array('id'=>$id));
35
if (!$course) {
36
    throw new \moodle_exception('invalidcourseid');
37
}
38
$context = context_course::instance($course->id);
39
 
40
// Sort (default lastname, optionally firstname)
41
$sort = optional_param('sort','',PARAM_ALPHA);
42
$firstnamesort = $sort == 'firstname';
43
 
44
// CSV format
45
$format = optional_param('format','',PARAM_ALPHA);
46
$excel = $format == 'excelcsv';
47
$csv = $format == 'csv' || $excel;
48
 
49
// Paging, sorting and filtering.
50
$page   = optional_param('page', 0, PARAM_INT);
51
$sifirst = optional_param('sifirst', 'all', PARAM_NOTAGS);
52
$silast  = optional_param('silast', 'all', PARAM_NOTAGS);
53
$groupid = optional_param('group', 0, PARAM_INT);
54
$activityinclude = optional_param('activityinclude', 'all', PARAM_TEXT);
55
$activityorder = optional_param('activityorder', 'orderincourse', PARAM_TEXT);
56
$activitysection = optional_param('activitysection', -1, PARAM_INT);
57
 
58
// Whether to show extra user identity information
59
$userfields = \core_user\fields::for_identity($context);
60
$extrafields = $userfields->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
61
$leftcols = 1 + count($extrafields);
62
 
63
function csv_quote($value) {
64
    global $excel;
65
    if ($excel) {
66
        return core_text::convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
67
    } else {
68
        return '"'.str_replace('"',"'",$value).'"';
69
    }
70
}
71
 
72
$url = new moodle_url('/report/progress/index.php', array('course'=>$id));
73
$PAGE->navigation->override_active_url($url);
74
if ($sort !== '') {
75
    $url->param('sort', $sort);
76
}
77
if ($format !== '') {
78
    $url->param('format', $format);
79
}
80
if ($page !== 0) {
81
    $url->param('page', $page);
82
}
83
if ($sifirst !== 'all') {
84
    $url->param('sifirst', $sifirst);
85
}
86
if ($silast !== 'all') {
87
    $url->param('silast', $silast);
88
}
89
if ($groupid !== 0) {
90
    $url->param('group', $groupid);
91
}
92
if ($activityinclude !== '') {
93
    $url->param('activityinclude', $activityinclude);
94
}
95
if ($activityorder !== '') {
96
    $url->param('activityorder', $activityorder);
97
}
98
if ($activitysection !== '') {
99
    $url->param('activitysection', $activitysection);
100
}
101
 
102
$PAGE->set_url($url);
103
$PAGE->set_pagelayout('report');
104
 
105
require_login($course);
106
 
107
// Check basic permission
108
require_capability('report/progress:view',$context);
109
 
110
// Get group mode
111
$group = groups_get_course_group($course,true); // Supposed to verify group
112
if ($group===0 && $course->groupmode==SEPARATEGROUPS) {
113
    require_capability('moodle/site:accessallgroups',$context);
114
}
115
 
116
// Get data on activities and progress of all users, and give error if we've
117
// nothing to display (no users or no activities).
118
$completion = new completion_info($course);
119
list($activitytypes, $activities) = helper::get_activities_to_show($completion, $activityinclude, $activityorder, $activitysection);
120
$output = $PAGE->get_renderer('report_progress');
121
 
122
if ($sifirst !== 'all') {
123
    set_user_preference('ifirst', $sifirst);
124
}
125
if ($silast !== 'all') {
126
    set_user_preference('ilast', $silast);
127
}
128
 
129
if (!empty($USER->preference['ifirst'])) {
130
    $sifirst = $USER->preference['ifirst'];
131
} else {
132
    $sifirst = 'all';
133
}
134
 
135
if (!empty($USER->preference['ilast'])) {
136
    $silast = $USER->preference['ilast'];
137
} else {
138
    $silast = 'all';
139
}
140
 
141
// Generate where clause
142
$where = array();
143
$where_params = array();
144
 
145
if ($sifirst !== 'all') {
146
    $where[] = $DB->sql_like('u.firstname', ':sifirst', false, false);
147
    $where_params['sifirst'] = $sifirst.'%';
148
}
149
 
150
if ($silast !== 'all') {
151
    $where[] = $DB->sql_like('u.lastname', ':silast', false, false);
152
    $where_params['silast'] = $silast.'%';
153
}
154
 
155
// Get user match count
156
$total = $completion->get_num_tracked_users(implode(' AND ', $where), $where_params, $group);
157
 
158
// Total user count
159
$grandtotal = $completion->get_num_tracked_users('', array(), $group);
160
 
161
// Get user data
162
$progress = array();
163
 
164
if ($total) {
165
    $progress = $completion->get_progress_all(
166
        implode(' AND ', $where),
167
        $where_params,
168
        $group,
169
        $firstnamesort ? 'u.firstname ASC, u.lastname ASC' : 'u.lastname ASC, u.firstname ASC',
170
        $csv ? 0 : helper::COMPLETION_REPORT_PAGE,
171
        $csv ? 0 : $page * helper::COMPLETION_REPORT_PAGE,
172
        $context
173
    );
174
}
175
 
176
if ($csv && $grandtotal && count($activities)>0) { // Only show CSV if there are some users/actvs
177
 
178
    $shortname = format_string($course->shortname, true, array('context' => $context));
179
    header('Content-Disposition: attachment; filename=progress.'.
180
        preg_replace('/[^a-z0-9-]/','_',core_text::strtolower(strip_tags($shortname))).'.csv');
181
    // Unicode byte-order mark for Excel
182
    if ($excel) {
183
        header('Content-Type: text/csv; charset=UTF-16LE');
184
        print chr(0xFF).chr(0xFE);
185
        $sep="\t".chr(0);
186
        $line="\n".chr(0);
187
    } else {
188
        header('Content-Type: text/csv; charset=UTF-8');
189
        $sep=",";
190
        $line="\n";
191
    }
192
} else {
193
 
194
    // Navigation and header
195
    $strreports = get_string("reports");
196
    $strcompletion = get_string('activitycompletion', 'completion');
197
 
198
    $PAGE->set_title($strcompletion);
199
    $PAGE->set_heading($course->fullname);
200
    echo $OUTPUT->header();
201
 
202
    // Print the selected dropdown.
203
    $pluginname = get_string('pluginname', 'report_progress');
204
    report_helper::print_report_selector($pluginname);
205
    $PAGE->requires->js_call_amd('report_progress/completion_override', 'init', [fullname($USER)]);
206
 
207
    // Handle groups (if enabled).
208
    echo $output->render_groups_select($url, $course);
209
 
210
    // Display include activity filter.
211
    echo $output->render_include_activity_select($url, $activitytypes, $activityinclude);
212
 
213
    // Display activity order options.
214
    echo $output->render_activity_order_select($url, $activityorder);
215
 
216
    // Display section selector.
217
    $modinfo = get_fast_modinfo($course);
218
    $sections = [];
219
    $cmids = array_keys($completion->get_activities());
220
    foreach ($modinfo->get_sections() as $sectionnum => $section) {
221
        if (empty(array_intersect($section, $cmids))) {
222
            continue;
223
        }
224
        $sectionname = get_section_name($course, $sectionnum);
225
        if (empty($sectionname)) {
226
            $sectionname = get_string('section') . ' ' . $sectionnum;
227
        }
228
        $sections[$sectionnum] = $sectionname;
229
    }
230
    echo $output->render_activity_section_select($url, $activitysection, $sections);
231
}
232
 
233
if (count($activities)==0) {
234
    echo $OUTPUT->container(get_string('err_noactivities', 'completion'), 'errorbox errorboxcontent');
235
    echo $OUTPUT->footer();
236
    exit;
237
}
238
 
239
// If no users in this course what-so-ever
240
if (!$grandtotal) {
241
    echo $OUTPUT->container(get_string('err_nousers', 'completion'), 'errorbox errorboxcontent');
242
    echo $OUTPUT->footer();
243
    exit;
244
}
245
 
246
// Build link for paging
247
$link = $CFG->wwwroot.'/report/progress/?course='.$course->id;
248
if (strlen($sort)) {
249
    $link .= '&amp;sort='.$sort;
250
}
251
$link .= '&amp;start=';
252
 
253
$pagingbar = '';
254
 
255
// Initials bar.
256
$prefixfirst = 'sifirst';
257
$prefixlast = 'silast';
258
 
259
// The URL used in the initials bar should reset the 'start' parameter.
260
$initialsbarurl = fullclone($url);
261
$initialsbarurl->remove_params('page');
262
 
263
$pagingbar .= $OUTPUT->initials_bar($sifirst, 'firstinitial mt-2', get_string('firstname'), $prefixfirst, $initialsbarurl);
264
$pagingbar .= $OUTPUT->initials_bar($silast, 'lastinitial', get_string('lastname'), $prefixlast, $initialsbarurl);
265
$pagingbar .= $OUTPUT->paging_bar($total, $page, helper::COMPLETION_REPORT_PAGE, $url);
266
 
267
// Okay, let's draw the table of progress info,
268
 
269
// Start of table
270
if (!$csv) {
271
    print '<br class="clearer"/>'; // ugh
272
 
273
    print $pagingbar;
274
 
275
    if (!$total) {
276
        echo $OUTPUT->notification(get_string('nothingtodisplay'), 'info', false);
277
        echo $OUTPUT->footer();
278
        exit;
279
    }
280
 
281
    print '<div id="completion-progress-wrapper" class="no-overflow">';
282
    print '<table id="completion-progress" class="generaltable flexible boxaligncenter"><thead><tr style="vertical-align:top">';
283
 
284
    // User heading / sort option
285
    print '<th scope="col" class="completion-sortchoice">';
286
 
287
    $sorturl = fullclone($url);
288
    if ($firstnamesort) {
289
        $sorturl->param('sort', 'lastname');
290
        $sortlink = html_writer::link($sorturl, get_string('lastname'));
291
        print
292
            get_string('firstname') . " / $sortlink";
293
    } else {
294
        $sorturl->param('sort', 'firstname');
295
        $sortlink = html_writer::link($sorturl, get_string('firstname'));
296
        print "$sortlink / " . get_string('lastname');
297
    }
298
    print '</th>';
299
 
300
    // Print user identity columns
301
    foreach ($extrafields as $field) {
302
        echo '<th scope="col" class="completion-identifyfield">' .
303
                \core_user\fields::get_display_name($field) . '</th>';
304
    }
305
} else {
306
    foreach ($extrafields as $field) {
307
        echo $sep . csv_quote(\core_user\fields::get_display_name($field));
308
    }
309
}
310
 
311
// Activities
312
$formattedactivities = array();
313
foreach($activities as $activity) {
314
    $datepassed = $activity->completionexpected && $activity->completionexpected <= time();
315
    $datepassedclass = $datepassed ? 'completion-expired' : '';
316
 
317
    if ($activity->completionexpected) {
318
        if ($csv) {
319
            $datetext = userdate($activity->completionexpected, "%F %T");
320
        } else {
321
            $datetext = userdate($activity->completionexpected, get_string('strftimedate', 'langconfig'));
322
        }
323
    } else {
324
        $datetext='';
325
    }
326
 
327
    // Some names (labels) come URL-encoded and can be very long, so shorten them
328
    $displayname = format_string($activity->name, true, array('context' => $activity->context));
329
 
330
    if ($csv) {
331
        print $sep.csv_quote($displayname).$sep.csv_quote($datetext);
332
    } else {
333
        $shortenedname = shorten_text($displayname);
334
        print '<th scope="col" class="completion-header '.$datepassedclass.'">'.
335
            '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
336
            '/view.php?id='.$activity->id.'" title="' . s($displayname) . '">'.
337
            '<div class="rotated-text-container"><span class="rotated-text">'.$shortenedname.'</span></div>'.
338
            '<div class="modicon">'.
339
            $OUTPUT->image_icon('monologo', get_string('modulename', $activity->modname), $activity->modname) .
340
            '</div>'.
341
            '</a>';
342
        if ($activity->completionexpected) {
343
            print '<div class="completion-expected"><span>'.$datetext.'</span></div>';
344
        }
345
        print '</th>';
346
    }
347
    $formattedactivities[$activity->id] = (object)array(
348
        'datepassedclass' => $datepassedclass,
349
        'displayname' => $displayname,
350
    );
351
}
352
 
353
if ($csv) {
354
    print $line;
355
} else {
356
    print '</tr></thead><tbody>';
357
}
358
 
359
// Row for each user
360
foreach($progress as $user) {
361
    // User name
362
    if ($csv) {
363
        print csv_quote(fullname($user, has_capability('moodle/site:viewfullnames', $context)));
364
        foreach ($extrafields as $field) {
365
            echo $sep . csv_quote($user->{$field});
366
        }
367
    } else {
368
        print '<tr><th scope="row"><a href="' . $CFG->wwwroot . '/user/view.php?id=' .
369
            $user->id . '&amp;course=' . $course->id . '">' .
370
            fullname($user, has_capability('moodle/site:viewfullnames', $context)) . '</a></th>';
371
        foreach ($extrafields as $field) {
372
            echo '<td>' . s($user->{$field}) . '</td>';
373
        }
374
    }
375
 
376
    // Progress for each activity
377
    foreach($activities as $activity) {
378
 
379
        // Get progress information and state
380
        if (array_key_exists($activity->id, $user->progress)) {
381
            $thisprogress = $user->progress[$activity->id];
382
            $state = $thisprogress->completionstate;
383
            $overrideby = $thisprogress->overrideby;
384
            $date = userdate($thisprogress->timemodified);
385
        } else {
386
            $state = COMPLETION_INCOMPLETE;
387
            $overrideby = 0;
388
            $date = '';
389
        }
390
 
391
        // Work out how it corresponds to an icon
392
        switch($state) {
393
            case COMPLETION_INCOMPLETE :
394
                $completiontype = 'n'.($overrideby ? '-override' : '');
395
                break;
396
            case COMPLETION_COMPLETE :
397
                $completiontype = 'y'.($overrideby ? '-override' : '');
398
                break;
399
            case COMPLETION_COMPLETE_PASS :
400
                $completiontype = 'pass';
401
                break;
402
            case COMPLETION_COMPLETE_FAIL :
403
                $completiontype = 'fail';
404
                break;
405
        }
406
        $completiontrackingstring = $activity->completion == COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual';
407
        $completionicon = 'completion-' . $completiontrackingstring. '-' . $completiontype;
408
 
409
        if ($overrideby) {
410
            $overridebyuser = \core_user::get_user($overrideby, '*', MUST_EXIST);
411
            $describe = get_string('completion-' . $completiontype, 'completion', fullname($overridebyuser));
412
        } else {
413
            $describe = get_string('completion-' . $completiontype, 'completion');
414
        }
415
        $a=new StdClass;
416
        $a->state=$describe;
417
        $a->date=$date;
418
        $a->user = fullname($user, has_capability('moodle/site:viewfullnames', $context));
419
        $a->activity = $formattedactivities[$activity->id]->displayname;
420
        $fulldescribe=get_string('progress-title','completion',$a);
421
 
422
        if ($csv) {
423
            if ($date != '') {
424
                $date = userdate($thisprogress->timemodified, "%F %T");
425
            }
426
            print $sep.csv_quote($describe).$sep.csv_quote($date);
427
        } else {
428
            $celltext = $OUTPUT->pix_icon('i/' . $completionicon, s($fulldescribe));
429
            if (has_capability('moodle/course:overridecompletion', $context) &&
430
                    $state != COMPLETION_COMPLETE_PASS && $state != COMPLETION_COMPLETE_FAIL) {
431
                $newstate = ($state == COMPLETION_COMPLETE) ? COMPLETION_INCOMPLETE : COMPLETION_COMPLETE;
432
                $changecompl = $user->id . '-' . $activity->id . '-' . $newstate;
433
                $url = new moodle_url($PAGE->url, ['sesskey' => sesskey()]);
434
                $celltext = html_writer::link($url, $celltext, array('class' => 'changecompl', 'data-changecompl' => $changecompl,
435
                                                                     'data-activityname' => $a->activity,
436
                                                                     'data-userfullname' => $a->user,
437
                                                                     'data-completiontracking' => $completiontrackingstring,
438
                                                                     'role' => 'button'));
439
            }
440
            print '<td class="completion-progresscell '.$formattedactivities[$activity->id]->datepassedclass.'">'.
441
                $celltext . '</td>';
442
        }
443
    }
444
 
445
    if ($csv) {
446
        print $line;
447
    } else {
448
        print '</tr>';
449
    }
450
}
451
 
452
if ($csv) {
453
    exit;
454
}
455
print '</tbody></table>';
456
print '</div>';
457
 
458
echo $output->render_download_buttons($url);
459
 
460
echo $OUTPUT->footer();
461