/** * Delete a course, including all related data from the database, * and any associated files. * * @global object * @global object * @param mixed $courseorid The id of the course or course object to delete. * @param bool $showfeedback Whether to display notifications of each action the function performs. * @return bool true if all the removals succeeded. false if there were any failures. If this * method returns false, some of the removals will probably have succeeded, and others * failed, but you have no way of knowing which. */ function delete_course($courseorid, $showfeedback = true) { global $DB; if (is_object($courseorid)) { $courseid = $courseorid->id; $course = $courseorid; } else { $courseid = $courseorid; if (!($course = $DB->get_record('course', array('id' => $courseid)))) { return false; } } $context = get_context_instance(CONTEXT_COURSE, $courseid); // frontpage course can not be deleted!! if ($courseid == SITEID) { return false; } // make the course completely empty remove_course_contents($courseid, $showfeedback); // delete the course and related context instance delete_context(CONTEXT_COURSE, $courseid); $DB->delete_records("course", array("id" => $courseid)); //trigger events $course->context = $context; // you can not fetch context in the event because it was already deleted events_trigger('course_deleted', $course); return true; }
/** * Delete a block, and associated data. * * @param object $instance a row from the block_instances table * @param bool $nolongerused legacy parameter. Not used, but kept for backwards compatibility. * @param bool $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient. */ function blocks_delete_instance($instance, $nolongerused = false, $skipblockstables = false) { global $DB; if ($block = block_instance($instance->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)); $DB->delete_records_list('user_preferences', 'name', array('block' . $instance->id . 'hidden', 'docked_block_instance_' . $instance->id)); } }
private function process_category_group_node($groupnode, $xpath) { global $DB, $CFG; $group = new stdClass(); if ($groupnode->getAttribute("recstatus") != 3) { $groupname = $xpath->evaluate("description/short", $groupnode)->item(0); if ($groupname) { $group->name = $groupname->nodeValue; } $groupdescription = $xpath->evaluate("sourcedid/id", $groupnode)->item(0); if ($groupdescription) { $group->description = htmlspecialchars_decode($groupdescription->nodeValue); } $parentgroup = $xpath->evaluate("relationship/sourcedid/id", $groupnode)->item(0); if ($parentgroup) { $parentid = $DB->get_field_select('course_categories', 'id', 'description =\' ' . htmlspecialchars_decode($parentgroup->nodeValue) . '\''); if ($parentid) { $group->parent = $parentid; } } else { $group->parent = 0; } $id = $DB->get_record_select('course_categories', 'description=\'' . $group->description . '\''); if (!$id) { $group->id = $DB->insert_record('course_categories', $group); $classname = context_helper::get_class_for_level(CONTEXT_COURSECAT); $group->context = $classname::instance($group->id, IGNORE_MISSING); mark_context_dirty($group->context->path); $DB->update_record('course_categories', $group); fix_course_sortorder(); } } else { $groupname = $xpath->evaluate("description/short", $groupnode)->item(0); if ($groupname) { $group->name = $groupname->nodeValue; } $groupdescription = $xpath->evaluate("sourcedid/id", $groupnode)->item(0); if ($groupdescription) { $group->description = htmlspecialchars_decode($groupdescription->nodeValue); } $parentgroup = $xpath->evaluate("relationship/sourcedid/id", $groupnode)->item(0); if ($parentgroup) { $parentid = $DB->get_field_select('course_categories', 'id', 'description =\' ' . htmlspecialchars_decode($parentgroup->nodeValue) . '\''); if ($parentid) { $group->parent = $parentid; } } else { $group->parent = 0; } $id = $DB->get_record_select('course_categories', 'description=\'' . $group->description . '\''); if ($id) { if ($children = $DB->get_records('course_categories', array('parent' => $id->id), 'sortorder ASC')) { echo 'has cats!'; } else { if ($courses = $DB->get_records('course', array('category' => $id->id), 'sortorder ASC')) { echo 'has courses!'; } else { $DB->delete_records('course_categories', array('id' => $id->id)); delete_context(CONTEXT_COURSECAT, $id->id); } } } } }
/** * A small functional test of accesslib functions and classes. * @return void */ public function test_everything_in_accesslib() { global $USER, $SITE, $CFG, $DB, $ACCESSLIB_PRIVATE; $this->resetAfterTest(true); $generator = $this->getDataGenerator(); // Fill the site with some real data $testcategories = array(); $testcourses = array(); $testpages = array(); $testblocks = array(); $allroles = $DB->get_records_menu('role', array(), 'id', 'archetype, id'); $systemcontext = context_system::instance(); $frontpagecontext = context_course::instance(SITEID); // Add block to system context $bi = $generator->create_block('online_users'); context_block::instance($bi->id); $testblocks[] = $bi->id; // Some users $testusers = array(); for($i=0; $i<20; $i++) { $user = $generator->create_user(); $testusers[$i] = $user->id; $usercontext = context_user::instance($user->id); // Add block to user profile $bi = $generator->create_block('online_users', array('parentcontextid'=>$usercontext->id)); $testblocks[] = $bi->id; } // Deleted user - should be ignored everywhere, can not have context $generator->create_user(array('deleted'=>1)); // Add block to frontpage $bi = $generator->create_block('online_users', array('parentcontextid'=>$frontpagecontext->id)); $frontpageblockcontext = context_block::instance($bi->id); $testblocks[] = $bi->id; // Add a resource to frontpage $page = $generator->create_module('page', array('course'=>$SITE->id)); $testpages[] = $page->id; $frontpagepagecontext = context_module::instance($page->cmid); // Add block to frontpage resource $bi = $generator->create_block('online_users', array('parentcontextid'=>$frontpagepagecontext->id)); $frontpagepageblockcontext = context_block::instance($bi->id); $testblocks[] = $bi->id; // Some nested course categories with courses $manualenrol = enrol_get_plugin('manual'); $parentcat = 0; for($i=0; $i<5; $i++) { $cat = $generator->create_category(array('parent'=>$parentcat)); $testcategories[] = $cat->id; $catcontext = context_coursecat::instance($cat->id); $parentcat = $cat->id; if ($i >=4) { continue; } // Add resource to each category $bi = $generator->create_block('online_users', array('parentcontextid'=>$catcontext->id)); context_block::instance($bi->id); // Add a few courses to each category for($j=0; $j<6; $j++) { $course = $generator->create_course(array('category'=>$cat->id)); $testcourses[] = $course->id; $coursecontext = context_course::instance($course->id); if ($j >= 5) { continue; } // Add manual enrol instance $manualenrol->add_default_instance($DB->get_record('course', array('id'=>$course->id))); // Add block to each course $bi = $generator->create_block('online_users', array('parentcontextid'=>$coursecontext->id)); $testblocks[] = $bi->id; // Add a resource to each course $page = $generator->create_module('page', array('course'=>$course->id)); $testpages[] = $page->id; $modcontext = context_module::instance($page->cmid); // Add block to each module $bi = $generator->create_block('online_users', array('parentcontextid'=>$modcontext->id)); $testblocks[] = $bi->id; } } // Make sure all contexts were created properly $count = 1; //system $count += $DB->count_records('user', array('deleted'=>0)); $count += $DB->count_records('course_categories'); $count += $DB->count_records('course'); $count += $DB->count_records('course_modules'); $count += $DB->count_records('block_instances'); $this->assertEquals($DB->count_records('context'), $count); $this->assertEquals($DB->count_records('context', array('depth'=>0)), 0); $this->assertEquals($DB->count_records('context', array('path'=>NULL)), 0); // ====== context_helper::get_level_name() ================================ $levels = context_helper::get_all_levels(); foreach ($levels as $level=>$classname) { $name = context_helper::get_level_name($level); $this->assertFalse(empty($name)); } // ======= context::instance_by_id(), context_xxx::instance(); $context = context::instance_by_id($frontpagecontext->id); $this->assertSame($context->contextlevel, CONTEXT_COURSE); $this->assertFalse(context::instance_by_id(-1, IGNORE_MISSING)); try { context::instance_by_id(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } $this->assertTrue(context_system::instance() instanceof context_system); $this->assertTrue(context_coursecat::instance($testcategories[0]) instanceof context_coursecat); $this->assertTrue(context_course::instance($testcourses[0]) instanceof context_course); $this->assertTrue(context_module::instance($testpages[0]) instanceof context_module); $this->assertTrue(context_block::instance($testblocks[0]) instanceof context_block); $this->assertFalse(context_coursecat::instance(-1, IGNORE_MISSING)); $this->assertFalse(context_course::instance(-1, IGNORE_MISSING)); $this->assertFalse(context_module::instance(-1, IGNORE_MISSING)); $this->assertFalse(context_block::instance(-1, IGNORE_MISSING)); try { context_coursecat::instance(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } try { context_course::instance(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } try { context_module::instance(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } try { context_block::instance(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } // ======= $context->get_url(), $context->get_context_name(), $context->get_capabilities() ========= $testcontexts = array(); $testcontexts[CONTEXT_SYSTEM] = context_system::instance(); $testcontexts[CONTEXT_COURSECAT] = context_coursecat::instance($testcategories[0]); $testcontexts[CONTEXT_COURSE] = context_course::instance($testcourses[0]); $testcontexts[CONTEXT_MODULE] = context_module::instance($testpages[0]); $testcontexts[CONTEXT_BLOCK] = context_block::instance($testblocks[0]); foreach ($testcontexts as $context) { $name = $context->get_context_name(true, true); $this->assertFalse(empty($name)); $this->assertTrue($context->get_url() instanceof moodle_url); $caps = $context->get_capabilities(); $this->assertTrue(is_array($caps)); foreach ($caps as $cap) { $cap = (array)$cap; $this->assertSame(array_keys($cap), array('id', 'name', 'captype', 'contextlevel', 'component', 'riskbitmask')); } } unset($testcontexts); // ===== $context->get_course_context() ========================================= $this->assertFalse($systemcontext->get_course_context(false)); try { $systemcontext->get_course_context(); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } $context = context_coursecat::instance($testcategories[0]); $this->assertFalse($context->get_course_context(false)); try { $context->get_course_context(); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } $this->assertSame($frontpagecontext->get_course_context(true), $frontpagecontext); $this->assertSame($frontpagepagecontext->get_course_context(true), $frontpagecontext); $this->assertSame($frontpagepageblockcontext->get_course_context(true), $frontpagecontext); // ======= $context->get_parent_context(), $context->get_parent_contexts(), $context->get_parent_context_ids() ======= $userid = reset($testusers); $usercontext = context_user::instance($userid); $this->assertSame($usercontext->get_parent_context(), $systemcontext); $this->assertSame($usercontext->get_parent_contexts(), array($systemcontext->id=>$systemcontext)); $this->assertSame($usercontext->get_parent_contexts(true), array($usercontext->id=>$usercontext, $systemcontext->id=>$systemcontext)); $this->assertSame($systemcontext->get_parent_contexts(), array()); $this->assertSame($systemcontext->get_parent_contexts(true), array($systemcontext->id=>$systemcontext)); $this->assertSame($systemcontext->get_parent_context_ids(), array()); $this->assertSame($systemcontext->get_parent_context_ids(true), array($systemcontext->id)); $this->assertSame($frontpagecontext->get_parent_context(), $systemcontext); $this->assertSame($frontpagecontext->get_parent_contexts(), array($systemcontext->id=>$systemcontext)); $this->assertSame($frontpagecontext->get_parent_contexts(true), array($frontpagecontext->id=>$frontpagecontext, $systemcontext->id=>$systemcontext)); $this->assertSame($frontpagecontext->get_parent_context_ids(), array($systemcontext->id)); $this->assertEquals($frontpagecontext->get_parent_context_ids(true), array($frontpagecontext->id, $systemcontext->id)); $this->assertSame($systemcontext->get_parent_context(), false); $frontpagecontext = context_course::instance($SITE->id); $parent = $systemcontext; foreach ($testcategories as $catid) { $catcontext = context_coursecat::instance($catid); $this->assertSame($catcontext->get_parent_context(), $parent); $parent = $catcontext; } $this->assertSame($frontpagepagecontext->get_parent_context(), $frontpagecontext); $this->assertSame($frontpageblockcontext->get_parent_context(), $frontpagecontext); $this->assertSame($frontpagepageblockcontext->get_parent_context(), $frontpagepagecontext); // ====== $context->get_child_contexts() ================================ $CFG->debug = 0; $children = $systemcontext->get_child_contexts(); $CFG->debug = DEBUG_DEVELOPER; $this->assertEquals(count($children)+1, $DB->count_records('context')); $context = context_coursecat::instance($testcategories[3]); $children = $context->get_child_contexts(); $countcats = 0; $countcourses = 0; $countblocks = 0; foreach ($children as $child) { if ($child->contextlevel == CONTEXT_COURSECAT) { $countcats++; } if ($child->contextlevel == CONTEXT_COURSE) { $countcourses++; } if ($child->contextlevel == CONTEXT_BLOCK) { $countblocks++; } } $this->assertEquals(count($children), 8); $this->assertEquals($countcats, 1); $this->assertEquals($countcourses, 6); $this->assertEquals($countblocks, 1); $context = context_course::instance($testcourses[2]); $children = $context->get_child_contexts(); $this->assertEquals(count($children), 7); // depends on number of default blocks $context = context_module::instance($testpages[3]); $children = $context->get_child_contexts(); $this->assertEquals(count($children), 1); $context = context_block::instance($testblocks[1]); $children = $context->get_child_contexts(); $this->assertEquals(count($children), 0); unset($children); unset($countcats); unset($countcourses); unset($countblocks); // ======= context_helper::reset_caches() ============================ context_helper::reset_caches(); $this->assertEquals(context_inspection::test_context_cache_size(), 0); context_course::instance($SITE->id); $this->assertEquals(context_inspection::test_context_cache_size(), 1); // ======= context preloading ======================================== context_helper::reset_caches(); $sql = "SELECT ".context_helper::get_preload_record_columns_sql('c')." FROM {context} c WHERE c.contextlevel <> ".CONTEXT_SYSTEM; $records = $DB->get_records_sql($sql); $firstrecord = reset($records); $columns = context_helper::get_preload_record_columns('c'); $firstrecord = (array)$firstrecord; $this->assertSame(array_keys($firstrecord), array_values($columns)); context_helper::reset_caches(); foreach ($records as $record) { context_helper::preload_from_record($record); $this->assertEquals($record, new stdClass()); } $this->assertEquals(context_inspection::test_context_cache_size(), count($records)); unset($records); unset($columns); context_helper::reset_caches(); context_helper::preload_course($SITE->id); $this->assertEquals(7, context_inspection::test_context_cache_size()); // depends on number of default blocks // ====== assign_capability(), unassign_capability() ==================== $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups')); $this->assertFalse($rc); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext->id); $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups')); $this->assertEquals($rc->permission, CAP_ALLOW); assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext->id); $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups')); $this->assertEquals($rc->permission, CAP_ALLOW); assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext, true); $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups')); $this->assertEquals($rc->permission, CAP_PREVENT); assign_capability('moodle/site:accessallgroups', CAP_INHERIT, $allroles['teacher'], $frontpagecontext); $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups')); $this->assertFalse($rc); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext); unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext, true); $rc = $DB->get_record('role_capabilities', array('contextid'=>$frontpagecontext->id, 'roleid'=>$allroles['teacher'], 'capability'=>'moodle/site:accessallgroups')); $this->assertFalse($rc); unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext->id, true); unset($rc); accesslib_clear_all_caches(false); // must be done after assign_capability() // ======= role_assign(), role_unassign(), role_unassign_all() ============== $context = context_course::instance($testcourses[1]); $this->assertEquals($DB->count_records('role_assignments', array('contextid'=>$context->id)), 0); role_assign($allroles['teacher'], $testusers[1], $context->id); role_assign($allroles['teacher'], $testusers[2], $context->id); role_assign($allroles['manager'], $testusers[1], $context->id); $this->assertEquals($DB->count_records('role_assignments', array('contextid'=>$context->id)), 3); role_unassign($allroles['teacher'], $testusers[1], $context->id); $this->assertEquals($DB->count_records('role_assignments', array('contextid'=>$context->id)), 2); role_unassign_all(array('contextid'=>$context->id)); $this->assertEquals($DB->count_records('role_assignments', array('contextid'=>$context->id)), 0); unset($context); accesslib_clear_all_caches(false); // just in case // ====== has_capability(), get_users_by_capability(), role_switch(), reload_all_capabilities() and friends ======================== $adminid = get_admin()->id; $guestid = $CFG->siteguest; // Enrol some users into some courses $course1 = $DB->get_record('course', array('id'=>$testcourses[22]), '*', MUST_EXIST); $course2 = $DB->get_record('course', array('id'=>$testcourses[7]), '*', MUST_EXIST); $cms = $DB->get_records('course_modules', array('course'=>$course1->id), 'id'); $cm1 = reset($cms); $blocks = $DB->get_records('block_instances', array('parentcontextid'=>context_module::instance($cm1->id)->id), 'id'); $block1 = reset($blocks); $instance1 = $DB->get_record('enrol', array('enrol'=>'manual', 'courseid'=>$course1->id)); $instance2 = $DB->get_record('enrol', array('enrol'=>'manual', 'courseid'=>$course2->id)); for($i=0; $i<9; $i++) { $manualenrol->enrol_user($instance1, $testusers[$i], $allroles['student']); } $manualenrol->enrol_user($instance1, $testusers[8], $allroles['teacher']); $manualenrol->enrol_user($instance1, $testusers[9], $allroles['editingteacher']); for($i=10; $i<15; $i++) { $manualenrol->enrol_user($instance2, $testusers[$i], $allroles['student']); } $manualenrol->enrol_user($instance2, $testusers[15], $allroles['editingteacher']); // Add tons of role assignments - the more the better role_assign($allroles['coursecreator'], $testusers[11], context_coursecat::instance($testcategories[2])); role_assign($allroles['manager'], $testusers[12], context_coursecat::instance($testcategories[1])); role_assign($allroles['student'], $testusers[9], context_module::instance($cm1->id)); role_assign($allroles['teacher'], $testusers[8], context_module::instance($cm1->id)); role_assign($allroles['guest'], $testusers[13], context_course::instance($course1->id)); role_assign($allroles['teacher'], $testusers[7], context_block::instance($block1->id)); role_assign($allroles['manager'], $testusers[9], context_block::instance($block1->id)); role_assign($allroles['editingteacher'], $testusers[9], context_course::instance($course1->id)); role_assign($allroles['teacher'], $adminid, context_course::instance($course1->id)); role_assign($allroles['editingteacher'], $adminid, context_block::instance($block1->id)); // Add tons of overrides - the more the better assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultuserroleid, $frontpageblockcontext, true); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpageblockcontext, true); assign_capability('moodle/block:view', CAP_PROHIBIT, $allroles['guest'], $frontpageblockcontext, true); assign_capability('block/online_users:viewlist', CAP_PREVENT, $allroles['user'], $frontpageblockcontext, true); assign_capability('block/online_users:viewlist', CAP_PREVENT, $allroles['student'], $frontpageblockcontext, true); assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $CFG->defaultuserroleid, $frontpagepagecontext, true); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagepagecontext, true); assign_capability('mod/page:view', CAP_PREVENT, $allroles['guest'], $frontpagepagecontext, true); assign_capability('mod/page:view', CAP_ALLOW, $allroles['user'], $frontpagepagecontext, true); assign_capability('moodle/page:view', CAP_ALLOW, $allroles['student'], $frontpagepagecontext, true); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultuserroleid, $frontpagecontext, true); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagecontext, true); assign_capability('mod/page:view', CAP_ALLOW, $allroles['guest'], $frontpagecontext, true); assign_capability('mod/page:view', CAP_PROHIBIT, $allroles['user'], $frontpagecontext, true); assign_capability('mod/page:view', CAP_PREVENT, $allroles['guest'], $systemcontext, true); accesslib_clear_all_caches(false); // must be done after assign_capability() // Extra tests for guests and not-logged-in users because they can not be verified by cross checking // with get_users_by_capability() where they are ignored $this->assertFalse(has_capability('moodle/block:view', $frontpageblockcontext, $guestid)); $this->assertFalse(has_capability('mod/page:view', $frontpagepagecontext, $guestid)); $this->assertTrue(has_capability('mod/page:view', $frontpagecontext, $guestid)); $this->assertFalse(has_capability('mod/page:view', $systemcontext, $guestid)); $this->assertFalse(has_capability('moodle/block:view', $frontpageblockcontext, 0)); $this->assertFalse(has_capability('mod/page:view', $frontpagepagecontext, 0)); $this->assertTrue(has_capability('mod/page:view', $frontpagecontext, 0)); $this->assertFalse(has_capability('mod/page:view', $systemcontext, 0)); $this->assertFalse(has_capability('moodle/course:create', $systemcontext, $testusers[11])); $this->assertTrue(has_capability('moodle/course:create', context_coursecat::instance($testcategories[2]), $testusers[11])); $this->assertFalse(has_capability('moodle/course:create', context_course::instance($testcourses[1]), $testusers[11])); $this->assertTrue(has_capability('moodle/course:create', context_course::instance($testcourses[19]), $testusers[11])); $this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[1]), $testusers[9])); $this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[19]), $testusers[9])); $this->assertFalse(has_capability('moodle/course:update', $systemcontext, $testusers[9])); // Test the list of enrolled users $coursecontext = context_course::instance($course1->id); $enrolled = get_enrolled_users($coursecontext); $this->assertEquals(count($enrolled), 10); for($i=0; $i<10; $i++) { $this->assertTrue(isset($enrolled[$testusers[$i]])); } $enrolled = get_enrolled_users($coursecontext, 'moodle/course:update'); $this->assertEquals(count($enrolled), 1); $this->assertTrue(isset($enrolled[$testusers[9]])); unset($enrolled); // role switching $userid = $testusers[9]; $USER = $DB->get_record('user', array('id'=>$userid)); load_all_capabilities(); $coursecontext = context_course::instance($course1->id); $this->assertTrue(has_capability('moodle/course:update', $coursecontext)); $this->assertFalse(is_role_switched($course1->id)); role_switch($allroles['student'], $coursecontext); $this->assertTrue(is_role_switched($course1->id)); $this->assertEquals($USER->access['rsw'][$coursecontext->path], $allroles['student']); $this->assertFalse(has_capability('moodle/course:update', $coursecontext)); reload_all_capabilities(); $this->assertFalse(has_capability('moodle/course:update', $coursecontext)); role_switch(0, $coursecontext); $this->assertTrue(has_capability('moodle/course:update', $coursecontext)); $userid = $adminid; $USER = $DB->get_record('user', array('id'=>$userid)); load_all_capabilities(); $coursecontext = context_course::instance($course1->id); $blockcontext = context_block::instance($block1->id); $this->assertTrue(has_capability('moodle/course:update', $blockcontext)); role_switch($allroles['student'], $coursecontext); $this->assertEquals($USER->access['rsw'][$coursecontext->path], $allroles['student']); $this->assertFalse(has_capability('moodle/course:update', $blockcontext)); reload_all_capabilities(); $this->assertFalse(has_capability('moodle/course:update', $blockcontext)); load_all_capabilities(); $this->assertTrue(has_capability('moodle/course:update', $blockcontext)); // temp course role for enrol $DB->delete_records('cache_flags', array()); // this prevents problem with dirty contexts immediately resetting the temp role - this is a known problem... $userid = $testusers[5]; $roleid = $allroles['editingteacher']; $USER = $DB->get_record('user', array('id'=>$userid)); load_all_capabilities(); $coursecontext = context_course::instance($course1->id); $this->assertFalse(has_capability('moodle/course:update', $coursecontext)); $this->assertFalse(isset($USER->access['ra'][$coursecontext->path][$roleid])); load_temp_course_role($coursecontext, $roleid); $this->assertEquals($USER->access['ra'][$coursecontext->path][$roleid], $roleid); $this->assertTrue(has_capability('moodle/course:update', $coursecontext)); remove_temp_course_roles($coursecontext); $this->assertFalse(has_capability('moodle/course:update', $coursecontext, $userid)); load_temp_course_role($coursecontext, $roleid); reload_all_capabilities(); $this->assertFalse(has_capability('moodle/course:update', $coursecontext, $userid)); $USER = new stdClass(); $USER->id = 0; // Now cross check has_capability() with get_users_by_capability(), each using different code paths, // they have to be kept in sync, usually only one of them breaks, so we know when something is wrong, // at the same time validate extra restrictions (guest read only no risks, admin exception, non existent and deleted users) $contexts = $DB->get_records('context', array(), 'id'); $contexts = array_values($contexts); $capabilities = $DB->get_records('capabilities', array(), 'id'); $capabilities = array_values($capabilities); $roles = array($allroles['guest'], $allroles['user'], $allroles['teacher'], $allroles['editingteacher'], $allroles['coursecreator'], $allroles['manager']); $userids = array_values($testusers); $userids[] = get_admin()->id; if (!PHPUNIT_LONGTEST) { $contexts = array_slice($contexts, 0, 10); $capabilities = array_slice($capabilities, 0, 5); $userids = array_slice($userids, 0, 5); } // Random time! //srand(666); foreach($userids as $userid) { // no guest or deleted // each user gets 0-10 random roles $rcount = rand(0, 10); for($j=0; $j<$rcount; $j++) { $roleid = $roles[rand(0, count($roles)-1)]; $contextid = $contexts[rand(0, count($contexts)-1)]->id; role_assign($roleid, $userid, $contextid); } } $permissions = array(CAP_ALLOW, CAP_PREVENT, CAP_INHERIT, CAP_PREVENT); $maxoverrides = count($contexts)*10; for($j=0; $j<$maxoverrides; $j++) { $roleid = $roles[rand(0, count($roles)-1)]; $contextid = $contexts[rand(0, count($contexts)-1)]->id; $permission = $permissions[rand(0,count($permissions)-1)]; $capname = $capabilities[rand(0, count($capabilities)-1)]->name; assign_capability($capname, $permission, $roleid, $contextid, true); } unset($permissions); unset($roles); accesslib_clear_all_caches(false); // must be done after assign_capability() // Test time - let's set up some real user, just in case the logic for USER affects the others... $USER = $DB->get_record('user', array('id'=>$testusers[3])); load_all_capabilities(); $userids[] = $CFG->siteguest; $userids[] = 0; // not-logged-in user $userids[] = -1; // non-existent user foreach ($contexts as $crecord) { $context = context::instance_by_id($crecord->id); if ($coursecontext = $context->get_course_context(false)) { $enrolled = get_enrolled_users($context); } else { $enrolled = array(); } foreach ($capabilities as $cap) { $allowed = get_users_by_capability($context, $cap->name, 'u.id, u.username'); if ($enrolled) { $enrolledwithcap = get_enrolled_users($context, $cap->name); } else { $enrolledwithcap = array(); } foreach ($userids as $userid) { if ($userid == 0 or isguestuser($userid)) { if ($userid == 0) { $CFG->forcelogin = true; $this->assertFalse(has_capability($cap->name, $context, $userid)); unset($CFG->forcelogin); } if (($cap->captype === 'write') or ($cap->riskbitmask & (RISK_XSS | RISK_CONFIG | RISK_DATALOSS))) { $this->assertFalse(has_capability($cap->name, $context, $userid)); } $this->assertFalse(isset($allowed[$userid])); } else { if (is_siteadmin($userid)) { $this->assertTrue(has_capability($cap->name, $context, $userid, true)); } $hascap = has_capability($cap->name, $context, $userid, false); $this->assertSame($hascap, isset($allowed[$userid]), "Capability result mismatch user:$userid, context:$context->id, $cap->name, hascap: ".(int)$hascap." "); if (isset($enrolled[$userid])) { $this->assertSame(isset($allowed[$userid]), isset($enrolledwithcap[$userid]), "Enrolment with capability result mismatch user:$userid, context:$context->id, $cap->name, hascap: ".(int)$hascap." "); } } } } } // Back to nobody $USER = new stdClass(); $USER->id = 0; unset($contexts); unset($userids); unset($capabilities); // Now let's do all the remaining tests that break our carefully prepared fake site // ======= $context->mark_dirty() ======================================= $DB->delete_records('cache_flags', array()); accesslib_clear_all_caches(false); $systemcontext->mark_dirty(); $dirty = get_cache_flags('accesslib/dirtycontexts', time()-2); $this->assertTrue(isset($dirty[$systemcontext->path])); $this->assertTrue(isset($ACCESSLIB_PRIVATE->dirtycontexts[$systemcontext->path])); // ======= $context->reload_if_dirty(); ================================= $DB->delete_records('cache_flags', array()); accesslib_clear_all_caches(false); load_all_capabilities(); $context = context_course::instance($testcourses[2]); $page = $DB->get_record('page', array('course'=>$testcourses[2])); $pagecontext = context_module::instance($page->id); $context->mark_dirty(); $this->assertTrue(isset($ACCESSLIB_PRIVATE->dirtycontexts[$context->path])); $USER->access['test'] = true; $context->reload_if_dirty(); $this->assertFalse(isset($USER->access['test'])); $context->mark_dirty(); $this->assertTrue(isset($ACCESSLIB_PRIVATE->dirtycontexts[$context->path])); $USER->access['test'] = true; $pagecontext->reload_if_dirty(); $this->assertFalse(isset($USER->access['test'])); // ======= context_helper::build_all_paths() ============================ $oldcontexts = $DB->get_records('context', array(), 'id'); $DB->set_field_select('context', 'path', NULL, "contextlevel <> ".CONTEXT_SYSTEM); $DB->set_field_select('context', 'depth', 0, "contextlevel <> ".CONTEXT_SYSTEM); context_helper::build_all_paths(); $newcontexts = $DB->get_records('context', array(), 'id'); $this->assertEquals($oldcontexts, $newcontexts); unset($oldcontexts); unset($newcontexts); // ======= $context->reset_paths() ====================================== $context = context_course::instance($testcourses[2]); $children = $context->get_child_contexts(); $context->reset_paths(false); $this->assertSame($DB->get_field('context', 'path', array('id'=>$context->id)), NULL); $this->assertEquals($DB->get_field('context', 'depth', array('id'=>$context->id)), 0); foreach ($children as $child) { $this->assertSame($DB->get_field('context', 'path', array('id'=>$child->id)), NULL); $this->assertEquals($DB->get_field('context', 'depth', array('id'=>$child->id)), 0); } $this->assertEquals(count($children)+1, $DB->count_records('context', array('depth'=>0))); $this->assertEquals(count($children)+1, $DB->count_records('context', array('path'=>NULL))); $context = context_course::instance($testcourses[2]); $context->reset_paths(true); $context = context_course::instance($testcourses[2]); $this->assertEquals($DB->get_field('context', 'path', array('id'=>$context->id)), $context->path); $this->assertEquals($DB->get_field('context', 'depth', array('id'=>$context->id)), $context->depth); $this->assertEquals(0, $DB->count_records('context', array('depth'=>0))); $this->assertEquals(0, $DB->count_records('context', array('path'=>NULL))); // ====== $context->update_moved(); ====================================== accesslib_clear_all_caches(false); $DB->delete_records('cache_flags', array()); $course = $DB->get_record('course', array('id'=>$testcourses[0])); $context = context_course::instance($course->id); $oldpath = $context->path; $miscid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}"); $categorycontext = context_coursecat::instance($miscid); $course->category = $miscid; $DB->update_record('course', $course); $context->update_moved($categorycontext); $context = context_course::instance($course->id); $this->assertEquals($context->get_parent_context(), $categorycontext); $dirty = get_cache_flags('accesslib/dirtycontexts', time()-2); $this->assertTrue(isset($dirty[$oldpath])); $this->assertTrue(isset($dirty[$context->path])); // ====== $context->delete_content() ===================================== context_helper::reset_caches(); $context = context_module::instance($testpages[3]); $this->assertTrue($DB->record_exists('context', array('id'=>$context->id))); $this->assertEquals(1, $DB->count_records('block_instances', array('parentcontextid'=>$context->id))); $context->delete_content(); $this->assertTrue($DB->record_exists('context', array('id'=>$context->id))); $this->assertEquals(0, $DB->count_records('block_instances', array('parentcontextid'=>$context->id))); // ====== $context->delete() ============================= context_helper::reset_caches(); $context = context_module::instance($testpages[4]); $this->assertTrue($DB->record_exists('context', array('id'=>$context->id))); $this->assertEquals(1, $DB->count_records('block_instances', array('parentcontextid'=>$context->id))); $bi = $DB->get_record('block_instances', array('parentcontextid'=>$context->id)); $bicontext = context_block::instance($bi->id); $DB->delete_records('cache_flags', array()); $context->delete(); // should delete also linked blocks $dirty = get_cache_flags('accesslib/dirtycontexts', time()-2); $this->assertTrue(isset($dirty[$context->path])); $this->assertFalse($DB->record_exists('context', array('id'=>$context->id))); $this->assertFalse($DB->record_exists('context', array('id'=>$bicontext->id))); $this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_MODULE, 'instanceid'=>$testpages[4]))); $this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_BLOCK, 'instanceid'=>$bi->id))); $this->assertEquals(0, $DB->count_records('block_instances', array('parentcontextid'=>$context->id))); context_module::instance($testpages[4]); // ====== context_helper::delete_instance() ============================= context_helper::reset_caches(); $lastcourse = array_pop($testcourses); $this->assertTrue($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$lastcourse))); $coursecontext = context_course::instance($lastcourse); $this->assertEquals(context_inspection::test_context_cache_size(), 1); $this->assertFalse($coursecontext->instanceid == CONTEXT_COURSE); $DB->delete_records('cache_flags', array()); context_helper::delete_instance(CONTEXT_COURSE, $lastcourse); $dirty = get_cache_flags('accesslib/dirtycontexts', time()-2); $this->assertTrue(isset($dirty[$coursecontext->path])); $this->assertEquals(context_inspection::test_context_cache_size(), 0); $this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$lastcourse))); context_course::instance($lastcourse); // ======= context_helper::create_instances() ========================== $prevcount = $DB->count_records('context'); $DB->delete_records('context', array('contextlevel'=>CONTEXT_BLOCK)); context_helper::create_instances(null, true); $this->assertSame($DB->count_records('context'), $prevcount); $this->assertEquals($DB->count_records('context', array('depth'=>0)), 0); $this->assertEquals($DB->count_records('context', array('path'=>NULL)), 0); $DB->delete_records('context', array('contextlevel'=>CONTEXT_BLOCK)); $DB->delete_records('block_instances', array()); $prevcount = $DB->count_records('context'); $DB->delete_records_select('context', 'contextlevel <> '.CONTEXT_SYSTEM); context_helper::create_instances(null, true); $this->assertSame($DB->count_records('context'), $prevcount); $this->assertEquals($DB->count_records('context', array('depth'=>0)), 0); $this->assertEquals($DB->count_records('context', array('path'=>NULL)), 0); // ======= context_helper::cleanup_instances() ========================== $lastcourse = $DB->get_field_sql("SELECT MAX(id) FROM {course}"); $DB->delete_records('course', array('id'=>$lastcourse)); $lastcategory = $DB->get_field_sql("SELECT MAX(id) FROM {course_categories}"); $DB->delete_records('course_categories', array('id'=>$lastcategory)); $lastuser = $DB->get_field_sql("SELECT MAX(id) FROM {user} WHERE deleted=0"); $DB->delete_records('user', array('id'=>$lastuser)); $DB->delete_records('block_instances', array('parentcontextid'=>$frontpagepagecontext->id)); $DB->delete_records('course_modules', array('id'=>$frontpagepagecontext->instanceid)); context_helper::cleanup_instances(); $count = 1; //system $count += $DB->count_records('user', array('deleted'=>0)); $count += $DB->count_records('course_categories'); $count += $DB->count_records('course'); $count += $DB->count_records('course_modules'); $count += $DB->count_records('block_instances'); $this->assertEquals($DB->count_records('context'), $count); // ======= context cache size restrictions ============================== $testusers= array(); for ($i=0; $i<CONTEXT_CACHE_MAX_SIZE + 100; $i++) { $user = $generator->create_user(); $testusers[$i] = $user->id; } context_helper::create_instances(null, true); context_helper::reset_caches(); for ($i=0; $i<CONTEXT_CACHE_MAX_SIZE + 100; $i++) { context_user::instance($testusers[$i]); if ($i == CONTEXT_CACHE_MAX_SIZE - 1) { $this->assertEquals(context_inspection::test_context_cache_size(), CONTEXT_CACHE_MAX_SIZE); } else if ($i == CONTEXT_CACHE_MAX_SIZE) { // once the limit is reached roughly 1/3 of records should be removed from cache $this->assertEquals(context_inspection::test_context_cache_size(), (int)(CONTEXT_CACHE_MAX_SIZE * (2/3) +102)); } } // We keep the first 100 cached $prevsize = context_inspection::test_context_cache_size(); for ($i=0; $i<100; $i++) { context_user::instance($testusers[$i]); $this->assertEquals(context_inspection::test_context_cache_size(), $prevsize); } context_user::instance($testusers[102]); $this->assertEquals(context_inspection::test_context_cache_size(), $prevsize+1); unset($testusers); // ================================================================= // ======= basic test of legacy functions ========================== // ================================================================= // note: watch out, the fake site might be pretty borked already $this->assertSame(get_system_context(), context_system::instance()); foreach ($DB->get_records('context') as $contextid=>$record) { $context = context::instance_by_id($contextid); $this->assertSame(get_context_instance_by_id($contextid), $context); $this->assertSame(get_context_instance($record->contextlevel, $record->instanceid), $context); $this->assertSame(get_parent_contexts($context), $context->get_parent_context_ids()); if ($context->id == SYSCONTEXTID) { $this->assertSame(get_parent_contextid($context), false); } else { $this->assertSame(get_parent_contextid($context), $context->get_parent_context()->id); } } $CFG->debug = 0; $children = get_child_contexts($systemcontext); $CFG->debug = DEBUG_DEVELOPER; $this->assertEquals(count($children), $DB->count_records('context')-1); unset($children); $DB->delete_records('context', array('contextlevel'=>CONTEXT_BLOCK)); create_contexts(); $this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_BLOCK))); $DB->set_field('context', 'depth', 0, array('contextlevel'=>CONTEXT_BLOCK)); build_context_path(); $this->assertFalse($DB->record_exists('context', array('depth'=>0))); $lastcourse = $DB->get_field_sql("SELECT MAX(id) FROM {course}"); $DB->delete_records('course', array('id'=>$lastcourse)); $lastcategory = $DB->get_field_sql("SELECT MAX(id) FROM {course_categories}"); $DB->delete_records('course_categories', array('id'=>$lastcategory)); $lastuser = $DB->get_field_sql("SELECT MAX(id) FROM {user} WHERE deleted=0"); $DB->delete_records('user', array('id'=>$lastuser)); $DB->delete_records('block_instances', array('parentcontextid'=>$frontpagepagecontext->id)); $DB->delete_records('course_modules', array('id'=>$frontpagepagecontext->instanceid)); cleanup_contexts(); $count = 1; //system $count += $DB->count_records('user', array('deleted'=>0)); $count += $DB->count_records('course_categories'); $count += $DB->count_records('course'); $count += $DB->count_records('course_modules'); $count += $DB->count_records('block_instances'); $this->assertEquals($DB->count_records('context'), $count); context_helper::reset_caches(); preload_course_contexts($SITE->id); $this->assertEquals(context_inspection::test_context_cache_size(), 1); context_helper::reset_caches(); list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx'); $sql = "SELECT c.id $select FROM {course_categories} c $join"; $records = $DB->get_records_sql($sql); foreach ($records as $record) { context_instance_preload($record); $record = (array)$record; $this->assertEquals(1, count($record)); // only id left } $this->assertEquals(count($records), context_inspection::test_context_cache_size()); accesslib_clear_all_caches(true); $DB->delete_records('cache_flags', array()); mark_context_dirty($systemcontext->path); $dirty = get_cache_flags('accesslib/dirtycontexts', time()-2); $this->assertTrue(isset($dirty[$systemcontext->path])); accesslib_clear_all_caches(false); $DB->delete_records('cache_flags', array()); $course = $DB->get_record('course', array('id'=>$testcourses[2])); $context = get_context_instance(CONTEXT_COURSE, $course->id); $oldpath = $context->path; $miscid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}"); $categorycontext = context_coursecat::instance($miscid); $course->category = $miscid; $DB->update_record('course', $course); context_moved($context, $categorycontext); $context = get_context_instance(CONTEXT_COURSE, $course->id); $this->assertEquals($context->get_parent_context(), $categorycontext); $this->assertTrue($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$testcourses[2]))); delete_context(CONTEXT_COURSE, $testcourses[2]); $this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$testcourses[2]))); $name = get_contextlevel_name(CONTEXT_COURSE); $this->assertFalse(empty($name)); $context = get_context_instance(CONTEXT_COURSE, $testcourses[2]); $name = print_context_name($context); $this->assertFalse(empty($name)); $url = get_context_url($coursecontext); $this->assertFalse($url instanceof modole_url); $page = $DB->get_record('page', array('id'=>$testpages[7])); $context = get_context_instance(CONTEXT_MODULE, $page->id); $coursecontext = get_course_context($context); $this->assertEquals($coursecontext->contextlevel, CONTEXT_COURSE); $this->assertEquals(get_courseid_from_context($context), $page->course); $caps = fetch_context_capabilities($systemcontext); $this->assertTrue(is_array($caps)); unset($caps); }
function link_to_gdoc($name, $link, $type = null, $modtype = 'url') { global $COURSE, $DB, $CFG, $USER; require_once "{$CFG->dirroot}/mod/{$modtype}/lib.php"; //add $fromform = new stdClass(); $newform = new stdClass(); $mform = new MoodleQuickForm(null, 'POST', 'nothing'); $module = $DB->get_record("modules", array('name' => $modtype)); $course = $COURSE; $cw = get_course_section(0, $course->id); $cm = null; // fields for mdl_url $fromform->course = $course->id; $fromform->name = $name; $fromform->introformat = 0; $fromform->introeditor = 0; $fromform->externalurl = $link; /* if ($type !== 'dir') { $fromform->display = 6; $fromform->displayoptions = 'a:2:{s:10:"popupwidth";i:1024;s:11:"popupheight";i:768;}'; } else { */ $fromform->display = 0; $fromform->popupwidth = 1024; $fromform->popupheight = 768; $fromform->popupwidth = null; $fromform->popupheight = null; $fromform->displayoptions = 'a:1:{s:10:"printintro";i:0;}'; // } // fields for mdl_course_module $fromform->module = $module->id; $fromform->instance = ''; $fromform->section = 0; // The section number itself - relative!!! (section column in course_sections) $fromform->idnumber = null; $fromform->score = 0; $fromform->indent = 0; $fromform->visible = 1; $fromform->visibleold = 1; $fromform->groupmode = $course->groupmode; $fromform->groupingid = 0; $fromform->groupmembersonly = 0; $fromform->completion = 0; $fromform->completionview = 0; $fromform->completionexpected = 0; $fromform->availablefrom = 0; $fromform->availableuntil = 0; $fromform->showavailability = 0; $fromform->showdescription = 0; $fromform->conditiongradegroup = array(); $fromform->conditionfieldgroup = array(); // fields for mdl_course_sections $fromform->summaryformat = 0; $fromform->modulename = clean_param($module->name, PARAM_SAFEDIR); // For safety // $fromform->add = 'resource'; // $fromform->type = $type == 'dir' ? 'collection' : 'file'; // $fromform->return = 0; //must be false if this is an add, go back to course view on cancel // $fromform->coursemodule = ''; // $fromform->popup = 'resizable=1,scrollbars=1,directories=1,location=1,menubar=1,toolbar=1,status=1,width=1024,height=768'; // require_login($course->id); // needed to setup proper $COURSE $context = get_context_instance(CONTEXT_COURSE, $course->id); require_capability('moodle/course:manageactivities', $context); if (!empty($course->groupmodeforce) or !isset($fromform->groupmode)) { $fromform->groupmode = 0; // do not set groupmode } if (!course_allowed_module($course, $fromform->modulename)) { print_error('moduledisable', '', '', $fromform->modulename); } // first add course_module record because we need the context $newcm = new stdClass(); $newcm->course = $course->id; $newcm->module = $fromform->module; $newcm->instance = 0; // not known yet, will be updated later (this is similar to restore code) $newcm->visible = $fromform->visible; $newcm->groupmode = $fromform->groupmode; $newcm->groupingid = $fromform->groupingid; $newcm->groupmembersonly = $fromform->groupmembersonly; $completion = new completion_info($course); if ($completion->is_enabled()) { $newcm->completion = $fromform->completion; $newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber; $newcm->completionview = $fromform->completionview; $newcm->completionexpected = $fromform->completionexpected; } if (!empty($CFG->enableavailability)) { $newcm->availablefrom = $fromform->availablefrom; $newcm->availableuntil = $fromform->availableuntil; $newcm->showavailability = $fromform->showavailability; } if (isset($fromform->showdescription)) { $newcm->showdescription = $fromform->showdescription; } else { $newcm->showdescription = 0; } if (!($fromform->coursemodule = add_course_module($newcm))) { print_error('cannotaddcoursemodule'); } if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) { $draftid_editor = file_get_submitted_draft_itemid('introeditor'); file_prepare_draft_area($draftid_editor, null, null, null, null); $fromform->introeditor = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor); // TODO: add better default } if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) { $introeditor = $fromform->introeditor; unset($fromform->introeditor); $fromform->intro = $introeditor['text']; $fromform->introformat = $introeditor['format']; } $addinstancefunction = $fromform->modulename . "_add_instance"; $updateinstancefunction = $fromform->modulename . "_update_instance"; $returnfromfunc = $addinstancefunction($fromform, $mform); // $returnfromfunc = url_add_instance($fromform, $mform); if (!$returnfromfunc or !is_number($returnfromfunc)) { // undo everything we can $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); delete_context(CONTEXT_MODULE, $fromform->coursemodule); $DB->delete_records('course_modules', array('id' => $fromform->coursemodule)); if (!is_number($returnfromfunc)) { print_error('invalidfunction', '', course_get_url($course, $cw->section)); } else { print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $fromform->modulename); } } $fromform->instance = $returnfromfunc; $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $fromform->coursemodule)); // update embedded links and save files $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); if (!empty($introeditor)) { $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $fromform->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']); $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id' => $fromform->instance)); } // course_modules and course_sections each contain a reference // to each other, so we have to update one of them twice. $sectionid = add_mod_to_section($fromform); $DB->set_field('course_modules', 'section', $sectionid, array('id' => $fromform->coursemodule)); // make sure visibility is set correctly (in particular in calendar) set_coursemodule_visible($fromform->coursemodule, $fromform->visible); if (isset($fromform->cmidnumber)) { //label // set cm idnumber set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber); } // Set up conditions if ($CFG->enableavailability) { condition_info::update_cm_from_form((object) array('id' => $fromform->coursemodule), $fromform, false); } $eventname = 'mod_created'; add_to_log($course->id, "course", "add mod", "../mod/{$fromform->modulename}/view.php?id={$fromform->coursemodule}", "{$fromform->modulename} {$fromform->instance}"); add_to_log($course->id, $fromform->modulename, "add", "view.php?id={$fromform->coursemodule}", "{$fromform->instance}", $fromform->coursemodule); // Trigger mod_created/mod_updated event with information about this module. $eventdata = new stdClass(); $eventdata->modulename = $fromform->modulename; $eventdata->name = $fromform->name; $eventdata->cmid = $fromform->coursemodule; $eventdata->courseid = $course->id; $eventdata->userid = $USER->id; events_trigger($eventname, $eventdata); rebuild_course_cache($course->id); return 1; }
/** * Delete a course, including all related data from the database, * and any associated files. * * @global object * @global object * @param mixed $courseorid The id of the course or course object to delete. * @param bool $showfeedback Whether to display notifications of each action the function performs. * @return bool true if all the removals succeeded. false if there were any failures. If this * method returns false, some of the removals will probably have succeeded, and others * failed, but you have no way of knowing which. */ function delete_course($courseorid, $showfeedback = true) { global $DB; if (is_object($courseorid)) { $courseid = $courseorid->id; $course = $courseorid; } else { $courseid = $courseorid; if (!($course = $DB->get_record('course', array('id' => $courseid)))) { return false; } } $context = get_context_instance(CONTEXT_COURSE, $courseid); // frontpage course can not be deleted!! if ($courseid == SITEID) { return false; } // make the course completely empty remove_course_contents($courseid, $showfeedback); // delete the course and related context instance delete_context(CONTEXT_COURSE, $courseid); // We will update the course's timemodified, as it will be passed to the course_deleted event, // which should know about this updated property, as this event is meant to pass the full course record $course->timemodified = time(); $DB->delete_records("course", array("id" => $courseid)); //trigger events $course->context = $context; // you can not fetch context in the event because it was already deleted events_trigger('course_deleted', $course); return true; }
/** * Clear a course out completely, deleting all content * but don't delete the course itself * * @uses $CFG * @param int $courseid The id of the course that is being deleted * @param bool $showfeedback Whether to display notifications of each action the function performs. * @return bool true if all the removals succeeded. false if there were any failures. If this * method returns false, some of the removals will probably have succeeded, and others * failed, but you have no way of knowing which. */ function remove_course_contents($courseid, $showfeedback = true) { global $CFG; require_once $CFG->libdir . '/questionlib.php'; require_once $CFG->libdir . '/gradelib.php'; $result = true; if (!($course = get_record('course', 'id', $courseid))) { error('Course ID was incorrect (can\'t find it)'); } $strdeleted = get_string('deleted'); /// Clean up course formats (iterate through all formats in the even the course format was ever changed) $formats = get_list_of_plugins('course/format'); foreach ($formats as $format) { $formatdelete = $format . '_course_format_delete_course'; $formatlib = "{$CFG->dirroot}/course/format/{$format}/lib.php"; if (file_exists($formatlib)) { include_once $formatlib; if (function_exists($formatdelete)) { if ($showfeedback) { notify($strdeleted . ' ' . $format); } $formatdelete($course->id); } } } /// Delete every instance of every module if ($allmods = get_records('modules')) { foreach ($allmods as $mod) { $modname = $mod->name; $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php'; $moddelete = $modname . '_delete_instance'; // Delete everything connected to an instance $moddeletecourse = $modname . '_delete_course'; // Delete other stray stuff (uncommon) $count = 0; if (file_exists($modfile)) { include_once $modfile; if (function_exists($moddelete)) { if ($instances = get_records($modname, 'course', $course->id)) { foreach ($instances as $instance) { if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) { /// Delete activity context questions and question categories question_delete_activity($cm, $showfeedback); } if ($moddelete($instance->id)) { $count++; } else { notify('Could not delete ' . $modname . ' instance ' . $instance->id . ' (' . format_string($instance->name) . ')'); $result = false; } if ($cm) { // delete cm and its context in correct order delete_records('course_modules', 'id', $cm->id); delete_context(CONTEXT_MODULE, $cm->id); } } } } else { notify('Function ' . $moddelete . '() doesn\'t exist!'); $result = false; } if (function_exists($moddeletecourse)) { $moddeletecourse($course, $showfeedback); } } if ($showfeedback) { notify($strdeleted . ' ' . $count . ' x ' . $modname); } } } else { error('No modules are installed!'); } /// Give local code a chance to delete its references to this course. require_once $CFG->libdir . '/locallib.php'; notify_local_delete_course($courseid, $showfeedback); /// Delete course blocks if ($blocks = get_records_sql("SELECT *\n FROM {$CFG->prefix}block_instance\n WHERE pagetype = '" . PAGE_COURSE_VIEW . "'\n AND pageid = {$course->id}")) { if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) { if ($showfeedback) { notify($strdeleted . ' block_instance'); } require_once $CFG->libdir . '/blocklib.php'; 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; } } /// Delete any groups, removing members and grouping/course links first. require_once $CFG->dirroot . '/group/lib.php'; groups_delete_groupings($courseid, $showfeedback); groups_delete_groups($courseid, $showfeedback); /// Delete all related records in other tables that may have a courseid /// This array stores the tables that need to be cleared, as /// table_name => column_name that contains the course id. $tablestoclear = array('event' => 'courseid', 'log' => 'course', 'course_sections' => 'course', 'course_modules' => 'course', 'backup_courses' => 'courseid', 'user_lastaccess' => 'courseid', 'backup_log' => 'courseid'); foreach ($tablestoclear as $table => $col) { if (delete_records($table, $col, $course->id)) { if ($showfeedback) { notify($strdeleted . ' ' . $table); } } else { $result = false; } } /// Clean up metacourse stuff if ($course->metacourse) { delete_records("course_meta", "parent_course", $course->id); sync_metacourse($course->id); // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id. if ($showfeedback) { notify("{$strdeleted} course_meta"); } } else { if ($parents = get_records("course_meta", "child_course", $course->id)) { foreach ($parents as $parent) { remove_from_metacourse($parent->parent_course, $parent->child_course); // this will do the unenrolments as well. } if ($showfeedback) { notify("{$strdeleted} course_meta"); } } } /// Delete questions and question categories 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); return $result; }
/** * Clear a course out completely, deleting all content * but don't delete the course itself * * @global object * @global object * @param int $courseid The id of the course that is being deleted * @param bool $showfeedback Whether to display notifications of each action the function performs. * @return bool true if all the removals succeeded. false if there were any failures. If this * method returns false, some of the removals will probably have succeeded, and others * failed, but you have no way of knowing which. */ function remove_course_contents($courseid, $showfeedback = true) { global $CFG, $DB, $OUTPUT; require_once $CFG->libdir . '/questionlib.php'; require_once $CFG->libdir . '/gradelib.php'; $result = true; if (!($course = $DB->get_record('course', array('id' => $courseid)))) { print_error('invalidcourseid'); } $context = get_context_instance(CONTEXT_COURSE, $courseid); $strdeleted = get_string('deleted'); /// Clean up course formats (iterate through all formats in the even the course format was ever changed) $formats = get_plugin_list('format'); foreach ($formats as $format => $formatdir) { $formatdelete = $format . '_course_format_delete_course'; $formatlib = "{$formatdir}/lib.php"; if (file_exists($formatlib)) { include_once $formatlib; if (function_exists($formatdelete)) { if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' ' . $format); } $formatdelete($course->id); } } } /// Delete every instance of every module if ($allmods = $DB->get_records('modules')) { foreach ($allmods as $mod) { $modname = $mod->name; $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php'; $moddelete = $modname . '_delete_instance'; // Delete everything connected to an instance $moddeletecourse = $modname . '_delete_course'; // Delete other stray stuff (uncommon) $count = 0; if (file_exists($modfile)) { include_once $modfile; if (function_exists($moddelete)) { if ($instances = $DB->get_records($modname, array('course' => $course->id))) { foreach ($instances as $instance) { if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) { /// Delete activity context questions and question categories question_delete_activity($cm, $showfeedback); } if ($moddelete($instance->id)) { $count++; } else { echo $OUTPUT->notification('Could not delete ' . $modname . ' instance ' . $instance->id . ' (' . format_string($instance->name) . ')'); $result = false; } if ($cm) { // delete cm and its context in correct order $DB->delete_records('course_modules', array('id' => $cm->id)); delete_context(CONTEXT_MODULE, $cm->id); } } } } else { echo $OUTPUT->notification('Function ' . $moddelete . '() doesn\'t exist!'); $result = false; } if (function_exists($moddeletecourse)) { $moddeletecourse($course, $showfeedback); } } if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' ' . $count . ' x ' . $modname); } } } else { print_error('nomodules', 'debug'); } /// Delete course blocks blocks_delete_all_for_context($context->id); /// Delete any groups, removing members and grouping/course links first. require_once $CFG->dirroot . '/group/lib.php'; groups_delete_groupings($courseid, $showfeedback); groups_delete_groups($courseid, $showfeedback); /// Delete all related records in other tables that may have a courseid /// This array stores the tables that need to be cleared, as /// table_name => column_name that contains the course id. $tablestoclear = array('event' => 'courseid', 'log' => 'course', 'course_sections' => 'course', 'course_modules' => 'course', 'backup_courses' => 'courseid', 'user_lastaccess' => 'courseid', 'backup_log' => 'courseid'); foreach ($tablestoclear as $table => $col) { if ($DB->delete_records($table, array($col => $course->id))) { if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' ' . $table); } } else { $result = false; } } /// Clean up metacourse stuff if ($course->metacourse) { $DB->delete_records("course_meta", array("parent_course" => $course->id)); sync_metacourse($course->id); // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id. if ($showfeedback) { echo $OUTPUT->notification("{$strdeleted} course_meta"); } } else { if ($parents = $DB->get_records("course_meta", array("child_course" => $course->id))) { foreach ($parents as $parent) { remove_from_metacourse($parent->parent_course, $parent->child_course); // this will do the unenrolments as well. } if ($showfeedback) { echo $OUTPUT->notification("{$strdeleted} course_meta"); } } } /// Delete questions and question categories question_delete_course($course, $showfeedback); /// Remove all data from gradebook remove_course_grades($courseid, $showfeedback); remove_grade_letters($context, $showfeedback); /// Delete course tags require_once $CFG->dirroot . '/tag/coursetagslib.php'; coursetag_delete_course_tags($course->id, $showfeedback); return $result; }
/** * Add course module. * * The function does not check user capabilities. * The function creates course module, module instance, add the module to the correct section. * It also trigger common action that need to be done after adding/updating a module. * * @param object $moduleinfo the moudle data * @param object $course the course of the module * @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance() * @return object the updated module info */ function local_ltiprovider_add_moduleinfo($moduleinfo, $course, $mform = null) { global $DB, $CFG; $moduleinfo->course = $course->id; $moduleinfo = local_ltiprovider_set_moduleinfo_defaults($moduleinfo); if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) { $moduleinfo->groupmode = 0; // Do not set groupmode. } if (!course_allowed_module($course, $moduleinfo->modulename)) { print_error('moduledisable', '', '', $moduleinfo->modulename); } // First add course_module record because we need the context. $newcm = new stdClass(); $newcm->course = $course->id; $newcm->module = $moduleinfo->module; $newcm->instance = 0; // Not known yet, will be updated later (this is similar to restore code). $newcm->visible = $moduleinfo->visible; $newcm->visibleold = $moduleinfo->visible; $newcm->groupmode = $moduleinfo->groupmode; $newcm->groupingid = $moduleinfo->groupingid; $newcm->groupmembersonly = $moduleinfo->groupmembersonly; $completion = new completion_info($course); if ($completion->is_enabled()) { $newcm->completion = $moduleinfo->completion; $newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber; $newcm->completionview = $moduleinfo->completionview; $newcm->completionexpected = $moduleinfo->completionexpected; } if (!empty($CFG->enableavailability)) { $newcm->availablefrom = $moduleinfo->availablefrom; $newcm->availableuntil = $moduleinfo->availableuntil; $newcm->showavailability = $moduleinfo->showavailability; } if (isset($moduleinfo->showdescription)) { $newcm->showdescription = $moduleinfo->showdescription; } else { $newcm->showdescription = 0; } if (!($moduleinfo->coursemodule = add_course_module($newcm))) { print_error('cannotaddcoursemodule'); } if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) { $introeditor = $moduleinfo->introeditor; unset($moduleinfo->introeditor); $moduleinfo->intro = $introeditor['text']; $moduleinfo->introformat = $introeditor['format']; } $addinstancefunction = $moduleinfo->modulename . "_add_instance"; $returnfromfunc = $addinstancefunction($moduleinfo, $mform); if (!$returnfromfunc or !is_number($returnfromfunc)) { // Undo everything we can. $modcontext = context_module::instance($moduleinfo->coursemodule); delete_context(CONTEXT_MODULE, $moduleinfo->coursemodule); $DB->delete_records('course_modules', array('id' => $moduleinfo->coursemodule)); if (!is_number($returnfromfunc)) { print_error('invalidfunction', '', course_get_url($course, $cw->section)); } else { print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $moduleinfo->modulename); } } $moduleinfo->instance = $returnfromfunc; $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $moduleinfo->coursemodule)); // Update embedded links and save files. $modcontext = context_module::instance($moduleinfo->coursemodule); if (!empty($introeditor)) { $moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']); $DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id' => $moduleinfo->instance)); } // Course_modules and course_sections each contain a reference to each other. // So we have to update one of them twice. $sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section); // Make sure visibility is set correctly (in particular in calendar). // Note: allow them to set it even without moodle/course:activityvisibility. set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible); if (isset($moduleinfo->cmidnumber)) { // Label. // Set cm idnumber - uniqueness is already verified by form validation. set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber); } // Set up conditions. if ($CFG->enableavailability) { condition_info::update_cm_from_form((object) array('id' => $moduleinfo->coursemodule), $moduleinfo, false); } $eventname = 'mod_created'; add_to_log($course->id, "course", "add mod", "../mod/{$moduleinfo->modulename}/view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->modulename} {$moduleinfo->instance}"); add_to_log($course->id, $moduleinfo->modulename, "add", "view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->instance}", $moduleinfo->coursemodule); $moduleinfo = local_ltiprovider_edit_module_post_actions($moduleinfo, $course, 'mod_created'); return $moduleinfo; }
/** * A small functional test of accesslib functions and classes. */ public function test_everything_in_accesslib() { global $USER, $SITE, $CFG, $DB, $ACCESSLIB_PRIVATE; // First of all finalize the session, we must not carry over any of this mess to the next page via SESSION!!! session_get_instance()->write_close(); // hack - this is going to take very long time set_time_limit(3600); // Get rid of current user that would not work anyway, // do NOT even think about using $this->switch_global_user_id()! if (!isset($this->accesslibprevuser)) { $this->accesslibprevuser = clone $USER; $USER = new stdClass(); $USER->id = 0; } // Backup $SITE global if (!isset($this->accesslibprevsite)) { $this->accesslibprevsite = $SITE; } // Switch DB, if it somehow fails or you specified wrong unittest prefix it will nuke real data, you have been warned! $this->switch_to_test_db(); // Let's switch the CFG $prevcfg = clone $CFG; $this->switch_to_test_cfg(); $CFG->config_php_settings = $prevcfg->config_php_settings; $CFG->noemailever = true; $CFG->admin = $prevcfg->admin; $CFG->lang = 'en'; $CFG->tempdir = $prevcfg->tempdir; $CFG->cachedir = $prevcfg->cachedir; $CFG->directorypermissions = $prevcfg->directorypermissions; $CFG->filepermissions = $prevcfg->filepermissions; // Reset all caches accesslib_clear_all_caches_for_unit_testing(); // Add some core tables - this is known to break constantly because we keep adding dependencies... $tablenames = array('config', 'config_plugins', 'modules', 'course', 'course_modules', 'course_sections', 'course_categories', 'mnet_host', 'mnet_application', 'capabilities', 'context', 'context_temp', 'role', 'role_capabilities', 'role_allow_switch', 'license', 'my_pages', 'block', 'block_instances', 'block_positions', 'role_allow_assign', 'role_allow_override', 'role_assignments', 'role_context_levels', 'enrol', 'user_enrolments', 'filter_active', 'filter_config', 'comments', 'user', 'groups_members', 'cache_flags', 'events_handlers', 'user_lastaccess', 'rating', 'files', 'role_names', 'user_preferences'); $this->create_test_tables($tablenames, 'lib'); // Create all core default records and default settings require_once "{$CFG->libdir}/db/install.php"; xmldb_main_install(); // installs the capabilities too // Fake mod_page install $tablenames = array('page'); $this->create_test_tables($tablenames, 'mod/page'); $module = new stdClass(); require $CFG->dirroot . '/mod/page/version.php'; $module->name = 'page'; $pagemoduleid = $DB->insert_record('modules', $module); update_capabilities('mod_page'); // Fake block_online_users install $plugin = new stdClass(); $plugin->version = NULL; $plugin->cron = 0; include $CFG->dirroot . '/blocks/online_users/version.php'; $plugin->name = 'online_users'; $onlineusersblockid = $DB->insert_record('block', $plugin); update_capabilities('block_online_users'); // Finish roles setup set_config('defaultfrontpageroleid', $DB->get_field('role', 'id', array('archetype' => 'frontpage'))); set_config('defaultuserroleid', $DB->get_field('role', 'id', array('archetype' => 'user'))); set_config('notloggedinroleid', $DB->get_field('role', 'id', array('archetype' => 'guest'))); set_config('rolesactive', 1); // Init manual enrol set_config('status', ENROL_INSTANCE_ENABLED, 'enrol_manual'); set_config('defaultperiod', 0, 'enrol_manual'); $manualenrol = enrol_get_plugin('manual'); // Fill the site with some real data $testcategories = array(); $testcourses = array(); $testpages = array(); $testblocks = array(); $allroles = $DB->get_records_menu('role', array(), 'id', 'archetype, id'); $systemcontext = context_system::instance(); $frontpagecontext = context_course::instance(SITEID); // Add block to system context $bi = new stdClass(); $bi->blockname = 'online_users'; $bi->parentcontextid = $systemcontext->id; $bi->showinsubcontexts = 1; $bi->pagetypepattern = ''; $bi->subpagepattern = ''; $bi->defaultregion = ''; $bi->defaultweight = 0; $bi->configdata = ''; $biid = $DB->insert_record('block_instances', $bi); context_block::instance($biid); $testblocks[] = $biid; // Some users $testusers = array(); for ($i = 0; $i < 20; $i++) { $user = new stdClass(); $user->auth = 'manual'; $user->firstname = 'user' . $i; $user->lastname = 'user' . $i; $user->username = '******' . $i; $user->password = '******'; $user->email = "user{$i}@example.com"; $user->confirmed = 1; $user->mnethostid = $CFG->mnet_localhost_id; $user->lang = $CFG->lang; $user->maildisplay = 1; $user->timemodified = time(); $user->deleted = 0; $user->lastip = '0.0.0.0'; $userid = $DB->insert_record('user', $user); $testusers[$i] = $userid; $usercontext = context_user::instance($userid); // Add block to user profile $bi->parentcontextid = $usercontext->id; $biid = $DB->insert_record('block_instances', $bi); context_block::instance($biid); $testblocks[] = $biid; } // Deleted user - should be ignored everywhere, can not have context $user = new stdClass(); $user->auth = 'manual'; $user->firstname = ''; $user->lastname = ''; $user->username = '******'; $user->password = ''; $user->email = ''; $user->confirmed = 1; $user->mnethostid = $CFG->mnet_localhost_id; $user->lang = $CFG->lang; $user->maildisplay = 1; $user->timemodified = time(); $user->deleted = 1; $user->lastip = '0.0.0.0'; $DB->insert_record('user', $user); unset($user); // Add block to frontpage $bi->parentcontextid = $frontpagecontext->id; $biid = $DB->insert_record('block_instances', $bi); $frontpageblockcontext = context_block::instance($biid); $testblocks[] = $biid; // Add a resource to frontpage $page = new stdClass(); $page->course = $SITE->id; $page->intro = '...'; $page->introformat = FORMAT_HTML; $page->content = '...'; $page->contentformat = FORMAT_HTML; $pageid = $DB->insert_record('page', $page); $testpages[] = $pageid; $cm = new stdClass(); $cm->course = $SITE->id; $cm->module = $pagemoduleid; $cm->section = 1; $cm->instance = $pageid; $cmid = $DB->insert_record('course_modules', $cm); $frontpagepagecontext = context_module::instance($cmid); // Add block to frontpage resource $bi->parentcontextid = $frontpagepagecontext->id; $biid = $DB->insert_record('block_instances', $bi); $frontpagepageblockcontext = context_block::instance($biid); $testblocks[] = $biid; // Some nested course categories with courses require_once "{$CFG->dirroot}/course/lib.php"; $path = ''; $parentcat = 0; for ($i = 0; $i < 5; $i++) { $cat = new stdClass(); $cat->name = 'category' . $i; $cat->parent = $parentcat; $cat->depth = $i + 1; $cat->sortorder = MAX_COURSES_IN_CATEGORY * ($i + 2); $cat->timemodified = time(); $catid = $DB->insert_record('course_categories', $cat); $path = $path . '/' . $catid; $DB->set_field('course_categories', 'path', $path, array('id' => $catid)); $parentcat = $catid; $testcategories[] = $catid; $catcontext = context_coursecat::instance($catid); if ($i >= 4) { continue; } // Add resource to each category $bi->parentcontextid = $catcontext->id; $biid = $DB->insert_record('block_instances', $bi); context_block::instance($biid); // Add a few courses to each category for ($j = 0; $j < 6; $j++) { $course = new stdClass(); $course->fullname = 'course' . $j; $course->shortname = 'c' . $j; $course->summary = 'bah bah bah'; $course->newsitems = 0; $course->numsections = 1; $course->category = $catid; $course->format = 'topics'; $course->timecreated = time(); $course->visible = 1; $course->timemodified = $course->timecreated; $courseid = $DB->insert_record('course', $course); $section = new stdClass(); $section->course = $courseid; $section->section = 0; $section->summaryformat = FORMAT_HTML; $DB->insert_record('course_sections', $section); $section->section = 1; $DB->insert_record('course_sections', $section); $testcourses[] = $courseid; $coursecontext = context_course::instance($courseid); if ($j >= 5) { continue; } // Add manual enrol instance $manualenrol->add_default_instance($DB->get_record('course', array('id' => $courseid))); // Add block to each course $bi->parentcontextid = $coursecontext->id; $biid = $DB->insert_record('block_instances', $bi); context_block::instance($biid); $testblocks[] = $biid; // Add a resource to each course $page->course = $courseid; $pageid = $DB->insert_record('page', $page); $testpages[] = $pageid; $cm->course = $courseid; $cm->instance = $pageid; $cm->id = $DB->insert_record('course_modules', $cm); $modcontext = context_module::instance($cm->id); // Add block to each module $bi->parentcontextid = $modcontext->id; $biid = $DB->insert_record('block_instances', $bi); context_block::instance($biid); $testblocks[] = $biid; } } // Make sure all contexts were created properly $count = 1; //system $count += $DB->count_records('user', array('deleted' => 0)); $count += $DB->count_records('course_categories'); $count += $DB->count_records('course'); $count += $DB->count_records('course_modules'); $count += $DB->count_records('block_instances'); $this->assertEqual($DB->count_records('context'), $count); $this->assertEqual($DB->count_records('context', array('depth' => 0)), 0); $this->assertEqual($DB->count_records('context', array('path' => NULL)), 0); // ====== context_helper::get_level_name() ================================ $levels = context_helper::get_all_levels(); foreach ($levels as $level => $classname) { $name = context_helper::get_level_name($level); $this->assertFalse(empty($name)); } // ======= context::instance_by_id(), context_xxx::instance(); $context = context::instance_by_id($frontpagecontext->id); $this->assertidentical($context->contextlevel, CONTEXT_COURSE); $this->assertFalse(context::instance_by_id(-1, IGNORE_MISSING)); try { context::instance_by_id(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } $this->assertTrue(context_system::instance() instanceof context_system); $this->assertTrue(context_coursecat::instance($testcategories[0]) instanceof context_coursecat); $this->assertTrue(context_course::instance($testcourses[0]) instanceof context_course); $this->assertTrue(context_module::instance($testpages[0]) instanceof context_module); $this->assertTrue(context_block::instance($testblocks[0]) instanceof context_block); $this->assertFalse(context_coursecat::instance(-1, IGNORE_MISSING)); $this->assertFalse(context_course::instance(-1, IGNORE_MISSING)); $this->assertFalse(context_module::instance(-1, IGNORE_MISSING)); $this->assertFalse(context_block::instance(-1, IGNORE_MISSING)); try { context_coursecat::instance(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } try { context_course::instance(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } try { context_module::instance(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } try { context_block::instance(-1); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } // ======= $context->get_url(), $context->get_context_name(), $context->get_capabilities() ========= $testcontexts = array(); $testcontexts[CONTEXT_SYSTEM] = context_system::instance(); $testcontexts[CONTEXT_COURSECAT] = context_coursecat::instance($testcategories[0]); $testcontexts[CONTEXT_COURSE] = context_course::instance($testcourses[0]); $testcontexts[CONTEXT_MODULE] = context_module::instance($testpages[0]); $testcontexts[CONTEXT_BLOCK] = context_block::instance($testblocks[0]); foreach ($testcontexts as $context) { $name = $context->get_context_name(true, true); $this->assertFalse(empty($name)); $this->assertTrue($context->get_url() instanceof moodle_url); $caps = $context->get_capabilities(); $this->assertTrue(is_array($caps)); foreach ($caps as $cap) { $cap = (array) $cap; $this->assertIdentical(array_keys($cap), array('id', 'name', 'captype', 'contextlevel', 'component', 'riskbitmask')); } } unset($testcontexts); // ===== $context->get_course_context() ========================================= $this->assertFalse($systemcontext->get_course_context(false)); try { $systemcontext->get_course_context(); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } $context = context_coursecat::instance($testcategories[0]); $this->assertFalse($context->get_course_context(false)); try { $context->get_course_context(); $this->fail('exception expected'); } catch (Exception $e) { $this->assertTrue(true); } $this->assertIdentical($frontpagecontext->get_course_context(true), $frontpagecontext); $this->assertIdentical($frontpagepagecontext->get_course_context(true), $frontpagecontext); $this->assertIdentical($frontpagepageblockcontext->get_course_context(true), $frontpagecontext); // ======= $context->get_parent_context(), $context->get_parent_contexts(), $context->get_parent_context_ids() ======= $userid = reset($testusers); $usercontext = context_user::instance($userid); $this->assertIdentical($usercontext->get_parent_context(), $systemcontext); $this->assertIdentical($usercontext->get_parent_contexts(), array($systemcontext->id => $systemcontext)); $this->assertIdentical($usercontext->get_parent_contexts(true), array($usercontext->id => $usercontext, $systemcontext->id => $systemcontext)); $this->assertIdentical($systemcontext->get_parent_contexts(), array()); $this->assertIdentical($systemcontext->get_parent_contexts(true), array($systemcontext->id => $systemcontext)); $this->assertIdentical($systemcontext->get_parent_context_ids(), array()); $this->assertIdentical($systemcontext->get_parent_context_ids(true), array($systemcontext->id)); $this->assertIdentical($frontpagecontext->get_parent_context(), $systemcontext); $this->assertIdentical($frontpagecontext->get_parent_contexts(), array($systemcontext->id => $systemcontext)); $this->assertIdentical($frontpagecontext->get_parent_contexts(true), array($frontpagecontext->id => $frontpagecontext, $systemcontext->id => $systemcontext)); $this->assertIdentical($frontpagecontext->get_parent_context_ids(), array($systemcontext->id)); $this->assertEqual($frontpagecontext->get_parent_context_ids(true), array($frontpagecontext->id, $systemcontext->id)); $this->assertIdentical($systemcontext->get_parent_context(), false); $frontpagecontext = context_course::instance($SITE->id); $parent = $systemcontext; foreach ($testcategories as $catid) { $catcontext = context_coursecat::instance($catid); $this->assertIdentical($catcontext->get_parent_context(), $parent); $parent = $catcontext; } $this->assertIdentical($frontpagepagecontext->get_parent_context(), $frontpagecontext); $this->assertIdentical($frontpageblockcontext->get_parent_context(), $frontpagecontext); $this->assertIdentical($frontpagepageblockcontext->get_parent_context(), $frontpagepagecontext); // ====== $context->get_child_contexts() ================================ $children = $systemcontext->get_child_contexts(); $this->assertEqual(count($children) + 1, $DB->count_records('context')); $context = context_coursecat::instance($testcategories[3]); $children = $context->get_child_contexts(); $countcats = 0; $countcourses = 0; $countblocks = 0; foreach ($children as $child) { if ($child->contextlevel == CONTEXT_COURSECAT) { $countcats++; } if ($child->contextlevel == CONTEXT_COURSE) { $countcourses++; } if ($child->contextlevel == CONTEXT_BLOCK) { $countblocks++; } } $this->assertEqual(count($children), 8); $this->assertEqual($countcats, 1); $this->assertEqual($countcourses, 6); $this->assertEqual($countblocks, 1); $context = context_course::instance($testcourses[2]); $children = $context->get_child_contexts(); $this->assertEqual(count($children), 3); $context = context_module::instance($testpages[3]); $children = $context->get_child_contexts(); $this->assertEqual(count($children), 1); $context = context_block::instance($testblocks[1]); $children = $context->get_child_contexts(); $this->assertEqual(count($children), 0); unset($children); unset($countcats); unset($countcourses); unset($countblocks); // ======= context_helper::reset_caches() ============================ context_helper::reset_caches(); $this->assertEqual(context_inspection::test_context_cache_size(), 0); context_course::instance($SITE->id); $this->assertEqual(context_inspection::test_context_cache_size(), 1); // ======= context preloading ======================================== context_helper::reset_caches(); $sql = "SELECT " . context_helper::get_preload_record_columns_sql('c') . "\n FROM {context} c\n WHERE c.contextlevel <> " . CONTEXT_SYSTEM; $records = $DB->get_records_sql($sql); $firstrecord = reset($records); $columns = context_helper::get_preload_record_columns('c'); $firstrecord = (array) $firstrecord; $this->assertIdentical(array_keys($firstrecord), array_values($columns)); context_helper::reset_caches(); foreach ($records as $record) { context_helper::preload_from_record($record); $this->assertIdentical($record, new stdClass()); } $this->assertEqual(context_inspection::test_context_cache_size(), count($records)); unset($records); unset($columns); context_helper::reset_caches(); context_helper::preload_course($SITE->id); $this->assertEqual(context_inspection::test_context_cache_size(), 4); // ====== assign_capability(), unassign_capability() ==================== $rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups')); $this->assertFalse($rc); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext->id); $rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups')); $this->assertEqual($rc->permission, CAP_ALLOW); assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext->id); $rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups')); $this->assertEqual($rc->permission, CAP_ALLOW); assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $allroles['teacher'], $frontpagecontext, true); $rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups')); $this->assertEqual($rc->permission, CAP_PREVENT); assign_capability('moodle/site:accessallgroups', CAP_INHERIT, $allroles['teacher'], $frontpagecontext); $rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups')); $this->assertFalse($rc); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $allroles['teacher'], $frontpagecontext); unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext, true); $rc = $DB->get_record('role_capabilities', array('contextid' => $frontpagecontext->id, 'roleid' => $allroles['teacher'], 'capability' => 'moodle/site:accessallgroups')); $this->assertFalse($rc); unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext->id, true); unset($rc); accesslib_clear_all_caches(false); // must be done after assign_capability() // ======= role_assign(), role_unassign(), role_unassign_all() ============== $context = context_course::instance($testcourses[1]); $this->assertEqual($DB->count_records('role_assignments', array('contextid' => $context->id)), 0); role_assign($allroles['teacher'], $testusers[1], $context->id); role_assign($allroles['teacher'], $testusers[2], $context->id); role_assign($allroles['manager'], $testusers[1], $context->id); $this->assertEqual($DB->count_records('role_assignments', array('contextid' => $context->id)), 3); role_unassign($allroles['teacher'], $testusers[1], $context->id); $this->assertEqual($DB->count_records('role_assignments', array('contextid' => $context->id)), 2); role_unassign_all(array('contextid' => $context->id)); $this->assertEqual($DB->count_records('role_assignments', array('contextid' => $context->id)), 0); unset($context); accesslib_clear_all_caches(false); // just in case // ====== has_capability(), get_users_by_capability(), role_switch(), reload_all_capabilities() and friends ======================== $adminid = get_admin()->id; $guestid = $CFG->siteguest; // Enrol some users into some courses $course1 = $DB->get_record('course', array('id' => $testcourses[22]), '*', MUST_EXIST); $course2 = $DB->get_record('course', array('id' => $testcourses[7]), '*', MUST_EXIST); $cms = $DB->get_records('course_modules', array('course' => $course1->id), 'id'); $cm1 = reset($cms); $blocks = $DB->get_records('block_instances', array('parentcontextid' => context_module::instance($cm1->id)->id), 'id'); $block1 = reset($blocks); $instance1 = $DB->get_record('enrol', array('enrol' => 'manual', 'courseid' => $course1->id)); $instance2 = $DB->get_record('enrol', array('enrol' => 'manual', 'courseid' => $course2->id)); for ($i = 0; $i < 9; $i++) { $manualenrol->enrol_user($instance1, $testusers[$i], $allroles['student']); } $manualenrol->enrol_user($instance1, $testusers[8], $allroles['teacher']); $manualenrol->enrol_user($instance1, $testusers[9], $allroles['editingteacher']); for ($i = 10; $i < 15; $i++) { $manualenrol->enrol_user($instance2, $testusers[$i], $allroles['student']); } $manualenrol->enrol_user($instance2, $testusers[15], $allroles['editingteacher']); // Add tons of role assignments - the more the better role_assign($allroles['coursecreator'], $testusers[11], context_coursecat::instance($testcategories[2])); role_assign($allroles['manager'], $testusers[12], context_coursecat::instance($testcategories[1])); role_assign($allroles['student'], $testusers[9], context_module::instance($cm1->id)); role_assign($allroles['teacher'], $testusers[8], context_module::instance($cm1->id)); role_assign($allroles['guest'], $testusers[13], context_course::instance($course1->id)); role_assign($allroles['teacher'], $testusers[7], context_block::instance($block1->id)); role_assign($allroles['manager'], $testusers[9], context_block::instance($block1->id)); role_assign($allroles['editingteacher'], $testusers[9], context_course::instance($course1->id)); role_assign($allroles['teacher'], $adminid, context_course::instance($course1->id)); role_assign($allroles['editingteacher'], $adminid, context_block::instance($block1->id)); // Add tons of overrides - the more the better assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultuserroleid, $frontpageblockcontext, true); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpageblockcontext, true); assign_capability('moodle/block:view', CAP_PROHIBIT, $allroles['guest'], $frontpageblockcontext, true); assign_capability('block/online_users:viewlist', CAP_PREVENT, $allroles['user'], $frontpageblockcontext, true); assign_capability('block/online_users:viewlist', CAP_PREVENT, $allroles['student'], $frontpageblockcontext, true); assign_capability('moodle/site:accessallgroups', CAP_PREVENT, $CFG->defaultuserroleid, $frontpagepagecontext, true); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagepagecontext, true); assign_capability('mod/page:view', CAP_PREVENT, $allroles['guest'], $frontpagepagecontext, true); assign_capability('mod/page:view', CAP_ALLOW, $allroles['user'], $frontpagepagecontext, true); assign_capability('moodle/page:view', CAP_ALLOW, $allroles['student'], $frontpagepagecontext, true); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultuserroleid, $frontpagecontext, true); assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagecontext, true); assign_capability('mod/page:view', CAP_ALLOW, $allroles['guest'], $frontpagecontext, true); assign_capability('mod/page:view', CAP_PROHIBIT, $allroles['user'], $frontpagecontext, true); assign_capability('mod/page:view', CAP_PREVENT, $allroles['guest'], $systemcontext, true); accesslib_clear_all_caches(false); // must be done after assign_capability() // Extra tests for guests and not-logged-in users because they can not be verified by cross checking // with get_users_by_capability() where they are ignored $this->assertFalse(has_capability('moodle/block:view', $frontpageblockcontext, $guestid)); $this->assertFalse(has_capability('mod/page:view', $frontpagepagecontext, $guestid)); $this->assertTrue(has_capability('mod/page:view', $frontpagecontext, $guestid)); $this->assertFalse(has_capability('mod/page:view', $systemcontext, $guestid)); $this->assertFalse(has_capability('moodle/block:view', $frontpageblockcontext, 0)); $this->assertFalse(has_capability('mod/page:view', $frontpagepagecontext, 0)); $this->assertTrue(has_capability('mod/page:view', $frontpagecontext, 0)); $this->assertFalse(has_capability('mod/page:view', $systemcontext, 0)); $this->assertFalse(has_capability('moodle/course:create', $systemcontext, $testusers[11])); $this->assertTrue(has_capability('moodle/course:create', context_coursecat::instance($testcategories[2]), $testusers[11])); $this->assertFalse(has_capability('moodle/course:create', context_course::instance($testcourses[1]), $testusers[11])); $this->assertTrue(has_capability('moodle/course:create', context_course::instance($testcourses[19]), $testusers[11])); $this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[1]), $testusers[9])); $this->assertFalse(has_capability('moodle/course:update', context_course::instance($testcourses[19]), $testusers[9])); $this->assertFalse(has_capability('moodle/course:update', $systemcontext, $testusers[9])); // Test the list of enrolled users $coursecontext = context_course::instance($course1->id); $enrolled = get_enrolled_users($coursecontext); $this->assertEqual(count($enrolled), 10); for ($i = 0; $i < 10; $i++) { $this->assertTrue(isset($enrolled[$testusers[$i]])); } $enrolled = get_enrolled_users($coursecontext, 'moodle/course:update'); $this->assertEqual(count($enrolled), 1); $this->assertTrue(isset($enrolled[$testusers[9]])); unset($enrolled); // role switching $userid = $testusers[9]; $USER = $DB->get_record('user', array('id' => $userid)); load_all_capabilities(); $coursecontext = context_course::instance($course1->id); $this->assertTrue(has_capability('moodle/course:update', $coursecontext)); $this->assertFalse(is_role_switched($course1->id)); role_switch($allroles['student'], $coursecontext); $this->assertTrue(is_role_switched($course1->id)); $this->assertEqual($USER->access['rsw'][$coursecontext->path], $allroles['student']); $this->assertFalse(has_capability('moodle/course:update', $coursecontext)); reload_all_capabilities(); $this->assertFalse(has_capability('moodle/course:update', $coursecontext)); role_switch(0, $coursecontext); $this->assertTrue(has_capability('moodle/course:update', $coursecontext)); $userid = $adminid; $USER = $DB->get_record('user', array('id' => $userid)); load_all_capabilities(); $coursecontext = context_course::instance($course1->id); $blockcontext = context_block::instance($block1->id); $this->assertTrue(has_capability('moodle/course:update', $blockcontext)); role_switch($allroles['student'], $coursecontext); $this->assertEqual($USER->access['rsw'][$coursecontext->path], $allroles['student']); $this->assertFalse(has_capability('moodle/course:update', $blockcontext)); reload_all_capabilities(); $this->assertFalse(has_capability('moodle/course:update', $blockcontext)); load_all_capabilities(); $this->assertTrue(has_capability('moodle/course:update', $blockcontext)); // temp course role for enrol $DB->delete_records('cache_flags', array()); // this prevents problem with dirty contexts immediately resetting the temp role - this is a known problem... $userid = $testusers[5]; $roleid = $allroles['editingteacher']; $USER = $DB->get_record('user', array('id' => $userid)); load_all_capabilities(); $coursecontext = context_course::instance($course1->id); $this->assertFalse(has_capability('moodle/course:update', $coursecontext)); $this->assertFalse(isset($USER->access['ra'][$coursecontext->path][$roleid])); load_temp_course_role($coursecontext, $roleid); $this->assertEqual($USER->access['ra'][$coursecontext->path][$roleid], $roleid); $this->assertTrue(has_capability('moodle/course:update', $coursecontext)); remove_temp_course_roles($coursecontext); $this->assertFalse(has_capability('moodle/course:update', $coursecontext, $userid)); load_temp_course_role($coursecontext, $roleid); reload_all_capabilities(); $this->assertFalse(has_capability('moodle/course:update', $coursecontext, $userid)); $USER = new stdClass(); $USER->id = 0; // Now cross check has_capability() with get_users_by_capability(), each using different code paths, // they have to be kept in sync, usually only one of them breaks, so we know when something is wrong, // at the same time validate extra restrictions (guest read only no risks, admin exception, non existent and deleted users) $contexts = $DB->get_records('context', array(), 'id'); $contexts = array_values($contexts); $capabilities = $DB->get_records('capabilities', array(), 'id'); $capabilities = array_values($capabilities); $roles = array($allroles['guest'], $allroles['user'], $allroles['teacher'], $allroles['editingteacher'], $allroles['coursecreator'], $allroles['manager']); // Random time! srand(666); foreach ($testusers as $userid) { // no guest or deleted // each user gets 0-20 random roles $rcount = rand(0, 10); for ($j = 0; $j < $rcount; $j++) { $roleid = $roles[rand(0, count($roles) - 1)]; $contextid = $contexts[rand(0, count($contexts) - 1)]->id; role_assign($roleid, $userid, $contextid); } } $permissions = array(CAP_ALLOW, CAP_PREVENT, CAP_INHERIT, CAP_PREVENT); for ($j = 0; $j < 10000; $j++) { $roleid = $roles[rand(0, count($roles) - 1)]; $contextid = $contexts[rand(0, count($contexts) - 1)]->id; $permission = $permissions[rand(0, count($permissions) - 1)]; $capname = $capabilities[rand(0, count($capabilities) - 1)]->name; assign_capability($capname, $permission, $roleid, $contextid, true); } unset($permissions); unset($roles); unset($contexts); unset($users); unset($capabilities); accesslib_clear_all_caches(false); // must be done after assign_capability() // Test time - let's set up some real user, just in case the logic for USER affects the others... $USER = $DB->get_record('user', array('id' => $testusers[3])); load_all_capabilities(); $contexts = $DB->get_records('context', array(), 'id'); $users = $DB->get_records('user', array(), 'id', 'id'); $capabilities = $DB->get_records('capabilities', array(), 'id'); $users[0] = null; // not-logged-in user $users[-1] = null; // non-existent user foreach ($contexts as $crecord) { $context = context::instance_by_id($crecord->id); if ($coursecontext = $context->get_course_context(false)) { $enrolled = get_enrolled_users($context); } else { $enrolled = array(); } foreach ($capabilities as $cap) { $allowed = get_users_by_capability($context, $cap->name, 'u.id, u.username'); if ($enrolled) { $enrolledwithcap = get_enrolled_users($context, $cap->name); } else { $enrolledwithcap = array(); } foreach ($users as $userid => $unused) { if ($userid == 0 or isguestuser($userid)) { if ($userid == 0) { $CFG->forcelogin = true; $this->assertFalse(has_capability($cap->name, $context, $userid)); unset($CFG->forcelogin); } if ($cap->captype === 'write' or $cap->riskbitmask & (RISK_XSS | RISK_CONFIG | RISK_DATALOSS)) { $this->assertFalse(has_capability($cap->name, $context, $userid)); } $this->assertFalse(isset($allowed[$userid])); } else { if (is_siteadmin($userid)) { $this->assertTrue(has_capability($cap->name, $context, $userid, true)); } $hascap = has_capability($cap->name, $context, $userid, false); $this->assertIdentical($hascap, isset($allowed[$userid]), "Capability result mismatch user:{$userid}, context:{$context->id}, {$cap->name}, hascap: " . (int) $hascap . " "); if (isset($enrolled[$userid])) { $this->assertIdentical(isset($allowed[$userid]), isset($enrolledwithcap[$userid]), "Enrolment with capability result mismatch user:{$userid}, context:{$context->id}, {$cap->name}, hascap: " . (int) $hascap . " "); } } } } } // Back to nobody $USER = new stdClass(); $USER->id = 0; unset($contexts); unset($users); unset($capabilities); // Now let's do all the remaining tests that break our carefully prepared fake site // ======= $context->mark_dirty() ======================================= $DB->delete_records('cache_flags', array()); accesslib_clear_all_caches(false); $systemcontext->mark_dirty(); $dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2); $this->assertTrue(isset($dirty[$systemcontext->path])); $this->assertTrue(isset($ACCESSLIB_PRIVATE->dirtycontexts[$systemcontext->path])); // ======= $context->reload_if_dirty(); ================================= $DB->delete_records('cache_flags', array()); accesslib_clear_all_caches(false); load_all_capabilities(); $context = context_course::instance($testcourses[2]); $page = $DB->get_record('page', array('course' => $testcourses[2])); $pagecontext = context_module::instance($page->id); $context->mark_dirty(); $this->assertTrue(isset($ACCESSLIB_PRIVATE->dirtycontexts[$context->path])); $USER->access['test'] = true; $context->reload_if_dirty(); $this->assertFalse(isset($USER->access['test'])); $context->mark_dirty(); $this->assertTrue(isset($ACCESSLIB_PRIVATE->dirtycontexts[$context->path])); $USER->access['test'] = true; $pagecontext->reload_if_dirty(); $this->assertFalse(isset($USER->access['test'])); // ======= context_helper::build_all_paths() ============================ $oldcontexts = $DB->get_records('context', array(), 'id'); $DB->set_field_select('context', 'path', NULL, "contextlevel <> " . CONTEXT_SYSTEM); $DB->set_field_select('context', 'depth', 0, "contextlevel <> " . CONTEXT_SYSTEM); context_helper::build_all_paths(); $newcontexts = $DB->get_records('context', array(), 'id'); $this->assertIdentical($oldcontexts, $newcontexts); unset($oldcontexts); unset($newcontexts); // ======= $context->reset_paths() ====================================== $context = context_course::instance($testcourses[2]); $children = $context->get_child_contexts(); $context->reset_paths(false); $this->assertIdentical($DB->get_field('context', 'path', array('id' => $context->id)), NULL); $this->assertEqual($DB->get_field('context', 'depth', array('id' => $context->id)), 0); foreach ($children as $child) { $this->assertIdentical($DB->get_field('context', 'path', array('id' => $child->id)), NULL); $this->assertEqual($DB->get_field('context', 'depth', array('id' => $child->id)), 0); } $this->assertEqual(count($children) + 1, $DB->count_records('context', array('depth' => 0))); $this->assertEqual(count($children) + 1, $DB->count_records('context', array('path' => NULL))); $context = context_course::instance($testcourses[2]); $context->reset_paths(true); $context = context_course::instance($testcourses[2]); $this->assertEqual($DB->get_field('context', 'path', array('id' => $context->id)), $context->path); $this->assertEqual($DB->get_field('context', 'depth', array('id' => $context->id)), $context->depth); $this->assertEqual(0, $DB->count_records('context', array('depth' => 0))); $this->assertEqual(0, $DB->count_records('context', array('path' => NULL))); // ====== $context->update_moved(); ====================================== accesslib_clear_all_caches(false); $DB->delete_records('cache_flags', array()); $course = $DB->get_record('course', array('id' => $testcourses[0])); $context = context_course::instance($course->id); $oldpath = $context->path; $miscid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}"); $categorycontext = context_coursecat::instance($miscid); $course->category = $miscid; $DB->update_record('course', $course); $context->update_moved($categorycontext); $context = context_course::instance($course->id); $this->assertIdentical($context->get_parent_context(), $categorycontext); $dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2); $this->assertTrue(isset($dirty[$oldpath])); $this->assertTrue(isset($dirty[$context->path])); // ====== $context->delete_content() ===================================== context_helper::reset_caches(); $context = context_module::instance($testpages[3]); $this->assertTrue($DB->record_exists('context', array('id' => $context->id))); $this->assertEqual(1, $DB->count_records('block_instances', array('parentcontextid' => $context->id))); $context->delete_content(); $this->assertTrue($DB->record_exists('context', array('id' => $context->id))); $this->assertEqual(0, $DB->count_records('block_instances', array('parentcontextid' => $context->id))); // ====== $context->delete() ============================= context_helper::reset_caches(); $context = context_module::instance($testpages[4]); $this->assertTrue($DB->record_exists('context', array('id' => $context->id))); $this->assertEqual(1, $DB->count_records('block_instances', array('parentcontextid' => $context->id))); $bi = $DB->get_record('block_instances', array('parentcontextid' => $context->id)); $bicontext = context_block::instance($bi->id); $DB->delete_records('cache_flags', array()); $context->delete(); // should delete also linked blocks $dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2); $this->assertTrue(isset($dirty[$context->path])); $this->assertFalse($DB->record_exists('context', array('id' => $context->id))); $this->assertFalse($DB->record_exists('context', array('id' => $bicontext->id))); $this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_MODULE, 'instanceid' => $testpages[4]))); $this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $bi->id))); $this->assertEqual(0, $DB->count_records('block_instances', array('parentcontextid' => $context->id))); context_module::instance($testpages[4]); // ====== context_helper::delete_instance() ============================= context_helper::reset_caches(); $lastcourse = array_pop($testcourses); $this->assertTrue($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $lastcourse))); $coursecontext = context_course::instance($lastcourse); $this->assertEqual(context_inspection::test_context_cache_size(), 1); $this->assertFalse($coursecontext->instanceid == CONTEXT_COURSE); $DB->delete_records('cache_flags', array()); context_helper::delete_instance(CONTEXT_COURSE, $lastcourse); $dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2); $this->assertTrue(isset($dirty[$coursecontext->path])); $this->assertEqual(context_inspection::test_context_cache_size(), 0); $this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $lastcourse))); context_course::instance($lastcourse); // ======= context_helper::create_instances() ========================== $prevcount = $DB->count_records('context'); $DB->delete_records('context', array('contextlevel' => CONTEXT_BLOCK)); context_helper::create_instances(null, true); $this->assertIdentical($DB->count_records('context'), $prevcount); $this->assertEqual($DB->count_records('context', array('depth' => 0)), 0); $this->assertEqual($DB->count_records('context', array('path' => NULL)), 0); $DB->delete_records('context', array('contextlevel' => CONTEXT_BLOCK)); $DB->delete_records('block_instances', array()); $prevcount = $DB->count_records('context'); $DB->delete_records_select('context', 'contextlevel <> ' . CONTEXT_SYSTEM); context_helper::create_instances(null, true); $this->assertIdentical($DB->count_records('context'), $prevcount); $this->assertEqual($DB->count_records('context', array('depth' => 0)), 0); $this->assertEqual($DB->count_records('context', array('path' => NULL)), 0); // ======= context_helper::cleanup_instances() ========================== $lastcourse = $DB->get_field_sql("SELECT MAX(id) FROM {course}"); $DB->delete_records('course', array('id' => $lastcourse)); $lastcategory = $DB->get_field_sql("SELECT MAX(id) FROM {course_categories}"); $DB->delete_records('course_categories', array('id' => $lastcategory)); $lastuser = $DB->get_field_sql("SELECT MAX(id) FROM {user} WHERE deleted=0"); $DB->delete_records('user', array('id' => $lastuser)); $DB->delete_records('block_instances', array('parentcontextid' => $frontpagepagecontext->id)); $DB->delete_records('course_modules', array('id' => $frontpagepagecontext->instanceid)); context_helper::cleanup_instances(); $count = 1; //system $count += $DB->count_records('user', array('deleted' => 0)); $count += $DB->count_records('course_categories'); $count += $DB->count_records('course'); $count += $DB->count_records('course_modules'); $count += $DB->count_records('block_instances'); $this->assertEqual($DB->count_records('context'), $count); // ======= context cache size restrictions ============================== $testusers = array(); for ($i = 0; $i < CONTEXT_CACHE_MAX_SIZE + 100; $i++) { $user = new stdClass(); $user->auth = 'manual'; $user->firstname = 'xuser' . $i; $user->lastname = 'xuser' . $i; $user->username = '******' . $i; $user->password = '******'; $user->email = "xuser{$i}@example.com"; $user->confirmed = 1; $user->mnethostid = $CFG->mnet_localhost_id; $user->lang = $CFG->lang; $user->maildisplay = 1; $user->timemodified = time(); $user->lastip = '0.0.0.0'; $userid = $DB->insert_record('user', $user); $testusers[$i] = $userid; } context_helper::create_instances(null, true); context_helper::reset_caches(); for ($i = 0; $i < CONTEXT_CACHE_MAX_SIZE + 100; $i++) { context_user::instance($testusers[$i]); if ($i == CONTEXT_CACHE_MAX_SIZE - 1) { $this->assertEqual(context_inspection::test_context_cache_size(), CONTEXT_CACHE_MAX_SIZE); } else { if ($i == CONTEXT_CACHE_MAX_SIZE) { // once the limit is reached roughly 1/3 of records should be removed from cache $this->assertEqual(context_inspection::test_context_cache_size(), (int) (CONTEXT_CACHE_MAX_SIZE * (2 / 3) + 102)); } } } // We keep the first 100 cached $prevsize = context_inspection::test_context_cache_size(); for ($i = 0; $i < 100; $i++) { context_user::instance($testusers[$i]); $this->assertEqual(context_inspection::test_context_cache_size(), $prevsize); } context_user::instance($testusers[102]); $this->assertEqual(context_inspection::test_context_cache_size(), $prevsize + 1); unset($testusers); // ================================================================= // ======= basic test of legacy functions ========================== // ================================================================= // note: watch out, the fake site might be pretty borked already $this->assertIdentical(get_system_context(), context_system::instance()); foreach ($DB->get_records('context') as $contextid => $record) { $context = context::instance_by_id($contextid); $this->assertIdentical(get_context_instance_by_id($contextid), $context); $this->assertIdentical(get_context_instance($record->contextlevel, $record->instanceid), $context); $this->assertIdentical(get_parent_contexts($context), $context->get_parent_context_ids()); if ($context->id == SYSCONTEXTID) { $this->assertIdentical(get_parent_contextid($context), false); } else { $this->assertIdentical(get_parent_contextid($context), $context->get_parent_context()->id); } } $children = get_child_contexts($systemcontext); $this->assertEqual(count($children), $DB->count_records('context') - 1); unset($children); $DB->delete_records('context', array('contextlevel' => CONTEXT_BLOCK)); create_contexts(); $this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK))); $DB->set_field('context', 'depth', 0, array('contextlevel' => CONTEXT_BLOCK)); build_context_path(); $this->assertFalse($DB->record_exists('context', array('depth' => 0))); $lastcourse = $DB->get_field_sql("SELECT MAX(id) FROM {course}"); $DB->delete_records('course', array('id' => $lastcourse)); $lastcategory = $DB->get_field_sql("SELECT MAX(id) FROM {course_categories}"); $DB->delete_records('course_categories', array('id' => $lastcategory)); $lastuser = $DB->get_field_sql("SELECT MAX(id) FROM {user} WHERE deleted=0"); $DB->delete_records('user', array('id' => $lastuser)); $DB->delete_records('block_instances', array('parentcontextid' => $frontpagepagecontext->id)); $DB->delete_records('course_modules', array('id' => $frontpagepagecontext->instanceid)); cleanup_contexts(); $count = 1; //system $count += $DB->count_records('user', array('deleted' => 0)); $count += $DB->count_records('course_categories'); $count += $DB->count_records('course'); $count += $DB->count_records('course_modules'); $count += $DB->count_records('block_instances'); $this->assertEqual($DB->count_records('context'), $count); context_helper::reset_caches(); preload_course_contexts($SITE->id); $this->assertEqual(context_inspection::test_context_cache_size(), 1); context_helper::reset_caches(); list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx'); $sql = "SELECT c.id {$select} FROM {course_categories} c {$join}"; $records = $DB->get_records_sql($sql); foreach ($records as $record) { context_instance_preload($record); $record = (array) $record; $this->assertEqual(1, count($record)); // only id left } $this->assertEqual(count($records), context_inspection::test_context_cache_size()); accesslib_clear_all_caches(true); $DB->delete_records('cache_flags', array()); mark_context_dirty($systemcontext->path); $dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2); $this->assertTrue(isset($dirty[$systemcontext->path])); accesslib_clear_all_caches(false); $DB->delete_records('cache_flags', array()); $course = $DB->get_record('course', array('id' => $testcourses[2])); $context = get_context_instance(CONTEXT_COURSE, $course->id); $oldpath = $context->path; $miscid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}"); $categorycontext = context_coursecat::instance($miscid); $course->category = $miscid; $DB->update_record('course', $course); context_moved($context, $categorycontext); $context = get_context_instance(CONTEXT_COURSE, $course->id); $this->assertIdentical($context->get_parent_context(), $categorycontext); $this->assertTrue($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $testcourses[2]))); delete_context(CONTEXT_COURSE, $testcourses[2]); $this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $testcourses[2]))); $name = get_contextlevel_name(CONTEXT_COURSE); $this->assertFalse(empty($name)); $context = get_context_instance(CONTEXT_COURSE, $testcourses[2]); $name = print_context_name($context); $this->assertFalse(empty($name)); $url = get_context_url($coursecontext); $this->assertFalse($url instanceof modole_url); $page = $DB->get_record('page', array('id' => $testpages[7])); $context = get_context_instance(CONTEXT_MODULE, $page->id); $coursecontext = get_course_context($context); $this->assertEqual($coursecontext->contextlevel, CONTEXT_COURSE); $this->assertEqual(get_courseid_from_context($context), $page->course); $caps = fetch_context_capabilities($systemcontext); $this->assertTrue(is_array($caps)); unset($caps); }
public function delete() { global $CFG; $result = false; $muser = cm_get_moodleuserid($this->id); if (empty($muser) || !is_primary_admin($muser)) { $level = context_level_base::get_custom_context_level('user', 'block_curr_admin'); $result = attendance::delete_for_user($this->id); $result = $result && curriculumstudent::delete_for_user($this->id); $result = $result && instructor::delete_for_user($this->id); $result = $result && student::delete_for_user($this->id); $result = $result && student_grade::delete_for_user($this->id); $result = $result && usertrack::delete_for_user($this->id); $result = $result && usercluster::delete_for_user($this->id); $result = $result && clusterassignment::delete_for_user($this->id); $result = $result && waitlist::delete_for_user($this->id); $result = $result && delete_context($level, $this->id); // Delete Moodle user. if ($muser = get_record('user', 'idnumber', $this->idnumber, 'mnethostid', $CFG->mnet_localhost_id, 'deleted', 0)) { $result = $result && delete_user($muser); } $result = $result && parent::delete(); } return $result; }
/** * Perform the necessary actions required to "delete" a cluster from the system. * * @uses CURMAN * @uses CFG * @param none * @return bool True on success, False otherwise. */ function delete($deletesubs = 0) { global $CURMAN, $CFG; require_once CURMAN_DIRLOCATION . '/cluster/profile/lib.php'; $result = true; $delete_ids = array(); $promote_ids = array(); $cluster_context_level = context_level_base::get_custom_context_level('cluster', 'block_curr_admin'); if ($deletesubs > 0) { /// Figure out all the sub-cluster ids and whether to delete or promote them $LIKE = $CURMAN->db->sql_compare(); $cluster_context_instance = get_context_instance($cluster_context_level, $this->id); $instance_id = $cluster_context_instance->id; $instance_path = $cluster_context_instance->path; $sql = "SELECT instanceid FROM {$CFG->prefix}context\n WHERE path {$LIKE} '{$instance_path}/%' ORDER BY instanceid DESC"; $clusters = get_records_sql($sql); foreach ($clusters as $cluster) { if ($deletesubs == 1) { // This sub-cluster will be deleted $delete_ids[] = $cluster->instanceid; } else { // This sub-cluster will be promoted $promote_ids[] = $cluster->instanceid; } } } $delete_ids[] = $this->id; // The specified cluster always gets deleted foreach ($delete_ids as $delete_id) { // Cascade to regular datarecords $result = $result && clustercurriculum::delete_for_cluster($delete_id); // in clustercurriculum $result = $result && clustertrack::delete_for_cluster($delete_id); // in clustercurriculum $result = $result && clusterassignment::delete_for_cluster($delete_id); $result = $result && usercluster::delete_for_cluster($delete_id); $result = $result && delete_context($cluster_context_level, $delete_id); // Cascade to all plugins $plugins = $this->get_plugins(); foreach ($plugins as $plugin) { require_once CURMAN_DIRLOCATION . '/cluster/' . $plugin . '/lib.php'; $result = $result && call_user_func('cluster_' . $plugin . '_delete_for_cluster', $delete_id); } $result = $result && datarecord::data_delete_record($delete_id); // this record } if (count($promote_ids) > 0) { foreach ($promote_ids as $promote_id) { $cluster_data = get_record(CLSTTABLE, 'id', $promote_id); $lower_depth = $cluster_data->depth - 1; $select = "id='{$cluster_data->parent}'"; $parent_cnt = $CURMAN->db->count_records_select(CLSTTABLE, $select); $newclusterdata = new stdClass(); $newclusterdata->id = $promote_id; if ($parent_cnt < 1) { /// Parent not found so this cluster will be top-level $newclusterdata->parent = 0; $newclusterdata->depth = 1; } else { /// A child cluster found so lets lower the depth $newclusterdata->depth = $lower_depth; } $result = update_record(CLSTTABLE, $newclusterdata); $cluster_context_level = context_level_base::get_custom_context_level('cluster', 'block_curr_admin'); $sql = "UPDATE {$CFG->prefix}context\n SET depth=0, path=NULL\n WHERE contextlevel='{$cluster_context_level}' AND instanceid='{$promote_id}'"; $feedback = ""; execute_sql($sql, $feedback); } build_context_path(); // Re-build the context table for all sub-clusters } return $result; }
$newcm->showavailability = $fromform->showavailability; } if (!($fromform->coursemodule = add_course_module($newcm))) { print_error('cannotaddcoursemodule'); } if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) { $introeditor = $fromform->introeditor; unset($fromform->introeditor); $fromform->intro = $introeditor['text']; $fromform->introformat = $introeditor['format']; } $returnfromfunc = $addinstancefunction($fromform, $mform); if (!$returnfromfunc or !is_number($returnfromfunc)) { // undo everything we can $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); delete_context(CONTEXT_MODULE, $fromform->coursemodule); $DB->delete_records('course_modules', array('id' => $fromform->coursemodule)); if (!is_number($returnfromfunc)) { print_error('invalidfunction', '', "view.php?id={$course->id}#section-{$cw->section}"); } else { print_error('cannotaddnewmodule', '', "view.php?id={$course->id}#section-{$cw->section}", $fromform->modulename); } } $fromform->instance = $returnfromfunc; $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $fromform->coursemodule)); // update embedded links and save files $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); if (!empty($introeditor)) { $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $fromform->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']); $DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id' => $fromform->instance)); }
function delete() { $status = true; if (!empty($this->id)) { instructor::delete_for_class($this->id); student::delete_for_class($this->id); trackassignmentclass::delete_for_class($this->id); classmoodlecourse::delete_for_class($this->id); student_grade::delete_for_class($this->id); attendance::delete_for_class($this->id); taginstance::delete_for_class($this->id); waitlist::delete_for_class($this->id); classmoodlecourse::delete_for_class($this->id); $level = context_level_base::get_custom_context_level('class', 'block_curr_admin'); $result = delete_context($level, $this->id); $status = $this->data_delete_record(); } return $status; }
/** * This function will handles the whole deletion process of a module. This includes calling * the modules delete_instance function, deleting files, events, grades, conditional data, * the data in the course_module and course_sections table and adding a module deletion * event to the DB. * * @param int $cmid the course module id * @since 2.5 */ function course_delete_module($cmid) { global $CFG, $DB, $USER; require_once $CFG->libdir . '/gradelib.php'; require_once $CFG->dirroot . '/blog/lib.php'; // Get the course module. if (!($cm = $DB->get_record('course_modules', array('id' => $cmid)))) { return true; } // Get the module context. $modcontext = context_module::instance($cm->id); // Get the course module name. $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module), MUST_EXIST); // Get the file location of the delete_instance function for this module. $modlib = "{$CFG->dirroot}/mod/{$modulename}/lib.php"; // Include the file required to call the delete_instance function for this module. if (file_exists($modlib)) { require_once $modlib; } else { throw new moodle_exception('cannotdeletemodulemissinglib', '', '', null, "Cannot delete this module as the file mod/{$modulename}/lib.php is missing."); } $deleteinstancefunction = $modulename . '_delete_instance'; // Ensure the delete_instance function exists for this module. if (!function_exists($deleteinstancefunction)) { throw new moodle_exception('cannotdeletemodulemissingfunc', '', '', null, "Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/{$modulename}/lib.php."); } // Call the delete_instance function, if it returns false throw an exception. if (!$deleteinstancefunction($cm->instance)) { throw new moodle_exception('cannotdeletemoduleinstance', '', '', null, "Cannot delete the module {$modulename} (instance)."); } // Remove all module files in case modules forget to do that. $fs = get_file_storage(); $fs->delete_area_files($modcontext->id); // Delete events from calendar. if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) { foreach ($events as $event) { delete_event($event->id); } } // Delete grade items, outcome items and grades attached to modules. if ($grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename, 'iteminstance' => $cm->instance, 'courseid' => $cm->course))) { foreach ($grade_items as $grade_item) { $grade_item->delete('moddelete'); } } // Delete completion and availability data; it is better to do this even if the // features are not turned on, in case they were turned on previously (these will be // very quick on an empty table). $DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id)); $DB->delete_records('course_modules_availability', array('coursemoduleid' => $cm->id)); $DB->delete_records('course_modules_avail_fields', array('coursemoduleid' => $cm->id)); $DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id, 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY)); // Delete the context. delete_context(CONTEXT_MODULE, $cm->id); // Delete the module from the course_modules table. $DB->delete_records('course_modules', array('id' => $cm->id)); // Delete module from that section. if (!delete_mod_from_section($cm->id, $cm->section)) { throw new moodle_exception('cannotdeletemodulefromsection', '', '', null, "Cannot delete the module {$modulename} (instance) from section."); } // Trigger a mod_deleted event with information about this module. $eventdata = new stdClass(); $eventdata->modulename = $modulename; $eventdata->cmid = $cm->id; $eventdata->courseid = $cm->course; $eventdata->userid = $USER->id; events_trigger('mod_deleted', $eventdata); add_to_log($cm->course, 'course', "delete mod", "view.php?id={$cm->course}", "{$modulename} {$cm->instance}", $cm->id); rebuild_course_cache($cm->course, true); }
function moodle_group_delete_groups($client) { global $DB, $CFG; //create category $category = new stdClass(); $category->name = 'tmpcategoryfortest123'; $category->id = $DB->insert_record('course_categories', $category); //create a course $course = new stdClass(); $course->fullname = 'tmpcoursefortest123'; $course->shortname = 'tmpcoursefortest123'; $course->idnumber = 'tmpcoursefortest123'; $course->category = $category->id; $course->id = $DB->insert_record('course', $course); //create a role $role1->id = create_role('role1thatshouldnotexist', 'role1thatshouldnotexist', ''); //create a user $user = new stdClass(); $user->username = '******'; $user->password = '******'; $user->firstname = 'testfirstname2'; $user->lastname = 'testlastname2'; $user->email = '*****@*****.**'; $user->mnethostid = $CFG->mnet_localhost_id; require_once($CFG->dirroot."/user/lib.php"); $user->id = user_create_user($user); //create course context $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST); //enrol the user in the course with the created role role_assign($role1->id, $user->id, $context->id); $enrol = new stdClass(); $enrol->courseid = $course->id; $enrol->roleid = $role1->id; $enrol->id = $DB->insert_record('enrol', $enrol); $enrolment = new stdClass(); $enrolment->userid = $user->id; $enrolment->enrolid = $enrol->id; $enrolment->id = $DB->insert_record('user_enrolments', $enrolment); //create a group in the course $group = new stdClass(); $group->courseid = $course->id; $group->name = 'tmpgroufortest123'; $group->enrolmentkey = ''; $group->description = ''; $group->id = $DB->insert_record('groups', $group); $group2 = new stdClass(); $group2->courseid = $course->id; $group2->name = 'tmpgroufortest1233'; $group2->enrolmentkey = ''; $group2->description = ''; $group2->id = $DB->insert_record('groups', $group2); $paramgroups = array($group, $group2); require_once($CFG->dirroot . "/group/lib.php"); $groups = groups_get_all_groups($course->id); $this->assertEqual(2, count($groups)); //WEBSERVICE CALL - delete the group $function = 'moodle_group_delete_groups'; $params = array('groupids' => array($group->id, $group2->id)); $client->call($function, $params); $groups = groups_get_all_groups($course->id); $this->assertEqual(0, count($groups)); //unenrol the user $DB->delete_records('user_enrolments', array('id' => $enrolment->id)); $DB->delete_records('enrol', array('id' => $enrol->id)); role_unassign($role1->id, $user->id, $context->id); //delete course context delete_context(CONTEXT_COURSE, $course->id); //delete the user $DB->delete_records('user', array('id' => $user->id)); //delete the role delete_role($role1->id); //delete the course $DB->delete_records('course', array('id' => $course->id)); //delete the category $DB->delete_records('course_categories', array('id' => $category->id)); }
/** * Remove stale context records * * @return bool */ function cleanup_contexts() { global $DB; $sql = " SELECT c.contextlevel,\n c.instanceid AS instanceid\n FROM {context} c\n LEFT OUTER JOIN {course}_categories t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSECAT . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {context} c\n LEFT OUTER JOIN {course} t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSE . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {context} c\n LEFT OUTER JOIN {course}_modules t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_MODULE . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {context} c\n LEFT OUTER JOIN {user} t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_USER . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {context} c\n LEFT OUTER JOIN {block_instances} t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_BLOCK . "\n "; if ($rs = $DB->get_recordset_sql($sql)) { $DB->begin_sql(); $ok = true; foreach ($rs as $ctx) { if (!delete_context($ctx->contextlevel, $ctx->instanceid)) { $ok = false; break; } } $rs->close(); if ($ok) { $DB->commit_sql(); return true; } else { $DB->rollback_sql(); return false; } } return true; }
/** * Remove stale context records * * @return bool */ function cleanup_contexts() { global $DB; $sql = " SELECT c.contextlevel,\n c.instanceid AS instanceid\n FROM {context} c\n LEFT OUTER JOIN {course}_categories t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSECAT . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {context} c\n LEFT OUTER JOIN {course} t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSE . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {context} c\n LEFT OUTER JOIN {course}_modules t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_MODULE . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {context} c\n LEFT OUTER JOIN {user} t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_USER . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {context} c\n LEFT OUTER JOIN {block_instances} t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_BLOCK . "\n "; // transactions used only for performance reasons here $transaction = $DB->start_delegated_transaction(); $rs = $DB->get_recordset_sql($sql); foreach ($rs as $ctx) { delete_context($ctx->contextlevel, $ctx->instanceid); } $rs->close(); $transaction->allow_commit(); return true; }
/** * Clear a course out completely, deleting all content * but don't delete the course itself. * This function does not verify any permissions. * * Please note this function also deletes all user enrolments, * enrolment instances and role assignments. * * @param int $courseid The id of the course that is being deleted * @param bool $showfeedback Whether to display notifications of each action the function performs. * @return bool true if all the removals succeeded. false if there were any failures. If this * method returns false, some of the removals will probably have succeeded, and others * failed, but you have no way of knowing which. */ function remove_course_contents($courseid, $showfeedback = true) { global $CFG, $DB, $OUTPUT; require_once $CFG->libdir . '/completionlib.php'; require_once $CFG->libdir . '/questionlib.php'; require_once $CFG->libdir . '/gradelib.php'; require_once $CFG->dirroot . '/group/lib.php'; require_once $CFG->dirroot . '/tag/coursetagslib.php'; $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST); $strdeleted = get_string('deleted'); // Delete course completion information, // this has to be done before grades and enrols $cc = new completion_info($course); $cc->clear_criteria(); // remove roles and enrolments role_unassign_all(array('contextid' => $context->id), true); enrol_course_delete($course); // Clean up course formats (iterate through all formats in the even the course format was ever changed) $formats = get_plugin_list('format'); foreach ($formats as $format => $formatdir) { $formatdelete = 'format_' . $format . '_delete_course'; $formatlib = "{$formatdir}/lib.php"; if (file_exists($formatlib)) { include_once $formatlib; if (function_exists($formatdelete)) { if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' ' . $format); } $formatdelete($course->id); } } } // Remove all data from gradebook - this needs to be done before course modules // because while deleting this information, the system may need to reference // the course modules that own the grades. remove_course_grades($courseid, $showfeedback); remove_grade_letters($context, $showfeedback); // Remove all data from availability and completion tables that is associated // with course-modules belonging to this course. Note this is done even if the // features are not enabled now, in case they were enabled previously $DB->delete_records_select('course_modules_completion', 'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)', array($courseid)); $DB->delete_records_select('course_modules_availability', 'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)', array($courseid)); // Delete course blocks - they may depend on modules so delete them first blocks_delete_all_for_context($context->id); // Delete every instance of every module if ($allmods = $DB->get_records('modules')) { foreach ($allmods as $mod) { $modname = $mod->name; $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php'; $moddelete = $modname . '_delete_instance'; // Delete everything connected to an instance $moddeletecourse = $modname . '_delete_course'; // Delete other stray stuff (uncommon) $count = 0; if (file_exists($modfile)) { include_once $modfile; if (function_exists($moddelete)) { if ($instances = $DB->get_records($modname, array('course' => $course->id))) { foreach ($instances as $instance) { if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) { /// Delete activity context questions and question categories question_delete_activity($cm, $showfeedback); } if ($moddelete($instance->id)) { $count++; } else { echo $OUTPUT->notification('Could not delete ' . $modname . ' instance ' . $instance->id . ' (' . format_string($instance->name) . ')'); } if ($cm) { // delete cm and its context in correct order delete_context(CONTEXT_MODULE, $cm->id); // some callbacks may try to fetch context, better delete first $DB->delete_records('course_modules', array('id' => $cm->id)); } } } } else { //note: we should probably delete these anyway echo $OUTPUT->notification('Function ' . $moddelete . '() doesn\'t exist!'); } if (function_exists($moddeletecourse)) { $moddeletecourse($course, $showfeedback); } } if ($showfeedback) { echo $OUTPUT->notification($strdeleted . ' ' . $count . ' x ' . $modname); } } } // Delete any groups, removing members and grouping/course links first. groups_delete_groupings($course->id, $showfeedback); groups_delete_groups($course->id, $showfeedback); // Delete questions and question categories question_delete_course($course, $showfeedback); // Delete course tags coursetag_delete_course_tags($course->id, $showfeedback); // Delete legacy files (just in case some files are still left there after conversion to new file api) fulldelete($CFG->dataroot . '/' . $course->id); // cleanup course record - remove links to delted stuff $oldcourse = new stdClass(); $oldcourse->id = $course->id; $oldcourse->summary = ''; $oldcourse->modinfo = NULL; $oldcourse->legacyfiles = 0; $oldcourse->defaultgroupingid = 0; $oldcourse->enablecompletion = 0; $DB->update_record('course', $oldcourse); // Delete all related records in other tables that may have a courseid // This array stores the tables that need to be cleared, as // table_name => column_name that contains the course id. $tablestoclear = array('event' => 'courseid', 'log' => 'course', 'course_sections' => 'course', 'course_modules' => 'course', 'course_display' => 'course', 'backup_courses' => 'courseid', 'user_lastaccess' => 'courseid', 'backup_log' => 'courseid'); foreach ($tablestoclear as $table => $col) { $DB->delete_records($table, array($col => $course->id)); } // Delete all remaining stuff linked to context, // such as remaining roles, files, comments, etc. // Keep the context record for now. delete_context(CONTEXT_COURSE, $course->id, false); //trigger events $course->context = $context; // you can not access context in cron event later after course is deleted events_trigger('course_content_removed', $course); return true; }
function delete() { $level = context_level_base::get_custom_context_level('curriculum', 'block_curr_admin'); $result = track::delete_for_curriculum($this->id); $result = $result && clustercurriculum::delete_for_curriculum($this->id); $result = $result && curriculumcourse::delete_for_curriculum($this->id); $result = $result && curriculumstudent::delete_for_curriculum($this->id); $result = $result && taginstance::delete_for_curriculum($this->id); $result = $result && delete_context($level, $this->id); return $result && $this->data_delete_record(); }
/** * Recursively delete category including all subcategories and courses * * Function {@link coursecat::can_delete_full()} MUST be called prior * to calling this function because there is no capability check * inside this function * * @param boolean $showfeedback display some notices * @return array return deleted courses */ public function delete_full($showfeedback = true) { global $CFG, $DB; require_once $CFG->libdir . '/gradelib.php'; require_once $CFG->libdir . '/questionlib.php'; require_once $CFG->dirroot . '/cohort/lib.php'; $deletedcourses = array(); // Get children. Note, we don't want to use cache here because // it would be rebuilt too often $children = $DB->get_records('course_categories', array('parent' => $this->id), 'sortorder ASC'); foreach ($children as $record) { $coursecat = new coursecat($record); $deletedcourses += $coursecat->delete_full($showfeedback); } if ($courses = $DB->get_records('course', array('category' => $this->id), 'sortorder ASC')) { foreach ($courses as $course) { if (!delete_course($course, false)) { throw new moodle_exception('cannotdeletecategorycourse', '', '', $course->shortname); } $deletedcourses[] = $course; } } // move or delete cohorts in this context cohort_delete_category($this); // now delete anything that may depend on course category context grade_course_category_delete($this->id, 0, $showfeedback); if (!question_delete_course_category($this, 0, $showfeedback)) { throw new moodle_exception('cannotdeletecategoryquestions', '', '', $this->get_formatted_name()); } // finally delete the category and it's context $DB->delete_records('course_categories', array('id' => $this->id)); delete_context(CONTEXT_COURSECAT, $this->id); add_to_log(SITEID, "category", "delete", "index.php", "{$this->name} (ID {$this->id})"); cache_helper::purge_by_event('changesincoursecat'); events_trigger('course_category_deleted', $this); // If we deleted $CFG->defaultrequestcategory, make it point somewhere else. if ($this->id == $CFG->defaultrequestcategory) { set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent' => 0))); } return $deletedcourses; }
/** * Remove stale context records * * @return bool */ function cleanup_contexts() { global $CFG; $sql = " SELECT c.contextlevel,\n c.instanceid AS instanceid\n FROM {$CFG->prefix}context c\n LEFT OUTER JOIN {$CFG->prefix}course_categories t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSECAT . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {$CFG->prefix}context c\n LEFT OUTER JOIN {$CFG->prefix}course t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSE . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {$CFG->prefix}context c\n LEFT OUTER JOIN {$CFG->prefix}course_modules t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_MODULE . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {$CFG->prefix}context c\n LEFT OUTER JOIN {$CFG->prefix}user t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_USER . "\n UNION\n SELECT c.contextlevel,\n c.instanceid\n FROM {$CFG->prefix}context c\n LEFT OUTER JOIN {$CFG->prefix}block_instance t\n ON c.instanceid = t.id\n WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_BLOCK . "\n "; if ($rs = get_recordset_sql($sql)) { begin_sql(); $tx = true; while ($tx && ($ctx = rs_fetch_next_record($rs))) { $tx = $tx && delete_context($ctx->contextlevel, $ctx->instanceid); } rs_close($rs); if ($tx) { commit_sql(); return true; } rollback_sql(); return false; rs_close($rs); } return true; }
/** * Delete a course module and any associated data at the course level (events) * Until 1.5 this function simply marked a deleted flag ... now it * deletes it completely. * * @deprecated since 2.5 * * @param int $id the course module id * @return boolean true on success, false on failure */ function delete_course_module($id) { debugging('Function delete_course_module() is deprecated. Please use course_delete_module() instead.', DEBUG_DEVELOPER); global $CFG, $DB; require_once $CFG->libdir . '/gradelib.php'; require_once $CFG->dirroot . '/blog/lib.php'; if (!($cm = $DB->get_record('course_modules', array('id' => $id)))) { return true; } $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module)); //delete events from calendar if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) { foreach ($events as $event) { delete_event($event->id); } } //delete grade items, outcome items and grades attached to modules if ($grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename, 'iteminstance' => $cm->instance, 'courseid' => $cm->course))) { foreach ($grade_items as $grade_item) { $grade_item->delete('moddelete'); } } // Delete completion and availability data; it is better to do this even if the // features are not turned on, in case they were turned on previously (these will be // very quick on an empty table) $DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id)); $DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id, 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY)); delete_context(CONTEXT_MODULE, $cm->id); return $DB->delete_records('course_modules', array('id' => $cm->id)); }
/** * Delete category, but move contents to another category. * @param object $ccategory * @param int $newparentid category id * @return bool status */ function category_delete_move($category, $newparentid, $showfeedback = true) { global $CFG; require_once $CFG->libdir . '/gradelib.php'; require_once $CFG->libdir . '/questionlib.php'; if (!($newparentcat = get_record('course_categories', 'id', $newparentid))) { return false; } if ($children = get_records('course_categories', 'parent', $category->id, 'sortorder ASC')) { foreach ($children as $childcat) { if (!move_category($childcat, $newparentcat)) { notify("Error moving category {$childcat->name}"); return false; } } } if ($courses = get_records('course', 'category', $category->id, 'sortorder ASC', 'id')) { if (!move_courses(array_keys($courses), $newparentid)) { notify("Error moving courses"); return false; } notify(get_string('coursesmovedout', '', format_string($category->name)), 'notifysuccess'); } // now delete anything that may depend on course category context grade_course_category_delete($category->id, $newparentid, $showfeedback); if (!question_delete_course_category($category, $newparentcat, $showfeedback)) { notify(get_string('errordeletingquestionsfromcategory', 'question', $category), 'notifysuccess'); return false; } // finally delete the category and it's context delete_records('course_categories', 'id', $category->id); delete_context(CONTEXT_COURSECAT, $category->id); events_trigger('course_category_deleted', $category); notify(get_string('coursecategorydeleted', '', format_string($category->name)), 'notifysuccess'); return true; }
/** * Delete all the blocks from a particular page. * * @param string $pagetype the page type. * @param integer $pageid the page id. * @return success of failure. */ function blocks_delete_all_on_page($pagetype, $pageid) { if ($instances = get_records_select('block_instance', "pageid = {$pageid} AND pagetype = '{$pagetype}'")) { foreach ($instances as $instance) { delete_context(CONTEXT_BLOCK, $instance->id); // Ingore any failures here. } } return delete_records('block_instance', 'pageid', $pageid, 'pagetype', $pagetype); }
public function delete() { $level = context_level_base::get_custom_context_level('course', 'block_curr_admin'); $return = curriculumcourse::delete_for_course($this->id); $return = $return && cmclass::delete_for_course($this->id); $return = $return && taginstance::delete_for_course($this->id); $return = $return && coursetemplate::delete_for_course($this->id); $return = $return && delete_context($level, $this->id); return $return && $this->data_delete_record(); }
/** * Delete category, but move contents to another category. * @param object $ccategory * @param int $newparentid category id * @return bool status */ function category_delete_move($category, $newparentid, $showfeedback = true) { global $CFG, $DB, $OUTPUT; require_once $CFG->libdir . '/gradelib.php'; require_once $CFG->libdir . '/questionlib.php'; require_once $CFG->dirroot . '/cohort/lib.php'; if (!($newparentcat = $DB->get_record('course_categories', array('id' => $newparentid)))) { return false; } if ($children = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) { foreach ($children as $childcat) { move_category($childcat, $newparentcat); } } if ($courses = $DB->get_records('course', array('category' => $category->id), 'sortorder ASC', 'id')) { if (!move_courses(array_keys($courses), $newparentid)) { echo $OUTPUT->notification("Error moving courses"); return false; } echo $OUTPUT->notification(get_string('coursesmovedout', '', format_string($category->name)), 'notifysuccess'); } // move or delete cohorts in this context cohort_delete_category($category); // now delete anything that may depend on course category context grade_course_category_delete($category->id, $newparentid, $showfeedback); if (!question_delete_course_category($category, $newparentcat, $showfeedback)) { echo $OUTPUT->notification(get_string('errordeletingquestionsfromcategory', 'question', $category), 'notifysuccess'); return false; } // finally delete the category and it's context $DB->delete_records('course_categories', array('id' => $category->id)); delete_context(CONTEXT_COURSECAT, $category->id); events_trigger('course_category_deleted', $category); echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($category->name)), 'notifysuccess'); return true; }
} // delete calendar events if (!$DB->delete_records("event", array("modulename" => $delete))) { echo $OUTPUT->notification("Error occurred while deleting all {$strmodulename} records in calendar event table"); } // clear course.modinfo for courses // that used this module... $sql = "UPDATE {course}\n SET modinfo=''\n WHERE id IN (SELECT DISTINCT course\n FROM {course_modules}\n WHERE module=?)"; $DB->execute($sql, array($module->id)); // Now delete all the course module records if (!$DB->delete_records("course_modules", array("module" => $module->id))) { echo $OUTPUT->notification("Error occurred while deleting all {$strmodulename} records in course_modules table"); } if ($coursemods) { foreach ($coursemods as $coursemod) { if (!delete_context(CONTEXT_MODULE, $coursemod->id)) { echo $OUTPUT->notification("Could not delete the context for {$strmodulename} with id = {$coursemod->id}"); } } } // Then delete all the logs if (!$DB->delete_records("log", array("module" => $module->name))) { echo $OUTPUT->notification("Error occurred while deleting all {$strmodulename} records in log table"); } // And log_display information if (!$DB->delete_records("log_display", array("module" => $module->name))) { echo $OUTPUT->notification("Error occurred while deleting all {$strmodulename} records in log_display table"); } // And the module entry itself if (!$DB->delete_records("modules", array("name" => $module->name))) { echo $OUTPUT->notification("Error occurred while deleting the {$strmodulename} record from modules table");
/** * Automatically clean-up all plugin data and remove the plugin DB tables * * @param string $type The plugin type, eg. 'mod', 'qtype', 'workshopgrading' etc. * @param string $name The plugin name, eg. 'forum', 'multichoice', 'accumulative' etc. * @uses global $OUTPUT to produce notices and other messages * @return void */ function uninstall_plugin($type, $name) { global $CFG, $DB, $OUTPUT; // recursively uninstall all module subplugins first if ($type === 'mod') { if (file_exists("{$CFG->dirroot}/mod/{$name}/db/subplugins.php")) { $subplugins = array(); include "{$CFG->dirroot}/mod/{$name}/db/subplugins.php"; foreach ($subplugins as $subplugintype => $dir) { $instances = get_plugin_list($subplugintype); foreach ($instances as $subpluginname => $notusedpluginpath) { uninstall_plugin($subplugintype, $subpluginname); } } } } $component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum' if ($type === 'mod') { $pluginname = $name; // eg. 'forum' if (get_string_manager()->string_exists('modulename', $component)) { $strpluginname = get_string('modulename', $component); } else { $strpluginname = $component; } } else { $pluginname = $component; if (get_string_manager()->string_exists('pluginname', $component)) { $strpluginname = get_string('pluginname', $component); } else { $strpluginname = $component; } } echo $OUTPUT->heading($pluginname); $plugindirectory = get_plugin_directory($type, $name); $uninstalllib = $plugindirectory . '/db/uninstall.php'; if (file_exists($uninstalllib)) { require_once $uninstalllib; $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()' if (function_exists($uninstallfunction)) { if (!$uninstallfunction()) { echo $OUTPUT->notification('Encountered a problem running uninstall function for ' . $pluginname); } } } if ($type === 'mod') { // perform cleanup tasks specific for activity modules if (!($module = $DB->get_record('modules', array('name' => $name)))) { print_error('moduledoesnotexist', 'error'); } // delete all the relevant instances from all course sections if ($coursemods = $DB->get_records('course_modules', array('module' => $module->id))) { foreach ($coursemods as $coursemod) { if (!delete_mod_from_section($coursemod->id, $coursemod->section)) { echo $OUTPUT->notification("Could not delete the {$strpluginname} with id = {$coursemod->id} from section {$coursemod->section}"); } } } // clear course.modinfo for courses that used this module $sql = "UPDATE {course}\n SET modinfo=''\n WHERE id IN (SELECT DISTINCT course\n FROM {course_modules}\n WHERE module=?)"; $DB->execute($sql, array($module->id)); // delete all the course module records $DB->delete_records('course_modules', array('module' => $module->id)); // delete module contexts if ($coursemods) { foreach ($coursemods as $coursemod) { if (!delete_context(CONTEXT_MODULE, $coursemod->id)) { echo $OUTPUT->notification("Could not delete the context for {$strpluginname} with id = {$coursemod->id}"); } } } // delete the module entry itself $DB->delete_records('modules', array('name' => $module->name)); // cleanup the gradebook require_once $CFG->libdir . '/gradelib.php'; grade_uninstalled_module($module->name); // Perform any custom uninstall tasks if (file_exists($CFG->dirroot . '/mod/' . $module->name . '/lib.php')) { require_once $CFG->dirroot . '/mod/' . $module->name . '/lib.php'; $uninstallfunction = $module->name . '_uninstall'; if (function_exists($uninstallfunction)) { debugging("{$uninstallfunction}() has been deprecated. Use the plugin's db/uninstall.php instead", DEBUG_DEVELOPER); if (!$uninstallfunction()) { echo $OUTPUT->notification('Encountered a problem running uninstall function for ' . $module->name . '!'); } } } } else { if ($type === 'enrol') { // NOTE: this is a bit brute force way - it will not trigger events and hooks properly // nuke all role assignments role_unassign_all(array('component' => $component)); // purge participants $DB->delete_records_select('user_enrolments', "enrolid IN (SELECT id FROM {enrol} WHERE enrol = ?)", array($name)); // purge enrol instances $DB->delete_records('enrol', array('enrol' => $name)); // tweak enrol settings if (!empty($CFG->enrol_plugins_enabled)) { $enabledenrols = explode(',', $CFG->enrol_plugins_enabled); $enabledenrols = array_unique($enabledenrols); $enabledenrols = array_flip($enabledenrols); unset($enabledenrols[$name]); $enabledenrols = array_flip($enabledenrols); if (is_array($enabledenrols)) { set_config('enrol_plugins_enabled', implode(',', $enabledenrols)); } } } else { if ($type === 'block') { if ($block = $DB->get_record('block', array('name' => $name))) { // Inform block it's about to be deleted if (file_exists("{$CFG->dirroot}/blocks/{$block->name}/block_{$block->name}.php")) { $blockobject = block_instance($block->name); if ($blockobject) { $blockobject->before_delete(); //only if we can create instance, block might have been already removed } } // First delete instances and related contexts $instances = $DB->get_records('block_instances', array('blockname' => $block->name)); foreach ($instances as $instance) { blocks_delete_instance($instance); } // Delete block $DB->delete_records('block', array('id' => $block->id)); } } } } // perform clean-up task common for all the plugin/subplugin types // delete calendar events $DB->delete_records('event', array('modulename' => $pluginname)); // delete all the logs $DB->delete_records('log', array('module' => $pluginname)); // delete log_display information $DB->delete_records('log_display', array('component' => $component)); // delete the module configuration records unset_all_config_for_plugin($pluginname); // delete message provider message_provider_uninstall($component); // delete message processor if ($type === 'message') { message_processor_uninstall($name); } // delete the plugin tables $xmldbfilepath = $plugindirectory . '/db/install.xml'; drop_plugin_tables($component, $xmldbfilepath, false); if ($type === 'mod' or $type === 'block') { // non-frankenstyle table prefixes drop_plugin_tables($name, $xmldbfilepath, false); } // delete the capabilities that were defined by this module capabilities_cleanup($component); // remove event handlers and dequeue pending events events_uninstall($component); echo $OUTPUT->notification(get_string('success'), 'notifysuccess'); }
/** * Removes all associations with a track, this entails removing * user track, cluster track and class track associations * @param none * @return none */ function delete() { // Cascade $level = context_level_base::get_custom_context_level('track', 'block_curr_admin'); $result = usertrack::delete_for_track($this->id); $result = $result && clustertrack::delete_for_track($this->id); $result = $result && trackassignmentclass::delete_for_track($this->id); $result = $result && delete_context($level, $this->id); return $result && $this->data_delete_record(); }