Skip to content

Commit 937e8c7

Browse files
Fix codechecker errors
1 parent a410f7b commit 937e8c7

14 files changed

+133
-168
lines changed

backup/moodle2/backup_qtype_drawlines_plugin.class.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ protected function define_question_plugin_structure() {
4242

4343
$plugin->add_child($pluginwrapper);
4444

45-
$options = new backup_nested_element('drawlines', ['id'], ['grademethod',
46-
'correctfeedback', 'correctfeedbackformat',
47-
'partiallycorrectfeedback', 'partiallycorrectfeedbackformat',
48-
'incorrectfeedback', 'incorrectfeedbackformat',
49-
'shownumcorrect', 'showmisplaced',
45+
$options = new backup_nested_element('drawlines', ['id'], [
46+
'grademethod',
47+
'correctfeedback', 'correctfeedbackformat',
48+
'partiallycorrectfeedback', 'partiallycorrectfeedbackformat',
49+
'incorrectfeedback', 'incorrectfeedbackformat',
50+
'shownumcorrect', 'showmisplaced',
5051
]);
5152
$pluginwrapper->add_child($options);
5253

@@ -69,12 +70,11 @@ protected function define_question_plugin_structure() {
6970
* files to be processed both in backup and restore.
7071
*/
7172
public static function get_qtype_fileareas() {
72-
return
73-
[
74-
'bgimage' => 'question_created',
75-
'correctfeedback' => 'question_created',
76-
'partiallycorrectfeedback' => 'question_created',
77-
'incorrectfeedback' => 'question_created',
73+
return [
74+
'bgimage' => 'question_created',
75+
'correctfeedback' => 'question_created',
76+
'partiallycorrectfeedback' => 'question_created',
77+
'incorrectfeedback' => 'question_created',
7878
];
7979
}
8080
}

classes/line.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
namespace qtype_drawlines;
18-
use qtype_drawlines\dragitem;
19-
use qtype_drawlines\dropzone;
18+
19+
defined('MOODLE_INTERNAL') || die;
2020

2121
/**
22-
* Represents a line objet of drawlines question.
22+
* Represents a line object of drawlines question.
2323
*
2424
* @package qtype_drawlines
2525
* @copyright 2024 The Open University
@@ -240,12 +240,11 @@ public static function are_response_coordinates_valid(string $linecoordinates):
240240
return true;
241241
}
242242

243-
244243
public static function make_drop_zone(int $linenumber, string $label, string $zone): drop_zone {
244+
// TODO: Need to chek if we need this.
245245
[$xleft, $ytop] = self::parse_into_cx_cy_with_or_without_radius($zone);
246-
return new drop_zone($linenumber, $label, $xleft, $ytop);
246+
return new drop_zone($linenumber, $label, $xleft, $ytop);
247247
}
248-
249248
}
250249

251250
/**

edit_drawlines_form.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ protected function data_preprocessing_hints($question, $withclearwrong = false,
215215
return $question;
216216
}
217217

218+
/**
219+
* The needed JS setup for this question type.
220+
*/
218221
public function js_call() {
219222
global $PAGE;
220223
$PAGE->requires->js_call_amd('qtype_drawlines/form', 'init');

lang/en/qtype_drawlines.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,31 @@
2727

2828
$string['bgimage'] = 'Background image';
2929

30-
$string['correctansweris'] = 'The correct answer is: {$a}';
31-
32-
$string['dropbackground'] = 'Background image for DrawLines';
33-
3430
$string['correctansweris'] = 'The correct answer is: {$a}';
3531
$string['correctanswersare'] = 'The correct answers are: {$a}';
32+
$string['dropbackground'] = 'Background image for DrawLines';
3633

3734
$string['formerror_nobgimage'] = 'You need to select an image to use as the background for the drag and drop area.';
3835
$string['formerror_notype'] = 'You have to select a type for Line {$a}';
39-
$string['formerror_zonestart'] = 'Start zone coordinates should be in x,y;r format, where x,y are the coordinates of the centre of a circle and r is the radius.';
4036
$string['formerror_zoneend'] = 'End zone coordinates should be in x,y;r format, where x,y are the coordinates of the centre of a circle and r is the radius.';
37+
$string['formerror_zonestart'] = 'Start zone coordinates should be in x,y;r format, where x,y are the coordinates of the centre of a circle and r is the radius.';
4138

4239
$string['grademethod'] = 'Grading type';
4340
$string['grademethod_desc'] = 'Give partial credit (default): each correct response in the body cells are worth one point, so students score a percentage of the total correct responses.
4441
All or nothing: students must get every response correct, otherwise they score zero.';
4542
$string['grademethod_help'] = 'Give partial credit (default): each correct response worth one point, so students score a percentage of the total correct responses.
4643
4744
All or nothing: students must get every response correct, otherwise they score zero.';
48-
$string['gradepartialcredit'] = 'Give partial credit';
4945
$string['gradeallornothing'] = 'All-or-nothing';
46+
$string['gradepartialcredit'] = 'Give partial credit';
5047

51-
$string['labelstart'] = 'Start label';
52-
$string['labelmiddle'] = 'Mid label';
5348
$string['labelend'] = 'End label';
49+
$string['labelmiddle'] = 'Mid label';
50+
$string['labelstart'] = 'Start label';
51+
$string['linedoublearrows'] = 'Double arrows ←--→';
5452
$string['linesegment'] = 'Line segment ---';
5553
$string['linesinglearrow'] = 'Single arrow -→';
56-
$string['linedoublearrows'] = 'Double arrows ←--→';
54+
5755
$string['lineinfinite'] = 'Infinite line --o--o--';
5856
$string['linexheader'] = 'Line {no}';
5957

@@ -76,8 +74,8 @@
7674

7775
$string['showmisplaced'] = 'State which zones are incorrectly placed';
7876
$string['summarisechoice'] = '{$a->no}. {$a->text}';
79-
$string['summariseplace'] = '{$a->no}. {$a->text}';
8077
$string['summarisechoiceno'] = 'Item {$a}';
78+
$string['summariseplace'] = '{$a->no}. {$a->text}';
8179
$string['summariseplaceno'] = 'Drop zone {$a}';
8280

8381
$string['type'] = 'Type';
@@ -86,11 +84,11 @@
8684
$string['xleft'] = 'Left';
8785

8886
$string['yougot1right'] = 'You have correctly selected one point.';
89-
$string['yougotnright'] = 'You have correctly selected {$a->num} points.';
9087
$string['yougot1rightline'] = 'You have correctly selected one line.';
88+
$string['yougotnright'] = 'You have correctly selected {$a->num} points.';
9189
$string['yougotnrightline'] = 'You have correctly selected {$a->num} lines.';
9290
$string['ytop'] = 'Top';
9391

9492
$string['zonecoordinates'] = 'x,y;r (where x,y are the coordinates of the centre of the circle and r is the radius)';
95-
$string['zonestart'] = 'Start zone coordinates';
9693
$string['zoneend'] = 'End zone coordinates';
94+
$string['zonestart'] = 'Start zone coordinates';

question.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2525
*/
2626
#[AllowDynamicProperties]
27-
//class qtype_drawlines_question extends question_graded_automatically_with_countback
28-
// implements question_automatically_gradable_with_countback {
2927
class qtype_drawlines_question extends question_graded_automatically {
3028

3129
/** @var lines[], an array of line objects. */
@@ -53,6 +51,7 @@ class qtype_drawlines_question extends question_graded_automatically {
5351
*/
5452
public $places;
5553

54+
/** @var array The order of the lines, key => choice number in the format c0, c1... */
5655
public $choicesorder;
5756

5857
#[\Override]
@@ -86,12 +85,12 @@ public function apply_attempt_state(question_attempt_step $step) {
8685
public function compute_final_grade(array $responses, int $totaltries): float {
8786
// TODO: To incorporate the question penalty for interactive with multiple tries behaviour.
8887

89-
$grade_value = 0;
90-
foreach($responses as $response) {
88+
$grade = 0;
89+
foreach ($responses as $response) {
9190
[$fraction, $state] = $this->grade_response($response);
92-
$grade_value += $fraction;
91+
$grade += $fraction;
9392
}
94-
return $grade_value;
93+
return $grade;
9594
}
9695

9796
/**
@@ -121,7 +120,7 @@ public function check_file_access($qa, $options, $component, $filearea, $args, $
121120
}
122121
#[\Override]
123122
public function get_expected_data() {
124-
$expecteddata =[];
123+
$expecteddata = [];
125124
foreach ($this->lines as $line) {
126125
$expecteddata[$this->choice($line->number - 1)] = PARAM_NOTAGS;
127126
}
@@ -213,7 +212,7 @@ public function arrays_same_at_key_integer(array $array1, array $array2, $key) {
213212
* @author dml at nm dot ru (taken from comments on php manual)
214213
*/
215214
protected function array_intersect_fixed($array1, $array2) {
216-
$result = array();
215+
$result = [];
217216
foreach ($array1 as $val) {
218217
if (($key = array_search($val, $array2, true)) !== false) {
219218
$result[] = $val;
@@ -235,10 +234,10 @@ public function get_validation_error(array $response): string {
235234
public function get_num_parts_right(array $response): array {
236235
$numpartrightstart = 0;
237236
$numpartrightend = 0;
238-
if(!$response) {
237+
if (!$response) {
239238
return [0, 0];
240239
}
241-
foreach($this->lines as $key => $line) {
240+
foreach ($this->lines as $key => $line) {
242241
if (array_key_exists($this->choice($key), $response) && $response[$this->choice($key)] !== '') {
243242
$coords = explode(' ', $response[$this->choice($key)]);
244243
if (line::is_dragitem_in_the_right_place($coords[0], $line->zonestart)) {
@@ -275,14 +274,14 @@ public function grade_response(array $response) {
275274
public function compute_distance_to_line(float $x1, float $y1, float $x2, float $y2, float $x, float $y): float {
276275
//print_object("float $x1, float $y1, float $x2, float $y2, float $x, float $y");
277276
//print_object(($x2 - $x1)**2);
278-
return sqrt(($x - $x1)**2 + ($y - $y1)**2 -
279-
(($x2 - $x1) * ($x - $x1) + ($y2 - $y1)*($y - $y1))**2/(($x2 - $x1)**2 + ($y2 - $y1)**2));
277+
return sqrt(($x - $x1) ** 2 + ($y - $y1) ** 2 -
278+
(($x2 - $x1) * ($x - $x1) + ($y2 - $y1)*($y - $y1)) ** 2/(($x2 - $x1) ** 2 + ($y2 - $y1) ** 2));
280279
}
281280

282281
#[\Override]
283282
public function classify_response(array $response) {
283+
// TODO: Need to check with function.
284284
$parts = [];
285-
//$hits = $this->choose_hits($response);
286285
foreach ($this->places as $placeno => $place) {
287286
if (isset($hits[$placeno])) {
288287
$shuffledchoiceno = $this->get_right_choice_for($placeno);

questiontype.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ class qtype_drawlines extends question_type {
3030
/** @var lines[], an array of line objects. */
3131
public $lines;
3232

33-
//protected function make_choice(stdClass $dragdata) {
34-
// return new drag_item($dragdata->linenumber, $dragdata->label);
35-
//}
36-
37-
//protected function make_place(stdclass $dropdata) {
38-
// return new drop_zone($dropdata->linenumber, $dropdata->label, $dropdata->xleft, $dropdata->ytop);
39-
//}
40-
4133
#[\Override]
4234
public function get_question_options($question): bool {
4335
global $DB, $OUTPUT;
@@ -95,17 +87,12 @@ public function save_question_options($fromform) {
9587
public function save_lines(stdClass $fromform): void {
9688
global $DB;
9789
$numberoflines = $fromform->numberoflines;
98-
$coordsregexp = "/^\d+,\d+;\d+$/";
9990
$index = 1;
10091
for ($i = 0; $i < $numberoflines; $i++) {
10192
// If line type is not set do not save the line object.
10293
if (!in_array($fromform->type[$i], array_keys(line::get_line_types()))) {
10394
continue;
10495
}
105-
// If line coordinates are not set or not in the right format, then do not save the line object.
106-
if (!preg_match($coordsregexp, $fromform->zonestart[$i]) || !preg_match($coordsregexp, $fromform->zoneend[$i])) {
107-
continue;
108-
}
10996
$line = new stdClass;
11097
$line->questionid = $fromform->id;
11198
$line->number = $index++;
@@ -228,7 +215,8 @@ protected function initialise_question_lines(question_definition $question, $que
228215
$placeinex = 1;
229216
foreach ($questiondata->lines as $line) {
230217
$question->lines[$line->number - 1] = $this->make_line($line);
231-
$question->choices['c' . $index] = Line::get_coordinates($line->zonestart) . ' ' . Line::get_coordinates($line->zoneend);
218+
$question->choices['c' . $index] = Line::get_coordinates($line->zonestart) .
219+
' ' . Line::get_coordinates($line->zoneend);
232220
$index++;
233221
$question->places[$placeinex++] = line::make_drop_zone(
234222
$line->number, $line->labelstart ?? 's' . $line->number, $line->zonestart);

renderer.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,32 @@ protected function hidden_field_for_qt_var(question_attempt $qa, $varname, $valu
8585
$value = $qa->get_last_qt_var($varname);
8686
}
8787
$fieldname = $qa->get_qt_field_name($varname);
88-
$attributes = array('type' => 'hidden',
88+
$attributes = ['type' => 'hidden',
8989
'id' => str_replace(':', '_', $fieldname),
9090
'name' => $fieldname,
91-
'value' => $value);
91+
'value' => $value];
9292
if ($classes !== null) {
9393
$attributes['class'] = join(' ', $classes);
9494
}
9595
return [$fieldname, html_writer::empty_tag('input', $attributes)."\n"];
9696
}
9797

98-
protected function hidden_field_choice(question_attempt $qa, $choicenumber, $value = null, $class = null) {
99-
$varname = 'c'. $choicenumber;
100-
$classes = ['choices', 'choice'. $choicenumber];
101-
[, $html] = $this->hidden_field_for_qt_var($qa, $varname, $value, $classes);
102-
return $html;
103-
}
98+
/**
99+
* Generate the hidden fields on the preview page to capture the responses.
100+
* @param question_attempt $qa
101+
* @param $choicenumber
102+
* @param $value
103+
* @param $class
104+
* @return mixed
105+
*/
106+
protected function hidden_field_choice(question_attempt $qa, $choicenumber, $value = null, $class = null) {
107+
$varname = 'c'. $choicenumber;
108+
$classes = ['choices', 'choice'. $choicenumber];
109+
[, $html] = $this->hidden_field_for_qt_var($qa, $varname, $value, $classes);
110+
return $html;
111+
}
104112

113+
#[\Override]
105114
public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
106115
$question = $qa->get_question();
107116
$response = $qa->get_last_qt_data();
@@ -150,12 +159,12 @@ public function formulation_and_controls(question_attempt $qa, question_display_
150159
$output .= html_writer::end_div();
151160

152161
$hiddenfields = '';
153-
foreach($question->lines as $line) {
162+
foreach ($question->lines as $line) {
154163
$hiddenfields .= $this->hidden_field_choice($qa, $line->number - 1);
155164
}
156165
$output .= html_writer::div($hiddenfields, 'dragchoices');
157166

158-
// Call to js
167+
// Call to js.
159168
$this->page->requires->js_call_amd('qtype_drawlines/question', 'init',
160169
[$qa->get_outer_question_div_unique_id(), $options->readonly, $visibilityzones, $question->lines]);
161170

@@ -164,6 +173,7 @@ public function formulation_and_controls(question_attempt $qa, question_display_
164173
return $output;
165174
}
166175

176+
#[\Override]
167177
public function specific_feedback(question_attempt $qa) {
168178
return $this->combined_feedback($qa);
169179
}

tests/backup_and_restore_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @copyright 2024 The Open University
3434
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
3535
*/
36-
class backup_and_restore_test extends \restore_date_testcase {
36+
final class backup_and_restore_test extends \restore_date_testcase {
3737

3838
/**
3939
* Backup and restore the course containing an drawlines question for testing drawlines backup and restore.

tests/behat/edit.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Feature: Test editing an DrawLines question
3535
# Correct the input for end zone coordinates.
3636
And I set the field "id_zonestart_0" to "10,10;12"
3737

38-
And I click on "Line 2" "button"
38+
And I click on "Line 2" "link"
3939
And I set the field "id_type_1" to "Choose"
4040
And I press "id_submitbutton"
4141
And I should see "You have to select a type for Line 2"

0 commit comments

Comments
 (0)