function blocks_repopulate_page($page) { global $CFG; $allblocks = blocks_get_record(); if (empty($allblocks)) { error('Could not retrieve blocks from the database'); } // Assemble the information to correlate block names to ids $idforname = array(); foreach ($allblocks as $block) { $idforname[$block->name] = $block->id; } /// If the site override has been defined, it is the only valid one. if (!empty($CFG->defaultblocks_override)) { $blocknames = $CFG->defaultblocks_override; } else { $blocknames = $page->blocks_get_default(); } $positions = $page->blocks_get_positions(); $posblocks = explode(':', $blocknames); // Now one array holds the names of the positions, and the other one holds the blocks // that are going to go in each position. Luckily for us, both arrays are numerically // indexed and the indexes match, so we can work straight away... but CAREFULLY! // Ready to start creating block instances, but first drop any existing ones blocks_delete_all_on_page($page->get_type(), $page->get_id()); // Here we slyly count $posblocks and NOT $positions. This can actually make a difference // if the textual representation has undefined slots in the end. So we only work with as many // positions were retrieved, not with all the page says it has available. $numpositions = count($posblocks); for ($i = 0; $i < $numpositions; ++$i) { $position = $positions[$i]; $blocknames = explode(',', $posblocks[$i]); $weight = 0; foreach ($blocknames as $blockname) { $newinstance = new stdClass(); $newinstance->blockid = $idforname[$blockname]; $newinstance->pageid = $page->get_id(); $newinstance->pagetype = $page->get_type(); $newinstance->position = $position; $newinstance->weight = $weight; $newinstance->visible = 1; $newinstance->configdata = ''; if (!empty($newinstance->blockid)) { // Only add block if it was recognized insert_record('block_instance', $newinstance); ++$weight; } } } return true; }
function restore_create_blocks($restore, $backup_block_format, $blockinfo, $xml_file) { global $CFG; $status = true; blocks_delete_all_on_page(PAGE_COURSE_VIEW, $restore->course_id); if (empty($backup_block_format)) { // This is a backup from Moodle < 1.5 if (empty($blockinfo)) { // Looks like it's from Moodle < 1.3. Let's give the course default blocks... $newpage = page_create_object(PAGE_COURSE_VIEW, $restore->course_id); blocks_repopulate_page($newpage); } else { // We just have a blockinfo field, this is a legacy 1.4 or 1.3 backup $blockrecords = get_records_select('block', '', '', 'name, id'); $temp_blocks_l = array(); $temp_blocks_r = array(); @(list($temp_blocks_l, $temp_blocks_r) = explode(':', $blockinfo)); $temp_blocks = array(BLOCK_POS_LEFT => explode(',', $temp_blocks_l), BLOCK_POS_RIGHT => explode(',', $temp_blocks_r)); foreach ($temp_blocks as $blockposition => $blocks) { $blockweight = 0; foreach ($blocks as $blockname) { if (!isset($blockrecords[$blockname])) { // We don't know anything about this block! continue; } $blockinstance = new stdClass(); // Remove any - prefix before doing the name-to-id mapping if (substr($blockname, 0, 1) == '-') { $blockname = substr($blockname, 1); $blockinstance->visible = 0; } else { $blockinstance->visible = 1; } $blockinstance->blockid = $blockrecords[$blockname]->id; $blockinstance->pageid = $restore->course_id; $blockinstance->pagetype = PAGE_COURSE_VIEW; $blockinstance->position = $blockposition; $blockinstance->weight = $blockweight; if (!($status = insert_record('block_instance', $blockinstance))) { $status = false; } ++$blockweight; } } } } else { if ($backup_block_format == 'instances') { $status = restore_create_block_instances($restore, $xml_file); } } return $status; }
function lesson_delete_instance($id) { /// Given an ID of an instance of this module, /// this function will permanently delete the instance /// and any data that depends on it. if (!($lesson = get_record("lesson", "id", "{$id}"))) { return false; } $result = true; if (!delete_records("lesson", "id", "{$lesson->id}")) { $result = false; } if (!delete_records("lesson_pages", "lessonid", "{$lesson->id}")) { $result = false; } if (!delete_records("lesson_answers", "lessonid", "{$lesson->id}")) { $result = false; } if (!delete_records("lesson_attempts", "lessonid", "{$lesson->id}")) { $result = false; } if (!delete_records("lesson_grades", "lessonid", "{$lesson->id}")) { $result = false; } if (!delete_records("lesson_timer", "lessonid", "{$lesson->id}")) { $result = false; } if (!delete_records("lesson_branch", "lessonid", "{$lesson->id}")) { $result = false; } if (!delete_records("lesson_high_scores", "lessonid", "{$lesson->id}")) { $result = false; } if ($events = get_records_select('event', "modulename = 'lesson' and instance = '{$lesson->id}'")) { foreach ($events as $event) { delete_event($event->id); } } $pagetypes = page_import_types('mod/lesson/'); foreach ($pagetypes as $pagetype) { if (!blocks_delete_all_on_page($pagetype, $lesson->id)) { $result = false; } } lesson_grade_item_delete($lesson); return $result; }
function quiz_delete_instance($id) { /// Given an ID of an instance of this module, /// this function will permanently delete the instance /// and any data that depends on it. if (!($quiz = get_record("quiz", "id", "{$id}"))) { return false; } $result = true; if ($attempts = get_records("quiz_attempts", "quiz", "{$quiz->id}")) { // TODO: this should use the delete_attempt($attempt->uniqueid) function in questionlib.php // require_once($CFG->libdir.'/questionlib.php'); foreach ($attempts as $attempt) { if (!delete_records("question_states", "attempt", "{$attempt->uniqueid}")) { $result = false; } if (!delete_records("question_sessions", "attemptid", "{$attempt->uniqueid}")) { $result = false; } } } $tables_to_purge = array('quiz_attempts' => 'quiz', 'quiz_grades' => 'quiz', 'quiz_question_instances' => 'quiz', 'quiz_grades' => 'quiz', 'quiz_feedback' => 'quizid', 'quiz' => 'id'); foreach ($tables_to_purge as $table => $keyfield) { if (!delete_records($table, $keyfield, $quiz->id)) { $result = false; } } $pagetypes = page_import_types('mod/quiz/'); foreach ($pagetypes as $pagetype) { if (!blocks_delete_all_on_page($pagetype, $quiz->id)) { $result = false; } } if ($events = get_records_select('event', "modulename = 'quiz' and instance = '{$quiz->id}'")) { foreach ($events as $event) { delete_event($event->id); } } quiz_grade_item_delete($quiz); return $result; }
function chat_delete_instance($id) { /// Given an ID of an instance of this module, /// this function will permanently delete the instance /// and any data that depends on it. if (!($chat = get_record('chat', 'id', $id))) { return false; } $result = true; # Delete any dependent records here # if (!delete_records('chat', 'id', $chat->id)) { $result = false; } if (!delete_records('chat_messages', 'chatid', $chat->id)) { $result = false; } if (!delete_records('chat_users', 'chatid', $chat->id)) { $result = false; } $pagetypes = page_import_types('mod/chat/'); foreach ($pagetypes as $pagetype) { if (!blocks_delete_all_on_page($pagetype, $chat->id)) { $result = false; } } if (!delete_records('event', 'modulename', 'chat', 'instance', $chat->id)) { $result = false; } return $result; }