ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilUnitConfigurationRepository.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/TestQuestionPool/classes/class.assFormulaQuestionUnit.php";
5 include_once "./Modules/TestQuestionPool/classes/class.assFormulaQuestionUnitCategory.php";
6 
11 {
15  protected $consumer_id = 0;
16 
20  protected $lng;
21 
25  private $units = array();
26 
30  private $categorizedUnits = array();
31 
35  public function __construct($consumer_id)
36  {
40  global $lng;
41 
42  $this->consumer_id = $consumer_id;
43  $this->lng = $lng;
44  }
45 
49  public function setConsumerId($consumer_id)
50  {
51  $this->consumer_id = $consumer_id;
52  }
53 
57  public function getConsumerId()
58  {
59  return $this->consumer_id;
60  }
61 
66  public function isCRUDAllowed($a_category_id)
67  {
71  global $ilDB;
72 
73  $res = $ilDB->queryF(
74  'SELECT * FROM il_qpl_qst_fq_ucat WHERE category_id = %s',
75  array('integer'),
76  array($a_category_id)
77  );
78  $row = $ilDB->fetchAssoc($res);
79  return isset($row['question_fi']) && $row['question_fi'] == $this->getConsumerId();
80  }
81 
88  public function copyCategory($a_category_id, $a_question_fi, $a_category_name = null)
89  {
93  global $ilDB;
94 
95  $res = $ilDB->queryF(
96  'SELECT category FROM il_qpl_qst_fq_ucat WHERE category_id = %s',
97  array('integer'),
98  array($a_category_id)
99  );
100  $row = $ilDB->fetchAssoc($res);
101 
102  if (null === $a_category_name) {
103  $a_category_name = $row['category'];
104  }
105 
106  $next_id = $ilDB->nextId('il_qpl_qst_fq_ucat');
107  $ilDB->insert(
108  'il_qpl_qst_fq_ucat',
109  array(
110  'category_id' => array('integer', $next_id),
111  'category' => array('text', $a_category_name),
112  'question_fi' => array('integer', (int) $a_question_fi)
113  )
114  );
115 
116  return $next_id;
117  }
118 
124  public function copyUnitsByCategories($a_from_category_id, $a_to_category_id, $a_question_fi)
125  {
129  global $ilDB;
130 
131  $res = $ilDB->queryF(
132  'SELECT * FROM il_qpl_qst_fq_unit WHERE category_fi = %s',
133  array('integer'),
134  array($a_from_category_id)
135  );
136  $i = 0;
137  $units = array();
138  while ($row = $ilDB->fetchAssoc($res)) {
139  $next_id = $ilDB->nextId('il_qpl_qst_fq_unit');
140 
141  $units[$i]['old_unit_id'] = $row['unit_id'];
142  $units[$i]['new_unit_id'] = $next_id;
143 
144  $ilDB->insert(
145  'il_qpl_qst_fq_unit',
146  array(
147  'unit_id' => array('integer', $next_id),
148  'unit' => array('text', $row['unit']),
149  'factor' => array('float', $row['factor']),
150  'baseunit_fi' => array('integer', (int) $row['baseunit_fi']),
151  'category_fi' => array('integer', (int) $a_to_category_id),
152  'sequence' => array('integer', (int) $row['sequence']),
153  'question_fi' => array('integer', (int) $a_question_fi)
154  )
155  );
156  $i++;
157  }
158 
159  foreach ($units as $unit) {
160  //update unit : baseunit_fi
161  $ilDB->update(
162  'il_qpl_qst_fq_unit',
163  array('baseunit_fi' => array('integer', (int) $unit['new_unit_id'])),
164  array(
165  'baseunit_fi' => array('integer', $unit['old_unit_id']),
166  'category_fi' => array('integer', $a_to_category_id)
167  )
168  );
169 
170  //update var : unit_fi
171  $ilDB->update(
172  'il_qpl_qst_fq_var',
173  array('unit_fi' => array('integer', (int) $unit['new_unit_id'])),
174  array(
175  'unit_fi' => array('integer', $unit['old_unit_id']),
176  'question_fi' => array('integer', $a_question_fi)
177  )
178  );
179 
180  //update res : unit_fi
181  $ilDB->update(
182  'il_qpl_qst_fq_res',
183  array('unit_fi' => array('integer', (int) $unit['new_unit_id'])),
184  array(
185  'unit_fi' => array('integer', $unit['old_unit_id']),
186  'question_fi' => array('integer', $a_question_fi)
187  )
188  );
189 
190  //update res_unit : unit_fi
191  $ilDB->update(
192  'il_qpl_qst_fq_res_unit',
193  array('unit_fi' => array('integer', (int) $unit['new_unit_id'])),
194  array(
195  'unit_fi' => array('integer', $unit['old_unit_id']),
196  'question_fi' => array('integer', $a_question_fi)
197  )
198  );
199  }
200  }
201 
202  public function getCategoryUnitCount($id)
203  {
204  global $ilDB;
205 
206  $result = $ilDB->queryF(
207  "SELECT * FROM il_qpl_qst_fq_unit WHERE category_fi = %s",
208  array('integer'),
209  array($id)
210  );
211  return $result->numRows();
212  }
213 
214  public function isUnitInUse($id)
215  {
216  global $ilDB;
217 
218  $result_1 = $ilDB->queryF(
219  "SELECT unit_fi FROM il_qpl_qst_fq_res_unit WHERE unit_fi = %s",
220  array('integer'),
221  array($id)
222  );
223 
224  $result_2 = $ilDB->queryF(
225  "SELECT unit_fi FROM il_qpl_qst_fq_var WHERE unit_fi = %s",
226  array('integer'),
227  array($id)
228  );
229  $result_3 = $ilDB->queryF(
230  "SELECT unit_fi FROM il_qpl_qst_fq_res WHERE unit_fi = %s",
231  array('integer'),
232  array($id)
233  );
234 
235  $cnt_1 = $ilDB->numRows($result_1);
236  $cnt_2 = $ilDB->numRows($result_2);
237  $cnt_3 = $ilDB->numRows($result_3);
238 
239  if ($cnt_1 || $cnt_2 || $cnt_3) {
240  return true;
241  } else {
242  return false;
243  }
244  }
245 
250  public function checkDeleteCategory($id)
251  {
255  global $ilDB;
256 
257  $res = $ilDB->queryF(
258  'SELECT unit_id FROM il_qpl_qst_fq_unit WHERE category_fi = %s',
259  array('integer'),
260  array($id)
261  );
262  if ($ilDB->numRows($res)) {
263  while ($row = $ilDB->fetchAssoc($res)) {
264  $unit_res = $this->checkDeleteUnit($row['unit_id'], $id);
265  if (!is_null($unit_res)) {
266  return $unit_res;
267  }
268  }
269  }
270  return null;
271  }
272 
273  public function deleteUnit($id)
274  {
275  global $ilDB;
276 
277  $res = $this->checkDeleteUnit($id);
278  if (!is_null($res)) {
279  return $res;
280  }
281  $affectedRows = $ilDB->manipulateF(
282  "DELETE FROM il_qpl_qst_fq_unit WHERE unit_id = %s",
283  array('integer'),
284  array($id)
285  );
286  if ($affectedRows > 0) {
287  $this->clearUnits();
288  }
289  return null;
290  }
291 
292  protected function loadUnits()
293  {
294  global $ilDB;
295 
296  $result = $ilDB->query(
297  "
298  SELECT units.*, il_qpl_qst_fq_ucat.category, baseunits.unit baseunit_title
299  FROM il_qpl_qst_fq_unit units
300  INNER JOIN il_qpl_qst_fq_ucat ON il_qpl_qst_fq_ucat.category_id = units.category_fi
301  LEFT JOIN il_qpl_qst_fq_unit baseunits ON baseunits.unit_id = units.baseunit_fi
302  ORDER BY il_qpl_qst_fq_ucat.category, units.sequence"
303  );
304 
305  if ($result->numRows()) {
306  while ($row = $ilDB->fetchAssoc($result)) {
307  $unit = new assFormulaQuestionUnit();
308  $unit->initFormArray($row);
309  $this->addUnit($unit);
310  }
311  }
312  }
313 
314  public function getCategorizedUnits()
315  {
316  global $ilDB;
317 
318  if (count($this->categorizedUnits) == 0) {
319  $result = $ilDB->queryF(
320  "
321  SELECT units.*, il_qpl_qst_fq_ucat.category, il_qpl_qst_fq_ucat.question_fi, baseunits.unit baseunit_title
322  FROM il_qpl_qst_fq_unit units
323  INNER JOIN il_qpl_qst_fq_ucat ON il_qpl_qst_fq_ucat.category_id = units.category_fi
324  LEFT JOIN il_qpl_qst_fq_unit baseunits ON baseunits.unit_id = units.baseunit_fi
325  WHERE units.question_fi = %s
326  ORDER BY il_qpl_qst_fq_ucat.category, units.sequence",
327  array('integer'),
328  array($this->getConsumerId())
329  );
330 
331  if ($result->numRows()) {
332  $category = '';
333  while ($row = $ilDB->fetchAssoc($result)) {
334  $unit = new assFormulaQuestionUnit();
335  $unit->initFormArray($row);
336  if (strcmp($category, $unit->getCategory()) != 0) {
337  $cat = new assFormulaQuestionUnitCategory();
338  $cat->initFormArray(array(
339  'category_id' => $row['category_fi'],
340  'category' => $row['category'],
341  'question_fi' => $row['question_fi'],
342  ));
343  array_push($this->categorizedUnits, $cat);
344  $category = $unit->getCategory();
345  }
346  array_push($this->categorizedUnits, $unit);
347  }
348  }
349  }
350 
352  }
353 
354  protected function clearUnits()
355  {
356  $this->units = array();
357  }
358 
359  protected function addUnit($unit)
360  {
361  $this->units[$unit->getId()] = $unit;
362  }
363 
364  public function getUnits()
365  {
366  if (count($this->units) == 0) {
367  $this->loadUnits();
368  }
369  return $this->units;
370  }
371 
372  public function loadUnitsForCategory($category)
373  {
374  global $ilDB;
375 
376  $units = array();
377  $result = $ilDB->queryF(
378  "
379  SELECT units.*, baseunits.unit baseunit_title
380  FROM il_qpl_qst_fq_unit units
381  INNER JOIN il_qpl_qst_fq_ucat ON il_qpl_qst_fq_ucat.category_id = units.category_fi
382  LEFT JOIN il_qpl_qst_fq_unit baseunits ON baseunits.unit_id = units.baseunit_fi
383  WHERE il_qpl_qst_fq_ucat.category_id = %s
384  ORDER BY units.sequence",
385  array('integer'),
386  array($category)
387  );
388  if ($result->numRows()) {
389  while ($row = $ilDB->fetchAssoc($result)) {
390  $unit = new assFormulaQuestionUnit();
391  $unit->initFormArray($row);
392  array_push($units, $unit);
393  }
394  }
395  return $units;
396  }
397 
402  public function getUnit($id)
403  {
404  if (count($this->units) == 0) {
405  $this->loadUnits();
406  }
407  if (array_key_exists($id, $this->units)) {
408  return $this->units[$id];
409  } else {
410  //maybee this is a new unit ...
411  // reload $this->units
412 
413  $this->loadUnits();
414  if (array_key_exists($id, $this->units)) {
415  return $this->units[$id];
416  }
417  }
418  return null;
419  }
420 
421 
422  public function getUnitCategories()
423  {
424  global $ilDB;
425 
426  $categories = array();
427  $result = $ilDB->queryF(
428  "SELECT * FROM il_qpl_qst_fq_ucat WHERE question_fi > %s ORDER BY category",
429  array('integer'),
430  array(0)
431  );
432  if ($result->numRows()) {
433  while ($row = $ilDB->fetchAssoc($result)) {
434  $value = (strcmp("-qpl_qst_formulaquestion_" . $row["category"] . "-", $this->lng->txt($row["category"])) == 0) ? $row["category"] : $this->lng->txt($row["category"]);
435 
436  if (strlen(trim($row["category"]))) {
437  $cat = array(
438  "value" => $row["category_id"],
439  "text" => $value,
440  "qst_id" => $row['question_fi']
441  );
442  $categories[$row["category_id"]] = $cat;
443  }
444  }
445  }
446  return $categories;
447  }
448 
449  public function getAdminUnitCategories()
450  {
451  global $ilDB;
452 
453  $categories = array();
454  $result = $ilDB->queryF(
455  "SELECT * FROM il_qpl_qst_fq_ucat WHERE question_fi = %s ORDER BY category",
456  array('integer'),
457  array(0)
458  );
459  if ($result->numRows()) {
460  while ($row = $ilDB->fetchAssoc($result)) {
461  $value = (strcmp("-qpl_qst_formulaquestion_" . $row["category"] . "-", $this->lng->txt($row["category"])) == 0) ? $row["category"] : $this->lng->txt($row["category"]);
462 
463  if (strlen(trim($row["category"]))) {
464  $cat = array(
465  "value" => $row["category_id"],
466  "text" => $value,
467  "qst_id" => $row['question_fi']
468  );
469  $categories[$row["category_id"]] = $cat;
470  }
471  }
472  }
473 
474  return $categories;
475  }
476 
481  public function saveUnitOrder($unit_id, $sequence)
482  {
486  global $ilDB;
487 
488  $ilDB->manipulateF(
489  '
490  UPDATE il_qpl_qst_fq_unit
491  SET sequence = %s
492  WHERE unit_id = %s AND question_fi = %s
493  ',
494  array('integer', 'integer', 'integer'),
495  array((int) $sequence, $unit_id, $this->getConsumerId())
496  );
497  }
498 
499  public function checkDeleteUnit($id, $category_id = null)
500  {
501  global $ilDB;
502 
503  $result = $ilDB->queryF(
504  "SELECT * FROM il_qpl_qst_fq_var WHERE unit_fi = %s",
505  array('integer'),
506  array($id)
507  );
508  if ($result->numRows() > 0) {
509  return $this->lng->txt("err_unit_in_variables");
510  }
511  $result = $ilDB->queryF(
512  "SELECT * FROM il_qpl_qst_fq_res WHERE unit_fi = %s",
513  array('integer'),
514  array($id)
515  );
516  if ($result->numRows() > 0) {
517  return $this->lng->txt("err_unit_in_results");
518  }
519  if (!is_null($category_id)) {
520  $result = $ilDB->queryF(
521  "SELECT * FROM il_qpl_qst_fq_unit WHERE baseunit_fi = %s AND category_fi != %s",
522  array('integer', 'integer', 'integer'),
523  array($id, $id, $category_id)
524  );
525  } else {
526  $result = $ilDB->queryF(
527  "SELECT * FROM il_qpl_qst_fq_unit WHERE baseunit_fi = %s AND unit_id != %s",
528  array('integer', 'integer'),
529  array($id, $id)
530  );
531  }
532  if ($result->numRows() > 0) {
533  return $this->lng->txt("err_unit_is_baseunit");
534  }
535  return null;
536  }
537 
543  public function getUnitCategoryById($id)
544  {
548  global $ilDB;
549 
550  $query = 'SELECT * FROM il_qpl_qst_fq_ucat WHERE category_id = ' . $ilDB->quote($id, 'integer');
551  $res = $ilDB->query($query);
552  if (!$ilDB->numRows($res)) {
553  throw new ilException('un_category_not_exist');
554  }
555 
556  $row = $ilDB->fetchAssoc($res);
557  $category = new assFormulaQuestionUnitCategory();
558  $category->initFormArray($row);
559  return $category;
560  }
561 
566  public function saveCategory(assFormulaQuestionUnitCategory $category)
567  {
571  global $ilDB;
572 
573  $res = $ilDB->queryF(
574  'SELECT * FROM il_qpl_qst_fq_ucat WHERE category = %s AND question_fi = %s AND category_id != %s',
575  array('text', 'integer', 'integer'),
576  array($category->getCategory(), $this->getConsumerId(), $category->getId())
577  );
578  if ($ilDB->numRows($res)) {
579  throw new ilException('err_wrong_categoryname');
580  }
581 
582  $ilDB->manipulateF(
583  'UPDATE il_qpl_qst_fq_ucat SET category = %s WHERE question_fi = %s AND category_id = %s',
584  array('text', 'integer', 'integer'),
585  array($category->getCategory(), $this->getConsumerId(), $category->getId())
586  );
587  }
588 
593  public function saveNewUnitCategory(assFormulaQuestionUnitCategory $category)
594  {
598  global $ilDB;
599 
600  $res = $ilDB->queryF(
601  'SELECT category FROM il_qpl_qst_fq_ucat WHERE category = %s AND question_fi = %s',
602  array('text', 'integer'),
603  array($category->getCategory(), $this->getConsumerId())
604  );
605  if ($ilDB->numRows($res)) {
606  throw new ilException('err_wrong_categoryname');
607  }
608 
609  $next_id = $ilDB->nextId('il_qpl_qst_fq_ucat');
610  $ilDB->manipulateF(
611  "INSERT INTO il_qpl_qst_fq_ucat (category_id, category, question_fi) VALUES (%s, %s, %s)",
612  array('integer', 'text', 'integer'),
613  array(
614  $next_id,
615  $category->getCategory(),
616  (int) $this->getConsumerId()
617  )
618  );
619  $category->setId($next_id);
620  }
621 
625  public function getAllUnitCategories()
626  {
630  global $ilDB;
631 
632  $categories = array();
633  $result = $ilDB->queryF(
634  "SELECT * FROM il_qpl_qst_fq_ucat WHERE question_fi = %s OR question_fi = %s ORDER BY category",
635  array('integer', 'integer'),
636  array($this->getConsumerId(), 0)
637  );
638  if ($result->numRows()) {
639  while ($row = $ilDB->fetchAssoc($result)) {
640  $category = new assFormulaQuestionUnitCategory();
641  $category->initFormArray($row);
642  $categories[] = $category;
643  }
644  }
645  return $categories;
646  }
647 
652  public function deleteCategory($id)
653  {
657  global $ilDB;
658 
659  $res = $this->checkDeleteCategory($id);
660  if (!is_null($res)) {
661  return $this->lng->txt('err_category_in_use');
662  }
663 
664  $res = $ilDB->queryF(
665  'SELECT * FROM il_qpl_qst_fq_unit WHERE category_fi = %s',
666  array('integer'),
667  array($id)
668  );
669  while ($row = $ilDB->fetchAssoc($res)) {
670  $this->deleteUnit($row['unit_id']);
671  }
672  $ar = $ilDB->manipulateF(
673  'DELETE FROM il_qpl_qst_fq_ucat WHERE category_id = %s',
674  array('integer'),
675  array($id)
676  );
677  if ($ar > 0) {
678  $this->clearUnits();
679  }
680  return null;
681  }
682 
686  public function createNewUnit(assFormulaQuestionUnit $unit)
687  {
691  global $ilDB;
692 
693  $next_id = $ilDB->nextId('il_qpl_qst_fq_unit');
694  $ilDB->manipulateF(
695  'INSERT INTO il_qpl_qst_fq_unit (unit_id, unit, factor, baseunit_fi, category_fi, sequence, question_fi) VALUES (%s, %s, %s, %s, %s, %s, %s)',
696  array('integer', 'text', 'float', 'integer', 'integer', 'integer', 'integer'),
697  array(
698  $next_id,
699  $unit->getUnit(),
700  1,
701  0,
702  (int) $unit->getCategory(),
703  0,
704  (int) $this->getConsumerId()
705  )
706  );
707  $unit->setId($next_id);
708  $unit->setFactor(1);
709  $unit->setBaseUnit(0);
710  $unit->setSequence(0);
711 
712  $this->clearUnits();
713  }
714 
718  public function saveUnit(assFormulaQuestionUnit $unit)
719  {
723  global $ilDB;
724 
725  $res = $ilDB->queryF(
726  'SELECT unit_id FROM il_qpl_qst_fq_unit WHERE unit_id = %s',
727  array('integer'),
728  array($unit->getId())
729  );
730  if ($ilDB->fetchAssoc($res)) {
731  $row = $ilDB->fetchAssoc($res);
732  $sequence = $row['sequence'];
733  if (is_null($unit->getBaseUnit()) || !strlen($unit->getBaseUnit())) {
734  $unit->setFactor(1);
735  }
736  $ar = $ilDB->manipulateF(
737  'UPDATE il_qpl_qst_fq_unit SET unit = %s, factor = %s, baseunit_fi = %s, category_fi = %s, sequence = %s WHERE unit_id = %s AND question_fi = %s',
738  array('text', 'float', 'integer', 'integer', 'integer', 'integer', 'integer'),
739  array($unit->getUnit(), $unit->getFactor(), (int) $unit->getBaseUnit(), (int) $unit->getCategory(), (int) $unit->getSequence(), (int) $unit->getId(), (int) $this->getConsumerId())
740  );
741  if ($ar > 0) {
742  $this->clearUnits();
743  }
744  }
745  }
746 
751  public function cloneUnits($a_from_consumer_id, $a_to_consumer_id)
752  {
756  global $ilDB;
757 
758  $category_mapping = array();
759 
760  $res = $ilDB->queryF("SELECT * FROM il_qpl_qst_fq_ucat WHERE question_fi = %s", array('integer'), array($a_from_consumer_id));
761  while ($row = $ilDB->fetchAssoc($res)) {
762  $new_category_id = $this->copyCategory($row['category_id'], $a_to_consumer_id);
763  $category_mapping[$row['category_id']] = $new_category_id;
764  }
765 
766  foreach ($category_mapping as $old_category_id => $new_category_id) {
767  $res = $ilDB->queryF(
768  'SELECT * FROM il_qpl_qst_fq_unit WHERE category_fi = %s',
769  array('integer'),
770  array($old_category_id)
771  );
772 
773  $i = 0;
774  $units = array();
775  while ($row = $ilDB->fetchAssoc($res)) {
776  $next_id = $ilDB->nextId('il_qpl_qst_fq_unit');
777 
778  $units[$i]['old_unit_id'] = $row['unit_id'];
779  $units[$i]['new_unit_id'] = $next_id;
780 
781  $ilDB->insert(
782  'il_qpl_qst_fq_unit',
783  array(
784  'unit_id' => array('integer', $next_id),
785  'unit' => array('text', $row['unit']),
786  'factor' => array('float', $row['factor']),
787  'baseunit_fi' => array('integer', (int) $row['baseunit_fi']),
788  'category_fi' => array('integer', (int) $new_category_id),
789  'sequence' => array('integer', (int) $row['sequence']),
790  'question_fi' => array('integer', (int) $a_to_consumer_id)
791  )
792  );
793  $i++;
794  }
795 
796  foreach ($units as $unit) {
797  //update unit : baseunit_fi
798  $ilDB->update(
799  'il_qpl_qst_fq_unit',
800  array('baseunit_fi' => array('integer', (int) $unit['new_unit_id'])),
801  array(
802  'baseunit_fi' => array('integer', (int) $unit['old_unit_id']),
803  'question_fi' => array('integer', (int) $a_to_consumer_id)
804  )
805  );
806 
807  //update var : unit_fi
808  $ilDB->update(
809  'il_qpl_qst_fq_var',
810  array('unit_fi' => array('integer', (int) $unit['new_unit_id'])),
811  array(
812  'unit_fi' => array('integer', (int) $unit['old_unit_id']),
813  'question_fi' => array('integer', (int) $a_to_consumer_id)
814  )
815  );
816 
817  //update res : unit_fi
818  $ilDB->update(
819  'il_qpl_qst_fq_res',
820  array('unit_fi' => array('integer', (int) $unit['new_unit_id'])),
821  array(
822  'unit_fi' => array('integer', (int) $unit['old_unit_id']),
823  'question_fi' => array('integer', (int) $a_to_consumer_id)
824  )
825  );
826 
827  //update res_unit : unit_fi
828  $ilDB->update(
829  'il_qpl_qst_fq_res_unit',
830  array('unit_fi' => array('integer', (int) $unit['new_unit_id'])),
831  array(
832  'unit_fi' => array('integer', (int) $unit['old_unit_id']),
833  'question_fi' => array('integer', (int) $a_to_consumer_id)
834  )
835  );
836  }
837  }
838  }
839 }
$result
if(!array_key_exists('StateId', $_REQUEST)) $id
Class ilUnitConfigurationRepository.
foreach($_POST as $key=> $value) $res
$query
Create styles array
The data for the language used.
global $ilDB
$i
Definition: disco.tpl.php:19