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