From bef12c997af85dcce30c9f070a8058836f40b464 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Thu, 13 Nov 2008 08:11:10 +0000 Subject: [PATCH] course settings: MDL-16327 when creating a course, the role renames were not saved. --- course/edit_form.php | 24 +++++++------- course/lib.php | 76 +++++++++++++++++++++++--------------------- lib/accesslib.php | 69 ++++++++++++++++++++++++++++++---------- 3 files changed, 104 insertions(+), 65 deletions(-) diff --git a/course/edit_form.php b/course/edit_form.php index ead6177d1c..13a3bb9aea 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -401,13 +401,19 @@ class course_edit_form extends moodleform { $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); } } } @@ -417,12 +423,6 @@ class course_edit_form extends moodleform { //-------------------------------------------------------------------------------- $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() { diff --git a/course/lib.php b/course/lib.php index 05d4d55c12..0407eb820b 100644 --- a/course/lib.php +++ b/course/lib.php @@ -3149,6 +3149,39 @@ function can_delete_course($courseid) { && 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 @@ -3191,7 +3224,10 @@ function create_course($data) { 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; @@ -3200,7 +3236,6 @@ function create_course($data) { return false; // error } - /** * Update a course and return true or false * @@ -3253,41 +3288,10 @@ function update_course($data) { $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; diff --git a/lib/accesslib.php b/lib/accesslib.php index f3d854ff3c..15b4f74fbf 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -158,6 +158,7 @@ define('ROLENAME_ORIGINAL', 0);// the name as defined in the role definition 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 @@ -4348,6 +4349,16 @@ function get_role_contextlevels($roleid) { '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(); @@ -5382,36 +5393,60 @@ function role_get_name($role, $coursecontext) { /** * 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; } -- 2.39.5