Esempio n. 1
0
/**
 * Similar to blocks_get_by_page(), except that, the array returned includes
 * pinned blocks as well. Pinned blocks are always appended before normal
 * block instances.
 */
function blocks_get_by_page_pinned($page)
{
    $pinned = blocks_get_pinned($page);
    $user = blocks_get_by_page($page);
    $weights = array();
    foreach ($pinned as $pos => $arr) {
        $weights[$pos] = count($arr);
    }
    foreach ($user as $pos => $blocks) {
        if (!array_key_exists($pos, $pinned)) {
            $pinned[$pos] = array();
        }
        if (!array_key_exists($pos, $weights)) {
            $weights[$pos] = 0;
        }
        foreach ($blocks as $block) {
            $pinned[$pos][$weights[$pos]] = $block;
            $weights[$pos]++;
        }
    }
    return $pinned;
}
Esempio n. 2
0
/**
 * Returns blocks organized by page and weight for
 * a given page.
 *
 * @return array
 * @todo Eventually, I think that when pageitems that are modules are added, then
 *       a corresponding block_instance record will be created.  Right now, this
 *       is just how it works and it does keep number of block_instance records down
 * @todo Support pinned blocks?
 **/
function page_blocks_setup()
{
    global $CFG, $PAGE;
    $pageid = $PAGE->get_id();
    $type = $PAGE->get_type();
    $as = sql_as();
    // We must have a real page_module instance to work with
    // otherwise capability checks fail - here we look for one
    // in database or create one
    $instances = get_records_sql("SELECT i.*\n                                    FROM {$CFG->prefix}block_instance i,\n                                         {$CFG->prefix}block b\n                                   WHERE b.id = i.blockid\n                                     AND b.name = 'page_module'\n                                     AND i.pagetype = '{$type}'\n                                     AND i.pageid = {$pageid}");
    if (!$instances) {
        // Make a new one
        if (!($blockid = get_field('block', 'id', 'name', 'page_module'))) {
            error('page_module block is not installed and is required');
        }
        $weight = get_record_sql('SELECT 1, MAX(weight) + 1 ' . sql_as() . ' nextfree
                                    FROM ' . $CFG->prefix . 'block_instance
                                   WHERE pageid = ' . $pageid . '
                                     AND pagetype = \'' . $type . '\'
                                     AND position = \'' . BLOCK_POS_LEFT . '\'');
        if (empty($weight->nextfree)) {
            $weight->nextfree = 0;
        }
        $dummy = new stdClass();
        $dummy->blockid = $blockid;
        $dummy->pageid = $pageid;
        $dummy->pagetype = $type;
        $dummy->position = BLOCK_POS_LEFT;
        $dummy->weight = $weight->nextfree;
        $dummy->visible = 1;
        $dummy->configdata = '';
        if (!($dummy->id = insert_record('block_instance', $dummy))) {
            error('Failed to create page_module block instance');
        }
    } else {
        // Any will do really...
        $dummy = array_shift($instances);
    }
    // OLD METHOD - keeping code since it might be handy later, not sure though :\
    //
    // // Get our block instances by combining fields from the block_instance
    // // table and format_page_items table.  This is done because API only
    // // supports left/right columns and pageid = $courseid (which then makes
    // // the need for new weight values)
    // $blocks = get_records_sql("SELECT i.id $as pageitemid, b.id, b.blockid, b.pageid, b.pagetype,
    //                                   i.position, i.sortorder $as weight, i.visible, b.configdata
    //                              FROM {$CFG->prefix}format_page_items i,
    //                                   {$CFG->prefix}block_instance b
    //                             WHERE i.blockinstance = b.id
    //                               AND b.pageid = $pageid
    //                               AND b.pagetype = '$type'
    //                               AND i.pageid = {$PAGE->formatpage->id}
    //                          ORDER BY i.position, i.sortorder");
    //
    // if (!$blocks) {
    //     $blocks = array();
    // }
    //
    // // Get our page pageitems that are modules and process those
    // $modules = get_records_sql("SELECT i.id $as pageitemid, i.position, i.sortorder $as weight, i.visible, i.cmid $as configdata
    //                               FROM {$CFG->prefix}format_page_items i
    //                              WHERE i.blockinstance = 0
    //                                AND i.pageid = {$PAGE->formatpage->id}");
    //
    // if ($modules) {
    //     // Since these do not have block_instance records in the database
    //     // we need to add a couple of fields from our dummy instance to make
    //     // it look like a block_instance record
    //     foreach ($modules as $module) {
    //         $module->id       = $dummy->id;
    //         $module->blockid  = $dummy->blockid;
    //         $module->pageid   = $dummy->pageid;
    //         $module->pagetype = $dummy->pagetype;
    //
    //         $blocks[$module->pageitemid] = $module;
    //     }
    //     // Need to resort blocks by position and weight
    //     $function = create_function('$a, $b', 'return ($a->position == $b->position) ? strnatcasecmp($a->weight, $b->weight) : strcmp($a->position, $b->position);');
    //     usort($blocks, $function);
    // }
    // Get our block instances by combining fields from the block_instance
    // table and format_page_items table.  This is done because API only
    // supports left/right columns and pageid = $courseid
    // We then combine those block instances with "fake" page_module block
    // instance records for every module page item in format_page_items
    $sql = "(SELECT i.id {$as} pageitemid, b.id, b.blockid, b.pageid, b.pagetype, i.position, i.sortorder {$as} weight, i.visible, 0 {$as} dummycmdid, b.configdata\n                                  FROM {$CFG->prefix}format_page_items i,\n                                       {$CFG->prefix}block_instance b\n                                 WHERE i.blockinstance = b.id\n                                   AND b.pageid = {$pageid}\n                                   AND b.pagetype = '{$type}'\n                                   AND i.pageid = {$PAGE->formatpage->id})\n                                 UNION\n                               (SELECT i.id {$as} pageitemid, b.id, b.blockid, b.pageid, b.pagetype, i.position, i.sortorder {$as} weight, i.visible, i.cmid {$as} dummycmdid, '' {$as} configdata\n                                  FROM {$CFG->prefix}format_page_items i,\n                                       {$CFG->prefix}block_instance b\n                                 WHERE i.blockinstance = 0\n                                   AND b.pageid = {$pageid}\n                                   AND b.pagetype = '{$type}'\n                                   AND i.pageid = {$PAGE->formatpage->id}\n                                   AND b.id = {$dummy->id})\n                              ORDER BY position, weight";
    $blocks = get_records_sql($sql);
    $stickypageobject = $PAGE->get_hacked_object_for_sticky();
    // force id to be set
    $stickypageobject->id = $pageid;
    $sticky = blocks_get_pinned($stickypageobject);
    // Initialize $pageblocks array
    $positions = $PAGE->blocks_get_positions();
    $arrays = array_pad(array(), count($positions), array());
    $pageblocks = array_combine($positions, $arrays);
    // insert the sticky blocks into the pageblocks
    // and create some shifted weights to move everything else down.
    $shiftedweights = array();
    foreach ($sticky as $pos => $sblocks) {
        foreach ($sblocks as $b) {
            $pageblocks[$pos][] = $b;
        }
        $shiftedweights[$pos] = count($sblocks);
    }
    // for each position.
    // Load blocks into their apporpriate locations
    if (!empty($blocks)) {
        foreach ($blocks as $block) {
            if ($block->id == $dummy->id) {
                // Since our page_module instances are dummies, need to fix their config value
                $config = new stdClass();
                $config->cmid = $block->dummycmdid;
                $block->configdata = base64_encode(serialize($config));
            }
            $newweight = $shiftedweights[$block->position] + $block->weight;
            $pageblocks[$block->position][$newweight] = $block;
        }
    }
    // Handle block actions
    page_blocks_execute_url_action();
    return $pageblocks;
}