source: src/mvc/event.cpp @ 5c7119d

qt5
Last change on this file since 5c7119d was 5c7119d, checked in by Philipp Spitzer <philipp@…>, 10 years ago

Fixed bug reported by gregor: Too many authors are shown (form other conferences as well).

  • Property mode set to 100644
File size: 8.5 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
4 *
5 * This file is part of ConfClerk.
6 *
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
19 */
20#include "event.h"
21#include "room.h"
22
23QString const Event::sTableName = QString("event");
24
25QSqlRecord const Event::sColumns = Event::toRecord(QList<QSqlField>()
26    << QSqlField("id", QVariant::Int)
27    << QSqlField("xid_conference", QVariant::Int)
28    << QSqlField("start", QVariant::DateTime)
29    << QSqlField("duration", QVariant::Int)
30    << QSqlField("xid_track", QVariant::Int)
31    << QSqlField("type", QVariant::String)
32    << QSqlField("language", QVariant::String)
33    << QSqlField("favourite", QVariant::Bool)
34    << QSqlField("alarm", QVariant::Bool)
35    << QSqlField("tag", QVariant::String)
36    << QSqlField("title", QVariant::String)
37    << QSqlField("subtitle", QVariant::String)
38    << QSqlField("abstract", QVariant::String)
39    << QSqlField("description", QVariant::String));
40
41Event::Event() :
42    room_(NULL)
43{
44}
45
46Event Event::getById(int id, int conferenceId)
47{
48    QSqlQuery query;
49    query.prepare(selectQuery() + "WHERE id = :id AND xid_conference = :conf");
50    query.bindValue(":id", id);
51    query.bindValue(":conf", conferenceId);
52    return loadOne(query);
53}
54
55QList<Event> Event::getByDate(const QDate& date, int conferenceId, QString orderBy)
56{
57    QSqlQuery query;
58    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy));
59    query.bindValue(":conf", conferenceId);
60    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
61    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
62
63    return load(query);
64}
65
66QList<Event> Event::getByDateAndRoom(const QDate& date, int conferenceId)
67{
68    QSqlQuery query;
69    QString aliasEvent("E");
70    QString aliasEventRoom("R");
71    query.prepare(QString("SELECT %1 FROM %2 %3, %4 %5 WHERE %3.xid_conference = :conf AND %3.start >= :start AND %3.start < :end AND %3.id = R.xid_event ORDER BY %5.xid_room, %3.start").arg(
72                    columnsForSelect(aliasEvent), Event::sTableName, aliasEvent, "EVENT_ROOM", aliasEventRoom));
73    query.bindValue(":conf", conferenceId);
74    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
75    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
76
77    return load(query);
78}
79
80QList<Event> Event::nowEvents(int conferenceId, QString orderBy)
81{
82    uint curTime_t =  QDateTime(QDate::currentDate(),QTime::currentTime(),Qt::UTC).toTime_t();
83    //uint curTime_t = 1265457610; // for testing
84
85    QSqlQuery query;
86    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start <= :now1 AND ( start + duration ) > :now2 ORDER BY %1").arg(orderBy));
87    query.bindValue(":conf", conferenceId);
88    query.bindValue(":now1", convertToDb(curTime_t, QVariant::DateTime));
89    query.bindValue(":now2", convertToDb(curTime_t, QVariant::DateTime));
90
91    return load(query);
92}
93
94QList<Event> Event::conflictEvents(int aEventId, int conferenceId)
95{
96    QSqlQuery query;
97    Event event = Event::getById(aEventId,conferenceId);
98    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \
99           ( start >= :s1 AND ( start + duration ) < :e1 ) \
100        OR ( ( start + duration ) > :s2 AND start < :e2 ) ) \
101        AND favourite = 1 AND NOT id = :id ORDER BY start");
102    query.bindValue(":conf", event.conferenceId());
103    query.bindValue(":s1", convertToDb(event.start(), QVariant::DateTime));
104    query.bindValue(":e1", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
105    query.bindValue(":s2", convertToDb(event.start(), QVariant::DateTime));
106    query.bindValue(":e2", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
107    query.bindValue(":id", event.id());
108
109    return load(query);
110}
111
112QList<Event> Event::getFavByDate(const QDate& date, int conferenceId)
113{
114    QSqlQuery query;
115    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start"));
116    query.bindValue(":conf", conferenceId);
117    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
118    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
119
120    return load(query);
121}
122
123Room* Event::room()
124{
125    if (room_ == NULL)
126    {
127        QSqlQuery query;
128        query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf");
129        query.bindValue(":id", id());
130        query.bindValue(":conf", conferenceId());
131        if (!query.isActive())
132            if (!query.exec())
133                throw OrmSqlException(query.lastError().text());
134        if (!query.next())
135        {
136            qDebug() << "No room found for event id: " << id();
137            throw OrmNoObjectException();
138        }
139        int id = query.record().value("xid_room").toInt();
140        room_ = new Room(Room::retrieve(id));
141    }
142    return room_;
143}
144
145QString Event::roomName()
146{
147    return room()->name();
148}
149
150int Event::roomId()
151{
152    return room()->id();
153}
154
155QStringList Event::persons()
156{
157    if( mPersonsList.isEmpty() )
158    {
159        QSqlQuery query;
160        query.prepare("SELECT person.name FROM person INNER JOIN event_person ON person.id = event_person.xid_person AND event_person.xid_event = :id AND event_person.xid_conference = :conf1 AND person.xid_conference = :conf2");
161        query.bindValue(":id", id());
162        query.bindValue(":conf1", conferenceId());
163        query.bindValue(":conf2", conferenceId());
164        if (!query.exec())  qDebug() << query.lastError();
165
166        while(query.next())
167            mPersonsList.append(query.record().value("name").toString());
168    }
169
170    return mPersonsList;
171}
172
173QMap<QString,QString> Event::links()
174{
175    if ( mLinksList.isEmpty() )
176    {
177        QSqlQuery query;
178        query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf");
179        query.bindValue(":id", id());
180        query.bindValue(":conf", conferenceId());
181        query.exec();
182        // TODO: handle qeury error
183        //qDebug() << query.lastError();
184
185        while(query.next())
186            mLinksList.insert(query.record().value("name").toString(), query.record().value("url").toString());
187    }
188    return mLinksList;
189}
190
191bool Event::hasTimeConflict() const
192{
193    if(!isFavourite()) // if it's not favourite, it can't have time-conflict
194        return false;
195
196    return conflictEvents(id(),conferenceId()).count() > 0 ? true : false;
197}
198
199void Event::setRoom(const QString &room)
200{
201    Q_UNUSED(room);
202
203    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
204    // TODO: implement
205}
206
207void Event::setPersons(const QStringList &persons)
208{
209    Q_UNUSED(persons);
210
211    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
212    // TODO: implement
213}
214
215void Event::setLinks(const QMap<QString,QString> &aLinks)
216{
217    Q_UNUSED(aLinks);
218
219    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
220    // TODO: implement
221}
222
223QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy)
224{
225    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
226    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
227
228    QList<Event> list;
229    QSqlQuery query;
230    try{
231        if( !query.prepare( strQuery ) ){
232            qDebug() << "QSqlQuery.prepare error";
233            throw OrmSqlException( query.lastError().text() );
234        }
235
236        query.bindValue(":conf", conferenceId);
237        query.bindValue(":start", convertToDb(date, QVariant::DateTime));
238        query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
239
240        list = load(query);
241    }
242    catch(OrmException &e)
243    {
244        qDebug() << "getSearchResultByDate error: " << e.text();
245    }
246    catch(...){
247        qDebug() << "getSearchResultByDate failed ...";
248    }
249    return list;
250}
251
Note: See TracBrowser for help on using the repository browser.