ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLSPostConditionDB.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
11 {
12  const TABLE_NAME = 'post_conditions';
13  const STD_ALWAYS_OPERATOR = 'always';
14 
18  protected $db;
19 
20  public function __construct(\ilDBInterface $db)
21  {
22  $this->db = $db;
23  }
24 
28  public function select(array $ref_ids) : array
29  {
30  if (count($ref_ids) === 0) {
31  return [];
32  }
33 
34  $data = [];
35  $query = "SELECT ref_id, condition_operator, value" . PHP_EOL
36  . "FROM " . static::TABLE_NAME . PHP_EOL
37  . "WHERE ref_id IN ("
38  . implode(',', $ref_ids)
39  . ")";
40 
41  $result = $this->db->query($query);
42  while ($row = $this->db->fetchAssoc($result)) {
43  $data[$row['ref_id']] = [$row['condition_operator'], (int) $row['value']];
44  }
45 
46  $conditions = [];
47  foreach ($ref_ids as $ref_id) {
48  //always-condition, standard
49  $op = self::STD_ALWAYS_OPERATOR;
50  $value = null;
51 
52  //if from db: proper values
53  if (array_key_exists($ref_id, $data)) {
54  list($op, $value) = $data[$ref_id];
55  }
56  $conditions[] = new \ilLSPostCondition($ref_id, $op, $value);
57  }
58  return $conditions;
59  }
60 
61  public function delete(array $ref_ids)
62  {
63  if (count($ref_ids) === 0) {
64  return;
65  }
66 
67  $query = "DELETE FROM " . static::TABLE_NAME . PHP_EOL
68  . "WHERE ref_id IN ("
69  . implode(',', $ref_ids)
70  . ")";
71  $this->db->manipulate($query);
72  }
73 
74  protected function insert(array $ls_post_conditions)
75  {
76  foreach ($ls_post_conditions as $condition) {
77  $values = array(
78  "ref_id" => array("integer", $condition->getRefId()),
79  "condition_operator" => array("text", $condition->getConditionOperator())
80  );
81  $this->db->insert(static::TABLE_NAME, $values);
82  }
83  }
84 
88  public function upsert(array $ls_post_conditions)
89  {
90  if (count($ls_post_conditions) === 0) {
91  return;
92  }
93 
94  $ref_ids = array_map(
95  function ($condition) {
96  return (int) $condition->getRefId();
97  },
98  $ls_post_conditions
99  );
100 
101  $ilAtomQuery = $this->db->buildAtomQuery();
102  $ilAtomQuery->addTableLock(static::TABLE_NAME);
103  $ilAtomQuery->addQueryCallable(
104  function (\ilDBInterface $db) use ($ref_ids, $ls_post_conditions) {
105  $this->delete($ref_ids);
106  $this->insert($ls_post_conditions);
107  }
108  );
109  $ilAtomQuery->run();
110  }
111 }
Storage for ilLSPostConditions.
$result
upsert(array $ls_post_conditions)
__construct(\ilDBInterface $db)
PHP_EOL
Definition: complexTest.php:5
insert(array $ls_post_conditions)
$values
$query
$row
$data
Definition: bench.php:6