Skip to content

Commit

Permalink
Some minor tidying up
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Sep 28, 2023
1 parent 4834b80 commit 946d2d4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 150 deletions.
2 changes: 2 additions & 0 deletions classes/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions classes/row.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
30 changes: 15 additions & 15 deletions edit_oumatrix_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
}

}
21 changes: 8 additions & 13 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
122 changes: 0 additions & 122 deletions tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
//}
}

0 comments on commit 946d2d4

Please sign in to comment.