ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilSCTasks.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
24 class ilSCTasks
25 {
26  private static array $instances = array();
27 
28  private int $grp_id = 0;
29  private array $tasks = array();
30 
31  protected ilDBInterface $db;
32 
33  private function __construct(int $a_grp_id)
34  {
35  global $DIC;
36 
37  $this->db = $DIC->database();
38  $this->grp_id = $a_grp_id;
39  $this->read();
40  }
41 
42  public static function getInstanceByGroupId(int $a_group_id): ilSCTasks
43  {
44  if (!array_key_exists($a_group_id, self::$instances)) {
45  return self::$instances[$a_group_id] = new self($a_group_id);
46  }
47  return self::$instances[$a_group_id];
48  }
49 
53  public static function lookupIdentifierForTask(int $a_task_id): string
54  {
55  global $DIC;
56 
57  $db = $DIC->database();
58  $query = 'select identifier from sysc_tasks ' .
59  'where id = ' . $db->quote($a_task_id, ilDBConstants::T_INTEGER);
60  $res = $db->query($query);
61  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
62  return (string) $row->identifier;
63  }
64  return '';
65  }
66 
67  public function updateFromComponentDefinition(string $a_identifier): int
68  {
69  foreach ($this->getTasks() as $task) {
70  if ($task->getIdentifier() === $a_identifier) {
71  return 1;
72  }
73  }
74 
75  $task = new ilSCTask();
76  $task->setGroupId($this->getGroupId());
77  $task->setIdentifier($a_identifier);
78  $task->create();
79 
80  return $task->getId();
81  }
82 
83  public static function lookupGroupId(int $a_task_id): int
84  {
85  global $DIC;
86 
87  $ilDB = $DIC->database();
88 
89  $query = 'SELECT grp_id FROM sysc_tasks ' .
90  'WHERE id = ' . $ilDB->quote($a_task_id, ilDBConstants::T_INTEGER);
91  $res = $ilDB->query($query);
92  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
93  return (int) $row->grp_id;
94  }
95  return 0;
96  }
97 
98  public static function lookupCompleted(int $a_grp_id): int
99  {
100  $tasks = self::getInstanceByGroupId($a_grp_id);
101 
102  $num_completed = 0;
103  foreach ($tasks->getTasks() as $task) {
104  if (!$task->isActive()) {
105  continue;
106  }
107  if ($task->getStatus() === ilSCTask::STATUS_COMPLETED) {
108  $num_completed++;
109  }
110  }
111  return $num_completed;
112  }
113 
114  public static function lookupFailed(int $a_grp_id): int
115  {
116  $tasks = self::getInstanceByGroupId($a_grp_id);
117 
118  $num_failed = 0;
119  foreach ($tasks->getTasks() as $task) {
120  if (!$task->isActive()) {
121  continue;
122  }
123 
124  if ($task->getStatus() === ilSCTask::STATUS_FAILED) {
125  $num_failed++;
126  }
127  }
128  return $num_failed;
129  }
130 
131  public static function lookupLastUpdate(int $a_grp_id): ilDateTime
132  {
133  global $DIC;
134 
135  $ilDB = $DIC->database();
136 
137  $query = 'SELECT MAX(last_update) last_update FROM sysc_tasks ' .
138  'WHERE status = ' . $ilDB->quote(ilSCTask::STATUS_FAILED, ilDBConstants::T_INTEGER) . ' ' .
139  'AND grp_id = ' . $ilDB->quote($a_grp_id, ilDBConstants::T_INTEGER);
140  $res = $ilDB->query($query);
141 
142  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
143  return new ilDateTime($row->last_update, IL_CAL_DATETIME, ilTimeZone::UTC);
144  }
145  return new ilDateTime(time(), IL_CAL_UNIX);
146  }
147 
148  public function getGroupId(): int
149  {
150  return $this->grp_id;
151  }
152 
156  public function getTasks(): array
157  {
158  return $this->tasks;
159  }
160 
161  protected function read(): void
162  {
163  $query = 'SELECT id, grp_id FROM sysc_tasks ' .
164  'ORDER BY id ';
165  $res = $this->db->query($query);
166 
167  $this->tasks = array();
168  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
169  $this->tasks[] = ilSCComponentTaskFactory::getTask((int) $row->grp_id, (int) $row->id);
170  }
171  }
172 }
Defines a system check task.
$res
Definition: ltiservices.php:66
static getInstanceByGroupId(int $a_group_id)
const IL_CAL_DATETIME
static getTask(int $a_group_id, int $a_task_id)
Description of class.
const STATUS_FAILED
static lookupLastUpdate(int $a_grp_id)
static lookupFailed(int $a_grp_id)
ilDBInterface $db
static lookupIdentifierForTask(int $a_task_id)
quote($value, string $type)
const IL_CAL_UNIX
updateFromComponentDefinition(string $a_identifier)
static lookupGroupId(int $a_task_id)
__construct(int $a_grp_id)
static array $instances
global $DIC
Definition: shib_login.php:22
query(string $query)
Run a (read-only) Query on the database.
static lookupCompleted(int $a_grp_id)
const STATUS_COMPLETED