Example #1
0
 def GetBranchTarget(self, state):
     """Returns the identifier of the next item or section to branch to, or
     one of the pre-defined EXIT_* identifiers.  If there is no branch rule
     in effect then None is returned.  *state* is a
     :py:class:`variables.TestSessionState` instance used to evaluate the
     branch rule expressions."""
     test = self.find_parent(AssessmentTest)
     testPart = self.find_parent(TestPart)
     if testPart.navigationMode != NavigationMode.linear:
         return None
     for r in self.BranchRule:
         if r.Evaluate(state):
             try:
                 if r.target in (u"EXIT_SECTION", u"EXIT_TESTPART",
                                 u"EXIT_TEST"):
                     return r.target
                 target = test.GetPart(r.target)
                 if not isinstance(target, SectionPart):
                     # section parts can only point at other section parts
                     raise core.ProcessingError(
                         "Target of section or item branch rule is not a section or item: %s"
                         % r.target)
                 if target.find_parent(TestPart) is not testPart:
                     raise core.ProcessingError(
                         "Target or section or item branch rule is not in the same testPart: %s"
                         % r.target)
                 return r.target
             except KeyError:
                 raise core.ProcessingError(
                     "Target of section or item branch rule has not been declared: %s"
                     % r.target)
Example #2
0
 def Run(self, state):
     if self.Expression is None:
         raise core.ProcessingError(
             "setTemplateValue with missing expression")
     value = self.Expression.Evaluate(state)
     if state.IsTemplate(self.identifier):
         state[self.identifier] = value
     else:
         raise core.ProcessingError("Template variable required: %s" %
                                    self.identifier)
Example #3
0
 def Run(self, state):
     if self.Expression is None:
         raise core.ProcessingError(
             "setCorrectResponse with missing expression")
     value = self.Expression.Evaluate(state)
     d = state.GetDeclaration(self.identifier)
     if isinstance(d, variables.ResponseDeclaration):
         state[self.identifier + ".CORRECT"] = value
     elif state.IsResponse(self.identifier):
         raise core.ProcessingError(
             "Can't set the correct value of a built-in response %s" %
             self.identifier)
     else:
         raise core.ProcessingError("%s is not a response variable" %
                                    self.identifier)
Example #4
0
 def Run(self, state):
     if self.Expression is None:
         raise core.ProcessingError(
             "setDefaultValue with missing expression")
     value = self.Expression.Evaluate(state)
     d = state.GetDeclaration(self.identifier)
     if isinstance(d, variables.ResponseDeclaration) or isinstance(
             d, variables.OutcomeDeclaration):
         state[self.identifier + ".DEFAULT"] = value
     elif state.IsResponse(self.identifier) or state.IsOutcome(
             self.identifier):
         raise core.ProcessingError(
             "Can't set the correct value of a built-in variable %s" %
             self.identifier)
     else:
         raise core.ProcessingError(
             "%s is not a response or outcome variable" % self.identifier)
Example #5
0
 def Run(self, itemState, testState):
     """Updates the value of a template variable in *itemState* based on the
     values in *testState*."""
     if self.Expression is None:
         raise core.ProcessingError(
             "templateDefault with missing expression")
     value = self.Expression.Evaluate(testState)
     if self.templateIdentifier is None:
         print self
     try:
         d = itemState.GetDeclaration(self.templateIdentifier)
     except KeyError:
         raise core.ProcessingError("%s is not a variable" %
                                    self.templateIdentifier)
     if isinstance(d, variables.TemplateDeclaration):
         # we don't actually use the .DEFAULT form for template variables as they have
         # their values set directly.
         itemState[self.templateIdentifier] = value
     else:
         raise core.ProcessingError("%s is not a template variable" %
                                    self.templateIdentifier)
Example #6
0
    def Evaluate(self, state):
        """Evaluates the condition using the values in *state*.

        *	*state* is a :py:class:`~pyslet.qtiv2.variables.TestSessionState`
                instance."""
        if self.Expression is None:
            raise core.ProcessingError(
                "preCondition or branchRule with missing condition")
        value = self.Expression.Evaluate(state)
        variables.CheckBaseTypes(value.baseType, variables.BaseType.boolean)
        variables.CheckCardinalities(value.Cardinality(),
                                     variables.Cardinality.single)
        return value and value.value
Example #7
0
 def GetBranchTarget(self, state):
     """Returns the identifier of the testPart to branch to, or the
     pre-defined EXIT_TEST identifier.  If there is no branch rule in effect
     then None is returned.  *state* is a
     :py:class:`variables.TestSessionState` instance used to evaluate the
     branch rule expressions."""
     test = self.find_parent(AssessmentTest)
     for r in self.BranchRule:
         if r.Evaluate(state):
             try:
                 if r.target == u"EXIT_TEST":
                     return r.target
                 target = test.GetPart(r.target)
                 if not isinstance(target, TestPart):
                     # test parts can only point at other test parts
                     raise core.ProcessingError(
                         "Target of testPart branch rule is not a testPart: %s"
                         % r.target)
                 return r.target
             except KeyError:
                 raise core.ProcessingError(
                     "Target of testPart branch rule has not been declared: %s"
                     % r.target)
Example #8
0
    def Run(self, state):
        """Run this test and, if True, any resulting rules.

        Returns *True* if the condition evaluated to *True*."""
        if self.Expression is None:
            raise core.ProcessingError("templateIf with missing condition")
        value = self.Expression.Evaluate(state)
        variables.CheckBaseTypes(value.baseType, variables.BaseType.boolean)
        variables.CheckCardinalities(value.Cardinality(),
                                     variables.Cardinality.single)
        if value and value.value:
            for r in self.TemplateRule:
                r.Run(state)
            return True
        else:
            return False