MDL-64214 core_calendar: fix event types form element handling

This commit is contained in:
Simey Lameze
2018-11-29 13:59:32 +08:00
parent df6d0b6d45
commit b13fe83adb
3 changed files with 31 additions and 9 deletions
@@ -40,6 +40,7 @@ class managesubscriptions extends \moodleform {
* Defines the form used to add calendar subscriptions.
*/
public function definition() {
global $PAGE;
$mform = $this->_form;
$eventtypes = calendar_get_allowed_event_types();
if (in_array(true, $eventtypes, true) === false) {
@@ -86,6 +87,9 @@ class managesubscriptions extends \moodleform {
// Eventtype: 0 = user, 1 = global, anything else = course ID.
$mform->addElement('submit', 'add', get_string('add'));
// Add the javascript required to enhance this mform.
$PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]);
}
/**
-8
View File
@@ -2711,14 +2711,6 @@ function calendar_add_subscription($sub) {
unset($sub->groupcourseid);
}
// Pull the group id back out of the value. The form saves the value
// as "<courseid>-<groupid>" to allow the javascript to work correctly.
if (!empty($sub->groupid)) {
list($courseid, $groupid) = explode('-', $sub->groupid);
$sub->courseid = $courseid;
$sub->groupid = $groupid;
}
// Default course id if none is set.
if (empty($sub->courseid)) {
if ($sub->eventtype === 'site') {
+27 -1
View File
@@ -71,7 +71,33 @@ $importresults = '';
$formdata = $form->get_data();
if (!empty($formdata)) {
require_sesskey(); // Must have sesskey for all actions.
$subscriptionid = calendar_add_subscription($formdata);
// The course field on group event type is named groupcourseid.
// So it's easy to assume it is going to add a subscription as group events.
if (isset($formdata->groupcourseid)) {
$courseid = $formdata->groupcourseid;
// We need to fetch the groups again for group event type.
// The groupid gets filtered out as part of the $form->get_data call because there form is originally
// constructed without any options for the groups.
// Consequently with no options it means there cant be a valid value so it end up ignoring it.
require_once($CFG->libdir . '/grouplib.php');
$groupcoursedata = groups_get_course_data($courseid);
$groups = [];
if (!empty($groupcoursedata->groups)) {
foreach ($groupcoursedata->groups as $groupid => $groupdata) {
$groups[$groupid] = $groupdata->name;
}
}
$simulatedformdata = [
'groups' => $groups,
'courseid' => $courseid,
];
// As commented above, we need to reconstruct the list of valid group options and recreate the form with
// those options for the form validation to recognise that the groupid value in the POST data is valid.
$form = new \core_calendar\local\event\forms\managesubscriptions(null, $simulatedformdata);
} else {
$subscriptionid = calendar_add_subscription($formdata);
}
if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
// Blank the URL if it's a file import.
$formdata->url = '';