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 |
* Manager helper class.
|
|
|
19 |
*
|
|
|
20 |
* @package block_dedication
|
|
|
21 |
* @copyright 2022 University of Canterbury
|
|
|
22 |
* @author Pramith Dayananda <pramithd@catalyst.net.nz>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
namespace block_dedication\lib;
|
|
|
26 |
|
|
|
27 |
use block_dedication\lib\utils;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Generate dedication reports based in passed params.
|
|
|
31 |
*/
|
|
|
32 |
class manager {
|
|
|
33 |
|
|
|
34 |
/** @var stdclass Course */
|
|
|
35 |
protected $course;
|
|
|
36 |
/** @var int $mintime - unix timestamp. */
|
|
|
37 |
protected $mintime;
|
|
|
38 |
/** @var int $maxtime - unix timestamp. */
|
|
|
39 |
protected $maxtime;
|
|
|
40 |
/** @var int $limit - duration in seconds. */
|
|
|
41 |
protected $limit;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Construtor.
|
|
|
45 |
*
|
|
|
46 |
* @param stdclass $course
|
|
|
47 |
* @param int $mintime
|
|
|
48 |
* @param int $maxtime
|
|
|
49 |
*/
|
|
|
50 |
public function __construct($course, $mintime = null, $maxtime = null) {
|
|
|
51 |
$this->course = $course;
|
|
|
52 |
|
|
|
53 |
$startdate = empty($course->startdate) ? time() - (90 * DAYSECS) : $course->startdate;
|
|
|
54 |
$this->mintime = !empty($mintime) ? $mintime : $startdate;
|
|
|
55 |
$this->maxtime = empty($maxtime) ? time() : $maxtime;
|
|
|
56 |
$limit = get_config('block_dedication', 'session_limit');
|
|
|
57 |
$this->limit = empty($limit) ? HOURSECS : $limit;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Get dedication for a specific user.
|
|
|
62 |
*
|
|
|
63 |
* @param stdclass|int $user
|
|
|
64 |
* @param boolean $simple
|
|
|
65 |
* @return void
|
|
|
66 |
*/
|
|
|
67 |
public function get_user_dedication($user, $simple = false) {
|
|
|
68 |
$config = get_config('block_dedication');
|
|
|
69 |
if (is_numeric($user)) {
|
|
|
70 |
$userid = $user;
|
|
|
71 |
} else {
|
|
|
72 |
$userid = $user->id;
|
|
|
73 |
}
|
|
|
74 |
$where = 'courseid = :courseid AND userid = :userid AND timecreated >= :mintime AND timecreated <= :maxtime ' .
|
|
|
75 |
'AND origin != :origin';
|
|
|
76 |
$params = array(
|
|
|
77 |
'courseid' => $this->course->id,
|
|
|
78 |
'userid' => $userid,
|
|
|
79 |
'mintime' => $this->mintime,
|
|
|
80 |
'maxtime' => $this->maxtime,
|
|
|
81 |
'origin' => 'cli',
|
|
|
82 |
);
|
|
|
83 |
$logs = utils::get_events_select($where, $params);
|
|
|
84 |
|
|
|
85 |
if ($simple) {
|
|
|
86 |
// Return total dedication time in seconds.
|
|
|
87 |
$total = 0;
|
|
|
88 |
|
|
|
89 |
if ($logs) {
|
|
|
90 |
$previouslog = array_shift($logs);
|
|
|
91 |
$previouslogtime = $previouslog->time;
|
|
|
92 |
$sessionstart = $previouslogtime;
|
|
|
93 |
|
|
|
94 |
foreach ($logs as $log) {
|
|
|
95 |
if (($log->time - $previouslogtime) > $this->limit) {
|
|
|
96 |
$dedication = $previouslogtime - $sessionstart;
|
|
|
97 |
$total += $dedication;
|
|
|
98 |
$sessionstart = $log->time;
|
|
|
99 |
}
|
|
|
100 |
$previouslogtime = $log->time;
|
|
|
101 |
}
|
|
|
102 |
$dedication = $previouslogtime - $sessionstart;
|
|
|
103 |
$total += $dedication;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return $total;
|
|
|
107 |
|
|
|
108 |
} else {
|
|
|
109 |
// Return user sessions with details.
|
|
|
110 |
$rows = array();
|
|
|
111 |
|
|
|
112 |
if ($logs) {
|
|
|
113 |
$previouslog = array_shift($logs);
|
|
|
114 |
$previouslogtime = $previouslog->time;
|
|
|
115 |
$sessionstart = $previouslogtime;
|
|
|
116 |
$ips = array($previouslog->ip => true);
|
|
|
117 |
|
|
|
118 |
foreach ($logs as $log) {
|
|
|
119 |
if (($log->time - $previouslogtime) > $this->limit) {
|
|
|
120 |
$dedication = $previouslogtime - $sessionstart;
|
|
|
121 |
|
|
|
122 |
// Ignore sessions with a really short duration.
|
|
|
123 |
if ($dedication > $config->ignore_sessions_limit) {
|
|
|
124 |
$rows[] = (object) array('start_date' => $sessionstart, 'dedicationtime' => $dedication);
|
|
|
125 |
$ips = array();
|
|
|
126 |
}
|
|
|
127 |
$sessionstart = $log->time;
|
|
|
128 |
}
|
|
|
129 |
$previouslogtime = $log->time;
|
|
|
130 |
$ips[$log->ip] = true;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
$dedication = $previouslogtime - $sessionstart;
|
|
|
134 |
|
|
|
135 |
// Ignore sessions with a really short duration.
|
|
|
136 |
if ($dedication > $config->ignore_sessions_limit) {
|
|
|
137 |
$rows[] = (object) array('start_date' => $sessionstart, 'dedicationtime' => $dedication);
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
return $rows;
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
}
|