ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilFixMissingQuestionDuplicationMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  private ilDBInterface $db;
27 
28  private bool $ilias_is_initialized = false;
29 
30  public function getLabel(): string
31  {
32  return 'Fix Missing Question Duplication When Creating in Test And Pool';
33  }
34 
36  {
37  return 10;
38  }
39 
40  public function getPreconditions(Environment $environment): array
41  {
42  return [
43  new \ilDatabaseInitializedObjective()
44  ];
45  }
46 
47  public function prepare(Environment $environment): void
48  {
49  $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
50  }
51 
52  public function step(Environment $environment): void
53  {
54  if (!$this->ilias_is_initialized) {
57  $this->ilias_is_initialized = true;
58  }
59  $query_main = 'SELECT qst.question_id FROM tst_tests AS tst'
60  . ' INNER JOIN tst_test_question AS tst_qst'
61  . ' ON tst.test_id = tst_qst.test_fi'
62  . ' INNER JOIN qpl_questions AS qst'
63  . ' ON tst_qst.question_fi = qst.question_id'
64  . ' WHERE tst.question_set_type = "FIXED_QUEST_SET"'
65  . ' AND qst.obj_fi != tst.obj_fi'
66  . ' AND ISNULL(qst.original_id)';
67  $result_main = $this->db->query($query_main);
68  while ($question = $this->db->fetchAssoc($result_main)) {
69  $question_obj = assQuestion::_instanciateQuestion((int) $question['question_id']);
70  $clone_id = $question_obj->duplicate(false);
71 
72  $this->db->update(
73  'qpl_questions',
74  ['original_id' => ['integer', $clone_id]],
75  ['original_id' => ['integer', $question['question_id']]]
76  );
77 
78  $test_query = 'SELECT obj_fi FROM tst_test_question INNER JOIN tst_tests ON tst_test_question.test_fi = tst_tests.test_id WHERE question_fi = %s';
79  $test_result = $this->db->queryF($test_query, ['integer'], [$question['question_id']]);
80 
81  $this->db->update(
82  'qpl_questions',
83  [
84  'original_id' => ['integer', $clone_id],
85  'obj_fi' => ['integer', ($this->db->fetchObject($test_result))->obj_fi]
86  ],
87  ['question_id' => ['integer', $question['question_id']]]
88  );
89  }
90  }
91 
92  public function getRemainingAmountOfSteps(): int
93  {
94  $query = 'SELECT COUNT(qst.question_id) AS cnt FROM tst_tests AS tst'
95  . ' INNER JOIN tst_test_question AS tst_qst'
96  . ' ON tst.test_id = tst_qst.test_fi'
97  . ' INNER JOIN qpl_questions AS qst'
98  . ' ON tst_qst.question_fi = qst.question_id'
99  . ' WHERE tst.question_set_type = "FIXED_QUEST_SET"'
100  . ' AND qst.obj_fi != tst.obj_fi'
101  . ' AND ISNULL(qst.original_id)';
102  $result = $this->db->query($query);
103  $row = $this->db->fetchAssoc($result);
104 
105  return (int) ($row['cnt'] ?? 0);
106  }
107 
108 }
step(Environment $environment)
Run one step of the migration.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
getPreconditions(Environment $environment)
Objectives the migration depend on.
const CONTEXT_CRON
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
static initILIAS()
ilias initialisation
$query
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
static init(string $a_type)
Init context by type.
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
static _instanciateQuestion(int $question_id)
prepare(Environment $environment)
Prepare the migration by means of some environment.