ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
class.ilLPMarks.php
Go to the documentation of this file.
1<?php
2
26{
27 protected ?ilDBInterface $db;
29
30 protected int $obj_id;
31 protected int $usr_id;
32 protected ?string $obj_type;
33
34 protected bool $completed = false;
35 protected string $comment = '';
36 protected string $mark = '';
37 protected string $status_changed = '';
38
39 protected $has_entry = false;
40
41 public function __construct(int $a_obj_id, int $a_usr_id)
42 {
43 global $DIC;
44
45 $this->ilObjectDataCache = $DIC['ilObjDataCache'];
46 $this->db = $DIC->database();
47
48 $this->obj_id = $a_obj_id;
49 $this->usr_id = $a_usr_id;
50 $this->obj_type = $this->ilObjectDataCache->lookupType($this->obj_id);
51
52 $this->__read();
53 }
54
55 public static function deleteObject(int $a_obj_id): void
56 {
57 global $DIC;
58
59 $ilDB = $DIC['ilDB'];
60
61 $query = "DELETE FROM ut_lp_marks " .
62 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer');
63 $res = $ilDB->manipulate($query);
64 }
65
66 public function getUserId(): int
67 {
68 return $this->usr_id;
69 }
70
71 public function setMark(string $a_mark): void
72 {
73 $this->mark = $a_mark;
74 }
75
76 public function getMark(): string
77 {
78 return $this->mark;
79 }
80
81 public function setComment(string $a_comment): void
82 {
83 $this->comment = $a_comment;
84 }
85
86 public function getComment(): string
87 {
88 return $this->comment;
89 }
90
91 public function setCompleted(bool $a_status): void
92 {
93 $this->completed = $a_status;
94 }
95
96 public function getCompleted(): bool
97 {
98 return $this->completed;
99 }
100
101 public function getStatusChanged(): string
102 {
104 }
105
106 public function getObjId(): int
107 {
108 return $this->obj_id;
109 }
110
111 public function update(): void
112 {
113 if (!$this->has_entry) {
114 $this->__add();
115 }
116 $query = "UPDATE ut_lp_marks " .
117 "SET mark = " . $this->db->quote($this->getMark(), 'text') . ", " .
118 "u_comment = " . $this->db->quote(
119 $this->getComment(),
120 'text'
121 ) . ", " .
122 "completed = " . $this->db->quote(
123 $this->getCompleted(),
124 'integer'
125 ) . " " .
126 "WHERE obj_id = " . $this->db->quote(
127 $this->getObjId(),
128 'integer'
129 ) . " " .
130 "AND usr_id = " . $this->db->quote($this->getUserId(), 'integer');
131 $res = $this->db->manipulate($query);
132 }
133
134 // Static
135 public static function _hasCompleted(int $a_usr_id, int $a_obj_id): bool
136 {
137 global $DIC;
138
139 $ilDB = $DIC['ilDB'];
140
141 $query = "SELECT * FROM ut_lp_marks " .
142 "WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
143 "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
144
145 $res = $ilDB->query($query);
146 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
147 return (bool) $row->completed;
148 }
149 return false;
150 }
151
152 public static function getCompletionsOfUser(
153 int $user_id,
154 string $from,
155 string $to
156 ): array {
157 global $DIC;
158
159 $ilDB = $DIC['ilDB'];
160
161 $query = "SELECT * FROM ut_lp_marks " .
162 "WHERE usr_id = " . $ilDB->quote($user_id, 'integer') .
163 " AND status = " . $ilDB->quote(
165 'integer'
166 ) .
167 " AND status_changed >= " . $ilDB->quote($from, "timestamp") .
168 " AND status_changed <= " . $ilDB->quote($to, "timestamp");
169
170 $set = $ilDB->query($query);
171 $completions = array();
172 while ($rec = $ilDB->fetchAssoc($set)) {
173 $completion = [
174 'obj_id' => (int) $rec['obj_id'],
175 'usr_id' => (int) $rec['usr_id'],
176 'completed' => (bool) $rec['completed'],
177 'mark' => (string) $rec['mark'],
178 'comment' => (string) $rec['u_comment'],
179 'status' => (int) $rec['status'],
180 'status_changed' => (string) $rec['status_changed'],
181 'status_dirty' => (int) $rec['status_changed'],
182 'percentage' => (int) $rec['percentage']
183 ];
184 $completions[] = $completion;
185 }
186 return $completions;
187 }
188
189 public static function _lookupMark(int $a_usr_id, int $a_obj_id): string
190 {
191 global $DIC;
192
193 $ilDB = $DIC['ilDB'];
194
195 $query = "SELECT * FROM ut_lp_marks " .
196 "WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
197 "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
198
199 $res = $ilDB->query($query);
200 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
201 return (string) $row->mark;
202 }
203 return '';
204 }
205
206 public static function _lookupComment(int $a_usr_id, int $a_obj_id): string
207 {
208 global $DIC;
209
210 $ilDB = $DIC['ilDB'];
211
212 $query = "SELECT * FROM ut_lp_marks " .
213 "WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
214 "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer');
215
216 $res = $ilDB->query($query);
217 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
218 return (string) $row->u_comment;
219 }
220 return '';
221 }
222
223 // Private
224 public function __read(): bool
225 {
226 $res = $this->db->query(
227 "SELECT * FROM ut_lp_marks " .
228 "WHERE obj_id = " . $this->db->quote(
229 $this->obj_id,
230 'integer'
231 ) . " " .
232 "AND usr_id = " . $this->db->quote($this->usr_id, 'integer')
233 );
234 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
235 $this->has_entry = true;
236 $this->completed = (int) $row->completed;
237 $this->comment = (string) $row->u_comment;
238 $this->mark = (string) $row->mark;
239 $this->status_changed = (string) $row->status_changed;
240 return true;
241 }
242 return false;
243 }
244
245 public function __add(): void
246 {
247 $query = "INSERT INTO ut_lp_marks (mark,u_comment, completed,obj_id,usr_id) " .
248 "VALUES( " .
249 $this->db->quote($this->getMark(), 'text') . ", " .
250 $this->db->quote($this->getComment(), 'text') . ", " .
251 $this->db->quote($this->getCompleted(), 'integer') . ", " .
252 $this->db->quote($this->getObjId(), 'integer') . ", " .
253 $this->db->quote($this->getUserId(), 'integer') . " " .
254 ")";
255 $res = $this->db->manipulate($query);
256 $this->has_entry = true;
257 }
258
259 public static function _deleteForUsers(
260 int $a_obj_id,
261 array $a_user_ids
262 ): void {
263 global $DIC;
264
265 $ilDB = $DIC['ilDB'];
266 $ilDB->manipulate(
267 "DELETE FROM ut_lp_marks" .
268 " WHERE obj_id = " . $ilDB->quote($a_obj_id, "integer") .
269 " AND " . $ilDB->in("usr_id", $a_user_ids, "", "integer")
270 );
271 }
272
273 public static function _getAllUserIds(int $a_obj_id): array
274 {
275 global $DIC;
276
277 $ilDB = $DIC['ilDB'];
278
279 $res = array();
280 $set = $ilDB->query(
281 "SELECT usr_id FROM ut_lp_marks" .
282 " WHERE obj_id = " . $ilDB->quote($a_obj_id, "integer")
283 );
284 while ($row = $ilDB->fetchAssoc($set)) {
285 $res[] = (int) $row["usr_id"];
286 }
287 return $res;
288 }
289}
const FETCHMODE_OBJECT
static deleteObject(int $a_obj_id)
static getCompletionsOfUser(int $user_id, string $from, string $to)
setComment(string $a_comment)
static _lookupComment(int $a_usr_id, int $a_obj_id)
static _getAllUserIds(int $a_obj_id)
static _hasCompleted(int $a_usr_id, int $a_obj_id)
string $status_changed
setMark(string $a_mark)
ilDBInterface $db
static _lookupMark(int $a_usr_id, int $a_obj_id)
static _deleteForUsers(int $a_obj_id, array $a_user_ids)
setCompleted(bool $a_status)
string $comment
__construct(int $a_obj_id, int $a_usr_id)
string $obj_type
ilObjectDataCache $ilObjectDataCache
const LP_STATUS_COMPLETED_NUM
class ilObjectDataCache
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26