[ Index ]

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

title

Body

[close]

/question/type/multianswer/ -> edit_multianswer_form.php (source)

   1  <?php
   2  /**
   3   * Defines the editing form for the multianswer 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   * multianswer editing form definition.
  14   */
  15  class question_edit_multianswer_form extends question_edit_form {
  16  
  17      //  $questiondisplay will contain the qtype_multianswer_extract_question from the questiontext
  18      var $questiondisplay ; 
  19  
  20      function definition_inner(&$mform) {
  21          $question_type_names = question_type_menu();
  22          $mform->addRule('questiontext', null, 'required', null, 'client');
  23          
  24          // Remove meaningless defaultgrade field.
  25          $mform->removeElement('defaultgrade');
  26       
  27           // display the questions from questiontext;
  28          if  (  "" != optional_param('questiontext','', PARAM_RAW)) {
  29          
  30              $this->questiondisplay = fullclone(qtype_multianswer_extract_question(optional_param('questiontext','', PARAM_RAW))) ;
  31              
  32          }else {
  33              $this->questiondisplay = "";
  34          }       
  35  
  36          if ( isset($this->questiondisplay->options->questions) && is_array($this->questiondisplay->options->questions) ) {
  37              $countsubquestions =0;
  38              foreach($this->questiondisplay->options->questions as $subquestion){
  39                  if ($subquestion != ''){
  40                     $countsubquestions++;
  41                  }
  42              } 
  43          } else {
  44              $countsubquestions =0;
  45          }
  46  
  47          $mform->addElement('submit', 'analyzequestion', get_string('decodeverifyquestiontext','qtype_multianswer'));
  48          $mform->registerNoSubmitButton('analyzequestion');
  49  
  50          for ($sub =1;$sub <=$countsubquestions ;$sub++) {
  51              $this->editas[$sub] =  'unknown type';
  52              if (isset( $this->questiondisplay->options->questions[$sub]->qtype) ) {
  53                  $this->editas[$sub] =  $this->questiondisplay->options->questions[$sub]->qtype ; 
  54              } else if (optional_param('sub_'.$sub."_".'qtype', '', PARAM_RAW) != '') {
  55                  $this->editas[$sub] = optional_param('sub_'.$sub."_".'qtype', '', PARAM_RAW);
  56              }
  57              $mform->addElement('header', 'subhdr', get_string('questionno', 'quiz',
  58                   '{#'.$sub.'}').'&nbsp;'.$question_type_names[$this->questiondisplay->options->questions[$sub]->qtype]);
  59  
  60              $mform->addElement('static', 'sub_'.$sub."_".'questiontext', "subquestiontext",array('cols'=>60, 'rows'=>3));
  61  
  62              if (isset ( $this->questiondisplay->options->questions[$sub]->questiontext)) {
  63                  $mform->setDefault('sub_'.$sub."_".'questiontext', $this->questiondisplay->options->questions[$sub]->questiontext);
  64      }
  65  
  66              $mform->addElement('static', 'sub_'.$sub."_".'defaultgrade', get_string('defaultgrade', 'quiz'));
  67              $mform->setDefault('sub_'.$sub."_".'defaultgrade',$this->questiondisplay->options->questions[$sub]->defaultgrade);
  68  
  69                  if ($this->questiondisplay->options->questions[$sub]->qtype =='multichoice'   ) {
  70                      $mform->addElement('static', 'sub_'.$sub."_".'layout', get_string('layout', 'quiz'),array('cols'=>60, 'rows'=>1)) ;//, $gradeoptions);
  71                  }
  72              foreach ($this->questiondisplay->options->questions[$sub]->answer as $key =>$ans) {
  73  
  74                 $mform->addElement('static', 'sub_'.$sub."_".'answer['.$key.']', get_string('answer', 'quiz'), array('cols'=>60, 'rows'=>1));
  75                  
  76                  if ($this->questiondisplay->options->questions[$sub]->qtype =='numerical' && $key == 0 ) {
  77                      $mform->addElement('static', 'sub_'.$sub."_".'tolerance['.$key.']', get_string('acceptederror', 'quiz')) ;//, $gradeoptions);
  78                  }    
  79  
  80                  $mform->addElement('static', 'sub_'.$sub."_".'fraction['.$key.']', get_string('grade')) ;//, $gradeoptions);
  81  
  82                  $mform->addElement('static', 'sub_'.$sub."_".'feedback['.$key.']', get_string('feedback', 'quiz'));
  83              } 
  84  
  85          }
  86  
  87      }
  88  
  89          
  90      function set_data($question) {
  91          $default_values =array();
  92          if (isset($question->id) and $question->id and $question->qtype and $question->questiontext) {
  93  
  94              foreach ($question->options->questions as $key => $wrapped) {
  95                  if($wrapped != ''){
  96                  // The old way of restoring the definitions is kept to gradually
  97                  // update all multianswer questions
  98                  if (empty($wrapped->questiontext)) {
  99                      $parsableanswerdef = '{' . $wrapped->defaultgrade . ':';
 100                      switch ($wrapped->qtype) {
 101                          case 'multichoice':
 102                              $parsableanswerdef .= 'MULTICHOICE:';
 103                              break;
 104                          case 'shortanswer':
 105                              $parsableanswerdef .= 'SHORTANSWER:';
 106                              break;
 107                          case 'numerical':
 108                              $parsableanswerdef .= 'NUMERICAL:';
 109                              break;
 110                          default:
 111                              print_error('unknownquestiontype', 'question', '', $wrapped->qtype);
 112                      }
 113                      $separator= '';
 114                      foreach ($wrapped->options->answers as $subanswer) {
 115                          $parsableanswerdef .= $separator
 116                                  . '%' . round(100*$subanswer->fraction) . '%';
 117                          $parsableanswerdef .= $subanswer->answer;
 118                          if (!empty($wrapped->options->tolerance)) {
 119                              // Special for numerical answers:
 120                              $parsableanswerdef .= ":{$wrapped->options->tolerance}";
 121                              // We only want tolerance for the first alternative, it will
 122                              // be applied to all of the alternatives.
 123                              unset($wrapped->options->tolerance);
 124                          }
 125                          if ($subanswer->feedback) {
 126                              $parsableanswerdef .= "#$subanswer->feedback";
 127                          }
 128                          $separator = '~';
 129                      }
 130                      $parsableanswerdef .= '}';
 131                      // Fix the questiontext fields of old questions
 132                      set_field('question', 'questiontext', addslashes($parsableanswerdef), 'id', $wrapped->id);
 133                  } else {
 134                      $parsableanswerdef = str_replace('&#', '&\#', $wrapped->questiontext);
 135                  }
 136                  $question->questiontext = str_replace("{#$key}", $parsableanswerdef, $question->questiontext);
 137              }
 138          }
 139          }
 140                  
 141          // set default to $questiondisplay questions elements
 142          if (isset($this->questiondisplay->options->questions)) {                
 143              $subquestions = fullclone($this->questiondisplay->options->questions) ;           
 144              if (count($subquestions)) {
 145                  $sub =1; 
 146                  foreach ($subquestions as $subquestion) {          
 147                      $prefix = 'sub_'.$sub.'_' ;
 148  
 149                      // validate parameters
 150                      $answercount = 0;
 151                      $maxgrade = false;
 152                      $maxfraction = -1;
 153                      if ($subquestion->qtype == 'multichoice' ) {
 154                          $default_values[$prefix.'layout']  = $subquestion->layout ;
 155                          switch ($subquestion->layout) {
 156                              case '0':
 157                                  $default_values[$prefix.'layout']= get_string('selectelement', 'qtype_multianswer');
 158                                  break;
 159                              case '1':
 160                                  $default_values[$prefix.'layout']= get_string('verticallayout', 'qtype_multianswer');
 161                                  break;                         
 162                              case '2':
 163                                  $default_values[$prefix.'layout']= get_string('horizontallayout', 'qtype_multianswer');
 164                                  break;
 165                              default:
 166                                  $default_values[$prefix.'layout']= get_string('unknownlayout', 'qtype_multianswer');
 167                          } 
 168                      }
 169                      foreach ($subquestion->answer as $key=>$answer) {
 170                          if ( $subquestion->qtype == 'numerical' && $key == 0 ) {
 171                              $default_values[$prefix.'tolerance['.$key.']']  = $subquestion->tolerance[0] ;
 172                          }
 173                          $trimmedanswer = trim($answer);
 174                          if ($trimmedanswer !== '') {
 175                              $answercount++;
 176                              if ($subquestion->qtype == 'numerical' && !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
 177                                  $this->_form->setElementError($prefix.'answer['.$key.']' , get_string('answermustbenumberorstar', 'qtype_numerical'));
 178                              }
 179                              if ($subquestion->fraction[$key] == 1) {
 180                                  $maxgrade = true;
 181                              }                       
 182                              if ($subquestion->fraction[$key] > $maxfraction) {
 183                                  $maxfraction = $subquestion->fraction[$key] ;
 184                              }
 185                          }
 186                                          
 187                          $default_values[$prefix.'answer['.$key.']']  = $answer;                         
 188                      }                                                     
 189                      if ($answercount == 0) {
 190                          if ($subquestion->qtype == 'multichoice' ) {
 191                              $this->_form->setElementError($prefix.'answer[0]' ,  get_string('notenoughanswers', 'qtype_multichoice', 2));
 192                          } else {
 193                              $this->_form->setElementError($prefix.'answer[0]' , get_string('notenoughanswers', 'quiz', 1));
 194                          }
 195                      }
 196                      if ($maxgrade == false) {
 197                          $this->_form->setElementError($prefix.'fraction[0]' ,get_string('fractionsnomax', 'question'));
 198                      }   
 199                      foreach ($subquestion->feedback as $key=>$answer) {
 200                          
 201                          $default_values[$prefix.'feedback['.$key.']']  = $answer;
 202                      }                                  
 203                         foreach ( $subquestion->fraction as $key=>$answer) {
 204                          $default_values[$prefix.'fraction['.$key.']']  = $answer;
 205                      }       
 206    
 207                   
 208                       $sub++;                     
 209                  }
 210              }
 211          }
 212             if( $default_values != "")   { 
 213              $question = (object)((array)$question + $default_values);
 214          }
 215          parent::set_data($question);
 216      }
 217  
 218      function validation($data, $files) {
 219          $errors = parent::validation($data, $files);
 220                   
 221          if (isset($this->questiondisplay->options->questions)) {
 222                  
 223             $subquestions = fullclone($this->questiondisplay->options->questions) ;           
 224              if (count($subquestions)) {
 225                  $sub =1; 
 226                  foreach ($subquestions as $subquestion) {          
 227                      $prefix = 'sub_'.$sub.'_' ;
 228                      $answercount = 0;
 229                      $maxgrade = false;
 230                      $maxfraction = -1;
 231                      foreach ( $subquestion->answer as $key=>$answer) {
 232                          $trimmedanswer = trim($answer);
 233                          if ($trimmedanswer !== '') {
 234                              $answercount++;
 235                              if ($subquestion->qtype =='numerical' && !(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
 236                                  $errors[$prefix.'answer['.$key.']']=  get_string('answermustbenumberorstar', 'qtype_numerical');
 237          }
 238                              if ($subquestion->fraction[$key] == 1) {
 239                                  $maxgrade = true;
 240          }
 241                              if ($subquestion->fraction[$key] > $maxfraction) {
 242                                  $maxfraction = $subquestion->fraction[$key] ;
 243                              }
 244                          }                                        
 245                      }                                                     
 246                      if ($answercount==0) {
 247                          if ( $subquestion->qtype =='multichoice' ) {
 248                              $errors[$prefix.'answer[0]']= get_string('notenoughanswers', 'qtype_multichoice', 2);
 249                          }else {
 250                              $errors[$prefix.'answer[0]'] = get_string('notenoughanswers', 'quiz', 1);
 251                          }
 252                      }
 253                      if ($maxgrade == false) {
 254                          $errors[$prefix.'fraction[0]']=get_string('fractionsnomax', 'question');
 255                      }   
 256                      $sub++;                     
 257                  }
 258              } else {
 259                  $errors['questiontext']=get_string('questionsmissing', 'qtype_multianswer');  
 260              }
 261          }
 262  
 263          return $errors;
 264      }
 265  
 266      function qtype() {
 267          return 'multianswer';
 268      }
 269  }
 270  ?>


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