From fc6db1b0cd4483a51e3434bd292ba77645f87e39 Mon Sep 17 00:00:00 2001 From: Nick Freear Date: Thu, 8 Mar 2018 16:41:45 +0000 Subject: [PATCH] Bug #8, Catch `dml_exception` in conditional_embedded_survey [iet:10314222] * Plus, fix to function call in `survey-end/index` PHP [ci skip] --- classes/local/conditional_embedded_survey.php | 27 ++++++++++++++----- survey-end/index.php | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/classes/local/conditional_embedded_survey.php b/classes/local/conditional_embedded_survey.php index e59a80e..b77cb69 100644 --- a/classes/local/conditional_embedded_survey.php +++ b/classes/local/conditional_embedded_survey.php @@ -18,7 +18,8 @@ class conditional_embedded_survey extends base { const CONFIG_KEY = self::BASE_KEY . '_conditional_survey_activity'; const MOD_TYPE_NAME = 'assign'; // 'mod/assign' const MOD_TYPE_ID = 1; // ID in 'mdl_modules' table. - const EMBED_LIKE = '%%'; // MySQL 'LIKE' + // const EMBED_LIKE = '%%'; // MySQL 'LIKE' + const EMBED_REGEXP = '(<\/iframe>|[?#!]-pre-survey-embed)'; // MySQL 'REGEXP' protected $course_id; // 4, protected $course_code; // 'FR', @@ -60,11 +61,20 @@ protected function set_config( $course_code, $userid = null ) { } public function make_complete() { + $b_ok = false; if ($this->is_valid_module() && $this->activity_has_embed()) { - - $this->assign_grades(); - $this->assign_submission(); - return $this->course_module_complete(); + try { + $this->assign_grades(); + $this->assign_submission(); + $b_ok = $this->course_module_complete(); + } catch (\dml_write_exception $ex) { + self::debug([ __FUNCTION__, 'dml_write_exception', $ex->getMessage(), $ex->debuginfo ]); + + if (! preg_match('/Duplicate entry .+/', $ex->debuginfo)) { + throw $ex; + } + } + return $b_ok; } return false; } @@ -91,8 +101,11 @@ protected function is_valid_module() { protected function activity_has_embed() { global $DB; // Moodle global. - $result = $DB->get_record_sql( 'SELECT * FROM {assign} WHERE ' . $DB->sql_like( 'intro', ':intro' ) . ' AND id = :id ', - [ 'intro' => self::EMBED_LIKE, 'id' => $this->activity_id ]); + $result = $DB->get_record_sql( 'SELECT * FROM {assign} WHERE intro REGEXP :intro_re AND id = :id ', + [ 'intro_re' => self::EMBED_REGEXP, 'id' => $this->activity_id ]); + + // Was: $result = $DB->get_record_sql( 'SELECT * FROM {assign} WHERE ' . $DB->sql_like( 'intro', ':intro' ) . ' AND id = :id ', + // [ 'intro' => self::EMBED_LIKE, 'id' => $this->activity_id ]); self::debug([ __METHOD__, $result ]); return $result; diff --git a/survey-end/index.php b/survey-end/index.php index 8402e7a..52b08db 100644 --- a/survey-end/index.php +++ b/survey-end/index.php @@ -124,7 +124,7 @@ public static function complete_conditional() {