source: src/mvc/event.cpp

Last change on this file was ffb6be7, checked in by gregor herrmann <gregor@…>, 4 months ago

Bump copyright years.

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2021 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    Conference conference = Conference::getById(conferenceId);
107    QSqlQuery query;
108    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND (start < :start AND alarm = 1) ORDER BY start, duration");
109    query.bindValue(":conf", conferenceId);
110    QDateTime start;
111    if (conference.hasUtcOffset()) {
112        start = QDateTime::currentDateTimeUtc().addSecs(conference.utcOffset() * 60);
113    } else {
114        start = QDateTime::currentDateTime();
115        if (conference.hasDisplayTimeShift()) start = start.addSecs(conference.displayTimeShift() * 60);
116    }
117    start = start.addSecs(maxSecToAlarm);
118    query.bindValue(":start", convertToDb(start, QVariant::DateTime));
119    return load(query);
120}
121
122
123QList<Event> Event::getFavByDate(const QDate& date, int conferenceId) {
124    Q_ASSERT(conferenceId > 0);
125    Conference conference = Conference::getById(conferenceId);
126    QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
127    QSqlQuery query;
128    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite >= 1 ORDER BY start, duration"));
129    query.bindValue(":conf", conferenceId);
130    query.bindValue(":start", dayStart.toTime_t());
131    query.bindValue(":end", dayStart.addDays(1).toTime_t());
132
133    return load(query);
134}
135
136Room* Event::room()
137{
138    if (room_ == NULL)
139    {
140        QSqlQuery query;
141        query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf");
142        query.bindValue(":id", id());
143        query.bindValue(":conf", conferenceId());
144        if (!query.isActive())
145            if (!query.exec())
146                throw OrmSqlException(query.lastError().text());
147        if (!query.next())
148        {
149            qDebug() << "No room found for event id: " << id();
150            throw OrmNoObjectException();
151        }
152        int id = query.record().value("xid_room").toInt();
153        room_ = new Room(Room::retrieve(id));
154    }
155    return room_;
156}
157
158QString Event::roomName()
159{
160    return room()->name();
161}
162
163int Event::roomId()
164{
165    return room()->id();
166}
167
168QStringList Event::persons()
169{
170    if( mPersonsList.isEmpty() )
171    {
172        QSqlQuery query;
173        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");
174        query.bindValue(":id", id());
175        query.bindValue(":conf1", conferenceId());
176        query.bindValue(":conf2", conferenceId());
177        if (!query.exec())  qDebug() << query.lastError();
178
179        while(query.next())
180            mPersonsList.append(query.record().value("name").toString());
181    }
182
183    return mPersonsList;
184}
185
186QMap<QString,QString> Event::links()
187{
188    if ( mLinksList.isEmpty() )
189    {
190        QSqlQuery query;
191        query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf");
192        query.bindValue(":id", id());
193        query.bindValue(":conf", conferenceId());
194        query.exec();
195        // TODO: handle query error
196        //qDebug() << query.lastError();
197
198        while(query.next())
199            mLinksList.insert(query.record().value("name").toString(), query.record().value("url").toString());
200    }
201    return mLinksList;
202}
203
204Favourite Event::timeConflict() const {
205    if (favourite() == Favourite_no) // if it's not favourite, it can't have time-conflict
206        return Favourite_no;
207
208    QList<Event> events = conflictEvents(id(),conferenceId());
209
210    // find "strongest" conflict
211    Favourite f = Favourite_no;
212    for (int i = 0; i != events.size(); ++i) {
213        switch (events[i].favourite()) {
214        case Favourite_strong: f = Favourite_strong; break;
215        case Favourite_weak: if (f == Favourite_no) f = Favourite_weak; break;
216        case Favourite_no: break;
217        }
218    }
219    return f;
220
221}
222
223void Event::cycleFavourite(bool back) {
224    switch (favourite()) {
225        case Favourite_no: setFavourite(back ? Favourite_weak : Favourite_strong); break;
226        case Favourite_strong: setFavourite(back ? Favourite_no : Favourite_weak); break;
227        case Favourite_weak: setFavourite(back ? Favourite_strong : Favourite_no); break;
228    }
229}
230
231void Event::setRoom(const QString &room)
232{
233    Q_UNUSED(room);
234
235    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
236    // TODO: implement
237}
238
239void Event::setPersons(const QStringList &persons)
240{
241    Q_UNUSED(persons);
242
243    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
244    // TODO: implement
245}
246
247void Event::setLinks(const QMap<QString,QString> &aLinks)
248{
249    Q_UNUSED(aLinks);
250
251    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
252    // TODO: implement
253}
254
255QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) {
256    QList<Event> list;
257
258    // Check whether the temporary table SEARCH_EVENT exists (http://www.sqlite.org/faq.html#q7)
259    QSqlQuery query("SELECT count(*) FROM sqlite_temp_master WHERE type='table' and name='SEARCH_EVENT'");
260    if (!query.exec()) {
261        qDebug() << "SQL Error: " << query.lastError().text();
262        return list;
263    }
264    query.first();
265    QVariant v = query.value(0);
266    if (v.toInt() != 1) return list;
267
268    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
269    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
270    query = QSqlQuery();
271    try {
272        if( !query.prepare( strQuery ) ){
273            qDebug() << "QSqlQuery.prepare error";
274            throw OrmSqlException( query.lastError().text() );
275        }
276
277        Q_ASSERT(conferenceId > 0);
278        Conference conference = Conference::getById(conferenceId);
279        QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
280
281        query.bindValue(":conf", conferenceId);
282        query.bindValue(":start", dayStart.toTime_t());
283        query.bindValue(":end", dayStart.addDays(1).toTime_t());
284
285        list = load(query);
286    }
287    catch(OrmException &e)
288    {
289        qDebug() << "getSearchResultByDate error: " << e.text();
290    }
291    catch(...){
292        qDebug() << "getSearchResultByDate failed ...";
293    }
294    return list;
295}
296
Note: See TracBrowser for help on using the repository browser.