Пример #1
0
 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);
 }
<?php

require_once '../Course.Class.php';
echo 'Test Lesson Class: </br>';
$lesson = new Lesson(array('type' => 'LEC', 'group' => 'FS2', 'time' => new LessonTime(array('startTime' => '0830', 'endTime' => '0930', 'wkDay' => 'mon')), 'venue' => 'LT1'));
echo 'Test Case 1: a new lesson created, </br>';
echo $lesson->toString();
echo '</br></br>';
echo 'Test Course Class: </br>';
echo 'Test case 1, getInstanceWithCourseInfo: </br>';
$course = Course::getInstanceWithCourseInfo(array('code' => 'CZ2001', 'index' => '1000', 'name' => 'Shit Course', 'au' => '3', 'lessons' => array($lesson)));
echo $course->toString() . '</br>';
echo 'Test case 2, getInstanceAuto: </br>';
$course = Course::getInstanceAuto(array('code' => 'CZ2001', 'index' => '10733', 'year' => '2013', 'sem' => '1'));
if (!$course) {
    echo 'cannot create an instance.';
} else {
    echo $course->toString();
}
Пример #3
0
function createCalWithCustomInformation(array $info)
{
    //check important value
    if (!array_key_exists('year', $info) || !array_key_exists('sem', $info) || !array_key_exists('courses', $info)) {
        return null;
    }
    $mode = array_key_exists('mode', $info) ? $info['mode'] : 'auto';
    $unique_id = array_key_exists('unique_id', $info) ? $info['unique_id'] : rand();
    $TZID = array_key_exists('tz', $info) ? $info['tz'] : 'Asia/Singapore';
    $filename = array_key_exists('filename', $info) ? $info['filename'] : 'Course_Cal_file';
    $year = $info['year'];
    $sem = $info['sem'];
    $courses = $info['courses'];
    if (!is_numeric($year) || !is_numeric($sem)) {
        return null;
    }
    //create ical
    $config = array('unique_id' => $unique_id, 'TZID' => $TZID, 'filename' => $filename);
    $ical = new vcalendar($config);
    //add courses
    foreach ($courses as $course) {
        $c = null;
        if ($mode == 'manual') {
            $c = Course::getInstanceWithCourseInfo($course);
        } else {
            $course['year'] = $info['year'];
            $course['sem'] = $info['sem'];
            $c = Course::getInstanceAuto($course);
        }
        if ($c) {
            setCourseEvent($c, $ical, $info);
        }
    }
    return array('ics' => $ical);
}