diff --git a/course/lib.php b/course/lib.php index c255e555347..a20a219577c 100644 --- a/course/lib.php +++ b/course/lib.php @@ -3710,6 +3710,41 @@ function core_course_inplace_editable($itemtype, $itemid, $newvalue) { } } +/** + * This function calculates the minimum and maximum cutoff values for the timestart of + * the given event. + * + * It will return an array with two values, the first being the minimum cutoff value and + * the second being the maximum cutoff value. Either or both values can be null, which + * indicates there is no minimum or maximum, respectively. + * + * If a cutoff is required then the function must return an array containing the cutoff + * timestamp and error string to display to the user if the cutoff value is violated. + * + * A minimum and maximum cutoff return value will look like: + * [ + * [1505704373, 'The date must be after this date'], + * [1506741172, 'The date must be before this date'] + * ] + * + * @param calendar_event $event The calendar event to get the time range for + * @param stdClass $course The course object to get the range from + * @return array Returns an array with min and max date. + */ +function core_course_core_calendar_get_valid_event_timestart_range(\calendar_event $event, $course) { + $mindate = null; + $maxdate = null; + + if ($course->startdate) { + $mindate = [ + $course->startdate, + get_string('errorbeforecoursestart', 'calendar') + ]; + } + + return [$mindate, $maxdate]; +} + /** * Returns course modules tagged with a specified tag ready for output on tag/index.php page * diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 297e54b4c85..1bdf3b52d25 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -4717,4 +4717,55 @@ class core_course_courselib_testcase extends advanced_testcase { $this->assertEquals($expectedcourses, $actual); $this->assertEquals($expectedprocessedcount, $processedcount); } + + + /** + * Testing core_course_core_calendar_get_valid_event_timestart_range when the course has no end date. + */ + public function test_core_course_core_calendar_get_valid_event_timestart_range_no_enddate() { + global $CFG; + require_once($CFG->dirroot . "/calendar/lib.php"); + + $this->resetAfterTest(true); + $this->setAdminUser(); + $generator = $this->getDataGenerator(); + $now = time(); + $course = $generator->create_course(['startdate' => $now - 86400]); + + // Create a course event. + $event = new \calendar_event([ + 'name' => 'Test course event', + 'eventtype' => 'course', + 'courseid' => $course->id, + ]); + + list ($min, $max) = core_course_core_calendar_get_valid_event_timestart_range($event, $course); + $this->assertEquals($course->startdate, $min[0]); + $this->assertNull($max); + } + + /** + * Testing core_course_core_calendar_get_valid_event_timestart_range when the course has end date. + */ + public function test_core_course_core_calendar_get_valid_event_timestart_range_with_enddate() { + global $CFG; + require_once($CFG->dirroot . "/calendar/lib.php"); + + $this->resetAfterTest(true); + $this->setAdminUser(); + $generator = $this->getDataGenerator(); + $now = time(); + $course = $generator->create_course(['startdate' => $now - 86400, 'enddate' => $now + 86400]); + + // Create a course event. + $event = new \calendar_event([ + 'name' => 'Test course event', + 'eventtype' => 'course', + 'courseid' => $course->id, + ]); + + list ($min, $max) = core_course_core_calendar_get_valid_event_timestart_range($event, $course); + $this->assertEquals($course->startdate, $min[0]); + $this->assertNull($max); + } }