ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
14{
19 function ilLicense($a_obj_id)
20 {
21 $this->obj_id = (int) $a_obj_id;
22 $this->read();
23 }
24
26 // SET GET
27
28 function setLicenses($a_licenses = 0)
29 {
30 $this->licenses = (int) $a_licenses;
31 }
32 function getLicenses()
33 {
34 return $this->licenses;
35 }
36 function setRemarks($a_remarks = '')
37 {
38 $this->remarks = $a_remarks;
39 }
40 function getRemarks()
41 {
42 return $this->remarks;
43 }
44 function getAccesses()
45 {
46 return $this->accesses;
47 }
49 {
50 return max(0, $this->licenses - $this->accesses);
51 }
52
60 {
61 global $ilDB;
62
63 // get the operation id for read access
64 $ops_ids = ilRbacReview::_getOperationIdsByName(array('read'));
65
66 // first get all roles with read access
67 $role_ids = array();
68 $query = 'SELECT DISTINCT pa.rol_id'
69 . ' FROM rbac_pa pa'
70 . ' INNER JOIN object_reference ob ON ob.ref_id = pa.ref_id'
71 . ' WHERE '.$ilDB->like('pa.ops_id', 'text', '%%i:'.$ops_ids[0].';%%')
72 . ' AND ob.obj_id = ' . $ilDB->quote($this->obj_id, 'integer');
73
74 $result = $ilDB->query($query);
75 while ($row = $ilDB->fetchObject($result))
76 {
77 $role_ids[] = $row->rol_id;
78 }
79
80 if (!count($role_ids))
81 {
82 return 0;
83 }
84
85 // then count all users of these roles without read events
86 $query = 'SELECT COUNT(DISTINCT(usr_id)) accesses '
87 . ' FROM rbac_ua'
88 . ' WHERE '. $ilDB->in('rol_id', $role_ids, false, 'integer')
89 . ' AND usr_id NOT IN'
90 . ' (SELECT usr_id FROM read_event'
91 . ' WHERE obj_id = ' . $ilDB->quote($this->obj_id, 'integer') . ')';
92
93 $result = $ilDB->query($query);
94 $row = $ilDB->fetchObject($result);
95 return $row->accesses;
96 }
97
98
100 // Data maintenance
101
107 function read()
108 {
109 global $ilDB;
110
111 $query = 'SELECT * FROM license_data WHERE obj_id = %s';
112 $result = $ilDB->queryF($query, array('integer'), array($this->obj_id));
113
114 if ($row = $ilDB->fetchObject($result))
115 {
116 $this->licenses = $row->licenses;
117 $this->accesses = $row->used;
118 $this->remarks = $row->remarks;
119 }
120 else
121 {
122 $this->licenses = 0;
123 $this->accesses = 0;
124 $this->remarks = '';
125 }
126 }
127
133 function update()
134 {
135 global $ilDB;
136
137 $query = 'SELECT * FROM license_data WHERE obj_id = %s';
138 $result = $ilDB->queryF($query, array('integer'), array($this->obj_id));
139
140 if ($row = $ilDB->fetchObject($result))
141 {
142 $ilDB->update('license_data',
143 array(
144 'licenses' => array('integer', $this->licenses),
145 'used' => array('integer', $this->accesses),
146 'remarks' => array('clob', $this->remarks)
147 ),
148 array(
149 'obj_id' => array('integer', $this->obj_id),
150 )
151 );
152 }
153 else
154 {
155 $ilDB->insert('license_data', array(
156 'obj_id' => array('integer', $this->obj_id),
157 'licenses' => array('integer', $this->licenses),
158 'used' => array('integer', $this->accesses),
159 'remarks' => array('clob', $this->remarks)
160 ));
161 }
162 }
163
169 function delete()
170 {
171 global $ilDB;
172
173 $query = 'DELETE FROM license_data WHERE obj_id = %s';
174 $ilDB->manipulateF($query, array('integer'), array($this->obj_id));
175 }
176
177
179 // Tracking
180
193 function _checkAccess($a_usr_id, $a_obj_id)
194 {
195 // Implementation moved
196 require_once("Services/License/classes/class.ilLicenseAccess.php");
197 return ilLicenseAccess::_checkAccess($a_usr_id, $a_obj_id);
198 }
199
200
209 function _noteAccess($a_obj_id, $a_type, $a_ref_id)
210 {
211 global $ilDB, $ilUser, $ilSetting;
212
213
214 // don't note the access if licensing is globally disabled
215 require_once("Services/License/classes/class.ilLicenseAccess.php");
217 {
218 return;
219 }
220
221 // check if user has already accessed
222 $query = 'SELECT read_count FROM read_event '
223 .'WHERE usr_id = %s AND obj_id = %s';
224 $result = $ilDB->queryF($query,
225 array('integer','integer'),
226 array($ilUser->getId(), $a_obj_id));
227
228 if ($row = $ilDB->fetchObject($result))
229 {
230 // already accessed -> nothing to do
231 return;
232 }
233 else
234 {
235 // note access
236 require_once('Services/Tracking/classes/class.ilChangeEvent.php');
237 ilChangeEvent::_recordReadEvent($a_type, $a_ref_id, $a_obj_id, $ilUser->getId());
238
239 if (self::_isLicensed($a_obj_id))
240 {
241 // increase used licenses
242 $query = "UPDATE license_data SET used = used + 1 "
243 ."WHERE obj_id = %s";
244 $ilDB->manipulateF($query, array('integer'), array($a_obj_id));
245 }
246 }
247 }
248
249
251 // Static Queries
252
260 {
261 global $ilDB;
262 $objects = array();
263
264 $query = 'SELECT od.obj_id, od.type, od.title, od.description, re.ref_id '
265 . 'FROM license_data ld '
266 . 'INNER JOIN object_data od ON od.obj_id = ld.obj_id '
267 . 'INNER JOIN object_reference re ON re.obj_id = od.obj_id '
268 . 'WHERE ld.licenses > 0 '
269 . 'ORDER BY od.title, od.obj_id';
270
271 $result = $ilDB->query($query);
272 $obj_id = 0;
273 while ($row = $ilDB->fetchAssoc($result))
274 {
275 if ($row['obj_id'] != $obj_id)
276 {
277 $objects[] = $row;
278 $obj_id = $row['obj_id'];
279 }
280 }
281 return $objects;
282 }
283
291 function _getLicensedChildObjects($a_ref_id)
292 {
293 global $ilDB, $tree;
294 $objects = array();
295
296 $childs = $tree->getChilds($a_ref_id, 'title');
297 foreach ($childs as $data)
298 {
299 if (in_array($data['type'], array('sahs','htlm'))
300 and self::_isLicensed($data['obj_id']))
301 {
302 $objects[] = $data;
303 }
304 }
305 return $objects;
306 }
307
315 function _isLicensed($a_obj_id)
316 {
317 global $ilDB;
318
319 $query = "SELECT licenses FROM license_data ".
320 "WHERE obj_id = %s ".
321 "AND licenses > 0";
322 $result = $ilDB->queryF($query, array('integer'), array($a_obj_id));
323 if ($row = $ilDB->fetchObject($result))
324 {
325 return true;
326 }
327 }
328}
329
330?>
$result
_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.
_isLicensed($a_obj_id)
Check if an object has licensing activated.
setLicenses($a_licenses=0)
getPotentialAccesses()
Get the number of users who may access the object but don't have yet a license.
_getLicensedObjects()
Get a list of all objects with activated licensing.
_noteAccess($a_obj_id, $a_type, $a_ref_id)
Note the access of the current usr to an object.
_getLicensedChildObjects($a_ref_id)
Get a list of all sub objects with activated licensing.
update()
Update the license data in the database.
_checkAccess($a_usr_id, $a_obj_id)
Check, if a user can access an object by license.
ilLicense($a_obj_id)
Constructor @access public.
read()
Read the license data from the database.
setRemarks($a_remarks='')
static _getOperationIdsByName($operations)
get ops_id's by name.
$data
global $ilSetting
Definition: privfeed.php:40
global $ilDB
global $ilUser
Definition: imgupload.php:15