Skip to content

Commit

Permalink
Changes to save column id for answers
Browse files Browse the repository at this point in the history
  • Loading branch information
AnupamaSarjoshi committed Sep 12, 2023
1 parent 2887b52 commit 96a0f4e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 295 deletions.
81 changes: 1 addition & 80 deletions classes/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
125 changes: 0 additions & 125 deletions classes/row.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

27 changes: 18 additions & 9 deletions edit_oumatrix_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -192,21 +194,24 @@ 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;
}
$key = 0;
$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;
Expand All @@ -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];
Expand Down
21 changes: 1 addition & 20 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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)];
}
Expand Down
Loading

0 comments on commit 96a0f4e

Please sign in to comment.