From d16cf374eb236034ee65839eb125a79a31d54f7d Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 6 Jun 2017 17:04:20 +0200 Subject: [PATCH] MDL-59162 tool_models: Remove dependant models on uninstall Part of MDL-57791 epic. --- admin/tool/models/db/uninstall.php | 44 ++++++++++++++++++++++++++++++ analytics/classes/model.php | 11 ++++++++ 2 files changed, 55 insertions(+) create mode 100644 admin/tool/models/db/uninstall.php diff --git a/admin/tool/models/db/uninstall.php b/admin/tool/models/db/uninstall.php new file mode 100644 index 00000000000..6034777c889 --- /dev/null +++ b/admin/tool/models/db/uninstall.php @@ -0,0 +1,44 @@ +. + +/** + * tool_models plugin uninstallation. + * + * @package tool_models + * @copyright 2017 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +function xmldb_tool_models_uninstall() { + global $DB; + + // Remove the models that are using this tool targets. + $targets = core_component::get_component_classes_in_namespace('tool_models', 'analytics\target'); + + $options = array(); + foreach ($targets as $classname => $unused) { + $target = \core_analytics\manager::get_target($classname); + $options[] = '\\' . get_class($target); + } + list($sql, $params) = $DB->get_in_or_equal($options); + $models = $DB->get_records_select('analytics_models', "target $sql", $params); + foreach ($models as $modelobj) { + $model = new \core_analytics\model($modelobj); + $model->delete(); + } +} diff --git a/analytics/classes/model.php b/analytics/classes/model.php index bf571a3180b..7fe00e9be2c 100644 --- a/analytics/classes/model.php +++ b/analytics/classes/model.php @@ -316,6 +316,17 @@ class model { $this->uniqueid = null; } + /** + * Removes the model. + * + * @return void + */ + public static function delete() { + global $DB; + $this->clear_model(); + $DB->delete_record('analytics_models', array('id' => $this->model->id)); + } + /** * Evaluates the model datasets. *