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 |
* Utils class helper.
|
|
|
19 |
* @package block_dedication
|
|
|
20 |
* @copyright 2022 University of Canterbury
|
|
|
21 |
* @author Pramith Dayananda <pramithd@catalyst.net.nz>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace block_dedication\lib;
|
|
|
26 |
/**
|
|
|
27 |
* Utils helper class.
|
|
|
28 |
*/
|
|
|
29 |
class utils {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* List of supported logstore plugins.
|
|
|
33 |
*
|
|
|
34 |
* @var array
|
|
|
35 |
*/
|
|
|
36 |
public static $logstores = array('logstore_standard');
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Return formatted events from logstores.
|
|
|
40 |
* @param string $selectwhere
|
|
|
41 |
* @param array $params
|
|
|
42 |
* @return array
|
|
|
43 |
*/
|
|
|
44 |
public static function get_events_select($selectwhere, array $params) {
|
|
|
45 |
$return = array();
|
|
|
46 |
|
|
|
47 |
static $allreaders = null;
|
|
|
48 |
|
|
|
49 |
if (is_null($allreaders)) {
|
|
|
50 |
$allreaders = get_log_manager()->get_readers();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$processedreaders = 0;
|
|
|
54 |
|
|
|
55 |
foreach (self::$logstores as $name) {
|
|
|
56 |
if (isset($allreaders[$name])) {
|
|
|
57 |
$reader = $allreaders[$name];
|
|
|
58 |
$events = $reader->get_events_select($selectwhere, $params, 'timecreated ASC', 0, 0);
|
|
|
59 |
foreach ($events as $event) {
|
|
|
60 |
// Note: see \core\event\base to view base class of event.
|
|
|
61 |
$obj = new \stdClass();
|
|
|
62 |
$obj->time = $event->timecreated;
|
|
|
63 |
$obj->ip = $event->get_logextra()['ip'];
|
|
|
64 |
$return[] = $obj;
|
|
|
65 |
}
|
|
|
66 |
if (!empty($events)) {
|
|
|
67 |
$processedreaders++;
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// Sort mixed array by time ascending again only when more of a reader has added events to return array.
|
|
|
73 |
if ($processedreaders > 1) {
|
|
|
74 |
usort($return, function($a, $b) {
|
|
|
75 |
return $a->time > $b->time;
|
|
|
76 |
});
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return $return;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Formats time based in Moodle function format_time($totalsecs).
|
|
|
84 |
* @param int $totalsecs
|
|
|
85 |
* @return string
|
|
|
86 |
*/
|
|
|
87 |
public static function format_dedication($totalsecs) {
|
|
|
88 |
if (empty($totalsecs)) {
|
|
|
89 |
return get_string('none');
|
|
|
90 |
}
|
|
|
91 |
$totalsecs = abs($totalsecs);
|
|
|
92 |
|
|
|
93 |
$str = new \stdClass();
|
|
|
94 |
$str->hour = get_string('hour');
|
|
|
95 |
$str->hours = get_string('hours');
|
|
|
96 |
$str->min = get_string('min');
|
|
|
97 |
$str->mins = get_string('mins');
|
|
|
98 |
$str->sec = get_string('sec');
|
|
|
99 |
$str->secs = get_string('secs');
|
|
|
100 |
|
|
|
101 |
$hours = floor($totalsecs / HOURSECS);
|
|
|
102 |
$remainder = $totalsecs - ($hours * HOURSECS);
|
|
|
103 |
$mins = floor($remainder / MINSECS);
|
|
|
104 |
$secs = round($remainder - ($mins * MINSECS), 2);
|
|
|
105 |
|
|
|
106 |
$ss = ($secs == 1) ? $str->sec : $str->secs;
|
|
|
107 |
$sm = ($mins == 1) ? $str->min : $str->mins;
|
|
|
108 |
$sh = ($hours == 1) ? $str->hour : $str->hours;
|
|
|
109 |
|
|
|
110 |
$ohours = '';
|
|
|
111 |
$omins = '';
|
|
|
112 |
$osecs = '';
|
|
|
113 |
|
|
|
114 |
if ($hours) {
|
|
|
115 |
$ohours = $hours . ' ' . $sh;
|
|
|
116 |
}
|
|
|
117 |
if ($mins) {
|
|
|
118 |
$omins = $mins . ' ' . $sm;
|
|
|
119 |
}
|
|
|
120 |
if ($secs) {
|
|
|
121 |
$osecs = $secs . ' ' . $ss;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if ($hours) {
|
|
|
125 |
return trim($ohours . ' ' . $omins);
|
|
|
126 |
}
|
|
|
127 |
if ($mins) {
|
|
|
128 |
if ($mins < 15) { // If less than 15min, show seconds value as well as minutes.
|
|
|
129 |
return trim($omins . ' ' . $osecs);
|
|
|
130 |
} else { // If over 15min, just display a minute value.
|
|
|
131 |
return trim($omins);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
if ($secs) {
|
|
|
135 |
return $osecs;
|
|
|
136 |
}
|
|
|
137 |
return get_string('none');
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Return table styles based on current theme.
|
|
|
142 |
* @return array
|
|
|
143 |
*/
|
|
|
144 |
public static function get_table_styles() {
|
|
|
145 |
global $PAGE;
|
|
|
146 |
|
|
|
147 |
// Twitter Bootstrap styling.
|
|
|
148 |
$isbootstrap = ($PAGE->theme->name === 'boost') ||
|
|
|
149 |
count(array_intersect(array('boost', 'bootstrapbase'), $PAGE->theme->parents)) > 0;
|
|
|
150 |
if ($isbootstrap) {
|
|
|
151 |
$styles = array(
|
|
|
152 |
'table_class' => 'table table-bordered table-hover table-sm table-condensed table-dedication',
|
|
|
153 |
'header_style' => 'background-color: #333; color: #fff;'
|
|
|
154 |
);
|
|
|
155 |
} else {
|
|
|
156 |
$styles = array(
|
|
|
157 |
'table_class' => 'table-dedication',
|
|
|
158 |
'header_style' => ''
|
|
|
159 |
);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
return $styles;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Generates generic Excel file for download.
|
|
|
167 |
* @param string $downloadname
|
|
|
168 |
* @param array $rows
|
|
|
169 |
* @return MoodleExcelWorkbook
|
|
|
170 |
* @throws coding_exception
|
|
|
171 |
*/
|
|
|
172 |
public static function generate_download($downloadname, $rows) {
|
|
|
173 |
global $CFG;
|
|
|
174 |
|
|
|
175 |
require_once($CFG->libdir . '/excellib.class.php');
|
|
|
176 |
|
|
|
177 |
$workbook = new \MoodleExcelWorkbook(clean_filename($downloadname));
|
|
|
178 |
|
|
|
179 |
$myxls = $workbook->add_worksheet(get_string('pluginname', 'block_dedication'));
|
|
|
180 |
|
|
|
181 |
$rowcount = 0;
|
|
|
182 |
foreach ($rows as $row) {
|
|
|
183 |
foreach ($row as $index => $content) {
|
|
|
184 |
$myxls->write($rowcount, $index, $content);
|
|
|
185 |
}
|
|
|
186 |
$rowcount++;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
$workbook->close();
|
|
|
190 |
|
|
|
191 |
return $workbook;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Generate stats
|
|
|
196 |
*
|
|
|
197 |
* @param int $timestart
|
|
|
198 |
* @param int $timeend
|
|
|
199 |
* @return void
|
|
|
200 |
*/
|
|
|
201 |
public static function generate_stats($timestart, $timeend) {
|
|
|
202 |
if ($timeend - $timestart > WEEKSECS) {
|
|
|
203 |
// Break it down into bite sized weeks.
|
|
|
204 |
while ($timeend - $timestart > WEEKSECS) {
|
|
|
205 |
$timechunkend = $timestart + WEEKSECS;
|
|
|
206 |
self::generate_stats($timestart, $timechunkend);
|
|
|
207 |
$timestart = $timechunkend;
|
|
|
208 |
}
|
|
|
209 |
} else {
|
|
|
210 |
self::calculate($timestart, $timeend);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* Calculate stats
|
|
|
217 |
*
|
|
|
218 |
* @param int $timestart
|
|
|
219 |
* @param int $timeend
|
|
|
220 |
* @return void
|
|
|
221 |
*/
|
|
|
222 |
public static function calculate($timestart, $timeend) {
|
|
|
223 |
global $DB;
|
|
|
224 |
mtrace("calculating stats from: " . userdate($timestart) . " to:". userdate($timeend));
|
|
|
225 |
// TODO: accessing logs data uses the log store reader classes - we should look at converting this to do something similar.
|
|
|
226 |
// Get list of courses and users we want to calculate for.
|
|
|
227 |
$sql = "SELECT distinct ". $DB->sql_concat_join("':'", ['courseid', 'userid'])." as tmpid, courseid, userid
|
|
|
228 |
FROM {logstore_standard_log}
|
|
|
229 |
WHERE timecreated >= :timestart AND timecreated < :timeend AND userid > 0 AND courseid > 0";
|
|
|
230 |
$records = $DB->get_recordset_sql($sql, ['timestart' => $timestart, 'timeend' => $timeend]);
|
|
|
231 |
$courses = [];
|
|
|
232 |
foreach ($records as $record) {
|
|
|
233 |
if (!isset($courses[$record->courseid])) {
|
|
|
234 |
$courses[$record->courseid] = [];
|
|
|
235 |
}
|
|
|
236 |
$courses[$record->courseid][] = $record->userid;
|
|
|
237 |
}
|
|
|
238 |
$records->close();
|
|
|
239 |
|
|
|
240 |
$records = [];
|
|
|
241 |
foreach ($courses as $courseid => $users) {
|
|
|
242 |
$course = $DB->get_record('course', ['id' => $courseid]);
|
|
|
243 |
if (empty($course)) {
|
|
|
244 |
mtrace("Course $courseid not found, it may have been deleted.");
|
|
|
245 |
continue;
|
|
|
246 |
}
|
|
|
247 |
$logs = new manager($course, $timestart, $timeend);
|
|
|
248 |
foreach ($users as $user) {
|
|
|
249 |
$events = $logs->get_user_dedication($user);
|
|
|
250 |
foreach ($events as $event) {
|
|
|
251 |
$data = new \stdClass();
|
|
|
252 |
if ($event->dedicationtime == 0) {
|
|
|
253 |
continue;
|
|
|
254 |
} else {
|
|
|
255 |
$data->userid = $user;
|
|
|
256 |
$data->timespent = $event->dedicationtime;
|
|
|
257 |
$data->courseid = $course->id;
|
|
|
258 |
$data->timestart = $event->start_date;
|
|
|
259 |
}
|
|
|
260 |
$records[] = $data;
|
|
|
261 |
}
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
if (!empty($records)) {
|
|
|
265 |
$DB->insert_records('block_dedication', $records);
|
|
|
266 |
// Save the last time we saved some records if we haven't stored newer items yet.
|
|
|
267 |
// Basically prevents cli process for old stuff from saving data.
|
|
|
268 |
if (get_config('block_dedication', 'lastcalculated') < $timeend) {
|
|
|
269 |
set_config('lastcalculated', $timeend, 'block_dedication');
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
/**
|
|
|
275 |
* Helper function to get a users total timespent in course.
|
|
|
276 |
*
|
|
|
277 |
* @param int $courseid
|
|
|
278 |
* @param int $userid
|
|
|
279 |
* @param boolean $rawformat
|
|
|
280 |
* @return void
|
|
|
281 |
*/
|
|
|
282 |
public static function timespent($courseid, $userid, $rawformat=false) {
|
|
|
283 |
global $DB;
|
|
|
284 |
$totaldedication = $DB->get_field_sql("SELECT SUM(timespent)
|
|
|
285 |
FROM {block_dedication}
|
|
|
286 |
WHERE courseid = ? AND userid = ?",
|
|
|
287 |
['courseid' => $courseid, 'userid' => $userid]);
|
|
|
288 |
if ($rawformat) {
|
|
|
289 |
return $totaldedication;
|
|
|
290 |
} else {
|
|
|
291 |
return self::format_dedication($totaldedication);
|
|
|
292 |
}
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* Calculate averages and totals for timespent in course.
|
|
|
297 |
*
|
|
|
298 |
* @param int $courseid
|
|
|
299 |
* @param int $duration
|
|
|
300 |
* @param bool $filter
|
|
|
301 |
* @return array
|
|
|
302 |
*/
|
|
|
303 |
public static function get_average($courseid, $duration = null, bool $filter = false) {
|
|
|
304 |
global $DB, $CFG, $SESSION;
|
|
|
305 |
|
|
|
306 |
$params = ['courseid' => $courseid];
|
|
|
307 |
$sqlextra = '';
|
|
|
308 |
if (!empty($duration)) {
|
|
|
309 |
$sqlextra = " AND timestart > :since";
|
|
|
310 |
$params['since'] = time() - $duration;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
if (!empty($SESSION->local_ace_filtervalues) && $filter && file_exists($CFG->dirroot . '/local/ace/locallib.php')) {
|
|
|
314 |
require_once($CFG->dirroot . '/local/ace/locallib.php');
|
|
|
315 |
list($joinsql, $wheresql, $filterparams) = local_ace_generate_filter_sql($SESSION->local_ace_filtervalues);
|
|
|
316 |
|
|
|
317 |
$sqltotal = "SELECT SUM(bd.timespent)
|
|
|
318 |
FROM {block_dedication} bd
|
|
|
319 |
JOIN {user} u ON u.id = bd.userid
|
|
|
320 |
" . implode(" ", $joinsql) . "
|
|
|
321 |
WHERE bd.courseid = :courseid" . $sqlextra . "
|
|
|
322 |
" . implode(" ", $wheresql);
|
|
|
323 |
$sqlusers = "SELECT count(DISTINCT bd.userid)
|
|
|
324 |
FROM {block_dedication} bd
|
|
|
325 |
JOIN {user} u ON u.id = bd.userid
|
|
|
326 |
" . implode(" ", $joinsql) . "
|
|
|
327 |
WHERE bd.courseid = :courseid" . $sqlextra . "
|
|
|
328 |
" . implode(" ", $wheresql);
|
|
|
329 |
$params = array_merge($params, $filterparams);
|
|
|
330 |
$totaldedication = $DB->get_field_sql($sqltotal, $params);
|
|
|
331 |
$totalusers = $DB->get_field_sql($sqlusers, $params);
|
|
|
332 |
} else {
|
|
|
333 |
$sqltotal = "SELECT SUM(timespent)
|
|
|
334 |
FROM {block_dedication}
|
|
|
335 |
WHERE courseid = :courseid" . $sqlextra;
|
|
|
336 |
$sqlusers = "SELECT count(DISTINCT userid)
|
|
|
337 |
FROM {block_dedication}
|
|
|
338 |
WHERE courseid = :courseid" . $sqlextra;
|
|
|
339 |
$totaldedication = $DB->get_field_sql($sqltotal, $params);
|
|
|
340 |
$totalusers = $DB->get_field_sql($sqlusers, $params);
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
return ['total' => self::format_dedication($totaldedication),
|
|
|
344 |
'average' => self::format_dedication(!empty($totalusers) ? $totaldedication / $totalusers : 0)];
|
|
|
345 |
}
|
|
|
346 |
}
|