public function convert(item $item, array $questions) { // Make sure we clean up the log LogService::flush(); // Try to build the identifier using item `reference` // Otherwise, generate an alternative identifier and store the original reference as `label` $itemReference = $item->get_reference(); $itemIdentifier = Format::isIdentifier($itemReference, false) ? $itemReference : 'ITEM_' . StringUtil::generateRandomString(12); if ($itemReference !== $itemIdentifier) { LogService::log("The item `reference` ({$itemReference}) is not a valid identifier, thus can not be used for `assessmentItem` identifier. " . "Replaced it with randomly generated `{$itemIdentifier}` and stored the original `reference` as `label` attribute"); } $builder = new AssessmentItemBuilder(); $assessmentItem = $builder->build($itemIdentifier, $itemReference, $questions, $item->get_content()); $xml = new XmlDocument(); $xml->setDocumentComponent($assessmentItem); // Flush out all the error messages stored in this static class, also ensure they are unique $messages = array_values(array_unique(LogService::flush())); return [$xml->saveToString(true), $messages]; }
private function map(Question $question) { $type = $question->get_type(); if (!in_array($type, Constants::$supportedQuestionTypes)) { throw new MappingException("Question type `{$type}` not yet supported to be mapped to QTI"); } $clazz = new \ReflectionClass(self::MAPPER_CLASS_BASE . ucfirst($type . 'Mapper')); $questionTypeMapper = $clazz->newInstance(); // Try to use question `reference` as identifier // Otherwise, generate an alternative identifier and store the original reference as `label` to be passed in $questionReference = $question->get_reference(); $interactionIdentifier = Format::isIdentifier($questionReference, false) ? $questionReference : strtoupper($type) . '_' . StringUtil::generateRandomString(12); if ($interactionIdentifier !== $questionReference) { LogService::log("The question `reference` ({$questionReference}) is not a valid identifier. " . "Replaced it with randomly generated `{$interactionIdentifier}` and stored the original `reference` as `label` attribute"); } $result = $questionTypeMapper->convert($question->get_data(), $interactionIdentifier, $questionReference); $result[] = $questionTypeMapper->getExtraContent(); return $result; }