Skip to content

Commit

Permalink
MDL-6568 shortname added to roles
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Sep 18, 2006
1 parent 7dbc351 commit 31f2679
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 17 deletions.
2 changes: 2 additions & 0 deletions admin/roles/manage.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<input type="hidden" name="sesskey" value="<?php print (sesskey()); ?>">
<input type="hidden" name="action" value="<?php echo $action; ?>">
<br/>Role Name: <input type="text" name="name" value="<?php echo $role->name; ?>">
&nbsp;
Role short name (ASCII): <input type="text" name="shortname" value="<?php echo $role->shortname; ?>" />
<br/>Role Description:

<?php print_textarea($CFG->htmleditor, 10, 50, 50, 10, 'description', "$role->description"); ?>
Expand Down
10 changes: 6 additions & 4 deletions admin/roles/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

admin_externalpage_setup('defineroles', $adminroot);

$roleid = optional_param('roleid', 0, PARAM_INT); // if set, we are editing a role
$roleid = optional_param('roleid', 0, PARAM_INT); // if set, we are editing a role
$name = optional_param('name', '', PARAM_MULTILANG); // new role name
$shortname = optional_param('shortname', '', PARAM_SAFEDIR); // new role shortname
$description = optional_param('description', '', PARAM_MULTILANG); // new role desc
$action = optional_param('action', '', PARAM_ALPHA);
$name = optional_param('name', '', PARAM_ALPHA); // new role name
$description = optional_param('description', '', PARAM_NOTAGS); // new role desc
$confirm = optional_param('confirm', 0, PARAM_BOOL);

$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
Expand All @@ -37,7 +38,7 @@
switch ($action) {
case 'add':

$newrole = create_role($name, $description);
$newrole = create_role($name, $shortname, $description);

$ignore = array('roleid', 'sesskey', 'action', 'name', 'description', 'contextid');

Expand Down Expand Up @@ -142,6 +143,7 @@
} else {
$action='add';
$role->name='';
$role->shortname='';
$role->description='';
}

