ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLSPostConditionDB.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
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
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}
Storage for ilLSPostConditions.
insert(array $ls_post_conditions, ilDBInterface $db)
__construct(ilDBInterface $db)
upsert(array $ls_post_conditions)
A PostCondition does restrict the progression of a user through the learning sequence.
Interface ilDBInterface.
insert(string $table_name, array $values)
manipulate(string $query)
Run a (write) Query on the database.
$ref_id
Definition: ltiauth.php:66