Skip to content

Commit

Permalink
Adding import/export functionality and behat test
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Jun 19, 2024
1 parent ef7cc2e commit fec85f9
Show file tree
Hide file tree
Showing 13 changed files with 600 additions and 88 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ jobs:
run: moodle-plugin-ci mustache

- name: Grunt
if: ${{ matrix.moodle-branch == 'MOODLE_403_STABLE' }}
run: moodle-plugin-ci grunt --max-lint-warnings 0

- name: PHPUnit tests
Expand Down
80 changes: 80 additions & 0 deletions backup/moodle2/backup_qtype_drawlines_plugin.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Provides the information to backup drawlines questions.
*
* @package qtype_drawlines
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_qtype_drawlines_plugin extends backup_qtype_plugin {
/**
* Returns the qtype name.
*
* @return string The type name
*/
protected static function qtype_name() {
return 'drawlines';
}

/**
* Returns the qtype information to attach to question element.
*/
protected function define_question_plugin_structure() {

$plugin = $this->get_plugin_element(null, '../../qtype', self::qtype_name());

$pluginwrapper = new backup_nested_element($this->get_recommended_name());

$plugin->add_child($pluginwrapper);

$options = new backup_nested_element('drawlines', ['id'], ['grademethod',
'correctfeedback', 'correctfeedbackformat',
'partiallycorrectfeedback', 'partiallycorrectfeedbackformat',
'incorrectfeedback', 'incorrectfeedbackformat',
'shownumcorrect', 'showmisplaced',
]);
$pluginwrapper->add_child($options);

$lines = new backup_nested_element('lines');
$line = new backup_nested_element('line', ['id'], ['number', 'type',
'labelstart', 'labelmiddle', 'labelend', 'zonestart', 'zoneend']);
$pluginwrapper->add_child($lines);
$lines->add_child($line);

$options->set_source_table('qtype_drawlines_options', ['questionid' => backup::VAR_PARENTID]);
$lines->set_source_table('qtype_drawlines_lines', ['questionid' => backup::VAR_PARENTID]);

return $plugin;
}

/**
* Returns one array with filearea => mappingname elements for the qtype
*
* Used by {@link get_components_and_fileareas} to know about all the qtype
* files to be processed both in backup and restore.
*/
public static function get_qtype_fileareas() {
return
[
'bgimage' => 'question_created',
'correctfeedback' => 'question_created',
'partiallycorrectfeedback' => 'question_created',
'incorrectfeedback' => 'question_created',
];
}
}
109 changes: 109 additions & 0 deletions backup/moodle2/restore_qtype_drawlines_plugin.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Restore code for qtype_drawlines.
*
* @package qtype_drawlines
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

