ILIAS  release_7 Revision v7.30-3-g800a261c036
ilLSStateDB Class Reference

Persistence for View-States. More...

+ Collaboration diagram for ilLSStateDB:

Public Member Functions

 __construct (ilDBInterface $db)
 
 getStatesFor (int $lso_ref_id, array $usr_ids=[])
 
 getCurrentItemsFor (int $lso_ref_id, array $usr_ids=[])
 
 getFirstAccessFor (int $lso_ref_id, array $usr_ids=[])
 
 getLastAccessFor (int $lso_ref_id, array $usr_ids=[])
 
 updateState (int $lso_ref_id, int $usr_id, int $ref_id, ILIAS\KioskMode\State $state, int $current_item=null)
 update a single State (for the item with ref_id); if $current_item is not set, assume that $ref_id is the current one. More...
 
 deleteFor (int $lso_ref_id, array $usr_ids=[])
 
 deleteForItem (int $lso_ref_id, int $item_ref_id)
 

Data Fields

const TABLE_NAME = 'lso_states'
 
const CURRENT_ITEM_ID = "current_item"
 
const STATES = "states"
 
const FIRST_ACCESS = "first_access"
 
const LAST_ACCESS = "last_access"
 

Protected Member Functions

 entryExistsFor (int $lso_ref_id, int $usr_id)
 
 insert (int $lso_ref_id, int $usr_id)
 
 update (ilDBInterface $db, int $lso_ref_id, int $usr_id, int $current_item, string $serialized)
 
 buildStates (string $serialized)
 
 serializeStates (array $states)
 
 select (int $lso_ref_id, array $usr_ids=[])
 

Protected Attributes

 $db
 

Detailed Description

Persistence for View-States.

Author
Nils Haagen nils..nosp@m.haag.nosp@m.en@co.nosp@m.ncep.nosp@m.ts-an.nosp@m.d-tr.nosp@m.ainin.nosp@m.g.de

Definition at line 10 of file class.ilLSStateDB.php.

Constructor & Destructor Documentation

◆ __construct()

ilLSStateDB::__construct ( ilDBInterface  $db)

Definition at line 24 of file class.ilLSStateDB.php.

25 {
26 $this->db = $db;
27 }

References $db.

Member Function Documentation

◆ buildStates()

ilLSStateDB::buildStates ( string  $serialized)
protected
Returns
array <int,ILIAS\KioskMode\State> ref_id=>State

Definition at line 216 of file class.ilLSStateDB.php.

216 : array
217 {
218 $states = [];
219 $data = json_decode($serialized, true);
220 foreach ($data as $ref_id => $kvpair) {
221 $states[$ref_id] = new ILIAS\KioskMode\State();
222 if (is_array($kvpair)) {
223 foreach ($kvpair as $key => $value) {
224 $states[$ref_id] = $states[$ref_id]->withValueFor($key, $value);
225 }
226 }
227 }
228
229 return $states;
230 }
Keeps the state of a view in a simple stringly type key-value store.
Definition: State.php:10
$data
Definition: storeScorm.php:23

References $data.

Referenced by select().

+ Here is the caller graph for this function:

◆ deleteFor()

ilLSStateDB::deleteFor ( int  $lso_ref_id,
array  $usr_ids = [] 
)
Parameters
int$lso_ref_id
int[]$usr_ids

Definition at line 167 of file class.ilLSStateDB.php.

168 {
169 $query =
170 "DELETE FROM " . static::TABLE_NAME . PHP_EOL
171 . "WHERE lso_ref_id = " . $this->db->quote($lso_ref_id, "integer") . PHP_EOL
172 ;
173
174 if (count($usr_ids) > 0) {
175 $query .= "AND usr_id IN (" . implode(',', $usr_ids) . ")";
176 }
177
178 $this->db->manipulate($query);
179 }
$query

References $query.

◆ deleteForItem()

ilLSStateDB::deleteForItem ( int  $lso_ref_id,
int  $item_ref_id 
)

Definition at line 181 of file class.ilLSStateDB.php.

182 {
183 $all_states = $this->select($lso_ref_id);
184 if (count($all_states) === 0) {
185 return;
186 }
187
188 $ilAtomQuery = $this->db->buildAtomQuery();
189 $ilAtomQuery->addTableLock(static::TABLE_NAME);
190 $ilAtomQuery->addQueryCallable(
191 function (ilDBInterface $db) use ($lso_ref_id, $all_states, $item_ref_id) {
192 foreach ($all_states as $usr_id => $state_entry) {
193 $current_item = $state_entry['current_item'];
194 $states = $state_entry['states'];
195
196 if ($current_item === $item_ref_id) {
197 $current_item = -1;
198 }
199
200 if (array_key_exists($item_ref_id, $states)) {
201 unset($states[$item_ref_id]);
202 }
203 $serialized = $this->serializeStates($states);
204 $this->update($db, $lso_ref_id, $usr_id, $current_item, $serialized);
205 }
206 }
207 );
208
209 $ilAtomQuery->run();
210 }
serializeStates(array $states)
select(int $lso_ref_id, array $usr_ids=[])
update(ilDBInterface $db, int $lso_ref_id, int $usr_id, int $current_item, string $serialized)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...

