Merge branch 'MDL-78175-master' of https://github.com/ferranrecio/moodle

This commit is contained in:
Jun Pataleta 2023-06-13 13:43:48 +08:00
commit ab384ef813
13 changed files with 174 additions and 44 deletions

View File

@ -71,13 +71,12 @@ Feature: Edit capabilities
@javascript
Scenario: Edit permissions escapes role names correctly
When I am on the "C1" "Course" page logged in as "admin"
And I navigate to "Settings" in current page administration
When I am on the "Course 1" "renameroles" page logged in as "admin"
And I set the following fields to these values:
| Your word for 'Teacher' | Teacher >= editing |
| Your word for 'Non-editing teacher' | Teacher < "editing" |
| Your word for 'Student' | Studier & 'learner' |
And I press "Save and display"
And I press "Save"
And I navigate to course participants
Then I should see "Teacher >= editing (Teacher)" in the "Teacher 1" "table_row"
And I should see "Teacher < \"editing\" (Non-editing teacher)" in the "Teaching Assistant" "table_row"

View File

@ -129,12 +129,6 @@ if (!empty($course)) {
file_prepare_standard_filemanager($course, 'overviewfiles', $overviewfilesoptions, $coursecontext, 'course', 'overviewfiles', 0);
}
// Inject current aliases.
$aliases = $DB->get_records('role_names', array('contextid'=>$coursecontext->id));
foreach($aliases as $alias) {
$course->{'role_'.$alias->roleid} = $alias->name;
}
// Populate course tags.
$course->tags = core_tag_tag::get_item_tags_array('core', 'course', $course->id);

View File

