MDL-57793 calendar: Add recursion to satisfy COUNT rule
authorJun Pataleta <jun@moodle.com>
Fri, 17 Mar 2017 05:27:20 +0000 (13:27 +0800)
committerJun Pataleta <jun@moodle.com>
Tue, 18 Apr 2017 05:55:25 +0000 (13:55 +0800)
* Add recursion to the creation of recurring calendar events to satisfy
  the COUNT rule, if necessary.

calendar/classes/rrule_manager.php

index 78cee59..ea8e184 100644 (file)
@@ -721,6 +721,20 @@ class rrule_manager {
             unset($cloneevent->id);
             \calendar_event::create($cloneevent, false);
         }
+
+        // If COUNT rule is defined and the number of the generated event times is less than the the COUNT rule,
+        // repeat the processing until the COUNT rule is satisfied.
+        if ($count !== false && $count > 0) {
+            // Set count to the remaining counts.
+            $this->count = $count;
+            // Clone the original event, but set the timestart to the last generated event time.
+            $tmpevent = clone($event);
+            $tmpevent->timestart = end($eventtimes);
+            // Generate the additional event times.
+            $additionaleventtimes = $this->generate_recurring_event_times($tmpevent);
+            // Create the additional events.
+            $this->create_recurring_events($event, $additionaleventtimes);
+        }
     }
 
     /**