ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 
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, \ilDBInterface $db = null)
62  {
63  if (count($ref_ids) === 0) {
64  return;
65  }
66 
67  if (is_null($db)) {
68  $db = $this->db;
69  }
70 
71  $query = "DELETE FROM " . static::TABLE_NAME . PHP_EOL
72  . "WHERE ref_id IN ("
73  . implode(',', $ref_ids)
74  . ")";
75  $db->manipulate($query);
76  }
77 
78  protected function insert(array $ls_post_conditions, \ilDBInterface $db)
79  {
80  foreach ($ls_post_conditions as $condition) {
81  $values = array(
82  "ref_id" => array("integer", $condition->getRefId()),
83  "condition_operator" => array("text", $condition->getConditionOperator())
84  );
85  $db->insert(static::TABLE_NAME, $values);
86  }
87  }
88 
92  public function upsert(array $ls_post_conditions)
93  {
94  if (count($ls_post_conditions) === 0) {
95  return;
96  }
97 
98  $ref_ids = array_map(
99  function ($condition) {
100  return (int) $condition->getRefId();
101  },
102  $ls_post_conditions
103  );
104 
105  $ilAtomQuery = $this->db->buildAtomQuery();
106  $ilAtomQuery->addTableLock(static::TABLE_NAME);
107  $ilAtomQuery->addQueryCallable(
108  function (\ilDBInterface $db) use ($ref_ids, $ls_post_conditions) {
109  $this->delete($ref_ids, $db);
110  $this->insert($ls_post_conditions, $db);
111  }
112  );
113  $ilAtomQuery->run();
114  }
115 }
Storage for ilLSPostConditions.
$data
Definition: storeScorm.php:23
$result
upsert(array $ls_post_conditions)
__construct(\ilDBInterface $db)
Interface ilDBInterface.
$query
insert(array $ls_post_conditions, \ilDBInterface $db)
insert($table_name, $values)