Skip to content

Commit

Permalink
Merge branch 'w28_MDL-8249_m24_roletranslations' of git://github.com/…
Browse files Browse the repository at this point in the history
…skodak/moodle

Conflicts:
	lib/db/upgrade.php
	version.php
  • Loading branch information
danpoltawski committed Jul 10, 2012
2 parents c4f48b5 + a2dd56d commit e1980f8
Show file tree
Hide file tree
Showing 37 changed files with 631 additions and 422 deletions.
48 changes: 22 additions & 26 deletions admin/roles/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,14 @@ class permissions_table extends capability_table_base {
* @param string $contextname print_context_name($context) - to save recomputing.
*/
public function __construct($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles) {
global $DB;

parent::__construct($context, 'permissions');
$this->contextname = $contextname;
$this->allowoverrides = $allowoverrides;
$this->allowsafeoverrides = $allowsafeoverrides;
$this->overridableroles = $overridableroles;

$roles = $DB->get_records('role', null, 'sortorder DESC');
foreach ($roles as $roleid=>$role) {
$roles[$roleid] = $role->name;
}
$this->roles = role_fix_names($roles, $context);
$roles = get_all_roles($context);
$this->roles = role_fix_names(array_reverse($roles, true), $context, ROLENAME_ALIAS, true);

}

Expand Down Expand Up @@ -593,18 +588,6 @@ public function read_submitted_permissions() {
global $DB;
$this->errors = array();

// Role name.
$name = optional_param('name', null, PARAM_MULTILANG);
if (!is_null($name)) {
$this->role->name = $name;
if (html_is_blank($this->role->name)) {
$this->errors['name'] = get_string('errorbadrolename', 'role');
}
}
if ($DB->record_exists_select('role', 'name = ? and id <> ?', array($this->role->name, $this->roleid))) {
$this->errors['name'] = get_string('errorexistsrolename', 'role');
}

// Role short name. We clean this in a special way. We want to end up
// with only lowercase safe ASCII characters.
$shortname = optional_param('shortname', null, PARAM_RAW);
Expand All @@ -620,6 +603,20 @@ public function read_submitted_permissions() {
$this->errors['shortname'] = get_string('errorexistsroleshortname', 'role');
}

// Role name.
$name = optional_param('name', null, PARAM_MULTILANG);
if (!is_null($name)) {
$this->role->name = $name;
// Hack: short names of standard roles are equal to archetypes, empty name means localised via lang packs.
$archetypes = get_role_archetypes();
if (!isset($archetypes[$shortname]) and html_is_blank($this->role->name)) {
$this->errors['name'] = get_string('errorbadrolename', 'role');
}
}
if ($this->role->name !== '' and $DB->record_exists_select('role', 'name = ? and id <> ?', array($this->role->name, $this->roleid))) {
$this->errors['name'] = get_string('errorexistsrolename', 'role');
}

// Description.
$description = optional_param('description', null, PARAM_RAW);
if (!is_null($description)) {
Expand Down Expand Up @@ -779,9 +776,9 @@ public function display() {
global $OUTPUT;
// Extra fields at the top of the page.
echo '<div class="topfields clearfix">';
$this->print_field('name', get_string('rolefullname', 'role'), $this->get_name_field('name'));
$this->print_field('shortname', get_string('roleshortname', 'role'), $this->get_shortname_field('shortname'));
$this->print_field('edit-description', get_string('description'), $this->get_description_field('description'));
$this->print_field('shortname', get_string('roleshortname', 'role').'&nbsp;'.$OUTPUT->help_icon('roleshortname', 'role'), $this->get_shortname_field('shortname'));
$this->print_field('name', get_string('customrolename', 'role').'&nbsp;'.$OUTPUT->help_icon('customrolename', 'role'), $this->get_name_field('name'));
$this->print_field('edit-description', get_string('customroledescription', 'role').'&nbsp;'.$OUTPUT->help_icon('customroledescription', 'role'), $this->get_description_field('description'));
$this->print_field('menuarchetype', get_string('archetype', 'role').'&nbsp;'.$OUTPUT->help_icon('archetype', 'role'), $this->get_archetype_field('archetype'));
$this->print_field('', get_string('maybeassignedin', 'role'), $this->get_assignable_levels_control());
echo "</div>";
Expand Down Expand Up @@ -862,15 +859,15 @@ public function save_changes() {
}

protected function get_name_field($id) {
return strip_tags(format_string($this->role->name));
return role_get_name($this->role);
}

protected function get_shortname_field($id) {
return $this->role->shortname;
}

protected function get_description_field($id) {
return format_text($this->role->description, FORMAT_HTML);
return role_get_description($this->role);
}

protected function get_archetype_field($id) {
Expand Down Expand Up @@ -1273,8 +1270,7 @@ public function __construct($tablename, $targetcolname) {
*/
protected function load_required_roles() {
/// Get all roles
$this->roles = get_all_roles();
role_fix_names($this->roles, get_context_instance(CONTEXT_SYSTEM), ROLENAME_ORIGINAL);
$this->roles = role_fix_names(get_all_roles(), get_context_instance(CONTEXT_SYSTEM), ROLENAME_ORIGINAL);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions admin/roles/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
admin_externalpage_setup('defineroles');

/// Get some basic data we are going to need.
$roles = get_all_roles();
role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
$roles = role_fix_names(get_all_roles(), $systemcontext, ROLENAME_ORIGINAL);

$undeletableroles = array();
$undeletableroles[$CFG->notloggedinroleid] = 1;
Expand Down Expand Up @@ -214,7 +213,7 @@
/// Basic data.
$row = array(
'<a href="' . $defineurl . '?action=view&amp;roleid=' . $role->id . '">' . $role->localname . '</a>',
format_text($role->description, FORMAT_HTML),
role_get_description($role),
s($role->shortname),
'',
);
Expand Down
21 changes: 9 additions & 12 deletions admin/roles/usersroles.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,20 @@
/// Now get the role assignments for this user.
$sql = "SELECT
ra.id, ra.userid, ra.contextid, ra.roleid, ra.component, ra.itemid,
c.path,
r.name AS rolename,
COALESCE(rn.name, r.name) AS localname
c.path
FROM
{role_assignments} ra
JOIN {context} c ON ra.contextid = c.id
JOIN {role} r ON ra.roleid = r.id
LEFT JOIN {role_names} rn ON rn.roleid = ra.roleid AND rn.contextid = ra.contextid
WHERE
ra.userid = ?
"./*AND ra.active = 1*/"
ORDER BY
contextlevel DESC, contextid ASC, r.sortorder ASC";
$roleassignments = $DB->get_records_sql($sql, array($user->id));

$allroles = role_fix_names(get_all_roles());

/// In order to display a nice tree of contexts, we need to get all the
/// ancestors of all the contexts in the query we just did.
$requiredcontexts = array();
Expand Down Expand Up @@ -142,14 +141,14 @@
if (!$roleassignments) {
echo '<p>', get_string('noroleassignments', 'role'), '</p>';
} else {
print_report_tree($systemcontext->id, $contexts, $systemcontext, $fullname);
print_report_tree($systemcontext->id, $contexts, $systemcontext, $fullname, $allroles);
}

/// End of page.
echo $OUTPUT->box_end();
echo $OUTPUT->footer();

function print_report_tree($contextid, $contexts, $systemcontext, $fullname) {
function print_report_tree($contextid, $contexts, $systemcontext, $fullname, $allroles) {
global $CFG, $OUTPUT;

// Only compute lang strings, etc once.
Expand All @@ -170,15 +169,13 @@ function print_report_tree($contextid, $contexts, $systemcontext, $fullname) {

// If there are any role assignments here, print them.
foreach ($contexts[$contextid]->roleassignments as $ra) {
$role = $allroles[$ra->roleid];

$value = $ra->contextid . ',' . $ra->roleid;
$inputid = 'unassign' . $value;

echo '<p>';
if ($ra->rolename == $ra->localname) {
echo strip_tags(format_string($ra->localname));
} else {
echo strip_tags(format_string($ra->localname . ' (' . $ra->rolename . ')'));
}
echo $role->localname;
if (has_capability('moodle/role:assign', $context)) {
$raurl = $assignurl . '?contextid=' . $ra->contextid . '&amp;roleid=' .
$ra->roleid . '&amp;removeselect[]=' . $ra->userid;
Expand Down Expand Up @@ -210,7 +207,7 @@ function print_report_tree($contextid, $contexts, $systemcontext, $fullname) {
echo '<ul>';
foreach ($contexts[$contextid]->children as $childcontextid) {
echo '<li>';
print_report_tree($childcontextid, $contexts, $systemcontext, $fullname);
print_report_tree($childcontextid, $contexts, $systemcontext, $fullname, $allroles);
echo '</li>';
}
echo '</ul>';
Expand Down
5 changes: 3 additions & 2 deletions admin/settings/frontpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@
// front page default role
$options = array(0=>new lang_string('none')); // roles to choose from
$defaultfrontpageroleid = 0;
foreach (get_all_roles() as $role) {
$roles = role_fix_names(get_all_roles(), null, ROLENAME_ORIGINALANDSHORT);
foreach ($roles as $role) {
if (empty($role->archetype) or $role->archetype === 'guest' or $role->archetype === 'frontpage' or $role->archetype === 'student') {
$options[$role->id] = strip_tags(format_string($role->name)) . ' ('. $role->shortname . ')';
$options[$role->id] = $role->localname;
if ($role->archetype === 'frontpage') {
$defaultfrontpageroleid = $role->id;
}
Expand Down
5 changes: 3 additions & 2 deletions admin/settings/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
$defaultuserid = null;
$defaultguestid = null;

foreach (get_all_roles() as $role) {
$rolename = strip_tags(format_string($role->name)) . ' ('. $role->shortname . ')';
$roles = role_fix_names(get_all_roles(), null, ROLENAME_ORIGINALANDSHORT);
foreach ($roles as $role) {
$rolename = $role->localname;
switch ($role->archetype) {
case 'manager':
$creatornewroles[$role->id] = $rolename;
Expand Down
8 changes: 4 additions & 4 deletions admin/tool/capability/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

// Clean the passed in list of role ids. If 'All' selected as an option, or
// if none were selected, do all roles.
$allroles = get_all_roles();
$allroles = role_fix_names(get_all_roles());
$cleanedroleids = array();
foreach ($roleids as $roleid) {
if ($roleid == 0) {
Expand Down Expand Up @@ -73,7 +73,7 @@
// Prepare the list of roles to choose from
$rolechoices = array('0' => get_string('all'));
foreach ($allroles as $role) {
$rolechoices[$role->id] = $role->name;
$rolechoices[$role->id] = $role->localname;
}
if (count($cleanedroleids) == count($allroles)) {
// Select 'All', rather than each role individually.
Expand Down Expand Up @@ -162,7 +162,7 @@
if (count($cleanedroleids) != count($allroles)) {
$rolenames = array();
foreach ($cleanedroleids as $roleid) {
$rolenames[] = $allroles[$roleid]->name;
$rolenames[] = $allroles[$roleid]->localname;
}
echo '<p>', get_string('forroles', 'tool_capability', implode(', ', $rolenames)), '</p>';
}
Expand Down Expand Up @@ -207,7 +207,7 @@ function print_report_tree($contextid, $contexts, $allroles) {
foreach ($allroles as $role) {
if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
$permission = $contexts[$contextid]->rolecapabilities[$role->id];
echo '<tr class="r' . ($rowcounter % 2) . '"><th class="cell">', $role->name,
echo '<tr class="r' . ($rowcounter % 2) . '"><th class="cell">', $role->localname,
'</th><td class="cell">' . $strpermissions[$permission] . '</td></tr>';
$rowcounter++;
}
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/unsuproles/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
$data = array();
foreach ($problems as $problem) {
$levelname = get_contextlevel_name($problem->contextlevel);
$rolename = format_string($roles[$problem->roleid]->name);
$rolename = role_get_name($roles[$problem->roleid]);
//TODO: show list of users if count low
$count = $problem->racount;
$edit = array();
Expand Down
7 changes: 7 additions & 0 deletions auth/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ This files describes API changes in /auth/* - plugins,
information provided here is intended especially for developers.


=== 2.4 ===

required changes in code:
* use role_get_name() or role_fix_names() if you need any role names, using role.name
directly from database is not correct any more


=== 2.2 ===

required changes in code:
Expand Down
1 change: 0 additions & 1 deletion backup/util/dbops/restore_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ protected static function get_best_assignable_role($role, $courseid, $userid, $s

// Gather various information about roles
$coursectx = get_context_instance(CONTEXT_COURSE, $courseid);
$allroles = $DB->get_records('role');
$assignablerolesshortname = get_assignable_roles($coursectx, ROLENAME_SHORT, false, $userid);

// Note: under 1.9 we had one function restore_samerole() that performed one complete
Expand Down
6 changes: 6 additions & 0 deletions course/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
$editoroptions['context'] = $coursecontext;
$course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0);

// Inject current aliases
$aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
foreach($aliases as $alias) {
$course->{'role_'.$alias->roleid} = $alias->name;
}

} else {
//editor should respect category context if course context is not set.
$editoroptions['context'] = $catcontext;
Expand Down
9 changes: 2 additions & 7 deletions course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,10 @@ function definition() {
$mform->addHelpButton('rolerenaming', 'rolerenaming');

if ($roles = get_all_roles()) {
if ($coursecontext) {
$roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
}
$roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
$assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
foreach ($roles as $role) {
$mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
if (isset($role->localname)) {
$mform->setDefault('role_'.$role->id, $role->localname);
}
$mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
$mform->setType('role_'.$role->id, PARAM_TEXT);
if (!in_array($role->id, $assignableroles)) {
$mform->setAdvanced('role_'.$role->id);
Expand Down
9 changes: 6 additions & 3 deletions course/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@
if (!empty($CFG->coursecontact)) {
$coursecontactroles = explode(',', $CFG->coursecontact);
foreach ($coursecontactroles as $roleid) {
$role = $DB->get_record('role', array('id'=>$roleid));
$roleid = (int) $roleid;
if ($users = get_role_users($roleid, $context, true)) {
foreach ($users as $teacher) {
$role = new stdClass();
$role->id = $teacher->roleid;
$role->name = $teacher->rolename;
$role->shortname = $teacher->roleshortname;
$role->coursealias = $teacher->rolecoursealias;
$fullname = fullname($teacher, has_capability('moodle/site:viewfullnames', $context));
$namesarray[] = format_string(role_get_name($role, $context)).': <a href="'.$CFG->wwwroot.'/user/view.php?id='.
$namesarray[] = role_get_name($role, $context).': <a href="'.$CFG->wwwroot.'/user/view.php?id='.
$teacher->id.'&amp;course='.SITEID.'">'.$fullname.'</a>';
}
}
Expand Down
33 changes: 15 additions & 18 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2530,32 +2530,26 @@ function print_course($course, $highlightterms = '') {
/// first find all roles that are supposed to be displayed
if (!empty($CFG->coursecontact)) {
$managerroles = explode(',', $CFG->coursecontact);
$namesarray = array();
$rusers = array();

if (!isset($course->managers)) {
$rusers = get_role_users($managerroles, $context, true,
'ra.id AS raid, u.id, u.username, u.firstname, u.lastname,
r.name AS rolename, r.sortorder, r.id AS roleid',
'ra.id AS raid, u.id, u.username, u.firstname, u.lastname, rn.name AS rolecoursealias,
r.name AS rolename, r.sortorder, r.id AS roleid, r.shortname AS roleshortname',
'r.sortorder ASC, u.lastname ASC');
} else {
// use the managers array if we have it for perf reasosn
// populate the datastructure like output of get_role_users();
foreach ($course->managers as $manager) {
$u = new stdClass();
$u = $manager->user;
$u->roleid = $manager->roleid;
$u->rolename = $manager->rolename;

$rusers[] = $u;
$user = clone($manager->user);
$user->roleid = $manager->roleid;
$user->rolename = $manager->rolename;
$user->roleshortname = $manager->roleshortname;
$user->rolecoursealias = $manager->rolecoursealias;
$rusers[$user->id] = $user;
}
}

/// Rename some of the role names if needed
if (isset($context)) {
$aliasnames = $DB->get_records('role_names', array('contextid'=>$context->id), '', 'roleid,contextid,name');
}

$namesarray = array();
$canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
foreach ($rusers as $ra) {
Expand All @@ -2564,12 +2558,15 @@ function print_course($course, $highlightterms = '') {
continue;
}

if (isset($aliasnames[$ra->roleid])) {
$ra->rolename = $aliasnames[$ra->roleid]->name;
}
$role = new stdClass();
$role->id = $ra->roleid;
$role->name = $ra->rolename;
$role->shortname = $ra->roleshortname;
$role->coursealias = $ra->rolecoursealias;
$rolename = role_get_name($role, $context, ROLENAME_ALIAS);

$fullname = fullname($ra, $canviewfullnames);
$namesarray[$ra->id] = format_string($ra->rolename).': '.
$namesarray[$ra->id] = $rolename.': '.
html_writer::link(new moodle_url('/user/view.php', array('id'=>$ra->id, 'course'=>SITEID)), $fullname);
}

Expand Down
Loading

0 comments on commit e1980f8

Please sign in to comment.