ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCmiXapiResult.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31{
32 protected int $id;
33
34 protected int $objId;
35
36 protected int $usrId;
37
38 protected int $version;
39
40 protected float $score;
41
42 protected string $status;
43
44 protected string $lastUpdate;
45
47
51 public function __construct()
52 {
53 global $DIC;
54 $this->database = $DIC->database();
55 $this->id = 0;
56 $this->objId = 0;
57 $this->usrId = 0;
58 $this->version = 0;
59 $this->score = 0.0;
60 $this->status = '';
61 $this->lastUpdate = '';
62 }
63
64 public function getId(): int
65 {
66 return $this->id;
67 }
68
69 public function setId(int $id): void
70 {
71 $this->id = $id;
72 }
73
74 public function getObjId(): int
75 {
76 return $this->objId;
77 }
78
79 public function setObjId(int $objId): void
80 {
81 $this->objId = $objId;
82 }
83
84 public function getUsrId(): int
85 {
86 return $this->usrId;
87 }
88
89 public function setUsrId(int $usrId): void
90 {
91 $this->usrId = $usrId;
92 }
93
94 public function getVersion(): int
95 {
96 return $this->version;
97 }
98
99 public function setVersion(int $version): void
100 {
101 $this->version = $version;
102 }
103
104 public function getScore(): float
105 {
106 return $this->score;
107 }
108
109 public function setScore(float $score): void
110 {
111 $this->score = $score;
112 }
113
114 public function getStatus(): string
115 {
116 return $this->status;
117 }
118
119 public function setStatus(string $status): void
120 {
121 $this->status = $status;
122 }
123
124 public function getLastUpdate(): string
125 {
126 return $this->lastUpdate;
127 }
128
129 public function setLastUpdate(string $lastUpdate): void
130 {
131 $this->lastUpdate = $lastUpdate;
132 }
133
134 public function save(): void
135 {
136 if ($this->getId()) {
137 $this->update();
138 } else {
139 $this->insert();
140 }
141 }
142
143 protected function update(): void
144 {
145 $this->database->update('cmix_results', array(
146 'obj_id' => array('intger', $this->getObjId()),
147 'usr_id' => array('intger', $this->getUsrId()),
148 'version' => array('intger', $this->getVersion()),
149 'score' => array('float', $this->getScore()),
150 'status' => array('text', $this->getStatus()),
151 'last_update' => array('timestamp', ilCmiXapiAuthToken::selectCurrentTimestamp())
152 ), array(
153 'id' => array('intger', $this->getId())
154 ));
155 }
156
157 protected function insert(): void
158 {
159 $this->setId($this->database->nextId('cmix_results'));
160
161 $this->database->insert('cmix_results', array(
162 'id' => array('integer', $this->getId()),
163 'obj_id' => array('integer', $this->getObjId()),
164 'usr_id' => array('integer', $this->getUsrId()),
165 'version' => array('integer', $this->getVersion()),
166 'score' => array('float', $this->getScore()),
167 'status' => array('text', $this->getStatus()),
168 'last_update' => array('timestamp', ilCmiXapiAuthToken::selectCurrentTimestamp())
169 ));
170 }
171
172 protected function assignFromDbRow(array $row): void
173 {
174 $this->setId((int) $row['id']);
175 $this->setObjId((int) $row['obj_id']);
176 $this->setUsrId((int) $row['usr_id']);
177 $this->setVersion((int) $row['version']);
178 $this->setScore((float) $row['score']);
179 $this->setStatus((string) $row['status']);
180 $this->setLastUpdate((string) $row['last_update']);
181 }
182
186 public static function getInstanceByObjIdAndUsrId(int $objId, int $usrId): \ilCmiXapiResult
187 {
188 global $DIC; /* @var \ILIAS\DI\Container $DIC */
189 $query = "
190 SELECT * FROM cmix_results
191 WHERE obj_id = %s AND usr_id = %s
192 ";
193
194 $res = $DIC->database()->queryF($query, array('integer', 'integer'), array($objId, $usrId));
195
196 while ($row = $DIC->database()->fetchAssoc($res)) {
197 $result = new self();
198 $result->assignFromDbRow($row);
199
200 return $result;
201 }
202
203 throw new ilCmiXapiException(
204 "no result record exists for: usr=$usrId obj=$objId"
205 );
206 }
207
208 public static function getEmptyInstance(): \ilCmiXapiResult
209 {
210 return new self();
211 }
212
216 public static function getResultsForObject(int $objId): array
217 {
218 global $DIC;
219
220 $query = 'SELECT * FROM cmix_results'
221 . ' WHERE obj_id = ' . $DIC->database()->quote($objId, 'integer');
222
223 $res = $DIC->database()->query($query);
224
225 $results = [];
226
227 if ($row = $DIC->database()->fetchAssoc($res)) {
228 $result = new self();
229 $result->assignFromDbRow($row);
230
231 $results[$result->getUsrId()] = $result;
232 }
233
234 return $results;
235 }
236}
__construct()
ilCmiXapiResult constructor.
setVersion(int $version)
setStatus(string $status)
static getInstanceByObjIdAndUsrId(int $objId, int $usrId)
static getResultsForObject(int $objId)
setLastUpdate(string $lastUpdate)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$results
global $DIC
Definition: shib_login.php:26