Skip to content

Commit d21921d

Browse files
authored
Merge pull request #13 from maths/main
Update branch to main
2 parents 4461795 + bef870b commit d21921d

File tree

15 files changed

+231
-54
lines changed

15 files changed

+231
-54
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ jobs:
1212
- php: '8.3'
1313
moodle-branch: 'main'
1414
database: 'pgsql'
15+
- php: '8.3'
16+
moodle-branch: 'MOODLE_501_STABLE'
17+
database: 'pgsql'
18+
- php: '8.3'
19+
moodle-branch: 'MOODLE_500_STABLE'
20+
database: 'pgsql'
1521
- php: '8.3'
1622
moodle-branch: 'MOODLE_405_STABLE'
1723
database: 'pgsql'
@@ -33,7 +39,7 @@ jobs:
3339

3440
services:
3541
postgres:
36-
image: postgres:14
42+
image: postgres:16
3743
env:
3844
POSTGRES_USER: 'postgres'
3945
POSTGRES_HOST_AUTH_METHOD: 'trust'
@@ -42,7 +48,7 @@ jobs:
4248
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
4349

4450
mariadb:
45-
image: mariadb:10
51+
image: mariadb:10.6
4652
env:
4753
MYSQL_USER: 'root'
4854
MYSQL_ALLOW_EMPTY_PASSWORD: "true"

CHANGES.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Change log for the Import question as new version question bank plugin
22

3+
## Changes in version 1.4
4+
5+
* Changed to default page style instead of popup style
6+
* Added pass through of error messages and notices during import
7+
* Add verified compatibility with Moodle 5.1
8+
9+
## Changes in version 1.3
10+
11+
* Add verified compatibility with Moodle 5.0
12+
13+
## Changes in version 1.2
14+
15+
* Add verified compatibility with Moodle 4.4 and 4.5
16+
317
## Changes in version 1.1
418

519
* Provide compatibility with Moodle 4.3.

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ Install from the Moodle plugins database https://moodle.org/plugins/qbank_import
1616

1717
### Install using git
1818

19-
Or you can install using git. Type this commands in the root of your Moodle install
19+
Or you can install using git. For Moodle versions up to 5.0, type these commands in the root folder of your Moodle install:
2020

2121
git clone https://github.com/maths/moodle-qbank_importasversion.git question/bank/importasversion
2222
echo /question/bank/importasversion/ >> .git/info/exclude
2323

24+
Moodle 5.1 introduced a new folder structure. From the root folder, type these commands:
25+
26+
git clone https://github.com/maths/moodle-qbank_importasversion.git public/question/bank/importasversion
27+
echo /public/question/bank/importasversion/ >> .git/info/exclude
28+
2429
Then run the moodle update process
30+
2531
Site administration > Notifications
2632

2733

