Skip to content

Commit

Permalink
PHPUNIT tests and some tidying up
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Oct 16, 2023
1 parent 0ed7eed commit ff82354
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 59 deletions.
15 changes: 7 additions & 8 deletions edit_oumatrix_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function definition_inner($mform) {
'multiple' => get_string('answermodemultiple', 'qtype_oumatrix'),
];
$mform->addElement('select', 'inputtype', get_string('answermode', 'qtype_oumatrix'), $answermodemenu);
$mform->setDefault('inputtype', $this->get_default_value('single',
$mform->setDefault('inputtype', $this->get_default_value('inputtype',
get_config('qtype_oumatrix', 'inputtype')));

$grademethod = [
Expand All @@ -77,14 +77,14 @@ protected function definition_inner($mform) {
];
$mform->addElement('select', 'grademethod', get_string('grademethod', 'qtype_oumatrix'), $grademethod);
$mform->addHelpButton('grademethod', 'grademethod', 'qtype_oumatrix');
$mform->setDefault('grademethod', $this->get_default_value(
'grademethod', get_config('qtype_oumatrix', 'grademethod')));
$mform->setDefault('grademethod', $this->get_default_value('grademethod',
get_config('qtype_oumatrix', 'grademethod')));
$mform->disabledIf('grademethod', 'inputtype', 'eq', 'single');

$mform->addElement('selectyesno', 'shuffleanswers', get_string('shuffleanswers', 'qtype_oumatrix'));
$mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_oumatrix');
$mform->setDefault('shuffleanswers', $this->get_default_value(
'shuffleanswers', get_config('qtype_oumatrix', 'shuffleanswers')));
$mform->setDefault('shuffleanswers', $this->get_default_value('shuffleanswers',
get_config('qtype_oumatrix', 'shuffleanswers')));

// Add update field.
$mform->addElement('submit', 'updateform', get_string('updateform', 'qtype_oumatrix'));
Expand Down Expand Up @@ -242,8 +242,7 @@ private function data_preprocessing_rows($question) {
public function validation($data, $files) {
$errors = parent::validation($data, $files);

// Validate required number of min and max columns.
// Ignore the blank columns.
// Validate minimum required number of columns.
$filteredcolscount = count(array_filter($data['columnname']));
if ($filteredcolscount < column::MIN_NUMBER_OF_COLUMNS) {
$errors['columnname[' . $filteredcolscount . ']'] = get_string('notenoughanswercols', 'qtype_oumatrix',
Expand Down Expand Up @@ -281,7 +280,7 @@ public function validation($data, $files) {
}
}

// Validate required number of min and max rows.
// Validate minimum required number of rows.
$countrows = count(array_filter($data['rowname']));
if ($countrows < row::MIN_NUMBER_OF_ROWS) {
$errors['rowoptions[' . $countrows . ']'] = get_string('notenoughquestionrows', 'qtype_oumatrix',
Expand Down
89 changes: 61 additions & 28 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function get_question_options($question) {
/**
* Create a default options object for the provided question.
*
* @param object $question The queston we are working with.
* @param object $question The question we are working with.
* @return object The options object.
*/
protected function create_default_options($question) {
Expand Down Expand Up @@ -89,11 +89,6 @@ public function save_defaults_for_new_questions(stdClass $fromform): void {
$this->set_default_value('shownumcorrect', $fromform->shownumcorrect);
}

public function save_question($question, $form) {
$question = parent::save_question($question, $form);
return $question;
}

public function save_question_options($question) {
global $DB;
$context = $question->context;
Expand Down Expand Up @@ -124,11 +119,12 @@ public function save_question_options($question) {
}

/**
* Save the question columns and return a list of columns to be used in the save_rows function.
*
* @param object $question This holds the information from the editing form.
* @return array The list of columns created.
*/
public function save_columns($question) {
public function save_columns(object $question): array {
global $DB;
$numcolumns = count($question->columnname);
$columnslist = [];
Expand All @@ -146,14 +142,14 @@ public function save_columns($question) {
}

/**
* Save the question rows.
*
* @param object $question This holds the information from the editing form
* @param array $columnslist
*/
public function save_rows($question, $columnslist) {
public function save_rows(object $question, array $columnslist) {
global $DB;
$context = $question->context;
$result = new stdClass();
$numrows = count($question->rowname);

// Insert row input data.
Expand Down Expand Up @@ -186,7 +182,7 @@ public function save_rows($question, $columnslist) {

if ($question->feedback[$i]['text'] != '') {
$questionrow->feedback = $this->import_or_save_files($question->feedback[$i],
$context, 'qtype_oumatrix', 'feedback', $questionrow->id);
$context, 'qtype_oumatrix', 'feedback', $questionrow->id);
$questionrow->feedbackformat = $question->feedback[$i]['format'];

$DB->update_record('qtype_oumatrix_rows', $questionrow);
Expand Down Expand Up @@ -222,29 +218,67 @@ public function delete_question($questionid, $contextid) {
parent::delete_question($questionid, $contextid);
}

protected function get_num_correct_choices($questiondata) {
public function get_num_correct_choices($questiondata) {
$numright = 0;
foreach ($questiondata->rows as $row) {
$rowanwers = json_decode($row->correctanswers);
foreach ($rowanwers as $key => $value) {
if ((int) $value === 1) {
$numright += 1;
}
}
$numright += count((array)$rowanwers);
}
return $numright;
}

/**
* Return total number if choices for both (single, multiple) matrix choices.
* @param object $questiondata
* @return int
*/
public function get_total_number_of_choices(object $questiondata):? int {
// if rows or columns are not set return null;
if (sizeof($questiondata->columns) === 0 || sizeof($questiondata->rows) === 0) {
return null;
}
// Total number of choices for each row is the number of columns,
// therefore the total number of choices for the question is
return count($questiondata->columns) * count($questiondata->rows);
}

public function get_random_guess_score($questiondata) {
// We compute the randome guess score here on the assumption we are using
// the deferred feedback behaviour, and the question text tells the
// student how many of the responses are correct.
// Amazingly, the forumla for this works out to be
// # correct choices / total # choices in all cases.

//TODO: improve this.
return $this->get_num_correct_choices($questiondata) /
count($questiondata->rows);
if ($this->get_total_number_of_choices($questiondata) === null) {
return null;
}
return $this->get_num_correct_choices($questiondata) / $this->get_total_number_of_choices($questiondata);
}

public function get_possible_responses($questiondata) {
if ($questiondata->options->single) {
$responses = array();

// TODO: Sort out this funtion to work with rows and columns, etc.
foreach ($questiondata->options->answers as $aid => $answer) {
$responses[$aid] = new question_possible_response(
question_utils::to_plain_text($answer->answer, $answer->answerformat),
$answer->fraction);
}

$responses[null] = question_possible_response::no_response();
return array($questiondata->id => $responses);
} else {
$parts = array();

foreach ($questiondata->options->answers as $aid => $answer) {
$parts[$aid] = array($aid => new question_possible_response(
question_utils::to_plain_text($answer->answer, $answer->answerformat),
$answer->fraction));
}

return $parts;
}
}

/**
Expand Down Expand Up @@ -296,7 +330,7 @@ public function make_row($rowdata) {
explode(',', $rowdata->correctanswers), $rowdata->feedback, $rowdata->feedbackformat);
}

public function import_from_xml($data, $question, qformat_xml $format, $extra = null) {
public function import_from_xml($data, $question, qformat_xml $format, $extra = null): object {
if (!isset($data['@']['type']) || $data['@']['type'] != 'oumatrix') {
return false;
}
Expand Down Expand Up @@ -337,7 +371,7 @@ public function import_from_xml($data, $question, qformat_xml $format, $extra =
return $question;
}

public function import_columns(qformat_xml $format, stdClass $question, array $columns): void {
public function import_columns(qformat_xml $format, stdClass $question, array $columns) {
foreach ($columns as $column) {
static $indexno = 0;
$question->columns[$indexno]['name'] =
Expand All @@ -350,7 +384,7 @@ public function import_columns(qformat_xml $format, stdClass $question, array $c
}
}

public function import_rows(qformat_xml $format, stdClass $question, array $rows): void {
public function import_rows(qformat_xml $format, stdClass $question, array $rows) {
foreach ($rows as $row) {
static $indexno = 0;
$question->rows[$indexno]['id'] = $format->getpath($row, ['@', 'key'], $indexno);
Expand Down Expand Up @@ -448,7 +482,7 @@ public function move_files($questionid, $oldcontextid, $newcontextid) {
$fs = get_file_storage();

parent::move_files($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_rowanswers($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_row_feedback($questionid, $oldcontextid, $newcontextid);
$this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);

$fs->move_area_files_to_new_context($oldcontextid,
Expand All @@ -460,15 +494,14 @@ public function move_files($questionid, $oldcontextid, $newcontextid) {
}

/**
* Move all the files belonging to each rows question answers when the question
* is moved from one context to another.
* Move all the feedback files belonging to each sub-question
* when the question is moved from one context to another.
*
* @param int $questionid the question being moved.
* @param int $oldcontextid the context it is moving from.
* @param int $newcontextid the context it is moving to.
*/
protected function move_files_in_rowanswers($questionid, $oldcontextid,
$newcontextid) {
protected function move_files_in_row_feedback(int $questionid, int $oldcontextid, int $newcontextid) {
global $DB;
$fs = get_file_storage();

Expand Down Expand Up @@ -497,7 +530,7 @@ protected function delete_files($questionid, $contextid) {
* @param int $questionid the question being deleted.
* @param int $contextid the context the question is in.
*/
protected function delete_files_in_row_feedback($questionid, $contextid) {
protected function delete_files_in_row_feedback(int $questionid, int $contextid) {
global $DB;
$fs = get_file_storage();

Expand Down
Loading

0 comments on commit ff82354

Please sign in to comment.