Skip to content

Commit

Permalink
phunit and herper class
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Sep 8, 2023
1 parent 614ed35 commit 69ab446
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 81 deletions.
24 changes: 10 additions & 14 deletions classes/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public function __construct(int $id = 0, int $questionid = 0, int $number = 0, s
$this->number = $number;
$this->name = $name;
$this->id = $id;
//$this->column = $this->populate();
$this->column = $this->populate();
}

private function populate(): ?stdClass {
public function populate(): ?stdClass {
if ($this->questionid && $this->number && $this->name) {
$column = new stdClass();
$column->questionid = $this->questionid;
Expand Down Expand Up @@ -101,33 +101,29 @@ public function create_a_column(int $questionid, int $number, string $name): int
}

/**
* Return column id
*
* @return int
*/
public function getQuestionid(): int {
return $this->questionid;
}

/**
* @return int
*/
public function getId(): int {
public function get_id(): int {
return $this->id;
}

/**
* Return column number
*
* @return int
*/
public function getNumber(): int {
public function get_number(): int {
return $this->number;
}

/**
* Return name
* Return column name
*
* @param int $id
* @return string
*/
public function getName(): string {
public function get_name(): string {
return $this->name;
}

Expand Down
18 changes: 9 additions & 9 deletions classes/row.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class row {
/** @var string The row name. */
public $name;

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

/** @var string The row specific feedback. */
Expand Down Expand Up @@ -80,8 +80,8 @@ public function __construct(int $id = 0, int $questionid = 0, int $number = 0, s
*/
public function get_a_row_by_id(int $id): ?stdClass {
global $DB;
if ($column = $DB->get_record('qtype_oumatrix_rows', ['id' => $id])) {
return $column;
if ($row = $DB->get_record('qtype_oumatrix_rows', ['id' => $id])) {
return $row;
}
return null;
}
Expand Down Expand Up @@ -148,42 +148,42 @@ public function delete_a_row(int $rownumber) {
/**
* @return int
*/
public function getId(): int {
public function get_id(): int {
return $this->id;
}

/**
* @return int
*/
public function getNumber(): int {
public function get_number(): int {
return $this->number;
}

/**
* @return string
*/
public function getName(): string {
public function get_name(): string {
return $this->name;
}

/**
* @return array
*/
public function getCorrectanswers(): array {
public function get_correctanswers(): array {
return $this->correctanswers;
}

/**
* @return string
*/
public function getFeedback(): string {
public function get_feedback(): string {
return $this->feedback;
}

/**
* @return int
*/
public function getFeedbackformat(): int {
public function get_feedbackformat(): int {
return $this->feedbackformat;
}

Expand Down
8 changes: 4 additions & 4 deletions edit_oumatrix_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ public function 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,);
print_object('data_preprocessing() 1111111111111111111111');
print_object('data_preprocessing() 222222222222222222222');return $question;
print_object($question);
print_object('data_preprocessing() 222222222222222222222');return $question;
//print_object('data_preprocessing() 1111111111111111111111');
//print_object('data_preprocessing() 222222222222222222222');return $question;
//print_object($question);
//print_object('data_preprocessing() 222222222222222222222');return $question;
}

function data_preprocessing_options($question) {
Expand Down
57 changes: 27 additions & 30 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use \qtype_oumatrix\row;
use qtype_oumatrix\column;
use qtype_oumatrix\row;

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

Expand Down Expand Up @@ -200,8 +201,11 @@ public function save_rows($question) {
// Prepare correct answers.
if($question->inputtype == 'multiple') {
for ($c = 0; $c < count($question->columnname); $c++) {
if ($question->columnname[$c] === '') {
continue;
}
$anslabel = get_string('a', 'qtype_oumatrix', $c + 1);
$rowanswerslabel = "rowanswers". $anslabel;
$rowanswerslabel = "rowanswers" . $anslabel;

if (!array_key_exists($i, $question->$rowanswerslabel)) {
$answerslist[$question->columnname[$c]] = "0";
Expand Down Expand Up @@ -265,12 +269,14 @@ public function delete_question($questionid, $contextid) {

protected function get_num_correct_choices($questiondata) {
$numright = 0;
// TODO: To be done correctly
//foreach ($questiondata->options->answers as $answer) {
// if (!question_state::graded_state_for_fraction($answer->fraction)->is_incorrect()) {
// $numright += 1;
// }
//}
foreach ($questiondata->options->rows as $row) {
$rowanwers = json_decode($row->correctanswers);
foreach ($rowanwers as $key => $value) {
if ((int) $value === 1) {
$numright += 1;
}
}
}
return $numright;
}

Expand All @@ -281,20 +287,20 @@ public function get_random_guess_score($questiondata) {
// Amazingly, the forumla for this works out to be
// # correct choices / total # choices in all cases.

//TODO: improve this is a correct way if we are not using the answers table, etc.
//TODO: improve this.
return $this->get_num_correct_choices($questiondata) /
count($questiondata->options->answers);
}
count($questiondata->options->rows);
}

public function get_possible_responses($questiondata) {
$numright = $this->get_num_correct_choices($questiondata);
$parts = [];

// TODO: To be done correctly
//foreach ($questiondata->options->answers as $aid => $answer) {
// $parts[$aid] = array($aid =>
// new question_possible_response($answer->answer, $answer->fraction / $numright));
//}
foreach ($questiondata->options->answers as $aid => $answer) {
$parts[$aid] = array($aid =>
new question_possible_response($answer->answer, $answer->fraction / $numright));
}

return $parts;
}
Expand All @@ -304,23 +310,17 @@ public function get_possible_responses($questiondata) {
* @param question_definition $question the question_definition we are creating.
* @param object $questiondata the question data loaded from the database.
*/
protected function initialise_question_rows(question_definition $question,
$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 = $newrow->getCorrectanswers();
if ($newrow->get_correctanswers() != '') {
$correctAnswers = $newrow->get_correctanswers();
//$decodedanswers = [];
if( $questiondata->options->inputtype == 'multiple') {
$correctAnswers = [];
$todecode = implode(",", $newrow->getCorrectanswers());
//foreach ($newrow->getCorrectanswers() as $value) {
// $decodedanswers = json_decode(implode("", [$value]), true);
//}
$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)) {
Expand All @@ -330,9 +330,6 @@ protected function initialise_question_rows(question_definition $question,
}
$newrow->setCorrectanswers($correctAnswers);
}
//print_object("****************************");
//print_object($newrow);

$question->rows[] = $newrow;
}
}
Expand All @@ -352,7 +349,7 @@ protected function initialise_question_columns(question_definition $question, $q
}

protected function make_column($columndata) {
return new \qtype_oumatrix\column($columndata->id, $columndata->questionid, $columndata->number, $columndata->name);
return new column($columndata->id, $columndata->questionid, $columndata->number, $columndata->name);
}

public function make_row($rowdata) {
Expand Down Expand Up @@ -409,7 +406,7 @@ public function import_from_xml($data, $question, qformat_xml $format, $extra=nu
}

public function export_to_xml($question, qformat_xml $format, $extra = null) {
print_object($question);
//print_object($question);
$output = '';

$output .= " <shuffleanswers>" . $format->get_single(
Expand Down
4 changes: 2 additions & 2 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ public function get_matrix(question_attempt $qa) {
<tr>
<th scope='col'></th>";
foreach ($question->columns as $key => $value) {
$colname[$key] = $value->getName();
$colname[$key] = $value->get_name();
$table .= "<th scope='col'><span id='$colname[$key]' class='answer_col' >$colname[$key]</span></th>";
}
$table .= "</tr>
<tr> ";
$i = 0;
foreach ($question->rows as $key => $value) {
$rowname = $value->getName();
$rowname = $value->get_name();
$rowid = 'row_'. $key;
$table .= "<th scope='col'><span id='$rowid'>$rowname</span></th>";
for ($j = 0; $j < count($colname); $j++) {
Expand Down
Loading

0 comments on commit 69ab446

Please sign in to comment.