Skip to content

Instantly share code, notes, and snippets.

@ksascomm
Last active December 12, 2017 19:08
Show Gist options
  • Save ksascomm/9f36e66e8a413f19ed8c2509e57fbe83 to your computer and use it in GitHub Desktop.
Save ksascomm/9f36e66e8a413f19ed8c2509e57fbe83 to your computer and use it in GitHub Desktop.
Courses
<?php
/*
Template Name: SIS Courses
*/
get_header(); ?>
<?php // Load Zebra Curl
require_once TEMPLATEPATH . "/library/Zebra_cURL.php";
//Set query sting variables
$theme_option = flagship_sub_get_global_options();
$department_unclean = $theme_option['flagship_sub_isis_name'];
$department = str_replace(' ', '%20', $department_unclean);
$department = str_replace('&', '%26', $department);
$fall = 'fall%202017';
$spring = 'spring%202018';
$open = 'open';
$approval = 'approval%20required';
$closed = 'closed';
$waitlist = 'waitlist%20only';
$key = 'Qrf9MQse2cdpgaYdPF23dkMaqrVKE5dP';
//Create first Zebra Curl class
$course_curl = new Zebra_cURL();
$course_curl->option(array(
CURLOPT_TIMEOUT => 60,
CURLOPT_CONNECTTIMEOUT => 60,
));
//Create API Url calls
$courses_fall_url = 'https://sis.jhu.edu/api/classes?key=' . $key . '&School=Krieger%20School%20of%20Arts%20and%20Sciences&Term=' . $fall . '&Department=AS%20' . $department;
$courses_spring_url = 'https://sis.jhu.edu/api/classes?key=' . $key . '&School=Krieger%20School%20of%20Arts%20and%20Sciences&Term=' . $spring . '&Department=AS%20' . $department;
$courses_call = array(
$courses_fall_url,
$courses_spring_url,
//$courses_intersession_url,
//$courses_summer_url
);
$courses_call_fall = $courses_fall_url;
$courses_call_spring = $courses_spring_url;
//Course display callback function
function display_courses($result) {
$result->body = json_decode(html_entity_decode($result->body));
$title = $result->body[0]->{'Title'};
$term = $result->body[0]->{'Term_IDR'};
$clean_term = str_replace(' ', '-', $term);
$meetings = $result->body[0]->{'Meetings'};
$status = $result->body[0]->{'Status'};
$course_number = $result->body[0]->{'OfferingName'};
$clean_course_number = preg_replace('/[^A-Za-z0-9\-]/', '', $course_number);
$credits = $result->body[0]->{'Credits'};
$section_number = $result->body[0]->{'SectionName'};
$instructor = $result->body[0]->{'InstructorsFullName'};
$course_level = $result->body[0]->{'Level'};
$description = $result->body[0]->{'SectionDetails'}[0]->{'Description'};
$room = $result->body[0]->{'SectionDetails'}[0]->{'Meetings'}[0]->{'Building'};
$roomnumber = $result->body[0]->{'SectionDetails'}[0]->{'Meetings'}[0]->{'Room'};
$postag = "N/A";
$sectiondetails = $result->body[0]->{'SectionDetails'}[0];
if (isset($sectiondetails->{'PosTags'}))
{
if(!empty($sectiondetails->{'PosTags'})) {
$postag=$sectiondetails->{'PosTags'};
$tags = [];
foreach ($postag as $tag) {
$tags[] = $tag->{'Tag'};
}
}
}
echo '<tr><td>' . $course_number . '</td><td>' . $title . '</td><td>' . $meetings . '</td><td>' . $section_number . '</td><td>' . $instructor . '</td><td>' . $room . '&nbsp;' . $roomnumber . '</td><td>' . $tags . '</td>';
echo '<td><button class="button" data-open="course-' . $clean_course_number . $section_number . $clean_term .'">More Info</button></td></tr>';
echo '<div class="reveal" id="course-'.$clean_course_number . $section_number . $clean_term .'" data-reveal><h1>'. $title . '</h1><p>'. $description .'<ul><li>Credits: ' . $credits . '</li><li>Level: ' . $course_level . ' </li><li>Satuts: '. $status . '</li><li>PosTag(s): '. $tags . '</li></ul></p><button class="close-button" data-close aria-label="Close reveal" type="button"><span aria-hidden="true">&times;</span></button></div>';
}
//ISIS Call callback function
function parse_courses($result) {
$cache_dir = TEMPLATEPATH . "/assets/functions/cache/";
$key = 'DZkN4QOJGaDKVg6Du1911u45d4TJNp6I';
$result->body = json_decode(html_entity_decode($result->body));
if ((!is_array ($result) && !is_object($result)) ||
(is_array($result) || count($result) == 0) ||
(json_last_error() != JSON_ERROR_NONE)) {// only for PHP >= 5.3.0
// log the error or warning here ...
$input = $result->body;
$output = print_r ($result, TRUE);
// Only for PHP >= 5.3.0
// json_last_error();
// json_last_error_msg();
return -1;
}
$course_data = array();
foreach($result->body as $course) {
$section = $course->{'SectionName'};
$level = $course->{'Level'};
$parent = 'Undergraduate';
if (strpos($level, $parent) !== false || ($level === "") !== false )
{
$number = $course->{'OfferingName'};
$clean_number = preg_replace('/[^A-Za-z0-9\-]/', '', $number);
$dirty_term = $course->{'Term_IDR'};
$clean_term = str_replace(' ', '%20', $dirty_term);
$details_url = 'https://sis.jhu.edu/api/classes/' . $clean_number . $section .'/' . $clean_term . '?key=' . $key;
$course_data[] = $details_url;
}
}
$curl = new Zebra_cURL();
$curl->option(array(
CURLOPT_TIMEOUT => 60,
CURLOPT_CONNECTTIMEOUT => 60,
));
$curl->get($course_data, 'display_courses');
}
?>
<div class="main-container">
<div class="main-grid">
<main class="main-content">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'page' ); ?>
<ul class="tabs" data-tabs id="example-tabs">
<li class="tabs-title is-active"><a href="#Fall" aria-selected="true">Fall 2017</a></li>
<li class="tabs-title"><a href="#Spring">Spring 2018</a></li>
</ul>
<div class="tabs-content" data-tabs-content="example-tabs">
<div class="tabs-panel is-active" id="Fall">
<table class="course-listings">
<thead>
<tr>
<th>Course #</th>
<th>Title</th>
<th>Day/Times</th>
<th>Section</th>
<th>Instructor</th>
<th>Room</th>
<th>PosTag(s)</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<?php $course_curl->get($courses_call_fall, 'parse_courses'); ?>
</tbody>
</table>
</div>
<div class="tabs-panel" id="Spring">
<table class="course-listings">
<thead>
<tr>
<th>Course #</th>
<th>Title</th>
<th>Day/Times</th>
<th>Section</th>
<th>Instructor</th>
<th>Room</th>
</tr>
</thead>
<tbody>
<?php $course_curl->get($courses_call_spring, 'parse_courses'); ?>
</tbody>
</table>
</div>
</div>
<?php endwhile;?>
</main>
<?php get_sidebar(); ?>
</div>
</div>
<?php get_template_part( 'template-parts/script-initiators' );
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment