Exemplo n.º 1
0
/**
 * Handles upgrading instances of this block.
 *
 * @param int $oldversion
 * @param object $block
 */
function xmldb_block_completionstatus_upgrade($oldversion, $block)
{
    global $DB;
    // Moodle v2.4.0 release upgrade line
    // Put any upgrade step following this.
    if ($oldversion < 2012112901) {
        // Get the instances of this block.
        if ($blocks = $DB->get_records('block_instances', array('blockname' => 'completionstatus', 'pagetypepattern' => 'my-index'))) {
            // Loop through and remove them from the My Moodle page.
            foreach ($blocks as $block) {
                blocks_delete_instance($block);
            }
        }
        // Savepoint reached.
        upgrade_block_savepoint(true, 2012112901, 'completionstatus');
    }
    // Moodle v2.5.0 release upgrade line.
    // Put any upgrade step following this.
    // Moodle v2.6.0 release upgrade line.
    // Put any upgrade step following this.
    // Moodle v2.7.0 release upgrade line.
    // Put any upgrade step following this.
    // Moodle v2.8.0 release upgrade line.
    // Put any upgrade step following this.
    // Moodle v2.9.0 release upgrade line.
    // Put any upgrade step following this.
    // Moodle v3.0.0 release upgrade line.
    // Put any upgrade step following this.
    return true;
}
Exemplo n.º 2
0
 protected function purge_blocks()
 {
     global $DB;
     $this->resetAfterTest();
     $bis = $DB->get_records('block_instances');
     foreach ($bis as $instance) {
         blocks_delete_instance($instance);
     }
 }
Exemplo n.º 3
0
 /**
  * Delete method.
  *
  * @param array $courses All courses we should be effecting.
  */
 public function delete($courses)
 {
     global $CFG;
     require_once $CFG->libdir . "/blocklib.php";
     // We now want to delete all blocks with that name.
     $instances = $this->get_instances($courses);
     foreach ($instances as $instance) {
         blocks_delete_instance($instance);
     }
 }
Exemplo n.º 4
0
function xmldb_local_iomad_dashboard_uninstall()
{
    global $DB;
    // Remove blocks to the dashboard
    // yes, I know this isn't really what this is for!!
    $systemcontext = context_system::instance();
    $contextid = $systemcontext->id;
    $instances = $DB->get_recordset('block_instances', array('parentcontextid' => $contextid, 'pagetypepattern' => 'local-iomad-dashboard-index'));
    foreach ($instances as $instance) {
        blocks_delete_instance($instance, true);
    }
    return true;
}
Exemplo n.º 5
0
 /**
  * Handle deleting a block.
  * @return boolean true if anything was done. False if not.
  */
 public function process_url_delete()
 {
     $blockid = optional_param('bui_deleteid', null, PARAM_INTEGER);
     if (!$blockid) {
         return false;
     }
     require_sesskey();
     $block = $this->page->blocks->find_instance($blockid);
     if (!$block->user_can_edit() || !$this->page->user_can_edit_blocks() || !$block->user_can_addto($this->page)) {
         throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('deleteablock'));
     }
     blocks_delete_instance($block->instance);
     // If the page URL was a guess, it will contain the bui_... param, so we must make sure it is not there.
     $this->page->ensure_param_not_in_url('bui_deleteid');
     return true;
 }
Exemplo n.º 6
0
Arquivo: lib.php Projeto: dg711/moodle
function my_reset_page($userid, $private = MY_PAGE_PRIVATE, $pagetype = 'my-index')
{
    global $DB, $CFG;
    $page = my_get_page($userid, $private);
    if ($page->userid == $userid) {
        $context = context_user::instance($userid);
        if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id, 'pagetypepattern' => $pagetype))) {
            foreach ($blocks as $block) {
                if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
                    blocks_delete_instance($block);
                }
            }
        }
        $DB->delete_records('my_pages', array('id' => $page->id));
    }
    // Get the system default page
    if (!($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private)))) {
        return false;
        // error
    }
    // Trigger dashboard has been reset event.
    $eventparams = array('context' => context_user::instance($userid), 'other' => array('private' => $private, 'pagetype' => $pagetype));
    $event = \core\event\dashboard_reset::create($eventparams);
    $event->trigger();
    return $systempage;
}
Exemplo n.º 7
0
function my_reset_page($userid, $private = MY_PAGE_PRIVATE, $pagetype = 'my-index')
{
    global $DB, $CFG;
    $page = my_get_page($userid, $private);
    if ($page->userid == $userid) {
        $context = context_user::instance($userid);
        if ($blocks = $DB->get_records('block_instances', array('parentcontextid' => $context->id, 'pagetypepattern' => $pagetype))) {
            foreach ($blocks as $block) {
                if (is_null($block->subpagepattern) || $block->subpagepattern == $page->id) {
                    blocks_delete_instance($block);
                }
            }
        }
        $DB->delete_records('my_pages', array('id' => $page->id));
    }
    // Get the system default page
    if (!($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => $private)))) {
        return false;
        // error
    }
    return $systempage;
}
Exemplo n.º 8
0
 /**
  * Delete all blocks in this region.
  */
 public function empty_content($courses)
 {
     // Our target is the name of a block region.
     $region = $this->get_identifier();
     // For each course, delete all blocks.
     foreach ($courses as $course) {
         $blockmanager = $this->get_block_manager($course);
         $blockmanager->add_region($region);
         $blockmanager->load_blocks();
         $blocks = $blockmanager->get_blocks_for_region($region);
         foreach ($blocks as $block) {
             blocks_delete_instance($block->instance);
         }
     }
 }
