Skip to content

Commit

Permalink
MDL-64032 enrol_manual: Make consistent the UI for setting enrolment
Browse files Browse the repository at this point in the history
Add end date/duration for the UI for setting enrolment should be consistent
  • Loading branch information
HuongNV13 committed Jul 23, 2019
1 parent 8d9614b commit 0fc3082
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 19 deletions.
3 changes: 3 additions & 0 deletions enrol/editenrolment.php
Expand Up @@ -87,6 +87,9 @@
redirect($returnurl);

} else if ($data = $mform->get_data()) {
if ($data->duration && $data->timeend == 0) {
$data->timeend = $data->timestart + $data->duration;
}
if ($manager->edit_enrolment($ue, $data)) {
redirect($returnurl);
}
Expand Down
10 changes: 10 additions & 0 deletions enrol/editenrolment_form.php
Expand Up @@ -22,6 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use core_enrol\enrol_helper;

defined('MOODLE_INTERNAL') || die();

require_once("$CFG->libdir/formslib.php");
Expand All @@ -34,6 +36,9 @@ function definition() {
$instancename = $this->_customdata['enrolinstancename'];
$modal = !empty($this->_customdata['modal']);

$periodmenu = enrol_get_period_list();
$duration = enrol_calculate_duration($ue->timestart, $ue->timeend);

$mform->addElement('static', 'enrolmentmethod', get_string('enrolmentmethod', 'enrol'), $instancename);

$options = array(ENROL_USER_ACTIVE => get_string('participationactive', 'enrol'),
Expand All @@ -44,6 +49,11 @@ function definition() {

$mform->addElement('date_time_selector', 'timestart', get_string('enroltimestart', 'enrol'), array('optional' => true));

$mform->addElement('select', 'duration', get_string('enrolperiod', 'enrol'), $periodmenu);
$mform->setDefault('duration', $duration);
$mform->disabledIf('duration', 'timestart[enabled]', 'notchecked', 1);
$mform->disabledIf('duration', 'timeend[enabled]', 'checked', 1);

$mform->addElement('date_time_selector', 'timeend', get_string('enroltimeend', 'enrol'), array('optional' => true));

$mform->addElement('static', 'timecreated', get_string('enroltimecreated', 'enrol'), userdate($ue->timecreated));
Expand Down
17 changes: 16 additions & 1 deletion enrol/manual/ajax.php
Expand Up @@ -32,6 +32,7 @@
require_once($CFG->dirroot.'/group/lib.php');
require_once($CFG->dirroot.'/enrol/manual/locallib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
require_once($CFG->dirroot . '/enrol/manual/classes/enrol_users_form.php');

$id = required_param('id', PARAM_INT); // Course id.
$action = required_param('action', PARAM_ALPHANUMEXT);
Expand Down Expand Up @@ -94,6 +95,7 @@
$duration = optional_param('duration', 0, PARAM_INT);
$startdate = optional_param('startdate', 0, PARAM_INT);
$recovergrades = optional_param('recovergrades', 0, PARAM_INT);
$timeend = optional_param_array('timeend', [], PARAM_INT);

if (empty($roleid)) {
$roleid = null;
Expand Down Expand Up @@ -122,12 +124,25 @@
$timestart = $today;
break;
}
if ($duration <= 0) {
if ($timeend) {
$timeend = make_timestamp($timeend['year'], $timeend['month'], $timeend['day'], $timeend['hour'], $timeend['minute']);
} else if ($duration <= 0) {
$timeend = 0;
} else {
$timeend = $timestart + $duration;
}

$mform = new enrol_manual_enrol_users_form(null, (object)["context" => $context]);
$userenroldata = [
'startdate' => $timestart,
'timeend' => $timeend,
];
$mform->set_data($userenroldata);
$validationerrors = $mform->validation($userenroldata, null);
if (!empty($validationerrors)) {
throw new enrol_ajax_exception('invalidenrolduration');
}

$instances = $manager->get_enrolment_instances();
$plugins = $manager->get_enrolment_plugins(true); // Do not allow actions on disabled plugins.
if (!array_key_exists($enrolid, $instances)) {
Expand Down
37 changes: 25 additions & 12 deletions enrol/manual/classes/enrol_users_form.php
Expand Up @@ -60,14 +60,7 @@ public function definition() {
$mform = $this->_form;
$mform->setDisableShortforms();
$mform->disable_form_change_checker();
// Build the list of options for the enrolment period dropdown.
$unlimitedperiod = get_string('unlimited');
$periodmenu = array();
$periodmenu[''] = $unlimitedperiod;
for ($i=1; $i<=365; $i++) {
$seconds = $i * 86400;
$periodmenu[$seconds] = get_string('numdays', '', $i);
}
$periodmenu = enrol_get_period_list();
// Work out the apropriate default settings.
$defaultperiod = $instance->enrolperiod;
if ($instance->enrolperiod > 0 && !isset($periodmenu[$instance->enrolperiod])) {
Expand Down Expand Up @@ -131,18 +124,38 @@ public function definition() {
$mform->addElement('checkbox', 'recovergrades', get_string('recovergrades', 'enrol'));
$mform->setAdvanced('recovergrades');
$mform->setDefault('recovergrades', $CFG->recovergradesdefault);
$mform->addElement('select', 'duration', get_string('defaultperiod', 'enrol_manual'), $periodmenu);
$mform->setDefault('duration', $defaultperiod);
$mform->setAdvanced('duration');
$mform->addElement('select', 'startdate', get_string('startingfrom'), $basemenu);
$mform->setDefault('startdate', $extendbase);
$mform->setAdvanced('startdate');

$mform->addElement('select', 'duration', get_string('enrolperiod', 'enrol'), $periodmenu);
$mform->setDefault('duration', $defaultperiod);
$mform->setAdvanced('duration');
$mform->disabledIf('duration', 'timeend[enabled]', 'checked', 1);
$mform->addElement('date_time_selector', 'timeend', get_string('enroltimeend', 'enrol'), ['optional' => true]);
$mform->setAdvanced('timeend');
$mform->addElement('hidden', 'id', $course->id);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'action', 'enrol');
$mform->setType('action', PARAM_ALPHA);
$mform->addElement('hidden', 'enrolid', $instance->id);
$mform->setType('enrolid', PARAM_INT);
}

/**
* Validate the submitted form data.
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (!empty($data['startdate']) && !empty($data['timeend'])) {
if ($data['startdate'] >= $data['timeend']) {
$errors['timeend'] = get_string('enroltimeendinvalid', 'enrol');
}
}
return $errors;
}
}
6 changes: 5 additions & 1 deletion enrol/manual/manage.php
Expand Up @@ -29,6 +29,7 @@
$roleid = optional_param('roleid', -1, PARAM_INT);
$extendperiod = optional_param('extendperiod', 0, PARAM_INT);
$extendbase = optional_param('extendbase', 0, PARAM_INT);
$timeend = optional_param_array('timeend', [], PARAM_INT);

$instance = $DB->get_record('enrol', array('id'=>$enrolid, 'enrol'=>'manual'), '*', MUST_EXIST);
$course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST);
Expand Down Expand Up @@ -135,7 +136,10 @@
break;
}

if ($extendperiod <= 0) {
if ($timeend) {
$timeend = make_timestamp($timeend['year'], $timeend['month'], $timeend['day'], $timeend['hour'],
$timeend['minute']);
} else if ($extendperiod <= 0) {
$timeend = 0;
} else {
$timeend = $timestart + $extendperiod;
Expand Down
52 changes: 48 additions & 4 deletions enrol/manual/tests/behat/quickenrolment.feature
Expand Up @@ -108,11 +108,11 @@ Feature: Teacher can search and enrol users one by one into the course
| student098 | Student | 098 | student098@example.com |
| student099 | Student | 099 | student099@example.com |
And the following "courses" exist:
| fullname | shortname |
| Course 001 | C001 |
| fullname | shortname | format | startdate |
| Course 001 | C001 | weeks | ##1 month ago## |
And the following "course enrolments" exist:
| user | course | role |
| teacher001 | C001 | editingteacher |
| user | course | role | timestart |
| teacher001 | C001 | editingteacher | ##1 month ago## |
And I log in as "teacher001"
And I am on "Course 001" course homepage

Expand Down Expand Up @@ -178,3 +178,47 @@ Feature: Teacher can search and enrol users one by one into the course
When I set the field "Select users" to "student100@example.com"
And I click on ".form-autocomplete-downarrow" "css_element" in the "Select users" "form_row"
Then I should see "student100@example.com, 1234567892, 1234567893, ABC1, ABC2"

@javascript
Scenario: Enrol user from participants page
Given I navigate to course participants
# Enrol user to course
And I press "Enrol users"
And I set the field "Select users" to "example.com"
And I expand the "Select users" autocomplete
When I click on "Student 099" item in the autocomplete list
Then I should see "Student 099" in the list of options for the "Select users" autocomplete
And I click on "Show more" "button"
# Fill data to input duration
And "input[name='timeend[enabled]'][checked=checked]" "css_element" should not exist
And the "Enrolment duration" "select" should be enabled
And I set the field "duration" to "2"
# Fill data to input end time
And I set the field "Starting from" to "2"
And I set the field "timeend[enabled]" to "1"
And I set the field "timeend[day]" to "10"
And the "Enrolment duration" "select" should be disabled
And I click on "Enrol users" "button" in the "Enrol users" "dialogue"
And I am on "Course 001" course homepage
And I navigate to course participants
And I should see "Student 099" in the "participants" "table"
And I click on "Edit enrolment" "icon" in the "Student 099" "table_row"
And the field "timeend[day]" matches value "10"

@javascript
Scenario: Update Enrol user
Given I am on "Course 001" course homepage
And I navigate to course participants
When I click on "Edit enrolment" "icon" in the "Teacher 001" "table_row"
Then the "Enrolment duration" "select" should be enabled
# Fill duration
And "input[name='timeend[enabled]'][checked=checked]" "css_element" should not exist
And the "Enrolment duration" "select" should be enabled
And I set the field "duration" to "2"
# Fill end time
And I set the field "timeend[enabled]" to "1"
And I set the field "timeend[day]" to "28"
And the "Enrolment duration" "select" should be disabled
And I press "Save changes"
And I click on "Edit enrolment" "icon" in the "Teacher 001" "table_row"
And the field "timeend[day]" matches value "28"
19 changes: 19 additions & 0 deletions enrol/tests/enrollib_test.php
Expand Up @@ -1080,4 +1080,23 @@ public function test_enrol_get_course_users_roles() {
$this->assertArrayHasKey($roles['student'], $return[$user2->id]);
$this->assertArrayNotHasKey($roles['teacher'], $return[$user2->id]);
}

/**
* Test enrol_calculate_duration function
*/
public function test_enrol_calculate_duration() {
// Start time 07/01/2019 @ 12:00am (UTC).
$timestart = 1561939200;
// End time 07/05/2019 @ 12:00am (UTC).
$timeend = 1562284800;
$duration = enrol_calculate_duration($timestart, $timeend);
$durationinday = $duration / DAYSECS;
$this->assertEquals(4, $durationinday);

// End time 07/10/2019 @ 12:00am (UTC).
$timeend = 1562716800;
$duration = enrol_calculate_duration($timestart, $timeend);
$durationinday = $duration / DAYSECS;
$this->assertEquals(9, $durationinday);
}
}
1 change: 1 addition & 0 deletions lang/en/enrol.php
Expand Up @@ -96,6 +96,7 @@
$string['instanceeditselfwarning'] = 'Warning:';
$string['instanceeditselfwarningtext'] = 'You are enrolled into this course through this enrolment method, changes may affect your access to this course.';
$string['invalidenrolinstance'] = 'Invalid enrolment instance';
$string['invalidenrolduration'] = 'Invalid enrolment duration';
$string['invalidrole'] = 'Invalid role';
$string['invalidrequest'] = 'Invalid request';
$string['manageenrols'] = 'Manage enrol plugins';
Expand Down
27 changes: 27 additions & 0 deletions lib/enrollib.php
Expand Up @@ -1701,6 +1701,33 @@ function enrol_get_course_users($courseid = false, $onlyactive = false, $usersfi
return $DB->get_records_sql($sql . ' ' . implode(' AND ', $conditions), $params);
}

/**
* Get the list of options for the enrolment period dropdown
*
* @return array List of options for the enrolment period dropdown
*/
function enrol_get_period_list() {
$periodmenu = [];
$periodmenu[''] = get_string('unlimited');
for ($i = 1; $i <= 365; $i++) {
$seconds = $i * DAYSECS;
$periodmenu[$seconds] = get_string('numdays', '', $i);
}
return $periodmenu;
}

/**
* Calculate duration base on start time and end time
*
* @param int $timestart Time start
* @param int $timeend Time end
* @return float|int Calculated duration
*/
function enrol_calculate_duration($timestart, $timeend) {
$duration = floor(($timeend - $timestart) / DAYSECS) * DAYSECS;
return $duration;
}

/**
* Enrolment plugins abstract class.
*
Expand Down
28 changes: 28 additions & 0 deletions lib/tests/behat/behat_forms.php
Expand Up @@ -546,4 +546,32 @@ public function i_open_the_autocomplete_suggestions_list() {
$csstarget = ".form-autocomplete-downarrow";
$this->execute('behat_general::i_click_on', [$csstarget, 'css_element']);
}

/**
* Expand the given autocomplete list
*
* @Given /^I expand the "(?P<field_string>(?:[^"]|\\")*)" autocomplete$/
*
* @param string $field Field name
*/
public function i_expand_the_autocomplete($field) {
$csstarget = '.form-autocomplete-downarrow';
$node = $this->get_node_in_container('css_element', $csstarget, 'form_row', $field);
$this->ensure_node_is_visible($node);
$node->click();
}

/**
* Assert the given option exist in the given autocomplete list
*
* @Given /^I should see "(?P<option_string>(?:[^"]|\\")*)" in the list of options for the "(?P<field_string>(?:[^"]|\\")*)" autocomplete$$/
*
* @param string $option Name of option
* @param string $field Field name
*/
public function i_should_see_in_the_list_of_option_for_the_autocomplete($option, $field) {
$xpathtarget = "//div[contains(@class, 'form-autocomplete-selection') and contains(.//div, '" . $option . "')]";
$node = $this->get_node_in_container('xpath_element', $xpathtarget, 'form_row', $field);
$this->ensure_node_is_visible($node);
}
}

0 comments on commit 0fc3082

Please sign in to comment.