<? require_once("../lib/course/Course.php"); ?>
<?
    // get current time
    $now = time();

    // get current announcements
    $announcements = array();
    $xml = new SimpleXMLElement(file_get_contents("http://cs75.net/phorum/feed.php?1,type=rss"));
    foreach ($xml->channel->item as $item)
    {
        if ($now - 5 * 24 * 60 * 60 < strtotime($item->pubDate))
            $announcements[] = $item;
    }

    // get recent posts (plus announcements)
    $posts = new SimpleXMLElement(file_get_contents("http://cs75.net/phorum/feed.php?0,type=rss"));

    // begin XHTML
    $xhtml = '<div>';

    // output current announcements
    if (count($announcements) > 0)
    {
        // header for announcements
        $xhtml .= '<b><a href="/phorum/list.php?1">Announcements</a></b>';
        $xhtml .= '<br /><br />';

        // start list
        $xhtml .= '<ul>';

        // output current announcements
        foreach ($announcements as $a)
        {
            // strip # of replies
            $a->title = preg_replace("/\([^(]*\)$/", "", $a->title);

            // output topic
            $xhtml .= "<li><a href='{$a->link}' style='font-weight: normal;'>{$a->title}</a></li>";
        }

        // end list
        $xhtml .= '</ul>';

        // horizontal rule 
        $xhtml .= '<hr style="height: 1px; margin-bottom: 12px; margin-top: 12px;" />';
    }

    // header for recent messages
    $xhtml .= '<b><a href="/phorum/addon.php?0,module=recent_messages">Recent Messages</a> in the <a href="/phorum/">Forum</a></b>';
    $xhtml .= '<br /><br />';

    // start list
    $xhtml .= '<ul>';

    // output <= 6 recent topics
    for ($i = 0, $n = count($posts->channel->item), $max = 2; $i < $n && $max >= 0; $i++)
    {
        // get current item
        $item = $posts->channel->item[$i];

        // skip announcements
        if (preg_match("/^(:?Announcements|Test)$/", $item->category)) continue;

        // print topic
        $xhtml .= "<li><a href='{$item->link}' style='font-weight: normal;'>{$item->title}</a></li>";

        // decrement counter
        $max--;
    }

    // end list
    $xhtml .= '</ul>';

    // end XHTML
    $xhtml .= '</div>';

    // cache XHTML
    course()->cache("posts", $xhtml);

    // output XML
    header("Content-type: text/xml");
    print('<?xml version="1.0" encoding="iso-8859-1"?>');
    print('<update><![CDATA[');
    print $xhtml;
    print(']]></update>');
?>
