public function getTotalWeeklyHours() { $total = 0; $selectedCourses = array(); // Get an array of selected courses foreach ($this->getCourses() as $courseId) { array_push($selectedCourses, CourseRepository::GetById($courseId)); } // Iterate through the array and sum the weekly hours foreach ($selectedCourses as $course) { $total += $course->getHours(); } return $total; }
$pass = isset($_POST["pass"]) ? Str::sanitize($_POST["pass"]) : ""; $confirmPass = isset($_POST["confirmPass"]) ? Str::sanitize($_POST["confirmPass"]) : ""; $courses = isset($_POST["courses"]) ? $_POST["courses"] : []; // Define validation rule messages $rules = array('empty' => 'This field cannot be empty', 'valid_phone' => 'Must be in the format of (NNN) NNN-NNNN', 'valid_postal' => 'Must be in the format of A1A 1A1, with or without spaces, case insensitive', 'strong_pass' => 'Password does not meet the required strength, please try again', 'pass_match' => 'Password does not match', 'hours_min' => 'Selected course hours cannot be less than 10 hours per week', 'hours_max' => 'Selected course hours cannot exceed 20 hours per week'); $val = new Validator(); $error_flag = false; $rule = ""; $totalHours = 0; // On submit if (isset($_POST["submit"])) { $total = 0; $selectedCourses = array(); // Get an array of selected courses foreach ($courses as $courseId) { array_push($selectedCourses, CourseRepository::GetById($courseId)); } // Iterate through the array and sum the weekly hours foreach ($selectedCourses as $course) { $totalHours += $course->getHours(); } if (!$val->is_valid($username) || !$val->is_phone($phone) || !$val->is_postal($postal) || !$val->is_strong_pass($pass) || !$val->compare($pass, $confirmPass) || !$val->has_items($courses) || $totalHours < 10 || $totalHours > 20) { $error_flag = true; } if (!$error_flag) { // Create a student from the data $student = new Student($username, $courses); // Store the student object in the session session_start(); $_SESSION["student"] = $student; // Redirect to result page