From 130fe2b8c72a4b959afa4359c3b2b1bfdb6c2b45 Mon Sep 17 00:00:00 2001
From: Phil Lello <philipl@catalyst-eu.net>
Date: Wed, 19 Feb 2014 12:38:31 +0000
Subject: [PATCH] Added utility function require_plugin which throws an
 exception if the passed plugin and optional version is
 missing.

Change-Id: I8939ad3535a017b2c26f206ea84aff9855ed47b3
---
 lib/pluginlib.php |   27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/lib/pluginlib.php b/lib/pluginlib.php
index 31ec2a4..6153faf 100644
--- a/lib/pluginlib.php
+++ b/lib/pluginlib.php
@@ -4024,3 +4024,30 @@ class plugininfo_format extends plugininfo_base {
                 array('sesskey' => sesskey(), 'action' => 'uninstall', 'format' => $this->name));
     }
 }
+
+/**
+ * A convenience function that tests if a plugin is installed, and optionally checks the version
+ *
+ * @copyright 2014 Catalyst IT Europe Ltd
+ * @author Phil Lello <philipl@catalyst-eu.net>
+ *
+ * @param string $component The plugin to check for
+ * @param int $version An optional version number to check for
+ * @return void terminates with an error if the plugin is missing, or too old
+ */
+function require_plugin($component, $version = NULL) {
+
+    $pluginfo = plugin_manager::instance()->get_plugin_info($component);
+
+    // Check the plugin is available
+    if (is_null($pluginfo)) {
+        throw new moodle_exception('err_unknown_plugin', 'core_plugin', '', array('plugin' => $component));
+    }
+
+    // Check the version
+    if ($version && ($pluginfo->versiondisk < $version || $pluginfo->versiondb < $version)) {
+        throw new moodle_exception('err_version_plugin', 'core_plugin', '',
+            array('plugin' => $component, 'versiondisk' => $pluginfo->versiondisk, 'versiondb' => $pluginfo->versiondb, 'required' => $version));
+    }
+}
+
-- 
1.7.9.5

