Skip to content

Commit

Permalink
Merge branch 'MDL-60590_master' of git://github.com/markn86/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Nov 6, 2017
2 parents 3602df5 + e2d635b commit 40f05d2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 11 deletions.
3 changes: 1 addition & 2 deletions cohort/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public static function search_cohorts_parameters() {
* @return array
*/
public static function search_cohorts($query, $context, $includes = 'parents', $limitfrom = 0, $limitnum = 25) {
global $DB, $CFG, $PAGE;
global $CFG;
require_once($CFG->dirroot . '/cohort/lib.php');

$params = self::validate_parameters(self::search_cohorts_parameters(), array(
Expand All @@ -341,7 +341,6 @@ public static function search_cohorts($query, $context, $includes = 'parents', $
$limitnum = $params['limitnum'];

self::validate_context($context);
$output = $PAGE->get_renderer('tool_lp');

$manager = has_capability('moodle/cohort:manage', $context);
if (!$manager) {
Expand Down
2 changes: 1 addition & 1 deletion enrol/manual/amd/build/quickenrolment.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions enrol/manual/amd/src/quickenrolment.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ define(['core/templates',
],
function(Template, $, Str, Config, Notification, ModalFactory, ModalEvents, Fragment) {

/** @type {Object} The list of selectors for the quick enrolment modal. */
var SELECTORS = {
COHORTSELECT: "#id_cohortlist",
TRIGGERBUTTONS: ".enrolusersbutton.enrol_manual_plugin [type='submit']",
UNWANTEDHIDDENFIELDS: ":input[value='_qf__force_multiselect_submission']"
};

/**
* Constructor
*
Expand All @@ -57,7 +64,7 @@ define(['core/templates',
* @private
*/
QuickEnrolment.prototype.initModal = function() {
var triggerButtons = $('.enrolusersbutton.enrol_manual_plugin [type="submit"]');
var triggerButtons = $(SELECTORS.TRIGGERBUTTONS);

var stringsPromise = Str.get_strings([
{key: 'enroluserscohorts', component: 'enrol_manual'},
Expand All @@ -68,10 +75,6 @@ define(['core/templates',
return strings[1];
});

var buttonPromise = stringsPromise.then(function(strings) {
return strings[0];
});

return ModalFactory.create({
type: ModalFactory.types.SAVE_CANCEL,
large: true,
Expand All @@ -81,6 +84,16 @@ define(['core/templates',
.then(function(modal) {
this.modal = modal;

// The save button text depends on whether or not cohorts exist.
var stringindex = 1;
if (this.modal.getRoot().find('form').find(SELECTORS.COHORTSELECT).length !== 0) {
stringindex = 0;
}

var buttonPromise = stringsPromise.then(function(strings) {
return strings[stringindex];
});

this.modal.setSaveButtonText(buttonPromise);

// We want the reset the form every time it is opened.
Expand Down Expand Up @@ -123,7 +136,7 @@ define(['core/templates',

// Before send the data through AJAX, we need to parse and remove some unwanted hidden fields.
// This hidden fields are added automatically by mforms and when it reaches the AJAX we get an error.
var hidden = form.find(':input[value="_qf__force_multiselect_submission"]');
var hidden = form.find(SELECTORS.UNWANTEDHIDDENFIELDS);
hidden.each(function () {
this.remove();
});
Expand Down
20 changes: 18 additions & 2 deletions enrol/manual/classes/enrol_users_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,24 @@ public function definition() {
);
$mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), array(), $options);

$options = ['contextid' => $context->id, 'multiple' => true];
$mform->addElement('cohort', 'cohortlist', get_string('selectcohorts', 'enrol_manual'), $options);
// Confirm the user can search for cohorts before displaying select.
if (has_capability('moodle/cohort:manage', $context) || has_capability('moodle/cohort:view', $context)) {
// Check to ensure there is at least one visible cohort before displaying the select box.
// Ideally it would be better to call external_api::call_external_function('core_cohort_search_cohorts')
// (which is used to populate the select box) instead of duplicating logic but there is an issue with globals
// being borked (in this case $PAGE) when combining the usage of fragments and call_external_function().
require_once($CFG->dirroot . '/cohort/lib.php');
$availablecohorts = cohort_get_cohorts($context->id, 0, 1, '');
$availablecohorts = $availablecohorts['cohorts'];
if (!($context instanceof context_system)) {
$availablecohorts = array_merge($availablecohorts,
cohort_get_available_cohorts($context, COHORT_ALL, 0, 1, ''));
}
if (!empty($availablecohorts)) {
$options = ['contextid' => $context->id, 'multiple' => true];
$mform->addElement('cohort', 'cohortlist', get_string('selectcohorts', 'enrol_manual'), $options);
}
}

$roles = get_assignable_roles($context);
$mform->addElement('select', 'roletoassign', get_string('assignrole', 'enrol_manual'), $roles);
Expand Down
67 changes: 67 additions & 0 deletions user/tests/behat/enrol_cohort_list.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@core @core_user
Feature: Viewing the list of cohorts to enrol in a course
In order to ensure we only display the cohorts when applicable
As a teacher
I should only see the list of cohorts under some circumstances

Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |

@javascript
Scenario: Check the teacher does not see the cohorts field without the proper capabilities
Given I log in as "admin"
And I set the following system permissions of "Teacher" role:
| capability | permission |
| moodle/cohort:manage | Prohibit |
| moodle/cohort:view | Prohibit |
And I navigate to "Cohorts" node in "Site administration > Users > Accounts"
And I follow "Add new cohort"
And I set the following fields to these values:
| Name | Test cohort name |
| Context | System |
| Cohort ID | 1337 |
| Description | Test cohort description |
And I press "Save changes"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to course participants
When I press "Enrol users"
Then I should not see "Select cohorts"
And I should not see "Enrol selected users and cohorts"

@javascript
Scenario: Check we show the cohorts field if there are some present
Given I log in as "admin"
And I navigate to "Cohorts" node in "Site administration > Users > Accounts"
And I follow "Add new cohort"
And I set the following fields to these values:
| Name | Test cohort name |
| Context | System |
| Cohort ID | 1337 |
| Description | Test cohort description |
And I press "Save changes"
And I log out
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to course participants
When I press "Enrol users"
Then I should see "Select cohorts"
And I should see "Enrol selected users and cohorts"

@javascript
Scenario: Check we do not show the cohorts field if there are none present
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to course participants
When I press "Enrol users"
Then I should not see "Select cohorts"
And I should not see "Enrol selected users and cohorts"

0 comments on commit 40f05d2

Please sign in to comment.