ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilRemoveDynamicTestsAndCorrespondingDataMigration.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Test\Setup;
22 
23 use ILIAS\Setup;
27 use ilDBInterface;
28 use Exception;
30 
32 {
33  private const DEFAULT_AMOUNT_OF_STEPS = 10000;
34  private ilDBInterface $db;
35 
39  private mixed $io;
40 
41  private bool $ilias_is_initialized = false;
42 
43  public function getLabel(): string
44  {
45  return "Delete All Data of Dynamic (CTM) Tests from Database.";
46  }
47 
49  {
50  return self::DEFAULT_AMOUNT_OF_STEPS;
51  }
52 
53  public function getPreconditions(Environment $environment): array
54  {
55  return [
58  ];
59  }
60 
61  public function prepare(Environment $environment): void
62  {
63  $this->db = $environment->getResource(Setup\Environment::RESOURCE_DATABASE);
64  $this->io = $environment->getResource(Environment::RESOURCE_ADMIN_INTERACTION);
65  }
66 
67  public function step(Environment $environment): void
68  {
69  if (!$this->ilias_is_initialized) {
70  //This is necessary for using ilObjects delete function to remove existing objects
73  $this->ilias_is_initialized = true;
74  }
75  $row_test_info = $this->db->fetchObject(
76  $this->db->query(
77  'SELECT obj_fi, test_id FROM tst_tests '
78  . 'WHERE '
79  . $this->db->equals('question_set_type', 'DYNAMIC_QUEST_SET', \ilDBConstants::T_TEXT, true)
80  . 'Limit 1'
81  )
82  );
83 
84  $row_ref_id = $this->db->fetchObject(
85  $this->db->query(
86  'SELECT ref_id FROM object_reference '
87  . 'WHERE '
88  . $this->db->equals('object_reference.obj_id', $row_test_info->obj_fi, \ilDBConstants::T_INTEGER)
89  )
90  );
91 
92  if ($row_ref_id !== null) {
93  try {
94  \ilRepUtil::removeObjectsFromSystem([$row_ref_id->ref_id]);
95  return;
96  } catch (\Exception $e) {
97  }
98  }
99 
100  try {
101  (new \ilObjTest($row_test_info->obj_fi, false))->delete();
102  } catch (\Exception $e) {
103  }
104 
105  try {
106  $test_obj = new \ilObjTest();
107  $test_obj->setTestId($row_test_info->test_id);
108  $test_obj->deleteTest();
109  } catch (\Exception $e) {
110  }
111  }
112 
113  public function getRemainingAmountOfSteps(): int
114  {
115  $result = $this->db->query(
116  "SELECT count(*) as cnt FROM tst_tests"
117  . " WHERE " . $this->db->equals('question_set_type', 'DYNAMIC_QUEST_SET', 'text', true)
118  );
119  $row = $this->db->fetchAssoc($result);
120 
121  return (int) $row['cnt'] ?? 0;
122  }
123 }
const CONTEXT_CRON
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:28
prepare(Environment $environment)
Prepare the migration by means of some environment.
static initILIAS()
ilias initialisation
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
static removeObjectsFromSystem(array $a_ref_ids, bool $a_from_recovery_folder=false)
remove objects from trash bin and all entries therefore every object needs a specific deleteObject() ...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
static init(string $a_type)
Init context by type.