|
54 | 54 | *
|
55 | 55 | * A further complication is that during an install of both Moodle and
|
56 | 56 | * CodeRunner together, the question bank module is installed after the question
|
57 |
| - * type, so we can't load the prototypes during the install. Instead we |
58 |
| - * schedule a task to do it later. |
| 57 | + * type, so we can't load the prototypes during the install. |
59 | 58 | *
|
60 | 59 | * @return bool true if successful
|
61 | 60 | */
|
62 | 61 |
|
63 | 62 | /**
|
64 |
| - * Schedule or perform the question type update depending on whether this is an |
65 |
| - * install or an upgrade. |
66 |
| - * @param bool $isinstall true if this is being called during install. |
| 63 | + * Update the question types. In Moode 4.6 or later, this is done in a |
| 64 | + * scheduled task that runs after the install/update. |
67 | 65 | * @return bool true if successful
|
68 | 66 | */
|
69 |
| -function update_question_types($isinstall = false) { |
70 |
| - if ($isinstall) { |
71 |
| - // During install, schedule the task for later execution. |
| 67 | +function update_question_types() { |
| 68 | + if (qtype_coderunner_util::using_mod_qbank()) { |
| 69 | + // In Moodle >=4.6, we need to update the question prototypes in a scheduled task |
| 70 | + // because (a) if this is an install, the qbank module hasn't been installed yet and |
| 71 | + // (b) we need to call question_bank_helper::get_default_open_instance_system_type |
| 72 | + // which cannot be used during install/upgrade. |
72 | 73 | $task = new qtype_coderunner\task\qtype_coderunner_setup_question_prototypes();
|
73 | 74 | core\task\manager::queue_adhoc_task($task);
|
74 | 75 | return true;
|
75 | 76 | } else {
|
76 |
| - // During upgrade, execute immediately. |
| 77 | + // In Moodle 4.5 or earlier, we can execute the update immediately. |
77 | 78 | return update_question_types_internal();
|
78 | 79 | }
|
79 | 80 | }
|
80 | 81 |
|
81 | 82 | /**
|
82 |
| - * The function that actually does the update. May be called directly during update |
83 |
| - * or indirectly from the queued task during install. |
| 83 | + * The function that actually does the update. May be called directly during update if using |
| 84 | + * moodle4.5 or earier, or indirectly from the queued task with later versions. |
84 | 85 | */
|
85 | 86 | function update_question_types_internal() {
|
86 | 87 | mtrace("Setting up CodeRunner question prototypes...");
|
@@ -108,6 +109,10 @@ function update_question_types_internal() {
|
108 | 109 | * If we can find an existing CR_PROTOTYPES category in the (defunct) system context,
|
109 | 110 | * move it to the new top category in the question bank instance on the front page.
|
110 | 111 | * Then delete all existing prototypes in the new top category and reload them.
|
| 112 | + * Note that this code is always running in a post-update scheduled task, because |
| 113 | + * question_bank_helper::get_default_open_instance_system_type, needed by |
| 114 | + * moodle5_top_category, cannot be used during update/install. |
| 115 | + * |
111 | 116 | * @return bool true if successful
|
112 | 117 | */
|
113 | 118 | function update_question_types_with_qbank() {
|
@@ -196,7 +201,7 @@ function moodle5_top_category() {
|
196 | 201 | $newmod = question_bank_helper::create_default_open_instance($course, $bankname, question_bank_helper::TYPE_SYSTEM);
|
197 | 202 | }
|
198 | 203 | } catch (Exception $e) {
|
199 |
| - throw new coding_exception('Upgrade failed: error creating system question bank'); |
| 204 | + throw new coding_exception("Upgrade failed: error creating system question bank: $e"); |
200 | 205 | }
|
201 | 206 | $newtopcategory = question_get_top_category($newmod->context->id, true);
|
202 | 207 | return $newtopcategory;
|
|
0 commit comments