]> git.mjollnir.org Git - moodle.git/commitdiff
accesslib: MDL-17626 delete the context whenever a block is deleted. This includes...
authortjhunt <tjhunt>
Fri, 9 Jan 2009 06:16:36 +0000 (06:16 +0000)
committertjhunt <tjhunt>
Fri, 9 Jan 2009 06:16:36 +0000 (06:16 +0000)
backup/restorelib.php
lib/blocklib.php
mod/chat/lib.php
mod/lesson/lib.php
mod/quiz/lib.php

index 7418b4e650cd1b971fce3886f060d7dbaf9b716f..365745bba0597062a2e61bc94b7d929bb4bab200 100644 (file)
@@ -792,7 +792,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
         global $CFG, $DB;
         $status = true;
 
-        $DB->delete_records('block_instance', array('pageid'=>$restore->course_id, 'pagetype'=>PAGE_COURSE_VIEW));
+        blocks_delete_all_on_page(PAGE_COURSE_VIEW, $restore->course_id);
         if (empty($backup_block_format)) {     // This is a backup from Moodle < 1.5
             if (empty($blockinfo)) {
                 // Looks like it's from Moodle < 1.3. Let's give the course default blocks...
index eb9aa3cc209d93136a01cc7a8a63370f97b989be..0c090d6fd6605f709298712fb423a76b438aed25 100644 (file)
@@ -210,6 +210,7 @@ function blocks_delete_instance($instance,$pinned=false) {
     } else {
         // Now kill the db record;
         $DB->delete_records('block_instance', array('id'=>$instance->id));
+        delete_context(CONTEXT_BLOCK, $instance->id);
         // And now, decrement the weight of all blocks after this one
         $sql = "UPDATE {block_instance}
                    SET weight = weight - 1
@@ -502,7 +503,7 @@ function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid,
             // Define the data we're going to silently include in the instance config form here,
             // so we can strip them from the submitted data BEFORE serializing it.
             $hiddendata = array(
-                'sesskey' => $USER->sesskey,
+                'sesskey' => sesskey(),
                 'instanceid' => $instance->id,
                 'blockaction' => 'config'
             );
@@ -981,12 +982,31 @@ function blocks_print_adminblock(&$page, &$pageblocks) {
         }
         asort($menu);
 
-        $target = $page->url_get_full(array('sesskey' => $USER->sesskey, 'blockaction' => 'add'));
+        $target = $page->url_get_full(array('sesskey' => sesskey(), 'blockaction' => 'add'));
         $content = popup_form($target.'&amp;blockid=', $menu, 'add_block', '', $stradd .'...', '', '', true);
         print_side_block($strblocks, $content, NULL, NULL, NULL, array('class' => 'block_adminblock'));
     }
 }
 
+/**
+ * Delete all the blocks from a particular page.
+ *
+ * @param string $pagetype the page type.
+ * @param integer $pageid the page id.
+ * @return success of failure.
+ */
+function blocks_delete_all_on_page($pagetype, $pageid) {
+    global $DB;
+    if ($instances = $DB->get_records('block_instance', array('pageid' => $pageid, 'pagetype' => $pagetype))) {
+        foreach ($instances as $instance) {
+            delete_context(CONTEXT_BLOCK, $instance->id); // Ingore any failures here.
+        }
+    }
+    return $DB->delete_records('block_instance', array('pageid' => $pageid, 'pagetype' => $pagetype));
+}
+
+// Dispite what this function is called, it seems to be mostly used to populate
+// the default blocks when a new course (or whatever) is created.
 function blocks_repopulate_page($page) {
     global $CFG, $DB;
 
@@ -1018,7 +1038,7 @@ function blocks_repopulate_page($page) {
     // indexed and the indexes match, so we can work straight away... but CAREFULLY!
 
     // Ready to start creating block instances, but first drop any existing ones
-    $DB->delete_records('block_instance', array('pageid'=>$page->get_id(), 'pagetype'=>$page->get_type()));
+    blocks_delete_all_on_page($page->get_type(), $page->get_id());
 
     // Here we slyly count $posblocks and NOT $positions. This can actually make a difference
     // if the textual representation has undefined slots in the end. So we only work with as many
index 1b3feb4060df0059268317d80826ec0d593edbc1..212e4bc263bf41aa566bc9a2a1d694bc10d20dcb 100644 (file)
@@ -153,7 +153,7 @@ function chat_delete_instance($id) {
 
     $pagetypes = page_import_types('mod/chat/');
     foreach($pagetypes as $pagetype) {
-        if (!$DB->delete_records('block_instance', array('pageid'=>$chat->id, 'pagetype'=>$pagetype))) {
+        if(!blocks_delete_all_on_page($pagetype, $chat->id)) {
             $result = false;
         }
     }
index 1d7ab950ed9b01cd2b3574ce07d780e632cb6de0..693d59cc6b193d534546ffa49826701544519e51 100644 (file)
@@ -119,7 +119,7 @@ function lesson_delete_instance($id) {
     }
     $pagetypes = page_import_types('mod/lesson/');
     foreach ($pagetypes as $pagetype) {
-        if (!$DB->delete_records('block_instance', array('pageid'=>$lesson->id, 'pagetype'=>$pagetype))) {
+        if (!blocks_delete_all_on_page($pagetype, $lesson->id)) {
             $result = false;
         }
     }
index 177837dca1d1bdebde5388833c8024901dc662b0..89dc61f9ffbefb61fcc9c5f76677c6e0c0826d5f 100644 (file)
@@ -179,7 +179,7 @@ function quiz_delete_instance($id) {
 
     $pagetypes = page_import_types('mod/quiz/');
     foreach($pagetypes as $pagetype) {
-        if (!$DB->delete_records('block_instance', array('pageid'=>$quiz->id, 'pagetype'=>$pagetype))) {
+        if(!blocks_delete_all_on_page($pagetype, $quiz->id)) {
             $result = false;
         }
     }