class restore_qtype_drawlines_plugin extends restore_qtype_plugin {
/**
* Returns the qtype name.
*
* @return string The type name
*/
protected static function qtype_name() {
return 'drawlines';
}

/**
* Returns the paths to be handled by the plugin at question level.
*
* @return array
*/
protected function define_question_plugin_structure() {

$paths = [];

$elements = ['qtype_drawlines' => '/drawlines', 'qtype_drawlines_lines' => '/lines/line'];

foreach ($elements as $elename => $path) {
$elepath = $this->get_pathfor($path);
$paths[] = new restore_path_element($elename, $elepath);
}

return $paths;
}

/**
*
* Process the qtype_drawlines element.
*
* @param array $data
*/
public function process_qtype_drawlines(array $data): void {
self::process_qtype_drawlines_data_with_table_name($data, 'qtype_drawlines_options');
}

/**
*
* Process the qtype_drawlines_lines element.
*
* @param array $data
*/
public function process_qtype_drawlines_lines(array $data): void {
self::process_qtype_drawlines_data_with_table_name($data, 'qtype_drawlines_lines');
}

/**
* Process the qtype drawlines data with the table name.
*
* @param array $data XML data.
* @param string $tablename Table name
*/
private function process_qtype_drawlines_data_with_table_name(array $data, string $tablename): void {
global $DB;
$data = (object)$data;
$oldid = $data->id;

// Detect if the question is created or mapped.
$questioncreated = $this->get_mappingid('question_created',
$this->get_old_parentid('question'));

// If the question has been created by restore, we need to create its question_drawlines too.
if ($questioncreated) {
// Adjust some columns.
$data->questionid = $this->get_new_parentid('question');
// Insert record.
$newitemid = $DB->insert_record($tablename, $data);
// Create mapping.
$this->set_mapping($tablename, $oldid, $newitemid);
}
}

/**
* Return the contents of this qtype to be processed by the links decoder.
*/
public static function define_decode_contents(): array {
$contents = [];
$contents[] = new restore_decode_content('qtype_drawlines_options',
['correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback'], 'qtype_drawlines_options');
return $contents;
}
}
27 changes: 12 additions & 15 deletions edit_drawlines_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class qtype_drawlines_edit_form extends question_edit_form {
/** @var int Number of lines. */
protected $numberoflines;

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

/**
Expand Down Expand Up @@ -74,7 +74,8 @@ public static function file_picker_options(): array {
return $filepickeroptions;
}

protected function definition_inner($mform) {
#[\Override]
protected function definition_inner($mform): void {

$this->set_current_settings();

Expand Down Expand Up @@ -130,7 +131,8 @@ protected function get_hint_fields($withclearwrong = false, $withshownumpartscor
return [$repeated, $repeatedoptions];
}

public function data_preprocessing($question) {
#[\Override]
public function data_preprocessing($question): object {
$question = parent::data_preprocessing($question);
$question = $this->data_preprocessing_options($question);
$question = $this->data_preprocessing_lines($question);
Expand All @@ -145,7 +147,7 @@ public function data_preprocessing($question) {
self::file_picker_options());
$question->bgimage = $draftitemid;

// TODO: Require js amd module for fom.
// TODO: Require js amd module for form.

return $question;
}
Expand Down Expand Up @@ -193,14 +195,7 @@ protected function data_preprocessing_lines(stdClass $question): object {
return $question;
}

/**
* Perform the necessary preprocessing for the hint fields.
*
* @param object $question The data being passed to the form.
* @param bool $withclearwrong Clear wrong hints.
* @param bool $withshownumpartscorrect Show number correct.
* @return object The modified data.
*/
#[\Override]
protected function data_preprocessing_hints($question, $withclearwrong = false,
$withshownumpartscorrect = false) {
if (empty($question->hints)) {
Expand All @@ -216,7 +211,8 @@ protected function data_preprocessing_hints($question, $withclearwrong = false,
return $question;
}

public function validation($data, $files) {
#[\Override]
public function validation($data, $files): array {
$errors = parent::validation($data, $files);
$bgimagesize = $this->get_image_size_in_draft_area($data['bgimage']);
if (!$bgimagesize === null) {
Expand Down Expand Up @@ -275,9 +271,10 @@ public function get_image_size_in_draft_area($draftitemid) {
* @param string $label the label to use for each line.
*/
protected function add_per_line_fields(MoodleQuickForm $mform, string $label) {
$repeatsatstart = line::LINE_NUMBER_START;
if (isset($this->question->lines)) {
$repeatsatstart = count((array)$this->question->lines);
$repeatsatstart = count($this->question->lines);
} else {
$repeatsatstart = line::LINE_NUMBER_START;
}
$repeatedoptions = [];
$this->repeat_elements($this->get_per_line_fields($mform, $label, $repeatedoptions),
Expand Down
41 changes: 41 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Serve question type files.
*
* @package qtype_drawlines
* @copyright 2024 The Open University
* @author The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Checks file access for drawlines questions.
*
* @param object $course The course we are in
* @param object $cm Course module
* @param object $context The context object
* @param string $filearea the name of the file area.
* @param array $args the remaining bits of the file path.
* @param bool $forcedownload whether the user must be forced to download the file.
* @param array $options additional options affecting the file serving
*/
function qtype_drawlines_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
question_pluginfile($course, $context, 'qtype_drawlines', $filearea, $args, $forcedownload, $options);
}
13 changes: 11 additions & 2 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[AllowDynamicProperties]
class qtype_drawlines_question extends question_graded_automatically_with_countback {

/** @var lines[], an array of line objects. */
Expand All @@ -31,7 +32,7 @@ class qtype_drawlines_question extends question_graded_automatically_with_countb
/** @var int The number of lines. */
public $numberoflines;


#[\Override]
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($filearea === 'bgimage') {
$validfilearea = true;
Expand Down Expand Up @@ -154,6 +155,7 @@ protected function choose_hits(array $response) {
}
return $chosenhits;
}

public function total_number_of_items_dragged(array $response) {
$total = 0;
foreach ($this->choiceorder[1] as $choice) {
Expand Down Expand Up @@ -206,6 +208,7 @@ protected function get_all_hits(array $response) {
return $hits;
}

#[\Override]
public function get_right_choice_for($place) {
$group = $this->places[$place]->group;
foreach ($this->choiceorder[$group] as $choicekey => $choiceid) {
Expand All @@ -215,12 +218,14 @@ public function get_right_choice_for($place) {
}
return null;
}
#[\Override]
public function grade_response(array $response) {
list($right, $total) = $this->get_num_parts_right($response);
$fraction = $right / $total;
return [$fraction, question_state::graded_state_for_fraction($fraction)];
}

#[\Override]
public function compute_final_grade($responses, $totaltries) {
$maxitemsdragged = 0;
$wrongtries = [];
Expand All @@ -246,6 +251,7 @@ public function compute_final_grade($responses, $totaltries) {
return $grade;
}

#[\Override]
public function classify_response(array $response) {
$parts = [];
$hits = $this->choose_hits($response);
Expand All @@ -264,6 +270,7 @@ public function classify_response(array $response) {
return $parts;
}

#[\Override]
public function get_correct_response() {
$responsecoords = [];
foreach ($this->places as $placeno => $place) {
Expand All @@ -286,7 +293,8 @@ public function get_correct_response() {
return $response;
}

public function summarise_response(array $response) {
#[\Override]
public function summarise_response(array $response): null|string {
$hits = $this->choose_hits($response);
$goodhits = [];
foreach ($this->places as $placeno => $place) {
Expand All @@ -302,6 +310,7 @@ public function summarise_response(array $response) {
return implode(', ', $goodhits);
}

#[\Override]
public function get_random_guess_score() {
return null;
}
Expand Down
Loading

0 comments on commit fec85f9

Please sign in to comment.