3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * renders tables using json output from API call * * PHP version 5 * * @author Matt Scaperoth <mscapero@gwu.edu> * */ ?> <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> <style> .center-wrapper .panel-pane{ margin-bottom:0; } </style> <h2>GW Lecture Capture Locations</h2> <p>All lecture capture classrooms record instructor audio and presentation materials. Select classrooms are also equipped with a camera to record video of the instructor, along with the audio and presentation materials.</p> <br /> <?php //================================== // MAIN: // edit variables for program here //================================== $API_url = "http://classrooms.at.gwu.edu"; $room_feature_type = "capabilities"; $room_feature_name = "lecture capture enabled"; //get the data //$json = get_data($API_url, $room_feature_type, $room_feature_name); //sort by room number //usort($json, "cmp"); // DATA ORGANIZATION //display names of campuses $foggy_title = "Foggy Bottom Campus"; $mvc_title = "Mount Vernon Campus"; $virginia_title = "Virginia Science and Technology Campus"; //links for pdf maps $foggy_bottom_map_url = "https://acadtech.gwu.edu/files/downloads/Foggy%20Bottom%20Campus%20Map.pdf"; $mount_vernon_map_url = "https://acadtech.gwu.edu/files/downloads/Mount%20Vernon%20Campus%20Map.pdf"; $virginia_map_url = "https://acadtech.gwu.edu/files/downloads/VSTC%20Map.pdf"; //campus details $foggy_info = array( "title" => $foggy_title, "link" => "foggy-bottom", "map" => $foggy_bottom_map_url, "buildings" => array(), "html" => null, "notes" => null, ); $mvc_info = array( "title" => $mvc_title, "link" => "mvc", "map" => $foggy_bottom_map_url, "buildings" => array(), "html" => null, "notes" => "*Equipment in Ames Hall is supported by MVC Faculty & Instructional Support, (202) 242-6670.", ); $vstc_info = array( "title" => $virginia_title, "link" => "vstc", "map" => $foggy_bottom_map_url, "buildings" => array(), "html" => null, "notes" => null, ); //we will store each table as it is created $campuses = array("FB" => $foggy_info, "MVC" => $mvc_info, "VSTC" => $vstc_info); //start by building the tables for each campus foreach ($campuses as $campus_info => $value) { //$campuses[$campus_info]["html"] = open_table($campuses[$campus_info]); } //-------------------------- // Process and Render Data //-------------------------- //generate the html tables using json data and campus info //$campuses = build_html($json, $campuses, $room_feature_name); //clean up the output with styles and sorting //$campuses = clean_output($campuses); //return the output to the screen //render($campuses); //================================== // FUNCTIONS // Here you can edit the output // and the data sources //==================================== //-------------------------- // Output Functions //-------------------------- /** * opens html table * @param array() $campus_info * @return String */ function open_table($campus_info) { return htmlspecialchars("<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width: 100%;\"><tbody>" . "<thead> <tr> <th colspan=\"3\" scope=\"col\" style=\"border-color: rgb(51, 51, 51); background-color: rgb(231, 244, 246);text-align:center\"> {$campus_info['title']} (<a href=\"{$campus_info['link']}\" target=\"_blank\">map</a>) </th> </tr> </thead>"); } /** * (b) closes html table * @param mixed $campus_info */ function close_table($campus_info) { return htmlspecialchars("</tr></tbody></table>"); } /** * cleans upthe output by providing <p> wrappers and adding notes * @param mixed $campuses * @return mixed function clean_output($campuses) { foreach ($campuses as $campus_info => $value) { //set pointers to manipulate $campuses directly $campus_info_ptr = &$campuses[$campus_info]; $html_ptr = &$campus_info_ptr["html"]; $notes_ptr = &$campus_info_ptr["notes"]; //close the tables $html_ptr .= close_table($campus_info_ptr); //wrap tables in <p> tags $html_ptr = "<p>" . $html_ptr . "</p>"; //add notes at the end of output $html_ptr .= $notes_ptr; //sorting ksort($campus_info_ptr["buildings"]); } return $campuses; } */ /** * used by uksort on json to sort buildings and rooms function cmp($a, $b) { if ($a->building_name == $b->building_name) { if ($a->room_num == $b->room_num) { return 0; } //do a second-degree sort on room number if (is_numeric($a->room_num) && !is_numeric($b->room_num)) { return 1; } else if (!is_numeric($a->room_num) && is_numeric($b->room_num)) { return -1; } else { return ($a->room_num < $b->room_num) ? -1 : 1; } } else { //if buildings are different, sort on building name return ($a->building_name < $b->building_name) ? -1 : 1; } } */ /** * echos the html from the room info * @param mixed $campuses array of campus information */ function render($campuses) { if (is_array($campuses) || is_object($campuses)) { foreach ($campuses as $campus_info) { echo $campuses[$campus_info]["html"]; } } } /** * generates tables from json data * @param array(Objects) $json * @param array(array()) $campuses * @return mixed */ function build_html($json, $campuses, $room_feature_name) { //used to filter out unwanted parts of name in building $replace_this_string1 = "MVC-"; $replace_this_string2 = "VSTC-"; $replace_these_strings = "/^$replace_this_string1|^$replace_this_string2/"; //regex //track the number of rooms so far $number_of_rooms = 0; foreach ($json as $item) { $campus = $item->campus; $campus_info = &$campuses[$campus]; //room information $room_num = $item->room_num; $room_class = ""; $room_link = ""; //use this variable to put commas between rooms $room_separator = ""; //trim unwanted characters from building name $item->building_name = preg_replace($replace_these_strings, '', $item->building_name); //start new building var $building = array( "name" => $item->building_name, "old_name" => $item->building_name, ); //format building name to make it look nice $building = format_building_name($building); //if building is not a part of campus info do work if (!in_array($building["name"], $campus_info["buildings"])) { //new building means new room count $number_of_rooms = 0; //if not first building, end previous cell and row if (!empty($campus_info["buildings"])) { //close previous cell $campus_info["html"] .= htmlspecialchars("</td>"); //close previous row $campus_info["html"] .= htmlspecialchars("</tr>"); } //add building to list of buildings array_push($campus_info["buildings"], $building["name"]); //create new row. will be closed in next building or at the end $campus_info["html"] .= htmlspecialchars("<tr>"); //create new cell $campus_info["html"] .= htmlspecialchars("<td width=\"50%\" id=\"{$building['name']}\">"); $campus_info["html"] .= htmlspecialchars($building["name"]); //close building cell $campus_info["html"] .= htmlspecialchars("</td>"); //start new cell for room numbers $campus_info["html"] .= htmlspecialchars("<td width=\"50%\">"); } //if it's not the first room, ad a comma before it $room_separator = $number_of_rooms > 0 ? ", " : ""; //for each building add the rooms //check to see if room has video and add camera icon if necessary if (stripos($room_feature_name, "lecture capture") !== false) { if (in_arrayi("(camera)", $item->equipment)) { $room_class = "fa fa-video-camera"; } } //only grab first "word" in building name to make for easier queries $building_name_no_spaces = strtok($building["old_name"], ' '); //generate the link for the website $room_link = "/location?building=$building_name_no_spaces&room=$room_num"; $campus_info["html"] .= htmlspecialchars("$room_separator<a href=\"$room_link\" target=\"_blank\">$room_num <i class=\"$room_class\"></i></a>"); $number_of_rooms++; } return $campuses; } /** * Case-insensitive in_array() wrapper. * https://gist.github.com/sepehr/6351397 * * @param mixed $needle Value to seek. * @param array $haystack Array to seek in. * * @return bool */ function in_arrayi($needle, $haystack) { foreach ($haystack as $value) { if (stripos($value, $needle) !== false) { return true; } } return false; } /** * formats building name to be more user friendly * @param array() $building * @return array() formatted $building */ function format_building_name($building) { switch ($building["name"]) { case "Ames": $building["old_name"] = $building["name"]; $building["name"] .= ' Hall*'; break; case "Corcoran": case "Discovery": case "Duques": case "Exploration": case "Funger": case "Innovation": case "Phillips": case "Rome": $building["old_name"] = $building["name"]; $building["name"] .= ' Hall'; break; case "Government": $building["old_name"] = $building["name"]; $building["name"] = 'Hall of ' . $building["name"]; break; case "GWSPH": $building["old_name"] = "GWSPH"; $building["name"] = "Milken Institute School of Public Health"; break; case "MPA": $building["old_name"] = "MPA"; $building["name"] = "Media and Public Affairs Building"; break; case "SEH": $building["old_name"] = "SEH"; $building["name"] = "Science and Engineering Hall"; break; } return $building; } //-------------------------- // json Functions //-------------------------- /** * uses API to get classroom data * @param String $room_feature_type item type: capabilities, software, quickcodes, etc. * @param String $room_feature_name name of item to look for: lecture capture, video conferencing, etc. * @return array(Objects) */ function get_data($API_url, $room_feature_type, $room_feature_name) { //get all capabilities available $capabilities = curl("$API_url/api/campuses/$room_feature_type"); //get the ids of the capabilities with lecture capture in the name $ids_of_lect_capt = searchJson($capabilities, "name", $room_feature_name); $rooms = array(); //get the ids of the rooms with lecture capture foreach ($ids_of_lect_capt as $cap_id) { $ids_of_rooms = curl("$API_url/api/campuses/capability/$cap_id/rooms"); foreach ($ids_of_rooms as $room) { array_push($rooms, $room->id); } } $json = array(); //get the details for each of the rooms and add it into one object foreach ($rooms as $room_id) { $room_details = curl("$API_url/api/campuses/building/room/$room_id/details"); array_push($json, $room_details); } return $json; } /** * performs curl * @param String $url * @return array(Objecs) */ function curl($url) { //echo '<h3 class="Legend">PHP</h3>'; // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $url); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // $output contains the output string $output = curl_exec($ch); // var_dump(curl_error($ch)); //decode json into array $json = json_decode($output); // close curl resource to free up system resources curl_close($ch); return $json; } /** * returns array of values found * @param array(Objects) $obj * @param String $field * @param String $value * @return array() */ function searchJson($obj, $field, $value) { $result = array(); if (is_array($obj) || is_object($obj)) { foreach ($obj as $item) { foreach ($item as $child) { if (isset($item->id) && stripos($child . $field, $value) !== false) { array_push($result, $item->id); } } } } return $result; } //-------------------------- // Debugging Functions //-------------------------- //formatted output of variable function test($var) { echo "<pre>"; var_dump($var); echo "</pre>"; } ?>
Output for 5.5.0 - 7.3.0rc3
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> <style> .center-wrapper .panel-pane{ margin-bottom:0; } </style> <h2>GW Lecture Capture Locations</h2> <p>All lecture capture classrooms record instructor audio and presentation materials. Select classrooms are also equipped with a camera to record video of the instructor, along with the audio and presentation materials.</p> <br />