Total Complexity | 49 |
Total Lines | 483 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like QuizExport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QuizExport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class QuizExport extends ActivityExport |
||
16 | { |
||
17 | /** |
||
18 | * Export a quiz to the specified directory. |
||
19 | * |
||
20 | * @param int $activityId The ID of the quiz. |
||
21 | * @param string $exportDir The directory where the quiz will be exported. |
||
22 | * @param int $moduleId The ID of the module. |
||
23 | * @param int $sectionId The ID of the section. |
||
24 | */ |
||
25 | public function export($activityId, $exportDir, $moduleId, $sectionId): void |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Retrieves the quiz data. |
||
49 | */ |
||
50 | public function getData(int $quizId, int $sectionId): array |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Exports a question in XML format. |
||
119 | */ |
||
120 | public function exportQuestion(array $question): string |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Retrieves the questions for a specific quiz. |
||
167 | */ |
||
168 | private function getQuestionsForQuiz(int $quizId): array |
||
169 | { |
||
170 | $questions = []; |
||
171 | $quizResources = $this->course->resources[RESOURCE_QUIZQUESTION] ?? []; |
||
172 | |||
173 | foreach ($quizResources as $questionId => $questionData) { |
||
174 | if (in_array($questionId, $this->course->resources[RESOURCE_QUIZ][$quizId]->obj->question_ids)) { |
||
175 | $categoryId = $questionData->question_category ?? 0; |
||
176 | $categoryId = $categoryId > 0 ? $categoryId : $this->getDefaultCategoryId(); |
||
177 | $questions[] = [ |
||
178 | 'id' => $questionData->source_id, |
||
179 | 'questiontext' => $questionData->question, |
||
180 | 'qtype' => $this->mapQuestionType($questionData->quiz_type), |
||
181 | 'questioncategoryid' => $categoryId, |
||
182 | 'answers' => $this->getAnswersForQuestion($questionData->source_id), |
||
183 | 'maxmark' => $questionData->ponderation ?? 1, |
||
184 | ]; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | return $questions; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Maps the quiz type code to a descriptive string. |
||
193 | */ |
||
194 | private function mapQuestionType(string $quizType): string |
||
195 | { |
||
196 | switch ($quizType) { |
||
197 | case UNIQUE_ANSWER: return 'multichoice'; |
||
198 | case MULTIPLE_ANSWER: return 'multichoice_nosingle'; |
||
199 | case FILL_IN_BLANKS: return 'match'; |
||
200 | case FREE_ANSWER: return 'shortanswer'; |
||
201 | case CALCULATED_ANSWER: return 'calculated'; |
||
202 | case UPLOAD_ANSWER: return 'fileupload'; |
||
203 | default: return 'unknown'; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Retrieves the answers for a specific question ID. |
||
209 | */ |
||
210 | private function getAnswersForQuestion(int $questionId): array |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Retrieves feedbacks for a specific quiz. |
||
232 | */ |
||
233 | private function getFeedbacksForQuiz(int $quizId): array |
||
234 | { |
||
235 | $feedbacks = []; |
||
236 | $quizResources = $this->course->resources[RESOURCE_QUIZ] ?? []; |
||
237 | |||
238 | foreach ($quizResources as $quiz) { |
||
239 | if ($quiz->obj->iid == $quizId) { |
||
240 | $feedbacks[] = [ |
||
241 | 'feedbacktext' => $quiz->obj->description ?? '', |
||
242 | 'mingrade' => 0.00000, |
||
243 | 'maxgrade' => $quiz->obj->grade ?? 10.00000, |
||
244 | ]; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | return $feedbacks; |
||
249 | } |
||
250 | |||
251 | private function getDefaultCategoryId(): int |
||
252 | { |
||
253 | return 1; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Creates the quiz.xml file. |
||
258 | */ |
||
259 | private function createQuizXml(array $quizData, string $destinationDir): void |
||
260 | { |
||
261 | $xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
||
262 | $xmlContent .= '<activity id="'.$quizData['id'].'" moduleid="'.$quizData['moduleid'].'" modulename="quiz" contextid="'.$quizData['contextid'].'">'.PHP_EOL; |
||
263 | $xmlContent .= ' <quiz id="'.$quizData['id'].'">'.PHP_EOL; |
||
264 | $xmlContent .= ' <name>'.htmlspecialchars($quizData['name']).'</name>'.PHP_EOL; |
||
265 | $xmlContent .= ' <intro>'.htmlspecialchars($quizData['intro']).'</intro>'.PHP_EOL; |
||
266 | $xmlContent .= ' <introformat>1</introformat>'.PHP_EOL; |
||
267 | $xmlContent .= ' <timeopen>'.($quizData['timeopen'] ?? 0).'</timeopen>'.PHP_EOL; |
||
268 | $xmlContent .= ' <timeclose>'.($quizData['timeclose'] ?? 0).'</timeclose>'.PHP_EOL; |
||
269 | $xmlContent .= ' <timelimit>'.($quizData['timelimit'] ?? 0).'</timelimit>'.PHP_EOL; |
||
270 | $xmlContent .= ' <overduehandling>'.($quizData['overduehandling'] ?? 'autosubmit').'</overduehandling>'.PHP_EOL; |
||
271 | $xmlContent .= ' <graceperiod>'.($quizData['graceperiod'] ?? 0).'</graceperiod>'.PHP_EOL; |
||
272 | $xmlContent .= ' <preferredbehaviour>'.htmlspecialchars($quizData['preferredbehaviour']).'</preferredbehaviour>'.PHP_EOL; |
||
273 | $xmlContent .= ' <canredoquestions>'.($quizData['canredoquestions'] ?? 0).'</canredoquestions>'.PHP_EOL; |
||
274 | $xmlContent .= ' <attempts_number>'.($quizData['attempts_number'] ?? 0).'</attempts_number>'.PHP_EOL; |
||
275 | $xmlContent .= ' <attemptonlast>'.($quizData['attemptonlast'] ?? 0).'</attemptonlast>'.PHP_EOL; |
||
276 | $xmlContent .= ' <grademethod>'.$quizData['grademethod'].'</grademethod>'.PHP_EOL; |
||
277 | $xmlContent .= ' <decimalpoints>'.$quizData['decimalpoints'].'</decimalpoints>'.PHP_EOL; |
||
278 | $xmlContent .= ' <questiondecimalpoints>'.($quizData['questiondecimalpoints'] ?? -1).'</questiondecimalpoints>'.PHP_EOL; |
||
279 | |||
280 | // Review options |
||
281 | $xmlContent .= ' <reviewattempt>'.($quizData['reviewattempt'] ?? 69888).'</reviewattempt>'.PHP_EOL; |
||
282 | $xmlContent .= ' <reviewcorrectness>'.($quizData['reviewcorrectness'] ?? 4352).'</reviewcorrectness>'.PHP_EOL; |
||
283 | $xmlContent .= ' <reviewmarks>'.($quizData['reviewmarks'] ?? 4352).'</reviewmarks>'.PHP_EOL; |
||
284 | $xmlContent .= ' <reviewspecificfeedback>'.($quizData['reviewspecificfeedback'] ?? 4352).'</reviewspecificfeedback>'.PHP_EOL; |
||
285 | $xmlContent .= ' <reviewgeneralfeedback>'.($quizData['reviewgeneralfeedback'] ?? 4352).'</reviewgeneralfeedback>'.PHP_EOL; |
||
286 | $xmlContent .= ' <reviewrightanswer>'.($quizData['reviewrightanswer'] ?? 4352).'</reviewrightanswer>'.PHP_EOL; |
||
287 | $xmlContent .= ' <reviewoverallfeedback>'.($quizData['reviewoverallfeedback'] ?? 4352).'</reviewoverallfeedback>'.PHP_EOL; |
||
288 | |||
289 | // Navigation and presentation settings |
||
290 | $xmlContent .= ' <questionsperpage>'.$quizData['questionsperpage'].'</questionsperpage>'.PHP_EOL; |
||
291 | $xmlContent .= ' <navmethod>'.htmlspecialchars($quizData['navmethod']).'</navmethod>'.PHP_EOL; |
||
292 | $xmlContent .= ' <shuffleanswers>'.$quizData['shuffleanswers'].'</shuffleanswers>'.PHP_EOL; |
||
293 | $xmlContent .= ' <sumgrades>'.$quizData['sumgrades'].'</sumgrades>'.PHP_EOL; |
||
294 | $xmlContent .= ' <grade>'.$quizData['grade'].'</grade>'.PHP_EOL; |
||
295 | |||
296 | // Timing and security |
||
297 | $xmlContent .= ' <timecreated>'.($quizData['timecreated'] ?? time()).'</timecreated>'.PHP_EOL; |
||
298 | $xmlContent .= ' <timemodified>'.($quizData['timemodified'] ?? time()).'</timemodified>'.PHP_EOL; |
||
299 | $xmlContent .= ' <password>'.(isset($quizData['password']) ? htmlspecialchars($quizData['password']) : '').'</password>'.PHP_EOL; |
||
300 | $xmlContent .= ' <subnet>'.(isset($quizData['subnet']) ? htmlspecialchars($quizData['subnet']) : '').'</subnet>'.PHP_EOL; |
||
301 | $xmlContent .= ' <browsersecurity>'.(isset($quizData['browsersecurity']) ? htmlspecialchars($quizData['browsersecurity']) : '-').'</browsersecurity>'.PHP_EOL; |
||
302 | $xmlContent .= ' <delay1>'.($quizData['delay1'] ?? 0).'</delay1>'.PHP_EOL; |
||
303 | $xmlContent .= ' <delay2>'.($quizData['delay2'] ?? 0).'</delay2>'.PHP_EOL; |
||
304 | |||
305 | // Additional options |
||
306 | $xmlContent .= ' <showuserpicture>'.($quizData['showuserpicture'] ?? 0).'</showuserpicture>'.PHP_EOL; |
||
307 | $xmlContent .= ' <showblocks>'.($quizData['showblocks'] ?? 0).'</showblocks>'.PHP_EOL; |
||
308 | $xmlContent .= ' <completionattemptsexhausted>'.($quizData['completionattemptsexhausted'] ?? 0).'</completionattemptsexhausted>'.PHP_EOL; |
||
309 | $xmlContent .= ' <completionpass>'.($quizData['completionpass'] ?? 0).'</completionpass>'.PHP_EOL; |
||
310 | $xmlContent .= ' <completionminattempts>'.($quizData['completionminattempts'] ?? 0).'</completionminattempts>'.PHP_EOL; |
||
311 | $xmlContent .= ' <allowofflineattempts>'.($quizData['allowofflineattempts'] ?? 0).'</allowofflineattempts>'.PHP_EOL; |
||
312 | |||
313 | // Subplugin, if applicable |
||
314 | $xmlContent .= ' <subplugin_quizaccess_seb_quiz>'.PHP_EOL; |
||
315 | $xmlContent .= ' </subplugin_quizaccess_seb_quiz>'.PHP_EOL; |
||
316 | |||
317 | // Add question instances |
||
318 | $xmlContent .= ' <question_instances>'.PHP_EOL; |
||
319 | foreach ($quizData['questions'] as $question) { |
||
320 | $xmlContent .= ' <question_instance id="'.$question['id'].'">'.PHP_EOL; |
||
321 | $xmlContent .= ' <slot>'.$question['id'].'</slot>'.PHP_EOL; |
||
322 | $xmlContent .= ' <page>1</page>'.PHP_EOL; |
||
323 | $xmlContent .= ' <requireprevious>0</requireprevious>'.PHP_EOL; |
||
324 | $xmlContent .= ' <questionid>'.$question['id'].'</questionid>'.PHP_EOL; |
||
325 | $xmlContent .= ' <questioncategoryid>'.$question['questioncategoryid'].'</questioncategoryid>'.PHP_EOL; |
||
326 | $xmlContent .= ' <includingsubcategories>$@NULL@$</includingsubcategories>'.PHP_EOL; |
||
327 | $xmlContent .= ' <maxmark>'.$question['maxmark'].'</maxmark>'.PHP_EOL; |
||
328 | $xmlContent .= ' </question_instance>'.PHP_EOL; |
||
329 | } |
||
330 | $xmlContent .= ' </question_instances>'.PHP_EOL; |
||
331 | |||
332 | // Quiz sections |
||
333 | $xmlContent .= ' <sections>'.PHP_EOL; |
||
334 | $xmlContent .= ' <section id="'.$quizData['id'].'">'.PHP_EOL; |
||
335 | $xmlContent .= ' <firstslot>1</firstslot>'.PHP_EOL; |
||
336 | $xmlContent .= ' <shufflequestions>0</shufflequestions>'.PHP_EOL; |
||
337 | $xmlContent .= ' </section>'.PHP_EOL; |
||
338 | $xmlContent .= ' </sections>'.PHP_EOL; |
||
339 | |||
340 | // Add feedbacks |
||
341 | $xmlContent .= ' <feedbacks>'.PHP_EOL; |
||
342 | foreach ($quizData['feedbacks'] as $feedback) { |
||
343 | $xmlContent .= ' <feedback id="'.$quizData['id'].'">'.PHP_EOL; |
||
344 | $xmlContent .= ' <feedbacktext>'.htmlspecialchars($feedback['feedbacktext']).'</feedbacktext>'.PHP_EOL; |
||
345 | $xmlContent .= ' <feedbacktextformat>1</feedbacktextformat>'.PHP_EOL; |
||
346 | $xmlContent .= ' <mingrade>'.$feedback['mingrade'].'</mingrade>'.PHP_EOL; |
||
347 | $xmlContent .= ' <maxgrade>'.$feedback['maxgrade'].'</maxgrade>'.PHP_EOL; |
||
348 | $xmlContent .= ' </feedback>'.PHP_EOL; |
||
349 | } |
||
350 | $xmlContent .= ' </feedbacks>'.PHP_EOL; |
||
351 | |||
352 | // Complete with placeholders for attempts and grades |
||
353 | $xmlContent .= ' <overrides>'.PHP_EOL.' </overrides>'.PHP_EOL; |
||
354 | $xmlContent .= ' <grades>'.PHP_EOL.' </grades>'.PHP_EOL; |
||
355 | $xmlContent .= ' <attempts>'.PHP_EOL.' </attempts>'.PHP_EOL; |
||
356 | |||
357 | // Close the activity tag |
||
358 | $xmlContent .= ' </quiz>'.PHP_EOL; |
||
359 | $xmlContent .= '</activity>'.PHP_EOL; |
||
360 | |||
361 | // Save the XML file |
||
362 | $xmlFile = $destinationDir.'/quiz.xml'; |
||
363 | if (file_put_contents($xmlFile, $xmlContent) === false) { |
||
364 | throw new Exception(get_lang('ErrorCreatingQuizXml')); |
||
365 | } |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Exports a multiple-choice question in XML format. |
||
370 | */ |
||
371 | private function exportMultichoiceQuestion(array $question): string |
||
372 | { |
||
373 | $xmlContent = ' <plugin_qtype_multichoice_question>'.PHP_EOL; |
||
374 | $xmlContent .= ' <answers>'.PHP_EOL; |
||
375 | foreach ($question['answers'] as $answer) { |
||
376 | $xmlContent .= $this->exportAnswer($answer); |
||
377 | } |
||
378 | $xmlContent .= ' </answers>'.PHP_EOL; |
||
379 | $xmlContent .= ' <multichoice id="'.($question['id'] ?? '0').'">'.PHP_EOL; |
||
380 | $xmlContent .= ' <layout>0</layout>'.PHP_EOL; |
||
381 | $xmlContent .= ' <single>1</single>'.PHP_EOL; |
||
382 | $xmlContent .= ' <shuffleanswers>1</shuffleanswers>'.PHP_EOL; |
||
383 | $xmlContent .= ' <correctfeedback>Your answer is correct.</correctfeedback>'.PHP_EOL; |
||
384 | $xmlContent .= ' <correctfeedbackformat>1</correctfeedbackformat>'.PHP_EOL; |
||
385 | $xmlContent .= ' <partiallycorrectfeedback>Your answer is partially correct.</partiallycorrectfeedback>'.PHP_EOL; |
||
386 | $xmlContent .= ' <partiallycorrectfeedbackformat>1</partiallycorrectfeedbackformat>'.PHP_EOL; |
||
387 | $xmlContent .= ' <incorrectfeedback>Your answer is incorrect.</incorrectfeedback>'.PHP_EOL; |
||
388 | $xmlContent .= ' <incorrectfeedbackformat>1</incorrectfeedbackformat>'.PHP_EOL; |
||
389 | $xmlContent .= ' <answernumbering>abc</answernumbering>'.PHP_EOL; |
||
390 | $xmlContent .= ' <shownumcorrect>1</shownumcorrect>'.PHP_EOL; |
||
391 | $xmlContent .= ' </multichoice>'.PHP_EOL; |
||
392 | $xmlContent .= ' </plugin_qtype_multichoice_question>'.PHP_EOL; |
||
393 | |||
394 | return $xmlContent; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Exports a multiple-choice question with single=0 in XML format. |
||
399 | */ |
||
400 | private function exportMultichoiceNosingleQuestion(array $question): string |
||
401 | { |
||
402 | // Similar structure to exportMultichoiceQuestion, but with single=0 |
||
403 | $xmlContent = str_replace('<single>1</single>', '<single>0</single>', $this->exportMultichoiceQuestion($question)); |
||
404 | |||
405 | return $xmlContent; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Exports a true/false question in XML format. |
||
410 | */ |
||
411 | private function exportTrueFalseQuestion(array $question): string |
||
412 | { |
||
413 | $xmlContent = ' <plugin_qtype_truefalse_question>'.PHP_EOL; |
||
414 | $xmlContent .= ' <answers>'.PHP_EOL; |
||
415 | foreach ($question['answers'] as $answer) { |
||
416 | $xmlContent .= $this->exportAnswer($answer); |
||
417 | } |
||
418 | $xmlContent .= ' </answers>'.PHP_EOL; |
||
419 | $xmlContent .= ' <truefalse id="'.($question['id'] ?? '0').'">'.PHP_EOL; |
||
420 | $xmlContent .= ' <trueanswer>'.($question['answers'][0]['id'] ?? '0').'</trueanswer>'.PHP_EOL; |
||
421 | $xmlContent .= ' <falseanswer>'.($question['answers'][1]['id'] ?? '0').'</falseanswer>'.PHP_EOL; |
||
422 | $xmlContent .= ' </truefalse>'.PHP_EOL; |
||
423 | $xmlContent .= ' </plugin_qtype_truefalse_question>'.PHP_EOL; |
||
424 | |||
425 | return $xmlContent; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Exports a short answer question in XML format. |
||
430 | */ |
||
431 | private function exportShortAnswerQuestion(array $question): string |
||
432 | { |
||
433 | $xmlContent = ' <plugin_qtype_shortanswer_question>'.PHP_EOL; |
||
434 | $xmlContent .= ' <answers>'.PHP_EOL; |
||
435 | foreach ($question['answers'] as $answer) { |
||
436 | $xmlContent .= $this->exportAnswer($answer); |
||
437 | } |
||
438 | $xmlContent .= ' </answers>'.PHP_EOL; |
||
439 | $xmlContent .= ' <shortanswer id="'.($question['id'] ?? '0').'">'.PHP_EOL; |
||
440 | $xmlContent .= ' <usecase>0</usecase>'.PHP_EOL; |
||
441 | $xmlContent .= ' </shortanswer>'.PHP_EOL; |
||
442 | $xmlContent .= ' </plugin_qtype_shortanswer_question>'.PHP_EOL; |
||
443 | |||
444 | return $xmlContent; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Exports a matching question in XML format. |
||
449 | */ |
||
450 | private function exportMatchQuestion(array $question): string |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Exports an answer in XML format. |
||
488 | */ |
||
489 | private function exportAnswer(array $answer): string |
||
498 | } |
||
499 | } |
||
500 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: