Exemplo n.º 1
0
/**
 * Removes multiple role assignments, parameters may contain:
 *   'roleid', 'userid', 'contextid', 'component', 'enrolid'.
 *
 * @param array $params role assignment parameters
 * @param bool $subcontexts unassign in subcontexts too
 * @param bool $includemanual include manual role assignments too
 * @return void
 */
function role_unassign_all(array $params, $subcontexts = false, $includemanual = false)
{
    global $USER, $CFG, $DB;
    require_once $CFG->libdir . '/coursecatlib.php';
    if (!$params) {
        throw new coding_exception('Missing parameters in role_unsassign_all() call');
    }
    $allowed = array('roleid', 'userid', 'contextid', 'component', 'itemid');
    foreach ($params as $key => $value) {
        if (!in_array($key, $allowed)) {
            throw new coding_exception('Unknown role_unsassign_all() parameter key', 'key:' . $key);
        }
    }
    if (isset($params['component']) and $params['component'] !== '' and strpos($params['component'], '_') === false) {
        throw new coding_exception('Invalid component paramter in role_unsassign_all() call', 'component:' . $params['component']);
    }
    if ($includemanual) {
        if (!isset($params['component']) or $params['component'] === '') {
            throw new coding_exception('include manual parameter requires component parameter in role_unsassign_all() call');
        }
    }
    if ($subcontexts) {
        if (empty($params['contextid'])) {
            throw new coding_exception('subcontexts paramtere requires component parameter in role_unsassign_all() call');
        }
    }
    $ras = $DB->get_records('role_assignments', $params);
    foreach ($ras as $ra) {
        $DB->delete_records('role_assignments', array('id' => $ra->id));
        if ($context = context::instance_by_id($ra->contextid, IGNORE_MISSING)) {
            // this is a bit expensive but necessary
            $context->mark_dirty();
            // If the user is the current user, then do full reload of capabilities too.
            if (!empty($USER->id) && $USER->id == $ra->userid) {
                reload_all_capabilities();
            }
            $event = \core\event\role_unassigned::create(array('context' => $context, 'objectid' => $ra->roleid, 'relateduserid' => $ra->userid, 'other' => array('id' => $ra->id, 'component' => $ra->component, 'itemid' => $ra->itemid)));
            $event->add_record_snapshot('role_assignments', $ra);
            $event->trigger();
            coursecat::role_assignment_changed($ra->roleid, $context);
        }
    }
    unset($ras);
    // process subcontexts
    if ($subcontexts and $context = context::instance_by_id($params['contextid'], IGNORE_MISSING)) {
        if ($params['contextid'] instanceof context) {
            $context = $params['contextid'];
        } else {
            $context = context::instance_by_id($params['contextid'], IGNORE_MISSING);
        }
        if ($context) {
            $contexts = $context->get_child_contexts();
            $mparams = $params;
            foreach ($contexts as $context) {
                $mparams['contextid'] = $context->id;
                $ras = $DB->get_records('role_assignments', $mparams);
                foreach ($ras as $ra) {
                    $DB->delete_records('role_assignments', array('id' => $ra->id));
                    // this is a bit expensive but necessary
                    $context->mark_dirty();
                    // If the user is the current user, then do full reload of capabilities too.
                    if (!empty($USER->id) && $USER->id == $ra->userid) {
                        reload_all_capabilities();
                    }
                    $event = \core\event\role_unassigned::create(array('context' => $context, 'objectid' => $ra->roleid, 'relateduserid' => $ra->userid, 'other' => array('id' => $ra->id, 'component' => $ra->component, 'itemid' => $ra->itemid)));
                    $event->add_record_snapshot('role_assignments', $ra);
                    $event->trigger();
                    coursecat::role_assignment_changed($ra->roleid, $context);
                }
            }
        }
    }
    // do this once more for all manual role assignments
    if ($includemanual) {
        $params['component'] = '';
        role_unassign_all($params, $subcontexts, false);
    }
}
Exemplo n.º 2
0
/**
 * Save the Your name for 'Some role' strings.
 *
 * @param integer $courseid the id of this course.
 * @param array $data the data that came from the course settings form.
 */
function save_local_role_names($courseid, $data)
{
    global $DB;
    $context = context_course::instance($courseid);
    foreach ($data as $fieldname => $value) {
        if (strpos($fieldname, 'role_') !== 0) {
            continue;
        }
        list($ignored, $roleid) = explode('_', $fieldname);
        // make up our mind whether we want to delete, update or insert
        if (!$value) {
            $DB->delete_records('role_names', array('contextid' => $context->id, 'roleid' => $roleid));
        } else {
            if ($rolename = $DB->get_record('role_names', array('contextid' => $context->id, 'roleid' => $roleid))) {
                $rolename->name = $value;
                $DB->update_record('role_names', $rolename);
            } else {
                $rolename = new stdClass();
                $rolename->contextid = $context->id;
                $rolename->roleid = $roleid;
                $rolename->name = $value;
                $DB->insert_record('role_names', $rolename);
            }
        }
        // This will ensure the course contacts cache is purged..
        coursecat::role_assignment_changed($roleid, $context);
    }
}
 public function save_changes()
 {
     global $DB, $CFG;
     if (!$this->roleid) {
         // Creating role.
         $this->role->id = create_role($this->role->name, $this->role->shortname, $this->role->description, $this->role->archetype);
         $this->roleid = $this->role->id;
         // Needed to make the parent::save_changes(); call work.
     } else {
         // Updating role.
         $DB->update_record('role', $this->role);
         // This will ensure the course contacts cache is purged so name changes get updated in
         // the UI. It would be better to do this only when we know that fields affected are
         // updated. But thats getting into the weeds of the coursecat cache and role edits
         // should not be that frequent, so here is the ugly brutal approach.
         require_once $CFG->libdir . '/coursecatlib.php';
         coursecat::role_assignment_changed($this->role->id, context_system::instance());
     }
     // Assignable contexts.
     set_role_contextlevels($this->role->id, $this->contextlevels);
     // Set allowed roles.
     $this->save_allow('assign');
     $this->save_allow('override');
     $this->save_allow('switch');
     // Permissions.
     parent::save_changes();
 }