MDL-44449 quiz access rules should have a delete hook.
authorTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 9 May 2014 13:20:17 +0000 (14:20 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 19 May 2014 15:59:04 +0000 (16:59 +0100)
So they can clean up their settings when the quiz is deleted.

See
https://github.com/moodleou/moodle-quizaccess_honestycheck/commit/babf149c2cc6dd734c272cd332b2032cc19db7d8
for an example of using this.

mod/quiz/accessmanager.php
mod/quiz/accessrule/accessrulebase.php
mod/quiz/accessrule/upgrade.txt
mod/quiz/lib.php

index 3d431a1..7a833a9 100644 (file)
@@ -30,8 +30,9 @@ defined('MOODLE_INTERNAL') || die();
  * This class keeps track of the various access rules that apply to a particular
  * quiz, with convinient methods for seeing whether access is allowed.
  *
- * @copyright  2009 Tim Hunt
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @copyright 2009 Tim Hunt
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since     Moodle 2.2
  */
 class quiz_access_manager {
     /** @var quiz the quiz settings object. */
@@ -143,11 +144,11 @@ class quiz_access_manager {
     /**
      * Save any submitted settings when the quiz settings form is submitted.
      *
-     * Note that the standard plugins do not use this mechanism, becuase all their
+     * Note that the standard plugins do not use this mechanism because their
      * settings are stored in the quiz table.
      *
      * @param object $quiz the data from the quiz form, including $quiz->id
-     *      which is the is of the quiz being saved.
+     *      which is the id of the quiz being saved.
      */
     public static function save_settings($quiz) {
 
@@ -156,6 +157,23 @@ class quiz_access_manager {
         }
     }
 
+    /**
+     * Delete any rule-specific settings when the quiz is deleted.
+     *
+     * Note that the standard plugins do not use this mechanism because their
+     * settings are stored in the quiz table.
+     *
+     * @param object $quiz the data from the database, including $quiz->id
+     *      which is the id of the quiz being deleted.
+     * @since Moodle 2.7.1, 2.6.4, 2.5.7
+     */
+    public static function delete_settings($quiz) {
+
+        foreach (self::get_rule_classes() as $rule) {
+            $rule::delete_settings($quiz);
+        }
+    }
+
     /**
      * Build the SQL for loading all the access settings in one go.
      * @param int $quizid the quiz id.
index ec3825c..649e410 100644 (file)
@@ -37,8 +37,9 @@ require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  * as true) if access should be blocked. Slighly unnatural, but acutally the easist
  * way to implement this.
  *
- * @copyright  2009 Tim Hunt
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @copyright 2009 Tim Hunt
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since     Moodle 2.2
  */
 abstract class quiz_access_rule_base {
     /** @var stdClass the quiz settings. */
@@ -281,12 +282,23 @@ abstract class quiz_access_rule_base {
      * Save any submitted settings when the quiz settings form is submitted. This
      * is called from {@link quiz_after_add_or_update()} in lib.php.
      * @param object $quiz the data from the quiz form, including $quiz->id
-     *      which is the is of the quiz being saved.
+     *      which is the id of the quiz being saved.
      */
     public static function save_settings($quiz) {
         // By default do nothing.
     }
 
+    /**
+     * Delete any rule-specific settings when the quiz is deleted. This is called
+     * from {@link quiz_delete_instance()} in lib.php.
+     * @param object $quiz the data from the database, including $quiz->id
+     *      which is the id of the quiz being deleted.
+     * @since Moodle 2.7.1, 2.6.4, 2.5.7
+     */
+    public static function delete_settings($quiz) {
+        // By default do nothing.
+    }
+
     /**
      * Return the bits of SQL needed to load all the settings from all the access
      * plugins in one DB query. The easiest way to understand what you need to do
index 1cae31e..f883b6e 100644 (file)
@@ -2,16 +2,24 @@ This files describes API changes for quiz access rule plugins.
 
 Overview of this plugin type at http://docs.moodle.org/dev/Quiz_access_rules
 
+=== 2.8, 2.7.1, 2.6.4 and 2.5.7 ===
+
+* New static method delete_settings for access rules, which is called when a
+  quiz is deleted.
+
+
 === 2.4 and 2.3.4 ===
 
 * Replaced time_left() with new time_left_display() and end_time() functions.
 
+
 === 2.3 ===
 
 * This plugin type now supports cron in the standard way. If required, Create a
   lib.php file containing
 function quizaccess_mypluginname_cron() {};
 
+
 === 2.2 ===
 
 * This plugin type was new in Moodle 2.2!
index a573dd2..6ed0e3f 100644 (file)
@@ -169,6 +169,8 @@ function quiz_delete_instance($id) {
     $DB->delete_records('quiz_slots', array('quizid' => $quiz->id));
     $DB->delete_records('quiz_feedback', array('quizid' => $quiz->id));
 
+    quiz_access_manager::delete_settings($quiz);
+
     $events = $DB->get_records('event', array('modulename' => 'quiz', 'instance' => $quiz->id));
     foreach ($events as $event) {
         $event = calendar_event::load($event);