Skip to content

Commit

Permalink
MDL-14679 towards /admin/roles conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed May 31, 2008
1 parent 823e64a commit 9bec527
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 81 deletions.
9 changes: 5 additions & 4 deletions admin/roles/allowassign.php
Expand Up @@ -37,11 +37,11 @@
foreach ($roles as $srole) {
foreach ($roles as $trole) {
if (isset($temp[$srole->id][$trole->id])) { // if set, need to write to db
if (!$record = get_record('role_allow_assign', 'roleid', $srole->id, 'allowassign', $trole->id)) {
if (!$record = $DB->get_record('role_allow_assign', array('roleid'=>$srole->id, 'allowassign'=>$trole->id))) {
allow_assign($srole->id, $trole->id);
}
} else { //if set, means can access, attempt to remove it from db
delete_records('role_allow_assign', 'roleid', $srole->id, 'allowassign', $trole->id);
$DB->delete_records('role_allow_assign', array('roleid'=>$srole->id, 'allowassign'=>$trole->id));
}
}
}
Expand Down Expand Up @@ -88,10 +88,11 @@



function get_box_list($roleid, $arraylist){
function get_box_list($roleid, $arraylist) {
global $DB;

foreach ($arraylist as $targetid) {
if (get_record('role_allow_assign', 'roleid', $roleid, 'allowassign', $targetid)) {
if ($DB->get_record('role_allow_assign', array('roleid'=>$roleid, 'allowassign'=>$targetid))) {
$array[] = '<input type="checkbox" name="s_'.$roleid.'_'.$targetid.'" value="1" checked="checked"/>';
} else {
$array[] = '<input type="checkbox" name="s_'.$roleid.'_'.$targetid.'" value="1" />';
Expand Down
11 changes: 6 additions & 5 deletions admin/roles/allowoverride.php
Expand Up @@ -20,7 +20,7 @@

$roles = get_all_roles();

if ($grant = data_submitted()) {
if ($grant = data_submitted(false)) {

foreach ($grant as $grole => $val) {
if ($grole == 'dummy') {
Expand All @@ -35,11 +35,11 @@
foreach ($roles as $srole) {
foreach ($roles as $trole) {
if (isset($temp[$srole->id][$trole->id])) { // if set, need to write to db
if (!$record = get_record('role_allow_override', 'roleid', $srole->id, 'allowoverride', $trole->id)) {
if (!$record = $DB->get_record('role_allow_override', array('roleid'=>$srole->id, 'allowoverride'=>$trole->id))) {
allow_override($srole->id, $trole->id);
}
} else { //if set, means can access, attempt to remove it from db
delete_records('role_allow_override', 'roleid', $srole->id, 'allowoverride', $trole->id);
$DB->delete_records('role_allow_override', array('roleid'=>$srole->id, 'allowoverride'=>$trole->id));
}
}
}
Expand Down Expand Up @@ -85,10 +85,11 @@
admin_externalpage_print_footer();

// returns array
function get_box_list($roleid, $arraylist){
function get_box_list($roleid, $arraylist) {
global $DB;

foreach ($arraylist as $targetid) {
if (get_record('role_allow_override', 'roleid', $roleid, 'allowoverride', $targetid)) {
if ($DB->get_record('role_allow_override', array('roleid'=>$roleid, 'allowoverride'=>$targetid))) {
$array[] = '<input type="checkbox" name="s_'.$roleid.'_'.$targetid.'" value="1" checked="checked"/>';
} else {
$array[] = '<input type="checkbox" name="s_'.$roleid.'_'.$targetid.'" value="1" />';
Expand Down
4 changes: 2 additions & 2 deletions admin/roles/assign.html
Expand Up @@ -61,7 +61,7 @@
$i=0;
if (!empty($searchtext)) {
echo "<optgroup label=\"$strsearchresults (" . $usercount . ")\">\n";
while ($user = rs_fetch_next_record($availableusers)) {
foreach ($availableusers as $user) {
$fullname = fullname($user, true);
echo "<option value=\"$user->id\">".$fullname.", ".$user->email."</option>\n";
$i++;
Expand All @@ -73,7 +73,7 @@
echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
.'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
} else {
while ($user = rs_fetch_next_record($availableusers)) {
foreach ($availableusers as $user) {
$fullname = fullname($user, true);
echo "<option value=\"$user->id\">".$fullname.", ".$user->email."</option>\n";
$i++;
Expand Down
103 changes: 55 additions & 48 deletions admin/roles/assign.php
Expand Up @@ -41,14 +41,14 @@
$inmeta = 0;
if ($context->contextlevel == CONTEXT_COURSE) {
$courseid = $context->instanceid;
if ($course = get_record('course', 'id', $courseid)) {
if ($course = $DB->get_record('course', array('id'=>$courseid))) {
$inmeta = $course->metacourse;
} else {
print_error('invalidcourse', 'error');
}

} else if (!empty($courseid)){ // we need this for user tabs in user context
if (!$course = get_record('course', 'id', $courseid)) {
if (!$course = $DB->get_record('course', array('id', $courseid))) {
print_error('invalidcourse', 'error');
}

Expand Down Expand Up @@ -114,7 +114,7 @@
}

if ($userid) {
$user = get_record('user', 'id', $userid);
$user = $DB->get_record('user', array('id', $userid));
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
}

Expand Down Expand Up @@ -177,7 +177,7 @@
} else {
$managerroles = get_roles_with_capability('moodle/course:managemetacourse', CAP_ALLOW, $context);
if (!empty($managerroles) and !array_key_exists($roleid, $managerroles)) {
$erruser = get_record('user', 'id', $adduser, '','','','', 'id, firstname, lastname');
$erruser = $DB->get_record('user', array('id'=>$adduser), 'id, firstname, lastname');
$errors[] = get_string('metaassignerror', 'role', fullname($erruser));
$allow = false;
}
Expand Down Expand Up @@ -210,7 +210,7 @@
}
}

$rolename = get_field('role', 'name', 'id', $roleid);
$rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
} else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) {

Expand Down Expand Up @@ -239,14 +239,14 @@
sync_metacourse($courseid);
$newroles = get_user_roles($context, $removeuser, false);
if (!empty($newroles) and !array_key_exists($roleid, $newroles)) {
$erruser = get_record('user', 'id', $removeuser, '','','','', 'id, firstname, lastname');
$erruser = $DB->get_record('user', array('id'=>$removeuser), 'id, firstname, lastname');
$errors[] = get_string('metaunassignerror', 'role', fullname($erruser));
$allow = false;
}
}
}

$rolename = get_field('role', 'name', 'id', $roleid);
$rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
add_to_log($course->id, 'role', 'unassign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
} else if ($showall) {
$searchtext = '';
Expand Down Expand Up @@ -277,19 +277,19 @@
}

$select = "username <> 'guest' AND deleted = 0 AND confirmed = 1";
$params = array();

$usercount = count_records_select('user', $select) - count($contextusers);
$usercount = $DB->count_records_select('user', $select, $params) - count($contextusers);

$searchtext = trim($searchtext);

if ($searchtext !== '') { // Search for a subset of remaining users
$LIKE = sql_ilike();
$FULLNAME = sql_fullname();
$LIKE = $DB->sql_ilike();
$FULLNAME = $DB->sql_fullname();

$selectsql = " AND ($FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%') ";
$select .= $selectsql;
} else {
$selectsql = "";
$select .= " AND ($FULLNAME $LIKE :search1 OR email $LIKE :search2) ";
$params['search1'] = "%$searchtext%";
$params['search2'] = "%$searchtext%";
}

if ($context->contextlevel > CONTEXT_COURSE) { // mod or block (or group?)
Expand Down Expand Up @@ -327,27 +327,30 @@
if ($validroleids) {
$roleids = '('.implode(',', $validroleids).')';

$select = " SELECT u.id, u.firstname, u.lastname, u.email";
$countselect = "SELECT COUNT(u.id)";
$from = " FROM {$CFG->prefix}user u
INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid";
$where = " WHERE ra.contextid ".get_related_contexts_string($context)."
AND u.deleted = 0
AND ra.roleid in $roleids";
$excsql = " AND u.id NOT IN (
SELECT u.id
FROM {$CFG->prefix}role_assignments r,
{$CFG->prefix}user u
WHERE r.contextid = $contextid
AND u.id = r.userid
AND r.roleid = $roleid
$selectsql)";

$availableusers = get_recordset_sql($select . $from . $where . $selectsql . $excsql);
$fields = "SELECT u.id, u.firstname, u.lastname, u.email";
$countfields = "SELECT COUNT('x')";

$sql = " FROM {user} u
JOIN {role_assignments} ra ON ra.userid = u.id
JOIN {role} r ON r.id = ra.roleid
WHERE ra.contextid ".get_related_contexts_string($context)."
AND $select AND ra.roleid in $roleids
AND u.id NOT IN (
SELECT u.id
FROM {role_assignments} r, {user} u
WHERE r.contextid = :contextid
AND u.id = r.userid
AND r.roleid = :roleid)";
$params['contextid'] = $contextid;
$params['roleid'] = $roleid;

$availableusers = $DB->get_recordset_sql("$fields $sql", $params);
$usercount = $DB->count_records_sql("$countfields $sql", $params);

} else {
$availableusers = array();
$usercount = 0;
}

$usercount = $availableusers->_numOfRows;
}

} else {
Expand All @@ -361,20 +364,24 @@

/// MDL-11111 do not include user already assigned this role in this context as available users
/// so that the number of available users is right and we save time looping later
$availableusers = get_recordset_sql('SELECT id, firstname, lastname, email
FROM '.$CFG->prefix.'user
WHERE '.$select.'
AND id NOT IN (
SELECT u.id
FROM '.$CFG->prefix.'role_assignments r,
'.$CFG->prefix.'user u
WHERE r.contextid = '.$contextid.'
AND u.id = r.userid
AND r.roleid = '.$roleid.'
'.$selectsql.')
ORDER BY lastname ASC, firstname ASC');

$usercount = $availableusers->_numOfRows;
$fields = "SELECT id, firstname, lastname, email";
$countfields = "SELECT COUNT('x')";

$sql = " FROM {user}
WHERE $select
AND id NOT IN (
SELECT u.id
FROM {role_assignments} r, {user} u
WHERE r.contextid = :contextid
AND u.id = r.userid
AND r.roleid = :roleid)";
$order = "ORDER BY lastname ASC, firstname ASC";

$params['contextid'] = $contextid;
$params['roleid'] = $roleid;

$availableusers = $DB->get_recordset_sql("$fields $sql $order", $params);
$usercount = $DB->count_records_sql("$countfields $sql", $params);
}

echo '<div class="selector">';
Expand Down
12 changes: 6 additions & 6 deletions admin/roles/manage.php
Expand Up @@ -38,9 +38,9 @@
$rolesort[$i] = $rolex->id;
if ($rolex->sortorder != $i) {
$r = new object();
$r->id = $rolex->id;
$r->id = $rolex->id;
$r->sortorder = $i;
update_record('role', $r);
$DB->update_record('role', $r);
$roles[$rolex->id]->sortorder = $i;
}
$i++;
Expand All @@ -56,7 +56,7 @@
/// form processing, editing a role, adding a role, deleting a role etc.
switch ($action) {
case 'add':
if ($data = data_submitted() and confirm_sesskey()) {
if ($data = data_submitted(false) and confirm_sesskey()) {

$shortname = moodle_strtolower(clean_param(clean_filename($shortname), PARAM_SAFEDIR)); // only lowercase safe ASCII characters
$legacytype = required_param('legacytype', PARAM_RAW);
Expand Down Expand Up @@ -124,7 +124,7 @@
mark_context_dirty($sitecontext->path);

if (empty($errors)) {
$rolename = get_field('role', 'name', 'id', $newroleid);
$rolename = $DB->get_field('role', 'name', array('id'=>$newroleid));
add_to_log(SITEID, 'role', 'add', 'admin/roles/manage.php?action=add', $rolename, '', $USER->id);
redirect('manage.php');
}
Expand All @@ -133,7 +133,7 @@
break;

case 'edit':
if ($data = data_submitted() and confirm_sesskey()) {
if ($data = data_submitted(false) and confirm_sesskey()) {

$shortname = moodle_strtolower(clean_param(clean_filename($shortname), PARAM_SAFEDIR)); // only lowercase safe ASCII characters
$legacytype = required_param('legacytype', PARAM_RAW);
Expand Down Expand Up @@ -275,7 +275,7 @@
$a->id = $roleid;
$a->name = $roles[$roleid]->name;
$a->shortname = $roles[$roleid]->shortname;
$a->count = (int)count_records('role_assignments', 'roleid', $roleid);
$a->count = $DB->count_records('role_assignments', array('roleid'=>$roleid));
notice_yesno(get_string('deleterolesure', 'role', $a), 'manage.php', 'manage.php', $optionsyes, NULL, 'post', 'get');
admin_externalpage_print_footer();
die;
Expand Down
14 changes: 7 additions & 7 deletions admin/roles/override.php
Expand Up @@ -25,7 +25,7 @@
}

if ($courseid) {
if (!$course = get_record('course', 'id', $courseid)) {
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
print_error('invalidcourse');
}
} else {
Expand Down Expand Up @@ -67,18 +67,18 @@
}

if ($userid) {
$user = get_record('user', 'id', $userid);
$user = $DB->get_record('user', array('id'=>$userid));
$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
}

/// get all cababilities
$capabilities = fetch_context_capabilities($context);

/// Process incoming role override
if ($data = data_submitted() and $roleid and confirm_sesskey()) {
if ($data = data_submitted(false) and $roleid and confirm_sesskey()) {
$allowed_values = array(CAP_INHERIT, CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT);

$localoverrides = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id",
$localoverrides = $DB->get_records_select('role_capabilities', "roleid = ? AND contextid = ?", array($roleid, $context->id),
'', 'capability, permission, id');

foreach ($capabilities as $cap) {
Expand Down Expand Up @@ -110,7 +110,7 @@

// force accessinfo refresh for users visiting this context...
mark_context_dirty($context->path);
$rolename = get_field('role', 'name', 'id', $roleid);
$rolename = $DB->get_field('role', 'name', array('id'=>$roleid));
add_to_log($course->id, 'role', 'override', 'admin/roles/override.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
redirect($baseurl);
}
Expand Down Expand Up @@ -171,7 +171,7 @@

$r_caps = role_context_capabilities($roleid, $parentcontext);

$localoverrides = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id",
$localoverrides = $DB->get_records_select('role_capabilities', "roleid = ? AND contextid = ?", array($roleid, $context->id),
'', 'capability, permission, id');

$lang = str_replace('_utf8', '', current_language());
Expand Down Expand Up @@ -199,7 +199,7 @@

foreach ($overridableroles as $roleid => $rolename) {
$countusers = 0;
$overridecount = count_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id");
$overridecount = $DB->count_records_select('role_capabilities', "roleid = ? AND contextid = ?", array($roleid, $context->id));
$description = format_string(get_field('role', 'description', 'id', $roleid));
$table->data[] = array('<a href="'.$baseurl.'&amp;roleid='.$roleid.'">'.$rolename.'</a>', $description, $overridecount);
}
Expand Down

0 comments on commit 9bec527

Please sign in to comment.