[ 'longPrefix' => 'wetRun', 'required' => false, 'description' => 'Wet run', 'defaultValue' => 0 ] ]; } /** * @return Report * @throws \common_Exception * @throws \common_exception_Error */ protected function run() { try { $this->init(); } catch (\Exception $e) { return new Report(Report::TYPE_ERROR, $e->getMessage()); } $this->recompileItems(); return $this->report; } /** * Initialize parameters */ protected function init() { $this->wetRun = (bool) $this->getOption('wetRun'); $this->report = new Report(Report::TYPE_INFO, 'Starting recompile deliveries'); } /** * Recompile items * * @throws \common_Exception * @throws \common_exception_Error */ protected function recompileItems() { $extManager = $this->getServiceLocator()->get(\common_ext_ExtensionsManager::SERVICE_ID); if ($extManager->isInstalled('taoDeliveryRdf') === true && $extManager->isEnabled('taoDeliveryRdf') === true) { $compiledDeliveryClass = $this->getClass(DeliveryAssemblyService::CLASS_URI); if ($compiledDeliveryClass->exists() === true) { foreach ($compiledDeliveryClass->getInstances(true) as $compiledDelivery) { try { $this->getAssessmentsFromDelivery($compiledDelivery); } catch (\Exception $e) { $this->report->add( new Report( Report::TYPE_WARNING, "Delivery {$compiledDelivery->getUri()} was skipped with message: '{$e->getMessage()}'" ) ); } } } } else { $this->report->add( new Report( Report::TYPE_WARNING, "Extension taoDeliveryRdf is not installed. No compilation environment is available." ) ); } } /** * Get assessmentItemRef from delivery * * @param \core_kernel_classes_Resource $compiledDelivery * * @throws \common_Exception * @throws \common_exception_Error */ protected function getAssessmentsFromDelivery(\core_kernel_classes_Resource $compiledDelivery) { /** @var UnionAssignmentService $unionAssignmentService */ $unionAssignmentService = $this->getServiceLocator()->get(UnionAssignmentService::SERVICE_ID); $runtime = $unionAssignmentService->getRuntime($compiledDelivery); $inputParameters = \tao_models_classes_service_ServiceCallHelper::getInputValues($runtime, []); $testDefinition = \taoQtiTest_helpers_Utils::getTestDefinition($inputParameters['QtiTestCompilation']); $assessmentItemRefs = $testDefinition->getComponentsByClassName('assessmentItemRef'); $this->report->add( new Report( Report::TYPE_INFO, "Starting to recompile items for delivery {$compiledDelivery->getLabel()} with identifier {$compiledDelivery->getUri()}:" ) ); $count = 0; /** @var AssessmentItemRef $assessmentItemRef */ foreach ($assessmentItemRefs as $assessmentItemRef) { $directoryIds = explode('|', $assessmentItemRef->getHref()); $item = $this->getResource($directoryIds[0]); $properties = []; foreach ($item->getRdfTriples() as $triple) { $properties[$triple->predicate] = $triple->object; } if ($properties) { $directory = \tao_models_classes_service_FileStorage::singleton()->getDirectoryById($directoryIds[2]); $languages = $item->getUsedLanguages( $this->getProperty(\taoItems_models_classes_ItemsService::PROPERTY_ITEM_CONTENT) ); foreach ($languages as $lang) { $path = $lang . DIRECTORY_SEPARATOR . QtiJsonItemCompiler::METADATA_FILE_NAME; if (!$directory->has($path)) { $this->writeMetadata($item, $directory, $path, $properties); $count++; } } } } $this->report->add(new Report(Report::TYPE_INFO, "Was updated {$count} items.")); } /** * @param \core_kernel_classes_Resource $item * @param \tao_models_classes_service_StorageDirectory $directory * @param $path * @param $properties * @throws \common_Exception * @throws \common_exception_Error */ protected function writeMetadata( \core_kernel_classes_Resource $item, \tao_models_classes_service_StorageDirectory $directory, $path, $properties ) { if ($this->wetRun === true) { $directory->write( $path, json_encode($properties) ); $this->report->add( new Report( Report::TYPE_SUCCESS, "Metadata for assessmentItemRef '{$item->getLabel()}' with identifier '{$item->getUri()}' was successfully compiled." ) ); } else { $this->report->add( new Report( Report::TYPE_SUCCESS, "Metadata for assessmentItemRef '{$item->getLabel()}' with identifier '{$item->getUri()}' will successfully compiled." ) ); } } }