websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 242 (differences)
Author: xbright
Log message: * Updated projects forum
* Fixed a few bugs
Change revision:
<?php
#    This file is a part of CodingTeam. See <http://www.codingteam.net>.
#    Copyright (C) 2007-2009 CodingTeam (See AUTHORS and THANKS for details)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, version 3 only.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
class Timeline {
    private $ct_db;
 
    public function __construct($db)
    {
        $this->ct_db = $db;
    }
 
    public function getTimeline($projectid, $feeds, $date, $days)
    {
        $project = getClass('projects.projects', $this->ct_db);
        $projects = $project->load($projectid);
 
        if ($projects)
            $dbname = htmlspecialchars($project->getDbname());
        else
            exit('Error.');
 
        // Guess datetime format
        $space = explode(' ', $date);
        if (count($space) == 1)
            $date .= ' 23:59:59';
 
        $infos = array($date, $days);
        $timeline = array();
        foreach ($feeds as $feed)
        {
            $contents = self::get_content($feed, $projectid, $dbname, $infos);
 
            foreach ($contents as $item)
            {
                $global_date = explode(' ', $item['time']);
                // Array for date
                if (!isset($timeline[$global_date[0]]))
                    $timeline[$global_date[0]] = array($global_date[0]);
                // Array for datas
                if (!isset($timeline[$global_date[0]][1]))
                    $timeline[$global_date[0]][1] = array();
 
                // Add to the timeline
                $timestamp = convertDate($item['time'], 'datetime', 'timestamp');
 
                if (!isset($timeline[$global_date[0]][1][$timestamp]))
                    $timeline[$global_date[0]][1][$timestamp] = array();
 
                array_push($timeline[$global_date[0]][1][$timestamp],
                           array('title'  => $item['title'],
                                 'time'   => $global_date[1],
                                 'link'   => $item['link'],
                                 'author' => $item['author'],
                                 'text'   => map_str($item['text'], 100),
                                 'type'   => $item['type']));
            }
        }
 
        return $timeline;
    }
 
