source: src/mvc/event.cpp @ 1343ea4

qt5
Last change on this file since 1343ea4 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
RevLine 
[ca90cb1]1/*
2 * Copyright (C) 2010 Ixonos Plc.
[4a87a3b]3 * Copyright (C) 2011-2015 Philipp Spitzer, gregor herrmann, Stefan Stahl
[ca90cb1]4 *
[6df32f2]5 * This file is part of ConfClerk.
[ca90cb1]6 *
[6df32f2]7 * ConfClerk is free software: you can redistribute it and/or modify it
[ca90cb1]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 *
[6df32f2]12 * ConfClerk is distributed in the hope that it will be useful, but
[ca90cb1]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
[6df32f2]18 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
[ca90cb1]19 */
[41c4ceb]20#include "conference.h"
[e5bc908]21#include "event.h"
[7620de0]22#include "room.h"
[e5bc908]23
[7d7659d]24QString const Event::sTableName = QString("event");
[c790268]25
[20a6010]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)
[4693fa6]31    << QSqlField("xid_track", QVariant::Int)
[20a6010]32    << QSqlField("type", QVariant::String)
[680a4da]33    << QSqlField("language", QVariant::String)
[3cd9fe6]34    << QSqlField("favourite", QVariant::Int)
[b6cd05c]35    << QSqlField("alarm", QVariant::Bool)
[c790268]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));
[5a73d27]41
[a1755df]42Event::Event() :
[0d995ed]43    room_(NULL)
[a1755df]44{
45}
[20a6010]46
[9196cb0]47Event Event::getById(int id, int conferenceId) {
[d0d0a66]48    QSqlQuery query;
[7d7659d]49    query.prepare(selectQuery() + "WHERE id = :id AND xid_conference = :conf");
[d0d0a66]50    query.bindValue(":id", id);
51    query.bindValue(":conf", conferenceId);
52    return loadOne(query);
53}
54
[9196cb0]55
56QList<Event> Event::getByDate(const QDate& date, int conferenceId, QString orderBy) {
[41c4ceb]57    Q_ASSERT(conferenceId > 0);
58    Conference conference = Conference::getById(conferenceId);
59    QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
[d0d0a66]60    QSqlQuery query;
[7d7659d]61    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy));
[d0d0a66]62    query.bindValue(":conf", conferenceId);
[41c4ceb]63    query.bindValue(":start", dayStart.toTime_t());
64    query.bindValue(":end", dayStart.addDays(1).toTime_t());
[d0d0a66]65    return load(query);
[20a6010]66}
[680a4da]67
[41c4ceb]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);
[7620de0]73    QSqlQuery query;
74    QString aliasEvent("E");
75    QString aliasEventRoom("R");
[42685b5]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(
[7620de0]77                    columnsForSelect(aliasEvent), Event::sTableName, aliasEvent, "EVENT_ROOM", aliasEventRoom));
[42685b5]78    query.bindValue(":conf_e", conferenceId);
79    query.bindValue(":conf_r", conferenceId);
[41c4ceb]80    query.bindValue(":start", dayStart.toTime_t());
81    query.bindValue(":end", dayStart.addDays(1).toTime_t());
[7620de0]82
83    return load(query);
84}
85
[b8a3ad1]86
[066b41f]87QList<Event> Event::conflictEvents(int aEventId, int conferenceId) {
[d49254d]88    QSqlQuery query;
[412cef6]89    Event event = Event::getById(aEventId,conferenceId);
90    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \
[eb21333]91           ( start >= :s1 AND ( start + duration ) < :e1 ) \
92        OR ( ( start + duration ) > :s2 AND start < :e2 ) ) \
[7b3cd0e]93        AND favourite >= 1 AND NOT id = :id ORDER BY start, duration");
[412cef6]94    query.bindValue(":conf", event.conferenceId());
[eb21333]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));
[412cef6]99    query.bindValue(":id", event.id());
[d49254d]100
101    return load(query);
102}
103
[066b41f]104
[3a09de6]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
[41c4ceb]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);
[6f39595]118    QSqlQuery query;
[7b3cd0e]119    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite >= 1 ORDER BY start, duration"));
[6f39595]120    query.bindValue(":conf", conferenceId);
[41c4ceb]121    query.bindValue(":start", dayStart.toTime_t());
122    query.bindValue(":end", dayStart.addDays(1).toTime_t());
[6f39595]123
124    return load(query);
125}
126
[0d995ed]127Room* Event::room()
[f9db452]128{
[0d995ed]129    if (room_ == NULL)
[a1755df]130    {
131        QSqlQuery query;
[5d9409d]132        query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf");
[a1755df]133        query.bindValue(":id", id());
[5d9409d]134        query.bindValue(":conf", conferenceId());
[a1755df]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        }
[0d995ed]143        int id = query.record().value("xid_room").toInt();
144        room_ = new Room(Room::retrieve(id));
[a1755df]145    }
[0d995ed]146    return room_;
147}
148
149QString Event::roomName()
150{
151    return room()->name();
152}
153
154int Event::roomId()
155{
156    return room()->id();
[7620de0]157}
158
[78e3575]159QStringList Event::persons()
[395d6d3]160{
[a1755df]161    if( mPersonsList.isEmpty() )
[78e3575]162    {
163        QSqlQuery query;
[5c7119d]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");
[78e3575]165        query.bindValue(":id", id());
[5c7119d]166        query.bindValue(":conf1", conferenceId());
167        query.bindValue(":conf2", conferenceId());
168        if (!query.exec())  qDebug() << query.lastError();
[78e3575]169
170        while(query.next())
[a1755df]171            mPersonsList.append(query.record().value("name").toString());
[78e3575]172    }
173
[a1755df]174    return mPersonsList;
[395d6d3]175}
176
[a1755df]177QMap<QString,QString> Event::links()
[6123b48]178{
[a1755df]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();
[a44d375]186        // TODO: handle query error
[a1755df]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;
[6123b48]193}
194
[1343ea4]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;
[9f367eb]211
[7b3cd0e]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    }
[9f367eb]220}
221
[f9db452]222void Event::setRoom(const QString &room)
223{
[395d6d3]224    Q_UNUSED(room);
225
[f9db452]226    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
227    // TODO: implement
228}
[27102d5]229
[395d6d3]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
[6123b48]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
[0ea5709]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
[7d7659d]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);
[0ea5709]261    query = QSqlQuery();
262    try {
[0d4ecc2]263        if( !query.prepare( strQuery ) ){
264            qDebug() << "QSqlQuery.prepare error";
265            throw OrmSqlException( query.lastError().text() );
266        }
267
[41c4ceb]268        Q_ASSERT(conferenceId > 0);
269        Conference conference = Conference::getById(conferenceId);
270        QDateTime dayStart(date, conference.dayChangeTime(), Qt::UTC);
271
[0d4ecc2]272        query.bindValue(":conf", conferenceId);
[41c4ceb]273        query.bindValue(":start", dayStart.toTime_t());
274        query.bindValue(":end", dayStart.addDays(1).toTime_t());
[0d4ecc2]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;
[e662750]286}
287
Note: See TracBrowser for help on using the repository browser.