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