[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/question/type/match/ -> edit_match_form.php (source)

   1  <?php
   2  /**
   3   * Defines the editing form for the match question type.
   4   *
   5   * @copyright &copy; 2007 Jamie Pratt
   6   * @author Jamie Pratt me@jamiep.org
   7   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   8   * @package questionbank
   9   * @subpackage questiontypes
  10   */
  11  
  12  /**
  13   * match editing form definition.
  14   */
  15  class question_edit_match_form extends question_edit_form {
  16      /**
  17       * Add question-type specific form fields.
  18       *
  19       * @param object $mform the form being built.
  20       */
  21      function definition_inner(&$mform) {
  22          $mform->addElement('advcheckbox', 'shuffleanswers', get_string('shuffle', 'quiz'), null, null, array(0,1));
  23          $mform->setHelpButton('shuffleanswers', array('matchshuffle', get_string('shuffle','quiz'), 'quiz'));
  24          $mform->setDefault('shuffleanswers', 1);
  25  
  26          $mform->addElement('static', 'answersinstruct', get_string('choices', 'quiz'), get_string('filloutthreequestions', 'quiz'));
  27          $mform->closeHeaderBefore('answersinstruct');
  28  
  29          $repeated = array();
  30          $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('questionno', 'quiz', '{no}'));
  31          $repeated[] =& $mform->createElement('textarea', 'subquestions', get_string('question', 'quiz'), array('cols'=>40, 'rows'=>3));
  32          $repeated[] =& $mform->createElement('text', 'subanswers', get_string('answer', 'quiz'), array('size'=>50));
  33  
  34          if (isset($this->question->options)){
  35              $countsubquestions = count($this->question->options->subquestions);
  36          } else {
  37              $countsubquestions = 0;
  38          }
  39          if ($this->question->formoptions->repeatelements){
  40              $repeatsatstart = (QUESTION_NUMANS_START > ($countsubquestions + QUESTION_NUMANS_ADD))?
  41                                  QUESTION_NUMANS_START : ($countsubquestions + QUESTION_NUMANS_ADD);
  42          } else {
  43              $repeatsatstart = $countsubquestions;
  44          }
  45          $mform->setType('subanswer', PARAM_TEXT);
  46          $mform->setType('subquestion', PARAM_TEXT);
  47  
  48          $this->repeat_elements($repeated, $repeatsatstart, array(), 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmoreqblanks', 'qtype_match'));
  49  
  50      }
  51  
  52      function set_data($question) {
  53          if (isset($question->options)){
  54              $subquestions = $question->options->subquestions;
  55              if (count($subquestions)) {
  56                  $key = 0;
  57                  foreach ($subquestions as $subquestion){
  58                      $default_values['subanswers['.$key.']'] = $subquestion->answertext;
  59                      $default_values['subquestions['.$key.']'] = $subquestion->questiontext;
  60                      $key++;
  61                  }
  62              }
  63              $default_values['shuffleanswers'] =  $question->options->shuffleanswers;
  64              $question = (object)((array)$question + $default_values);
  65          }
  66          parent::set_data($question);
  67      }
  68  
  69      function qtype() {
  70          return 'match';
  71      }
  72  
  73      function validation($data, $files) {
  74          $errors = parent::validation($data, $files);
  75          $answers = $data['subanswers'];
  76          $questions = $data['subquestions'];
  77          $questioncount = 0;
  78          foreach ($questions as $key => $question){
  79              $trimmedquestion = trim($question);
  80              $trimmedanswer = trim($answers[$key]);
  81              if ($trimmedanswer != '' && $trimmedquestion != ''){
  82                  $questioncount++;
  83              }
  84              if ($trimmedquestion != '' && $trimmedanswer == ''){
  85                  $errors['subanswers['.$key.']'] = get_string('nomatchinganswerforq', 'qtype_match', $trimmedquestion);
  86              }
  87          }
  88          if ($questioncount==0){
  89              $errors['subquestions[0]'] = get_string('notenoughquestions', 'qtype_match', 3);
  90              $errors['subquestions[1]'] = get_string('notenoughquestions', 'qtype_match', 3);
  91              $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
  92          } elseif ($questioncount==1){
  93              $errors['subquestions[1]'] = get_string('notenoughquestions', 'qtype_match', 3);
  94              $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
  95  
  96          } elseif ($questioncount==2){
  97              $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
  98          }
  99          return $errors;
 100      }
 101  }
 102  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7