From a9146bef1fac6723a6f41a73a553672fb75fa49f Mon Sep 17 00:00:00 2001 From: Paolo Tramonti Date: Thu, 31 Jul 2014 14:48:46 +0200 Subject: [PATCH] When Multiple sessions is selected and no valid days of the week are selected show a tidy error. Fixed no-daysofweek-selected issue --- add_form.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/add_form.php b/add_form.php index a9550a3..46764e2 100644 --- a/add_form.php +++ b/add_form.php @@ -175,7 +175,37 @@ class mod_attendance_add_form extends moodleform { $data['sdays']= array(); $errors['sdays'] = get_string('required', 'attendance'); } + if (isset($data['sdays'])) { + if (!$this->checkWeekDays($data['sessiondate'], $data['sessionenddate'], $data['sdays']) ) { + $errors['sdays'] = get_string('checkweekdays', 'attendance'); + } + } return $errors; } + private function checkWeekDays($sessiondate, $sessionenddate, $sdays) { + + $found = false; + + $daysOfWeek = array(0 => "Sun", 1 => "Mon", 2 => "Tue", 3 => "Wed", 4 => "Thu", 5 => "Fri", 6 => "Sat"); + $start = new DateTime( date("Y-m-d",$sessiondate) ); + $interval = new DateInterval('P1D'); + $end = new DateTime( date("Y-m-d",$sessionenddate) ); + $end->add( new DateInterval('P1D') ); + + $period = new DatePeriod($start, $interval, $end); + foreach ($period as $date) { + if (!$found) { + foreach ($sdays as $name => $value) { + $key = array_search($name, $daysOfWeek); + if ($date->format("w") == $key) { + $found = true; + break; + } + } + } + } + + return $found; + } }