ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilLSPostConditionDB.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
25 {
26  public const TABLE_NAME = 'post_conditions';
27  public const STD_ALWAYS_OPERATOR = 'always';
28 
29  protected ilDBInterface $db;
30 
31  public function __construct(ilDBInterface $db)
32  {
33  $this->db = $db;
34  }
35 
39  public function select(array $ref_ids): array
40  {
41  if ($ref_ids === []) {
42  return [];
43  }
44 
45  $data = [];
46  $query =
47  "SELECT ref_id, condition_operator, value" . PHP_EOL
48  . "FROM " . static::TABLE_NAME . PHP_EOL
49  . "WHERE ref_id IN (" . implode(',', $ref_ids) . ")" . PHP_EOL
50  ;
51 
52  $result = $this->db->query($query);
53 
54  while ($row = $this->db->fetchAssoc($result)) {
55  $data[$row['ref_id']] = [$row['condition_operator'], $row['value']];
56  }
57 
58  $conditions = [];
59  foreach ($ref_ids as $ref_id) {
60  //always-condition, standard
61  $op = self::STD_ALWAYS_OPERATOR;
62  $value = null;
63 
64  //if from db: proper values
65  if (array_key_exists($ref_id, $data)) {
66  list($op, $value) = $data[$ref_id];
67  }
68  $conditions[] = new \ilLSPostCondition($ref_id, $op, $value);
69  }
70  return $conditions;
71  }
72 
73  public function delete(array $ref_ids, ilDBInterface $db = null): void
74  {
75  if ($ref_ids === []) {
76  return;
77  }
78 
79  if (is_null($db)) {
80  $db = $this->db;
81  }
82 
83  $query =
84  "DELETE FROM " . static::TABLE_NAME . PHP_EOL
85  . "WHERE ref_id IN (" . implode(',', $ref_ids) . ")" . PHP_EOL
86  ;
87 
88  $db->manipulate($query);
89  }
90 
91  protected function insert(array $ls_post_conditions, ilDBInterface $db): void
92  {
93  foreach ($ls_post_conditions as $condition) {
94  $values = [
95  "ref_id" => ["integer", $condition->getRefId()],
96  "condition_operator" => ["text", $condition->getConditionOperator()],
97  "value" => ["text", $condition->getValue()]
98  ];
99  $db->insert(static::TABLE_NAME, $values);
100  }
101  }
102 
106  public function upsert(array $ls_post_conditions): void
107  {
108  if ($ls_post_conditions === []) {
109  return;
110  }
111 
112  $ref_ids = array_map(
113  fn (ilLSPostCondition $condition) => $condition->getRefId(),
114  $ls_post_conditions
115  );
116 
117  $ilAtomQuery = $this->db->buildAtomQuery();
118  $ilAtomQuery->addTableLock(static::TABLE_NAME);
119  $ilAtomQuery->addQueryCallable(
120  function (ilDBInterface $db) use ($ref_ids, $ls_post_conditions): void {
121  $this->delete($ref_ids, $db);
122  $this->insert($ls_post_conditions, $db);
123  }
124  );
125  $ilAtomQuery->run();
126  }
127 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
insert(array $ls_post_conditions, ilDBInterface $db)
insert(string $table_name, array $values)
upsert(array $ls_post_conditions)
__construct(ilDBInterface $db)
$ref_id
Definition: ltiauth.php:67
$query
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...