ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLPCronObjectStatistics.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=0);
4
25
32{
33 protected int $date = 0;
34
35 protected ilLanguage $lng;
36 protected ilDBInterface $db;
37 protected ilTree $tree;
38 protected ilLogger $logger;
40
41 public function __construct()
42 {
43 global $DIC;
44
45 $this->logger = $DIC->logger()->trac();
46 $this->lng = $DIC->language();
47 $this->lng->loadLanguageModule("trac");
48 $this->db = $DIC->database();
49 $this->tree = $DIC->repositoryTree();
50 $this->cron_manager = $DIC->cron()->manager();
51 }
52
53 public function getId(): string
54 {
55 return "lp_object_statistics";
56 }
57
58 public function getTitle(): string
59 {
60 return $this->lng->txt("trac_object_statistics");
61 }
62
63 public function getDescription(): string
64 {
65 return $this->lng->txt("trac_object_statistics_info");
66 }
67
69 {
70 return JobScheduleType::DAILY;
71 }
72
73 public function getDefaultScheduleValue(): ?int
74 {
75 return null;
76 }
77
78 public function hasAutoActivation(): bool
79 {
80 return true;
81 }
82
83 public function hasFlexibleSchedule(): bool
84 {
85 return false;
86 }
87
88 public function run(): JobResult
89 {
90 // all date related operations are based on this timestamp
91 // should be midnight of yesterday (see gatherUserData()) to always have full day
92 $this->date = strtotime("yesterday");
93
94 $status = JobResult::STATUS_NO_ACTION;
95 $message = array();
96
97 $count = 0;
98 $count += $this->gatherCourseLPData();
99 $count += $this->gatherTypesData();
100 $count += $this->gatherUserData();
101
102 if ($count) {
103 $status = JobResult::STATUS_OK;
104 }
105
106 $result = new JobResult();
107 $result->setStatus($status);
108
109 return $result;
110 }
111
115 protected function gatherCourseLPData(): int
116 {
117 $count = 0;
118
119 // process all courses
120 $all_courses = array_keys(ilObject::_getObjectsByType("crs"));
121 if ($all_courses) {
122 // gather objects in trash
123 $trashed_objects = $this->tree->getSavedNodeObjIds($all_courses);
124
125 foreach ($all_courses as $crs_id) {
126 // trashed objects will not change
127 if (!in_array($crs_id, $trashed_objects)) {
128 $refs = ilObject::_getAllReferences($crs_id);
129 if (!count($refs)) {
130 $this->logger->warning(
131 'Found course without reference: obj_id = ' . $crs_id
132 );
133 continue;
134 }
135
136 // only if LP is active
137 $olp = ilObjectLP::getInstance($crs_id);
138 if (!$olp->isActive()) {
139 continue;
140 }
141
142 // only save once per day
143 $this->db->manipulate(
144 "DELETE FROM obj_lp_stat WHERE" .
145 " obj_id = " . $this->db->quote($crs_id, "integer") .
146 " AND fulldate = " . $this->db->quote(
147 date("Ymd", $this->date),
148 "integer"
149 )
150 );
151
152 $members = new ilCourseParticipants($crs_id);
153 $members = $members->getMembers();
154
155 $in_progress = count(
157 $crs_id,
158 $members
159 )
160 );
161 $completed = count(
163 $crs_id,
164 $members
165 )
166 );
167 $failed = count(
169 $crs_id,
170 $members
171 )
172 );
173
174 // calculate with other values - there is not direct method
175 $not_attempted = count(
176 $members
177 ) - $in_progress - $completed - $failed;
178
179 $set = array(
180 "type" => array("text", "crs"),
181 "obj_id" => array("integer", $crs_id),
182 "yyyy" => array("integer", date("Y", $this->date)),
183 "mm" => array("integer", date("m", $this->date)),
184 "dd" => array("integer", date("d", $this->date)),
185 "fulldate" => array("integer",
186 date("Ymd", $this->date)
187 ),
188 "mem_cnt" => array("integer", count($members)),
189 "in_progress" => array("integer", $in_progress),
190 "completed" => array("integer", $completed),
191 "failed" => array("integer", $failed),
192 "not_attempted" => array("integer", $not_attempted)
193 );
194
195 $this->db->insert("obj_lp_stat", $set);
196 $count++;
197 $this->cron_manager->ping($this->getId());
198 }
199 }
200 }
201 return $count;
202 }
203
204 protected function gatherTypesData(): int
205 {
206 $count = 0;
208 foreach ($data as $type => $item) {
209 // only save once per day
210 $this->db->manipulate(
211 "DELETE FROM obj_type_stat WHERE" .
212 " type = " . $this->db->quote($type, "text") .
213 " AND fulldate = " . $this->db->quote(
214 date("Ymd", $this->date),
215 "integer"
216 )
217 );
218
219 $set = array(
220 "type" => array("text", $type),
221 "yyyy" => array("integer", date("Y", $this->date)),
222 "mm" => array("integer", date("m", $this->date)),
223 "dd" => array("integer", date("d", $this->date)),
224 "fulldate" => array("integer", date("Ymd", $this->date)),
225 "cnt_references" => array("integer", (int) $item["references"]),
226 "cnt_objects" => array("integer", (int) $item["objects"]),
227 "cnt_deleted" => array("integer", isset($item["deleted"]) ? (int) $item["deleted"] : 0)
228 );
229
230 $this->db->insert("obj_type_stat", $set);
231
232 $count++;
233 $this->cron_manager->ping($this->getId());
234 }
235 return $count;
236 }
237
238 protected function gatherUserData(): int
239 {
240 $count = 0;
241 $to = mktime(
242 23,
243 59,
244 59,
245 date("m", $this->date),
246 date("d", $this->date),
247 date("Y", $this->date)
248 );
249
250 $sql = "SELECT COUNT(DISTINCT(usr_id)) counter,obj_id FROM read_event" .
251 " WHERE last_access >= " . $this->db->quote(
252 $this->date,
253 "integer"
254 ) .
255 " AND last_access <= " . $this->db->quote($to, "integer") .
256 " GROUP BY obj_id";
257 $set = $this->db->query($sql);
258 while ($row = $this->db->fetchAssoc($set)) {
259 // only save once per day
260 $this->db->manipulate(
261 "DELETE FROM obj_user_stat" .
262 " WHERE fulldate = " . $this->db->quote(
263 date("Ymd", $this->date),
264 "integer"
265 ) .
266 " AND obj_id = " . $this->db->quote($row["obj_id"], "integer")
267 );
268
269 $iset = array(
270 "obj_id" => array("integer", $row["obj_id"]),
271 "yyyy" => array("integer", date("Y", $this->date)),
272 "mm" => array("integer", date("m", $this->date)),
273 "dd" => array("integer", date("d", $this->date)),
274 "fulldate" => array("integer", date("Ymd", $this->date)),
275 "counter" => array("integer", $row["counter"])
276 );
277
278 $this->db->insert("obj_user_stat", $iset);
279
280 $count++;
281 $this->cron_manager->ping($this->getId());
282 }
283 return $count;
284 }
285}
Cron for lp object statistics.
hasAutoActivation()
Is to be activated on "installation", does only work for ILIAS core cron jobs.
static _lookupCompletedForObject(int $a_obj_id, ?array $a_user_ids=null)
static _lookupFailedForObject(int $a_obj_id, ?array $a_user_ids=null)
static _lookupInProgressForObject(int $a_obj_id, ?array $a_user_ids=null)
language handling
Component logger with individual log levels by component id.
static getInstance(int $obj_id)
static _getAllReferences(int $id)
get all reference ids for object ID
static _getObjectsByType(string $obj_type="", ?int $owner=null)
static getObjectTypeStatistics()
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26
$message
Definition: xapiexit.php:31