Skip to content

Commit

Permalink
MDL-61114 mod_assign: assignment overrides to observe group membership
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies authored and stronk7 committed Jul 3, 2019
1 parent ad75599 commit 211f680
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 59 deletions.
2 changes: 1 addition & 1 deletion mod/assign/lang/en/assign.php
Expand Up @@ -285,7 +285,7 @@
$string['gradingsummary'] = 'Grading summary';
$string['groupoverrides'] = 'Group overrides';
$string['groupoverridesdeleted'] = 'Group overrides deleted';
$string['groupsnone'] = 'There are no groups in this course';
$string['groupsnone'] = 'No groups you can access.';
$string['hidegrader'] = 'Hide grader identity from students';
$string['hidegrader_help'] = 'Hides the identity of any user who grades an assignment submission, so students can\'t see who marked their work.';
$string['hideshow'] = 'Hide/Show';
Expand Down
32 changes: 27 additions & 5 deletions mod/assign/override_form.php
Expand Up @@ -85,13 +85,16 @@ public function __construct($submiturl, $cm, $assign, $context, $groupmode, $ove
* Define this form - called by the parent constructor
*/
protected function definition() {
global $CFG, $DB;
global $DB;

$cm = $this->cm;
$mform = $this->_form;

$mform->addElement('header', 'override', get_string('override', 'assign'));

$assigngroupmode = groups_get_activity_groupmode($cm);
$accessallgroups = ($assigngroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $this->context);

if ($this->groupmode) {
// Group override.
if ($this->groupid) {
Expand All @@ -107,7 +110,8 @@ protected function definition() {
$mform->freeze('sortorder');
} else {
// Prepare the list of groups.
$groups = groups_get_all_groups($cm->course);
// Only include the groups the current can access.
$groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm);
if (empty($groups)) {
// Generate an error.
$link = new moodle_url('/mod/assign/overrides.php', array('cmid' => $cm->id));
Expand Down Expand Up @@ -140,8 +144,27 @@ protected function definition() {
$mform->freeze('userid');
} else {
// Prepare the list of users.
$users = get_enrolled_users($this->context, '', 0,
'u.id, u.email, ' . get_all_user_name_fields(true, 'u'));
$users = [];
list($sort) = users_order_by_sql('u');

// Get the list of appropriate users, depending on whether and how groups are used.
if ($accessallgroups) {
$users = get_enrolled_users($this->context, '', 0,
'u.id, u.email, ' . get_all_user_name_fields(true, 'u'), $sort);
} else if ($groups = groups_get_activity_allowed_groups($cm)) {
$enrolledjoin = get_enrolled_join($this->context, 'u.id');
$userfields = 'u.id, u.email, ' . get_all_user_name_fields(true, 'u');
list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
$params = $enrolledjoin->params + $ingroupparams;
$sql = "SELECT $userfields
FROM {user} u
JOIN {groups_members} gm ON gm.userid = u.id
{$enrolledjoin->joins}
WHERE gm.groupid $ingroupsql
AND {$enrolledjoin->wheres}
ORDER BY $sort";
$users = $DB->get_records_sql($sql, $params);
}

// Filter users based on any fixed restrictions (groups, profile).
$info = new \core_availability\info_module($cm);
Expand Down Expand Up @@ -233,7 +256,6 @@ protected function definition() {
* @return array of "element_name"=>"error_description" if there are errors
*/
public function validation($data, $files) {
global $COURSE, $DB;
$errors = parent::validation($data, $files);

$mform =& $this->_form;
Expand Down
107 changes: 75 additions & 32 deletions mod/assign/overrides.php
Expand Up @@ -38,13 +38,20 @@
list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'assign');
$assign = $DB->get_record('assign', array('id' => $cm->instance), '*', MUST_EXIST);

require_login($course, false, $cm);

$context = context_module::instance($cm->id);

// Check the user has the required capabilities to list overrides.
require_capability('mod/assign:manageoverrides', $context);

$assigngroupmode = groups_get_activity_groupmode($cm);
$accessallgroups = ($assigngroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $context);

$overridecountgroup = $DB->count_records('assign_overrides', array('userid' => null, 'assignid' => $assign->id));

// Get the course groups.
$groups = groups_get_all_groups($cm->course);
if ($groups === false) {
$groups = array();
}
// Get the course groups that the current user can access.
$groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm);

// Default mode is "group", unless there are no groups.
if ($mode != "user" and $mode != "group") {
Expand All @@ -60,13 +67,6 @@

$PAGE->set_url($url);

require_login($course, false, $cm);

$context = context_module::instance($cm->id);

// Check the user has the required capabilities to list overrides.
require_capability('mod/assign:manageoverrides', $context);

if ($action == 'movegroupoverride') {
$id = required_param('id', PARAM_INT);
$dir = required_param('dir', PARAM_ALPHA);
Expand All @@ -86,38 +86,63 @@

// Delete orphaned group overrides.
$sql = 'SELECT o.id
FROM {assign_overrides} o LEFT JOIN {groups} g
ON o.groupid = g.id
WHERE o.groupid IS NOT NULL
AND g.id IS NULL
AND o.assignid = ?';
FROM {assign_overrides} o
LEFT JOIN {groups} g ON o.groupid = g.id
WHERE o.groupid IS NOT NULL
AND g.id IS NULL
AND o.assignid = ?';
$params = array($assign->id);
$orphaned = $DB->get_records_sql($sql, $params);
if (!empty($orphaned)) {
$DB->delete_records_list('assign_overrides', 'id', array_keys($orphaned));
}

$overrides = [];

// Fetch all overrides.
if ($groupmode) {
$colname = get_string('group');
$sql = 'SELECT o.*, g.name
FROM {assign_overrides} o
JOIN {groups} g ON o.groupid = g.id
WHERE o.assignid = :assignid
ORDER BY o.sortorder';
$params = array('assignid' => $assign->id);
// To filter the result by the list of groups that the current user has access to.
if ($groups) {
$params = ['assignid' => $assign->id];
list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
$params += $inparams;

$sql = "SELECT o.*, g.name
FROM {assign_overrides} o
JOIN {groups} g ON o.groupid = g.id
WHERE o.assignid = :assignid AND g.id $insql
ORDER BY o.sortorder";

$overrides = $DB->get_records_sql($sql, $params);
}
} else {
$colname = get_string('user');
list($sort, $params) = users_order_by_sql('u');
$sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
FROM {assign_overrides} o
JOIN {user} u ON o.userid = u.id
WHERE o.assignid = :assignid
ORDER BY ' . $sort;
$params['assignid'] = $assign->id;
}

$overrides = $DB->get_records_sql($sql, $params);
if ($accessallgroups) {
$sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
FROM {assign_overrides} o
JOIN {user} u ON o.userid = u.id
WHERE o.assignid = :assignid
ORDER BY ' . $sort;

$overrides = $DB->get_records_sql($sql, $params);
} else if ($groups) {
list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
$params += $inparams;

$sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
FROM {assign_overrides} o
JOIN {user} u ON o.userid = u.id
JOIN {groups_members} gm ON u.id = gm.userid
WHERE o.assignid = :assignid AND gm.groupid ' . $insql . '
ORDER BY ' . $sort;

$overrides = $DB->get_records_sql($sql, $params);
}
}

// Initialise table.
$table = new html_table();
Expand Down Expand Up @@ -278,13 +303,31 @@
} else {
$users = array();
// See if there are any users in the assign.
$users = get_enrolled_users($context);
if ($accessallgroups) {
$users = get_enrolled_users($context, '', 0, 'u.id');
$nousermessage = get_string('usersnone', 'assign');
} else if ($groups) {
$enrolledjoin = get_enrolled_join($context, 'u.id');
list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
$params = $enrolledjoin->params + $ingroupparams;
$sql = "SELECT u.id
FROM {user} u
JOIN {groups_members} gm ON gm.userid = u.id
{$enrolledjoin->joins}
WHERE gm.groupid $ingroupsql
AND {$enrolledjoin->wheres}
ORDER BY $sort";
$users = $DB->get_records_sql($sql, $params);
$nousermessage = get_string('usersnone', 'assign');
} else {
$nousermessage = get_string('groupsnone', 'assign');
}
$info = new \core_availability\info_module($cm);
$users = $info->filter_user_list($users);

if (empty($users)) {
// There are no users.
echo $OUTPUT->notification(get_string('usersnone', 'assign'), 'error');
echo $OUTPUT->notification($nousermessage, 'error');
$options['disabled'] = true;
}
echo $OUTPUT->single_button($overrideediturl->out(true,
Expand Down
100 changes: 90 additions & 10 deletions mod/assign/tests/behat/assign_group_override.feature
Expand Up @@ -29,18 +29,13 @@ Feature: Assign group override
| student1 | G1 |
| student2 | G2 |
| student3 | G1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I add a "Assignment" to section "1" and I fill the form with:
| Assignment name | Test assignment name |
| Description | Submit your online text |
| assignsubmission_onlinetext_enabled | 1 |
| assignsubmission_onlinetext_wordlimit_enabled | 1 |
| assignsubmission_onlinetext_wordlimit | 10 |
| assignsubmission_file_enabled | 0 |
| gradingduedate[enabled] | 0 |
And the following "activities" exist:
| activity | name | intro | course | idnumber | assignsubmission_onlinetext_enabled |
| assign | Test assignment name | Submit your online text | C1 | assign1 | 1 |

Scenario: Add, modify then delete a group override
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
When I follow "Test assignment name"
And I navigate to "Group overrides" in current page administration
And I press "Add group override"
Expand All @@ -64,6 +59,8 @@ Feature: Assign group override
And I should not see "Group 1"

Scenario: Duplicate a user override
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
When I follow "Test assignment name"
And I navigate to "Group overrides" in current page administration
And I press "Add group override"
Expand All @@ -86,6 +83,8 @@ Feature: Assign group override
And I should see "Group 2"

Scenario: Allow a group to have a different due date
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
When I follow "Test assignment name"
And I navigate to "Edit settings" in current page administration
And I set the following fields to these values:
Expand Down Expand Up @@ -122,6 +121,8 @@ Feature: Assign group override
And I should see "Wednesday, 1 January 2020, 8:00"

Scenario: Allow a group to have a different cut off date
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
When I follow "Test assignment name"
And I navigate to "Edit settings" in current page administration
And I set the following fields to these values:
Expand Down Expand Up @@ -158,6 +159,8 @@ Feature: Assign group override
And I should see "You have not made a submission yet."

Scenario: Allow a group to have a different start date
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
When I follow "Test assignment name"
And I navigate to "Edit settings" in current page administration
And I set the following fields to these values:
Expand Down Expand Up @@ -196,6 +199,8 @@ Feature: Assign group override

@javascript
Scenario: Add both a user and group override and verify that both are applied correctly
Given I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
When I follow "Test assignment name"
And I navigate to "Edit settings" in current page administration
And I set the following fields to these values:
Expand Down Expand Up @@ -248,3 +253,78 @@ Feature: Assign group override
And I am on "Course 1" course homepage
And I follow "Test assignment name"
And I should see "This assignment will accept submissions from Wednesday, 1 January 2020, 8:00"

Scenario: Override a group when teacher is in no group, and does not have accessallgroups permission, and the activity's group mode is "separate groups"
Given the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
| moodle/site:accessallgroups | Prevent | editingteacher | Course | C1 |
And the following "activities" exist:
| activity | name | intro | course | idnumber | groupmode |
| assign | Assignment 2 | Assignment 2 description | C1 | assign2 | 1 |
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Assignment 2"
And I navigate to "Group overrides" in current page administration
Then I should see "No groups you can access."
And the "Add group override" "button" should be disabled

Scenario: A teacher without accessallgroups permission should only be able to add group override for groups that he/she is a member of,
when the activity's group mode is "separate groups"
Given the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
| moodle/site:accessallgroups | Prevent | editingteacher | Course | C1 |
And the following "activities" exist:
| activity | name | intro | course | idnumber | groupmode |
| assign | Assignment 2 | Assignment 2 description | C1 | assign2 | 1 |
And the following "group members" exist:
| user | group |
| teacher1 | G1 |
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Assignment 2"
And I navigate to "Group overrides" in current page administration
And I press "Add group override"
Then the "Override group" select box should contain "Group 1"
And the "Override group" select box should not contain "Group 2"

Scenario: A teacher without accessallgroups permission should only be able to see the group overrides for groups that he/she is a member of,
when the activity's group mode is "separate groups"
Given the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
| moodle/site:accessallgroups | Prevent | editingteacher | Course | C1 |
And the following "activities" exist:
| activity | name | intro | course | idnumber | groupmode |
| assign | Assignment 2 | Assignment 2 description | C1 | assign2 | 1 |
And the following "group members" exist:
| user | group |
| teacher1 | G1 |
And I log in as "admin"
And I am on "Course 1" course homepage
And I follow "Assignment 2"
And I navigate to "Group overrides" in current page administration
And I press "Add group override"
And I set the following fields to these values:
| Override group | Group 1 |
| id_allowsubmissionsfromdate_enabled | 1 |
| allowsubmissionsfromdate[day] | 1 |
| allowsubmissionsfromdate[month] | January |
| allowsubmissionsfromdate[year] | 2020 |
| allowsubmissionsfromdate[hour] | 08 |
| allowsubmissionsfromdate[minute] | 00 |
And I press "Save and enter another override"
And I set the following fields to these values:
| Override group | Group 2 |
| id_allowsubmissionsfromdate_enabled | 1 |
| allowsubmissionsfromdate[day] | 1 |
| allowsubmissionsfromdate[month] | January |
| allowsubmissionsfromdate[year] | 2020 |
| allowsubmissionsfromdate[hour] | 08 |
| allowsubmissionsfromdate[minute] | 00 |
And I press "Save"
And I log out
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Assignment 2"
And I navigate to "Group overrides" in current page administration
Then I should see "Group 1" in the ".generaltable" "css_element"
And I should not see "Group 2" in the ".generaltable" "css_element"

0 comments on commit 211f680

Please sign in to comment.