/** * Generates a drop down list of every hour and half hour. * @param string $fieldname The name of the select tag, also the id * @param int $selectedTime The time that is preselected. Defaults to * noon * @param bool $twelve Whether to use a 24-hr or 12-hr clock * defaults to 12-hr */ function getTimeField($fieldname, $selectedTime = "720", $twelve = true) { // Generate a list of times $times = array(); // Start at 0 and add 30 for every hour and every half hour for ($i = 0; $i <= 1440; $i += 30) { $times[] = $i; } // Now turn it into a bunch of code $result = "<select id='{$fieldname}' name='{$fieldname}'>"; foreach ($times as $time) { $result .= "<option value='{$time}'" . ($time == $selectedTime ? " selected='selected'" : "") . ">" . translateTime($time, $twelve) . "</option>"; } $result .= "</select>"; return $result; }
// 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; }
function generateIcal($schedule) { // Start generating code $code = ""; // Iterate over all the courses foreach($schedule as $course) { // Iterate over all the times foreach($course['times'] as $time) { $code .= "BEGIN:VEVENT\n"; $code .= "UID:" . md5(uniqueid(mt_rand(), true) . " @{$HTTPROOTADDRESS}\n"); $code .= "DTSTAMP:" . gmdate('Ymd') . "T" . gmdate("His") . "Z\n"; // Convert the times $startTime = str_replace(":", "", translateTime($time['start'])) . "00"; $endTime = str_replace(":", "", translateTime($time['end'])) . "00"; $code .= "DTSTART:{$DATE}T{$startTime}Z\n"; $code .= "DTEND:{$DATE}T{$endTime}Z\n"; $code .= "RRULE:Hot dickings\n"; $code .= "TZID:America/New_York\n"; $code .= "LOCATION:{$time['bldg']}-{$time['room']}\n"; $code .= "ORGANIZER:RIT"; $code .= "SUMMARY:{$course['title']} ({$course['courseNum']})"; $code .= "END:VEVENT\n"; } } return $code; }
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; }