示例#1
0
 /**
  * @dataProvider impactedVariablesProvider
  * 
  * @param TemplateProcessing $templateProcessing
  * @param array $expectedIdentifiers
  */
 public function testImpactedVariables(TemplateProcessing $templateProcessing, array $expectedIdentifiers)
 {
     $this->assertEquals($expectedIdentifiers, Utils::templateProcessingImpactedVariables($templateProcessing));
 }
 /**
  * Process the template processing.
  * 
  * @throws \qtism\runtime\common\ProcessingException If an error occurs while executing the TemplateProcessing.
  */
 public function process()
 {
     $context = $this->getContext();
     $templateProcessing = $this->getComponent();
     $trialCount = 0;
     // Make a copy of possibly impacted variables.
     $impactedVariables = Utils::templateProcessingImpactedVariables($templateProcessing);
     $tplVarCopies = array();
     foreach ($impactedVariables as $varIdentifier) {
         $tplVarCopies[] = clone $context->getVariable($varIdentifier);
     }
     do {
         $validConstraints = true;
         try {
             $trialCount++;
             foreach ($templateProcessing->getTemplateRules() as $rule) {
                 $processor = $this->getRuleProcessorFactory()->createProcessor($rule);
                 $processor->setState($context);
                 $processor->process();
                 $this->trace($rule->getQtiClassName() . ' executed.');
             }
         } catch (RuleProcessingException $e) {
             if ($e->getCode() === RuleProcessingException::EXIT_TEMPLATE) {
                 $this->trace('Termination of template processing.');
             } else {
                 if ($e->getCode() === RuleProcessingException::TEMPLATE_CONSTRAINT_UNSATISFIED) {
                     $this->trace('Unsatisfied template constraint.');
                     // Reset variables with their originals.
                     foreach ($tplVarCopies as $copy) {
                         $context->getVariable($copy->getIdentifier())->setValue($copy->getDefaultValue());
                         $context->getVariable($copy->getIdentifier())->setDefaultValue($copy->getDefaultValue());
                         if ($copy instanceof ResponseVariable) {
                             $context->getVariable($copy->getIdentifier())->setCorrectResponse($copy->getCorrectResponse());
                         }
                     }
                     $validConstraints = false;
                 } else {
                     throw $e;
                 }
             }
         }
     } while ($validConstraints === false && $trialCount < 100);
 }