Skip to content

Commit

Permalink
Changes to save checkbox answers
Browse files Browse the repository at this point in the history
  • Loading branch information
AnupamaSarjoshi committed Aug 16, 2023
1 parent d317b24 commit 4461272
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 71 deletions.
8 changes: 4 additions & 4 deletions classes/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
*/
class column {
/** @var int The id of the question. */
private $questionid;
public $questionid;

/** @var int The column id. */
private $id;
public $id;

/** @var int The column number. */
private $number;
public $number;

/** @var string The column name. */
private $name;
public $name;

/**
* Construct the column object.
Expand Down
14 changes: 7 additions & 7 deletions classes/row.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@
*/
class row {
/** @var int The id of the question. */
private $questionid;
public $questionid;

/** @var int The row id. */
private $id;
public $id;

/** @var int The row number. */
private $number;
public $number;

/** @var string The row name. */
private $name;
public $name;

/** @var array The list of correct answers A json-encoded list of correct answerids for a given row. */
private $correctanswers = [];
public $correctanswers = [];

/** @var string The row specific feedback. */
private $feedback;
public $feedback;

/** @var int The row feedback format. E.g: FORMAT_HTML, FORMAT_PLAIN */
private $feedbackformat;
public $feedbackformat;

/**
* Construct the matrix object to be used by rows and colums objects.
Expand Down
22 changes: 8 additions & 14 deletions edit_oumatrix_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,7 @@ protected function data_preprocessing_rows($question) {
if ($question->options->inputtype == 'single') {
$question->rowanswers[] = $row->correctanswers;
} else {
$anslabel = get_string('a', 'qtype_oumatrix', $row->number + 1);
$rowanswerslabel = "rowanswers".$anslabel;
$question->$rowanswerslabel = $this->format_correct_answers($row->correctanswers, $question);
$this->format_correct_answers_multiple($row->number, $row->correctanswers, $question);
}
$itemid = (int)$row->id ?? null;

Expand All @@ -252,18 +250,14 @@ protected function data_preprocessing_rows($question) {
return $question;
}

protected function format_correct_answers($answers, $question) :array {
$correctAnswers = [];
if (!empty($answers)) {
$decodedanswers = json_decode($answers, true);
foreach ($question->options->columns as $key => $column) {
if ($decodedanswers != null && array_key_exists($column->name, $decodedanswers)) {
$correctAnswers[$column->number] = $decodedanswers[$column->name];
}
}
protected function format_correct_answers_multiple($rownumber, $answers, $question) {
$decodedanswers = json_decode($answers, true);
foreach ($question->options->columns as $key => $column) {
$anslabel = get_string('a', 'qtype_oumatrix', $column->number + 1);
$rowanswerslabel = "rowanswers".$anslabel;
$question->$rowanswerslabel[$rownumber] = $decodedanswers[$column->name];
}
return $correctAnswers;
}
}

protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
list($repeated, $repeatedoptions) = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect);
Expand Down
124 changes: 94 additions & 30 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ abstract class qtype_oumatrix_base extends question_graded_automatically {

public function get_expected_data(): array {
//$rows = new \qtype_oumatrix\oumatirx_info();
$response = [];
for ($i = 0; $i < count($this->answers); $i++) {
$response[$this->field($i)] = PARAM_RAW_TRIMMED;
}
return $response;
//print_object($this->rows);
//$response = [];
//for ($i = 0; $i < count($this->answers); $i++) {
// $response[$this->field($i)] = PARAM_RAW_TRIMMED;
//}
//return $response;
}

public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
Expand Down Expand Up @@ -135,14 +136,15 @@ 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)];
//[$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)];
return [];
}

public function get_num_parts_right(array $response): array {
Expand Down Expand Up @@ -266,6 +268,12 @@ public function get_expected_data(): array {
return $response;
}

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 @@ -336,17 +344,21 @@ 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;

//[$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)];
}

public function get_num_parts_right(array $response): array {

$numright = 0;
foreach ($this->answers as $key => $answer) {
if ($this->is_full_fraction($answer, $response[$this->field($key)])) {
Expand Down Expand Up @@ -461,12 +473,47 @@ public function get_renderer(moodle_page $page) {
public function get_expected_data(): array {
//$rows = new \qtype_oumatrix\oumatirx_info();
$response = [];
for ($i= 0; $i < count($this->answers); $i++) {
/*foreach ($this->rows as $key => $row) {
//$newrow = $this->qtype->make_row($row);
for ($i = 0; $i < count($row->correctanswers); $i++) {
$response[$key][$i] = PARAM_INT;
}
//$response[$key][$row->correctAnswers] = PARAM_INT;
}*/
/*for ($i= 0; $i < count($this->answers); $i++) {
$response[$this->field($i)] = PARAM_RAW_TRIMMED;
}*/
foreach ($this->rows as $row) {
//$newrow = $this->make_row($row);

if ($row->correctanswers != '') {
$rowanswers[] = PARAM_INT;

//$decodedanswers = [];
if($this->inputtype == 'multiple') {
foreach($this->columns as $key => $column) {
$anslabel = get_string('a', 'qtype_oumatrix', $column->number + 1);
$rowanswerslabel = "rowanswers".$anslabel."_".$row->number;
//$correctAnswers[$rownumber] = $decodedanswers[$column->name];
$response[$rowanswerslabel] = PARAM_INT;
}
}
}
}
if($this->inputtype == 'single') {
return $rowanswers;
}
print_object("11111111111111111111111111111");
print_object($response);
return $response;
}

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,9 +543,22 @@ protected function field(int $key): string {

public function get_correct_response(): ?array {
$response = [];
for ($i = 0; $i < count($this->answers); $i++) {
$response[$this->field($i)] = $this->answers[$i]->answer;
print_object("In get_correct_response^^^^^^^^^^^^^^^^^^^^");
for ($i = 0; $i < count($this->rows); $i++) {
foreach($this->columns as $key => $column) {
$anslabel = get_string('a', 'qtype_oumatrix', $column->number + 1);
$rowanswerslabel = "rowanswers".$anslabel."_".$i;
//$correctAnswers[$rownumber] = $decodedanswers[$column->name];
//$rowanswerslabel[$i] = 1;
$response[$rowanswerslabel] = $this->rows[$i]->correctanswers[$column->number];
//$response->$rowanswerslabel[$i] = $this->rows[$i]->correctanswers[$column->number];
}
//$response[$this->field($i)] = $this->answers[$i]->answer;
}
//for ($i = 0; $i < count($this->answers); $i++) {
// $response[$this->field($i)] = $this->answers[$i]->answer;
//}
print_object($response);
return $response;
}

Expand Down Expand Up @@ -539,13 +599,17 @@ 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;

//[$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)];
print_object("grade_response££££££££££££££££££££££££££££");
print_object($response);
$fraction = 1;
return [$fraction, question_state::graded_state_for_fraction($fraction)];
}

Expand Down
42 changes: 26 additions & 16 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,15 @@ public function save_rows($formdata) {
//$json['answertext'] = $formdata->columnname[$i];
//$questionrow->correctanswers = json_encode($json);
if($formdata->inputtype == 'multiple') {
$anslabel = get_string('a', 'qtype_oumatrix', $i+1);
$rowanswerslabel = "rowanswers".$anslabel;
$answerslist = [];
foreach ($formdata->$rowanswerslabel as $key => $value) {
$answerslist[$formdata->columnname[$key]] = $value;
for ($j = 0; $j < count($formdata->columnname); $j++) {
$anslabel = get_string('a', 'qtype_oumatrix', $j + 1);
$rowanswerslabel = "rowanswers".$anslabel;

if (!array_key_exists($i, $formdata->$rowanswerslabel)) {
$answerslist[$formdata->columnname[$j]] = "0";
continue;
}
$answerslist[$formdata->columnname[$j]] = $formdata->$rowanswerslabel[$i];
}
$questionrow->correctanswers = json_encode($answerslist);
} else {
Expand Down Expand Up @@ -384,19 +388,25 @@ public function get_possible_responses($questiondata) {
protected function initialise_question_rows(question_definition $question,
$questiondata) {
if (!empty($questiondata->options->rows)) {
print_object("222222222222222222222222222");
print_object($questiondata);
foreach ($questiondata->options->rows as $row) {
$newrow = $this->make_row($row);

if ($newrow->getCorrectanswers() != '') {
$correctAnswers = [];
$decodedanswers = [];
$todecode = implode(",", $newrow->getCorrectanswers());
//foreach ($newrow->getCorrectanswers() as $value) {
// $decodedanswers = json_decode(implode("", [$value]), true);
//}
$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];
$correctAnswers = $newrow->getCorrectanswers();
//$decodedanswers = [];
if( $questiondata->options->inputtype == 'multiple') {
$correctAnswers = [];
$todecode = implode(",", $newrow->getCorrectanswers());
//foreach ($newrow->getCorrectanswers() as $value) {
// $decodedanswers = json_decode(implode("", [$value]), true);
//}
$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];
}
}
}
$newrow->setCorrectanswers($correctAnswers);
Expand Down Expand Up @@ -428,7 +438,7 @@ protected function make_column($columndata) {
return new \qtype_oumatrix\column($columndata->id, $columndata->questionid, $columndata->number, $columndata->name);
}

protected function make_row($rowdata) {
public function make_row($rowdata) {
return new row($rowdata->id, $rowdata->questionid, $rowdata->number, $rowdata->name,
explode(',',$rowdata->correctanswers), $rowdata->feedback, $rowdata->feedbackformat);
}
Expand Down
4 changes: 4 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ 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("renderer.... question ");
//print_object("renderer.... $response ".$response);
//print_object("renderer.... $qa ".$qa);
Expand Down Expand Up @@ -234,6 +236,8 @@ public function formulation_and_controls(question_attempt $qa,
public function get_matrix($question) {
$columns = ['Copper', 'Gold', 'Iron'];
$rows = ['Is a good electrical', 'Is a good insulator', 'Can be magnetised'];
print_object("*******************************************");
print_object($question);
$caption = "Matrix question";
$colname[] = null;
$table = "
Expand Down

0 comments on commit 4461272

Please sign in to comment.