ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
12class ilLicense {
13
17 protected $obj_id = 0;
21 protected $licenses = 0;
25 protected $remarks = '';
29 protected $accesses = 0;
30
31
37 public function __construct($a_obj_id) {
38 $this->obj_id = (int)$a_obj_id;
39 $this->read();
40 }
41
42
46 public function getObjId() {
47 return $this->obj_id;
48 }
49
50
54 public function setObjId($obj_id) {
55 $this->obj_id = $obj_id;
56 }
57
58
62 public function setLicenses($a_licenses = 0) {
63 $this->licenses = (int)$a_licenses;
64 }
65
66
70 public function getLicenses() {
71 return $this->licenses;
72 }
73
74
78 public function setRemarks($a_remarks = '') {
79 $this->remarks = $a_remarks;
80 }
81
82
86 public function getRemarks() {
87 return $this->remarks;
88 }
89
90
94 public function getAccesses() {
95 return $this->accesses;
96 }
97
98
102 public function getRemainingLicenses() {
103 return max(0, $this->licenses - $this->accesses);
104 }
105
106
113 public function getPotentialAccesses() {
114 global $ilDB;
115
116 // get the operation id for read access
117 $ops_ids = ilRbacReview::_getOperationIdsByName(array( 'read' ));
118
119 // first get all roles with read access
120 $role_ids = array();
121 $query = 'SELECT DISTINCT pa.rol_id' . ' FROM rbac_pa pa' . ' INNER JOIN object_reference ob ON ob.ref_id = pa.ref_id' . ' WHERE '
122 . $ilDB->like('pa.ops_id', 'text', '%%i:' . $ops_ids[0] . ';%%') . ' AND ob.obj_id = ' . $ilDB->quote($this->getObjId(), 'integer');
123
124 $result = $ilDB->query($query);
125 while ($row = $ilDB->fetchObject($result)) {
126 $role_ids[] = $row->rol_id;
127 }
128
129 if (!count($role_ids)) {
130 return 0;
131 }
132
133 // then count all users of these roles without read events
134 $query = 'SELECT COUNT(DISTINCT(usr_id)) accesses ' . ' FROM rbac_ua' . ' WHERE ' . $ilDB->in('rol_id', $role_ids, false, 'integer')
135 . ' AND usr_id NOT IN' . ' (SELECT usr_id FROM read_event' . ' WHERE obj_id = ' . $ilDB->quote($this->getObjId(), 'integer') . ')';
136
137 $result = $ilDB->query($query);
138 $row = $ilDB->fetchObject($result);
139
140 return $row->accesses;
141 }
142
143
145 // Data maintenance
146
152 public function read() {
153 global $ilDB;
154
155 $query = 'SELECT * FROM license_data WHERE obj_id = %s';
156 $result = $ilDB->queryF($query, array( 'integer' ), array( $this->getObjId() ));
157
158 if ($row = $ilDB->fetchObject($result)) {
159 $this->licenses = $row->licenses;
160 $this->accesses = $row->used;
161 $this->remarks = $row->remarks;
162 } else {
163 $this->licenses = 0;
164 $this->accesses = 0;
165 $this->remarks = '';
166 }
167 }
168
169
175 public function update() {
176 global $ilDB;
177
178 $query = 'SELECT * FROM license_data WHERE obj_id = %s';
179 $result = $ilDB->queryF($query, array( 'integer' ), array( $this->obj_id ));
180
181 if ($row = $ilDB->fetchObject($result)) {
182 $ilDB->update('license_data', array(
183 'licenses' => array( 'integer', $this->licenses ),
184 'used' => array( 'integer', $this->accesses ),
185 'remarks' => array( 'clob', $this->remarks ),
186 ), array(
187 'obj_id' => array( 'integer', $this->obj_id ),
188 ));
189 } else {
190 $ilDB->insert('license_data', array(
191 'obj_id' => array( 'integer', $this->obj_id ),
192 'licenses' => array( 'integer', $this->licenses ),
193 'used' => array( 'integer', $this->accesses ),
194 'remarks' => array( 'clob', $this->remarks ),
195 ));
196 }
197 }
198
199
205 public function delete() {
206 global $ilDB;
207
208 $query = 'DELETE FROM license_data WHERE obj_id = %s';
209 $ilDB->manipulateF($query, array( 'integer' ), array( $this->obj_id ));
210 }
211
212
214 // Tracking
215
229 public static function _checkAccess($a_usr_id, $a_obj_id) {
230 // Implementation moved
231 require_once("Services/License/classes/class.ilLicenseAccess.php");
232
233 return ilLicenseAccess::_checkAccess($a_usr_id, $a_obj_id);
234 }
235
236
246 public static function _noteAccess($a_obj_id, $a_type, $a_ref_id) {
247 global $ilDB, $ilUser;
248
249 // don't note the access if licensing is globally disabled
250 require_once("Services/License/classes/class.ilLicenseAccess.php");
252 return;
253 }
254
255 // check if user has already accessed
256 $query = 'SELECT read_count FROM read_event ' . 'WHERE usr_id = %s AND obj_id = %s';
257 $result = $ilDB->queryF($query, array( 'integer', 'integer' ), array( $ilUser->getId(), $a_obj_id ));
258
259 if ($row = $ilDB->fetchObject($result)) {
260 // already accessed -> nothing to do
261 return;
262 } else {
263 // note access
264 require_once('Services/Tracking/classes/class.ilChangeEvent.php');
265 ilChangeEvent::_recordReadEvent($a_type, $a_ref_id, $a_obj_id, $ilUser->getId());
266
267 if (self::_isLicensed($a_obj_id)) {
268 // increase used licenses
269 $query = "UPDATE license_data SET used = used + 1 " . "WHERE obj_id = %s";
270 $ilDB->manipulateF($query, array( 'integer' ), array( $a_obj_id ));
271 }
272 }
273 }
274
275
277 // Static Queries
278
284 public static function _getLicensedObjects() {
285 global $ilDB;
286 $objects = array();
287
288 $query = 'SELECT od.obj_id, od.type, od.title, od.description, re.ref_id ' . 'FROM license_data ld '
289 . 'INNER JOIN object_data od ON od.obj_id = ld.obj_id ' . 'INNER JOIN object_reference re ON re.obj_id = od.obj_id '
290 . 'WHERE ld.licenses > 0 ' . 'ORDER BY od.title, od.obj_id';
291
292 $result = $ilDB->query($query);
293 $obj_id = 0;
294 while ($row = $ilDB->fetchAssoc($result)) {
295 if ($row['obj_id'] != $obj_id) {
296 $objects[] = $row;
297 $obj_id = $row['obj_id'];
298 }
299 }
300
301 return $objects;
302 }
303
304
311 public static function _getLicensedChildObjects($a_ref_id) {
312 global $tree;
313 $objects = array();
314
315 $childs = $tree->getChilds($a_ref_id, 'title');
316 foreach ($childs as $data) {
317 if (in_array($data['type'], array( 'sahs', 'htlm' )) and self::_isLicensed($data['obj_id'])) {
318 $objects[] = $data;
319 }
320 }
321
322 return $objects;
323 }
324
325
332 public static function _isLicensed($a_obj_id) {
333 global $ilDB;
334
335 $query = "SELECT licenses FROM license_data " . "WHERE obj_id = %s " . "AND licenses > 0";
336 $result = $ilDB->queryF($query, array( 'integer' ), array( $a_obj_id ));
337 if ($row = $ilDB->fetchObject($result)) {
338 return true;
339 }
340 }
341}
342
$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.
global $ilDB
$ilUser
Definition: imgupload.php:18
$a_type
Definition: workflow.php:93