source: src/mvc/event.cpp @ 81d87d7

qt5
Last change on this file since 81d87d7 was 1343ea4, checked in by Philipp Spitzer <philipp@…>, 5 years ago

Now the conflict severity is drawn.

  • Property mode set to 100644
File size: 10.0 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
195Favourite Event::timeConflict() const {
196    if (favourite() == Favourite_no) // if it's not favourite, it can't have time-conflict
197        return Favourite_no;
198
199    QList<Event> events = conflictEvents(id(),conferenceId());
200
201    // find "strongest" conflict
202    Favourite f = Favourite_no;
203    for (int i = 0; i != events.size(); ++i) {
204        switch (events[i].favourite()) {
205        case Favourite_strong: f = Favourite_strong; break;
206        case Favourite_weak: if (f == Favourite_no) f = Favourite_weak; break;
207        case Favourite_no: break;
208        }
209    }
210    return f;
211
212}
213
214void Event::cycleFavourite() {
215    switch (favourite()) {
216        case Favourite_no: setFavourite(Favourite_strong); break;
217        case Favourite_strong: setFavourite(Favourite_weak); break;
218        case Favourite_weak: setFavourite(Favourite_no); break;
219    }
220}
221
222void Event::setRoom(const QString &room)
223{
224    Q_UNUSED(room);
225
226    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
227    // TODO: implement
228}
229
230void Event::setPersons(const QStringList &persons)
231{
232    Q_UNUSED(persons);
233
234    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
235    // TODO: implement
236}
237
238void Event::setLinks(const QMap<QString,QString> &aLinks)
239{
240    Q_UNUSED(aLinks);
241
242    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
243    // TODO: implement
244}
245
246QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) {
247    QList<Event> list;
248
249    // Check whether the temporary table SEARCH_EVENT exists (http://www.sqlite.org/faq.html#q7)
250    QSqlQuery query("SELECT count(*) FROM sqlite_temp_master WHERE type='table' and name='SEARCH_EVENT'");
251    if (!query.exec()) {
252        qDebug() << "SQL Error: " << query.lastError().text();
253        return list;
254    }
255    query.first();
256    QVariant v = query.value(0);
257    if (v.toInt() != 1) return list;
258
259    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
260    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
261    query = QSqlQuery();
262    try {
263        if( !query.prepare( strQuery ) ){
264            qDebug() << "QSqlQuery.prepare error";
265            throw OrmSqlException( query.lastError().text() );
266        }
267
268        Q_ASSERT(conferenceId > 0);
269        Conference conference = Conference::getById(conferenceId);
270        QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
271
272        query.bindValue(":conf", conferenceId);
273        query.bindValue(":start", dayStart.toTime_t());
274        query.bindValue(":end", dayStart.addDays(1).toTime_t());
275
276        list = load(query);
277    }
278    catch(OrmException &e)
279    {
280        qDebug() << "getSearchResultByDate error: " << e.text();
281    }
282    catch(...){
283        qDebug() << "getSearchResultByDate failed ...";
284    }
285    return list;
286}
287
Note: See TracBrowser for help on using the repository browser.