ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilTestQuestionPoolImporter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
34 {
36 
39 
40  public function __construct()
41  {
43 
44  $local_dic = QuestionPoolDIC::dic();
45  $this->request_data_collector = $local_dic['request_data_collector'];
46  }
47 
48  public function importXmlRepresentation(
49  string $a_entity,
50  string $a_id,
51  string $a_xml,
52  ilImportMapping $a_mapping
53  ): void {
54  global $DIC;
55  // Container import => pool object already created
56  if (($new_id = $a_mapping->getMapping('components/ILIAS/Container', 'objs', $a_id)) !== null) {
57  $new_obj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
58  $new_obj->getObjectProperties()->storePropertyIsOnline($new_obj->getObjectProperties()->getPropertyIsOnline()->withOffline()); // sets Question pools to always online
59 
60  $selected_questions = [];
61  [$importdir, $xmlfile, $qtifile] = $this->buildImportDirectoriesFromContainerImport(
62  $this->getImportDirectory()
63  );
64  } elseif (($new_id = $a_mapping->getMapping('components/ILIAS/TestQuestionPool', 'qpl', 'new_id')) !== null) {
65  $new_obj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
66 
67  $selected_questions = ilSession::get('qpl_import_selected_questions');
68  [$subdir, $importdir, $xmlfile, $qtifile] = $this->buildImportDirectoriesFromImportFile(
69  ilSession::get('path_to_import_file')
70  );
71  ilSession::clear('qpl_import_selected_questions');
72  } else {
73  // Shouldn't happen
74  $DIC['ilLog']->write(__METHOD__ . ': non container and no tax mapping, perhaps old qpl export');
75  return;
76  }
77 
78  if (!file_exists($xmlfile)) {
79  $DIC['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xmlfile);
80  return;
81  }
82  if (!file_exists($qtifile)) {
83  $DIC['ilLog']->write(__METHOD__ . ': Cannot find qti definition: ' . $qtifile);
84  return;
85  }
86 
87  $this->pool_obj = $new_obj;
88 
89  $new_obj->fromXML($xmlfile);
90 
91  $qpl_new = $this->request_data_collector->string('qpl_new');
92 
93  // set another question pool name (if possible)
94  if ($qpl_new !== '') {
95  $new_obj->setTitle($qpl_new);
96  }
97 
98  $new_obj->update();
99  $new_obj->saveToDb();
100 
101  // FIXME: Copied from ilObjQuestionPoolGUI::importVerifiedFileObject
102  // TODO: move all logic to ilObjQuestionPoolGUI::importVerifiedFile and call
103  // this method from ilObjQuestionPoolGUI and ilTestImporter
104 
105  $DIC['ilLog']->write(__METHOD__ . ': xml file: ' . $xmlfile . ', qti file:' . $qtifile);
106 
107  $qtiParser = new ilQTIParser(
108  $importdir,
109  $qtifile,
111  $new_obj->getId(),
112  $selected_questions
113  );
114  $qtiParser->startParsing();
115 
116  $questionPageParser = new ilQuestionPageParser(
117  $new_obj,
118  $xmlfile,
119  $importdir
120  );
121  $questionPageParser->setQuestionMapping($qtiParser->getImportMapping());
122  $questionPageParser->startParsing();
123 
124  foreach ($qtiParser->getImportMapping() as $k => $v) {
125  $old_question_id = substr($k, strpos($k, 'qst_') + strlen('qst_'));
126  $new_question_id = (string) $v['pool']; // yes, this is the new question id ^^
127 
128  $a_mapping->addMapping(
129  'components/ILIAS/Taxonomy',
130  'tax_item',
131  "qpl:quest:{$old_question_id}",
132  $new_question_id
133  );
134 
135  $a_mapping->addMapping(
136  'components/ILIAS/Taxonomy',
137  'tax_item_obj_id',
138  "qpl:quest:{$old_question_id}",
139  (string) $new_obj->getId()
140  );
141 
142  $a_mapping->addMapping(
143  'components/ILIAS/TestQuestionPool',
144  'quest',
145  $old_question_id,
146  $new_question_id
147  );
148  }
149 
150  $this->importQuestionSkillAssignments($xmlfile, $a_mapping, $new_obj->getId());
151 
152  $a_mapping->addMapping('components/ILIAS/TestQuestionPool', 'qpl', $a_id, (string) $new_obj->getId());
153  $a_mapping->addMapping(
154  'components/ILIAS/MetaData',
155  'md',
156  $a_id . ':0:qpl',
157  $new_obj->getId() . ':0:qpl'
158  );
159 
160 
161  $new_obj->saveToDb();
162  }
163 
169  public function finalProcessing(ilImportMapping $a_mapping): void
170  {
171  $maps = $a_mapping->getMappingsOfEntity('components/ILIAS/TestQuestionPool', 'qpl');
172  foreach ($maps as $old => $new) {
173  if ($old !== 'new_id' && (int) $old > 0) {
174  $new_tax_ids = $a_mapping->getMapping('components/ILIAS/Taxonomy', 'tax_usage_of_obj', (string) $old);
175  if ($new_tax_ids !== null) {
176  $tax_ids = explode(':', $new_tax_ids);
177  foreach ($tax_ids as $tid) {
178  ilObjTaxonomy::saveUsage((int) $tid, (int) $new);
179  }
180  }
181  }
182  }
183  }
184 
185  protected function importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId): void
186  {
187  $parser = new ilAssQuestionSkillAssignmentXmlParser($xmlFile);
188  $parser->startParsing();
189 
190  $importer = new ilAssQuestionSkillAssignmentImporter();
191  $importer->setTargetParentObjId($targetParentObjId);
192  $importer->setImportInstallationId($this->getInstallId());
193  $importer->setImportMappingRegistry($mappingRegistry);
194  $importer->setImportMappingComponent('components/ILIAS/TestQuestionPool');
195  $importer->setImportAssignmentList($parser->getAssignmentList());
196 
197  $importer->import();
198 
199  if ($importer->getFailedImportAssignmentList()->assignmentsExist()) {
200  $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($targetParentObjId);
201  $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
202 
203  $this->pool_obj->getObjectProperties()->storePropertyIsOnline($this->pool_obj->getObjectProperties()->getPropertyIsOnline()->withOffline());
204  }
205  }
206 }
Importer class for question pools.
static get(string $a_var)
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
readonly RequestDataCollector $request_data_collector
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
buildImportDirectoriesFromContainerImport(string $importdir)
finalProcessing(ilImportMapping $a_mapping)
Final processing.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getMapping(string $a_comp, string $a_entity, string $a_old_id)
getMappingsOfEntity(string $a_comp, string $a_entity)
global $DIC
Definition: shib_login.php:22
Legacy Content Object Parser.
static saveUsage(int $a_tax_id, int $a_obj_id)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
__construct(Container $dic, ilPlugin $plugin)
array buildImportDirectoriesFromImportFile(string $file_to_import)
importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId)
static clear(string $a_var)
Xml importer class.