ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilLicense.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
13{
14
18 protected $obj_id = 0;
22 protected $licenses = 0;
26 protected $remarks = '';
30 protected $accesses = 0;
31
32
38 public function __construct($a_obj_id)
39 {
40 $this->obj_id = (int) $a_obj_id;
41 $this->read();
42 }
43
44
48 public function getObjId()
49 {
50 return $this->obj_id;
51 }
52
53
57 public function setObjId($obj_id)
58 {
59 $this->obj_id = $obj_id;
60 }
61
62
66 public function setLicenses($a_licenses = 0)
67 {
68 $this->licenses = (int) $a_licenses;
69 }
70
71
75 public function getLicenses()
76 {
77 return $this->licenses;
78 }
79
80
84 public function setRemarks($a_remarks = '')
85 {
86 $this->remarks = $a_remarks;
87 }
88
89
93 public function getRemarks()
94 {
95 return $this->remarks;
96 }
97
98
102 public function getAccesses()
103 {
104 return $this->accesses;
105 }
106
107
111 public function getRemainingLicenses()
112 {
113 return max(0, $this->licenses - $this->accesses);
114 }
115
116
123 public function getPotentialAccesses()
124 {
125 global $ilDB;
126
127 // get the operation id for read access
128 $ops_ids = ilRbacReview::_getOperationIdsByName(array( 'read' ));
129
130 // first get all roles with read access
131 $role_ids = array();
132 $query = 'SELECT DISTINCT pa.rol_id' . ' FROM rbac_pa pa' . ' INNER JOIN object_reference ob ON ob.ref_id = pa.ref_id' . ' WHERE '
133 . $ilDB->like('pa.ops_id', 'text', '%%i:' . $ops_ids[0] . ';%%') . ' AND ob.obj_id = ' . $ilDB->quote($this->getObjId(), 'integer');
134
135 $result = $ilDB->query($query);
136 while ($row = $ilDB->fetchObject($result)) {
137 $role_ids[] = $row->rol_id;
138 }
139
140 if (!count($role_ids)) {
141 return 0;
142 }
143
144 // then count all users of these roles without read events
145 $query = 'SELECT COUNT(DISTINCT(usr_id)) accesses ' . ' FROM rbac_ua' . ' WHERE ' . $ilDB->in('rol_id', $role_ids, false, 'integer')
146 . ' AND usr_id NOT IN' . ' (SELECT usr_id FROM read_event' . ' WHERE obj_id = ' . $ilDB->quote($this->getObjId(), 'integer') . ')';
147
148 $result = $ilDB->query($query);
149 $row = $ilDB->fetchObject($result);
150
151 return $row->accesses;
152 }
153
154
156 // Data maintenance
157
163 public function read()
164 {
165 global $ilDB;
166
167 $query = 'SELECT * FROM license_data WHERE obj_id = %s';
168 $result = $ilDB->queryF($query, array( 'integer' ), array( $this->getObjId() ));
169
170 if ($row = $ilDB->fetchObject($result)) {
171 $this->licenses = $row->licenses;
172 $this->accesses = $row->used;
173 $this->remarks = $row->remarks;
174 } else {
175 $this->licenses = 0;
176 $this->accesses = 0;
177 $this->remarks = '';
178 }
179 }
180
181
187 public function update()
188 {
189 global $ilDB;
190
191 $query = 'SELECT * FROM license_data WHERE obj_id = %s';
192 $result = $ilDB->queryF($query, array( 'integer' ), array( $this->obj_id ));
193
194 if ($row = $ilDB->fetchObject($result)) {
195 $ilDB->update('license_data', array(
196 'licenses' => array( 'integer', $this->licenses ),
197 'used' => array( 'integer', $this->accesses ),
198 'remarks' => array( 'clob', $this->remarks ),
199 ), array(
200 'obj_id' => array( 'integer', $this->obj_id ),
201 ));
202 } else {
203 $ilDB->insert('license_data', array(
204 'obj_id' => array( 'integer', $this->obj_id ),
205 'licenses' => array( 'integer', $this->licenses ),
206 'used' => array( 'integer', $this->accesses ),
207 'remarks' => array( 'clob', $this->remarks ),
208 ));
209 }
210 }
211
212
218 public function delete()
219 {
220 global $ilDB;
221
222 $query = 'DELETE FROM license_data WHERE obj_id = %s';
223 $ilDB->manipulateF($query, array( 'integer' ), array( $this->obj_id ));
224 }
225
226
228 // Tracking
229
243 public static function _checkAccess($a_usr_id, $a_obj_id)
244 {
245 // Implementation moved
246 require_once("Services/License/classes/class.ilLicenseAccess.php");
247
248 return ilLicenseAccess::_checkAccess($a_usr_id, $a_obj_id);
249 }
250
251
261 public static function _noteAccess($a_obj_id, $a_type, $a_ref_id)
262 {
263 global $ilDB, $ilUser;
264
265 // don't note the access if licensing is globally disabled
266 require_once("Services/License/classes/class.ilLicenseAccess.php");
268 return;
269 }
270
271 // check if user has already accessed
272 $query = 'SELECT read_count FROM read_event ' . 'WHERE usr_id = %s AND obj_id = %s';
273 $result = $ilDB->queryF($query, array( 'integer', 'integer' ), array( $ilUser->getId(), $a_obj_id ));
274
275 if ($row = $ilDB->fetchObject($result)) {
276 // already accessed -> nothing to do
277 return;
278 } else {
279 // note access
280 require_once('Services/Tracking/classes/class.ilChangeEvent.php');
281 ilChangeEvent::_recordReadEvent($a_type, $a_ref_id, $a_obj_id, $ilUser->getId());
282
283 if (self::_isLicensed($a_obj_id)) {
284 // increase used licenses
285 $query = "UPDATE license_data SET used = used + 1 " . "WHERE obj_id = %s";
286 $ilDB->manipulateF($query, array( 'integer' ), array( $a_obj_id ));
287 }
288 }
289 }
290
291
293 // Static Queries
294
300 public static function _getLicensedObjects()
301 {
302 global $ilDB;
303 $objects = array();
304
305 $query = 'SELECT od.obj_id, od.type, od.title, od.description, re.ref_id ' . 'FROM license_data ld '
306 . 'INNER JOIN object_data od ON od.obj_id = ld.obj_id ' . 'INNER JOIN object_reference re ON re.obj_id = od.obj_id '
307 . 'WHERE ld.licenses > 0 ' . 'ORDER BY od.title, od.obj_id';
308
309 $result = $ilDB->query($query);
310 $obj_id = 0;
311 while ($row = $ilDB->fetchAssoc($result)) {
312 if ($row['obj_id'] != $obj_id) {
313 $objects[] = $row;
314 $obj_id = $row['obj_id'];
315 }
316 }
317
318 return $objects;
319 }
320
321
328 public static function _getLicensedChildObjects($a_ref_id)
329 {
330 global $tree;
331 $objects = array();
332
333 $childs = $tree->getChilds($a_ref_id, 'title');
334 foreach ($childs as $data) {
335 if (in_array($data['type'], array( 'sahs', 'htlm' )) and self::_isLicensed($data['obj_id'])) {
336 $objects[] = $data;
337 }
338 }
339
340 return $objects;
341 }
342
343
350 public static function _isLicensed($a_obj_id)
351 {
352 global $ilDB;
353
354 $query = "SELECT licenses FROM license_data " . "WHERE obj_id = %s " . "AND licenses > 0";
355 $result = $ilDB->queryF($query, array( 'integer' ), array( $a_obj_id ));
356 if ($row = $ilDB->fetchObject($result)) {
357 return true;
358 }
359 }
360}
$result
An exception for terminatinating execution or to throw for unit testing.
static _recordReadEvent( $a_type, $a_ref_id, $obj_id, $usr_id, $isCatchupWriteEvents=true, $a_ext_rc=false, $a_ext_time=false)
Records a read event and catches up with write events.
static _isEnabled()
Check, if licencing is enabled This check is called from the ilAccessHandler class.
static _checkAccess($a_usr_id, $a_obj_id)
Check, if a user can access an object by license.
__construct($a_obj_id)
Constructor.
static _getLicensedObjects()
Get a list of all objects with activated licensing.
setLicenses($a_licenses=0)
setObjId($obj_id)
getPotentialAccesses()
Get the number of users who may access the object but don't have yet a license.
static _checkAccess($a_usr_id, $a_obj_id)
Check, if a user can access an object by license.
static _noteAccess($a_obj_id, $a_type, $a_ref_id)
Note the access of the current usr to an object.
update()
Update the license data in the database.
static _isLicensed($a_obj_id)
Check if an object has licensing activated.
read()
Read the license data from the database.
static _getLicensedChildObjects($a_ref_id)
Get a list of all sub objects with activated licensing.
setRemarks($a_remarks='')
static _getOperationIdsByName($operations)
get ops_id's by name.
$query
global $ilDB
$ilUser
Definition: imgupload.php:18
$a_type
Definition: workflow.php:92