ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSurveySkill.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected ilObjSurvey $survey;
26  protected ilDBInterface $db;
27 
31  protected array $q_skill = array();
32  protected ilLogger $log;
33  protected \ILIAS\Skill\Service\SkillProfileService $skill_profile_service;
34 
35  public function __construct(ilObjSurvey $a_survey)
36  {
37  global $DIC;
38 
39  $this->db = $DIC->database();
40  $this->survey = $a_survey;
41  $this->read();
42  $this->log = ilLoggerFactory::getLogger("svy");
43  $this->skill_profile_service = $DIC->skills()->profile();
44  }
45 
46  public function read(): void
47  {
48  $ilDB = $this->db;
49 
50  $set = $ilDB->query(
51  "SELECT * FROM svy_quest_skill " .
52  " WHERE survey_id = " . $ilDB->quote($this->survey->getId(), "integer")
53  );
54 
55  while ($rec = $ilDB->fetchAssoc($set)) {
56  if (SurveyQuestion::_questionExists($rec["q_id"])) {
57  $this->q_skill[(int) $rec["q_id"]] = array(
58  "q_id" => (int) $rec["q_id"],
59  "base_skill_id" => (int) $rec["base_skill_id"],
60  "tref_id" => (int) $rec["tref_id"]
61  );
62  }
63  }
64  }
65 
71  public function getSkillForQuestion(
72  int $a_question_id
73  ): ?array {
74  return $this->q_skill[$a_question_id] ?? null;
75  }
76 
81  public function getQuestionsForSkill(
82  int $a_base_skill_id,
83  int $a_tref_id
84  ): array {
85  $q_ids = array();
86  foreach ($this->q_skill as $q_id => $s) {
87  if ($s["base_skill_id"] === $a_base_skill_id &&
88  $s["tref_id"] === $a_tref_id) {
89  $q_ids[] = $q_id;
90  }
91  }
92  return $q_ids;
93  }
94 
95 
102  public function addQuestionSkillAssignment(
103  int $a_question_id,
104  int $a_base_skill_id,
105  int $a_tref_id
106  ): void {
107  $ilDB = $this->db;
108 
109  $ilDB->replace(
110  "svy_quest_skill",
111  array("q_id" => array("integer", $a_question_id)),
112  array(
113  "survey_id" => array("integer", $this->survey->getId()),
114  "base_skill_id" => array("integer", $a_base_skill_id),
115  "tref_id" => array("integer", $a_tref_id)
116  )
117  );
118  $this->q_skill[$a_question_id] = array(
119  "q_id" => $a_question_id,
120  "base_skill_id" => $a_base_skill_id,
121  "tref_id" => $a_tref_id
122  );
123 
124  // add usage
125  ilSkillUsage::setUsage($this->survey->getId(), $a_base_skill_id, $a_tref_id);
126  }
127 
129  int $a_question_id
130  ): void {
131  $ilDB = $this->db;
132 
133  // read skills that are assigned to the quesiton
134  $set = $ilDB->query(
135  "SELECT * FROM svy_quest_skill " .
136  " WHERE q_id = " . $ilDB->quote($a_question_id, "integer")
137  );
138  $skills = array();
139  while ($rec = $ilDB->fetchAssoc($set)) {
140  $skills[] = array(
141  "skill_id" => $rec["base_skill_id"],
142  "tref_id" => $rec["tref_id"]
143  );
144  }
145 
146  // remove assignment of question
147  $ilDB->manipulate(
148  "DELETE FROM svy_quest_skill WHERE " .
149  " q_id = " . $ilDB->quote($a_question_id, "integer")
150  );
151  unset($this->q_skill[$a_question_id]);
152 
153  $this->removeUsagesOfSkills($skills);
154  }
155 
159  public static function handleQuestionDeletion(
160  int $a_question_id,
161  int $a_obj_id
162  ): void {
163 
164  $svy_log = ilLoggerFactory::getLogger("svy");
165 
166  $svy_log->debug("delete skill assignment, obj id " . $a_obj_id .
167  ", obj type: " . ilObject::_lookupType($a_obj_id));
168 
169  if (ilObject::_lookupType($a_obj_id) === "svy" &&
170  ilObject::_exists($a_obj_id)) {
171  $svy_log->debug("call removeQuestionSkillAssignment.");
172  // mantis 11691
173  $svy = new ilObjSurvey($a_obj_id, false);
174  $svy_skill = new ilSurveySkill($svy);
175  $svy_skill->removeQuestionSkillAssignment($a_question_id);
176  }
177  }
178 
184  public function removeUsagesOfSkills(
185  array $a_skills
186  ): void {
187  $used_skills = array();
188  foreach ($a_skills as $skill) {
189  if ($this->isSkillAssignedToQuestion($skill["skill_id"], $skill["tref_id"])) {
190  $used_skills[] = $skill["skill_id"] . ":" . $skill["tref_id"];
191  }
192  }
193  reset($a_skills);
194 
195  // now remove all usages that have been confirmed
196  foreach ($a_skills as $skill) {
197  if (!in_array($skill["skill_id"] . ":" . $skill["tref_id"], $used_skills, true)) {
198  ilSkillUsage::setUsage($this->survey->getId(), $skill["skill_id"], $skill["tref_id"], false);
199  }
200  }
201  }
202 
203  public function isSkillAssignedToQuestion(
204  int $a_skill_id,
205  int $a_tref_id
206  ): bool {
207  $ilDB = $this->db;
208 
209  $set = $ilDB->query(
210  "SELECT * FROM svy_quest_skill " .
211  " WHERE base_skill_id = " . $ilDB->quote($a_skill_id, "integer") .
212  " AND tref_id = " . $ilDB->quote($a_tref_id, "integer") .
213  " AND survey_id = " . $ilDB->quote($this->survey->getId(), "integer")
214  );
215  if ($rec = $ilDB->fetchAssoc($set)) {
216  return true;
217  }
218  return false;
219  }
220 
221 
225  public function getAllAssignedSkillsAsOptions(): array
226  {
227  $skills = array();
228  foreach ($this->q_skill as $sk) {
229  $skills[$sk["base_skill_id"] . ":" . $sk["tref_id"]] = ilBasicSkill::_lookupTitle($sk["base_skill_id"]);
230  }
231  return $skills;
232  }
233 
240  int $a_appraisee_id,
241  bool $a_self_eval = false,
242  int $finished_id = 0
243  ): array {
244  $skills = array();
245 
246  // get all skills
247  $opts = $this->getAllAssignedSkillsAsOptions();
248  foreach ($opts as $k => $title) {
249  $k = explode(":", $k);
250 
251  $bs = new ilBasicSkill((int) $k[0]);
252  $ld = $bs->getLevelData();
253 
254  $skills[] = array(
255  "base_skill_id" => (int) $k[0],
256  "tref_id" => (int) $k[1],
257  "skill_title" => $title,
258  "level_data" => $ld
259  );
260  }
261 
262  $finished_ids = [];
263  if (!$a_self_eval) {
264  if ($finished_id > 0) {
265  $finished_ids = array($finished_id);
266  } else {
267  $finished_ids = $this->survey->getFinishedIdsForAppraiseeId($a_appraisee_id, true);
268  }
269  } else {
270  $finished_id = $this->survey->getFinishedIdForAppraiseeIdAndRaterId($a_appraisee_id, $a_appraisee_id);
271  if ($finished_id > 0) {
272  $finished_ids = array($finished_id);
273  }
274  }
275 
276  /* ???
277  if (!is_array($finished_ids)) {
278  $finished_ids = array(-1);
279  }*/
280 
281  $results = $this->survey->getUserSpecificResults($finished_ids);
282  $this->log->debug("Finished IDS: " . print_r($finished_ids, true));
283  foreach ($skills as $k => $s) {
284  $q_ids = $this->getQuestionsForSkill($s["base_skill_id"], $s["tref_id"]);
285  $this->log->debug("Skill: " . $s["base_skill_id"] . ":" . $s["tref_id"] . ", Questions: " . implode(",", $q_ids));
286  $mean_sum = 0;
287  foreach ($q_ids as $q_id) {
288  $qmean = 0;
289  if (is_array($results[$q_id])) {
290  $cnt = 0;
291  $sum = 0;
292  foreach ($results[$q_id] as $uid => $answer) { // answer of user $uid for question $q_id
293  // $answer has the scale values as keys and the answer texts as values.
294  // In case of single choice this is an array with one key => value pair.
295  // For multiple choice questions (currently not supported for being used for competences)
296  // multiple elements may be in the array (in the future).
297  $scale_values = array_keys($answer); // scale values of the answer
298  $this->log->debug("User answer (scale values): " . print_r($scale_values, true));
299  $sum += array_sum($scale_values);
300  $cnt += count($scale_values); // nr of answers (always one in the case of single choice)
301  }
302  if ($cnt > 0) {
303  $qmean = $sum / $cnt;
304  }
305  $this->log->debug("MEAN: " . $qmean);
306  }
307  $mean_sum += $qmean;
308  $this->log->debug("MEAN SUM: " . $mean_sum);
309  }
310  $skills[$k]["mean_sum"] = $mean_sum;
311 
312  $skthr = new ilSurveySkillThresholds($this->survey);
313  $thresholds = $skthr->getThresholds();
314  $previous = 0;
315  $previous_t = 0;
316  foreach ($skills[$k]["level_data"] as $l) {
317  $t = $thresholds[$l["id"]][$s["tref_id"]] ?? 0;
318  if ($t > 0 && $mean_sum >= $t) {
319  $skills[$k]["new_level"] = $l["title"];
320  $skills[$k]["new_level_id"] = $l["id"];
321  $skills[$k]["next_level_perc"] = 0;
322  } elseif ($t > 0 && $mean_sum < $t) {
323  // first unfulfilled level
324  if ($previous == ($skills[$k]["new_level_id"] ?? null) && !isset($skills[$k]["next_level_perc"])) {
325  $skills[$k]["next_level_perc"] = 1 / ($t - $previous_t) * ($mean_sum - $previous_t);
326  }
327  }
328  if ($t > 0) {
329  $previous = $l["id"];
330  $previous_t = $t;
331  }
332  }
333  }
334  return $skills;
335  }
336 
337  public function determineMaxScale(
338  int $a_base_skill,
339  int $a_tref_id = 0
340  ): int {
341  $ssk = new ilSurveySkill($this->survey);
342  $question_ids = $ssk->getQuestionsForSkill($a_base_skill, $a_tref_id);
343  $scale_sum = 0;
344  foreach ($question_ids as $q_id) {
346  if (!is_object($q)) {
347  continue;
348  }
349  $cats = $q->getCategories();
350  $max_scale = 0;
351  for ($i = 0; $i < $cats->getCategoryCount(); $i++) {
352  $c = $cats->getCategory($i);
353  $n = $c->neutral;
354  $s = $c->scale;
355  if (!$c->neutral) {
356  if ($c->scale > $max_scale) {
357  $max_scale = $c->scale;
358  }
359  }
360  }
361  $scale_sum += $max_scale;
362  }
363 
364  return $scale_sum;
365  }
366 
370  public function writeAndAddAppraiseeSkills(
371  int $user_id
372  ): void {
373  // write raters evaluation
374  $new_levels = $this->determineSkillLevelsForAppraisee($user_id);
375  foreach ($new_levels as $nl) {
376  if (($nl["new_level_id"] ?? 0) > 0) {
378  (int) $nl["new_level_id"],
379  $user_id,
380  $this->survey->getRefId(),
381  (int) $nl["tref_id"],
383  true,
384  false,
385  "",
386  $nl["next_level_perc"]
387  );
388 
389  if (($nl["tref_id"] ?? 0) > 0) {
390  ilPersonalSkill::addPersonalSkill($user_id, (int) $nl["tref_id"]);
391  } else {
392  ilPersonalSkill::addPersonalSkill($user_id, (int) $nl["base_skill_id"]);
393  }
394  }
395  }
396 
397  //write profile completion entries if fulfilment status has changed
398  $this->skill_profile_service->writeCompletionEntryForAllProfiles($user_id);
399 
400  // write self evaluation
401  $this->writeAndAddSelfEvalSkills($user_id);
402  }
403 
405  int $finished_id,
406  int $appr_id,
407  string $rater_id
408  ): void {
409  $new_levels = $this->determineSkillLevelsForAppraisee($appr_id, false, $finished_id);
410  foreach ($new_levels as $nl) {
411  if (($nl["new_level_id"] ?? 0) > 0) {
413  (int) $nl["new_level_id"],
414  $appr_id,
415  $this->survey->getRefId(),
416  (int) $nl["tref_id"],
418  true,
419  false,
420  "",
421  $nl["next_level_perc"],
422  (int) $rater_id
423  );
424 
425  if (($nl["tref_id"] ?? 0) > 0) {
426  ilPersonalSkill::addPersonalSkill($appr_id, (int) $nl["tref_id"]);
427  } else {
428  ilPersonalSkill::addPersonalSkill($appr_id, (int) $nl["base_skill_id"]);
429  }
430  }
431  }
432  }
433 
437  public function writeAndAddSelfEvalSkills(
438  int $user_id
439  ): void {
440  if ($user_id > 0 && in_array($this->survey->getMode(), [ilObjSurvey::MODE_SELF_EVAL, ilObjSurvey::MODE_360], true)) {
441  $new_levels = $this->determineSkillLevelsForAppraisee($user_id, true);
442  foreach ($new_levels as $nl) {
443  if (($nl["new_level_id"] ?? 0) > 0) {
445  (int) $nl["new_level_id"],
446  $user_id,
447  $this->survey->getRefId(),
448  (int) $nl["tref_id"],
450  true,
451  1,
452  "",
453  $nl["next_level_perc"]
454  );
455 
456  if (($nl["tref_id"] ?? 0) > 0) {
457  ilPersonalSkill::addPersonalSkill($user_id, (int) $nl["tref_id"]);
458  } else {
459  ilPersonalSkill::addPersonalSkill($user_id, (int) $nl["base_skill_id"]);
460  }
461  }
462  }
463  }
464  }
465 }
$c
Definition: cli.php:38
static getLogger(string $a_component_id)
Get component logger.
writeAndAddAppraiseeSkills(int $user_id)
Write appraisee skills and add them to user&#39;s competence records.
addQuestionSkillAssignment(int $a_question_id, int $a_base_skill_id, int $a_tref_id)
Add survey question to skill assignment.
static _lookupTitle(int $a_obj_id, int $a_tref_id=0)
__construct(ilObjSurvey $a_survey)
static setUsage(int $a_obj_id, int $a_skill_id, int $a_tref_id, bool $a_use=true)
determineSkillLevelsForAppraisee(int $a_appraisee_id, bool $a_self_eval=false, int $finished_id=0)
Determine skill levels for appraisee.
writeAndAddIndFeedbackSkills(int $finished_id, int $appr_id, string $rater_id)
static handleQuestionDeletion(int $a_question_id, int $a_obj_id)
Remove question skill assignment.
writeAndAddSelfEvalSkills(int $user_id)
Write skills on self evaluation and add them to user&#39;s competence records.
getSkillForQuestion(int $a_question_id)
Get skill for question.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
determineMaxScale(int $a_base_skill, int $a_tref_id=0)
removeQuestionSkillAssignment(int $a_question_id)
static writeUserSkillLevelStatus(int $a_level_id, int $a_user_id, int $a_trigger_ref_id, int $a_tref_id=0, int $a_status=ilBasicSkill::ACHIEVED, bool $a_force=false, bool $a_self_eval=false, string $a_unique_identifier="", float $a_next_level_fulfilment=0.0, string $trigger_user_id="")
$results
ilDBInterface $db
getQuestionsForSkill(int $a_base_skill_id, int $a_tref_id)
Get questions for skill.
removeUsagesOfSkills(array $a_skills)
Remove usages of skills This function checks, if the skills are really not in use anymore...
static addPersonalSkill(int $a_user_id, int $a_skill_node_id)
isSkillAssignedToQuestion(int $a_skill_id, int $a_tref_id)
ILIAS Skill Service SkillProfileService $skill_profile_service
static _lookupType(int $id, bool $reference=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _instanciateQuestion(int $question_id)
Get question object.
static _questionExists(int $question_id)
$i
Definition: metadata.php:41