classes/event/question_version_imported.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3737
*/
3838
class question_version_imported extends question_base {
39-
4039
#[\Override]
4140
protected function init() {
4241
parent::init();

classes/form/import_form.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@
4242
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4343
*/
4444
class import_form extends moodleform {
45-
4645
#[\Override]
4746
protected function definition() {
4847
$mform = $this->_form;
4948

50-
$mform->addElement('filepicker', 'newfile', get_string('import'),
51-
null, ['accepted_types' => '.xml']);
49+
$mform->addElement(
50+
'filepicker',
51+
'newfile',
52+
get_string('import'),
53+
null,
54+
['accepted_types' => '.xml']
55+
);
5256
$mform->addRule('newfile', null, 'required', null, 'client');
5357

5458
$mform->addElement('hidden', 'id');

classes/import_as_version_action_column.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
// Support multiple Moodle versions. This can be cleaned up once 4.3 is the lowest supported version.
2222
if (!class_exists('\core_question\local\bank\question_action_base')) {
2323
// Moodle up to 4.2.x.
24-
class_alias('\core_question\local\bank\menu_action_column_base',
25-
'qbank_importasversion\qbank_importasversion_column_parent_class');
24+
class_alias(
25+
'\core_question\local\bank\menu_action_column_base',
26+
'qbank_importasversion\qbank_importasversion_column_parent_class'
27+
);
2628
} else {
2729
// Moodle 4.3+.
28-
class_alias('\core_question\local\bank\question_action_base',
29-
'qbank_importasversion\qbank_importasversion_column_parent_class');
30+
class_alias(
31+
'\core_question\local\bank\question_action_base',
32+
'qbank_importasversion\qbank_importasversion_column_parent_class'
33+
);
3034
}
3135

3236
/**
@@ -37,7 +41,6 @@ class_alias('\core_question\local\bank\question_action_base',
3741
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3842
*/
3943
class import_as_version_action_column extends qbank_importasversion_column_parent_class {
40-
4144
/** @var string store the value of the name lang string for performance. */
4245
protected $actionname;
4346

classes/importer.php

100755100644
Lines changed: 83 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,43 @@
3939
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4040
*/
4141
class importer extends qformat_xml {
42-
4342
/**
4443
* Import a file to update a given question.
4544
*
4645
* @param qformat_xml $qformat an instance of
4746
* @param question_definition $question the question to add a version to.
4847
* @param string $importedquestionfile filename of the file to import.
49-
* @return bool true on success. False on failure.
48+
* @return object|boolean Either a simple object with error and/or notice properties when there are issues
49+
* or true on success.
5050
*/
5151
public static function import_file(
5252
qformat_xml $qformat,
5353
question_definition $question,
5454
string $importedquestionfile
5555
) {
56-
global $USER, $DB, $OUTPUT;
56+
global $USER, $DB;
5757

5858
$context = context::instance_by_id($question->contextid);
5959

6060
// STAGE 1: Parse the file.
6161
if (! $importedlines = $qformat->readdata($importedquestionfile)) {
62-
throw new exception(get_string('cannotread', 'question'));
62+
$result = new stdClass();
63+
$result->error = get_string('cannotread', 'question');
64+
return $result;
6365
}
6466

6567
// Extract all the questions.
6668
if (!$importedquestions = $qformat->readquestions($importedlines)) {
67-
throw new exception(get_string('noquestionsinfile', 'question'));
69+
$result = new stdClass();
70+
$result->error = get_string('noquestionsinfile', 'question');
71+
return $result;
6872
}
6973

7074
// Check if there's only one question in the file -- remove this once we do batch processing!
7175
if (count($importedquestions) != 1) {
72-
throw new exception(get_string('toomanyquestionsinfile', 'qbank_importasversion'));
76+
$result = new stdClass();
77+
$result->error = get_string('toomanyquestionsinfile', 'qbank_importasversion');
78+
return $result;
7379
}
7480

7581
// Count number of questions processed.
@@ -111,23 +117,45 @@ public static function import_file(
111117
$questionversion->id = $DB->insert_record('question_versions', $questionversion);
112118

113119
if (isset($newquestion->questiontextitemid)) {
114-
$newquestion->questiontext = file_save_draft_area_files($newquestion->questiontextitemid,
115-
$context->id, 'question', 'questiontext', $newquestion->id,
116-
$fileoptions, $newquestion->questiontext);
120+
$newquestion->questiontext = file_save_draft_area_files(
121+
$newquestion->questiontextitemid,
122+
$context->id,
123+
'question',
124+
'questiontext',
125+
$newquestion->id,
126+
$fileoptions,
127+
$newquestion->questiontext
128+
);
117129
} else if (isset($newquestion->questiontextfiles)) {
118130
foreach ($newquestion->questiontextfiles as $file) {
119131
question_bank::get_qtype($newquestion->qtype)->import_file(
120-
$context, 'question', 'questiontext', $newquestion->id, $file);
132+
$context,
133+
'question',
134+
'questiontext',
135+
$newquestion->id,
136+
$file
137+
);
121138
}
122139
}
123140
if (isset($newquestion->generalfeedbackitemid)) {
124-
$newquestion->generalfeedback = file_save_draft_area_files($newquestion->generalfeedbackitemid,
125-
$context->id, 'question', 'generalfeedback', $newquestion->id,
126-
$fileoptions, $newquestion->generalfeedback);
141+
$newquestion->generalfeedback = file_save_draft_area_files(
142+
$newquestion->generalfeedbackitemid,
143+
$context->id,
144+
'question',
145+
'generalfeedback',
146+
$newquestion->id,
147+
$fileoptions,
148+
$newquestion->generalfeedback
149+
);
127150
} else if (isset($newquestion->generalfeedbackfiles)) {
128151
foreach ($newquestion->generalfeedbackfiles as $file) {
129152
question_bank::get_qtype($newquestion->qtype)->import_file(
130-
$context, 'question', 'generalfeedback', $newquestion->id, $file);
153+
$context,
154+
'question',
155+
'generalfeedback',
156+
$newquestion->id,
157+
$file
158+
);
131159
}
132160
}
133161
$DB->update_record('question', $newquestion);
@@ -148,29 +176,48 @@ public static function import_file(
148176
if ($isimportingcontextcourseoractivity) {
149177
$mergedtags = array_merge($newquestion->coursetags, $newquestion->tags);
150178

151-
core_tag_tag::set_item_tags('core_question', 'question', $newquestion->id,
152-
$newquestion->context, $mergedtags);
179+
core_tag_tag::set_item_tags(
180+
'core_question',
181+
'question',
182+
$newquestion->id,
183+
$newquestion->context,
184+
$mergedtags
185+
);
153186
} else {
154-
core_tag_tag::set_item_tags('core_question', 'question', $newquestion->id,
155-
context_course::instance($qformat->course->id), $newquestion->coursetags);
187+
core_tag_tag::set_item_tags(
188+
'core_question',
189+
'question',
190+
$newquestion->id,
191+
context_course::instance($qformat->course->id),
192+
$newquestion->coursetags
193+
);
156194

157195
if (!empty($newquestion->tags)) {
158-
core_tag_tag::set_item_tags('core_question', 'question', $newquestion->id,
159-
$importingcontext, $newquestion->tags);
196+
core_tag_tag::set_item_tags(
197+
'core_question',
198+
'question',
199+
$newquestion->id,
200+
$importingcontext,
201+
$newquestion->tags
202+
);
160203
}
161204
}
162205
} else if (!empty($newquestion->tags)) {
163-
core_tag_tag::set_item_tags('core_question', 'question', $newquestion->id,
164-
$newquestion->context, $newquestion->tags);
206+
core_tag_tag::set_item_tags(
207+
'core_question',
208+
'question',
209+
$newquestion->id,
210+
$newquestion->context,
211+
$newquestion->tags
212+
);
165213
}
166214
}
167215

168216
if (!empty($result->error)) {
169-
echo $OUTPUT->notification($result->error);
170217
// Can't use $transaction->rollback(); since it requires an exception,
171218
// and I don't want to rewrite this code to change the error handling now.
172219
$DB->force_transaction_rollback();
173-
return false;
220+
return $result;
174221
}
175222

176223
question_version_imported::create([
@@ -185,11 +232,19 @@ public static function import_file(
185232

186233
$transaction->allow_commit();
187234

188-
if (!empty($result->notice)) {
189-
echo $OUTPUT->notification($result->notice);
190-
return true;
235+
if ($result === null) {
236+
// Some question types don't have a return value when saving options.
237+
// If it hasn't thrown an Exception then it's fine.
238+
$result = true;
239+
}
240+
if ($result === false) {
241+
// This probably shouldn't happen but given all the question types out there
242+
// it's probably worth making sure we're handling it.
243+
$result = new stdClass();
244+
$result->error = get_string('unknownerror', 'qbank_importasversion');
245+
return $result;
191246
}
192247

193-
return true;
248+
return $result;
194249
}
195250
}

classes/plugin_feature.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2828
*/
2929
class plugin_feature extends plugin_features_base {
30-
3130
#[\Override]
3231
public function get_question_actions(view $qbank): array {
3332
// This is what is used in Moodle 4.3+.

classes/privacy/provider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2828
*/
2929
class provider implements null_provider {
30-
3130
#[\Override]
3231
public static function get_reason(): string {
3332
return 'privacy:metadata';

0 commit comments

Comments
 (0)