ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilSkillProfile.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2020 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
15 protected $db;
16
20 protected $lng;
21
25 protected $review;
26
27 protected $id;
28 protected $title;
29 protected $description;
30 protected $ref_id = 0;
31 protected $skill_level = array();
32
38 public function __construct($a_id = 0)
39 {
40 global $DIC;
41
42 $this->db = $DIC->database();
43 $this->lng = $DIC->language();
44 $this->review = $DIC->rbac()->review();
45 if ($a_id > 0) {
46 $this->setId($a_id);
47 $this->read();
48 }
49 }
50
56 public function setId($a_val)
57 {
58 $this->id = $a_val;
59 }
60
66 public function getId()
67 {
68 return $this->id;
69 }
70
76 public function setTitle($a_val)
77 {
78 $this->title = $a_val;
79 }
80
86 public function getTitle()
87 {
88 return $this->title;
89 }
90
96 public function setDescription($a_val)
97 {
98 $this->description = $a_val;
99 }
100
106 public function getDescription()
107 {
108 return $this->description;
109 }
110
114 public function setRefId($a_val)
115 {
116 $this->ref_id = $a_val;
117 }
118
122 public function getRefId()
123 {
124 return $this->ref_id;
125 }
126
133 public function addSkillLevel($a_base_skill_id, $a_tref_id, $a_level_id, $a_order_nr)
134 {
135 //echo "-".$a_base_skill_id."-";
136 $this->skill_level[] = array(
137 "base_skill_id" => $a_base_skill_id,
138 "tref_id" => $a_tref_id,
139 "level_id" => $a_level_id,
140 "order_nr" => $a_order_nr
141 );
142 }
143
150 public function removeSkillLevel($a_base_skill_id, $a_tref_id, $a_level_id, $a_order_nr)
151 {
152 foreach ($this->skill_level as $k => $sl) {
153 if ((int) $sl["base_skill_id"] == (int) $a_base_skill_id &&
154 (int) $sl["tref_id"] == (int) $a_tref_id &&
155 (int) $sl["level_id"] == (int) $a_level_id &&
156 (int) $sl["order_nr"] == (int) $a_order_nr) {
157 unset($this->skill_level[$k]);
158 }
159 }
160 }
161
168 public function getSkillLevels()
169 {
170 usort($this->skill_level, function($level_a, $level_b) {
171 return $level_a['order_nr'] <=> $level_b['order_nr'];
172 });
173
174 return $this->skill_level;
175 }
176
183 public function read()
184 {
186
187 $set = $ilDB->query(
188 "SELECT * FROM skl_profile " .
189 " WHERE id = " . $ilDB->quote($this->getId(), "integer")
190 );
191 $rec = $ilDB->fetchAssoc($set);
192 $this->setTitle($rec["title"]);
193 $this->setDescription($rec["description"]);
194 $this->setRefId($rec["ref_id"]);
195
196 $set = $ilDB->query(
197 "SELECT * FROM skl_profile_level " .
198 " WHERE profile_id = " . $ilDB->quote($this->getId(), "integer")
199 );
200 while ($rec = $ilDB->fetchAssoc($set)) {
201 $this->addSkillLevel(
202 (int) $rec["base_skill_id"],
203 (int) $rec["tref_id"],
204 (int) $rec["level_id"],
205 (int) $rec["order_nr"]
206 );
207 }
208 }
209
213 public function create()
214 {
216
217 // profile
218 $this->setId($ilDB->nextId("skl_profile"));
219 $ilDB->manipulate("INSERT INTO skl_profile " .
220 "(id, title, description, ref_id) VALUES (" .
221 $ilDB->quote($this->getId(), "integer") . "," .
222 $ilDB->quote($this->getTitle(), "text") . "," .
223 $ilDB->quote($this->getDescription(), "text") . "," .
224 $ilDB->quote($this->getRefId(), "integer") .
225 ")");
226
227 // profile levels
228 foreach ($this->skill_level as $level) {
229 $ilDB->replace(
230 "skl_profile_level",
231 array("profile_id" => array("integer", $this->getId()),
232 "tref_id" => array("integer", (int) $level["tref_id"]),
233 "base_skill_id" => array("integer", (int) $level["base_skill_id"])
234 ),
235 array("order_nr" => array("integer", (int) $level["order_nr"]),
236 "level_id" => array("integer", (int) $level["level_id"])
237 )
238 );
239 }
240 }
241
245 public function update()
246 {
248
249 // profile
250 $ilDB->manipulate(
251 "UPDATE skl_profile SET " .
252 " title = " . $ilDB->quote($this->getTitle(), "text") . "," .
253 " description = " . $ilDB->quote($this->getDescription(), "text") .
254 " WHERE id = " . $ilDB->quote($this->getId(), "integer") .
255 " AND ref_id = " . $ilDB->quote($this->getRefId(), "integer")
256 );
257
258 // profile levels
259 $ilDB->manipulate(
260 "DELETE FROM skl_profile_level WHERE " .
261 " profile_id = " . $ilDB->quote($this->getId(), "integer")
262 );
263 foreach ($this->skill_level as $level) {
264 $ilDB->replace(
265 "skl_profile_level",
266 array("profile_id" => array("integer", $this->getId()),
267 "tref_id" => array("integer", (int) $level["tref_id"]),
268 "base_skill_id" => array("integer", (int) $level["base_skill_id"])
269 ),
270 array("order_nr" => array("integer", (int) $level["order_nr"]),
271 "level_id" => array("integer", (int) $level["level_id"])
272 )
273 );
274
275 /*$ilDB->manipulate("INSERT INTO skl_profile_level ".
276 "(profile_id, base_skill_id, tref_id, level_id) VALUES (".
277 $ilDB->quote($this->getId(), "integer").",".
278 $ilDB->quote((int) $level["base_skill_id"], "integer").",".
279 $ilDB->quote((int) $level["tref_id"], "integer").",".
280 $ilDB->quote((int) $level["level_id"], "integer").
281 ")");*/
282 }
283 }
284
288 public function delete()
289 {
291
292 // TODO: Split the deletions when refactoring to repository pattern
293
294 // profile levels
295 $ilDB->manipulate(
296 "DELETE FROM skl_profile_level WHERE " .
297 " profile_id = " . $ilDB->quote($this->getId(), "integer")
298 );
299
300 // profile users
301 $ilDB->manipulate(
302 "DELETE FROM skl_profile_user WHERE " .
303 " profile_id = " . $ilDB->quote($this->getId(), "integer")
304 );
305
306 // profile roles
307 $ilDB->manipulate(
308 "DELETE FROM skl_profile_role WHERE " .
309 " profile_id = " . $ilDB->quote($this->getId(), "integer")
310 );
311
312 // profile
313 $ilDB->manipulate(
314 "DELETE FROM skl_profile WHERE " .
315 " id = " . $ilDB->quote($this->getId(), "integer")
316 );
317 }
318
319 public static function deleteProfilesFromObject(int $a_ref_id)
320 {
321 global $DIC;
322
323 $ilDB = $DIC->database();
324
325 $ilDB->manipulate(
326 "DELETE FROM skl_profile WHERE " .
327 " ref_id = " . $ilDB->quote($a_ref_id, "integer")
328 );
329 }
330
336 public function updateSkillOrder(array $order)
337 {
339
340 asort($order);
341
342 $cnt = 1;
343 foreach ($order as $id => $o) {
344 $id_arr = explode("_", $id);
345 $ilDB->manipulate(
346 "UPDATE skl_profile_level SET " .
347 " order_nr = " . $ilDB->quote(($cnt * 10), "integer") .
348 " WHERE base_skill_id = " . $ilDB->quote($id_arr[0], "integer") .
349 " AND tref_id = " . $ilDB->quote($id_arr[1], "integer") .
350 " AND profile_id = " . $ilDB->quote($this->getId(), "integer")
351 );
352 $cnt++;
353 }
354 }
355
359 public function fixSkillOrderNumbering()
360 {
362
363 $set = $ilDB->query(
364 "SELECT profile_id, base_skill_id, tref_id, order_nr FROM skl_profile_level WHERE " .
365 " profile_id = " . $ilDB->quote($this->getId(), "integer") .
366 " ORDER BY order_nr ASC"
367 );
368 $cnt = 1;
369 while ($rec = $ilDB->fetchAssoc($set)) {
370 $ilDB->manipulate(
371 "UPDATE skl_profile_level SET " .
372 " order_nr = " . $ilDB->quote(($cnt * 10), "integer") .
373 " WHERE profile_id = " . $ilDB->quote($rec["profile_id"], "integer") .
374 " AND base_skill_id = " . $ilDB->quote($rec["base_skill_id"], "integer") .
375 " AND tref_id = " . $ilDB->quote($rec["tref_id"], "integer")
376 );
377 $cnt++;
378 }
379 }
380
386 public function getMaxLevelOrderNr()
387 {
389
390 $set = $ilDB->query(
391 "SELECT MAX(order_nr) mnr FROM skl_profile_level WHERE " .
392 " profile_id = " . $ilDB->quote($this->getId(), "integer")
393 );
394 $rec = $ilDB->fetchAssoc($set);
395 return (int) $rec["mnr"];
396 }
397
404 public static function getProfiles()
405 {
406 global $DIC;
407
408 $ilDB = $DIC->database();
409
410 $set = $ilDB->query(
411 "SELECT * FROM skl_profile " .
412 " ORDER BY title "
413 );
414 $profiles = array();
415 while ($rec = $ilDB->fetchAssoc($set)) {
416 $profiles[$rec["id"]] = $rec;
417 }
418
419 return $profiles;
420 }
421
427 public static function getGlobalProfiles()
428 {
429 global $DIC;
430
431 $ilDB = $DIC->database();
432
433 $set = $ilDB->query(
434 "SELECT * FROM skl_profile " .
435 " WHERE ref_id = 0 " .
436 " ORDER BY title "
437 );
438 $profiles = array();
439 while ($rec = $ilDB->fetchAssoc($set)) {
440 $profiles[$rec["id"]] = $rec;
441 }
442
443 return $profiles;
444 }
445
452 public static function getLocalProfiles(int $a_ref_id)
453 {
454 global $DIC;
455
456 $ilDB = $DIC->database();
457
458 $set = $ilDB->query(
459 "SELECT * FROM skl_profile " .
460 " WHERE ref_id = " . $a_ref_id .
461 " ORDER BY title "
462 );
463 $profiles = array();
464 while ($rec = $ilDB->fetchAssoc($set)) {
465 $profiles[$rec["id"]] = $rec;
466 }
467
468 return $profiles;
469 }
470
477 protected static function lookup($a_id, $a_field)
478 {
479 global $DIC;
480
481 $ilDB = $DIC->database();
482
483 $set = $ilDB->query(
484 "SELECT " . $a_field . " FROM skl_profile " .
485 " WHERE id = " . $ilDB->quote($a_id, "integer")
486 );
487 $rec = $ilDB->fetchAssoc($set);
488 return $rec[$a_field];
489 }
490
497 public static function lookupTitle($a_id)
498 {
499 return self::lookup($a_id, "title");
500 }
501
502 public static function lookupRefId($a_id)
503 {
504 return self::lookup($a_id, "ref_id");
505 }
506
512 public function updateRefIdAfterImport(int $a_new_ref_id)
513 {
515
516 $ilDB->update(
517 "skl_profile",
518 array(
519 "ref_id" => array("integer", $a_new_ref_id)),
520 array(
521 "id" => array("integer", $this->getId()))
522 );
523 }
524
528
532 public function getAssignments()
533 {
534 $assignments = array();
535
536 $users = $this->getAssignedUsers();
537 $roles = $this->getAssignedRoles();
538 $assignments = $users + $roles;
539 ksort($assignments);
540
541 return $assignments;
542 }
543
547 public function getAssignedUsers()
548 {
551
552 $set = $ilDB->query(
553 "SELECT * FROM skl_profile_user " .
554 " WHERE profile_id = " . $ilDB->quote($this->getId(), "integer")
555 );
556 $users = array();
557 while ($rec = $ilDB->fetchAssoc($set)) {
558 $name = ilUserUtil::getNamePresentation($rec["user_id"]);
559 $type = $lng->txt("user");
560 $users[$rec["user_id"]] = array(
561 "type" => $type,
562 "name" => $name,
563 "id" => $rec["user_id"]
564 );
565 }
566 return $users;
567 }
568
574 public function addUserToProfile($a_user_id)
575 {
577
578 $ilDB->replace(
579 "skl_profile_user",
580 array("profile_id" => array("integer", $this->getId()),
581 "user_id" => array("integer", (int) $a_user_id),
582 ),
583 array()
584 );
585 }
586
592 public function removeUserFromProfile($a_user_id)
593 {
595
596 $ilDB->manipulate(
597 "DELETE FROM skl_profile_user WHERE " .
598 " profile_id = " . $ilDB->quote($this->getId(), "integer") .
599 " AND user_id = " . $ilDB->quote($a_user_id, "integer")
600 );
601 }
602
608 public static function removeUserFromAllProfiles($a_user_id)
609 {
610 global $DIC;
611 $ilDB = $DIC->database();
612
613 $ilDB->manipulate(
614 "DELETE FROM skl_profile_user WHERE " .
615 " user_id = " . $ilDB->quote($a_user_id, "integer")
616 );
617 }
618
619
625 public static function getProfilesOfUser($a_user_id)
626 {
627 global $DIC;
628
629 $ilDB = $DIC->database();
630 $rbacreview = $DIC->rbac()->review();
631
632 $all_profiles = array();
633
634 // competence profiles coming from user assignments
635 $user_profiles = array();
636 $set = $ilDB->query(
637 "SELECT p.id, p.title FROM skl_profile_user u JOIN skl_profile p " .
638 " ON (u.profile_id = p.id) " .
639 " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
640 " ORDER BY p.title ASC"
641 );
642 while ($rec = $ilDB->fetchAssoc($set)) {
643 $user_profiles[] = $rec;
644 }
645
646 // competence profiles coming from role assignments
647 $role_profiles = array();
648 $user_roles = $rbacreview->assignedRoles($a_user_id);
649 foreach ($user_roles as $role) {
650 $profiles = self::getGlobalProfilesOfRole($role);
651 foreach ($profiles as $profile) {
652 $role_profiles[] = $profile;
653 }
654 }
655
656 // merge competence profiles and remove multiple occurrences
657 $all_profiles = array_merge($user_profiles, $role_profiles);
658 $temp_profiles = array();
659 foreach ($all_profiles as &$v) {
660 if (!isset($temp_profiles[$v["id"]])) {
661 $temp_profiles[$v["id"]] = &$v;
662 }
663 }
664 $all_profiles = array_values($temp_profiles);
665 return $all_profiles;
666 }
667
671 public static function countUsers($a_profile_id)
672 {
673 global $DIC;
674
675 $ilDB = $DIC->database();
676
677 $set = $ilDB->query(
678 "SELECT count(*) ucnt FROM skl_profile_user " .
679 " WHERE profile_id = " . $ilDB->quote($a_profile_id, "integer")
680 );
681 $rec = $ilDB->fetchAssoc($set);
682 return (int) $rec["ucnt"];
683 }
684
690 public function getAssignedRoles()
691 {
695
696 $set = $ilDB->query(
697 "SELECT * FROM skl_profile_role " .
698 " WHERE profile_id = " . $ilDB->quote($this->getId(), "integer")
699 );
700 $roles = array();
701 while ($rec = $ilDB->fetchAssoc($set)) {
703 $type = $lng->txt("role");
704 // get object of role
705 $obj_id = ilObject::_lookupObjectId($review->getObjectReferenceOfRole($rec["role_id"]));
706 // get title of object if course or group
707 $obj_title = "";
708 $obj_type = "";
709 if (ilObject::_lookupType($obj_id) == "crs" || ilObject::_lookupType($obj_id) == "grp") {
710 $obj_title = ilObject::_lookupTitle($obj_id);
711 $obj_type = ilObject::_lookupType($obj_id);
712 }
713
714 $roles[$rec["role_id"]] = array(
715 "type" => $type,
716 "name" => $name,
717 "id" => $rec["role_id"],
718 "object_title" => $obj_title,
719 "object_type" => $obj_type,
720 "object_id" => $obj_id
721 );
722 }
723
724 return $roles;
725 }
726
732 public function addRoleToProfile(int $a_role_id)
733 {
735
736 $ilDB->replace(
737 "skl_profile_role",
738 array("profile_id" => array("integer", $this->getId()),
739 "role_id" => array("integer", (int) $a_role_id),
740 ),
741 array()
742 );
743 }
744
750 public function removeRoleFromProfile(int $a_role_id)
751 {
753
754 $ilDB->manipulate(
755 "DELETE FROM skl_profile_role WHERE " .
756 " profile_id = " . $ilDB->quote($this->getId(), "integer") .
757 " AND role_id = " . $ilDB->quote($a_role_id, "integer")
758 );
759 }
760
766 public static function removeRoleFromAllProfiles(int $a_role_id)
767 {
768 global $DIC;
769 $ilDB = $DIC->database();
770
771 $ilDB->manipulate(
772 "DELETE FROM skl_profile_role WHERE " .
773 " role_id = " . $ilDB->quote($a_role_id, "integer")
774 );
775 }
776
783 public static function getAllProfilesOfRole(int $a_role_id)
784 {
785 global $DIC;
786
787 $ilDB = $DIC->database();
788
789 $profiles = array();
790 $set = $ilDB->query(
791 "SELECT p.id, p.title FROM skl_profile_role r JOIN skl_profile p " .
792 " ON (r.profile_id = p.id) " .
793 " WHERE r.role_id = " . $ilDB->quote($a_role_id, "integer") .
794 " ORDER BY p.title ASC"
795 );
796 while ($rec = $ilDB->fetchAssoc($set)) {
797 $profiles[] = $rec;
798 }
799 return $profiles;
800 }
801
808 public static function getGlobalProfilesOfRole(int $a_role_id)
809 {
810 global $DIC;
811
812 $ilDB = $DIC->database();
813
814 $profiles = array();
815 $set = $ilDB->query(
816 "SELECT p.id, p.title FROM skl_profile_role r JOIN skl_profile p " .
817 " ON (r.profile_id = p.id) " .
818 " WHERE r.role_id = " . $ilDB->quote($a_role_id, "integer") .
819 " AND p.ref_id = 0" .
820 " ORDER BY p.title ASC"
821 );
822 while ($rec = $ilDB->fetchAssoc($set)) {
823 $profiles[] = $rec;
824 }
825 return $profiles;
826 }
827
835 public static function getLocalProfilesOfRole(int $a_role_id, int $a_ref_id)
836 {
837 global $DIC;
838
839 $ilDB = $DIC->database();
840
841 $profiles = array();
842 $set = $ilDB->query(
843 "SELECT p.id, p.title FROM skl_profile_role r JOIN skl_profile p " .
844 " ON (r.profile_id = p.id) " .
845 " WHERE r.role_id = " . $ilDB->quote($a_role_id, "integer") .
846 " AND p.ref_id = " . $ilDB->quote($a_ref_id, "integer") .
847 " ORDER BY p.title ASC"
848 );
849 while ($rec = $ilDB->fetchAssoc($set)) {
850 $profiles[] = $rec;
851 }
852 return $profiles;
853 }
854
861 public static function countRoles(int $a_profile_id)
862 {
863 global $DIC;
864
865 $ilDB = $DIC->database();
866
867 $set = $ilDB->query(
868 "SELECT count(*) rcnt FROM skl_profile_role " .
869 " WHERE profile_id = " . $ilDB->quote($a_profile_id, "integer")
870 );
871 $rec = $ilDB->fetchAssoc($set);
872 return (int) $rec["rcnt"];
873 }
874
881 public static function getUsageInfo($a_cskill_ids, &$a_usages)
882 {
883 global $DIC;
884
886 $a_cskill_ids,
887 $a_usages,
889 "skl_profile_level",
890 "profile_id",
891 "base_skill_id"
892 );
893 }
894}
An exception for terminatinating execution or to throw for unit testing.
static _getTranslation($a_role_title)
static _lookupTitle($a_id)
lookup object title
static _lookupObjectId($a_ref_id)
lookup object id
static _lookupType($a_id, $a_reference=false)
lookup object type
__construct($a_id=0)
Constructor.
setTitle($a_val)
Set title.
updateRefIdAfterImport(int $a_new_ref_id)
Update the old ref id with the new ref id after import.
getAssignedRoles()
Get assigned roles.
static getProfilesOfUser($a_user_id)
Get profiles of a user.
getSkillLevels()
Get skill levels.
static getGlobalProfilesOfRole(int $a_role_id)
Get global profiles of a role.
static lookupRefId($a_id)
updateSkillOrder(array $order)
Update skill order.
setId($a_val)
Set id.
static removeRoleFromAllProfiles(int $a_role_id)
Remove role from all profiles.
getAssignedUsers()
Get assigned users.
read()
Read skill profile from db.
addSkillLevel($a_base_skill_id, $a_tref_id, $a_level_id, $a_order_nr)
Add skill level.
static lookup($a_id, $a_field)
Lookup.
static getUsageInfo($a_cskill_ids, &$a_usages)
Get usage info.
getAssignments()
Get all assignments (users and roles)
static removeUserFromAllProfiles($a_user_id)
Remove user from all profiles.
static deleteProfilesFromObject(int $a_ref_id)
static countUsers($a_profile_id)
Get assigned users.
update()
Update skill profile.
getMaxLevelOrderNr()
Get maximum order number of levels.
create()
Create skill profile.
static getAllProfilesOfRole(int $a_role_id)
Get global and local profiles of a role.
setDescription($a_val)
Set description.
removeRoleFromProfile(int $a_role_id)
Remove role from profile.
removeSkillLevel($a_base_skill_id, $a_tref_id, $a_level_id, $a_order_nr)
Remove skill level.
static getGlobalProfiles()
Get global profiles.
removeUserFromProfile($a_user_id)
Remove user from profile.
addUserToProfile($a_user_id)
Add user to profile.
static getProfiles()
Get profiles.
addRoleToProfile(int $a_role_id)
Add role to profile.
fixSkillOrderNumbering()
Fix skill order numbering.
static countRoles(int $a_profile_id)
Count assigned roles of a profile.
getDescription()
Get description.
static getLocalProfiles(int $a_ref_id)
Get local profiles of object.
static lookupTitle($a_id)
Lookup title.
static getLocalProfilesOfRole(int $a_role_id, int $a_ref_id)
Get local profiles of a role.
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.
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:
global $DIC
Definition: goto.php:24
Get info on usages of skills.
if($format !==null) $name
Definition: metadata.php:230
$type
global $ilDB