source: src/mvc/event.cpp @ 3cd9fe6

qt5
Last change on this file since 3cd9fe6 was 3cd9fe6, checked in by Philipp Spitzer <philipp@…>, 6 years ago

favourite is now tristate instead of bool.

  • Property mode set to 100644
File size: 9.4 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2015 Philipp Spitzer, gregor herrmann, Stefan Stahl
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 "conference.h"
21#include "event.h"
22#include "room.h"
23
24QString const Event::sTableName = QString("event");
25
26QSqlRecord const Event::sColumns = Event::toRecord(QList<QSqlField>()
27    << QSqlField("id", QVariant::Int)
28    << QSqlField("xid_conference", QVariant::Int)
29    << QSqlField("start", QVariant::DateTime)
30    << QSqlField("duration", QVariant::Int)
31    << QSqlField("xid_track", QVariant::Int)
32    << QSqlField("type", QVariant::String)
33    << QSqlField("language", QVariant::String)
34    << QSqlField("favourite", QVariant::Int)
35    << QSqlField("alarm", QVariant::Bool)
36    << QSqlField("tag", QVariant::String)
37    << QSqlField("title", QVariant::String)
38    << QSqlField("subtitle", QVariant::String)
39    << QSqlField("abstract", QVariant::String)
40    << QSqlField("description", QVariant::String));
41
42Event::Event() :
43    room_(NULL)
44{
45}
46
47Event Event::getById(int id, int conferenceId) {
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
55
56QList<Event> Event::getByDate(const QDate& date, int conferenceId, QString orderBy) {
57    Q_ASSERT(conferenceId > 0);
58    Conference conference = Conference::getById(conferenceId);
59    QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
60    QSqlQuery query;
61    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy));
62    query.bindValue(":conf", conferenceId);
63    query.bindValue(":start", dayStart.toTime_t());
64    query.bindValue(":end", dayStart.addDays(1).toTime_t());
65    return load(query);
66}
67
68
69QList<Event> Event::getByDateAndRoom(const QDate& date, int conferenceId) {
70    Q_ASSERT(conferenceId > 0);
71    Conference conference = Conference::getById(conferenceId);
72    QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
73    QSqlQuery query;
74    QString aliasEvent("E");
75    QString aliasEventRoom("R");
76    query.prepare(QString("SELECT %1 FROM %2 %3, %4 %5 WHERE %3.xid_conference = :conf_e AND %5.xid_conference = :conf_r AND %3.start >= :start AND %3.start < :end AND %3.id = %5.xid_event ORDER BY %5.xid_room, %3.start, %3.duration").arg(
77                    columnsForSelect(aliasEvent), Event::sTableName, aliasEvent, "EVENT_ROOM", aliasEventRoom));
78    query.bindValue(":conf_e", conferenceId);
79    query.bindValue(":conf_r", conferenceId);
80    query.bindValue(":start", dayStart.toTime_t());
81    query.bindValue(":end", dayStart.addDays(1).toTime_t());
82
83    return load(query);
84}
85
86
87QList<Event> Event::conflictEvents(int aEventId, int conferenceId) {
88    QSqlQuery query;
89    Event event = Event::getById(aEventId,conferenceId);
90    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \
91           ( start >= :s1 AND ( start + duration ) < :e1 ) \
92        OR ( ( start + duration ) > :s2 AND start < :e2 ) ) \
93        AND favourite = 1 AND NOT id = :id ORDER BY start, duration");
94    query.bindValue(":conf", event.conferenceId());
95    query.bindValue(":s1", convertToDb(event.start(), QVariant::DateTime));
96    query.bindValue(":e1", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
97    query.bindValue(":s2", convertToDb(event.start(), QVariant::DateTime));
98    query.bindValue(":e2", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
99    query.bindValue(":id", event.id());
100
101    return load(query);
102}
103
104
105QList<Event> Event::getImminentAlarmEvents(int maxSecToAlarm, int conferenceId) {
106    QSqlQuery query;
107    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND (start < :start AND alarm = 1) ORDER BY start, duration");
108    query.bindValue(":conf", conferenceId);
109    query.bindValue(":start", convertToDb(QDateTime::currentDateTime().addSecs(maxSecToAlarm), QVariant::DateTime));
110    return load(query);
111}
112
113
114QList<Event> Event::getFavByDate(const QDate& date, int conferenceId) {
115    Q_ASSERT(conferenceId > 0);
116    Conference conference = Conference::getById(conferenceId);
117    QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
118    QSqlQuery query;
119    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start, duration"));
120    query.bindValue(":conf", conferenceId);
121    query.bindValue(":start", dayStart.toTime_t());
122    query.bindValue(":end", dayStart.addDays(1).toTime_t());
123
124    return load(query);
125}
126
127Room* Event::room()
128{
129    if (room_ == NULL)
130    {
131        QSqlQuery query;
132        query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf");
133        query.bindValue(":id", id());
134        query.bindValue(":conf", conferenceId());
135        if (!query.isActive())
136            if (!query.exec())
137                throw OrmSqlException(query.lastError().text());
138        if (!query.next())
139        {
140            qDebug() << "No room found for event id: " << id();
141            throw OrmNoObjectException();
142        }
143        int id = query.record().value("xid_room").toInt();
144        room_ = new Room(Room::retrieve(id));
145    }
146    return room_;
147}
148
149QString Event::roomName()
150{
151    return room()->name();
152}
153
154int Event::roomId()
155{
156    return room()->id();
157}
158
159QStringList Event::persons()
160{
161    if( mPersonsList.isEmpty() )
162    {
163        QSqlQuery query;
164        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");
165        query.bindValue(":id", id());
166        query.bindValue(":conf1", conferenceId());
167        query.bindValue(":conf2", conferenceId());
168        if (!query.exec())  qDebug() << query.lastError();
169
170        while(query.next())
171            mPersonsList.append(query.record().value("name").toString());
172    }
173
174    return mPersonsList;
175}
176
177QMap<QString,QString> Event::links()
178{
179    if ( mLinksList.isEmpty() )
180    {
181        QSqlQuery query;
182        query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf");
183        query.bindValue(":id", id());
184        query.bindValue(":conf", conferenceId());
185        query.exec();
186        // TODO: handle query error
187        //qDebug() << query.lastError();
188
189        while(query.next())
190            mLinksList.insert(query.record().value("name").toString(), query.record().value("url").toString());
191    }
192    return mLinksList;
193}
194
195bool Event::hasTimeConflict() const
196{
197    if(!isFavourite()) // if it's not favourite, it can't have time-conflict
198        return false;
199
200    return conflictEvents(id(),conferenceId()).count() > 0 ? true : false;
201}
202
203void Event::setRoom(const QString &room)
204{
205    Q_UNUSED(room);
206
207    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
208    // TODO: implement
209}
210
211void Event::setPersons(const QStringList &persons)
212{
213    Q_UNUSED(persons);
214
215    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
216    // TODO: implement
217}
218
219void Event::setLinks(const QMap<QString,QString> &aLinks)
220{
221    Q_UNUSED(aLinks);
222
223    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
224    // TODO: implement
225}
226
227QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) {
228    QList<Event> list;
229
230    // Check whether the temporary table SEARCH_EVENT exists (http://www.sqlite.org/faq.html#q7)
231    QSqlQuery query("SELECT count(*) FROM sqlite_temp_master WHERE type='table' and name='SEARCH_EVENT'");
232    if (!query.exec()) {
233        qDebug() << "SQL Error: " << query.lastError().text();
234        return list;
235    }
236    query.first();
237    QVariant v = query.value(0);
238    if (v.toInt() != 1) return list;
239
240    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
241    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
242    query = QSqlQuery();
243    try {
244        if( !query.prepare( strQuery ) ){
245            qDebug() << "QSqlQuery.prepare error";
246            throw OrmSqlException( query.lastError().text() );
247        }
248
249        Q_ASSERT(conferenceId > 0);
250        Conference conference = Conference::getById(conferenceId);
251        QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
252
253        query.bindValue(":conf", conferenceId);
254        query.bindValue(":start", dayStart.toTime_t());
255        query.bindValue(":end", dayStart.addDays(1).toTime_t());
256
257        list = load(query);
258    }
259    catch(OrmException &e)
260    {
261        qDebug() << "getSearchResultByDate error: " << e.text();
262    }
263    catch(...){
264        qDebug() << "getSearchResultByDate failed ...";
265    }
266    return list;
267}
268
Note: See TracBrowser for help on using the repository browser.