diff --git a/classes/column.php b/classes/column.php index afafce9..847a6ed 100644 --- a/classes/column.php +++ b/classes/column.php @@ -32,6 +32,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class column { + const MIN_NUMBER_OF_COLUMNS = 2; + /** @var int The id of the question. */ public $questionid; diff --git a/classes/row.php b/classes/row.php index dd20785..5e86bd8 100644 --- a/classes/row.php +++ b/classes/row.php @@ -32,6 +32,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class row { + const MIN_NUMBER_OF_ROWS = 2; + /** @var int The id of the question. */ public $questionid; diff --git a/edit_oumatrix_form.php b/edit_oumatrix_form.php index f1d795a..0afdc4b 100644 --- a/edit_oumatrix_form.php +++ b/edit_oumatrix_form.php @@ -22,9 +22,8 @@ * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -//namespace gtype_oumatirx; -use \qtype_oumatirx\row; -use \qtype_oumatirx\column; +use qtype_oumatrix\row; +use qtype_oumatrix\column; /** * Editing form for the oumatrix question type. @@ -238,6 +237,13 @@ private function data_preprocessing_rows($question) { public function validation($data, $files) { $errors = parent::validation($data, $files); + print_object($data); + //if (count($data->columns) < column::MIN_NUMBER_OF_COLUMNS) { + // $errors['columnname'] = get_string('noduplication', 'qtype_oumatix'); + //} + if (count($data->rowname) < row::MIN_NUMBER_OF_ROWS) { + $errors['columnname'] = get_string('noduplication', 'qtype_oumatix'); + } //print_object($data->columnname); // Check for duplicated column name. @@ -300,7 +306,7 @@ protected function add_per_column_fields(object $mform, string $label, $this->repeat_elements($this->get_per_column_fields($mform, $label, $repeatedoptions), $repeatsatstart, $repeatedoptions, 'nocolumns', 'addcolumns', $addoptions, - $this->get_more_blanks('columns'), true); + get_string('addmoreblanks', 'qtype_oumatrix', 'columns'), true); } protected function get_per_column_fields($mform, $label, $repeatedoptions) { @@ -334,16 +340,18 @@ protected function add_per_row_fields(object $mform, string $label, $this->repeat_elements($this->get_per_row_fields($mform, $label, $repeatedoptions), $repeatsatstart, $repeatedoptions, 'norows', 'addrows', $addoptions, - $this->get_more_blanks('rows'), true); + get_string('addmoreblanks', 'qtype_oumatrix', 'rows'), true); } /** + * Returns a row object with relevant input fields. + * * @param object $mform * @param string $label - * @param array $repeatedoptions reference to array of repeated options to fill + * @param array $repeatedoptions * @return array */ - protected function get_per_row_fields(MoodleQuickForm $mform, string $label, array &$repeatedoptions): array { + protected function get_per_row_fields(object $mform, string $label, array &$repeatedoptions): array { $repeated = []; $rowoptions = []; $rowoptions[] = $mform->createElement('text', 'rowname', '', ['size' => 40]); @@ -372,12 +380,4 @@ protected function get_per_row_fields(MoodleQuickForm $mform, string $label, arr $repeatedoptions['rowname']['type'] = PARAM_RAW; return $repeated; } - - /** - * Language string to use for 'Add {no} more {rows or columns}'. - */ - protected function get_more_blanks(string $string) { - return get_string('addmoreblanks', 'qtype_oumatrix', $string); - } - } diff --git a/questiontype.php b/questiontype.php index 625f9fe..b1f72de 100644 --- a/questiontype.php +++ b/questiontype.php @@ -38,11 +38,6 @@ * in various formats. */ class qtype_oumatrix extends question_type { - - const MIN_NUMBER_OF_columns = 2; - const MIN_NUMBER_OF_ROWS = 2; - - public function get_question_options($question) { global $DB, $OUTPUT;; parent::get_question_options($question); @@ -138,8 +133,8 @@ public function save_columns($formdata) { $numcolumns = count($formdata->columnname); // Check if the question has the minimum number of colunms. - if ($numcolumns < self::MIN_NUMBER_OF_columns) { - $result->error = get_string('notenoughanswercols', 'qtype_oumatrix', self::MIN_NUMBER_OF_columns); + if ($numcolumns < column::MIN_NUMBER_OF_COLUMNS) { + $result->error = get_string('notenoughanswercols', 'qtype_oumatrix', column::MIN_NUMBER_OF_COLUMNS); return $result; } $columnslist = []; @@ -185,8 +180,8 @@ public function save_rows($question, $columnslist) { $numrows = count($question->rowname); // Check if the question has the minimum number of rows. - if ($numrows < self::MIN_NUMBER_OF_ROWS) { - $result->error = get_string('notenoughquestionrows', 'qtype_oumatrix', self::MIN_NUMBER_OF_ROWS); + if ($numrows < row::MIN_NUMBER_OF_ROWS) { + $result->error = get_string('notenoughquestionrows', 'qtype_oumatrix', row::MIN_NUMBER_OF_ROWS); return $result; } @@ -205,15 +200,15 @@ public function save_rows($question, $columnslist) { $questionrow->name = $question->rowname[$i]; // Prepare correct answers. for ($c = 0; $c < count($columnslist); $c++) { - if ($question->inputtype == 'multiple') { + if ($question->inputtype == 'single') { + $columnindex = preg_replace("/[^0-9]/", "", $question->rowanswers[$i]); + $answerslist[$columnslist[$columnindex - 1]->id] = "1"; + } else { $rowanswerslabel = "rowanswers" . 'a' . ($c + 1); if (!isset($question->$rowanswerslabel) || !array_key_exists($i, $question->$rowanswerslabel)) { continue; } $answerslist[$columnslist[$c]->id] = $question->$rowanswerslabel[$i]; - } else { - $columnindex = preg_replace("/[^0-9]/", "", $question->rowanswers[$i]); - $answerslist[$columnslist[$columnindex - 1]->id] = "1"; } } $questionrow->correctanswers = json_encode($answerslist); diff --git a/tests/helper.php b/tests/helper.php index 661a2dc..abf0fc5 100644 --- a/tests/helper.php +++ b/tests/helper.php @@ -434,126 +434,4 @@ public static function get_oumatrix_question_form_data_oumatrix_pultiple() { public function get_test_question_data($witch) { return \test_question_maker::get_question_data('oumatrix', $witch); } - - // TODO: following methods are copied from the old marix and may need some chnages. - /** - * - * @return qtype_oumatrix_question - */ - //public function make_oumatrix_question_single() { - // $result = $this->make_oumatrix_question(); - // $result->multiple = false; - // return $result; - //} - // - ///** - // * - // * @return qtype_oumatrix_question - // */ - //public function make_matrix_question_multiple() { - // $result = $this->make_matrix_question(); - // $result->multiple = true; - // return $result; - //} - // - ///** - // * - // * @return qtype_matrix_question - // */ - //public function make_matrix_question_any() { - // $result = $this->make_matrix_question(); - // $result->grademethod = 'any'; - // return $result; - //} - // - ///** - // * - // * @return qtype_matrix_question - // */ - //public function make_matrix_question_none() { - // $result = $this->make_matrix_question(); - // $result->grademethod = 'none'; - // return $result; - //} - // - ///** - // * - // * @return qtype_matrix_question - // */ - //public function make_matrix_question_weighted() { - // $result = $this->init_matrix_question(); - // - // for ($r = 0; $r < 4; $r++) { - // $row = (object) array(); - // $row->id = $r; - // $row->shorttext = "Row $r"; - // $row->description = "Description $r"; - // $row->feedback = "Feedback $r"; - // $result->rows[$r] = $row; - // for ($c = 0; $c < 4; $c++) { - // $col = (object) array(); - // $col->id = $c; - // $col->shorttext = "Column $c"; - // $col->description = "Description $c"; - // $result->cols[$c] = $col; - // - // $result->weights[$r][$c] = ($c < 2) ? 0.5 : 0; - // } - // } - // - // $result->grademethod = 'weighted'; - // $result->multiple = true; - // - // return $result; - //} - // - ///** - // * - // * @return qtype_oumatrix_question - // */ - //protected function make_oumatrix_question() { - // $result = $this->init_matrix_question(); - // - // for ($r = 0; $r < 4; $r++) { - // $row = (object) array(); - // $row->id = $r; - // $row->shorttext = "Row $r"; - // $row->description = "Description $r"; - // $row->feedback = "Feedback $r"; - // $result->rows[$r] = $row; - // for ($c = 0; $c < 4; $c++) { - // $col = (object) array(); - // $col->id = $c; - // $col->shorttext = "Column $c"; - // $col->description = "Description $c"; - // $result->cols[$c] = $col; - // - // $result->weights[$r][$c] = ($c == 0) ? 1 : 0; - // } - // } - // - // $result->grademethod = 'kprime'; - // $result->multiple = true; - // - // return $result; - //} - ///** - // * Initialise an OU Matrix question. - // * - // * @return \qtype_oumatrix_question - // */ - //public function init_oumatrix_question(): qtype_oumatrix_question { - // question_bank::load_question_definition_classes('oumatrix'); - // $result = new qtype_oumatrix_question(); - // test_question_maker::initialise_a_question($result); - // $result->name = 'OU Matrix question'; - // $result->questiontext = 'Single partical graded question.'; - // $result->generalfeedback = 'First column is true.'; - // $result->penalty = 0.3333333; - // $result->qtype = question_bank::get_qtype('oumatrix'); - // - // $result->rows = []; - // $result->cols = []; - // return $result; - //} }