Exemplo n.º 9
0
 /**
  * Deletes the rule block.
  */
 public function delete()
 {
     blocks_delete_instance($this->_block->instance);
 }
Exemplo n.º 10
0
/**
 * Sets up a default instance of the curr admin blocks that
 * is viewable anywhere on the site, and cleans all other instances
 */
function block_elisadmin_create_instance()
{
    global $DB;
    // First delete instances
    $params = array('blockname' => 'elisadmin');
    $instances = $DB->get_recordset('block_instances', $params);
    foreach ($instances as $instance) {
        blocks_delete_instance($instance);
    }
    unset($instances);
    // Set up the new instance
    $block_instance_record = new stdclass();
    $block_instance_record->blockname = 'elisadmin';
    $block_instance_record->pagetypepattern = '*';
    $block_instance_record->parentcontextid = 1;
    $block_instance_record->showinsubcontexts = 1;
    // Force location
    $block_instance_record->defaultregion = 'side-pre';
    $block_instance_record->defaultweight = -999;
    $DB->insert_record('block_instances', $block_instance_record);
}
Exemplo n.º 11
0
 /**
  * Pre-uninstall hook.
  *
  * This is intended for disabling of plugin, some DB table purging, etc.
  *
  * NOTE: to be called from uninstall_plugin() only.
  * @private
  */
 public function uninstall_cleanup()
 {
     global $DB, $CFG;
     if ($block = $DB->get_record('block', array('name' => $this->name))) {
         // Inform block it's about to be deleted.
         if (file_exists("{$CFG->dirroot}/blocks/{$block->name}/block_{$block->name}.php")) {
             $blockobject = block_instance($block->name);
             if ($blockobject) {
                 $blockobject->before_delete();
                 // Only if we can create instance, block might have been already removed.
             }
         }
         // First delete instances and related contexts.
         $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
         foreach ($instances as $instance) {
             blocks_delete_instance($instance);
         }
         // Delete block.
         $DB->delete_records('block', array('id' => $block->id));
     }
     parent::uninstall_cleanup();
 }
Exemplo n.º 12
0
 /**
  * Handle deleting a block.
  * @return boolean true if anything was done. False if not.
  */
 public function process_url_delete()
 {
     global $CFG, $PAGE, $OUTPUT;
     $blockid = optional_param('bui_deleteid', null, PARAM_INT);
     $confirmdelete = optional_param('bui_confirm', null, PARAM_INT);
     if (!$blockid) {
         return false;
     }
     require_sesskey();
     $block = $this->page->blocks->find_instance($blockid);
     if (!$this->user_can_delete_block($block)) {
         throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('deleteablock'));
     }
     if (!$confirmdelete) {
         $deletepage = new moodle_page();
         $deletepage->set_pagelayout('admin');
         $deletepage->blocks->show_only_fake_blocks(true);
         $deletepage->set_course($this->page->course);
         $deletepage->set_context($this->page->context);
         if ($this->page->cm) {
             $deletepage->set_cm($this->page->cm);
         }
         $deleteurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring());
         $deleteurlparams = $this->page->url->params();
         $deletepage->set_url($deleteurlbase, $deleteurlparams);
         $deletepage->set_block_actions_done();
         // At this point we are either going to redirect, or display the form, so
         // overwrite global $PAGE ready for this. (Formslib refers to it.)
         $PAGE = $deletepage;
         //some functions like MoodleQuickForm::addHelpButton use $OUTPUT so we need to replace that too
         $output = $deletepage->get_renderer('core');
         $OUTPUT = $output;
         $site = get_site();
         $blocktitle = $block->get_title();
         $strdeletecheck = get_string('deletecheck', 'block', $blocktitle);
         $message = get_string('deleteblockcheck', 'block', $blocktitle);
         // If the block is being shown in sub contexts display a warning.
         if ($block->instance->showinsubcontexts == 1) {
             $parentcontext = context::instance_by_id($block->instance->parentcontextid);
             $systemcontext = context_system::instance();
             $messagestring = new stdClass();
             $messagestring->location = $parentcontext->get_context_name();
             // Checking for blocks that may have visibility on the front page and pages added on that.
             if ($parentcontext->id != $systemcontext->id && is_inside_frontpage($parentcontext)) {
                 $messagestring->pagetype = get_string('showonfrontpageandsubs', 'block');
             } else {
                 $pagetypes = generate_page_type_patterns($this->page->pagetype, $parentcontext);
                 $messagestring->pagetype = $block->instance->pagetypepattern;
                 if (isset($pagetypes[$block->instance->pagetypepattern])) {
                     $messagestring->pagetype = $pagetypes[$block->instance->pagetypepattern];
                 }
             }
             $message = get_string('deleteblockwarning', 'block', $messagestring);
         }
         $PAGE->navbar->add($strdeletecheck);
         $PAGE->set_title($blocktitle . ': ' . $strdeletecheck);
         $PAGE->set_heading($site->fullname);
         echo $OUTPUT->header();
         $confirmurl = new moodle_url($deletepage->url, array('sesskey' => sesskey(), 'bui_deleteid' => $block->instance->id, 'bui_confirm' => 1));
         $cancelurl = new moodle_url($deletepage->url);
         $yesbutton = new single_button($confirmurl, get_string('yes'));
         $nobutton = new single_button($cancelurl, get_string('no'));
         echo $OUTPUT->confirm($message, $yesbutton, $nobutton);
         echo $OUTPUT->footer();
         // Make sure that nothing else happens after we have displayed this form.
         exit;
     } else {
         blocks_delete_instance($block->instance);
         // bui_deleteid and bui_confirm should not be in the PAGE url.
         $this->page->ensure_param_not_in_url('bui_deleteid');
         $this->page->ensure_param_not_in_url('bui_confirm');
         return true;
     }
 }
