ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSkillProfile.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once("./Services/Skill/interfaces/interface.ilSkillUsageInfo.php");
6 
15 {
19  protected $db;
20 
24  protected $lng;
25 
29  protected $review;
30 
31  protected $id;
32  protected $title;
33  protected $description;
34  protected $skill_level = array();
35 
41  public function __construct($a_id = 0)
42  {
43  global $DIC;
44 
45  $this->db = $DIC->database();
46  $this->lng = $DIC->language();
47  $this->review = $DIC->rbac()->review();
48  if ($a_id > 0) {
49  $this->setId($a_id);
50  $this->read();
51  }
52  }
53 
59  public function setId($a_val)
60  {
61  $this->id = $a_val;
62  }
63 
69  public function getId()
70  {
71  return $this->id;
72  }
73 
79  public function setTitle($a_val)
80  {
81  $this->title = $a_val;
82  }
83 
89  public function getTitle()
90  {
91  return $this->title;
92  }
93 
99  public function setDescription($a_val)
100  {
101  $this->description = $a_val;
102  }
103 
109  public function getDescription()
110  {
111  return $this->description;
112  }
113 
120  public function addSkillLevel($a_base_skill_id, $a_tref_id, $a_level_id)
121  {
122  //echo "-".$a_base_skill_id."-";
123  $this->skill_level[] = array(
124  "base_skill_id" => $a_base_skill_id,
125  "tref_id" => $a_tref_id,
126  "level_id" => $a_level_id
127  );
128  }
129 
136  public function removeSkillLevel($a_base_skill_id, $a_tref_id, $a_level_id)
137  {
138  foreach ($this->skill_level as $k => $sl) {
139  if ((int) $sl["base_skill_id"] == (int) $a_base_skill_id &&
140  (int) $sl["tref_id"] == (int) $a_tref_id &&
141  (int) $sl["level_id"] == (int) $a_level_id) {
142  unset($this->skill_level[$k]);
143  }
144  }
145  }
146 
153  public function getSkillLevels()
154  {
155  return $this->skill_level;
156  }
157 
164  public function read()
165  {
166  $ilDB = $this->db;
167 
168  $set = $ilDB->query(
169  "SELECT * FROM skl_profile " .
170  " WHERE id = " . $ilDB->quote($this->getId(), "integer")
171  );
172  $rec = $ilDB->fetchAssoc($set);
173  $this->setTitle($rec["title"]);
174  $this->setDescription($rec["description"]);
175 
176  $set = $ilDB->query(
177  "SELECT * FROM skl_profile_level " .
178  " WHERE profile_id = " . $ilDB->quote($this->getId(), "integer")
179  );
180  while ($rec = $ilDB->fetchAssoc($set)) {
181  $this->addSkillLevel(
182  (int) $rec["base_skill_id"],
183  (int) $rec["tref_id"],
184  (int) $rec["level_id"]
185  );
186  }
187  }
188 
192  public function create()
193  {
194  $ilDB = $this->db;
195 
196  // profile
197  $this->setId($ilDB->nextId("skl_profile"));
198  $ilDB->manipulate("INSERT INTO skl_profile " .
199  "(id, title, description) VALUES (" .
200  $ilDB->quote($this->getId(), "integer") . "," .
201  $ilDB->quote($this->getTitle(), "text") . "," .
202  $ilDB->quote($this->getDescription(), "text") .
203  ")");
204 
205  // profile levels
206  foreach ($this->skill_level as $level) {
207  $ilDB->replace(
208  "skl_profile_level",
209  array("profile_id" => array("integer", $this->getId()),
210  "tref_id" => array("integer", (int) $level["tref_id"]),
211  "base_skill_id" => array("integer", (int) $level["base_skill_id"])
212  ),
213  array("level_id" => array("integer", (int) $level["level_id"]))
214  );
215  }
216  }
217 
221  public function update()
222  {
223  $ilDB = $this->db;
224 
225  // profile
226  $ilDB->manipulate(
227  "UPDATE skl_profile SET " .
228  " title = " . $ilDB->quote($this->getTitle(), "text") . "," .
229  " description = " . $ilDB->quote($this->getDescription(), "text") .
230  " WHERE id = " . $ilDB->quote($this->getId(), "integer")
231  );
232 
233  // profile levels
234  $ilDB->manipulate(
235  "DELETE FROM skl_profile_level WHERE " .
236  " profile_id = " . $ilDB->quote($this->getId(), "integer")
237  );
238  foreach ($this->skill_level as $level) {
239  $ilDB->replace(
240  "skl_profile_level",
241  array("profile_id" => array("integer", $this->getId()),
242  "tref_id" => array("integer", (int) $level["tref_id"]),
243  "base_skill_id" => array("integer", (int) $level["base_skill_id"])
244  ),
245  array("level_id" => array("integer", (int) $level["level_id"]))
246  );
247 
248  /*$ilDB->manipulate("INSERT INTO skl_profile_level ".
249  "(profile_id, base_skill_id, tref_id, level_id) VALUES (".
250  $ilDB->quote($this->getId(), "integer").",".
251  $ilDB->quote((int) $level["base_skill_id"], "integer").",".
252  $ilDB->quote((int) $level["tref_id"], "integer").",".
253  $ilDB->quote((int) $level["level_id"], "integer").
254  ")");*/
255  }
256  }
257 
261  public function delete()
262  {
263  $ilDB = $this->db;
264 
265  // TODO: Split the deletions when refactoring to repository pattern
266 
267  // profile levels
268  $ilDB->manipulate(
269  "DELETE FROM skl_profile_level WHERE " .
270  " profile_id = " . $ilDB->quote($this->getId(), "integer")
271  );
272 
273  // profile users
274  $ilDB->manipulate(
275  "DELETE FROM skl_profile_user WHERE " .
276  " profile_id = " . $ilDB->quote($this->getId(), "integer")
277  );
278 
279  // profile roles
280  $ilDB->manipulate(
281  "DELETE FROM skl_profile_role WHERE " .
282  " profile_id = " . $ilDB->quote($this->getId(), "integer")
283  );
284 
285  // profile
286  $ilDB->manipulate(
287  "DELETE FROM skl_profile WHERE " .
288  " id = " . $ilDB->quote($this->getId(), "integer")
289  );
290  }
291 
298  public static function getProfiles()
299  {
300  global $DIC;
301 
302  $ilDB = $DIC->database();
303 
304  $set = $ilDB->query(
305  "SELECT * FROM skl_profile " .
306  " ORDER BY title "
307  );
308  $profiles = array();
309  while ($rec = $ilDB->fetchAssoc($set)) {
310  $profiles[$rec["id"]] = $rec;
311  }
312 
313  return $profiles;
314  }
315 
322  protected static function lookup($a_id, $a_field)
323  {
324  global $DIC;
325 
326  $ilDB = $DIC->database();
327 
328  $set = $ilDB->query(
329  "SELECT " . $a_field . " FROM skl_profile " .
330  " WHERE id = " . $ilDB->quote($a_id, "integer")
331  );
332  $rec = $ilDB->fetchAssoc($set);
333  return $rec[$a_field];
334  }
335 
342  public static function lookupTitle($a_id)
343  {
344  return self::lookup($a_id, "title");
345  }
346 
350 
354  public function getAssignments()
355  {
356  $assignments = array();
357 
358  $users = $this->getAssignedUsers();
359  $roles = $this->getAssignedRoles();
360  $assignments = $users + $roles;
361  ksort($assignments);
362 
363  return $assignments;
364  }
365 
369  public function getAssignedUsers()
370  {
371  $ilDB = $this->db;
372  $lng = $this->lng;
373 
374  $set = $ilDB->query(
375  "SELECT * FROM skl_profile_user " .
376  " WHERE profile_id = " . $ilDB->quote($this->getId(), "integer")
377  );
378  $users = array();
379  while ($rec = $ilDB->fetchAssoc($set)) {
380  $name = ilUserUtil::getNamePresentation($rec["user_id"]);
381  $type = $lng->txt("user");
382  $users[$rec["user_id"]] = array(
383  "type" => $type,
384  "name" => $name,
385  "id" => $rec["user_id"]
386  );
387  }
388  return $users;
389  }
390 
396  public function addUserToProfile($a_user_id)
397  {
398  $ilDB = $this->db;
399 
400  $ilDB->replace(
401  "skl_profile_user",
402  array("profile_id" => array("integer", $this->getId()),
403  "user_id" => array("integer", (int) $a_user_id),
404  ),
405  array()
406  );
407  }
408 
414  public function removeUserFromProfile($a_user_id)
415  {
416  $ilDB = $this->db;
417 
418  $ilDB->manipulate(
419  "DELETE FROM skl_profile_user WHERE " .
420  " profile_id = " . $ilDB->quote($this->getId(), "integer") .
421  " AND user_id = " . $ilDB->quote($a_user_id, "integer")
422  );
423  }
424 
430  public static function removeUserFromAllProfiles($a_user_id)
431  {
432  global $DIC;
433  $ilDB = $DIC->database();
434 
435  $ilDB->manipulate(
436  "DELETE FROM skl_profile_user WHERE " .
437  " user_id = " . $ilDB->quote($a_user_id, "integer")
438  );
439  }
440 
441 
447  public static function getProfilesOfUser($a_user_id)
448  {
449  global $DIC;
450 
451  $ilDB = $DIC->database();
452  $rbacreview = $DIC->rbac()->review();
453 
454  $all_profiles = array();
455 
456  // competence profiles coming from user assignments
457  $user_profiles = array();
458  $set = $ilDB->query(
459  "SELECT p.id, p.title FROM skl_profile_user u JOIN skl_profile p " .
460  " ON (u.profile_id = p.id) " .
461  " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
462  " ORDER BY p.title ASC"
463  );
464  while ($rec = $ilDB->fetchAssoc($set)) {
465  $user_profiles[] = $rec;
466  }
467 
468  // competence profiles coming from role assignments
469  $role_profiles = array();
470  $user_roles = $rbacreview->assignedRoles($a_user_id);
471  foreach ($user_roles as $role) {
472  $profiles = self::getProfilesOfRole($role);
473  foreach ($profiles as $profile) {
474  $role_profiles[] = $profile;
475  }
476  }
477 
478  // merge competence profiles and remove multiple occurrences
479  $all_profiles = array_merge($user_profiles, $role_profiles);
480  $temp_profiles = array();
481  foreach ($all_profiles as &$v) {
482  if (!isset($temp_profiles[$v["id"]])) {
483  $temp_profiles[$v["id"]] = &$v;
484  }
485  }
486  $all_profiles = array_values($temp_profiles);
487  return $all_profiles;
488  }
489 
493  public static function countUsers($a_profile_id)
494  {
495  global $DIC;
496 
497  $ilDB = $DIC->database();
498 
499  $set = $ilDB->query(
500  "SELECT count(*) ucnt FROM skl_profile_user " .
501  " WHERE profile_id = " . $ilDB->quote($a_profile_id, "integer")
502  );
503  $rec = $ilDB->fetchAssoc($set);
504  return (int) $rec["ucnt"];
505  }
506 
512  public function getAssignedRoles()
513  {
514  $ilDB = $this->db;
515  $lng = $this->lng;
517 
518  $set = $ilDB->query(
519  "SELECT * FROM skl_profile_role " .
520  " WHERE profile_id = " . $ilDB->quote($this->getId(), "integer")
521  );
522  $roles = array();
523  while ($rec = $ilDB->fetchAssoc($set)) {
525  $type = $lng->txt("role");
526  // get object of role
527  $obj = ilObject::_lookupObjectId($review->getObjectReferenceOfRole($rec["role_id"]));
528  // get title of object if course or group
529  if (ilObject::_lookupType($obj) == "crs" || ilObject::_lookupType($obj) == "grp") {
530  $obj_title = ilObject::_lookupTitle($obj);
531  }
532 
533  $roles[$rec["role_id"]] = array(
534  "type" => $type,
535  "name" => $name,
536  "id" => $rec["role_id"],
537  "object" => $obj_title
538  );
539  }
540 
541  return $roles;
542  }
543 
549  public function addRoleToProfile(int $a_role_id)
550  {
551  $ilDB = $this->db;
552 
553  $ilDB->replace(
554  "skl_profile_role",
555  array("profile_id" => array("integer", $this->getId()),
556  "role_id" => array("integer", (int) $a_role_id),
557  ),
558  array()
559  );
560  }
561 
567  public function removeRoleFromProfile(int $a_role_id)
568  {
569  $ilDB = $this->db;
570 
571  $ilDB->manipulate(
572  "DELETE FROM skl_profile_role WHERE " .
573  " profile_id = " . $ilDB->quote($this->getId(), "integer") .
574  " AND role_id = " . $ilDB->quote($a_role_id, "integer")
575  );
576  }
577 
583  public static function removeRoleFromAllProfiles(int $a_role_id)
584  {
585  global $DIC;
586  $ilDB = $DIC->database();
587 
588  $ilDB->manipulate(
589  "DELETE FROM skl_profile_role WHERE " .
590  " role_id = " . $ilDB->quote($a_role_id, "integer")
591  );
592  }
593 
600  public static function getProfilesOfRole(int $a_role_id)
601  {
602  global $DIC;
603 
604  $ilDB = $DIC->database();
605 
606  $profiles = array();
607  $set = $ilDB->query(
608  "SELECT p.id, p.title FROM skl_profile_role r JOIN skl_profile p " .
609  " ON (r.profile_id = p.id) " .
610  " WHERE role_id = " . $ilDB->quote($a_role_id, "integer") .
611  " ORDER BY p.title ASC"
612  );
613  while ($rec = $ilDB->fetchAssoc($set)) {
614  $profiles[] = $rec;
615  }
616  return $profiles;
617  }
618 
625  public static function countRoles(int $a_profile_id)
626  {
627  global $DIC;
628 
629  $ilDB = $DIC->database();
630 
631  $set = $ilDB->query(
632  "SELECT count(*) rcnt FROM skl_profile_role " .
633  " WHERE profile_id = " . $ilDB->quote($a_profile_id, "integer")
634  );
635  $rec = $ilDB->fetchAssoc($set);
636  return (int) $rec["rcnt"];
637  }
638 
645  public static function getUsageInfo($a_cskill_ids, &$a_usages)
646  {
647  global $DIC;
648 
649  $ilDB = $DIC->database();
650 
651  include_once("./Services/Skill/classes/class.ilSkillUsage.php");
653  $a_cskill_ids,
654  $a_usages,
656  "skl_profile_level",
657  "profile_id",
658  "base_skill_id"
659  );
660  }
661 }
Get info on usages of skills.
static removeRoleFromAllProfiles(int $a_role_id)
Remove role from all profiles.
static getProfiles()
Get profiles.
$type
removeRoleFromProfile(int $a_role_id)
Remove role from profile.
static removeUserFromAllProfiles($a_user_id)
Remove user from all profiles.
setTitle($a_val)
Set title.
static _lookupTitle($a_id)
lookup object title
setId($a_val)
Set id.
static getUsageInfoGeneric( $a_cskill_ids, &$a_usages, $a_usage_type, $a_table, $a_key_field, $a_skill_field="skill_id", $a_tref_field="tref_id")
Get standard usage query.
addUserToProfile($a_user_id)
Add user to profile.
update()
Update skill profile.
static _lookupObjectId($a_ref_id)
lookup object id
getSkillLevels()
Get skill levels.
if($format !==null) $name
Definition: metadata.php:230
static getProfilesOfUser($a_user_id)
Get profiles of a user.
getDescription()
Get description.
__construct($a_id=0)
Constructor.
static getProfilesOfRole(int $a_role_id)
Get profiles of a role.
static getNamePresentation( $a_user_id, $a_user_image=false, $a_profile_link=false, $a_profile_back_link="", $a_force_first_lastname=false, $a_omit_login=false, $a_sortable=true, $a_return_data_array=false, $a_ctrl_path="ilpublicuserprofilegui")
Default behaviour is:
getAssignedRoles()
Get assigned roles.
static _getTranslation($a_role_title)
static _lookupType($a_id, $a_reference=false)
lookup object type
setDescription($a_val)
Set description.
static getUsageInfo($a_cskill_ids, &$a_usages)
Get usage info.
read()
Read skill profile from db.
removeSkillLevel($a_base_skill_id, $a_tref_id, $a_level_id)
Remove skill level.
static countUsers($a_profile_id)
Get assigned users.
global $ilDB
$DIC
Definition: xapitoken.php:46
getAssignments()
Get all assignments (users and roles)
addSkillLevel($a_base_skill_id, $a_tref_id, $a_level_id)
Add skill level.
create()
Create skill profile.
static lookup($a_id, $a_field)
Lookup.
static countRoles(int $a_profile_id)
Count assigned roles of a profile.
static lookupTitle($a_id)
Lookup title.
getAssignedUsers()
Get assigned users.
addRoleToProfile(int $a_role_id)
Add role to profile.
removeUserFromProfile($a_user_id)
Remove user from profile.