ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilAssQuestionSkillAssignmentList.php
Go to the documentation of this file.
1 <?php
2 
26 {
30  private $db;
31 
35  private $parentObjId;
36 
40  private $assignments;
41 
46 
51 
56 
57  public function __construct(ilDBInterface $db)
58  {
59  $this->db = $db;
60 
61  $this->parentObjId = null;
62  $this->assignments = [];
63  $this->numAssignsBySkill = [];
64  $this->maxPointsBySkill = [];
65  $this->questionIdFilter = null;
66  }
67 
71  public function setParentObjId($parentObjId): void
72  {
73  $this->parentObjId = $parentObjId;
74  }
75 
79  public function getParentObjId(): ?int
80  {
81  return $this->parentObjId;
82  }
83 
87  public function getQuestionIdFilter(): ?int
88  {
90  }
91 
95  public function setQuestionIdFilter($questionIdFilter): void
96  {
97  $this->questionIdFilter = $questionIdFilter;
98  }
99 
100  public function reset(): void
101  {
102  $this->assignments = [];
103  $this->numAssignsBySkill = [];
104  $this->maxPointsBySkill = [];
105  }
106 
107  public function addAssignment(ilAssQuestionSkillAssignment $assignment): void
108  {
109  if (!isset($this->assignments[$assignment->getQuestionId()])) {
110  $this->assignments[$assignment->getQuestionId()] = [];
111  }
112 
113  $this->assignments[$assignment->getQuestionId()][] = $assignment;
114  }
115 
116  private function incrementNumAssignsBySkill(ilAssQuestionSkillAssignment $assignment): void
117  {
118  $key = $this->buildSkillKey($assignment->getSkillBaseId(), $assignment->getSkillTrefId());
119 
120  if (!isset($this->numAssignsBySkill[$key])) {
121  $this->numAssignsBySkill[$key] = 0;
122  }
123 
124  $this->numAssignsBySkill[$key]++;
125  }
126 
127  private function incrementMaxPointsBySkill(ilAssQuestionSkillAssignment $assignment): void
128  {
129  $key = $this->buildSkillKey($assignment->getSkillBaseId(), $assignment->getSkillTrefId());
130 
131  if (!isset($this->maxPointsBySkill[$key])) {
132  $this->maxPointsBySkill[$key] = 0;
133  }
134 
135  $this->maxPointsBySkill[$key] += $assignment->getMaxSkillPoints();
136  }
137 
138  public function loadFromDb(): void
139  {
140  $this->reset();
141 
142  $res = $this->db->query("
143  SELECT obj_fi, question_fi, skill_base_fi, skill_tref_fi, skill_points, eval_mode
144  FROM qpl_qst_skl_assigns
145  WHERE {$this->getWhereConditions()}
146  ");
147 
148  while ($row = $this->db->fetchAssoc($res)) {
149  $assignment = $this->buildSkillQuestionAssignmentByArray($row);
150 
151  if ($assignment->hasEvalModeBySolution()) {
152  $assignment->loadComparisonExpressions(); // db query
153  }
154 
155  $this->addAssignment($assignment);
156  $this->incrementNumAssignsBySkill($assignment);
157  $this->incrementMaxPointsBySkill($assignment);
158  }
159  }
160 
161  private function getWhereConditions(): string
162  {
163  $conditions = [
164  'obj_fi = ' . $this->db->quote($this->getParentObjId(), 'integer')
165  ];
166 
167  if ($this->getQuestionIdFilter()) {
168  $conditions[] = 'question_fi = ' . $this->db->quote($this->getQuestionIdFilter(), 'integer');
169  }
170 
171  return implode(' AND ', $conditions);
172  }
173 
179  {
180  $assignment = new ilAssQuestionSkillAssignment($this->db);
181 
182  $assignment->setParentObjId($data['obj_fi']);
183  $assignment->setQuestionId($data['question_fi']);
184  $assignment->setSkillBaseId($data['skill_base_fi']);
185  $assignment->setSkillTrefId($data['skill_tref_fi']);
186  $assignment->setSkillPoints($data['skill_points']);
187  $assignment->setEvalMode($data['eval_mode']);
188 
189  return $assignment;
190  }
191 
192  private function buildSkillKey($skillBaseId, $skillTrefId): string
193  {
194  return $skillBaseId . ':' . $skillTrefId;
195  }
196 
197  public function loadAdditionalSkillData(): void
198  {
199  foreach ($this->assignments as $assignmentsByQuestion) {
200  foreach ($assignmentsByQuestion as $assignment) {
201  $assignment->loadAdditionalSkillData();
202  }
203  }
204  }
205 
210  public function getAssignmentsByQuestionId($questionId): array
211  {
212  if (!isset($this->assignments[$questionId])) {
213  return [];
214  }
215 
216  return $this->assignments[$questionId];
217  }
218 
219  public function isAssignedToQuestionId($skillBaseId, $skillTrefId, $questionId): bool
220  {
221  if (!isset($this->assignments[$questionId])) {
222  return false;
223  }
224 
225  foreach ($this->assignments[$questionId] as $assignment) {
226  if ($assignment->getSkillBaseId() != $skillBaseId) {
227  continue;
228  }
229 
230  if ($assignment->getSkillTrefId() != $skillTrefId) {
231  continue;
232  }
233 
234  return true;
235  }
236 
237  return false;
238  }
239 
240  public function getUniqueAssignedSkills(): array
241  {
242  $skills = [];
243 
244  foreach ($this->assignments as $assignmentsByQuestion) {
245  foreach ($assignmentsByQuestion as $assignment) {
246  $key = $this->buildSkillKey($assignment->getSkillBaseId(), $assignment->getSkillTrefId());
247  if (!isset($skills[$key])) {
248  $skills[$key] = [
249  'skill' => new ilBasicSkill($assignment->getSkillBaseId()),
250  'skill_base_id' => $assignment->getSkillBaseId(),
251  'skill_tref_id' => $assignment->getSkillTrefId(),
252  'skill_title' => $assignment->getSkillTitle(),
253  'skill_path' => $assignment->getSkillPath(),
254  'num_assigns' => $this->getNumAssignsBySkill(
255  $assignment->getSkillBaseId(),
256  $assignment->getSkillTrefId()
257  ),
258  'max_points' => $this->getMaxPointsBySkill(
259  $assignment->getSkillBaseId(),
260  $assignment->getSkillTrefId()
261  )
262  ];
263  }
264  }
265  }
266 
267  return $skills;
268  }
269 
270  public function isAssignedSkill($skillBaseId, $skillTrefId): bool
271  {
272  foreach ($this->getUniqueAssignedSkills() as $assignedSkill) {
273  if ($assignedSkill['skill_base_id'] != $skillBaseId) {
274  continue;
275  }
276 
277  if ($assignedSkill['skill_tref_id'] == $skillTrefId) {
278  return true;
279  }
280  }
281 
282  return false;
283  }
284 
285  public function getNumAssignsBySkill($skillBaseId, $skillTrefId)
286  {
287  return $this->numAssignsBySkill[$this->buildSkillKey($skillBaseId, $skillTrefId)] ?? null;
288  }
289 
290  public function getMaxPointsBySkill($skillBaseId, $skillTrefId)
291  {
292  return $this->maxPointsBySkill[$this->buildSkillKey($skillBaseId, $skillTrefId)] ?? null;
293  }
294 
295  public function hasSkillsAssignedLowerThanBarrier(): bool
296  {
297  $global_barrier = (new ilObjTestFolder())->getGlobalSettingsRepository()
298  ->getGlobalSettings()->getSkillTriggeringNumberOfAnswers();
299 
300  foreach ($this->getUniqueAssignedSkills() as $skill_data) {
301  if ($skill_data['num_assigns'] < $global_barrier) {
302  return true;
303  }
304  }
305 
306  return false;
307  }
308 }
$res
Definition: ltiservices.php:66
incrementMaxPointsBySkill(ilAssQuestionSkillAssignment $assignment)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
addAssignment(ilAssQuestionSkillAssignment $assignment)
Class ilObjTestFolder.
Basic Skill.
isAssignedToQuestionId($skillBaseId, $skillTrefId, $questionId)
incrementNumAssignsBySkill(ilAssQuestionSkillAssignment $assignment)