Exemplo n.º 13
0
     notice_yesno(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete=' . $block->id . '&amp;confirm=1&amp;sesskey=' . sesskey(), 'blocks.php');
     admin_externalpage_print_footer();
     exit;
 } else {
     // Inform block it's about to be deleted
     $blockobject = block_instance($block->name);
     if ($blockobject) {
         $blockobject->before_delete();
         //only if we can create instance, block might have been already removed
     }
     // First delete instances and then block
     $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
     if (!empty($instances)) {
         foreach ($instances as $instance) {
             blocks_delete_instance($instance);
             blocks_delete_instance($instance, true);
         }
     }
     // Delete block
     if (!$DB->delete_records('block', array('id' => $block->id))) {
         notify("Error occurred while deleting the {$strblockname} record from blocks table");
     }
     drop_plugin_tables($block->name, "{$CFG->dirroot}/blocks/{$block->name}/db/install.xml", false);
     // old obsoleted table names
     drop_plugin_tables('block_' . $block->name, "{$CFG->dirroot}/blocks/{$block->name}/db/install.xml", false);
     // Delete the capabilities that were defined by this block
     capabilities_cleanup('block/' . $block->name);
     // remove entent handlers and dequeue pending events
     events_uninstall('block/' . $block->name);
     $a->block = $strblockname;
     $a->directory = $CFG->dirroot . '/blocks/' . $block->name;
Exemplo n.º 14
0
function blocks_execute_action($page, &$blockmanager, $blockaction, $instanceorid, $pinned = false, $redirect = true)
{
    global $CFG, $USER, $DB;
    if (!in_array($blockaction, array('config', 'add', 'delete'))) {
        throw new moodle_exception('Sorry, blocks editing is currently broken. Will be fixed. See MDL-19010.');
    }
    if (is_int($instanceorid)) {
        $blockid = $instanceorid;
    } else {
        if (is_object($instanceorid)) {
            $instance = $instanceorid;
        }
    }
    switch ($blockaction) {
        case 'config':
            // First of all check to see if the block wants to be edited
            if (!$instance->user_can_edit()) {
                break;
            }
            // Now get the title and AFTER that load up the instance
            $blocktitle = $instance->get_title();
            // 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' => sesskey(), 'instanceid' => $instance->instance->id, 'blockaction' => 'config');
            // To this data, add anything the page itself needs to display
            $hiddendata = $page->url->params($hiddendata);
            if ($data = data_submitted()) {
                $remove = array_keys($hiddendata);
                foreach ($remove as $item) {
                    unset($data->{$item});
                }
                $instance->instance_config_save($data);
                redirect($page->url->out());
            } else {
                // We need to show the config screen, so we highjack the display logic and then die
                $strheading = get_string('blockconfiga', 'moodle', $blocktitle);
                $nav = build_navigation($strheading, $page->cm);
                print_header($strheading, $strheading, $nav);
                echo '<div class="block-config" id="' . $instance->name() . '">';
                /// Make CSS easier
                print_heading($strheading);
                echo '<form method="post" name="block-config" action="' . $page->url->out(false) . '">';
                echo '<p>';
                echo $page->url->hidden_params_out(array(), 0, $hiddendata);
                echo '</p>';
                $instance->instance_config_print();
                echo '</form>';
                echo '</div>';
                global $PAGE;
                $PAGE->set_docs_path('blocks/' . $instance->name());
                print_footer();
                die;
                // Do not go on with the other page-related stuff
            }
            break;
        case 'toggle':
            if (empty($instance)) {
                print_error('invalidblockinstance', '', '', $blockaction);
            }
            $instance->visible = $instance->visible ? 0 : 1;
            if (!empty($pinned)) {
                $DB->update_record('block_pinned_old', $instance);
            } else {
                $DB->update_record('block_instance_old', $instance);
            }
            break;
        case 'delete':
            if (empty($instance)) {
                print_error('invalidblockinstance', '', '', $blockaction);
            }
            blocks_delete_instance($instance->instance, $pinned);
            break;
        case 'moveup':
            if (empty($instance)) {
                print_error('invalidblockinstance', '', '', $blockaction);
            }
            if ($instance->weight == 0) {
                // The block is the first one, so a move "up" probably means it changes position
                // Where is the instance going to be moved?
                $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_UP);
                $newweight = empty($blockmanager[$newpos]) ? 0 : max(array_keys($blockmanager[$newpos])) + 1;
                blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
            } else {
                // The block is just moving upwards in the same position.
                // This configuration will make sure that even if somehow the weights
                // become not continuous, block move operations will eventually bring
                // the situation back to normal without printing any warnings.
                if (!empty($blockmanager[$instance->position][$instance->weight - 1])) {
                    $other = $blockmanager[$instance->position][$instance->weight - 1];
                }
                if (!empty($other)) {
                    ++$other->weight;
                    if (!empty($pinned)) {
                        $DB->update_record('block_pinned_old', $other);
                    } else {
                        $DB->update_record('block_instance_old', $other);
                    }
                }
                --$instance->weight;
                if (!empty($pinned)) {
                    $DB->update_record('block_pinned_old', $instance);
                } else {
                    $DB->update_record('block_instance_old', $instance);
                }
            }
            break;
        case 'movedown':
            if (empty($instance)) {
                print_error('invalidblockinstance', '', '', $blockaction);
            }
            if ($instance->weight == max(array_keys($blockmanager[$instance->position]))) {
                // The block is the last one, so a move "down" probably means it changes position
                // Where is the instance going to be moved?
                $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_DOWN);
                $newweight = empty($blockmanager[$newpos]) ? 0 : max(array_keys($blockmanager[$newpos])) + 1;
                blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
            } else {
                // The block is just moving downwards in the same position.
                // This configuration will make sure that even if somehow the weights
                // become not continuous, block move operations will eventually bring
                // the situation back to normal without printing any warnings.
                if (!empty($blockmanager[$instance->position][$instance->weight + 1])) {
                    $other = $blockmanager[$instance->position][$instance->weight + 1];
                }
                if (!empty($other)) {
                    --$other->weight;
                    if (!empty($pinned)) {
                        $DB->update_record('block_pinned_old', $other);
                    } else {
                        $DB->update_record('block_instance_old', $other);
                    }
                }
                ++$instance->weight;
                if (!empty($pinned)) {
                    $DB->update_record('block_pinned_old', $instance);
                } else {
                    $DB->update_record('block_instance_old', $instance);
                }
            }
            break;
        case 'moveleft':
            if (empty($instance)) {
                print_error('invalidblockinstance', '', '', $blockaction);
            }
            // Where is the instance going to be moved?
            $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_LEFT);
            $newweight = empty($blockmanager[$newpos]) ? 0 : max(array_keys($blockmanager[$newpos])) + 1;
            blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
            break;
        case 'moveright':
            if (empty($instance)) {
                print_error('invalidblockinstance', '', '', $blockaction);
            }
            // Where is the instance going to be moved?
            $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_RIGHT);
            $newweight = empty($blockmanager[$newpos]) ? 0 : max(array_keys($blockmanager[$newpos])) + 1;
            blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
            break;
        case 'add':
            // Add a new instance of this block, if allowed
            $block = blocks_get_record($blockid);
            if (empty($block) || !$block->visible) {
                // Only allow adding if the block exists and is enabled
                break;
            }
            if (!$block->multiple && blocks_find_block($blockid, $blockmanager) !== false) {
                // If no multiples are allowed and we already have one, return now
                break;
            }
            if (!block_method_result($block->name, 'user_can_addto', $page)) {
                // If the block doesn't want to be added...
                break;
            }
            $region = $page->blocks->get_default_region();
            $weight = $DB->get_field_sql("SELECT MAX(defaultweight) FROM {block_instances} \n                    WHERE contextid = ? AND defaultregion = ?", array($page->context->id, $region));
            $pagetypepattern = $page->pagetype;
            if (strpos($pagetypepattern, 'course-view') === 0) {
                $pagetypepattern = 'course-view-*';
            }
            $page->blocks->add_block($block->name, $region, $weight, false, $pagetypepattern);
            break;
    }
    if ($redirect) {
        // In order to prevent accidental duplicate actions, redirect to a page with a clean url
        redirect($page->url->out());
    }
}
Exemplo n.º 15
0
function blocks_execute_action($page, &$pageblocks, $blockaction, $instanceorid, $pinned = false, $redirect = true)
{
    global $CFG;
    if (is_int($instanceorid)) {
        $blockid = $instanceorid;
    } else {
        if (is_object($instanceorid)) {
            $instance = $instanceorid;
        }
    }
    switch ($blockaction) {
        case 'config':
            global $USER;
            $block = blocks_get_record($instance->blockid);
            // Hacky hacky tricky stuff to get the original human readable block title,
            // even if the block has configured its title to be something else.
            // Create the object WITHOUT instance data.
            $blockobject = block_instance($block->name);
            if ($blockobject === false) {
                break;
            }
            // First of all check to see if the block wants to be edited
            if (!$blockobject->user_can_edit()) {
                break;
            }
            // Now get the title and AFTER that load up the instance
            $blocktitle = $blockobject->get_title();
            $blockobject->_load_instance($instance);
            optional_param('submitted', 0, PARAM_INT);
            // 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, 'instanceid' => $instance->id, 'blockaction' => 'config');
            // To this data, add anything the page itself needs to display
            $hiddendata = array_merge($hiddendata, $page->url_get_parameters());
            if ($data = data_submitted()) {
                $remove = array_keys($hiddendata);
                foreach ($remove as $item) {
                    unset($data->{$item});
                }
                if (!$blockobject->instance_config_save($data, $pinned)) {
                    error('Error saving block configuration');
                }
                // And nothing more, continue with displaying the page
            } else {
                // We need to show the config screen, so we highjack the display logic and then die
                $strheading = get_string('blockconfiga', 'moodle', $blocktitle);
                $page->print_header(get_string('pageheaderconfigablock', 'moodle'), array($strheading => ''));
                echo '<div class="block-config" id="' . $block->name . '">';
                /// Make CSS easier
                print_heading($strheading);
                echo '<form method="post" name="block-config" action="' . $page->url_get_path() . '">';
                echo '<p>';
                foreach ($hiddendata as $name => $val) {
                    echo '<input type="hidden" name="' . $name . '" value="' . $val . '" />';
                }
                echo '</p>';
                $blockobject->instance_config_print();
                echo '</form>';
                echo '</div>';
                $CFG->pagepath = 'blocks/' . $block->name;
                print_footer();
                die;
                // Do not go on with the other page-related stuff
            }
            break;
        case 'toggle':
            if (empty($instance)) {
                error('Invalid block instance for ' . $blockaction);
            }
            $instance->visible = $instance->visible ? 0 : 1;
            if (!empty($pinned)) {
                update_record('block_pinned', $instance);
            } else {
                update_record('block_instance', $instance);
            }
            break;
        case 'delete':
            if (empty($instance)) {
                error('Invalid block instance for ' . $blockaction);
            }
            blocks_delete_instance($instance, $pinned);
            break;
        case 'moveup':
            if (empty($instance)) {
                error('Invalid block instance for ' . $blockaction);
            }
            if ($instance->weight == 0) {
                // The block is the first one, so a move "up" probably means it changes position
                // Where is the instance going to be moved?
                $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_UP);
                $newweight = empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1;
                blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
            } else {
                // The block is just moving upwards in the same position.
                // This configuration will make sure that even if somehow the weights
                // become not continuous, block move operations will eventually bring
                // the situation back to normal without printing any warnings.
                if (!empty($pageblocks[$instance->position][$instance->weight - 1])) {
                    //define instance's position in the array
                    foreach ($pageblocks[$instance->position] as $instancekeysindex => $index) {
                        if ($pageblocks[$instance->position][$instancekeysindex]->id == $instance->id) {
                            $instanceindex = $instancekeysindex;
                        }
                    }
                    $other = $pageblocks[$instance->position][$instanceindex - 1];
                }
                if (!empty($other)) {
                    ++$other->weight;
                    if (!empty($pinned)) {
                        update_record('block_pinned', $other);
                    } else {
                        update_record('block_instance', $other);
                    }
                }
                --$instance->weight;
                if (!empty($pinned)) {
                    update_record('block_pinned', $instance);
                } else {
                    update_record('block_instance', $instance);
                }
            }
            break;
        case 'movedown':
            if (empty($instance)) {
                error('Invalid block instance for ' . $blockaction);
            }
            if ($instance->weight == max(array_keys($pageblocks[$instance->position]))) {
                // The block is the last one, so a move "down" probably means it changes position
                // Where is the instance going to be moved?
                $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_DOWN);
                $newweight = empty($pageblocks[$newpos]) ? 0 : max(array_keys($pageblocks[$newpos])) + 1;
                blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
            } else {
                // The block is just moving downwards in the same position.
                // This configuration will make sure that even if somehow the weights
                // become not continuous, block move operations will eventually bring
                // the situation back to normal without printing any warnings.
                if (!empty($pageblocks[$instance->position][$instance->weight + 1])) {
                    //define instance's position in the array
                    foreach ($pageblocks[$instance->position] as $instancekeysindex => $index) {
                        if ($pageblocks[$instance->position][$instancekeysindex]->id == $instance->id) {
                            $instanceindex = $instancekeysindex;
                        }
                    }
                    $other = $pageblocks[$instance->position][$instanceindex + 1];
                }
                if (!empty($other)) {
                    --$other->weight;
                    if (!empty($pinned)) {
                        update_record('block_pinned', $other);
                    } else {
                        update_record('block_instance', $other);
                    }
                }
                ++$instance->weight;
                if (!empty($pinned)) {
                    update_record('block_pinned', $instance);
                } else {
                    update_record('block_instance', $instance);
                }
            }
            break;
        case 'moveleft':
            if (empty($instance)) {
                error('Invalid block instance for ' . $blockaction);
            }
            // Where is the instance going to be moved?
            $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_LEFT);
            $newweight = 0;
            if (!empty($pinned) && !empty($pageblocks[$newpos])) {
                $newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))]->weight + 1;
            } else {
                if (!empty($pageblocks[$newpos]) && !array_key_exists('pinned', $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))])) {
                    $newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))]->weight + 1;
                }
            }
            blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
            break;
        case 'moveright':
            if (empty($instance)) {
                error('Invalid block instance for ' . $blockaction);
            }
            // Where is the instance going to be moved?
            $newpos = $page->blocks_move_position($instance, BLOCK_MOVE_RIGHT);
            $newweight = 0;
            if (!empty($pinned) && !empty($pageblocks[$newpos])) {
                $newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))]->weight + 1;
            } else {
                if (!empty($pageblocks[$newpos]) && !array_key_exists('pinned', $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))])) {
                    $newweight = $pageblocks[$newpos][max(array_keys($pageblocks[$newpos]))]->weight + 1;
                }
            }
            blocks_execute_repositioning($instance, $newpos, $newweight, $pinned);
            break;
        case 'add':
            // Add a new instance of this block, if allowed
            $block = blocks_get_record($blockid);
            if (empty($block) || !$block->visible) {
                // Only allow adding if the block exists and is enabled
                break;
            }
            if (!$block->multiple && blocks_find_block($blockid, $pageblocks) !== false) {
                // If no multiples are allowed and we already have one, return now
                break;
            }
            if (!block_method_result($block->name, 'user_can_addto', $page)) {
                // If the block doesn't want to be added...
                break;
            }
            $newpos = $page->blocks_default_position();
            if (!empty($pinned)) {
                $sql = 'SELECT 1, max(weight) + 1 AS nextfree FROM ' . $CFG->prefix . 'block_pinned WHERE ' . ' pagetype = \'' . $page->get_type() . '\' AND position = \'' . $newpos . '\'';
            } else {
                $sql = 'SELECT 1, max(weight) + 1 AS nextfree FROM ' . $CFG->prefix . 'block_instance WHERE pageid = ' . $page->get_id() . ' AND pagetype = \'' . $page->get_type() . '\' AND position = \'' . $newpos . '\'';
            }
            $weight = get_record_sql($sql);
            $newinstance = new stdClass();
            $newinstance->blockid = $blockid;
            if (empty($pinned)) {
                $newinstance->pageid = $page->get_id();
            }
            $newinstance->pagetype = $page->get_type();
            $newinstance->position = $newpos;
            $newinstance->weight = empty($weight->nextfree) ? 0 : $weight->nextfree;
            $newinstance->visible = 1;
            $newinstance->configdata = '';
            if (!empty($pinned)) {
                $newinstance->id = insert_record('block_pinned', $newinstance);
            } else {
                $newinstance->id = insert_record('block_instance', $newinstance);
            }
            // If the new instance was created, allow it to do additional setup
            if ($newinstance && ($obj = block_instance($block->name, $newinstance))) {
                // Return value ignored
                $obj->instance_create();
            }
            break;
    }
    if ($redirect) {
        // In order to prevent accidental duplicate actions, redirect to a page with a clean url
        redirect($page->url_get_full());
    }
}
Exemplo n.º 16
0
 /**
  * Handle deleting a block.
  * @return boolean true if anything was done. False if not.
  */
 public function process_url_delete()
 {
     global $CFG, $PAGE, $OUTPUT;
     $blockid = optional_param('bui_deleteid', null, PARAM_INT);
     $confirmdelete = optional_param('bui_confirm', null, PARAM_INT);
     if (!$blockid) {
         return false;
     }
     require_sesskey();
     $block = $this->page->blocks->find_instance($blockid);
     if (!$this->user_can_delete_block($block)) {
         throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('deleteablock'));
     }
     if (!$confirmdelete) {
         $deletepage = new moodle_page();
         $deletepage->set_pagelayout('admin');
         $deletepage->set_course($this->page->course);
         $deletepage->set_context($this->page->context);
         if ($this->page->cm) {
             $deletepage->set_cm($this->page->cm);
         }
         $deleteurlbase = str_replace($CFG->wwwroot . '/', '/', $this->page->url->out_omit_querystring());
         $deleteurlparams = $this->page->url->params();
         $deletepage->set_url($deleteurlbase, $deleteurlparams);
         $deletepage->set_block_actions_done();
         // At this point we are either going to redirect, or display the form, so
         // overwrite global $PAGE ready for this. (Formslib refers to it.)
         $PAGE = $deletepage;
         //some functions like MoodleQuickForm::addHelpButton use $OUTPUT so we need to replace that too
         $output = $deletepage->get_renderer('core');
         $OUTPUT = $output;
         $site = get_site();
         $blocktitle = $block->get_title();
         $strdeletecheck = get_string('deletecheck', 'block', $blocktitle);
         $message = get_string('deleteblockcheck', 'block', $blocktitle);
         $PAGE->navbar->add($strdeletecheck);
         $PAGE->set_title($blocktitle . ': ' . $strdeletecheck);
         $PAGE->set_heading($site->fullname);
         echo $OUTPUT->header();
         $confirmurl = new moodle_url($deletepage->url, array('sesskey' => sesskey(), 'bui_deleteid' => $block->instance->id, 'bui_confirm' => 1));
         $cancelurl = new moodle_url($deletepage->url);
         $yesbutton = new single_button($confirmurl, get_string('yes'));
         $nobutton = new single_button($cancelurl, get_string('no'));
         echo $OUTPUT->confirm($message, $yesbutton, $nobutton);
         echo $OUTPUT->footer();
         // Make sure that nothing else happens after we have displayed this form.
         exit;
     } else {
         blocks_delete_instance($block->instance);
         // bui_deleteid and bui_confirm should not be in the PAGE url.
         $this->page->ensure_param_not_in_url('bui_deleteid');
         $this->page->ensure_param_not_in_url('bui_confirm');
         return true;
     }
 }