    private function get_content($type, $projectid, $dbname, $infos)
    {
        $return = array();
 
        list($date, $days) = $infos;
 
        switch ($type)
        {
            case 'download':
                $down = getClass('project.downloads', $this->ct_db);
                $latest = $down->getDownloadsByInterval($projectid, $date, $days);
 
                foreach ($latest as $item)
                {
                    $link = CT_BASEURL.'project/'.$dbname.'/download/'.$item['version'];
                    array_push($return, array('title'  => $item['name'],
                                              'time'   => $item['date'],
                                              'link'   => $link,
                                              'author' => $dbname,
                                              'text'   => $item['text'],
                                              'type'   => $type));
                }
                break;
 
            case 'bugs':
                $bugs = getClass('project.bugs', $this->ct_db);
                $latest = $bugs->getBugsByInterval($projectid, $date, $days);
 
                foreach ($latest as $item)
                {
                    $user = getUser($item['authorid'], $this->ct_db);
                    $user_name = $user->getNickname();
 
                    $link = CT_BASEURL.'project/'.$dbname.'/bugs/show/'.$item['id'];
                    array_push($return, array('title'  => $item['name'],
                                              'time'   => $item['datetime'],
                                              'link'   => $link,
                                              'author' => $user_name,
                                              'text'   => $item['text'],
                                              'type'   => $type));
                }
                break;
 
            case 'bugs-updates':
                $bugs = getClass('project.bugs', $this->ct_db);
                $latest = $bugs->getActivityByInterval($projectid, $date, $days);
 
                $type = array('bugreport'      => i18n('Bug report'),
                              'featurerequest' => i18n('Feature request'));
 
                $status = array('unconfirmed'      => i18n('Unconfirmed'),
                                'confirmed'        => i18n('Confirmed'),
                                'working'          => i18n('Working'),
                                'needinformations' => i18n('Need informations'),
                                'needcontributors' => i18n('Need contributors'),
                                'needtests'        => i18n('Need tests'),
                                'resolved'         => i18n('Resolved'),
                                'rejected'         => i18n('Rejected'));
 
                $priority = array('blocker'  => i18n('Blocker'),
                                  'critical' => i18n('Critical'),
                                  'major'    => i18n('Major'),
                                  'normal'   => i18n('Normal'),
                                  'minor'    => i18n('Minor'),
                                  'trivial'  => i18n('Trivial'));
 
                foreach ($latest as $item)
                {
                    $user = getUser($item['authorid'], $this->ct_db);
                    $user_name = $user->getNickname();
 
 
                    $editlist = '';
                    if ($item['type'] != '!nochange!')
                        $editlist .= ((!empty($editlist)) ? ', ' : '').i18n('type changed to %(type)s',
                                                                            array('type' => $type[$item['type']]));
 
                    if ($item['priority'] != '!nochange!')
                        $editlist .= ((!empty($editlist)) ? ', ' : '').i18n('priority changed to %(priority)s',
                                                                            array('priority' => $priority[$item['priority']]));
 
                    if ($item['status'] != '!nochange!')
                        $editlist .= ((!empty($editlist)) ? ', ' : '').i18n('status changed to %(status)s',
                                                                            array('status' => $status[$item['status']]));
 
                    if ($item['version'] != '!nochange!')
                        $editlist .= ((!empty($editlist)) ? ', ' : '').i18n('version changed to %(version)s',
                                                                            array('version' => $item['version']));
 
                    if ($item['assignedid'] != 0)
                    {
                        $user = getUser($item['assignedid'], $this->ct_db);
                        $nick = htmlspecialchars($user->getNickname());
 
                        $editlist .= ((!empty($editlist)) ? ', ' : '').i18n('assign to %(nick)s', array('nick' => $nick));
                    }
 
                    if ($item['attachement'] != 0)
                    {
                        $attach = $bugs->getAttachementById($item['attachement']);
 
                        if (!empty($attach[0]['file']))
                            $type = i18n('file');
                        else
                            $type = i18n('paste');
 
                        $editlist .= ((!empty($editlist)) ? ', ' : '').i18n('attached a %(type)s', array('type' => $type));
                    }
 
                    if ($item['answer'] != 0)
                        $editlist .= ((!empty($editlist)) ? ', ' : '').i18n('added an answer');
 
                    $title = i18n('Changes on #%(bugid)d', array('bugid' => $item['bugid']));
 
                    $link = CT_BASEURL.'project/'.$dbname.'/bugs/show/'.$item['bugid'];
                    array_push($return, array('title'  => $title,
                                              'time'   => $item['datetime'],
                                              'link'   => $link,
                                              'author' => $user_name,
                                              'text'   => $editlist,
                                              'type'   => 'bugs'));
                }
                break;
 
            case 'news':
                $news = getClass('project.news', $this->ct_db);
                $latest = $news->getNewsByInterval($projectid, $date, $days);
 
                foreach ($latest as $item)
                {
                    $user = getUser($item['authorid'], $this->ct_db);
                    $user_name = $user->getNickname();
 
                    $link = CT_BASEURL.'project/'.$dbname.'/news/show/'.$item['id'].'-'.$item['link'];
                    array_push($return, array('title'  => $item['title'],
                                              'time'   => $item['datetime'],
                                              'link'   => $link,
                                              'author' => $user_name,
                                              'text'   => $item['text'],
                                              'type'   => $type));
                }
                break;
 
            case 'doc':
                $doc = getClass('project.doc', $this->ct_db);
                $latest = $doc->getByInterval($projectid, $date, $days);
 
                foreach ($latest as $item)
                {
                    if ($item['registered_id'] != 0)
                    {
                        $user = getUser($item['registered_id'], $this->ct_db);
                        $user_name = $user->getNickname();
                    }
                    else
                        $user_name = $item['unregistered_nick'];
 
                    $link = CT_BASEURL.'project/'.$dbname.'/doc/'.$item['link'].':'.convertDate($item['datetime'], 'datetime', 'timestamp');
                    array_push($return, array('title'  => $item['link'],
                                              'time'   => $item['datetime'],
                                              'link'   => $link,
                                              'author' => $user_name,
                                              'text'   => $item['text'],
                                              'type'   => $type));
                }
                break;
 
            case 'code':
                $commits = getClass('projects.commits', $this->ct_db);
                $latest = $commits->getByInterval($projectid, $date, $days);
 
                foreach ($latest as $item)
                {
                    $link = CT_BASEURL.'project/'.$dbname.'/browse/diff/'.$item['prev_rev'].'/'.$item['new_rev'];
                    array_push($return, array('title'  => 'r'.$item['new_rev'],
                                              'time'   => $item['datetime'],
                                              'link'   => $link,
                                              'author' => $item['author'],
                                              'text'   => $item['log'],
                                              'type'   => 'browse'));
                }
                break;
 
            case 'forum':
                $forum = getClass('project.forum', $this->ct_db);
                $latest = $forum->getTopicsByInterval($projectid, $date, $days);
 
                foreach ($latest as $item)
                {
                    if ($item['registered_id'] != 0)
                    {
                        $user = getUser($item['registered_id'], $this->ct_db);
                        $user_name = $user->getNickname();
                    }
                    else
                        $user_name = $item['unregistered_nick'];
 
                    $link = CT_BASEURL.'project/'.$dbname.'/forum/show/'.$item['id'];
                    array_push($return, array('title'  => $item['title'],
                                              'time'   => $item['datetime'],
                                              'link'   => $link,
                                              'author' => $user_name,
                                              'text'   => $item['text'],
                                              'type'   => $type));
                }
                break;
 
            case 'forum-answers':
                $forum = getClass('project.forum', $this->ct_db);
                $latest = $forum->getAnswersByInterval($projectid, $date, $days);
 
                foreach ($latest as $item)
                {
                    if ($item['registered_id'] != 0)
                    {
                        $user = getUser($item['registered_id'], $this->ct_db);
                        $user_name = $user->getNickname();
                    }
                    else
                        $user_name = $item['unregistered_nick'];
 
                    $forum->load($item['postid']);
                    $total = $forum->getAnswersList();
                    $nbtotal = $total[0] + 1;
                    $max = 10;
 
                    $i = 0;
 
                    if($nbtotal > $max)
                        while($i < ($nbtotal / $max))
                            $i++;
                    else
                        $i == 0;
 
                    if ($i != 0)
                        $page = '/'.$i;
                    else
                        $page = '';
 
                    $link = CT_BASEURL.'project/'.$dbname.'/forum/show/'.$item['postid'].$page.'#answer'.$item['id'];
                    array_push($return, array('title'  => 'Re: '.$item['title'],
                                              'time'   => $item['datetime'],
                                              'link'   => $link,
                                              'author' => $user_name,
                                              'text'   => $item['text'],
                                              'type'   => 'forum'));
 
                    $forum->unload();
                }
                break;
 
            default:
                exit('Error.');
        }
 
        return $return;
    }
}
?>