Beispiel #1
0
 public function execute()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . DIRECTORY_SEPARATOR . "accesslib.php";
     $options = $this->expandedOptions;
     $arguments = $this->arguments;
     //execute method
     //delete by id?
     if ($options['id']) {
         $role = $DB->get_record('role', array('id' => $arguments[0]));
         if (!$role) {
             echo "Role with id '" . $arguments[0] . "' does not exist\n";
             exit(0);
         }
         delete_role($arguments[0]);
     } else {
         $role = $DB->get_record('role', array('shortname' => $arguments[0]));
         if (!$role) {
             echo "Role '" . $arguments[0] . "' does not exist.\n";
             exit(0);
         }
         delete_role($role->id);
     }
     echo "\n";
 }
/**
 * @package mod
 * @subpackage adobeconnect
 * @author Akinsaya Delamarre (adelamarre@remote-learner.net)
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
function xmldb_adobeconnect_uninstall()
{
    global $DB;
    $result = true;
    $param = array('shortname' => 'adobeconnectparticipant');
    if ($mrole = $DB->get_record('role', $param)) {
        $result = $result && delete_role($mrole->id);
    }
    $param = array('shortname' => 'adobeconnectpresenter');
    if ($mrole = $DB->get_record('role', $param)) {
        $result = $result && delete_role($mrole->id);
    }
    $param = array('shortname' => 'adobeconnecthost');
    if ($mrole = $DB->get_record('role', $param)) {
        $result = $result && delete_role($mrole->id);
    }
    return $result;
}
/**
 * @package block
 * @subpackage demostudent
 * @author Dominik Royko royko@ualberta.ca
 */
