diff --git a/classes/column.php b/classes/column.php index 3dd0055..e1eb2d2 100644 --- a/classes/column.php +++ b/classes/column.php @@ -52,89 +52,10 @@ class column { * @param int $number * @param string $name */ - public function __construct(int $id = 0, int $questionid = 0, int $number = 0, string $name = '') { + public function __construct(int $questionid = 0, int $number = 0, string $name = '', int $id = 0) { $this->questionid = $questionid; $this->number = $number; $this->name = $name; $this->id = $id; - $this->column = $this->populate(); - } - - public function populate(): ?stdClass { - if ($this->questionid && $this->name) { - $column = new stdClass(); - $column->questionid = $this->questionid; - $column->number = $this->number; - $column->name = $this->name; - return $column; - } - return null; - } - - /** - * Return a column object - * - * @param string $name - * @return stdClass - * @throws \dml_exception - */ - public function get_column_by_name(string $name): ?stdClass { - if ($name === $this->name) { - return $this->column; - } - return null; - } - - /** - * Create a column. - * - * @param int $questionid - * @param int $number - * @param string $name - * @return int - */ - public function create_a_column(int $questionid, int $number, string $name): int { - global $DB; - $id = $DB->insert_record('qtype_oumatrix_columns', - ['questionid' => $questionid, 'number' => $number,'name' => $name ]); - return $id; - } - - /** - * Return column id - * - * @return int - */ - public function get_id(): int { - return $this->id; - } - - /** - * Return column number - * - * @return int - */ - public function get_number(): int { - return $this->number; - } - - /** - * Return column name - * - * @return string - */ - public function get_name(): string { - return $this->name; - } - - /** - * Delete a column. - * - * @param int $id - * @return void - */ - public function delete_a_column(int $id) { - global $DB; - $DB->delete_records('qtype_oumatrix_columns', ['questionid' => $this->questionid, 'id' => $id]); } } diff --git a/classes/row.php b/classes/row.php index f72f890..dd20785 100644 --- a/classes/row.php +++ b/classes/row.php @@ -71,130 +71,5 @@ public function __construct(int $id = 0, int $questionid = 0, int $number = 0, s $this->feedbackformat = $feedbackformat; $this->id = $id; } - - /** - * Return a row - * - * @param int $id - * @return stdClass - */ - public function get_a_row_by_id(int $id): ?stdClass { - global $DB; - if ($row = $DB->get_record('qtype_oumatrix_rows', ['id' => $id])) { - return $row; - } - return null; - } - - public function create_default_row(int $questionid, int $number = 1, string $name = 'row', array $correctanswers = [], - string $feedback = '', int $feedbackformat = 0) { - global $DB; - $row = new stdClass(); - $row->questionid = $questionid; - $row->number = $number; - $row->name = $name; - $row->correctanswers = json_encode($correctanswers); - $row->feedback = $feedback; - $row->feedbackformat = $feedbackformat; - return $row; - } - - public function create_row(int $questionid, int $number = 1, string $name = 'row', array $correctanswers = [], - string $feedback = '', int $feedbackformat = 0) { - $row = new stdClass(); - $row->questionid = $questionid; - $row->number = $number; - $row->name = $name; - $row->correctanswers = json_encode($correctanswers); - $row->feedback = $feedbackformat; - $row->feedbackformat = $feedbackformat; - return $row; - } - - /** - * @param array $correctanswers - */ - public function setCorrectanswers(array $correctanswers): void { - $this->correctanswers = $correctanswers; - } - - /** - * Retunr a row. - * - * @param int $rownumber - * @return stdClass - * @throws \dml_exception - */ - public function get_a_row(int $rownumber): ?stdClass { - global $DB; - if ($row = $DB->get_record('qtype_oumatrix_rows', ['questionid' => $this->questionid, 'number' => $rownumber])) { - return $row; - } - return null; - } - - /** - * Delete a row. - * - * @param int $rownumber - * @return void - * @throws \dml_exception - */ - public function delete_a_row(int $rownumber) { - global $DB; - $DB->delete_records('qtype_oumatrix_rows', ['questionid' => $this->questionid, 'number' => $rownumber]); - } - - /** - * @return int - */ - public function get_id(): int { - return $this->id; - } - - /** - * @return int - */ - public function get_number(): int { - return $this->number; - } - - /** - * @return string - */ - public function get_name(): string { - return $this->name; - } - - /** - * @return array - */ - public function get_correctanswers(): array { - return $this->correctanswers; - } - - /** - * @return string - */ - public function get_feedback(): string { - return $this->feedback; - } - - /** - * @return int - */ - public function get_feedbackformat(): int { - return $this->feedbackformat; - } - - /** - * Retun number of row. - * - * @return int - */ - public function get_number_of_rows() { - return $this->numberofrows; - } - } diff --git a/edit_oumatrix_form.php b/edit_oumatrix_form.php index 344e1b1..f7fb15a 100644 --- a/edit_oumatrix_form.php +++ b/edit_oumatrix_form.php @@ -130,7 +130,8 @@ protected function set_current_settings(): void { $this->grademethod = $grademethod; $columns = optional_param_array('columnname', '', PARAM_TEXT); - $this->numcolumns = $columns ? count($columns) : self::COL_NUM_START; + $this->numcolumns = $columns ? count($columns) : + ($this->question->options->columns ? count($this->question->options->columns) : self::COL_NUM_START); } /** @@ -146,6 +147,7 @@ public function data_preprocessing($question) { $question = parent::data_preprocessing($question); $question = $this->data_preprocessing_combined_feedback($question, true); $question = $this->data_preprocessing_hints($question, true, true); + $question = $this->data_preprocessing_options($question); $question = $this->data_preprocessing_columns($question); $question = $this->data_preprocessing_rows($question); @@ -192,6 +194,7 @@ private function data_preprocessing_columns($question) { * @return object The modified data. */ private function data_preprocessing_rows($question) { + // preprocess rows. if (empty($question->options->rows)) { return $question; } @@ -199,14 +202,16 @@ private function data_preprocessing_rows($question) { $question->rowname = []; foreach ($question->options->rows as $index => $row) { $question->rowname[] = $row->name; - if ($question->options->inputtype == 'single') { - $question->rowanswers[] = $row->correctanswers; - } else { - $decodedanswers = json_decode($row->correctanswers, true); - foreach ($question->options->columns as $key => $column) { - $anslabel = get_string('a', 'qtype_oumatrix', $column->number + 1); - $rowanswerslabel = 'rowanswers' . $anslabel; - $question->$rowanswerslabel[$row->number] = $decodedanswers[$column->name]; + $decodedanswers = json_decode($row->correctanswers, true); + foreach ($question->options->columns as $key => $column) { + if (array_key_exists($column->id, $decodedanswers)) { + if ($question->options->inputtype == 'single') { + $anslabel = get_string('a', 'qtype_oumatrix', $column->number + 1); + $question->rowanswers[] = $anslabel; + } else { + $rowanswerslabel = "rowanswers" . $column->number; + $question->$rowanswerslabel[$row->number] = $decodedanswers[$column->id]; + } } } $itemid = (int)$row->id ?? null; @@ -230,8 +235,12 @@ private function data_preprocessing_rows($question) { $key++; } $question->feedback = $feedback; + + //$this->data_preprocessing_columns($question); + //$this->data_preprocessing_rows($question); return $question; } + protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) { [$repeated, $repeatedoptions] = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect); $repeatedoptions['hintclearwrong']['disabledif'] = ['single', 'eq', 1]; diff --git a/question.php b/question.php index 4cee2f9..5d1dc0a 100644 --- a/question.php +++ b/question.php @@ -304,26 +304,17 @@ protected function field(int $rowkey, int $columnkey = 0): string { } public function get_correct_response(): ?array { - print_object("get_correct_response"); - print_object($this); $response = []; foreach ($this->rows as $row) { if ($row->correctanswers != '') { - $answer = (int)substr($row->correctanswers[0], 1); - $response[$this->field($row->number)] = $this->columns[$answer - 1]->name; + $response[$this->field($row->number)] = $this->columns[array_key_first($row->correctanswers)]->name; } } - print_object("====================================="); - print_object($response); return $response; } public function summarise_response(array $response): ?string { $responsewords = []; - print_object("summarise_response"); - print_object($response); - print_object($this); - foreach ($this->rows as $row) { $fieldname = $this->field($row->number); if (array_key_exists($fieldname, $response) && $response[$fieldname]) { @@ -349,16 +340,6 @@ public function get_validation_error(array $response): string { } public function grade_response(array $response): array { - // Retrieve a number of right answers and total answers. - //[$numrightparts, $total] = $this->get_num_parts_right($response); - // // //// Retrieve a number of wrong accent numbers. - // // //$numpartialparts = $this->get_num_parts_partial($response); - // // //// Calculate fraction. - // // //$fraction = ($numrightparts + $numpartialparts - $numpartialparts * $this->accentpenalty) - // // // / $total; - // // // - // // //return [$fraction, question_state::graded_state_for_fraction($fraction)]; - $fraction = 1; return [$fraction, question_state::graded_state_for_fraction($fraction)]; } diff --git a/questiontype.php b/questiontype.php index b779d72..cf6ecdb 100644 --- a/questiontype.php +++ b/questiontype.php @@ -125,8 +125,8 @@ public function save_question_options($question) { $options = $this->save_combined_feedback_helper($options, $question, $context, true); $DB->update_record('qtype_oumatrix_options', $options); - $this->save_columns($question); - $this->save_rows($question); + $columnslist = $this->save_columns($question); + $this->save_rows($question, $columnslist); $this->save_hints($question, true); } @@ -142,7 +142,7 @@ public function save_columns($formdata) { $result->error = get_string('notenoughanswercols', 'qtype_oumatrix', self::MIN_NUMBER_OF_columns); return $result; } - + $columnslist = []; // Insert column input data. for ($i = 0; $i < $numcolumns; $i++) { if (trim(($formdata->columnname[$i]) ?? '') === '') { @@ -151,11 +151,9 @@ public function save_columns($formdata) { // Update an existing word if possible. $column = array_shift($oldcolumns); if (!$column) { - $column = new stdClass(); - $column->questionid = $formdata->id; - $column->number = $i; - $column->name = $formdata->columnname[$i]; + $column = new column($formdata->id, $i, $formdata->columnname[$i]); $column->id = $DB->insert_record('qtype_oumatrix_columns', $column); + $columnslist[] = $column; } // Remove old columns. @@ -167,18 +165,19 @@ public function save_columns($formdata) { $DB->delete_records_select('qtype_oumatrix_columns', "id $idssql", $idsparams); } } + return $columnslist; } /** * * @param object $question This holds the information from the editing form + * @param array $columnslist * @return void * @throws coding_exception * @throws dml_exception */ - public function save_rows($question) { + public function save_rows($question, $columnslist) { global $DB; - $context = $question->context; $result = new stdClass(); $oldrows = $DB->get_records('qtype_oumatrix_rows', ['questionid' => $question->id], 'id ASC'); $numrows = count($question->rowname); @@ -191,6 +190,7 @@ public function save_rows($question) { // Insert row input data. for ($i = 0; $i < $numrows; $i++) { + $answerslist = []; if (trim($question->rowname[$i] ?? '') === '') { continue; } @@ -202,27 +202,33 @@ public function save_rows($question) { $questionrow->number = $i; $questionrow->name = $question->rowname[$i]; // Prepare correct answers. - if($question->inputtype == 'multiple') { - for ($c = 0; $c < count($question->columnname); $c++) { - if ($question->columnname[$c] === '') { - continue; - } - $rowanswerslabel = "rowanswers" . 'a' . ($c + 1); - - if (!array_key_exists($i, $question->$rowanswerslabel)) { - $answerslist[$question->columnname[$c]] = "0"; - continue; + if ($question->inputtype == 'multiple') { + //for ($c = 0; $c < count($question->columnname); $c++) { + // if ($question->columnname[$c] === '') { + // continue; + // } + // $rowanswerslabel = "rowanswers" . 'a' . ($c + 1); + // + // if (!array_key_exists($i, $question->$rowanswerslabel)) { + // $answerslist[$question->columnname[$c]] = "0"; + for ($c = 0; $c < count($columnslist); $c++) { + if ($question->inputtype == 'multiple') { + $rowanswerslabel = "rowanswers" . 'a' . ($c + 1); + if ($question->columnname[$c] === '' || !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"; } - $answerslist[$question->columnname[$c]] = $question->$rowanswerslabel[$i]; } $questionrow->correctanswers = json_encode($answerslist); - } else { - $questionrow->correctanswers = $question->rowanswers[$i] ?? ''; + $questionrow->feedback = $question->feedback[$i]['text']; + $questionrow->feedbackitemid = $question->feedback[$i]['itemid']; + $questionrow->feedbackformat = FORMAT_HTML; + $questionrow->id = $DB->insert_record('qtype_oumatrix_rows', $questionrow); } - $questionrow->feedback = $question->feedback[$i]['text']; - $questionrow->feedbackitemid = $question->feedback[$i]['itemid']; - $questionrow->feedbackformat = FORMAT_HTML; - $questionrow->id = $DB->insert_record('qtype_oumatrix_rows', $questionrow); } } // Remove old rows. @@ -316,20 +322,21 @@ protected function initialise_question_rows(question_definition $question, $ques if (!empty($questiondata->options->rows)) { foreach ($questiondata->options->rows as $row) { $newrow = $this->make_row($row); - - if ($newrow->get_correctanswers() != '') { - $correctAnswers = $newrow->get_correctanswers(); - if( $questiondata->options->inputtype == 'multiple') { - $correctAnswers = []; - $todecode = implode(",", $newrow->get_correctanswers()); - $decodedanswers = json_decode($todecode, true); - foreach($questiondata->options->columns as $key => $column) { - if ($decodedanswers != null && array_key_exists($column->name, $decodedanswers)) { - $correctAnswers[$column->number] = $decodedanswers[$column->name]; + if ($newrow->correctanswers != '') { + $correctAnswers = []; + $todecode = implode(",", $newrow->correctanswers); + $decodedanswers = json_decode($todecode, true); + foreach($questiondata->options->columns as $key => $column) { + if ($decodedanswers != null && array_key_exists($column->id, $decodedanswers)) { + if ($questiondata->options->inputtype == 'single') { + $anslabel = 'a' . $column->number+1; + $correctAnswers[$column->id] = $anslabel; + } else { + $correctAnswers[$column->id] = $decodedanswers[$column->id]; } } } - $newrow->setCorrectanswers($correctAnswers); + $newrow->correctanswers = $correctAnswers; } $question->rows[] = $newrow; } @@ -342,18 +349,15 @@ 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) { - if (!empty($questiondata->options->columns)) { - foreach ($questiondata->options->columns as $column) { - $question->columns[] = $this->make_column($column); - } - } + $question->columns = $questiondata->options->columns; } protected function make_column($columndata) { - return new column($columndata->id, $columndata->questionid, $columndata->number, $columndata->name); + return new column($columndata->questionid, $columndata->number, $columndata->name, $columndata->id); } public function make_row($rowdata) { + // Need to explode correctanswers as it is in the string format. return new row($rowdata->id, $rowdata->questionid, $rowdata->number, $rowdata->name, explode(',',$rowdata->correctanswers), $rowdata->feedback, $rowdata->feedbackformat); } diff --git a/renderer.php b/renderer.php index 7f097f9..0e93204 100644 --- a/renderer.php +++ b/renderer.php @@ -62,16 +62,8 @@ protected abstract function prompt(); public function formulation_and_controls(question_attempt $qa, question_display_options $options) { - print_object("Inside renderer.... question "); - print_object($qa); $question = $qa->get_question(); - //$response = $question->get_response($qa); $response = $qa->get_last_qt_data(); - print_object("response &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); - print_object($response); - //print_object("renderer.... question "); - //print_object("renderer.... $response ".$response); - //print_object("renderer.... $qa ".$qa); $inputname = $qa->get_qt_field_name('answer'); $inputattributes = array( @@ -232,27 +224,25 @@ public function formulation_and_controls(question_attempt $qa, public function get_matrix(question_attempt $qa) { $question = $qa->get_question(); - print_object("*******************************************"); - print_object($question); $response = $qa->get_last_qt_data(); - print_object("*******************************************"); - print_object($response); $caption = "Matrix question"; $colname[] = null; $table = " -
"; + $index = 0; foreach ($question->columns as $key => $value) { - $colname[$key] = $value->get_name(); - $table .= " | $colname[$key] | "; + $colname[$index] = $value->name; + $table .= "$colname[$index] | "; + $index += 1; } $table .= "
---|---|---|
$rowname | "; for ($j = 0; $j < count($colname); $j++) { @@ -351,8 +341,7 @@ public function correct_response(question_attempt $qa) { $right = []; foreach ($question->rows as $row) { if ($row->correctanswers != '') { - $answer = (int)substr($row->correctanswers[0], 1); - $right[] = $row->name . " => " . $question->columns[$answer - 1]->name; + $right[] = $row->name . " => " . $question->columns[array_key_first($row->correctanswers)]->name; } } return $this->correct_choices($right);