$mform->addElement('header','rolerenaming', get_string('rolerenaming'));
$mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
- if ($roles = $DB->get_records('role')) {
+ if ($roles = get_all_roles()) {
+ if ($coursecontext) {
+ $roles = role_fix_names($roles, $coursecontext,ROLENAME_ALIAS_RAW);
+ }
+ $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
foreach ($roles as $role) {
- $mform->addElement('text', 'role_'.$role->id, $role->name);
- if ($coursecontext) {
- if ($rolename = $DB->get_record('role_names', array('roleid'=>$role->id, 'contextid'=>$coursecontext->id))) {
- $mform->setDefault('role_'.$role->id, $rolename->name);
- }
+ $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
+ if (isset($role->localname)) {
+ $mform->setDefault('role_'.$role->id, $role->localname);
+ }
+ $mform->setType('role_'.$role->id, PARAM_TEXT);
+ if (!in_array($role->id, $assignableroles)) {
+ $mform->setAdvanced('role_'.$role->id);
}
}
}
//--------------------------------------------------------------------------------
$mform->addElement('hidden', 'id', null);
$mform->setType('id', PARAM_INT);
-
- // fill in default teacher and student names to keep backwards compatibility for a while
- $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
- $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
- $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
- $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
}
function definition_after_data() {
&& has_capability('moodle/course:manageactivities', $context)) );
}
+/**
+ * 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 = get_context_instance(CONTEXT_COURSE, $courseid);
+
+ foreach ($data as $fieldname => $value) {
+ if (!strstr($fieldname, 'role_')) {
+ 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);
+ }
+ }
+}
/**
* Create a course and either return a $course object or false
add_to_log(SITEID, 'course', 'new', 'view.php?id='.$course->id, $data->fullname.' (ID '.$course->id.')');
- //trigger events
+ // Save any custom role names.
+ save_local_role_names($course->id, $data);
+
+ // Trigger events
events_trigger('course_created', $course);
return $course;
return false; // error
}
-
/**
* Update a course and return true or false
*
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
blocks_remove_inappropriate($page);
- // put custom role names into db
- $context = get_context_instance(CONTEXT_COURSE, $course->id);
-
- foreach ($data as $dname => $dvalue) {
-
- // is this the right param?
- $dvalue = clean_param($dvalue, PARAM_NOTAGS);
-
- if (!strstr($dname, 'role_')) {
- continue;
- }
-
- $dt = explode('_', $dname);
- $roleid = $dt[1];
- // make up our mind whether we want to delete, update or insert
-
- if (empty($dvalue)) {
+ // Save any custom role names.
+ save_local_role_names($course->id, $data);
- $DB->delete_records('role_names', array('contextid'=>$context->id, 'roleid'=>$roleid));
-
- } else if ($t = $DB->get_record('role_names', array('contextid'=>$context->id, 'roleid'=>$roleid))) {
-
- $t->name = $dvalue;
- $DB->update_record('role_names', $t);
-
- } else {
-
- $t->contextid = $context->id;
- $t->roleid = $roleid;
- $t->name = $dvalue;
- $DB->insert_record('role_names', $t);
- }
-
- }
- //trigger events
+ // Trigger events
events_trigger('course_updated', $course);
return true;
define('ROLENAME_ALIAS', 1); // the name as defined by a role alias
define('ROLENAME_BOTH', 2); // Both, like this: Role alias (Original)
define('ROLENAME_ORIGINALANDSHORT', 0); // the name as defined in the role definition and the shortname in brackets
+define('ROLENAME_ALIAS_RAW', 1); // the name as defined by a role alias, in raw form suitable for editing
$context_cache = array(); // Cache of all used context objects for performance (by level and instance)
$context_cache_id = array(); // Index to above cache by id
'contextlevel', 'id,contextlevel');
}
+/**
+ * @param integer $contextlevel a contextlevel.
+ * @return array list of role ids that are assignable at this context level.
+ */
+function get_roles_for_contextlevels($contextlevel) {
+ global $DB;
+ return $DB->get_records_menu('role_context_levels', array('contextlevel' => $contextlevel),
+ '', 'id,roleid');
+}
+
/**
* @param string $roleid one of the legacy role types - that is, one of the keys
* from the array returned by get_legacy_roles();
/**
* Prepare list of roles for display, apply aliases and format text
- * @param array $roleoptions array roleid=>rolename
- * @param object $context
- * @return array of role names
+ * @param array $roleoptions array roleid => rolename or roleid => roleobject
+ * @param object $context a context
+ * @return array of context-specific role names, or role objexts with a ->localname field added.
*/
function role_fix_names($roleoptions, $context, $rolenamedisplay=ROLENAME_ALIAS) {
global $DB;
+
+ // Make sure we are working with an array roleid => name. Normally we
+ // want to use the unlocalised name if the localised one is not present.
+ $newnames = array();
+ foreach ($roleoptions as $rid => $roleorname) {
+ if ($rolenamedisplay != ROLENAME_ALIAS_RAW) {
+ if (is_object($roleorname)) {
+ $newnames[$rid] = $roleorname->name;
+ } else {
+ $newnames[$rid] = $roleorname;
+ }
+ } else {
+ $newnames[$rid] = '';
+ }
+ }
+
+ // If necessary, get the localised names.
if ($rolenamedisplay != ROLENAME_ORIGINAL && !empty($context->id)) {
+ // Make sure we have a course context.
if ($context->contextlevel == CONTEXT_MODULE || $context->contextlevel == CONTEXT_BLOCK) { // find the parent course context
if ($parentcontextid = array_shift(get_parent_contexts($context))) {
$context = get_context_instance_by_id($parentcontextid);
}
}
- if ($aliasnames = $DB->get_records('role_names', array('contextid'=>$context->id))) {
- if ($rolenamedisplay == ROLENAME_ALIAS) {
- foreach ($aliasnames as $alias) {
- if (isset($roleoptions[$alias->roleid])) {
- $roleoptions[$alias->roleid] = format_string($alias->name);
- }
- }
- } else if ($rolenamedisplay == ROLENAME_BOTH) {
- foreach ($aliasnames as $alias) {
- if (isset($roleoptions[$alias->roleid])) {
- $roleoptions[$alias->roleid] = format_string($alias->name).' ('.format_string($roleoptions[$alias->roleid]).')';
- }
+
+ // The get the relevant renames, and use them.
+ $aliasnames = $DB->get_records('role_names', array('contextid'=>$context->id));
+ foreach ($aliasnames as $alias) {
+ if (isset($newnames[$alias->roleid])) {
+ if ($rolenamedisplay == ROLENAME_ALIAS || $rolenamedisplay == ROLENAME_ALIAS_RAW) {
+ $newnames[$alias->roleid] = $alias->name;
+ } else if ($rolenamedisplay == ROLENAME_BOTH) {
+ $newnames[$alias->roleid] = $alias->name . ' (' . $roleoptions[$alias->roleid] . ')';
}
}
}
}
- foreach ($roleoptions as $rid => $name) {
- $roleoptions[$rid] = strip_tags(format_string($name));
+
+ // Finally, apply format_string and put the result in the right place.
+ foreach ($roleoptions as $rid => $roleorname) {
+ if ($rolenamedisplay != ROLENAME_ALIAS_RAW) {
+ $newnames[$rid] = strip_tags(format_string($newnames[$rid]));
+ }
+ if (is_object($roleorname)) {
+ $roleoptions[$rid]->localname = $newnames[$rid];
+ } else {
+ $roleoptions[$rid] = $newnames[$rid];
+ }
}
return $roleoptions;
}