diff --git a/classes/package.php b/classes/package.php index efe516cf..486da872 100644 --- a/classes/package.php +++ b/classes/package.php @@ -210,7 +210,10 @@ public function store_in_db(int $contextid = 0) { "icon" => $this->icon, "license" => $this->license ]; + + $transaction = $DB->start_delegated_transaction(); $packageid = $DB->insert_record('qtype_questionpy_package', $packagedata); + $transaction->allow_commit(); // For each language store the localized package data as a separate record. $languagedata = array(); @@ -231,25 +234,11 @@ public function store_in_db(int $contextid = 0) { "tag" => $tag, ]; } - $transaction = $DB->start_delegated_transaction(); - try { - try { - $DB->insert_records('qtype_questionpy_tags', $tagsdata); - $DB->insert_records('qtype_questionpy_language', $languagedata); - } catch (\dml_exception $e) { - $DB->rollback_delegated_transaction($transaction, $e); - } - } catch (\coding_exception | \dml_transaction_exception | \Throwable $e) { - throw new moodle_exception("dmltransactionexception", "error", "", null, - "Cannot delete records from DB. Transaction rollback failed."); - } - if ($transaction->is_disposed()) { - // Transaction was rolled back earlier. - throw new moodle_exception("invalidrecordunknown", "error", "", null, - "Cannot insert $tagsdata or $languagedata into table. Transaction rollback."); - } - $DB->commit_delegated_transaction($transaction); + $transaction = $DB->start_delegated_transaction(); + $DB->insert_records('qtype_questionpy_tags', $tagsdata); + $DB->insert_records('qtype_questionpy_language', $languagedata); + $transaction->allow_commit(); } /** @@ -262,27 +251,13 @@ public function store_in_db(int $contextid = 0) { */ public function delete_from_db() { global $DB; + $transaction = $DB->start_delegated_transaction(); $packageid = $DB->get_field('qtype_questionpy_package', 'id', ['hash' => $this->hash]); - try { - try { - $DB->delete_records('qtype_questionpy_package', ['id' => $packageid]); - $DB->delete_records('qtype_questionpy_language', ['packageid' => $packageid]); - $DB->delete_records('qtype_questionpy_tags', ['packageid' => $packageid]); - } catch (\dml_exception $e) { - $DB->rollback_delegated_transaction($transaction, $e); - } - } catch (\coding_exception | \dml_transaction_exception | \Throwable $e) { - throw new moodle_exception("dmltransactionexception", "error", "", null, - "Cannot delete records from DB. Transaction rollback failed: $e->debuginfo"); - } - - if ($transaction->is_disposed()) { - // Transaction was rolled back earlier. - throw new moodle_exception("invalidrecordunknown", "error", "", null, - "Cannot delete records from DB. Transaction rollback."); - } - $DB->commit_delegated_transaction($transaction); + $DB->delete_records('qtype_questionpy_package', ['id' => $packageid]); + $DB->delete_records('qtype_questionpy_language', ['packageid' => $packageid]); + $DB->delete_records('qtype_questionpy_tags', ['packageid' => $packageid]); + $transaction->allow_commit(); } /**