Skip to content

Commit

Permalink
adding some css and other chnages in question ans questintype
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Aug 21, 2023
1 parent aaab2ea commit 8461533
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 367 deletions.
30 changes: 15 additions & 15 deletions classes/column.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,35 @@ class column {
* @param int $number
* @param string $name
*/
public function __construct(int $id, int $questionid = 0, int $number = 0, string $name = '') {
public function __construct(int $id = 0, int $questionid = 0, int $number = 0, string $name = '') {
$this->questionid = $questionid;
$this->number = $number;
$this->name = $name;
$this->id = $id;
//$this->column = $this->populate();
}

public function populate(stdClass $column) {
$this->column = $column;
$this->id = $column->id;
$this->questionid = $column->questionid;
$this->number = $column->number;
$this->name = $column->name;
private function populate(): ?stdClass {
if ($this->questionid && $this->number && $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 int $id
* @param string $name
* @return stdClass
* @throws \dml_exception
*/
public function get_a_column_by_id(int $id): ?stdClass {
global $DB;
//if ($this->column->id === $id) {
// return $this->column;
//}
if ($column = $DB->get_record('qtype_oumatrix_columns', ['id' => $id])) {
return $column;
public function get_column_by_name(string $name): ?stdClass {
if ($name === $this->name) {
return $this->column;
}
return null;
}
Expand Down
55 changes: 10 additions & 45 deletions classes/row.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ class row {
/**
* Construct the matrix object to be used by rows and colums objects.
*
* @param int $id
* @param int $questionid
* @param int $numberofrows
* @param int $numberofcolumns
*/
public function __construct(int $id, int $questionid = 0, int $number = 0, string $name = '', array $correctanswers = [], string $feedback = '', int $feedbackformat = 1) {
$this->id = $id;
public function __construct(int $id = 0, int $questionid = 0, int $number = 0, string $name = '', array $correctanswers = [],
string $feedback = '', int $feedbackformat = 0) {
$this->questionid = $questionid;
$this->number = $number;
$this->name = $name;
$this->correctanswers = $correctanswers;
$this->feedback = $feedback;
$this->feedbackformat = $feedbackformat;
$this->id = $id;
}

/**
Expand All @@ -84,22 +86,26 @@ public function get_a_row_by_id(int $id): ?stdClass {
return null;
}

public function create_default_row(int $questionid, int $number = 1, string $name = 'row', string $feedback = '', int $feedbackformat = 2) {
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', string $feedback = '', int $feedbackformat = 2) {
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;
Expand All @@ -112,27 +118,6 @@ public function setCorrectanswers(array $correctanswers): void {
$this->correctanswers = $correctanswers;
}

/**
* Create default rows.
*
* @param int $questionid
* @param int $rowstart
* @param int $numberofrows
* @return void
* @throws \dml_exception
*/
public function create_default_rows(int $questionid, int $rowstart = 1, int $numberofrows = 0) {
global $DB;
if ($numberofrows === 0) {
$numberofrows = $this->numberofrows;
}
for ($r = $rowstart; $r <= $numberofrows; $r++) {
$row = $this->create_a_default_row($questionid, $r, 'r'.$r, );
$row->id = $DB->insert_record('qtype_oumatrix_rows', $row);
$this->rows[] = $row;
}
}

/**
* Retunr a row.
*
Expand Down Expand Up @@ -160,26 +145,6 @@ public function delete_a_row(int $rownumber) {
$DB->delete_records('qtype_oumatrix_rows', ['questionid' => $this->questionid, 'number' => $rownumber]);
}

/**
* Return an array of rows.
*
* @param int $numberofrows
* @return array|null
* @throws \dml_exception
*/
public function get_rows(int $numberofrows = 0): ?array {
$rows = [];
// If there is no changes in number of rows
if ($this->numberofrows === $numberofrows) {
for ($r = 1; $r <= $numberofrows; $r++) {
$rows[] = $this->get_a_row($r);
}
return $rows;
} else {
return $this->create_default_rows($this->questionid, 1);
}
}

/**
* @return int
*/
Expand Down
103 changes: 39 additions & 64 deletions edit_oumatrix_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
use \qtype_oumatirx\row;
use \qtype_oumatirx\column;

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

/**
* Editing form for the oumatrix question type.
*
Expand All @@ -36,46 +34,29 @@
*/
class qtype_oumatrix_edit_form extends question_edit_form {

/**
* The default starting number of columns (answers).
*/
protected const COL_NUM_START = 3;
/** The default starting number of columns (answers). */
private const COL_NUM_START = 3;

/**
* The number of columns (answers) that get added at a time.
*/
protected const COL_NUM_ADD = 2;
/** The number of columns (answers) that get added at a time. */
private const COL_NUM_ADD = 2;

/**
* The default starting number of rows (question row).
*/
protected const ROW_NUM_START = 4;

/**
* The number of rows (question row) that get added at a time.
*/
protected const ROW_NUM_ADD = 2;
/**The default starting number of rows (question row). */
private const ROW_NUM_START = 4;

/** @var int Number of rows. */
protected $numrows;
/** The number of rows (question row) that get added at a time.*/
private const ROW_NUM_ADD = 2;

/** @var int Number of columns. */
protected $numcolumns;

/** @var array The grid options. */
protected $gridoptions;
private $numcolumns;

/** @var object The matrix row object. */
protected $rowinfo = null;

/** @var object The matrix column (answer) object. */
protected $columninfo = null;
/** @var int Number of rows. */
private $numrows;

/** @var string answermode of rows. */
protected $inputtype;
private $inputtype;

/** @var string grading method of rows. */
protected $grademethod;
private $grademethod;

/**
* Add question-type specific form fields.
Expand All @@ -86,7 +67,7 @@ protected function definition_inner($mform) {

// Sett the number of columns and rows.self::COL_NUM_START;
$this->numcolumns = $this->numcolumns ?? self::COL_NUM_START;
$this->numrows = $this->numrows ??self::ROW_NUM_START;
$this->numrows = $this->numrows ?? self::ROW_NUM_START;

$qtype = 'qtype_oumatrix';
$answermodemenu = [
Expand Down Expand Up @@ -162,24 +143,24 @@ public function qtype() {

public function data_preprocessing($question) {
$question = parent::data_preprocessing($question);
//$question = $this->data_preprocessing_answers($question, true);
$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($question);
print_object('data_preprocessing() 222222222222222222222');
return $question;
}

protected function data_preprocessing_options($question) {
function data_preprocessing_options($question) {
if (empty($question->options)) {
return $question;
}
$question->inputtype = $question->options->inputtype;
$question->grademethod = $question->options->grademethod;
$question->shuffleanswers = $question->options->shuffleanswers;
$question->shownumcorrect = $question->options->shownumcorrect;

if (!empty($question->options)) {
$question->inputtype = $question->options->inputtype;
$question->grademethod = $question->options->grademethod;
$question->shuffleanswers = $question->options->shuffleanswers;
$question->shownumcorrect = $question->options->shownumcorrect;
}
$this->data_preprocessing_columns($question);
$this->data_preprocessing_rows($question);
return $question;
Expand All @@ -191,16 +172,18 @@ protected function data_preprocessing_options($question) {
* @param object $question The data being passed to the form.
* @return object The modified data.
*/
protected function data_preprocessing_columns($question) {
private function data_preprocessing_columns($question) {
if (empty($question->options->columns)) {
return $question;
}

$question->columnname = [];
foreach ($question->options->columns as $column) {
if (trim($column->name ?? '') === '') {
continue;
}
$question->columnname[] = $column->name;
}
//$this->numcolumns = count($question->options->columns);
//$this->numcolumns = count($question->columnname);
return $question;
}

Expand All @@ -210,8 +193,7 @@ protected function data_preprocessing_columns($question) {
* @param object $question The data being passed to the form.
* @return object The modified data.
*/
protected function data_preprocessing_rows($question) {
$feedback = [];
private function data_preprocessing_rows($question) {
if (empty($question->options->rows)) {
return $question;
}
Expand All @@ -227,6 +209,7 @@ protected function data_preprocessing_rows($question) {
$itemid = (int)$row->id ?? null;

// Prepare the feedback editor to display files in draft area.
$feedback = [];
$feedbackdraftitemid = file_get_submitted_draft_itemid('feedback['.$key.']');
$feedback[$key]['text'] = file_prepare_draft_area(
$feedbackdraftitemid,
Expand All @@ -244,7 +227,6 @@ protected function data_preprocessing_rows($question) {
$key++;
}
$question->feedback = $feedback;
//$this->numrows = count($question->options->rows);
return $question;
}

Expand All @@ -255,10 +237,10 @@ protected function format_correct_answers_multiple($rownumber, $answers, $questi
$rowanswerslabel = "rowanswers".$anslabel;
$question->$rowanswerslabel[$rownumber] = $decodedanswers[$column->name];
}
}
}

protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
list($repeated, $repeatedoptions) = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect);
[$repeated, $repeatedoptions] = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect);
$repeatedoptions['hintclearwrong']['disabledif'] = ['single', 'eq', 1];
$repeatedoptions['hintshownumcorrect']['disabledif'] = ['single', 'eq', 1];
return [$repeated, $repeatedoptions];
Expand All @@ -272,8 +254,7 @@ protected function get_hint_fields($withclearwrong = false, $withshownumpartscor
* @param bool $withshownumpartscorrect Show number correct.
* @return object The modified data.
*/
protected function data_preprocessing_hints($question, $withclearwrong = false,
$withshownumpartscorrect = false) {
protected function data_preprocessing_hints($question, $withclearwrong = false, $withshownumpartscorrect = false) {
if (empty($question->hints)) {
return $question;
}
Expand Down Expand Up @@ -315,8 +296,6 @@ protected function add_per_column_fields(MoodleQuickForm $mform, string $label,

protected function get_per_column_fields($mform, $label, $repeatedoptions, $columns) {
$repeated = [];
// $repeated[] = $mform->createElement('editor', 'columnname', $label, ['rows' => 2], $this->editoroptions);
// TODO: If needed replace the line below the line above.
$repeated[] = $mform->createElement('text', 'columnname', $label, ['size' => 40]);
$mform->setType('columnname', PARAM_RAW);
$repeatedoptions['column']['type'] = PARAM_RAW;
Expand Down Expand Up @@ -358,30 +337,26 @@ protected function add_per_row_fields(MoodleQuickForm $mform, string $label,
* @param array $rows
* @return array
*/
protected function get_per_row_fields(MoodleQuickForm $mform, string $label, array &$repeatedoptions, array &$rows): array {
//print_object($this->question);
protected function get_per_row_fields(MoodleQuickForm $mform, string $label, array $repeatedoptions, array $rows): array {
$repeated = [];
$rowoptions = [];
// $rowoptions[] = $mform->createElement('editor', 'rowname', $label, ['rows' => 2], $this->editoroptions);
// TODO: If needed replace the line below the line above.
$rowoptions[] = $mform->createElement('text', 'rowname', 'Name', ['size' => 40]);

// Get the list answer input type (radio buttons or checkbexs).
//$inputtype = $this->question->options->inputtype ?? get_config('qtype_oumatrix', 'inputtype');
for ($i = 1; $i <= $this->numcolumns; $i++) {
$anslabel = get_string('a', 'qtype_oumatrix', $i);
// Get the list answer input type (radio buttons or checkboxes).
for ($i = 0; $i < $this->numcolumns; $i++) {
$anslabel = get_string('a', 'qtype_oumatrix', $i+1);
if ($this->inputtype === 'single') {
$rowoptions[] = $mform->createElement('radio', 'rowanswers', '', $anslabel, $anslabel);
$rowoptions[] = $mform->createElement('radio', "rowanswers$anslabel", '', $anslabel);
} else {
$rowoptions[] = $mform->createElement('checkbox', "rowanswers$anslabel",'', $anslabel);
//$rowoptions[] = $mform->addElement('advcheckbox', "rowanswers$anslabel",'', $anslabel,"", array(0, 1));
$rowoptions[] = $mform->createElement('checkbox', "rowanswers$anslabel", '', $anslabel);
}
}
$rowoptions[] = $mform->createElement('editor', 'feedback',
get_string('feedback', 'question'), ['rows' => 2], $this->editoroptions);
$repeated[] = $mform->createElement('group', 'rowoptions', $label, $rowoptions, null, false);
$mform->setType('rowname', PARAM_RAW);
$repeatedoptions['row']['type'] = PARAM_RAW;
//$repeatedoptions['row']['correctanswers'] = PARAM_RAW;
$rows = 'rows';
return $repeated;
}
Expand Down
2 changes: 1 addition & 1 deletion lang/en/qtype_oumatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
All or nothing: students must get every response correct, otherwise they score zero.';
$string['gradepartialcredit'] = 'Give partial credit';
$string['gradeallornothing'] = 'All-or-nothing';
$string['notenoughquestions'] = 'This type of question requires at least {$a} word';
$string['notenoughanswercols'] = 'This type of question requires at least {$a} answers columns';
$string['pluginname'] = 'Matrix';
$string['pluginname_help'] = 'Creating a matrix question requires you to specify column headings (values) to row headings (items). For example, you might ask students to classify an item as animal, vegetable, or mineral using Single Choice. You can use Multiple Response so that several values may apply to an item.';
$string['pluginnameadding'] = 'Adding a Matrix question';
Expand Down
Loading

0 comments on commit 8461533

Please sign in to comment.