<?php require_once '../network.php'; echo 'Test Course URL: </br>'; $url = courseURL('CZ2001', $info); echo '<b>Course URL for CZ2001:</b> ' . '<a href="' . $url . '">' . $url . '</a>'; echo '</br></br>'; echo 'Test Fetch Function: </br>'; echo fetch($url);
public static function getInstanceAuto(array $course) { if (!array_key_exists('code', $course) || !array_key_exists('index', $course) || !array_key_exists('year', $course) || !array_key_exists('sem', $course)) { return null; } $code = $course['code']; $code = strtoupper($code); $index = $course['index']; $examTime = array_key_exists('examTime', $course) ? $course['examTime'] : null; $info = array('year' => $course['year'], 'sem' => $course['sem']); if (!array_key_exists('year', $info) || !array_key_exists('sem', $info)) { return null; } $html = str_get_html(fetch(courseURL($code, $info))); if (!$html) { return null; } $table = $html->find('table'); if (!$table) { return null; } //get course information $tr = str_get_html($table[0])->find('tr'); $InfoBlock = str_get_html($tr[0]); $nameAndAuBlock = $InfoBlock->find('b'); $courseName = $nameAndAuBlock[1]->plaintext; $courseAU = $nameAndAuBlock[2]->plaintext; //get index block $indexBlock = null; $line = -1; $trBlocks = str_get_html($table[1])->find('tr'); for ($i = 0; $i < count($trBlocks); $i++) { $cols = str_get_html($trBlocks[$i])->find('b'); for ($j = 0; $j < count($cols); $j++) { if ($index == $cols[0]->plaintext) { $indexBlock = $trBlocks[$i]; $line = $i; break; } } if ($line != -1) { break; } } if ($line == -1) { return null; } //create a course instance $c = array('code' => $code, 'index' => $index, 'name' => $courseName, 'au' => $courseAU, 'examTime' => $examTime, 'lessons' => array()); $trs = array(); array_push($trs, $trBlocks[$line]); $line++; while (TRUE) { if ($line >= count($trBlocks)) { break; } $cols = str_get_html($trBlocks[$line])->find('b'); $first = $cols[0]->plaintext; if ($first != '') { break; } array_push($trs, $trBlocks[$line]); $line++; } foreach ($trs as $value) { $lessonInfo = str_get_html($value)->find('b'); if (!$lessonInfo) { return null; } $times = explode('-', $lessonInfo[4]->plaintext); if (!$times) { return null; } $lesson = new Lesson(array('type' => $lessonInfo[1]->plaintext, 'group' => $lessonInfo[2]->plaintext, 'time' => new LessonTime(array('wkDay' => $lessonInfo[3]->plaintext, 'startTime' => intval($times[0]), 'endTime' => intval($times[1]))), 'venue' => $lessonInfo[5]->plaintext, 'remark' => $lessonInfo[6]->plaintext)); array_push($c['lessons'], $lesson); } return Course::getInstanceWithCourseInfo($c); }