function pad_zero($value) { return sprintf("%02d", $value); } if (isset($_POST['module']) && !empty($_POST['module'])) { $nom_module = $_POST['module']; $sql = "SELECT *, matieres.alias as alias, enseignements.nom as nom_enseignement,seances.dureeSeance as seanceDuree, seances.commentaire as seancesCommentaire FROM matieres, seances LEFT JOIN (enseignements) ON (seances.codeEnseignement=enseignements.codeEnseignement) where seances.deleted='0' and seances.codeSeance!='' AND enseignements.deleted='0' AND enseignements.codeMatiere = matieres.codeMatiere and enseignements.nom like " . $dbh->quote("%" . $nom_module . "%", PDO::PARAM_STR) . " order by seances.dateSeance,seances.heureSeance "; $req4 = $dbh->prepare($sql); $req4->execute(); while ($res_4 = $req4->fetch()) { $annee = substr($res_4['dateSeance'], 0, 4); $mois = substr($res_4['dateSeance'], 5, 2); $jour = substr($res_4['dateSeance'], 8, 2); $nom_jour = date("l", mktime(0, 0, 0, $mois, $jour, $annee)); //traduction francais du nom du jour $nom_jour = translateDay($nom_jour); unset($req_type); /* $sqlType="SELECT * FROM types_activites WHERE codeTypeActivite=".$res_4['codeTypeActivite']; $req_type=$dbh->prepare($sqlType); $req_type->execute(); while($res_type = $req_type->fetch()) { $nom_type_seance = $res_type['alias']; } $req_type->closeCursor(); */ // enseignement $enseignement = $res_4['alias']; $nom_type_seance = ''; $duree = pad_zero(floor($res_4["seanceDuree"] / 100)) . 'h' . pad_zero(floor($res_4["seanceDuree"] % 100));
$timequery[] = "(start >= 480 AND start < 720)"; } if (in_array("aftn", $times)) { $timequery[] = "(start >= 720 AND start < 1020)"; } if (in_array("even", $times)) { $timequery[] = "(start > 1020)"; } $timeConstraints[] = "(" . implode(" OR ", $timequery) . ")"; // Make it a single string (condition OR condition ...) } if ($days) { // Process the day constraints $dayquery = array(); foreach ($days as $day) { $dayquery[] = "day = " . translateDay($day); } $timeConstraints[] = "(" . implode(" OR ", $dayquery) . ")"; // Do the same as we did with the times } if (count($timeConstraints)) { // Now cram the two together into one concise subquery $query .= " AND s.id IN (SELECT section FROM times WHERE " . implode(" AND ", $timeConstraints) . ")"; } // Run it! $result = mysql_query($query); if (!$result) { echo json_encode(array("error" => "mysql", "msg" => "An error occurred while searching the database.")); break; } if (mysql_num_rows($result) == 0) {
// Set the course title depending on its section title if ($section['sectiontitle'] != NULL) { $section['title'] = $section['sectiontitle']; } else { $section['title'] = $section['coursetitle']; } unset($section['sectiontitle']); unset($section['coursetitle']); // If it's online, don't bother looking up the times if ($section['type'] == "O") { $section['online'] = true; $sections[] = $section; continue; } $query = "SELECT day, start, end, building, room FROM times WHERE times.section = {$section['id']} ORDER BY day, start"; $timeResult = mysql_query($query); if (!$timeResult) { die(json_encode(array("error" => "mysql", "msg" => mysql_error()))); } while ($time = mysql_fetch_assoc($timeResult)) { $time['start'] = translateTime($time['start']); $time['end'] = translateTime($time['end']); $time['day'] = translateDay($time['day']); $section['times'][] = $time; } $sections[] = $section; } // Spit out the json echo json_encode(array("sections" => $sections)); break; }
// If both the nonCourse items AND the course items list is empty, we can't draw a schedule if (empty($courseSet) && empty($nonCourseSet)) { die(json_encode(array("error" => "user", "msg" => "Cannot generate schedules because no courses or course items were provided"))); } // Process the list of noCourse Times $noCourseSet = array(); for ($i = 1; $i <= $_POST['noCourseCount']; $i++) { // If there are no days set for the time slot, ignore it if (empty($_POST["noCourseDays{$i}"])) { continue; } // Create a new noCourse time slot $noCourse = array(); $noCourse['times'] = array(); foreach ($_POST["noCourseDays{$i}"] as $day) { $noCourse['times'][] = array("day" => translateDay($day), "start" => $_POST["noCourseStartTime{$i}"], "end" => $_POST["noCourseEndTime{$i}"]); } $noCourseSet[] = $noCourse; } // Generate valid schedules, and include the errors if we're being verbose $results = array(); if (!empty($courseSet)) { $results['schedules'] = generateSchedules($courseSet, $nonCourseSet, $noCourseSet); } else { $results['schedules'] = array(array()); } // Add the nonCourse items to the schedules (they are guaranteed not to overlap via generateSchedules) foreach ($results['schedules'] as $k => $schedule) { foreach ($nonCourseSet as $nonCourse) { $results['schedules'][$k][] = $nonCourse; }
break; } if (mysql_num_rows($result) == 0) { echo json_encode(array("error" => "result", "msg" => "No courses matched your criteria")); break; } // Now we build an array of the results $courses = array(); while ($row = mysql_fetch_assoc($result)) { $courses[] = $row; } // @todo: store this in session to avoid lengthy and costly queries // Now pick a course at random, grab it's times, $courseNum = rand(0, count($courses) - 1); $query = "SELECT day, start, end, building, room FROM times WHERE section={$courses[$courseNum]['id']}"; $result = mysql_query($query); if (!$result) { echo json_encode(array("error" => "mysql", "msg" => mysql_error())); break; } $courses[$courseNum]['times'] = array(); while ($row = mysql_fetch_assoc($result)) { $session = array('day' => translateDay($row['day']), 'start' => translateTime($row['start']), 'end' => translateTime($row['end']), 'bldg' => $row['building'], 'room' => $row['room']); $courses[$courseNum]['times'][] = $session; } echo json_encode($courses[$courseNum]); break; default: echo json_encode(array("error" => "argument", "msg" => "Invalid or no action provided", "arg" => "action")); break; }