References $db, select(), serializeStates(), and update().

+ Here is the call graph for this function:

◆ entryExistsFor()

ilLSStateDB::entryExistsFor ( int  $lso_ref_id,
int  $usr_id 
)
protected

Definition at line 125 of file class.ilLSStateDB.php.

125 : bool
126 {
127 return count($this->select($lso_ref_id, [$usr_id])) > 0;
128 }

References select().

Referenced by updateState().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCurrentItemsFor()

ilLSStateDB::getCurrentItemsFor ( int  $lso_ref_id,
array  $usr_ids = [] 
)
Returns
array <int, int> usr_id => item_ref_id

Definition at line 49 of file class.ilLSStateDB.php.

49 : array
50 {
51 $data = $this->select($lso_ref_id, $usr_ids);
52 $ret = [];
53 foreach ($usr_ids as $usr_id) {
54 $ret[$usr_id] = -1;
55 if (array_key_exists($usr_id, $data)) {
56 $ret[$usr_id] = $data[$usr_id][self::CURRENT_ITEM_ID];
57 }
58 }
59
60 return $ret;
61 }
$ret
Definition: parser.php:6

References $data, $ret, CURRENT_ITEM_ID, and select().

+ Here is the call graph for this function:

◆ getFirstAccessFor()

ilLSStateDB::getFirstAccessFor ( int  $lso_ref_id,
array  $usr_ids = [] 
)

Definition at line 63 of file class.ilLSStateDB.php.

63 : array
64 {
65 $data = $this->select($lso_ref_id, $usr_ids);
66 $ret = [];
67 foreach ($usr_ids as $usr_id) {
68 $ret[$usr_id] = -1;
69 if (array_key_exists($usr_id, $data)) {
70 $ret[$usr_id] = $data[$usr_id][self::FIRST_ACCESS];
71 }
72 }
73
74 return $ret;
75 }

References $data, $ret, FIRST_ACCESS, and select().

+ Here is the call graph for this function:

◆ getLastAccessFor()

ilLSStateDB::getLastAccessFor ( int  $lso_ref_id,
array  $usr_ids = [] 
)

Definition at line 77 of file class.ilLSStateDB.php.

77 : array
78 {
79 $data = $this->select($lso_ref_id, $usr_ids);
80 $ret = [];
81 foreach ($usr_ids as $usr_id) {
82 $ret[$usr_id] = -1;
83 if (array_key_exists($usr_id, $data)) {
84 $ret[$usr_id] = $data[$usr_id][self::LAST_ACCESS];
85 }
86 }
87
88 return $ret;
89 }

References $data, $ret, LAST_ACCESS, and select().

+ Here is the call graph for this function:

◆ getStatesFor()

ilLSStateDB::getStatesFor ( int  $lso_ref_id,
array  $usr_ids = [] 
)
Returns
array <int,ILIAS\KioskMode\State> usr_id => [item_ref_id=>State]

Definition at line 32 of file class.ilLSStateDB.php.

32 : array
33 {
34 $data = $this->select($lso_ref_id, $usr_ids);
35 $ret = [];
36 foreach ($usr_ids as $usr_id) {
37 $ret[$usr_id] = [];
38 if (array_key_exists($usr_id, $data)) {
39 $ret[$usr_id] = $data[$usr_id][self::STATES];
40 }
41 }
42
43 return $ret;
44 }

References $data, $ret, select(), and STATES.

Referenced by updateState().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insert()

ilLSStateDB::insert ( int  $lso_ref_id,
int  $usr_id 
)
protected

Definition at line 130 of file class.ilLSStateDB.php.

131 {
132 $first_access = date("d.m.Y H:i:s");
133 $values = array(
134 "lso_ref_id" => array("integer", $lso_ref_id),
135 "usr_id" => array("integer", $usr_id),
136 "first_access" => array("text", $first_access)
137 );
138
139 $this->db->insert(static::TABLE_NAME, $values);
140 }

Referenced by updateState().

+ Here is the caller graph for this function:

◆ select()

ilLSStateDB::select ( int  $lso_ref_id,
array  $usr_ids = [] 
)
protected

Definition at line 245 of file class.ilLSStateDB.php.