function xmldb_block_demostudent_uninstall()
{
    global $DB;
    $result = true;
    // Delete all traces of the DemoStudent role.
    if ($cruftyrole = $DB->get_record('role', array('shortname' => 'demostudent'))) {
        $result = $result && delete_role($cruftyrole->id);
    }
    // And delete the capabilities specific to the block.
    // Would have expected this to happen automatically, but it didn't seem to.
    $capparam = array('capability' => 'block/demostudent:myaddinstance');
    if ($cruftycapability = $DB->get_records('role_capabilities', $capparam)) {
        $result = $result && $DB->delete_records('role_capabilities', $capparam);
    }
    $capparam = array('capability' => 'block/demostudent:seedemostudentblock');
    if ($cruftycapability = $DB->get_records('role_capabilities', $capparam)) {
        $result = $result && $DB->delete_records('role_capabilities', $capparam);
    }
    return $result;
}
 /**
  * Test deleting of roles.
  */
 public function test_delete_role()
 {
     global $DB;
     $this->resetAfterTest();
     $role = $DB->get_record('role', array('shortname' => 'manager'), '*', MUST_EXIST);
     $user = $this->getDataGenerator()->create_user();
     role_assign($role->id, $user->id, context_system::instance());
     $course = $this->getDataGenerator()->create_course();
     $rolename = (object) array('roleid' => $role->id, 'name' => 'Man', 'contextid' => context_course::instance($course->id)->id);
     $DB->insert_record('role_names', $rolename);
     $this->assertTrue($DB->record_exists('role_assignments', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_capabilities', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_names', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_context_levels', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_allow_assign', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_allow_assign', array('allowassign' => $role->id)));
     $this->assertTrue($DB->record_exists('role_allow_override', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_allow_override', array('allowoverride' => $role->id)));
     // Delete role and get event.
     $sink = $this->redirectEvents();
     $result = delete_role($role->id);
     $events = $sink->get_events();
     $sink->close();
     $event = array_pop($events);
     $this->assertTrue($result);
     $this->assertFalse($DB->record_exists('role', array('id' => $role->id)));
     $this->assertFalse($DB->record_exists('role_assignments', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_capabilities', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_names', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_context_levels', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_allow_assign', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_allow_assign', array('allowassign' => $role->id)));
     $this->assertFalse($DB->record_exists('role_allow_override', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_allow_override', array('allowoverride' => $role->id)));
     // Test triggered event.
     $this->assertInstanceOf('\\core\\event\\role_deleted', $event);
     $this->assertSame('role', $event->target);
     $this->assertSame('role', $event->objecttable);
     $this->assertSame($role->id, $event->objectid);
     $this->assertEquals(context_system::instance(), $event->get_context());
     $this->assertSame($role->shortname, $event->other['shortname']);
     $this->assertSame($role->description, $event->other['description']);
     $this->assertSame($role->archetype, $event->other['archetype']);
     $expectedlegacylog = array(SITEID, 'role', 'delete', 'admin/roles/manage.php?action=delete&roleid=' . $role->id, $role->shortname, '');
     $this->assertEventLegacyLogData($expectedlegacylog, $event);
 }
Beispiel #5
0
 /**
  * Test deleting of roles.
  * @return void
  */
 public function test_delete_role()
 {
     global $DB;
     $this->resetAfterTest();
     $role = $DB->get_record('role', array('shortname' => 'manager'), '*', MUST_EXIST);
     $user = $this->getDataGenerator()->create_user();
     role_assign($role->id, $user->id, context_system::instance());
     $course = $this->getDataGenerator()->create_course();
     $rolename = (object) array('roleid' => $role->id, 'name' => 'Man', 'contextid' => context_course::instance($course->id)->id);
     $DB->insert_record('role_names', $rolename);
     $this->assertTrue($DB->record_exists('role_assignments', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_capabilities', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_names', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_context_levels', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_allow_assign', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_allow_assign', array('allowassign' => $role->id)));
     $this->assertTrue($DB->record_exists('role_allow_override', array('roleid' => $role->id)));
     $this->assertTrue($DB->record_exists('role_allow_override', array('allowoverride' => $role->id)));
     $result = delete_role($role->id);
     $this->assertTrue($result);
     $this->assertFalse($DB->record_exists('role', array('id' => $role->id)));
     $this->assertFalse($DB->record_exists('role_assignments', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_capabilities', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_names', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_context_levels', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_allow_assign', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_allow_assign', array('allowassign' => $role->id)));
     $this->assertFalse($DB->record_exists('role_allow_override', array('roleid' => $role->id)));
     $this->assertFalse($DB->record_exists('role_allow_override', array('allowoverride' => $role->id)));
 }
Beispiel #6
0
     if (!$confirmed) {
         // Show confirmation.
         echo $OUTPUT->header();
         $optionsyes = array('action' => 'delete', 'roleid' => $roleid, 'sesskey' => sesskey(), 'confirm' => 1);
         $a = new stdClass();
         $a->id = $roleid;
         $a->name = $roles[$roleid]->name;
         $a->shortname = $roles[$roleid]->shortname;
         $a->count = $DB->count_records_select('role_assignments', 'roleid = ?', array($roleid), 'COUNT(DISTINCT userid)');
         $formcontinue = new single_button(new moodle_url($baseurl, $optionsyes), get_string('yes'));
         $formcancel = new single_button(new moodle_url($baseurl), get_string('no'), 'get');
         echo $OUTPUT->confirm(get_string('deleterolesure', 'core_role', $a), $formcontinue, $formcancel);
         echo $OUTPUT->footer();
         die;
     }
     if (!delete_role($roleid)) {
         // The delete failed, but mark the context dirty in case.
         $systemcontext->mark_dirty();
         print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
     }
     // Deleted a role sitewide...
     $systemcontext->mark_dirty();
     redirect($baseurl);
     break;
 case 'moveup':
     if (confirm_sesskey()) {
         $prevrole = null;
         $thisrole = null;
         foreach ($roles as $role) {
             if ($role->id == $roleid) {
                 $thisrole = $role;
Beispiel #7
0
    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));

    }
Beispiel #8
0
<?php

$semid = isset($_POST['semid']) ? $_POST['semid'] : '';
$semester = semester_load($semid);
if (isset($_POST['submit'])) {
    delete_role($_POST['id']);
    sleep(1);
    header('location: ' . currentURL() . '?p=semester');
}
?>
<h3>Do you want to delete the semester "<?php 
print $semester['Semester_Code'];
?>
"?</h3>
<form method="post" action="">
	<input type="hidden" name="id" value="<?php 
print $semid;
?>
"/>
	<input type="submit" name="submit" value="Delete" />
	<button onclick="history.go(-1);return false;">Cancel</button>
</form>
Beispiel #9
0
/**
* When called resets all custom roles as per definition set down in /local/roles.php
*
* Note that this uses the non-core role.custom field to isolate roles to remove.
*
* Utilise the $path parameter to allow for localisation (i.e. different roles defintion than core).
*
* Sort order is reset based on the order listed in the defintion.
*
* WARNING: as long as you retain the same shortname existing user role assigments will
*             be retained.  if you change the shortname they will be lost.
*
* KNOWN ISSUE: we rely on shortname being unique, but this is not enforced by the db.
*                       this is more a problem with moodle.
*
* @param text $path
*
*/
function tao_reset_custom_roles($path = 'local')
{
    global $CFG;
    if (!get_site()) {
        // not finished installing, skip
        return true;
    }
    // get latest role definition from roles file
    $rolespath = $CFG->dirroot . '/' . $path . '/roles.php';
    if (!file_exists($rolespath)) {
        debugging("Local caps reassignment called with invalid path {$path}");
        return false;
    }
    require_once $rolespath;
    if (!isset($customroles)) {
        return true;
        // nothing to do.
    }
    $undeletableroles = array();
    $undeletableroles[$CFG->notloggedinroleid] = 1;
    $undeletableroles[$CFG->guestroleid] = 1;
    $undeletableroles[$CFG->defaultuserroleid] = 1;
    $undeletableroles[$CFG->defaultcourseroleid] = 1;
    // If there is only one admin role, add that to $undeletableroles too.
    $adminroles = get_admin_roles();
    if (count($adminroles) == 1) {
        $undeletableroles[reset($adminroles)->id] = 1;
    }
    // get recordset of existing custom roles
    $sql = "SELECT id, name, shortname, description, sortorder, custom\n              FROM {$CFG->prefix}role\n              WHERE custom IS NOT NULL";
    $roles = get_records_sql($sql);
    // remove custom roles that are not in the latest definition
    foreach ($roles as $role) {
        // check whether this role is in the latest definition
        if (array_key_exists($role->shortname, $customroles)) {
            continue;
        }
        // extra safety: check undeletable roles
        if (isset($undeletableroles[$role->id])) {
            continue;
        }
        delete_role($role->id);
    }
    // hack to avoid sortorder unique constraint
    execute_sql("UPDATE {$CFG->prefix}role SET sortorder = (sortorder+1000) WHERE custom IS NOT NULL");
    // set sortorder to current highest value
    $sortorder = get_field_sql("SELECT " . sql_max('sortorder') . " FROM {$CFG->prefix}role WHERE custom IS NULL");
    // now loop through the new settings
    foreach ($customroles as $shortname => $role) {
        $sortorder++;
        // get the roleid
        $roleid = get_field('role', 'id', 'shortname', $shortname);
        // if exists then make updates
        if (!empty($roleid)) {
            // only update fields that have been set
            if (isset($role['name'])) {
                set_field('role', 'name', $role['name'], 'shortname', $shortname);
            }
            if (isset($role['description'])) {
                set_field('role', 'description', $role['description'], 'shortname', $shortname);
            }
            // reset sortorder
            set_field('role', 'sortorder', $sortorder, 'shortname', $shortname);
            // else create record
        } else {
            $newrole = new stdclass();
            $newrole->name = $role['name'];
            $newrole->shortname = $shortname;
            $newrole->description = $role['description'];
            $newrole->sortorder = $sortorder;
            $newrole->custom = 1;
            $roleid = insert_record('role', $newrole);
        }
        // remove any previously set legacy roles
        $legacyroles = get_legacy_roles();
        foreach ($legacyroles as $ltype => $lcap) {
            unassign_capability($lcap, $roleid);
        }
        // reset legacy role
        if (isset($role['legacy'])) {
            $legacycap = $legacyroles[$role['legacy']];
            $context = get_context_instance(CONTEXT_SYSTEM);
            assign_capability($legacycap, CAP_ALLOW, $roleid, $context->id);
        }
        // update the context settings
        set_role_contextlevels($roleid, $role['context']);
        //  e.g. array(CONTEXT_SYSTEM, CONTEXT_COURSECAT)
        // set allow assigns
        if (is_array($role['canassign'])) {
            // delete existing
            delete_records('role_allow_assign', 'allowassign', $roleid);
            foreach ($role['canassign'] as $canassign) {
                $canassignid = get_field('role', 'id', 'shortname', $canassign);
                allow_assign($canassignid, $roleid);
            }
        }
    }
    // reset custom capabilities to keep up with changes
    return tao_reset_capabilities();
}
Beispiel #10
0
/**
 * Execute post-uninstall custom actions for the module
 * This function was added in 1.9
 *
 * @return boolean true if success, false on error
 */
function adobeconnect_uninstall()
{
    $result = true;
    if ($mrole = get_record('role', 'shortname', 'adobeconnectparticipant')) {
        $result = $result && delete_role($mrole->id);
        $result = $result && delete_records('role_allow_assign', 'allowassign', $mrole->id);
    }
    if ($prole = get_record('role', 'shortname', 'adobeconnectpresenter')) {
        $result = $result && delete_role($prole->id);
        $result = $result && delete_records('role_allow_assign', 'allowassign', $prole->id);
    }
    if ($prole = get_record('role', 'shortname', 'adobeconnecthost')) {
        $result = $result && delete_role($prole->id);
        $result = $result && delete_records('role_allow_assign', 'allowassign', $prole->id);
    }
    return $result;
}
/**
 * Clean up the roles we created during install.
 *
 * @param none
 * @return bool True on success, False otherwise.
 */
function elluminate_uninstall()
{
    $result = true;
    if ($mrole = get_record('role', 'shortname', 'elluminatemoderator')) {
        $result = $result && delete_role($mrole->id);
        $result = $result && delete_records('role_allow_assign', 'allowassign', $mrole->id);
    }
    if ($prole = get_record('role', 'shortname', 'elluminateparticipant')) {
        $result = $result && delete_role($prole->id);
        $result = $result && delete_records('role_allow_assign', 'allowassign', $prole->id);
    }
    return $result;
}