MDL-59921 calendar: replace selector for course events
authorSimey Lameze <simey@moodle.com>
Mon, 9 Oct 2017 01:35:51 +0000 (09:35 +0800)
committerSimey Lameze <simey@moodle.com>
Mon, 9 Oct 2017 01:51:14 +0000 (09:51 +0800)
calendar/classes/local/event/forms/create.php
calendar/classes/local/event/mappers/create_update_form_mapper.php
calendar/lib.php

index 8892bfb..0b7c175 100644 (file)
@@ -132,6 +132,10 @@ class create extends \moodleform {
             }
         }
 
+        if ($eventtype == 'course' && empty($data['courseid'])) {
+            $errors['courseid'] = 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)) {
@@ -237,13 +241,7 @@ class create extends \moodleform {
         }
 
         if (isset($eventtypes['course'])) {
-            $courseoptions = [];
-            foreach ($eventtypes['course'] as $course) {
-                $courseoptions[$course->id] = format_string($course->fullname, true,
-                    ['context' => \context_course::instance($course->id)]);
-            }
-
-            $mform->addElement('select', 'courseid', get_string('course'), $courseoptions);
+            $mform->addElement('course', 'courseid', get_string('course'), ['limittoenrolled' => true]);
             $mform->hideIf('courseid', 'eventtype', 'noteq', 'course');
         }
 
index 75a688f..23184b3 100644 (file)
@@ -61,7 +61,10 @@ class create_update_form_mapper implements create_update_form_mapper_interface {
             $data->groupid = "{$legacyevent->courseid}-{$legacyevent->groupid}";
             $data->groupcourseid = $legacyevent->courseid;
         }
-
+        if ($legacyevent->eventtype == 'course') {
+            // Set up the correct value for the to display on the form.
+            $data->courseid = $legacyevent->courseid;
+        }
         $data->description = [
             'text' => $data->description,
             'format' => $data->format
@@ -87,6 +90,19 @@ class create_update_form_mapper implements create_update_form_mapper_interface {
 
         // Undo the form definition work around to allow us to have two different
         // course selectors present depending on which event type the user selects.
+        if ($data->eventtype == 'course') {
+            // Default course id if none is set.
+            if (!isset($properties->courseid)) {
+                if ($properties->eventtype === 'site') {
+                    $properties->courseid = SITEID;
+                } else {
+                    $properties->courseid = 0;
+                }
+            } else {
+                $properties->courseid = $data->courseid;
+            }
+        }
+
         if (isset($data->groupcourseid)) {
             $properties->courseid = $data->groupcourseid;
             unset($properties->groupcourseid);
@@ -99,14 +115,6 @@ class create_update_form_mapper implements create_update_form_mapper_interface {
             $properties->groupid = $groupid;
         }
 
-        // Default course id if none is set.
-        if (!isset($properties->courseid)) {
-            if ($properties->eventtype === 'site') {
-                $properties->courseid = SITEID;
-            } else {
-                $properties->courseid = 0;
-            }
-        }
 
         // Decode the form fields back into valid event property.
         $properties->timeduration = $this->get_time_duration_from_form_data($data);
index 48754d1..3f72345 100644 (file)
@@ -2481,7 +2481,7 @@ function calendar_get_all_allowed_types() {
             if (!isset($types['course'])) {
                 $types['course'] = [$course];
             } else {
-                $types['course'][] = $course;
+                $types['course'][$course->id] = $course;
             }
         }