Expand Down
24 changes: 15 additions & 9 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,15 +887,15 @@ function moodle_install_roles() {

// Create default/legacy roles and capabilities.
// (1 legacy capability per legacy role at system level).
$adminrole = create_role(get_string('administrator'), get_string('administratordescription'), 'moodle/legacy:admin');
$adminrole = create_role(get_string('administrator'), 'admin', get_string('administratordescription'), 'moodle/legacy:admin');
if (!assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $systemcontext->id)) {
error('Could not assign moodle/site:doanything to the admin role');
}
$coursecreatorrole = create_role(get_string('coursecreators'), get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator');
$noneditteacherrole = create_role(get_string('noneditingteacher'), get_string('noneditingteacherdescription'), 'moodle/legacy:teacher');
$editteacherrole = create_role(get_string('defaultcourseteacher'), get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher');
$studentrole = create_role(get_string('defaultcoursestudent'), get_string('defaultcoursestudentdescription'), 'moodle/legacy:student');
$guestrole = create_role(get_string('guest'), get_string('guestdescription'), 'moodle/legacy:guest');
$coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator');
$editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher');
$noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', get_string('noneditingteacherdescription'), 'moodle/legacy:teacher');
$studentrole = create_role(get_string('defaultcoursestudent'), 'student', get_string('defaultcoursestudentdescription'), 'moodle/legacy:student');
$guestrole = create_role(get_string('guest'), 'guest', get_string('guestdescription'), 'moodle/legacy:guest');


// Look inside user_admin, user_creator, user_teachers, user_students and
Expand Down Expand Up @@ -1168,19 +1168,25 @@ function get_local_override($roleid, $contextid, $capability) {
/**
* function that creates a role
* @param name - role name
* @param shortname - role short name
* @param description - role description
* @param legacy - optional legacy capability
* @return id or false
*/
function create_role($name, $description, $legacy='') {
function create_role($name, $shortname, $description, $legacy='') {

// check for duplicate role name

if ($role = get_record('role','name', $name)) {
error('there is already a role with this name!');
}

if ($role = get_record('role','shortname', $shortname)) {
error('there is already a role with this shortname!');
}

$role->name = $name;
$role->shortname = $shortname;
$role->description = $description;

$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
Expand Down Expand Up @@ -2207,7 +2213,7 @@ function get_roles_used_in_context($context) {

global $CFG;

return get_records_sql('SELECT distinct r.id, r.name
return get_records_sql('SELECT distinct r.id, r.name, r.shortname
FROM '.$CFG->prefix.'role_assignments ra,
'.$CFG->prefix.'role r
WHERE r.id = ra.roleid
Expand Down Expand Up @@ -2313,7 +2319,7 @@ function get_user_roles($context, $userid=0, $checkparentcontexts=true) {
$contexts = ' ra.contextid = \''.$context->id.'\'';
}

return get_records_sql('SELECT ra.*, r.name
return get_records_sql('SELECT ra.*, r.name, r.shortname
FROM '.$CFG->prefix.'role_assignments ra,
'.$CFG->prefix.'role r,
'.$CFG->prefix.'context c
Expand Down
7 changes: 4 additions & 3 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20060917" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20060918" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -915,8 +915,9 @@
<TABLE NAME="role" COMMENT="moodle roles" PREVIOUS="blog_tag_instance" NEXT="context">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="name"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="description"/>
<FIELD NAME="description" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="name" NEXT="sortorder"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="shortname"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="name" NEXT="description"/>
<FIELD NAME="description" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="shortname" NEXT="sortorder"/>
<FIELD NAME="sortorder" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="description"/>
</FIELDS>
<KEYS>
Expand Down
24 changes: 24 additions & 0 deletions lib/db/mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,30 @@ function main_upgrade($oldversion=0) {
delete_records('config', 'name', 'requestedstudentsname');
}

if ($oldversion < 2006091804) {
$roles = get_records('role');
$first = array_shift($roles);
if (!empty($first->shortname)) {
// shortnames already exist
} else {
table_column('role', '', 'shortname', 'varchar', '100', '', '', 'not null', 'name');
$legacy_names = array('admin', 'coursecreator', 'editingteacher', 'teacher', 'student', 'guest');
foreach ($legacy_names as $name) {
if ($roles = get_roles_with_capability('moodle/legacy:'.$name, CAP_ALLOW)) {
$i = '';
foreach ($roles as $role) {
if (empty($role->shortname)) {
$updated = new object();
$updated->id = $role->id;
$updated->shortname = $name.$i;
update_record('role', $updated);
$i++;
}
}
}
}
}
}
return $result;
}

Expand Down
1 change: 1 addition & 0 deletions lib/db/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ CREATE TABLE prefix_blog_tag_instance (
CREATE TABLE prefix_role (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`shortname` varchar(100) NOT NULL default '',
`description` text NOT NULL default '',
`sortorder` int(10) unsigned NOT NULL default '0',
KEY `sortorder` (`sortorder`),
Expand Down
1 change: 1 addition & 0 deletions lib/db/postgres7.sql
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ CREATE INDEX prefix_bti_tagid_idx ON prefix_blog_tag_instance (tagid);
CREATE TABLE prefix_role (
id SERIAL PRIMARY KEY,
name varchar(255) NOT NULL default '',
shortname varchar(100) NOT NULL default '',
description text NOT NULL default '',
sortorder integer NOT NULL default '0'
);
Expand Down
8 changes: 8 additions & 0 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@
*/
define('PARAM_NOTAGS', 0x0008);

/**
* PARAM_MULTILANG - general plain text compatible with multilang filter, no other html tags.
*/
define('PARAM_MULTILANG', 0x0009);

/**
* PARAM_FILE - safe file name, all dangerous chars are stripped, protects against XSS, SQL injections and directory traversals
*/
Expand Down Expand Up @@ -368,6 +373,9 @@ function clean_param($param, $type) {
case PARAM_NOTAGS: // Strip all tags
return strip_tags($param);

case PARAM_MULTILANG: // leave only tags needed for multilang
return clean_param(strip_tags($param, '<lang><span>'), PARAM_CLEAN);

case PARAM_SAFEDIR: // Remove everything not a-zA-Z0-9_-
return eregi_replace('[^a-zA-Z0-9_-]', '', $param);

Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// This is compared against the values stored in the database to determine
// whether upgrades should be performed (see lib/db/*.php)

$version = 2006091800; // YYYYMMDD = date
$version = 2006091804; // YYYYMMDD = date
// XY = increments within a single day

$release = '1.7 dev'; // Human-friendly version name
Expand Down

0 comments on commit 31f2679

Please sign in to comment.