Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into main
  • Loading branch information
mkassaei committed Oct 22, 2023
2 parents f1bd36d + 7bef6ec commit b56f8ed
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 71 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ jobs:
run: moodle-plugin-ci phplint

- name: PHP Copy/Paste Detector
continue-on-error: true # This step will show errors but will not fail.
if: ${{ always() }}
run: moodle-plugin-ci phpcpd

Expand Down
14 changes: 14 additions & 0 deletions classes/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ public function __construct(int $questionid, int $number = 0, string $name = '',
$this->name = $name;
$this->id = $id;
}

/**
* Returns the array of columns by id's.
*
* @param array $columns
* @return array
*/
public static function get_column_ids(array $columns): array {
$columnids = [];
foreach ($columns as $column) {
$columnids[$column->id] = $column;
}
return $columnids;
}
}
7 changes: 2 additions & 5 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;

defined('MOODLE_INTERNAL') || die();

/**
* Privacy Subsystem for qtype_oumatrix implementing user_preference_provider.
*
Expand All @@ -40,8 +38,7 @@ class provider implements
// This component has data.
// We need to return default options that have been set a user preferences.
\core_privacy\local\metadata\provider,
\core_privacy\local\request\user_preference_provider
{
\core_privacy\local\request\user_preference_provider {

/**
* Returns meta data about this system.
Expand Down Expand Up @@ -94,4 +91,4 @@ public static function export_user_preferences(int $userid) {
writer::export_user_preference('qtype_oumatrix', 'shuffleanswers', transform::yesno($preference), $desc);
}
}
}
}
20 changes: 8 additions & 12 deletions edit_oumatrix_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ private function data_preprocessing_columns($question) {
return $question;
}
$question->columnname = [];
foreach ($question->columns as $column) {
foreach ($question->columns as $column) { // Class column $column.
if (trim($column->name ?? '') === '') {
continue;
}
$question->columnname[] = $column->name;
$question->columnname[$column->number - 1] = $column->name;
}
$this->numcolumns = count($question->columnname);
return $question;
Expand All @@ -258,21 +258,17 @@ private function data_preprocessing_rows($question) {
}
$key = 0;
$question->rowname = [];
/**
* @var int $index
* @var row $row
*/
foreach ($question->rows as $index => $row) {
$question->rowname[$row->number] = $row->name;
foreach ($question->rows as $index => $row) { // Key int $index, class row $row.
$question->rowname[$row->number - 1] = $row->name;
$decodedanswers = json_decode($row->correctanswers, true);
foreach ($question->columns as $key => $column) {
if (array_key_exists($column->id, $decodedanswers)) {
$columnvalue = 'a' . ($column->number + 1);
$columnvalue = 'a' . ($column->number);
if ($question->options->inputtype == 'single') {
$question->rowanswers[$row->number] = $columnvalue;
$question->rowanswers[$row->number - 1] = $columnvalue;
} else {
$rowanswerslabel = "rowanswers" . $columnvalue;
$question->{$rowanswerslabel}[$row->number] = $decodedanswers[$column->id];
$question->{$rowanswerslabel}[$row->number - 1] = $decodedanswers[$column->id];
}
}
}
Expand All @@ -294,7 +290,7 @@ private function data_preprocessing_rows($question) {
$feedback[$key]['format'] = $row->feedbackformat ?? FORMAT_HTML;
$question->rows[$index]->feedbackformat = $feedback[$key]['format'];
$question->rows[$index]->feedback = $feedback[$key]['text'];
$question->feedback[$row->number] = $feedback[$key];
$question->feedback[$row->number - 1] = $feedback[$key];
$key++;
}
return $question;
Expand Down
40 changes: 24 additions & 16 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use qtype_oumatrix\column;
use qtype_oumatrix\row;

defined('MOODLE_INTERNAL') || die();

/**
* Class that represents an oumatrix question.
*
Expand Down Expand Up @@ -166,7 +168,7 @@ public function get_renderer(moodle_page $page) {
public function get_expected_data(): array {
$expected = [];
foreach ($this->rows as $row) {
$expected[$this->field($row->number)] = PARAM_INT;
$expected[$this->field($row->number - 1)] = PARAM_INT;
}
return $expected;
}
Expand All @@ -180,7 +182,7 @@ public function is_choice_selected($response, $rowkey, $colkey) {
}

public function prepare_simulated_post_data($simulatedresponse) {
return $simulatedresponse; // TODO
return $simulatedresponse; // TODO.
}

public function is_same_response(array $prevresponse, array $newresponse): bool {
Expand All @@ -205,9 +207,13 @@ protected function field(int $rowkey): string {

public function get_correct_response(): ?array {
$response = [];
$columnids = column::get_column_ids($this->columns);
foreach ($this->roworder as $key => $rownumber) {
$row = $this->rows[$rownumber];
$response[$this->field($key)] = $this->columns[array_key_first($row->correctanswers)]->number;
foreach ($row->correctanswers as $colkey => $answer) {
// Get the corresponding column number associated with the column key.
$response[$this->field($key)] = $columnids[$colkey]->number;
}
}
return $response;
}
Expand All @@ -231,8 +237,8 @@ public function summarise_response(array $response): ?string {
}

public function is_complete_response(array $response): bool {
foreach ($this->rows as $row) {
$fieldname = $this->field($row->number);
foreach ($this->roworder as $key => $rownumber) {
$fieldname = $this->field($key);
if (!array_key_exists($fieldname, $response)) {
return false;
}
Expand All @@ -249,10 +255,12 @@ public function grade_response(array $response): array {

public function get_num_parts_right(array $response): array {
$numright = 0;
$columnids = column::get_column_ids($this->columns);
foreach ($this->roworder as $key => $rownumber) {
$row = $this->rows[$rownumber];
$columnnumber = $this->columns[array_key_first($row->correctanswers)]->number;
if (array_key_exists($this->field($key), $response) && $response[$this->field($key)] == $columnnumber) {
$column = $columnids[array_key_first($row->correctanswers)];
if (array_key_exists($this->field($key), $response) &&
$response[$this->field($key)] == $column->number) {
$numright++;
}
}
Expand All @@ -273,7 +281,7 @@ public function get_expected_data(): array {
$expected = [];
foreach ($this->rows as $row) {
foreach ($this->columns as $column) {
$expected[$this->field($row->number, $column->number)] = PARAM_INT;
$expected[$this->field($row->number - 1, $column->number)] = PARAM_INT;
}
}
return $expected;
Expand All @@ -299,7 +307,7 @@ protected function field(int $rowkey, int $columnkey): string {
}

public function prepare_simulated_post_data($simulatedresponse) {
return $simulatedresponse; // TODO
return $simulatedresponse; // TODO.
}

public function is_same_response(array $prevresponse, array $newresponse): bool {
Expand All @@ -315,16 +323,16 @@ public function is_same_response(array $prevresponse, array $newresponse): bool
}

public function get_correct_response(): ?array {
$answers = [];
$response = [];
$columnids = column::get_column_ids($this->columns);
foreach ($this->roworder as $key => $rownumber) {
$row = $this->rows[$rownumber];
foreach ($row->correctanswers as $colkey => $answer) {
// Get the corresponding column object associated with the column key.
$column = $this->columns[$colkey];
$answers[$this->field($key, $column->number)] = $answer;
// Get the corresponding column number associated with the column key.
$response[$this->field($key, $columnids[$colkey]->number)] = $answer;
}
}
return $answers;
return $response;
}

public function summarise_response(array $response): ?string {
Expand All @@ -349,10 +357,10 @@ public function summarise_response(array $response): ?string {
}

public function is_complete_response(array $response): bool {
foreach ($this->rows as $row) {
foreach ($this->roworder as $key => $rownumber) {
$inputresponse = false;
foreach ($this->columns as $column) {
$fieldname = $this->field($row->number, $column->number);
$fieldname = $this->field($key, $column->number);
if (array_key_exists($fieldname, $response)) {
$inputresponse = true;
}
Expand Down
45 changes: 25 additions & 20 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public function get_question_options($question) {
echo $OUTPUT->notification('Error: Missing matrix question options!');
return false;
}
if (!$question->columns = $DB->get_records('qtype_oumatrix_columns', ['questionid' => $question->id])) {
if (!$question->columns = $DB->get_records('qtype_oumatrix_columns', ['questionid' => $question->id], 'id')) {
echo $OUTPUT->notification('Error: Missing question columns!');
return false;
}
if (!$question->rows = $DB->get_records('qtype_oumatrix_rows', ['questionid' => $question->id])) {
if (!$question->rows = $DB->get_records('qtype_oumatrix_rows', ['questionid' => $question->id], 'id')) {
echo $OUTPUT->notification('Error: Missing question rows!');
return false;
}
Expand Down Expand Up @@ -94,22 +94,24 @@ public function save_question_options($question) {
* @param stdClass $question This holds the information from the editing form.
* @return array The list of columns created.
*/
public function save_columns(object $question): array {
public function save_columns(stdClass $question): array {
global $DB;
$numcolumns = count($question->columnname);
$columnslist = [];

// Insert column input data.
$columnnumber = 1;
for ($i = 0; $i < $numcolumns; $i++) {
if (trim(($question->columnname[$i]) ?? '') === '') {
continue;
}
$column = new stdClass();
$column->questionid = $question->id;
$column->number = $i;
$column->number = $columnnumber;
$column->name = $question->columnname[$i];
$column->id = $DB->insert_record('qtype_oumatrix_columns', $column);
$columnslist[] = $column;
$columnnumber++;
}
return $columnslist;
}
Expand All @@ -120,20 +122,21 @@ public function save_columns(object $question): array {
* @param stdClass $question This holds the information from the editing form
* @param array $columnslist
*/
public function save_rows(object $question, array $columnslist) {
public function save_rows(stdClass $question, array $columnslist) {
global $DB;
$context = $question->context;
$numrows = count($question->rowname);

// Insert row input data.
$rownumber = 1;
for ($i = 0; $i < $numrows; $i++) {
$answerslist = [];
if (trim($question->rowname[$i] ?? '') === '') {
continue;
}
$questionrow = new stdClass();
$questionrow->questionid = $question->id;
$questionrow->number = $i;
$questionrow->number = $rownumber;
$questionrow->name = $question->rowname[$i];
// Prepare correct answers.
for ($c = 0; $c < count($columnslist); $c++) {
Expand All @@ -160,6 +163,7 @@ public function save_rows(object $question, array $columnslist) {

$DB->update_record('qtype_oumatrix_rows', $questionrow);
}
$rownumber++;
}
}

Expand Down Expand Up @@ -189,8 +193,8 @@ protected function initialise_question_instance(question_definition $question, $
* @param stdClass $questiondata the question data loaded from the database.
*/
protected function initialise_question_columns(question_definition $question, stdClass $questiondata): void {
foreach ($questiondata->columns as $index => $column) {
$question->columns[$index] = $this->make_column($column);
foreach ($questiondata->columns as $column) {
$question->columns[$column->number] = $this->make_column($column);
}
}

Expand All @@ -211,22 +215,22 @@ protected function make_column(stdClass $columndata): column {
* @param stdClass $questiondata the question data loaded from the database.
*/
protected function initialise_question_rows(question_definition $question, stdClass $questiondata): void {
foreach ($questiondata->rows as $index => $row) {
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 ($questiondata->options->inputtype == 'single') {
$anslabel = 'a' . ($column->number + 1);
$anslabel = 'a' . $column->number;
$correctanswers[$column->id] = $anslabel;
} else {
$correctanswers[$column->id] = $decodedanswers[$column->id];
}
}
}
$newrow->correctanswers = $correctanswers;
$question->rows[$index] = $newrow;
$question->rows[$row->number] = $newrow;
}
}

Expand Down Expand Up @@ -268,12 +272,12 @@ public function get_random_guess_score($questiondata) {
* @return int
*/
public function get_total_number_of_choices(object $questiondata):? int {
// if rows or columns are not set return null;
if (sizeof($questiondata->columns) === 0 || sizeof($questiondata->rows) === 0) {
// If rows or columns are not set return null.
if (count($questiondata->columns) === 0 || count($questiondata->rows) === 0) {
return null;
}
// Total number of choices for each row is the number of columns,
// therefore the total number of choices for the question is
// therefore the total number of choices for the question is.
return count($questiondata->columns) * count($questiondata->rows);
}

Expand All @@ -288,7 +292,7 @@ public function get_num_correct_choices($questiondata) {

public function get_possible_responses($questiondata) {
if ($questiondata->options->single) {
$responses = array();
$responses = [];

// TODO: Sort out this funtion to work with rows and columns, etc.
foreach ($questiondata->options->answers as $aid => $answer) {
Expand All @@ -298,14 +302,15 @@ public function get_possible_responses($questiondata) {
}

$responses[null] = question_possible_response::no_response();
return array($questiondata->id => $responses);
return [$questiondata->id => $responses];
} else {
$parts = array();
$parts = [];

foreach ($questiondata->options->answers as $aid => $answer) {
$parts[$aid] = array($aid => new question_possible_response(
question_utils::to_plain_text($answer->answer, $answer->answerformat),
$answer->fraction));
$parts[$aid] = [
$aid => new question_possible_response(question_utils::to_plain_text(
$answer->answer, $answer->answerformat), $answer->fraction),
];
}

return $parts;
Expand Down
Loading

0 comments on commit b56f8ed

Please sign in to comment.