246 {
247 $query =
248 "SELECT usr_id, current_item, states, first_access, last_access" . PHP_EOL
249 . "FROM " . static::TABLE_NAME . PHP_EOL
250 . "WHERE lso_ref_id = " . $this->db->quote($lso_ref_id, "integer") . PHP_EOL
251 ;
252
253 if (count($usr_ids) > 0) {
254 $query .= "AND usr_id IN (" . implode(',', $usr_ids) . ")";
255 }
256
257 $result = $this->db->query($query);
258
259 $ret = [];
260 while ($row = $this->db->fetchAssoc($result)) {
261 $ret[$row['usr_id']] = [
262 self::CURRENT_ITEM_ID => (int) $row[self::CURRENT_ITEM_ID],
263 self::STATES => $this->buildStates($row[self::STATES]),
264 self::FIRST_ACCESS => $row[self::FIRST_ACCESS],
265 self::LAST_ACCESS => $row[self::LAST_ACCESS]
266 ];
267 }
268
269 return $ret;
270 }
$result
buildStates(string $serialized)

References $query, $result, $ret, buildStates(), FIRST_ACCESS, and LAST_ACCESS.

Referenced by deleteForItem(), entryExistsFor(), getCurrentItemsFor(), getFirstAccessFor(), getLastAccessFor(), and getStatesFor().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ serializeStates()

ilLSStateDB::serializeStates ( array  $states)
protected
Parameters
array<int,ILIAS\KioskMode\State> ref_id=>State

Definition at line 235 of file class.ilLSStateDB.php.

236 {
237 $data = [];
238 foreach ($states as $ref_id => $state) {
239 $data[$ref_id] = json_decode($state->serialize());
240 }
241
242 return json_encode($data);
243 }

References $data.

Referenced by deleteForItem(), and updateState().

+ Here is the caller graph for this function:

◆ update()

ilLSStateDB::update ( ilDBInterface  $db,
int  $lso_ref_id,
int  $usr_id,
int  $current_item,
string  $serialized 
)
protected

Definition at line 142 of file class.ilLSStateDB.php.

148 {
149 $last_access = date("d.m.Y H:i:s");
150 $where = array(
151 "lso_ref_id" => array("integer", $lso_ref_id),
152 "usr_id" => array("integer", $usr_id)
153 );
154 $values = array(
155 "current_item" => array("integer", $current_item),
156 "states" => array("text", $serialized),
157 "last_access" => array("text", $last_access)
158 );
159
160 $db->update(static::TABLE_NAME, $values, $where);
161 }

References $db.

Referenced by deleteForItem(), and updateState().

+ Here is the caller graph for this function:

◆ updateState()

ilLSStateDB::updateState ( int  $lso_ref_id,
int  $usr_id,
int  $ref_id,
ILIAS\KioskMode\State  $state,
int  $current_item = null 
)

update a single State (for the item with ref_id); if $current_item is not set, assume that $ref_id is the current one.

Definition at line 95 of file class.ilLSStateDB.php.

101 {
102 $insert_first = $this->entryExistsFor($lso_ref_id, $usr_id) === false;
103 $states = $this->getStatesFor($lso_ref_id, [$usr_id]);
104 $states = $states[$usr_id];
105 $states[$ref_id] = $state;
106 $serialized = $this->serializeStates($states);
107 if (is_null($current_item)) {
108 $current_item = $ref_id;
109 }
110
111 $ilAtomQuery = $this->db->buildAtomQuery();
112 $ilAtomQuery->addTableLock(static::TABLE_NAME);
113 $ilAtomQuery->addQueryCallable(
114 function (ilDBInterface $db) use ($insert_first, $lso_ref_id, $usr_id, $current_item, $serialized) {
115 if ($insert_first) {
116 $this->insert($lso_ref_id, $usr_id);
117 }
118 $this->update($db, $lso_ref_id, $usr_id, $current_item, $serialized);
119 }
120 );
121
122 $ilAtomQuery->run();
123 }
insert(int $lso_ref_id, int $usr_id)
entryExistsFor(int $lso_ref_id, int $usr_id)
getStatesFor(int $lso_ref_id, array $usr_ids=[])

References $db, entryExistsFor(), getStatesFor(), insert(), serializeStates(), and update().

+ Here is the call graph for this function:

Field Documentation

◆ $db

ilLSStateDB::$db
protected

Definition at line 22 of file class.ilLSStateDB.php.

Referenced by __construct(), deleteForItem(), update(), and updateState().

◆ CURRENT_ITEM_ID

const ilLSStateDB::CURRENT_ITEM_ID = "current_item"

Definition at line 14 of file class.ilLSStateDB.php.

Referenced by getCurrentItemsFor().

◆ FIRST_ACCESS

const ilLSStateDB::FIRST_ACCESS = "first_access"

Definition at line 16 of file class.ilLSStateDB.php.

Referenced by getFirstAccessFor(), and select().

◆ LAST_ACCESS

const ilLSStateDB::LAST_ACCESS = "last_access"

Definition at line 17 of file class.ilLSStateDB.php.

Referenced by getLastAccessFor(), and select().

◆ STATES

const ilLSStateDB::STATES = "states"

Definition at line 15 of file class.ilLSStateDB.php.

Referenced by getStatesFor().

◆ TABLE_NAME

const ilLSStateDB::TABLE_NAME = 'lso_states'

Definition at line 12 of file class.ilLSStateDB.php.


The documentation for this class was generated from the following file: