-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoursereport.php
226 lines (188 loc) · 8.62 KB
/
coursereport.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
<?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/>.
/**
* Course trainingsessions report
*
* @package report_trainingsessions
* @category report
* @version moodle 2.x
* @author Valery Fremaux ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
ob_start();
require_once($CFG->dirroot.'/blocks/use_stats/locallib.php');
require_once($CFG->dirroot.'/report/trainingsessions/locallib.php');
require_once($CFG->dirroot.'/report/trainingsessions/selector_form.php');
require_once($CFG->dirroot.'/report/trainingsessions/renderers/htmlrenderers.php');
$id = required_param('id', PARAM_INT); // The course id.
$rt = \report\trainingsessions\trainingsessions::instance();
$renderer = new \report\trainingsessions\HtmlRenderer($rt);
// Calculate start time.
$selform = new SelectorForm($id, 'course');
if (!$data = $selform->get_data()) {
$data = new StdClass;
$data->from = optional_param('from', -1, PARAM_NUMBER);
$data->to = optional_param('to', -1, PARAM_NUMBER);
$data->userid = optional_param('userid', $USER->id, PARAM_INT);
$data->fromstart = optional_param('fromstart', 0, PARAM_BOOL);
$data->tonow = optional_param('tonow', 0, PARAM_BOOL);
$data->output = optional_param('output', 'html', PARAM_ALPHA);
$data->groupid = optional_param('group', '0', PARAM_ALPHA);
}
$context = context_course::instance($id);
$config = get_config('report_trainingsessions');
// Calculate start time.
$rt->process_bounds($data, $course);
$iscourseset = !is_null($rt->get_courseset($course->id));
if ($data->output == 'html') {
echo $OUTPUT->header();
echo $OUTPUT->container_start();
echo $rtrenderer->tabs($course, $view, $data->from, $data->to, $iscourseset);
echo $OUTPUT->container_end();
echo $OUTPUT->box_start('block');
$selform->set_data($data);
$selform->display();
echo $OUTPUT->box_end();
echo get_string('from', 'report_trainingsessions')." : ".userdate($data->from);
echo ' '.get_string('to', 'report_trainingsessions')." : ".userdate($data->to);
}
// Compute target group.
$allgroupsaccess = has_capability('moodle/site:accessallgroups', $context);
if (!$allgroupsaccess) {
$mygroups = groups_get_my_groups();
$allowedgroupids = array();
if ($mygroups) {
foreach ($mygroups as $g) {
$allowedgroupids[] = $g->id;
}
if (empty($data->groupid) || !in_array($data->groupid, $allowedgroupids)) {
$data->groupid = $allowedgroupids[0];
}
} else {
echo $OUTPUT->notification(get_string('errornotingroups', 'report_trainingsessions'));
echo $OUTPUT->footer($course);
die;
}
} else {
if ($allowedgroups = groups_get_all_groups($COURSE->id, $USER->id, 0, 'g.id,g.name')) {
$allowedgroupids = array_keys($allowedgroups);
}
}
if ($data->groupid) {
$targetusers = get_enrolled_users($context, '', $data->groupid, 'u.*', 'u.lastname,u.firstname', 0, 0, $config->disablesuspendedenrolments);
} else {
$targetusers = get_enrolled_users($context, '', 0, 'u.*', 'u.lastname,u.firstname', 0, 0, $config->disablesuspendedenrolments);
if (count($targetusers) > 100) {
if (!empty($allowedgroupids)) {
$OUTPUT->notification(get_string('errorcoursetoolarge', 'report_trainingsessions'));
$data->groupid = $allowedgroupids[0];
// Refetch again after eventual group correction.
$targetusers = get_enrolled_users($context, '', $data->groupid, 'u.*', 'u.lastname,u.firstname', 0, 0, $config->disablesuspendedenrolments);
} else {
// DO NOT COMPILE.
echo $OUTPUT->notification(get_string('coursetoolargenotice', 'report_trainingsessions'));
echo $OUTPUT->footer($course);
die;
}
}
}
// Filter out non compiling users.
$rt->filter_unwanted_users($targetusers, $course);
// Get course structure.
$coursestructure = $rt->get_course_structure($course->id, $items);
// Print result.
if ($config->disablesuspendedenrolments && has_capability('report/trainingsessions:viewother', $context)) {
echo $OUTPUT->notification(get_string('hasdisabledenrolmentsrestriction', 'report_trainingsessions'));
}
echo '<link rel="stylesheet" href="reports.css" type="text/css" />';
$cols = $rt->get_summary_cols('keys');
$rt->add_graded_columns($cols, $unusedtitles);
if (!empty($targetusers)) {
foreach ($targetusers as $auser) {
use_stats_fix_last_course_access($auser->id, $course->id);
$logusers = $auser->id;
$logs = use_stats_extract_logs($data->from, $data->to, $auser->id, $course);
$aggregate = use_stats_aggregate_logs($logs, $data->from, $data->to);
if (empty($aggregate['sessions'])) {
$aggregate['sessions'] = array();
}
$headdata = (object) $rt->map_summary_cols($cols, $auser, $aggregate, $weekaggregate, $course->id, true);
$headdata->gradecols = [];
$rt->add_graded_data($headdata->gradecols, $auser->id, $aggregate);
$headdata->done = 0;
$headdata->items = $items;
if (!empty($aggregate)) {
// Calculate everything.
$sesscount = $rt->count_sessions_in_course($aggregate['sessions'], $course->id);
$headdata->sessions = (!empty($aggregate['sessions'])) ? $sesscount : 0;
foreach (array_keys($aggregate) as $module) {
/*
* Exclude from calculation some pseudo-modules that are not part of
* a course structure.
*/
if (preg_match('/course|user|upload|sessions|system|activities|other/', $module)) {
continue;
}
$headdata->done += count($aggregate[$module]);
}
} else {
$headdata->sessions = 0;
}
if ($headdata->done > $items) {
$headdata->done = $items;
}
$headdata->linktousersheet = 1;
$headdata->from = $data->from;
$headdata->to = $data->to;
echo $renderer->print_header_html($auser, $course, $headdata, $cols, true /* short */);
}
} else {
echo $OUTPUT->notification(get_string('nothing', 'report_trainingsessions'));
}
$options['id'] = $course->id;
$options['groupid'] = $data->groupid;
$options['from'] = $data->from; // Alternate way.
$options['to'] = $data->to; // Alternate way.
$options['output'] = 'xls'; // Ask for XLS.
$options['asxls'] = 'xls'; // Force XLS for index.php.
$options['view'] = 'course'; // Force course view.
$template = new StdClass;
$params = array('id' => $course->id,
'from' => $data->from,
'to' => $data->to,
'timesession' => time(),
'groupid' => $data->groupid);
$csvurl = new moodle_url('/report/trainingsessions/tasks/groupcsvreportonerow_batch_task.php', $params);
$template->generatecsvbutton = $OUTPUT->single_button($csvurl, get_string('generatecsv', 'report_trainingsessions'), 'get');
$params = array('id' => $course->id,
'view' => 'course',
'groupid' => $data->groupid,
'from' => $data->from,
'to' => $data->to,
'output' => 'xls');
$url = new moodle_url('/report/trainingsessions/tasks/groupxlsreportperuser_batch_task.php', $params);
$template->generatexlsbutton = $OUTPUT->single_button($url, get_string('generatexls', 'report_trainingsessions'), 'get');
if (report_trainingsessions_supports_feature('format/pdf')) {
$params = array('id' => $course->id,
'view' => 'course',
'groupid' => $data->groupid,
'from' => $data->from,
'to' => $data->to);
$url = new moodle_url('/report/trainingsessions/pro/tasks/grouppdfreportperuser_batch_task.php', $params);
$template->generatepdfbutton = $OUTPUT->single_button($url, get_string('generatepdf', 'report_trainingsessions'), 'get');
}
echo $OUTPUT->render_from_template('report_trainingsessions/coursereportbuttons', $template);