@ -374,22 +374,6 @@ class course_edit_form extends moodleform {
$options[0] = get_string('none');
$mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
|| (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
// Customizable role names in this course.
$mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
$mform->addHelpButton('rolerenaming', 'rolerenaming');
if ($roles = get_all_roles()) {
$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->localname));
$mform->setType('role_' . $role->id, PARAM_TEXT);
}
}
}
if (core_tag_tag::is_enabled('core', 'course') &&
((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
|| (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {

View File

@ -19,13 +19,11 @@ Feature: Rename roles within a course
| teacher1 | C1 | editingteacher |
| teacher2 | C1 | teacher |
| student1 | C1 | student |
And I log in as "teacher1"
And I am on "Course 1" course homepage
When I navigate to "Settings" in current page administration
And I am on the "Course 1" "renameroles" page logged in as "teacher1"
And I set the following fields to these values:
| Your word for 'Non-editing teacher' | Tutor |
| Your word for 'Student' | Learner |
And I press "Save and display"
And I press "Save"
And I follow "Switch role to..." in the user menu
Then "Tutor" "button" should exist
And "Learner" "button" should exist
@ -42,12 +40,11 @@ Feature: Rename roles within a course
And I press "Enrol users"
And the "Assign role" select box should contain "Learner (Student)"
And I click on "Cancel" "button" in the "Enrol users" "dialogue"
And I am on "Course 1" course homepage
And I navigate to "Settings" in current page administration
And I am on the "Course 1" "renameroles" page
And I set the following fields to these values:
| Your word for 'Non-editing teacher' | |
| Your word for 'Student' | |
And I press "Save and display"
And I press "Save"
And I follow "Switch role to..." in the user menu
And I should see "Teacher"
And "Student" "button" should exist

View File

@ -18,14 +18,11 @@ Feature: Rename roles in a course
| teacher1 | C1 | editingteacher |
Scenario: Teacher can rename roles
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Settings" in current page administration
And I should see "Role renaming"
Given I am on the "Course 1" "renameroles" page logged in as "teacher1"
When I set the following fields to these values:
| Your word for 'Teacher' | Lecturer |
| Your word for 'Student' | Learner |
And I press "Save and display"
And I press "Save"
And I navigate to course participants
Then I should see "Lecturer (Teacher)" in the "Teacher 1" "table_row"
And I should see "Learner (Student)" in the "Student 1" "table_row"
@ -46,6 +43,5 @@ Feature: Rename roles in a course
And I follow "Log out"
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Settings" in current page administration
And I navigate to course participants
Then I should not see "Role renaming"
And I should not see "Your word for 'Teacher'"

View File

@ -0,0 +1,53 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_enrol\form;
/**
* Form to customise the course role names.
*
* @package core_enrol
* @copyright 2023 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renameroles extends \moodleform {
/**
* Form definition.
*/
public function definition() {
$mform = $this->_form;
$courseid = $this->_customdata['id'];
$roles = $this->_customdata['roles'] ?? [];
$formdata = new \stdClass();
$mform->addElement('hidden', 'id', $courseid);
$mform->setType('id', PARAM_INT);
foreach ($roles as $role) {
$settingname = 'role_' . $role->id;
$mform->addElement('text', $settingname, get_string('yourwordforx', '', $role->localname));
$mform->setType($settingname, PARAM_TEXT);
$formdata->{$settingname} = $role->coursealias;
}
$mform->addElement('submit', 'submit', get_string('save'));
$this->set_data($formdata);
}
}

67
enrol/renameroles.php Normal file
View File

@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Customise the course role names.
*
* @package core_enrol
* @copyright 2023 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../config.php');
require_once("$CFG->dirroot/course/lib.php");
$id = required_param('id', PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHANUMEXT);
$filter = optional_param('ifilter', 0, PARAM_INT);
$course = $DB->get_record('course', ['id' => $id], '*', MUST_EXIST);
$context = core\context\course::instance($course->id, MUST_EXIST);
require_login($course);
require_capability('moodle/course:renameroles', $context);
if ($course->id == SITEID) {
redirect("$CFG->wwwroot/");
}
$PAGE->set_pagelayout('admin');
$PAGE->set_url('/enrol/renameroles.php', ['id' => $course->id]);
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('rolerenaming'));
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->render_participants_tertiary_nav($course);
echo $OUTPUT->heading(get_string('rolerenaming'));
echo $OUTPUT->paragraph(get_string('rolerenaming_help'));
$customdata = [
'id' => $course->id,
'roles' => role_get_names($context, ROLENAME_ORIGINAL),
];
$mform = new core_enrol\form\renameroles(null, $customdata);
if ($data = $mform->get_data()) {
save_local_role_names($course->id, (array)$data);
core\notification::add(get_string('rolerenaming_success'), core\notification::SUCCESS);
}
$mform->display();
echo $OUTPUT->footer();

View File

@ -1884,7 +1884,8 @@ $string['role'] = 'Role';
$string['roleassignments'] = 'Role assignments';
$string['rolemappings'] = 'Role mappings';
$string['rolerenaming'] = 'Role renaming';
$string['rolerenaming_help'] = 'This setting allows the displayed names for roles used in the course to be changed. Only the displayed name is changed - role permissions are not affected. New role names will appear on the course participants page and elsewhere within the course. If the renamed role is one that the administrator has selected as a course manager role, then the new role name will also appear as part of the course listings.';
$string['rolerenaming_help'] = 'Change the display names of roles in this course.';
$string['rolerenaming_success'] = 'Course role names updated.';
$string['roles'] = 'Roles';
$string['rss'] = 'RSS';
$string['rssarticles'] = 'Number of RSS recent articles';

View File

@ -68,7 +68,8 @@ class participants_action_bar implements \renderable {
return [
'enrolments:enrol' => [
'review',
'manageinstances'
'manageinstances',
'renameroles',
],
'groups:group' => [
'groups'
@ -81,7 +82,7 @@ class participants_action_bar implements \renderable {
'roleoverride',
'rolecheck',
'roleassign',
]
],
];
}

View File

@ -476,6 +476,17 @@ function enrol_add_course_navigation(navigation_node $coursenode, $course) {
if (!$url) {
$instancesnode->trim_if_empty();
}
if (has_capability('moodle/course:renameroles', $coursecontext)) {
$url = new moodle_url('/enrol/renameroles.php', array('id' => $course->id));
$instancesnode->add(
get_string('rolerenaming'),
$url,
navigation_node::TYPE_SETTING,
null,
'renameroles'
);
}
}
// Manage groups in this course or even frontpage
@ -484,7 +495,10 @@ function enrol_add_course_navigation(navigation_node $coursenode, $course) {
$usersnode->add(get_string('groups'), $url, navigation_node::TYPE_SETTING, null, 'groups', new pix_icon('i/group', ''));
}
if (has_any_capability(array( 'moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:review'), $coursecontext)) {
if (has_any_capability(
[ 'moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:review'],
$coursecontext
)) {
// Override roles
if (has_capability('moodle/role:review', $coursecontext)) {
$url = new moodle_url('/admin/roles/permissions.php', array('contextid'=>$coursecontext->id));
@ -505,7 +519,7 @@ function enrol_add_course_navigation(navigation_node $coursenode, $course) {
$url = new moodle_url('/admin/roles/check.php', array('contextid'=>$coursecontext->id));
$permissionsnode->add(get_string('checkpermissions', 'role'), $url, navigation_node::TYPE_SETTING, null, 'permissions', new pix_icon('i/checkpermissions', ''));
}
}
}
// Deal somehow with users that are not enrolled but still got a role somehow
if ($course->id != SITEID) {

View File

@ -3263,6 +3263,22 @@ EOD;
return $this->opencontainers->pop('box');
}
/**
* Outputs a paragraph.
*
* @param string $contents The contents of the paragraph
* @param string|null $classes A space-separated list of CSS classes
* @param string|null $id An optional ID
* @return string the HTML to output.
*/
public function paragraph(string $contents, ?string $classes = null, ?string $id = null): string {
return html_writer::tag(
'p',
$contents,
['id' => $id, 'class' => renderer_base::prepare_classes($classes)]
);
}
/**
* Outputs a container.
*

View File

@ -882,6 +882,13 @@ class behat_navigation extends behat_base {
$identifier . '" does not exist');
}
return new moodle_url('/enrol/otherusers.php', ['id' => $courseid]);
case 'renameroles':
$courseid = $this->get_course_id($identifier);
if (!$courseid) {
throw new Exception('The specified course with shortname, fullname, or idnumber "' .
$identifier . '" does not exist');
}
return new moodle_url('/enrol/renameroles.php', ['id' => $courseid]);
}
$parts = explode(' ', $type);

View File

@ -15,6 +15,7 @@ information provided here is intended especially for developers.
* Added new \admin_setting::is_forceable() method to determine whether the setting can be overridden or not. Therefore,
whether the settings can be overriden or not will depend on the value of implemented \admin_setting::is_forceable() method,
even if we define the settings in config.php.
* New core_renderer::paragraph method to replace the overused html_writer::tag('p', ...) pattern.
=== 4.2 ===