Exemplo n.º 17
0
/**
 * This function removes blocks/modules from a page in the case of
 * blocks it also removes the entry from block_instance 
 *
 * @param object $pageitem a fully populated page_item object
 * @uses $CFG
 * @uses $COURSE
 */
function page_block_delete($pageitem)
{
    global $CFG, $COURSE;
    require_once $CFG->libdir . '/blocklib.php';
    // we leave module cleanup to the manage modules tab... blocks need some help though.
    if (!empty($pageitem->blockinstance)) {
        if ($blockinstance = get_record('block_instance', 'id', $pageitem->blockinstance)) {
            // see if this is the last reference to the blockinstance
            $count = count_records('format_page_items', 'blockinstance', $pageitem->blockinstance);
            if ($count == 1) {
                if ($block = blocks_get_record($blockinstance->blockid)) {
                    if ($block->name != 'course_menu') {
                        // At this point, the format has done all of its own checking,
                        // hand it off to block API
                        blocks_delete_instance($blockinstance);
                    }
                }
            }
        }
    }
    delete_records('format_page_items', 'id', $pageitem->id);
    execute_sql("UPDATE {$CFG->prefix}format_page_items\n                    SET sortorder = sortorder - 1\n                  WHERE pageid = {$pageitem->pageid}\n                    AND position = '{$pageitem->position}'\n                    AND sortorder > {$pageitem->sortorder}", false);
}
Exemplo n.º 18
0
/**
 * Automatically clean-up all plugin data and remove the plugin DB tables
 *
 * @param string $type The plugin type, eg. 'mod', 'qtype', 'workshopgrading' etc.
 * @param string $name The plugin name, eg. 'forum', 'multichoice', 'accumulative' etc.
 * @uses global $OUTPUT to produce notices and other messages
 * @return void
 */
