/** * This function displays the controls to add modules and blocks to a page * * @param object $page A fully populated page object * @param object $course A fully populated course object * @uses $USER; * @uses $CFG; */ function page_print_add_mods_form($page, $course) { global $USER, $CFG, $PAGE; if (empty($PAGE)) { $PAGE = page_create_object(PAGE_COURSE_VIEW, $course->id); } print_box_start('centerpara addpageitems'); // Add drop down to add blocks if ($blocks = get_records('block', 'visible', '1', 'name')) { $format = $PAGE->get_format_name(); $options = array(); foreach ($blocks as $b) { if (in_array($b->name, array('format_page', 'page_module'))) { continue; } if (!blocks_name_allowed_in_format($b->name, $format)) { continue; } $blockobject = block_instance($b->name); if ($blockobject !== false && $blockobject->user_can_addto($PAGE)) { $options[$b->id] = $blockobject->get_title(); } } asort($options); print '<span class="addblock">'; $common = $CFG->wwwroot . '/course/format/page/format.php?id=' . $course->id . '&page=' . $page->id . '&blockaction=add&sesskey=' . sesskey() . '&blockid='; popup_form($common, $options, 'addblock', '', get_string('addblock', 'format_page')); print '</span> '; } // Add drop down to add existing module instances if ($modules = page_get_modules($course, 'name')) { // From our modules object we can build an existing module menu using separators $options = array(); foreach ($modules as $modplural => $instances) { // Sets an optgroup which can't be selected/submitted $options[$modplural . '_group_start'] = "--{$modplural}"; foreach ($instances as $cmid => $name) { $options[$cmid] = shorten_text($name); } // Ends an optgroup $options[$modplural . '_group_end'] = '--'; } print '<span class="addexistingmodule">'; $common = $CFG->wwwroot . '/course/format/page/format.php?id=' . $course->id . '&page=' . $page->id . '&blockaction=addmod&sesskey=' . sesskey() . '&instance='; popup_form($common, $options, 'addinstance', '', get_string('addexistingmodule', 'format_page')); print '</span>'; } print_box_end(); }
/** * The list of block types that may be added to this page. * * @return array block name => record from block table. */ public function get_addable_blocks() { $this->check_is_loaded(); if (!is_null($this->addableblocks)) { return $this->addableblocks; } // Lazy load. $this->addableblocks = array(); $allblocks = blocks_get_record(); if (empty($allblocks)) { return $this->addableblocks; } $unaddableblocks = self::get_undeletable_block_types(); $pageformat = $this->page->pagetype; foreach ($allblocks as $block) { if (!($bi = block_instance($block->name))) { continue; } if ($block->visible && !in_array($block->name, $unaddableblocks) && ($bi->instance_allow_multiple() || !$this->is_block_present($block->name)) && blocks_name_allowed_in_format($block->name, $pageformat) && $bi->user_can_addto($this->page)) { $this->addableblocks[$block->name] = $block; } } return $this->addableblocks; }
function blocks_remove_inappropriate($page) { $pageblocks = blocks_get_by_page($page); if (empty($pageblocks)) { return; } if (($pageformat = $page->get_format_name()) == NULL) { return; } foreach ($pageblocks as $position) { foreach ($position as $instance) { $block = blocks_get_record($instance->blockid); if (!blocks_name_allowed_in_format($block->name, $pageformat)) { blocks_delete_instance($instance); } } } }
/** * The list of block types that may be added to this page. * * @return array block name => record from block table. */ public function get_addable_blocks() { $this->check_is_loaded(); if (!is_null($this->addableblocks)) { return $this->addableblocks; } // Lazy load. $this->addableblocks = array(); $allblocks = blocks_get_record(); if (empty($allblocks)) { return $this->addableblocks; } $pageformat = $this->page->pagetype; foreach ($allblocks as $block) { if ($block->visible && (block_method_result($block->name, 'instance_allow_multiple') || !$this->is_block_present($block->name)) && blocks_name_allowed_in_format($block->name, $pageformat) && block_method_result($block->name, 'user_can_addto', $this->page)) { $this->addableblocks[$block->name] = $block; } } return $this->addableblocks; }