function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     if ($this->col_count($data) == 0) {
         $errors['colshort[0]'] = qtype_matrix::get_string('mustdefine1by1');
     }
     if ($this->row_count($data) == 0) {
         $errors['rowshort[0]'] = qtype_matrix::get_string('mustdefine1by1');
     }
     $grading = qtype_matrix::grading($data['grademethod']);
     $grading_errors = $grading->validation($data);
     $errors = array_merge($errors, $grading_errors);
     return $errors ? $errors : true;
 }
 function add_matrix()
 {
     $mform = $this->_form;
     $data = $mform->exportValues();
     if (isset($_POST['colshort'])) {
         $cols_count = count($_POST['colshort']);
     } else {
         if (isset($this->question->options->cols) && count($this->question->options->cols) > 0) {
             $cols_count = count($this->question->options->cols);
         } else {
             $cols_count = self::DEFAULT_COLS;
         }
     }
     if ($add_cols = optional_param('add_cols', '', PARAM_TEXT)) {
         $cols_count++;
     }
     if (isset($_POST['rowshort'])) {
         $rows_count = count($_POST['rowshort']);
     } else {
         if (isset($this->question->options->rows) && count($this->question->options->rows) > 0) {
             $rows_count = count($this->question->options->rows);
         } else {
             $rows_count = self::DEFAULT_ROWS;
         }
     }
     if ($add_rows = optional_param('add_rows', '', PARAM_TEXT)) {
         $rows_count++;
     }
     $grademethod = isset($data['grademethod']) ? $data['grademethod'] : qtype_matrix::defaut_grading()->get_name();
     $grading = qtype_matrix::grading($grademethod);
     $multiple = isset($data['multiple']) ? $data['multiple'] : true;
     $matrix = array();
     $html = '<table class="quedit matrix"><thead><tr>';
     $html .= '<th></th>';
     $matrix[] = $this->create_static($html);
     for ($col = 0; $col < $cols_count; $col++) {
         $matrix[] = $this->create_static('<th>');
         $matrix[] = $this->create_text("colshort[{$col}]");
         $popup = $this->create_htmlpopup("collong[{$col}]", qtype_matrix::get_string('collong'));
         $matrix = array_merge($matrix, $popup);
         //            $id = "popcol$col";
         //            $matrix[] = $this->create_static('<a class="pbutton" href="#" onclick="mtrx_popup(\'' . $id . '\');return false;" >...</a>');
         //            $matrix[] = $this->create_static('<span id="' . $id . '" class="popup">');
         //            $matrix[] = $this->create_static('<a class="pbutton close" href="#" onclick="mtrx_popup(\'' . $id . '\');return false;" >&nbsp;&nbsp;&nbsp;</a>');
         //            $matrix[] = $this->create_static('<span class="title">');
         //            $matrix[] = $this->create_static(qtype_matrix::get_string('collong'));
         //            $matrix[] = $this->create_static('</span>');
         //            $matrix[] = $this->create_htmleditor("collong[$col]");
         //            $matrix[] = $this->create_hidden("colid[$col]");
         //            $matrix[] = $this->create_static('</span>');
         $matrix[] = $this->create_hidden("colid[{$col}]");
         $matrix[] = $this->create_static('</th>');
     }
     $matrix[] = $this->create_static('<th>');
     $matrix[] = $this->create_static(qtype_matrix::get_string('rowfeedback'));
     $matrix[] = $this->create_static('</th>');
     $matrix[] = $this->create_static('<th>');
     $matrix[] = $this->create_submit('add_cols', '  ', array('class' => 'button add'));
     $this->register_no_submit_button('add_cols');
     $matrix[] = $this->create_static('</th>');
     $matrix[] = $this->create_static('</tr></thead><tbody>');
     for ($row = 0; $row < $rows_count; $row++) {
         $matrix[] = $this->create_static('<tr>');
         $matrix[] = $this->create_static('<td>');
         $matrix[] = $this->create_text("rowshort[{$row}]");
         $popup = $this->create_htmlpopup("rowlong[{$row}]", qtype_matrix::get_string('rowlong'));
         $matrix = array_merge($matrix, $popup);
         $matrix[] = $this->create_hidden("rowid[{$row}]");
         //            $id = "poprow$row";
         //            $matrix[] = $this->create_static('<a class="pbutton" href="#" onclick="mtrx_popup(\'' . $id . '\');return false;" >...</a>');
         //            $matrix[] = $this->create_static('<span id="' . $id . '" class="popup">');
         //            $matrix[] = $this->create_static('<a class="pbutton close" href="#" onclick="mtrx_popup(\'' . $id . '\');return false;" >&nbsp;&nbsp;&nbsp;</a>');
         //            $matrix[] = $this->create_static('<span class="title">');
         //            $matrix[] = $this->create_static(qtype_matrix::get_string('rowlong'));
         //            $matrix[] = $this->create_static('</span>');
         //            $matrix[] = $this->create_htmleditor("rowlong[$row]");
         //            $matrix[] = $this->create_static('</span>');
         //            $matrix[] = $this->create_hidden("rowid[$row]");
         $matrix[] = $this->create_static('</td>');
         for ($col = 0; $col < $cols_count; $col++) {
             $matrix[] = $this->create_static('<td>');
             $cell_content = $grading->create_cell_element($mform, $row, $col, $multiple);
             $cell_content = $cell_content ? $cell_content : $this->create_static('');
             $matrix[] = $cell_content;
             $matrix[] = $this->create_static('</td>');
         }
         //            $id = "popfeedback$row";
         $matrix[] = $this->create_static('<td class="feedback">');
         $popup = $this->create_htmlpopup("rowfeedback[{$row}]", qtype_matrix::get_string('rowfeedback'));
         $matrix = array_merge($matrix, $popup);
         //            $matrix[] = $this->create_static('<a class="pbutton" href="#" onclick="mtrx_popup(\'' . $id . '\');return false;" >...</a>');
         //            $matrix[] = $this->create_static('<span id="' . $id . '" class="popup">');
         //            $matrix[] = $this->create_static('<a class="pbutton close" href="#" onclick="mtrx_popup(\'' . $id . '\');return false;" >&nbsp;&nbsp;&nbsp;</a>');
         //            $matrix[] = $this->create_static('<span class="title">');
         //            $matrix[] = $this->create_static(qtype_matrix::get_string('rowfeedback'));
         //            $matrix[] = $this->create_static('</span>');
         //            $matrix[] = $this->create_htmleditor("rowfeedback[$row]");
         //            $matrix[] = $this->create_static('</span>');
         $matrix[] = $this->create_static('</td>');
         $matrix[] = $this->create_static('<td></td>');
         $matrix[] = $this->create_static('</tr>');
     }
     $matrix[] = $this->create_static('<tr>');
     $matrix[] = $this->create_static('<td>');
     $matrix[] = $this->create_submit('add_rows', '  ', array('class' => 'button add'));
     $this->register_no_submit_button('add_rows');
     $matrix[] = $this->create_static('</td>');
     for ($col = 0; $col < $cols_count; $col++) {
         $matrix[] = $this->create_static('<td>');
         $matrix[] = $this->create_static('</td>');
     }
     $matrix[] = $this->create_static('</tr>');
     $matrix[] = $this->create_static('</tbody></table>');
     $matrixheader = $this->create_header('matrixheader');
     $matrix_group = $this->create_group('matrix', null, $matrix, '', false);
     if (isset($this['tagsheader'])) {
         $this->insert_element_before($matrixheader, 'tagsheader');
         $refresh_button = $this->create_submit('refresh_matrix');
         $this->register_no_submit_button('refresh_matrix');
         $this->disabled_if('refresh_matrix', 'grademethod', 'eq', 'none');
         $this->disabled_if('defaultgrade', 'grademethod', 'eq', 'none');
         $this->insert_element_before($refresh_button, 'tagsheader');
         $this->insert_element_before($matrix_group, 'tagsheader');
     } else {
         $this[] = $matrixheader;
         $refresh_button = $this->create_submit('refresh_matrix');
         $this->register_no_submit_button('refresh_matrix');
         $this->disabled_if('refresh_matrix', 'grademethod', 'eq', 'none');
         $this->disabled_if('defaultgrade', 'grademethod', 'eq', 'none');
         $this[] = $refresh_button;
         $this[] = $matrix_group;
     }
     if ($cols_count > 1) {
         $this->set_default('colshort[0]', qtype_matrix::get_string('true'));
         $this->set_default('colshort[1]', qtype_matrix::get_string('false'));
     }
 }
 /**
  *
  * @return qtype_matrix_grading
  */
 public function grading()
 {
     return qtype_matrix::grading($this->grademethod);
 }