}
// First delete instances and then block
- $instances = $DB->get_records('block_instance_old', array('blockid'=>$block->id));
+ $instances = $DB->get_records('block_instances', array('blockname' => $block->name));
if(!empty($instances)) {
foreach($instances as $instance) {
blocks_delete_instance($instance);
// MDL-11167, blocks can be placed on mymoodle, or the blogs page
// and it should not show up on course search page
- $totalcount = $DB->count_records('block_instance_old', array('blockid'=>$blockid));
- $count = $DB->count_records('block_instance_old', array('blockid'=>$blockid, 'pagetype'=>'course-view'));
+ $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname));
+ $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*'));
if ($count>0) {
$blocklist = "<a href=\"{$CFG->wwwroot}/course/search.php?blocklist=$blockid&sesskey=".sesskey()."\" ";
/// Try to replace some well-known serialised contents (html blocks)
notify('Replacing in html blocks...');
-$sql = "SELECT bi.*
- FROM {block_instance_old} bi
- JOIN {block} b ON b.id = bi.blockid
- WHERE b.name = 'html'";
-if ($instances = $DB->get_records_sql($sql)) {
- foreach ($instances as $instance) {
- $blockobject = block_instance('html', $instance);
- $blockobject->config->text = str_replace($search, $replace, $blockobject->config->text);
- $blockobject->instance_config_commit($blockobject->pinned);
- }
+$instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
+foreach ($instances as $instance) {
+ $blockobject = block_instance('html', $instance);
+ $blockobject->config->text = str_replace($search, $replace, $blockobject->config->text);
+ $blockobject->instance_config_commit($blockobject->pinned);
}
+$instances->close();
/// Rebuild course cache which might be incorrect now
notify('Rebuilding course cache...', 'notifysuccess');
}
// add all roles assigned at block context
- if ($courseblocks = $DB->get_records_sql("SELECT *
- FROM {block_instance_old}
- WHERE pagetype = '".PAGE_COURSE_VIEW."'
- AND pageid = ?", array($preferences->backup_course))) {
+ if ($courseblocks = $DB->get_records_sql("SELECT * FROM {block_instances} WHERE contextid = ?",
+ array($coursecontext->id))) {
foreach ($courseblocks as $courseblock) {
/**
* This file contains the parent class for moodle blocks, block_base.
*
- * @author Jon Papaioannou
- * @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package blocks
*/
*/
function is_empty() {
- // TODO
- if (empty($this->instance->pinned)) {
+ // TODO - temporary hack to get the block context only if it already exists.
+ global $DB;
+ if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
$context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
} else {
$context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
function _add_edit_controls($options) {
global $CFG, $USER;
- // TODO
- if (empty($this->instance->pinned)) {
+ // TODO - temporary hack to get the block context only if it already exists.
+ global $DB;
+ if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
$context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
} else {
$context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
/**
* Serialize and store config data
- * @return boolean
- * @todo finish documenting this function
*/
- function instance_config_save($data,$pinned=false) {
+ function instance_config_save($data, $nolongerused = false) {
global $DB;
-
- $data = $data;
- $this->config = $data;
- $table = 'block_instance_old';
- $field = 'oldid';
- if (!empty($pinned)) {
- $table = 'block_pinned_old';
- $field = 'id';
- }
- return $DB->set_field($table, 'configdata', base64_encode(serialize($data)), array($field => $this->instance->id));
+ $DB->set_field('block_instances', 'configdata', base64_encode(serialize($data)),
+ array($field => $this->instance->id));
}
/**
* @return boolean
* @todo finish documenting this function
*/
- function instance_config_commit($pinned=false) {
+ function instance_config_commit($nolongerused = false) {
global $DB;
-
- $table = 'block_instance_old';
- $field = 'oldid';
- if (!empty($pinned)) {
- $table = 'block_pinned_old';
- $field = 'id';
- }
- return $DB->set_field($table, 'configdata', base64_encode(serialize($this->config)), array($field => $this->instance->id));
+ $this->instance_config_save($this->config);
}
/**
function is_empty() {
- // TODO
- if (empty($this->instance->pinned)) {
+ // TODO - temporary hack to get the block context only if it already exists.
+ global $DB;
+ if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
$context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
} else {
$context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
// Get context so we can check capabilities.
$context = $this->page->context;
- // TODO
- if (empty($this->instance->pinned)) {
- $blockcontext = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
+ // TODO - temporary hack to get the block context only if it already exists.
+ global $DB;
+ if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
+ $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
} else {
- $blockcontext = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
+ $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
}
//Calculate if we are in separate groups
}
}
- // TODO
- if (empty($this->instance->pinned)) {
+ // TODO - temporary hack to get the block context only if it already exists.
+ global $DB;
+ if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
$context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
} else {
$context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
print $checkbox . $feedtitle .'<br />'."\n";
}
} else {
- // TODO
- if (empty($this->instance->pinned)) {
+ // TODO - temporary hack to get the block context only if it already exists.
+ global $DB;
+ if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
$context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
} else {
$context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
} else {
global $act, $url, $rssid, $preferredtitle, $shared;
print '</div></form></div>'; // Closes off page form
- // TODO
- if (empty($this->instance->pinned)) {
+ // TODO - temporary hack to get the block context only if it already exists.
+ global $DB;
+ if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
$context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
} else {
$context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
global $USER;
$tabs = $row = array();
-// TODO
-if (empty($this->instance->pinned)) {
- $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
-} else {
- $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
-}
+ // TODO - temporary hack to get the block context only if it already exists.
+ global $DB;
+ if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
+ $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
+ } else {
+ $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
+ }
if (has_capability('moodle/site:manageblocks', $context)) {
$script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'configblock', 'id' => $id, 'section' => 'rss'));
$tagsblock = $DB->get_record('block', array('name'=>'blog_tags'));
// add those 2 into block_instance page
- // add blog_menu block
- $newblock = new object();
- $newblock->blockid = $menublock->id;
- $newblock->pageid = $USER->id;
- $newblock->pagetype = 'blog-view';
- $newblock->position = 'r';
- $newblock->weight = 0;
- $newblock->visible = 1;
- $DB->insert_record('block_instance_old', $newblock);
-
- // add blog_tags menu
- $newblock -> blockid = $tagsblock->id;
- $newblock -> weight = 1;
- $DB->insert_record('block_instance_old', $newblock);
+// Commmented out since the block changes broke it. Hopefully nico will fix it ;-)
+// // add blog_menu block
+// $newblock = new object();
+// $newblock->blockid = $menublock->id;
+// $newblock->pageid = $USER->id;
+// $newblock->pagetype = 'blog-view';
+// $newblock->position = 'r';
+// $newblock->weight = 0;
+// $newblock->visible = 1;
+// $DB->insert_record('block_instances', $newblock);
+//
+// // add blog_tags menu
+// $newblock -> blockid = $tagsblock->id;
+// $newblock -> weight = 1;
+// $DB->insert_record('block_instances', $newblock);
// finally we set the page size pref
set_user_preference('blogpagesize', 10);
<?php // $Id$
// Admin-only code to delete a course utterly
- require_once("../config.php");
+ require_once(dirname(__FILE__) . '/../config.php');
+ require_once($CFG->dirroot . '/course/lib.php');
$id = required_param('id', PARAM_INT); // course id
$delete = optional_param('delete', '', PARAM_ALPHANUM); // delete confirmation hash
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
+ $PAGE->set_url('course/index.php');
+ $PAGE->set_context($systemcontext);
+
if (update_category_button()) {
if ($categoryedit !== -1) {
$USER->editing = $categoryedit;
// get list of courses containing blocks if required
if (!empty($blocklist) and confirm_sesskey()) {
- $blockid = $blocklist;
- if (!$blocks = $DB->get_records('block_instance_old', array('blockid'=>$blockid))) {
- print_error('blockcannotread', '', '', $blockid);
- }
-
- // run through blocks and get (unique) courses
+ $blockname = $DB->get_field('block', 'name', array('id' => $blocklist));
+ $courses = $DB->get_recordset_sql("
+ SELECT * FROM {course} WHERE id IN (
+ SELECT DISTINCT ctx.instanceid
+ FROM {context} ctx
+ JOIN {block_instances} bi ON bi.contextid = ctx.id
+ WHERE ctx.contextlevel = " . CONTEXT_COURSE . " AND bi.blockname = ?)",
+ array($blockname));
$courses = array();
- foreach ($blocks as $block) {
- $courseid = $block->pageid;
- // MDL-11167, blocks can be placed on mymoodle, or the blogs page
- // and it should not show up on course search page
- if ($courseid==0 || $block->pagetype != 'course-view') {
- continue;
- }
- if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
- print_error('invalidcourseid', '', '', $courseid);
- }
- $courses[$courseid] = $course;
+ foreach ($courses as $course) {
+ $courses[$course->id] = $course;
}
- $totalcount = count( $courses );
+ $totalcount = count($courses);
}
// get list of courses containing modules if required
elseif (!empty($modulelist) and confirm_sesskey()) {
case CONTEXT_BLOCK:
// Only non-pinned & course-page based
$sql = "SELECT ctx.path, ctx.depth
- FROM {context} ctx
- JOIN {block_instance_old} bi
- ON (bi.pageid=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
- WHERE bi.oldid=? AND bi.pagetype='course-view'";
- $params = array($instanceid);
+ FROM {context} ctx
+ JOIN {block_instances} bi ON (bi.contextid=ctx.id)
+ WHERE bi.id=? AND ctx.contextlevel=?";
+ $params = array($instanceid, CONTEXT_COURSE);
if ($p = $DB->get_record_sql($sql, $params)) {
$basepath = $p->path;
$basedepth = $p->depth;
- } else if ($bi = $DB->get_record('block_instance_old', array('oldid'=>$instanceid))) {
- if ($bi->pagetype != 'course-view') {
- // ok - not a course block
- } else if ($parent = get_context_instance(CONTEXT_COURSE, $bi->pageid)) {
- $basepath = $parent->path;
- $basedepth = $parent->depth;
- } else {
- // parent course does not exist - course blocks can not exist without a course
- $error_message = 'parent course does not exist - course blocks can not exist without a course';
- $result = false;
- }
} else {
// block does not exist
- $error_message = 'block does not exist';
+ $error_message = 'block or parent context does not exist';
$result = false;
}
break;
if (empty($contextlevel) or $contextlevel == CONTEXT_BLOCK) {
$sql = "INSERT INTO {context} (contextlevel, instanceid)
- SELECT ".CONTEXT_BLOCK.", bi.oldid
- FROM {block_instance_old} bi
+ SELECT ".CONTEXT_BLOCK.", bi.id
+ FROM {block_instances} bi
WHERE NOT EXISTS (SELECT 'x'
FROM {context} cx
WHERE bi.id = cx.instanceid AND cx.contextlevel=".CONTEXT_BLOCK.")";
SELECT c.contextlevel,
c.instanceid
FROM {context} c
- LEFT OUTER JOIN {block_instance_old} t
- ON c.instanceid = t.oldid
+ LEFT OUTER JOIN {block_instances} t
+ ON c.instanceid = t.id
WHERE t.id IS NULL AND c.contextlevel = ".CONTEXT_BLOCK."
";
if ($rs = $DB->get_recordset_sql($sql)) {
UNION ALL
SELECT x.instanceid, x.id, x.contextlevel, x.path, x.depth
- FROM {block_instance_old} bi
- JOIN {context} x ON x.instanceid=bi.oldid
- WHERE bi.pageid=? AND bi.pagetype='course-view'
+ FROM {context} px
+ JOIN {block_instances} bi ON bi.contextid = px.id
+ JOIN {context} x ON x.instanceid=bi.id
+ WHERE px.instanceid = ? AND px.contextlevel = ".CONTEXT_COURSE."
AND x.contextlevel=".CONTEXT_BLOCK."
UNION ALL
break;
case CONTEXT_BLOCK: // not necessarily 1 to 1 to course
- if ($blockinstance = $DB->get_record('block_instance_old', array('oldid'=>$context->instanceid))) {
- if ($block = $DB->get_record('block', array('id'=>$blockinstance->blockid))) {
- global $CFG;
- require_once("$CFG->dirroot/blocks/moodleblock.class.php");
- require_once("$CFG->dirroot/blocks/$block->name/block_$block->name.php");
- $blockname = "block_$block->name";
- if ($blockobject = new $blockname()) {
- if ($withprefix){
- $name = get_string('block').': ';
- }
- $name .= $blockobject->title;
+ if ($blockinstance = $DB->get_record('block_instances', array('id'=>$context->instanceid))) {
+ global $CFG;
+ require_once("$CFG->dirroot/blocks/moodleblock.class.php");
+ require_once("$CFG->dirroot/blocks/$blockinstance->blockname/block_$blockinstance->blockname.php");
+ $blockname = "block_$blockinstance->blockname";
+ if ($blockobject = new $blockname()) {
+ if ($withprefix){
+ $name = get_string('block').': ';
}
+ $name .= $blockobject->title;
}
}
break;
break;
case CONTEXT_BLOCK: // block caps
- $cb = $DB->get_record('block_instance_old', array('oldid'=>$context->instanceid));
- $block = $DB->get_record('block', array('id'=>$cb->blockid));
-
- $extra = "";
- if ($blockinstance = block_instance($block->name)) {
- if ($extracaps = $blockinstance->get_extra_capabilities()) {
- list($extra, $params) = $DB->get_in_or_equal($extracaps, SQL_PARAMS_NAMED, 'cap0');
- $extra = "OR name $extra";
- }
+ $cb = $DB->get_record('block_instances', array('id'=>$context->instanceid));
+
+ $extra = '';
+ $extracaps = block_method_result($cb->blockname, 'get_extra_capabilities');
+ if ($extracaps) {
+ list($extra, $params) = $DB->get_in_or_equal($extracaps, SQL_PARAMS_NAMED, 'cap0');
+ $extra = "OR name $extra";
}
$SQL = "SELECT *
return $DB->get_records_sql("
SELECT ctx.*
FROM {context} ctx
- LEFT JOIN {user} u ON ctx.contextlevel = 30 AND u.id = ctx.instanceid
- LEFT JOIN {course_categories} cat ON ctx.contextlevel = 40 AND cat.id = ctx.instanceid
- LEFT JOIN {course} c ON ctx.contextlevel = 50 AND c.id = ctx.instanceid
- LEFT JOIN {course_modules} cm ON ctx.contextlevel = 70 AND cm.id = ctx.instanceid
- LEFT JOIN {block_instance_old} bi ON ctx.contextlevel = 80 AND bi.oldid = ctx.instanceid
+ LEFT JOIN {user} u ON ctx.contextlevel = " . CONTEXT_USER . " AND u.id = ctx.instanceid
+ LEFT JOIN {course_categories} cat ON ctx.contextlevel = " . CONTEXT_COURSECAT . " AND cat.id = ctx.instanceid
+ LEFT JOIN {course} c ON ctx.contextlevel = " . CONTEXT_COURSE . " AND c.id = ctx.instanceid
+ LEFT JOIN {course_modules} cm ON ctx.contextlevel = " . CONTEXT_MODULE . " AND cm.id = ctx.instanceid
+ LEFT JOIN {block_instances} bi ON ctx.contextlevel = " . CONTEXT_BLOCK . " AND bi.id = ctx.instanceid
$select
- ORDER BY ctx.contextlevel, bi.position, COALESCE(cat.sortorder, c.sortorder, cm.section, bi.weight), u.lastname, u.firstname, cm.id
+ ORDER BY ctx.contextlevel, bi.region, COALESCE(cat.sortorder, c.sortorder, cm.section, bi.weight), u.lastname, u.firstname, cm.id
", $params);
}
return false;
}
+/**
+ * Delete a block, and associated data.
+ * @param object $instance a row from the block_instances table
+ * @param $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient.
+ */
+function blocks_delete_block($instance, $skipblockstables = false) {
+ if ($block = block_instance($block->blockname, $instance)) {
+ $block->instance_delete();
+ }
+ delete_context(CONTEXT_BLOCK, $instance->id);
+
+ if (!$skipblockstables) {
+ $DB->delete_records('block_positions', array('blockinstanceid' => $instance->id));
+ $DB->delete_records('block_instances', array('id' => $instance->id));
+ }
+}
+
/**
* Delete all the blocks that belong to a particular context.
* @param $contextid the context id.
global $DB;
$instances = $DB->get_recordset('block_instances', array('contextid' => $contextid));
foreach ($instances as $instance) {
- delete_context(CONTEXT_BLOCK, $instance->id);
+ blocks_delete_block($instance, true);
}
$instances->close();
$DB->delete_records('block_instances', array('contextid' => $contextid));
* @return array
*/
function blocks_parse_default_blocks_list($blocksstr) {
- list($left, $right) = explode(':', $blocksstr);
- return array(
- BLOCK_POS_LEFT => explode(',', $left),
- BLOCK_POS_RIGHT => explode(',', $right),
- );
+ $blocks = array();
+ $bits = explode(':', $blocksstr);
+ if (!empty($bits)) {
+ $blocks[BLOCK_POS_LEFT] = explode(',', array_shift($bits));
+ }
+ if (!empty($bits)) {
+ $blocks[BLOCK_POS_RIGHT] = explode(',', array_shift($bits));
+ }
+ return $blocks;
}
/**
global $CFG;
if (!empty($CFG->defaultblocks_site)) {
- blocks_parse_default_blocks_list($CFG->defaultblocks_site);
+ return blocks_parse_default_blocks_list($CFG->defaultblocks_site);
} else {
- $blocknames = array(
+ return array(
BLOCK_POS_LEFT => array('site_main_menu', 'admin_tree'),
BLOCK_POS_RIGHT => array('course_summary', 'calendar_month')
);
}
}
+ if ($course->id == SITEID) {
+ $pagetypepattern = 'site-index';
+ } else {
+ $pagetypepattern = 'course-view-*';
+ }
+
$page = new moodle_page();
$page->set_course($course);
- $page->blocks->add_blocks(array($blocknames), 'course-view-*');
+ print_object($page); // DONOTCOMMIT
+ print_object($pagetypepattern); // DONOTCOMMIT
+ print_object($blocknames); // DONOTCOMMIT
+ $page->blocks->add_blocks($blocknames, $pagetypepattern);
}
/**
if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
print_error('invalidcourseid');
}
+ $context = get_context_instance(CONTEXT_COURSE, $courseid);
$strdeleted = get_string('deleted');
notify_local_delete_course($courseid, $showfeedback);
/// Delete course blocks
-
- if ($blocks = $DB->get_records_sql("SELECT *
- FROM {block_instance_old}
- WHERE pagetype = '".PAGE_COURSE_VIEW."'
- AND pageid = ?", array($course->id))) {
- if ($DB->delete_records('block_instance_old', array('pagetype'=>PAGE_COURSE_VIEW, 'pageid'=>$course->id))) {
- if ($showfeedback) {
- notify($strdeleted .' block_instance_old');
- }
-
- foreach ($blocks as $block) { /// Delete any associated contexts for this block
-
- delete_context(CONTEXT_BLOCK, $block->id);
-
- // fix for MDL-7164
- // Get the block object and call instance_delete()
- if (!$record = blocks_get_record($block->blockid)) {
- $result = false;
- continue;
- }
- if (!$obj = block_instance($record->name, $block)) {
- $result = false;
- continue;
- }
- // Return value ignored, in core mods this does not do anything, but just in case
- // third party blocks might have stuff to clean up
- // we execute this anyway
- $obj->instance_delete();
-
- }
- } else {
- $result = false;
- }
- }
+ blocks_delete_all_for_context($context->id);
/// Delete any groups, removing members and grouping/course links first.
require_once($CFG->dirroot.'/group/lib.php');
question_delete_course($course, $showfeedback);
/// Remove all data from gradebook
- $context = get_context_instance(CONTEXT_COURSE, $courseid);
remove_course_grades($courseid, $showfeedback);
remove_grade_letters($context, $showfeedback);
if ($this->_legacypageobject) {
return $this->_legacypageobject->user_allowed_editing();
}
- return has_any_capability($this->all_editing_caps(), $this->_context);
+ return has_any_capability($this->all_editing_caps(), $this->context);
}
/// Setter methods =============================================================
$legacypage->set_course($SITE);
}
}
+
$legacypage->set_pagetype($type);
$legacypage->set_url($ME);