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