ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCmiXapiResult.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
16{
20 protected $id;
21
25 protected $objId;
26
30 protected $usrId;
31
35 protected $version;
36
40 protected $score;
41
45 protected $status;
46
50 protected $lastUpdate;
51
62 public function __construct()
63 {
64 $this->id = 0;
65 $this->objId = 0;
66 $this->usrId = 0;
67 $this->version = 0;
68 $this->score = 0.0;
69 $this->status = '';
70 $this->lastUpdate = '';
71 }
72
76 public function getId() : int
77 {
78 return $this->id;
79 }
80
84 public function setId(int $id)
85 {
86 $this->id = $id;
87 }
88
92 public function getObjId() : int
93 {
94 return $this->objId;
95 }
96
100 public function setObjId(int $objId)
101 {
102 $this->objId = $objId;
103 }
104
108 public function getUsrId() : int
109 {
110 return $this->usrId;
111 }
112
116 public function setUsrId(int $usrId)
117 {
118 $this->usrId = $usrId;
119 }
120
124 public function getVersion() : int
125 {
126 return $this->version;
127 }
128
132 public function setVersion(int $version)
133 {
134 $this->version = $version;
135 }
136
140 public function getScore() : float
141 {
142 return $this->score;
143 }
144
148 public function setScore(float $score)
149 {
150 $this->score = $score;
151 }
152
156 public function getStatus() : string
157 {
158 return $this->status;
159 }
160
164 public function setStatus(string $status)
165 {
166 $this->status = $status;
167 }
168
172 public function getLastUpdate() : string
173 {
174 return $this->lastUpdate;
175 }
176
180 public function setLastUpdate(string $lastUpdate)
181 {
182 $this->lastUpdate = $lastUpdate;
183 }
184
185 public function save()
186 {
187 if ($this->getId()) {
188 $this->update();
189 } else {
190 $this->insert();
191 }
192 }
193
194 protected function update()
195 {
196 global $DIC; /* @var \ILIAS\DI\Container $DIC */
197
198 $DIC->database()->update('cmix_results', array(
199 'obj_id' => array('intger', $this->getObjId()),
200 'usr_id' => array('intger', $this->getUsrId()),
201 'version' => array('intger', $this->getVersion()),
202 'score' => array('float', $this->getScore()),
203 'status' => array('text', $this->getStatus()),
204 'last_update' => array('timestamp', ilCmiXapiAuthToken::selectCurrentTimestamp())
205 ), array(
206 'id' => array('intger', $this->getId())
207 ));
208 }
209
210 protected function insert()
211 {
212 global $DIC; /* @var \ILIAS\DI\Container $DIC */
213
214 $this->setId($DIC->database()->nextId('cmix_results'));
215
216 $DIC->database()->insert('cmix_results', array(
217 'id' => array('intger', $this->getId()),
218 'obj_id' => array('intger', $this->getObjId()),
219 'usr_id' => array('intger', $this->getUsrId()),
220 'version' => array('intger', $this->getVersion()),
221 'score' => array('float', $this->getScore()),
222 'status' => array('text', $this->getStatus()),
223 'last_update' => array('timestamp', ilCmiXapiAuthToken::selectCurrentTimestamp())
224 ));
225 }
226
231 protected function assignFromDbRow($row)
232 {
233 $this->setId($row['id']);
234 $this->setObjId($row['obj_id']);
235 $this->setUsrId($row['usr_id']);
236 $this->setVersion($row['version']);
237 $this->setScore($row['score']);
238 $this->setStatus($row['status']);
239 $this->setLastUpdate($row['last_update']);
240 }
241
242 public static function getInstanceByObjIdAndUsrId($objId, $usrId)
243 {
244 global $DIC; /* @var \ILIAS\DI\Container $DIC */
245
246 $query = "
247 SELECT * FROM cmix_results
248 WHERE obj_id = %s AND usr_id = %s
249 ";
250
251 $res = $DIC->database()->queryF($query, array('integer', 'integer'), array($objId, $usrId));
252
253 while ($row = $DIC->database()->fetchAssoc($res)) {
254 $result = new self();
255 $result->assignFromDbRow($row);
256
257 return $result;
258 }
259
260 throw new ilCmiXapiException(
261 "no result record exists for: usr=$usrId obj=$objId"
262 );
263 }
264
265 public static function getEmptyInstance()
266 {
267 return new self();
268 }
269
274 public static function getResultsForObject($objId)
275 {
276 global $DIC;
277
278 $query = 'SELECT * FROM cmix_results'
279 . ' WHERE obj_id = ' . $DIC->database()->quote($objId, 'integer');
280
281 $res = $DIC->database()->query($query);
282
283 $results = [];
284
285 if ($row = $DIC->database()->fetchAssoc($res)) {
286 $result = new self();
287 $result->assignFromDbRow($row);
288
289 $results[$result->getUsrId()] = $result;
290 }
291
292 return $results;
293 }
294}
$result
An exception for terminatinating execution or to throw for unit testing.
static getInstanceByObjIdAndUsrId($objId, $usrId)
static getResultsForObject($objId)
__construct()
ilCmiXapiResult constructor.
setStatus(string $status)
setLastUpdate(string $lastUpdate)
$query
$results
foreach($_POST as $key=> $value) $res
$DIC
Definition: xapitoken.php:46