function uninstall_plugin($type, $name)
{
    global $CFG, $DB, $OUTPUT;
    // recursively uninstall all module subplugins first
    if ($type === 'mod') {
        if (file_exists("{$CFG->dirroot}/mod/{$name}/db/subplugins.php")) {
            $subplugins = array();
            include "{$CFG->dirroot}/mod/{$name}/db/subplugins.php";
            foreach ($subplugins as $subplugintype => $dir) {
                $instances = get_plugin_list($subplugintype);
                foreach ($instances as $subpluginname => $notusedpluginpath) {
                    uninstall_plugin($subplugintype, $subpluginname);
                }
            }
        }
    }
    $component = $type . '_' . $name;
    // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum'
    if ($type === 'mod') {
        $pluginname = $name;
        // eg. 'forum'
        if (get_string_manager()->string_exists('modulename', $component)) {
            $strpluginname = get_string('modulename', $component);
        } else {
            $strpluginname = $component;
        }
    } else {
        $pluginname = $component;
        if (get_string_manager()->string_exists('pluginname', $component)) {
            $strpluginname = get_string('pluginname', $component);
        } else {
            $strpluginname = $component;
        }
    }
    echo $OUTPUT->heading($pluginname);
    $plugindirectory = get_plugin_directory($type, $name);
    $uninstalllib = $plugindirectory . '/db/uninstall.php';
    if (file_exists($uninstalllib)) {
        require_once $uninstalllib;
        $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall';
        // eg. 'xmldb_workshop_uninstall()'
        if (function_exists($uninstallfunction)) {
            if (!$uninstallfunction()) {
                echo $OUTPUT->notification('Encountered a problem running uninstall function for ' . $pluginname);
            }
        }
    }
    if ($type === 'mod') {
        // perform cleanup tasks specific for activity modules
        if (!($module = $DB->get_record('modules', array('name' => $name)))) {
            print_error('moduledoesnotexist', 'error');
        }
        // delete all the relevant instances from all course sections
        if ($coursemods = $DB->get_records('course_modules', array('module' => $module->id))) {
            foreach ($coursemods as $coursemod) {
                if (!delete_mod_from_section($coursemod->id, $coursemod->section)) {
                    echo $OUTPUT->notification("Could not delete the {$strpluginname} with id = {$coursemod->id} from section {$coursemod->section}");
                }
            }
        }
        // clear course.modinfo for courses that used this module
        $sql = "UPDATE {course}\n                   SET modinfo=''\n                 WHERE id IN (SELECT DISTINCT course\n                                FROM {course_modules}\n                               WHERE module=?)";
        $DB->execute($sql, array($module->id));
        // delete all the course module records
        $DB->delete_records('course_modules', array('module' => $module->id));
        // delete module contexts
        if ($coursemods) {
            foreach ($coursemods as $coursemod) {
                if (!delete_context(CONTEXT_MODULE, $coursemod->id)) {
                    echo $OUTPUT->notification("Could not delete the context for {$strpluginname} with id = {$coursemod->id}");
                }
            }
        }
        // delete the module entry itself
        $DB->delete_records('modules', array('name' => $module->name));
        // cleanup the gradebook
        require_once $CFG->libdir . '/gradelib.php';
        grade_uninstalled_module($module->name);
        // Perform any custom uninstall tasks
        if (file_exists($CFG->dirroot . '/mod/' . $module->name . '/lib.php')) {
            require_once $CFG->dirroot . '/mod/' . $module->name . '/lib.php';
            $uninstallfunction = $module->name . '_uninstall';
            if (function_exists($uninstallfunction)) {
                debugging("{$uninstallfunction}() has been deprecated. Use the plugin's db/uninstall.php instead", DEBUG_DEVELOPER);
                if (!$uninstallfunction()) {
                    echo $OUTPUT->notification('Encountered a problem running uninstall function for ' . $module->name . '!');
                }
            }
        }
    } else {
        if ($type === 'enrol') {
            // NOTE: this is a bit brute force way - it will not trigger events and hooks properly
            // nuke all role assignments
            role_unassign_all(array('component' => $component));
            // purge participants
            $DB->delete_records_select('user_enrolments', "enrolid IN (SELECT id FROM {enrol} WHERE enrol = ?)", array($name));
            // purge enrol instances
            $DB->delete_records('enrol', array('enrol' => $name));
            // tweak enrol settings
            if (!empty($CFG->enrol_plugins_enabled)) {
                $enabledenrols = explode(',', $CFG->enrol_plugins_enabled);
                $enabledenrols = array_unique($enabledenrols);
                $enabledenrols = array_flip($enabledenrols);
                unset($enabledenrols[$name]);
                $enabledenrols = array_flip($enabledenrols);
                if (is_array($enabledenrols)) {
                    set_config('enrol_plugins_enabled', implode(',', $enabledenrols));
                }
            }
        } else {
            if ($type === 'block') {
                if ($block = $DB->get_record('block', array('name' => $name))) {
                    // Inform block it's about to be deleted
                    if (file_exists("{$CFG->dirroot}/blocks/{$block->name}/block_{$block->name}.php")) {
                        $blockobject = block_instance($block->name);
                        if ($blockobject) {
                            $blockobject->before_delete();
                            //only if we can create instance, block might have been already removed
                        }
                    }
                    // First delete instances and related contexts
                    $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
                    foreach ($instances as $instance) {
                        blocks_delete_instance($instance);
                    }
                    // Delete block
                    $DB->delete_records('block', array('id' => $block->id));
                }
            }
        }
    }
    // perform clean-up task common for all the plugin/subplugin types
    // delete calendar events
    $DB->delete_records('event', array('modulename' => $pluginname));
    // delete all the logs
    $DB->delete_records('log', array('module' => $pluginname));
    // delete log_display information
    $DB->delete_records('log_display', array('component' => $component));
    // delete the module configuration records
    unset_all_config_for_plugin($pluginname);
    // delete message provider
    message_provider_uninstall($component);
    // delete message processor
    if ($type === 'message') {
        message_processor_uninstall($name);
    }
    // delete the plugin tables
    $xmldbfilepath = $plugindirectory . '/db/install.xml';
    drop_plugin_tables($component, $xmldbfilepath, false);
    if ($type === 'mod' or $type === 'block') {
        // non-frankenstyle table prefixes
        drop_plugin_tables($name, $xmldbfilepath, false);
    }
    // delete the capabilities that were defined by this module
    capabilities_cleanup($component);
    // remove event handlers and dequeue pending events
    events_uninstall($component);
    echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
}
Exemplo n.º 19
0
     echo $OUTPUT->footer();
     exit;
 } else {
     // Inform block it's about to be deleted
     if (file_exists("{$CFG->dirroot}/blocks/{$block->name}/block_{$block->name}.php")) {
         $blockobject = block_instance($block->name);
         if ($blockobject) {
             $blockobject->before_delete();
             //only if we can create instance, block might have been already removed
         }
     }
     // First delete instances and then block
     $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
     if (!empty($instances)) {
         foreach ($instances as $instance) {
             blocks_delete_instance($instance);
         }
     }
     // Delete block
     $DB->delete_records('block', array('id' => $block->id));
     drop_plugin_tables($block->name, "{$CFG->dirroot}/blocks/{$block->name}/db/install.xml", false);
     // old obsoleted table names
     drop_plugin_tables('block_' . $block->name, "{$CFG->dirroot}/blocks/{$block->name}/db/install.xml", false);
     // Delete the capabilities that were defined by this block
     capabilities_cleanup('block/' . $block->name);
     // Remove event handlers and dequeue pending events
     events_uninstall('block/' . $block->name);
     $a->block = $strblockname;
     $a->directory = $CFG->dirroot . '/blocks/' . $block->name;
     notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
 }