Skip to content

Commit

Permalink
REmove the cols and rows from the opstions object
Browse files Browse the repository at this point in the history
 Enhancing automated testing
  • Loading branch information
mkassaei committed Sep 22, 2023
1 parent 827c887 commit c0e2033
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 151 deletions.
2 changes: 1 addition & 1 deletion classes/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class column {
* @param int $number
* @param string $name
*/
public function __construct(int $questionid = 0, int $number = 0, string $name = '', int $id = 0) {
public function __construct(int $questionid, int $number = 0, string $name = '', int $id = 0) {
$this->questionid = $questionid;
$this->number = $number;
$this->name = $name;
Expand Down
43 changes: 28 additions & 15 deletions edit_oumatrix_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ protected function set_current_settings(): void {

$columns = optional_param_array('columnname', '', PARAM_TEXT);
$this->numcolumns = $columns ? count($columns) : self::COL_NUM_START;
if (isset($this->question->options->columns)) {
$this->numcolumns = count($this->question->options->columns);
if (isset($this->question->columns)) {
$this->numcolumns = count($this->question->columns);
}
}

Expand Down Expand Up @@ -171,11 +171,11 @@ function data_preprocessing_options($question) {
* @return object The modified data.
*/
private function data_preprocessing_columns($question) {
if (empty($question->options->columns)) {
if (empty($question->columns)) {
return $question;
}
$question->columnname = [];
foreach ($question->options->columns as $column) {
foreach ($question->columns as $column) {
if (trim($column->name ?? '') === '') {
continue;
}
Expand All @@ -193,15 +193,15 @@ private function data_preprocessing_columns($question) {
*/
private function data_preprocessing_rows($question) {
// preprocess rows.
if (empty($question->options->rows)) {
if (empty($question->rows)) {
return $question;
}
$key = 0;
$question->rowname = [];
foreach ($question->options->rows as $index => $row) {
foreach ($question->rows as $index => $row) {
$question->rowname[] = $row->name;
$decodedanswers = json_decode($row->correctanswers, true);
foreach ($question->options->columns as $key => $column) {
foreach ($question->columns as $key => $column) {
if (array_key_exists($column->id, $decodedanswers)) {
$columnvalue = 'a' . $column->number + 1;
if ($question->options->inputtype == 'single') {
Expand All @@ -228,15 +228,28 @@ private function data_preprocessing_rows($question) {
);
$feedback[$key]['itemid'] = $feedbackdraftitemid;
$feedback[$key]['format'] = $row->feedbackformat ?? FORMAT_HTML;
$question->options->rows[$index]->feedbackformat = $feedback[$key]['format'];
$question->options->rows[$index]->feedback = $feedback[$key]['text'];
$question->rows[$index]->feedbackformat = $feedback[$key]['format'];
$question->rows[$index]->feedback = $feedback[$key]['text'];
$question->feedback[] = $feedback[$key];
$key++;
}
$question->feedback = $feedback;
return $question;
}

public function validation($data, $files) {
$errors = parent::validation($data, $files);
//print_object($data->columnname);

// Check for duplicated column name.
//$colnames = array_unique((array)$data->columnname);
//if (sizeof($colnames) < sizeof($data->columnname)) {
// $errors['columnname'] = get_string('noduplication', 'qtype_oumatix');
//}

return $errors;
}

protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
[$repeated, $repeatedoptions] = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect);
$repeatedoptions['hintclearwrong']['disabledif'] = ['single', 'eq', 1];
Expand Down Expand Up @@ -313,8 +326,8 @@ protected function add_per_row_fields(object $mform, string $label,
$mform->setExpanded('rowshdr', 1);
$repeatedoptions = [];

if (isset($this->question->options->rows)) {
$repeatsatstart = count($this->question->options->rows);
if (isset($this->question->rows)) {
$repeatsatstart = count($this->question->rows);
} else {
$repeatsatstart = $minoptions;
}
Expand All @@ -340,19 +353,19 @@ protected function get_per_row_fields(MoodleQuickForm $mform, string $label, arr
$rowanswerlistlabel = ($this->inputtype === 'single') ?
get_string('correctanswer', 'qtype_oumatrix') :
get_string('correctanswers', 'qtype_oumatrix');
$mform->setType('correctanswerlist', PARAM_RAW);
// TODO: To find a better way tio display $rowanswerlistlabel string after the rowname.

// Get the list answer input type (radio buttons or checkboxes).
for ($i = 0; $i < $this->numcolumns; $i++) {
$anslabel = get_string('a', 'qtype_oumatrix', $i+1);
$columnvalue = 'a' . $i + 1;
if ($this->inputtype === 'single') {
$rowanswerlist[] = $mform->createElement('radio', 'rowanswers', '', $anslabel, $columnvalue);
$rowoptions[] = $mform->createElement('radio', 'rowanswers', '', $anslabel, $columnvalue);
} else {
$rowanswerlist[] = $mform->createElement('checkbox', "rowanswers$columnvalue", '', $anslabel);
$rowoptions[] = $mform->createElement('checkbox', "rowanswers$columnvalue", '', $anslabel);
}
}
$rowoptions[] = $mform->createElement('group', 'rowanswerlist', $rowanswerlistlabel, $rowanswerlist, null, false);
//$rowoptions[] = $mform->createElement('group', 'rowanswerlist', $rowanswerlistlabel, $rowanswerlist, null, false);
$repeated[] = $mform->createElement('group', 'rowoptions', $label, $rowoptions, null, false);
$repeated[] = $mform->createElement('editor', 'feedback',
get_string('feedback', 'question'), ['rows' => 2], $this->editoroptions);
Expand Down
8 changes: 0 additions & 8 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,6 @@ public function is_choice_selected($response, $rowkey, $colkey) {
}
}

/*public function prepare_simulated_post_data($simulatedresponse) {
print_object("simulated response&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
print_object($simulatedresponse);
return $simulatedresponse;
}*/

public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload);
}
Expand Down Expand Up @@ -496,8 +490,6 @@ protected function field(int $rowkey, int $columnkey): string {
}

public function prepare_simulated_post_data($simulatedresponse) {
print_object("simulated response&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
print_object($simulatedresponse);
return $simulatedresponse;
}

Expand Down
19 changes: 11 additions & 8 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function get_question_options($question) {
if (!$question->options = $DB->get_record('qtype_oumatrix_options', ['questionid' => $question->id])) {
$question->options = $this->create_default_options($question);
}
if (!$question->options->columns = $DB->get_records('qtype_oumatrix_columns', ['questionid' => $question->id])) {
if (!$question->columns = $DB->get_records('qtype_oumatrix_columns', ['questionid' => $question->id])) {
echo $OUTPUT->notification('Error: Missing question columns!');
return false;
}
if (!$question->options->rows = $DB->get_records('qtype_oumatrix_rows', ['questionid' => $question->id])) {
if (!$question->rows = $DB->get_records('qtype_oumatrix_rows', ['questionid' => $question->id])) {
echo $OUTPUT->notification('Error: Missing question rows!');
return false;
}
Expand Down Expand Up @@ -177,6 +177,7 @@ public function save_columns($formdata) {
* @throws dml_exception
*/
public function save_rows($question, $columnslist) {
print_object($question);
global $DB;
$context = $question->context;
$result = new stdClass();
Expand Down Expand Up @@ -273,7 +274,7 @@ public function delete_question($questionid, $contextid) {

protected function get_num_correct_choices($questiondata) {
$numright = 0;
foreach ($questiondata->options->rows as $row) {
foreach ($questiondata->rows as $row) {
$rowanwers = json_decode($row->correctanswers);
foreach ($rowanwers as $key => $value) {
if ((int) $value === 1) {
Expand All @@ -293,7 +294,7 @@ public function get_random_guess_score($questiondata) {

//TODO: improve this.
return $this->get_num_correct_choices($questiondata) /
count($questiondata->options->rows);
count($questiondata->rows);
}

public function get_possible_responses($questiondata) {
Expand All @@ -315,14 +316,16 @@ public function get_possible_responses($questiondata) {
* @param object $questiondata the question data loaded from the database.
*/
protected function initialise_question_rows(question_definition $question, $questiondata) {
if (!empty($questiondata->options->rows)) {
foreach ($questiondata->options->rows as $row) {
print_object($questiondata);
print_object('-------------------------- $questiondata');
if (!empty($questiondata->rows)) {
foreach ($questiondata->rows as $row) {
$newrow = $this->make_row($row);
if ($newrow->correctanswers != '') {
$correctAnswers = [];
$todecode = implode(",", $newrow->correctanswers);
$decodedanswers = json_decode($todecode, true);
foreach($questiondata->options->columns as $key => $column) {
foreach($questiondata->columns as $key => $column) {
if ($decodedanswers != null && array_key_exists($column->id, $decodedanswers)) {
if ($questiondata->options->inputtype == 'single') {
$anslabel = 'a' . $column->number+1;
Expand All @@ -345,7 +348,7 @@ protected function initialise_question_rows(question_definition $question, $ques
* @param object $questiondata the question data loaded from the database.
*/
protected function initialise_question_columns(question_definition $question, $questiondata) {
$question->columns = $questiondata->options->columns;
$question->columns = $questiondata->columns;
}

protected function make_column($columndata) {
Expand Down
88 changes: 48 additions & 40 deletions tests/behat/add.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,60 @@ Feature: Test creating a Matrix question
| user | course | role |
| teacher | C1 | editingteacher |

@javascript
# @javascript
Scenario: Create a Matrix question with single choice option
When I am on the "Course 1" "core_question > course question bank" page logged in as teacher
Given I am on the "Course 1" "core_question > course question bank" page logged in as teacher
When I add a "Matrix" question filling the form with:
| Question name | Matrix-001 |
| Question text | Choose a correct answer for each row. |
| General feedback | Salmon(Fish), Chickern(Bird), Lamb(Mammal) |
| id_status | Ready |
| id_defaultmark | 1 |
| id_inputtype | single |
| id_columnname_0 | Salmon |
| id_columnname_1 | Chicken |
| id_columnname_2 | Lamb |
| id_rowname_0 | Is a fish |
| id_rowanswers_0_a1 | 1 |
| id_rowname_1 | Is a bird |
| id_rowanswers_1_a2 | 1 |
| id_rowname_2 | Is a mammal |
| id_rowanswers_2_a3 | 1 |
| For any correct response | Correct feedback |
| For any partially correct response | Partially correct feedback. |
| For any incorrect response | Incorrect feedback. |
| Hint 1 | First hint |
| Hint 2 | Second hint |
Then I should see "Matrix-001"

@javascript
Scenario: Create a Matrix question with multiple response option
Given I am on the "Course 1" "core_question > course question bank" page logged in as teacher
And I add a "Matrix" question filling the form with:
| Question name | Matrix-001 |
| Question text | Choose the answer from the options provided |
| General feedback | Bird, fish and mammal |
| Question name | Matrix-mulriple-002 |
| Question text | Choose a correct answers for each row. |
| General feedback | One and Three, Two, Two and Three |
| id_status | Ready |
| id_defaultmark | 1 |
| Answer mode | Single choice |
| Marking mode | Give partial credit |
| id_columnname_0 | salmon |
| id_columnname_1 | chicken |
| id_columnname_2 | lamb |
| id_rowname_0 | Is a fish |
| id_rowanswers_0_A1 | 1 |
| id_rowname_1 | Is a bird |
| id_rowanswers_1_A2 | 1 |
| id_rowname_2 | Is a mammal |
| id_rowanswers_2_A3 | 1 |
| id_inputtype | multiple |
| id_grademethod | partial |
When I press "id_updateform"
And I pause
And I set the following fields to these values:
| id_columnname_0 | One |
| id_columnname_1 | Two |
| id_columnname_2 | Three |
| id_rowname_0 | Is an odd number |
| id_rowanswersa1_0 | 1 |
| id_rowanswersa3_1 | 1 |
| id_rowname_1 | Is an even number |
| id_rowanswersa2_1 | 1 |
| id_rowname_2 | Is a prime number |
| id_rowanswersa2_2 | 1 |
| id_rowanswersa3_2 | 1 |
| For any correct response | Correct feedback |
| For any partially correct response | Partially correct feedback. |
| For any incorrect response | Incorrect feedback. |
| Hint 1 | First hint |
| Hint 2 | Second hint |
Then I should see "Matrix-001"

@javascript
Scenario: Create a Matrix question with multiple response option
When I am on the "Course 1" "core_question > course question bank" page logged in as teacher
And I add a "Matrix" question filling the form with:
| Question name | Matrix-002 |
| Question text | You can choose multiple answers |
| Default mark | 1 |
| Answer mode | Multiple response |
| id_answer_0 | 1 |
| id_answer_1 | 2 |
| id_answer_2 | 1 |
| id_startrow_0 | Copper |
| id_startrow_1 | Plastic |
| id_startrow_2 | Gold |
| id_startcolumn_0 | Is a good electrical conductor |
| id_startcolumn_1 | Is an insulator |
| For any correct response | Correct feedback |
| For any partially correct response | Partially correct feedback. |
| For any incorrect response | Incorrect feedback. |
| Hint 1 | First hint |
| Hint 2 | Second hint |
Then I should see "Matrix-001"
Then I should see "Matrix-mulriple-002"
Loading

0 comments on commit c0e2033

Please sign in to comment.