MDL-59921 calendar: replace selector for groups events
authorSimey Lameze <simey@moodle.com>
Mon, 9 Oct 2017 01:48:42 +0000 (09:48 +0800)
committerSimey Lameze <simey@moodle.com>
Mon, 9 Oct 2017 01:51:14 +0000 (09:51 +0800)
calendar/amd/build/event_form.min.js
calendar/amd/src/event_form.js
calendar/classes/local/event/forms/create.php
calendar/classes/local/event/mappers/create_update_form_mapper.php
calendar/lib.php

index 1f6e3ff..0abfe87 100644 (file)
Binary files a/calendar/amd/build/event_form.min.js and b/calendar/amd/build/event_form.min.js differ
index f537f6d..2b6707d 100644 (file)
@@ -74,14 +74,14 @@ define(['jquery'], function($) {
         var filterGroupSelectOptions = function() {
             var selectedCourseId = courseGroupSelect.val();
             var selectedIndex = null;
         var filterGroupSelectOptions = function() {
             var selectedCourseId = courseGroupSelect.val();
             var selectedIndex = null;
-
+            var hasGroups = false;
             groupSelectOptions.each(function(index, element) {
                 element = $(element);
 
                 if (element.attr('data-course-id') == selectedCourseId) {
                     element.removeClass('hidden');
                     element.prop('disabled', false);
             groupSelectOptions.each(function(index, element) {
                 element = $(element);
 
                 if (element.attr('data-course-id') == selectedCourseId) {
                     element.removeClass('hidden');
                     element.prop('disabled', false);
-
+                    hasGroups = true;
                     if (selectedIndex === null || element.attr('selected')) {
                         selectedIndex = index;
                     }
                     if (selectedIndex === null || element.attr('selected')) {
                         selectedIndex = index;
                     }
@@ -91,6 +91,12 @@ define(['jquery'], function($) {
                 }
             });
 
                 }
             });
 
+            if (hasGroups) {
+                groupSelect.prop('disabled', false);
+            } else {
+                groupSelect.prop('disabled', true);
+            }
+
             groupSelect.prop('selectedIndex', selectedIndex);
         };
 
             groupSelect.prop('selectedIndex', selectedIndex);
         };
 
index 0b7c175..0f482a9 100644 (file)
@@ -136,6 +136,10 @@ class create extends \moodleform {
             $errors['courseid'] = get_string('selectacourse');
         }
 
             $errors['courseid'] = get_string('selectacourse');
         }
 
+        if ($eventtype == 'group' && empty($data['groupcourseid'])) {
+            $errors['groupcourseid'] = get_string('selectacourse');
+        }
+
         if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
             $errors['durationgroup'] = get_string('invalidtimedurationuntil', 'calendar');
         } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
         if ($data['duration'] == 1 && $data['timestart'] > $data['timedurationuntil']) {
             $errors['durationgroup'] = get_string('invalidtimedurationuntil', 'calendar');
         } else if ($data['duration'] == 2 && (trim($data['timedurationminutes']) == '' || $data['timedurationminutes'] < 1)) {
@@ -246,13 +250,11 @@ class create extends \moodleform {
         }
 
         if (isset($eventtypes['group'])) {
         }
 
         if (isset($eventtypes['group'])) {
-            $courseoptions = [];
-            foreach ($eventtypes['groupcourses'] as $course) {
-                $courseoptions[$course->id] = format_string($course->fullname, true,
-                    ['context' => \context_course::instance($course->id)]);
-            }
+            // Exclude courses without group.
+            $excludedcourses = array_diff(array_keys($eventtypes['course']), array_keys($eventtypes['groupcourses']));
 
 
-            $mform->addElement('select', 'groupcourseid', get_string('course'), $courseoptions);
+            $options = ['limittoenrolled' => true, 'exclude' => $excludedcourses];
+            $mform->addElement('course', 'groupcourseid', get_string('course'), $options);
             $mform->hideIf('groupcourseid', 'eventtype', 'noteq', 'group');
 
             $groupoptions = [];
             $mform->hideIf('groupcourseid', 'eventtype', 'noteq', 'group');
 
             $groupoptions = [];
@@ -266,6 +268,7 @@ class create extends \moodleform {
 
             $mform->addElement('select', 'groupid', get_string('group'), $groupoptions);
             $mform->hideIf('groupid', 'eventtype', 'noteq', 'group');
 
             $mform->addElement('select', 'groupid', get_string('group'), $groupoptions);
             $mform->hideIf('groupid', 'eventtype', 'noteq', 'group');
+            $mform->disabledIf('groupid', 'groupcourseid', 'eq', '');
         }
     }
 
         }
     }
 
index 23184b3..2f0326a 100644 (file)
@@ -101,21 +101,20 @@ class create_update_form_mapper implements create_update_form_mapper_interface {
             } else {
                 $properties->courseid = $data->courseid;
             }
             } else {
                 $properties->courseid = $data->courseid;
             }
-        }
-
-        if (isset($data->groupcourseid)) {
-            $properties->courseid = $data->groupcourseid;
-            unset($properties->groupcourseid);
-        }
+        } else if ($data->eventtype == 'group') {
+            if (isset($data->groupcourseid)) {
+                $properties->courseid = $data->groupcourseid;
+                unset($properties->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 (isset($data->groupid)) {
-            list($courseid, $groupid) = explode('-', $data->groupid);
-            $properties->groupid = $groupid;
+            // 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 (isset($data->groupid)) {
+                list($courseid, $groupid) = explode('-', $data->groupid);
+                $properties->groupid = $groupid;
+            }
         }
 
         }
 
-
         // Decode the form fields back into valid event property.
         $properties->timeduration = $this->get_time_duration_from_form_data($data);
 
         // Decode the form fields back into valid event property.
         $properties->timeduration = $this->get_time_duration_from_form_data($data);
 
index 3f72345..916ed6e 100644 (file)
@@ -2489,7 +2489,7 @@ function calendar_get_all_allowed_types() {
             if (!isset($types['groupcourses'])) {
                 $types['groupcourses'] = [$course];
             } else {
             if (!isset($types['groupcourses'])) {
                 $types['groupcourses'] = [$course];
             } else {
-                $types['groupcourses'][] = $course;
+                $types['groupcourses'][$course->id] = $course;
             }
 
             if (!isset($types['group'])) {
             }
 
             if (!isset($types['group'])) {