Ejemplo n.º 1
0
/**
 * Same functionality as blocks_execute_url_action()
 * Handle all block actions ourselves.
 *
 * @param boolean $redirect (Optional) Redirect after action
 * @return void
 **/
function page_blocks_execute_url_action($redirect = true)
{
    global $CFG, $COURSE, $PAGE;
    $pageitemid = optional_param('pageitemid', 0, PARAM_INT);
    $blockaction = optional_param('blockaction', '', PARAM_ALPHA);
    // Reasons to stop right meow
    if (empty($blockaction) || !$PAGE->user_allowed_editing() || !confirm_sesskey()) {
        return;
    }
    // Make sure if we have a valid pageitem.
    if ($pageitemid and !($pageitem = get_record('format_page_items', 'id', $pageitemid))) {
        return;
    }
    switch ($blockaction) {
        case 'config':
            if (empty($pageitem->blockinstance) and !empty($pageitem->cmid)) {
                // Its a module - go to module update
                redirect("{$CFG->wwwroot}/course/mod.php?update={$pageitem->cmid}&sesskey=" . sesskey());
            } else {
                if (!empty($pageitem->blockinstance)) {
                    // Its a block instance - allow core routine to handle
                    redirect($PAGE->url_build('instanceid', $pageitem->blockinstance, 'blockaction', 'config', 'sesskey', sesskey()));
                } else {
                    error('Invalid page item to configure');
                }
            }
            break;
        case 'toggle':
            $update = new stdClass();
            $update->id = $pageitem->id;
            if (empty($pageitem->visible)) {
                $update->visible = 1;
            } else {
                $update->visible = 0;
            }
            update_record('format_page_items', $update);
            break;
        case 'delete':
            page_block_delete($pageitem);
            break;
        case 'moveup':
            page_block_move($pageitem, $pageitem->position, $pageitem->sortorder - 1);
            break;
        case 'movedown':
            page_block_move($pageitem, $pageitem->position, $pageitem->sortorder + 1);
            break;
        case 'moveright':
            $destposition = $PAGE->blocks_move_position($pageitem, BLOCK_MOVE_RIGHT);
            $destweight = page_get_next_weight($pageitem->pageid, $destposition);
            page_block_move($pageitem, $destposition, $destweight);
            break;
        case 'moveleft':
            $destposition = $PAGE->blocks_move_position($pageitem, BLOCK_MOVE_LEFT);
            $destweight = page_get_next_weight($pageitem->pageid, $destposition);
            page_block_move($pageitem, $destposition, $destweight);
            break;
        case 'addmod':
            // Right now, modules are added differently
            $instance = required_param('instance', PARAM_INT);
            $record = new stdClass();
            $record->pageid = $PAGE->formatpage->id;
            $record->cmid = $instance;
            $record->blockinstance = 0;
            $record->position = $PAGE->blocks_default_position();
            $record->sortorder = page_get_next_weight($record->pageid, $record->position);
            insert_record('format_page_items', $record);
            break;
        case 'add':
            // Add a block instance and a pageitem
            $blockid = required_param('blockid', PARAM_INT);
            $block = blocks_get_record($blockid);
            if (empty($block) or !$block->visible) {
                break;
            }
            if (!block_method_result($block->name, 'user_can_addto', $PAGE)) {
                break;
            }
            // Add a block instance if one does not already exist or if the block allows multiple block instances
            $exists = record_exists('block_instance', 'pageid', $PAGE->get_id(), 'pagetype', $PAGE->get_type(), 'blockid', $blockid);
            if ($block->multiple || !$exists) {
                // Get the next weight value NOTE: hard code left position
                $weight = get_record_sql('SELECT 1, MAX(weight) + 1 ' . sql_as() . ' nextfree
                                            FROM ' . $CFG->prefix . 'block_instance
                                           WHERE pageid = ' . $PAGE->get_id() . '
                                             AND pagetype = \'' . $PAGE->get_type() . '\'
                                             AND position = \'' . BLOCK_POS_LEFT . '\'');
                if (empty($weight->nextfree)) {
                    $weight->nextfree = 0;
                }
                $newinstance = new stdClass();
                $newinstance->blockid = $blockid;
                $newinstance->pageid = $PAGE->get_id();
                $newinstance->pagetype = $PAGE->get_type();
                $newinstance->position = BLOCK_POS_LEFT;
                // Make sure we keep them all in same column
                $newinstance->weight = $weight->nextfree;
                $newinstance->visible = 1;
                $newinstance->configdata = '';
                $instanceid = $newinstance->id = insert_record('block_instance', $newinstance);
                if ($newinstance and $obj = block_instance($block->name, $newinstance)) {
                    // Return value ignored
                    $obj->instance_create();
                }
            } else {
                if ($exists) {
                    // Get the existing blockinstance as the block only allows one instance.
                    $instanceid = get_field('block_instance', 'id', 'pageid', $PAGE->get_id(), 'pagetype', $PAGE->get_type(), 'blockid', $blockid);
                }
            }
            if (!empty($instanceid)) {
                // Create a new page item that links to the instance
                $record = new stdClass();
                $record->pageid = $PAGE->formatpage->id;
                $record->cmid = 0;
                $record->blockinstance = $instanceid;
                $record->position = $PAGE->blocks_default_position();
                $record->sortorder = page_get_next_weight($record->pageid, $record->position);
                insert_record('format_page_items', $record);
            }
            break;
    }
    if ($redirect) {
        // In order to prevent accidental duplicate actions, redirect to a page with a clean url
        redirect($PAGE->url_get_full());
    }
}
Ejemplo n.º 2
0
function tao_add_learningpath_pageitem($formatpageid, $instanceid, $raflitemid = null)
{
    // Create a new page item that links to the instance
    $pageitem = new stdClass();
    $pageitem->pageid = $formatpageid;
    $pageitem->cmid = 0;
    $pageitem->blockinstance = $instanceid;
    $pageitem->position = BLOCK_POS_CENTER;
    $pageitem->sortorder = page_get_next_weight($pageitem->pageid, $pageitem->position);
    $pageitem->visible = 1;
    $pageitem->rafl_item = $raflitemid;
    if (!insert_record('format_page_items', $pageitem)) {
        error('could not insert format_page_item');
    }
}