Example #1
0
  /**
   * Load XML Quiz moodle format.
   *
   * @param url url of the XML format of an moodle quiz
   * @return true if import success false otherwise
   */
  public final boolean load(final String url) {
    boolean bImportOK = true;
    /* Clear all the question */
    questions.clear();
    /* Load the XML document */
    bImportOK = xmlImporter.load(url);

    /* Import */
    if (bImportOK && (!privLoadQuiz())) {
      questions.clear();
      bImportOK = false;
    }
    /* Load completed */
    return bImportOK;
  }
Example #2
0
  /**
   * Load a XML quiz.
   *
   * @return true if success false otherwise
   */
  private boolean privLoadQuiz() {
    /* Variable */
    boolean bLoadOk = true;
    Category currentCategory = null;
    /* Check that the root element is "quiz" */
    final Element root = xmlImporter.getRootElement();

    if (!(xmlImporter.isElementName(root, "quiz"))) {
      xmlImporter.getErrors().addBadElement(root, "quiz");
      bLoadOk = false;
    }

    /* Load all the questions if OK */
    final Elements childs = root.getChildElements();
    for (int i = 0; (bLoadOk) && (i < childs.size()); i++) {
      final Element child = childs.get(i);

      if (xmlImporter.isElementName(child, "question")) {

        final String str = child.getAttributeValue("type");
        if (str.equalsIgnoreCase("category")) {
          /* Load category */
          currentCategory = doImportCategory(child);
          /* Check if loaded */
          if (currentCategory == null) {
            bLoadOk = false;
            break;
          }
        } else {
          final QuestionType type = questions.stringToquestionType(str);
          if (type == null) {
            xmlImporter
                .getErrors()
                .addAttrError(
                    child,
                    "type",
                    str,
                    "multichoice|truefalse|"
                        + "shortanswer|matching|cloze|"
                        + "description|calculated|"
                        + "numerical|essay|category");
          } else {
            /* Create the question */
            final AbstractQuestion ques = questions.createQuestion(type);
            ques.setCategory(currentCategory);
            /* Load the question */
            if (!ques.doImport(xmlImporter, child)) {
              bLoadOk = false;
              break;
            }
            /* Add the question */
            questions.add(ques);
          }
        }

      } else {
        xmlImporter.getWarnings().addUnknonwElement(child);
      }
    }
    /* Return */
    return bLoadOk;
  }
Example #3
0
 /**
  * Add a question directly.
  *
  * @param type type of the question
  * @return the question added
  */
 public final AbstractQuestion addQuestion(final QuestionType type) {
   final AbstractQuestion ques = questions.createQuestion(type);
   questions.add(ques);
   return ques;
 }
Example #4
0
 /**
  * Allow the usage of java for-each syntax with question.
  *
  * @return a valid iterator for question
  */
 public final Iterator<AbstractQuestion> iterator() {
   return questions.iterator();
 }