Skip to content

Commit

Permalink
PHPdoc and tidying json decoding and encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Oct 23, 2023
1 parent a8547c4 commit bd80c93
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 114 deletions.
2 changes: 1 addition & 1 deletion classes/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class column {
* @param string $name
* @param int $id
*/
public function __construct(int $questionid, int $number = 0, string $name = '', int $id = 0) {
public function __construct(int $questionid, int $number, string $name, int $id = 0) {
$this->questionid = $questionid;
$this->number = $number;
$this->name = $name;
Expand Down
22 changes: 13 additions & 9 deletions classes/row.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class row {
/** @var int Minimum number of rows. */
const MIN_NUMBER_OF_ROWS = 2;

/** @var int The id of the row */
public $id;

/** @var int The id of the question. */
public $questionid;

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

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

/** @var array The string of json encoded correct answers. */
/** @var array The array of correct answers. */
public $correctanswers;

/** @var string The row specific feedback. */
Expand All @@ -48,15 +49,18 @@ class row {
public $feedbackformat;

/**
* Construct the matrix object to be used by rows and colums objects.
* Construct the matrix object to be used by rows.
*
* @param int $id
* @param int $questionid
* @param int $numberofrows
* @param int $numberofcolumns
* @param int $id the row id
* @param int $questionid the questionid
* @param int $number the row number
* @param string $name the row name
* @param array $correctanswers the list of correct answers
* @param string $feedback the row feedback
* @param string $feedbackformat the row feedback format
*/
public function __construct(int $id = 0, int $questionid = 0, int $number = 0, string $name = '', string $correctanswers = '',
string $feedback = '', int $feedbackformat = 0) {
public function __construct(int $id, int $questionid, int $number, string $name, array $correctanswers,
string $feedback = '', string $feedbackformat = FORMAT_HTML) {
$this->questionid = $questionid;
$this->number = $number;
$this->name = $name;
Expand Down
31 changes: 0 additions & 31 deletions db/install.php

This file was deleted.

31 changes: 0 additions & 31 deletions db/uninstall.php

This file was deleted.

33 changes: 1 addition & 32 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,7 @@
function xmldb_qtype_oumatrix_upgrade($oldversion) {
global $DB;

$dbman = $DB->get_manager();
if ($oldversion < 2023080300) {

$table = new xmldb_table('qtype_oumatrix_rows');
$field = new xmldb_field('correctanswers', XMLDB_TYPE_TEXT, 'small', null, false, false);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Data savepoint reached.
upgrade_plugin_savepoint(true, 2023080300, 'qtype', 'oumatrix');
}

if ($oldversion < 2023081400) {

// Changing type of field name on table qtype_oumatrix_columns to text.
$table = new xmldb_table('qtype_oumatrix_columns');
$field = new xmldb_field('name', XMLDB_TYPE_TEXT, 'smal', null, XMLDB_NOTNULL, null, null, 'number');

// Launch change of type for field name.
$dbman->change_field_type($table, $field);

// Changing type of field name on table qtype_oumatrix_rows to text.
$table = new xmldb_table('qtype_oumatrix_rows');
$field = new xmldb_field('name', XMLDB_TYPE_TEXT, 'smal', null, XMLDB_NOTNULL, null, null, 'number');

// Launch change of type for field name.
$dbman->change_field_type($table, $field);

// Oumatrix savepoint reached.
upgrade_plugin_savepoint(true, 2023081400, 'qtype', 'oumatrix');
}
// Put any upgrade step following this.

return true;
}
11 changes: 5 additions & 6 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,13 @@ protected function initialise_question_rows(question_definition $question, stdCl
foreach ($questiondata->rows as $row) {
$newrow = $this->make_row($row);
$correctanswers = [];
$decodedanswers = json_decode($newrow->correctanswers, true);
foreach ($questiondata->columns as $column) {
if ($decodedanswers != null && array_key_exists($column->id, $decodedanswers)) {
if (array_key_exists($column->id, $newrow->correctanswers)) {
if ($questiondata->options->inputtype == 'single') {
$anslabel = 'a' . $column->number;
$correctanswers[$column->id] = $anslabel;
} else {
$correctanswers[$column->id] = $decodedanswers[$column->id];
$correctanswers[$column->id] = $newrow->correctanswers[$column->id];
}
}
}
Expand All @@ -242,7 +241,7 @@ protected function initialise_question_rows(question_definition $question, stdCl
*/
public function make_row(stdClass $rowdata): row {
return new row($rowdata->id, $rowdata->questionid, $rowdata->number, $rowdata->name,
$rowdata->correctanswers, $rowdata->feedback, $rowdata->feedbackformat);
json_decode($rowdata->correctanswers, true), $rowdata->feedback, $rowdata->feedbackformat);
}

public function delete_question($questionid, $contextid) {
Expand Down Expand Up @@ -284,8 +283,8 @@ public function get_total_number_of_choices(object $questiondata):? int {
public function get_num_correct_choices($questiondata) {
$numright = 0;
foreach ($questiondata->rows as $row) {
$rowanwers = json_decode($row->correctanswers);
$numright += count((array)$rowanwers);
$rowanwers = json_decode($row->correctanswers, true);
$numright += count($rowanwers);
}
return $numright;
}
Expand Down
8 changes: 4 additions & 4 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@

if ($ADMIN->fulltree) {
$inputtype = [
'single' => get_string('inputtypesingle', 'qtype_oumatrix'),
'multiple' => get_string('inputtypemultiple', 'qtype_oumatrix'),
'single' => new lang_string('inputtypesingle', 'qtype_oumatrix'),
'multiple' => new lang_string('inputtypemultiple', 'qtype_oumatrix'),
];
$settings->add(new admin_setting_configselect('qtype_oumatrix/inputtype',
new lang_string('inputtype', 'qtype_oumatrix'),
new lang_string('inputtype_desc', 'qtype_oumatrix'), 'single', $inputtype));

$grademethod = [
'partial' => get_string('gradepartialcredit', 'qtype_oumatrix'),
'allnone' => get_string('gradeallornothing', 'qtype_oumatrix'),
'partial' => new lang_string('gradepartialcredit', 'qtype_oumatrix'),
'allnone' => new lang_string('gradeallornothing', 'qtype_oumatrix'),
];
$settings->add(new admin_setting_configselect('qtype_oumatrix/grademethod',
new lang_string('grademethod', 'qtype_oumatrix'),
Expand Down
5 changes: 5 additions & 0 deletions tests/questiontype_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,14 @@ public function todo_test_initialise_question_instance() {

public function test_get_random_guess_score(): void {
$helper = new qtype_oumatrix_test_helper();

$qdata = $helper->get_test_question_data('animals_single');
$expected = $this->qtype->get_num_correct_choices($qdata) / $this->qtype->get_total_number_of_choices($qdata);
$this->assertEquals($expected, $this->qtype->get_random_guess_score($qdata));

$qdata = $helper->get_test_question_data('food_multiple');
$expected = $this->qtype->get_num_correct_choices($qdata) / $this->qtype->get_total_number_of_choices($qdata);
$this->assertEquals($expected, $this->qtype->get_random_guess_score($qdata));
}

public function test_get_random_guess_score_broken_question(): void {
Expand Down

0 comments on commit bd80c93

Please sign in to comment.