aboutsummaryrefslogtreecommitdiffstats
path: root/src/activitypub/apquestion.cpp
blob: 379617437db92ab7923fd27b2657955cc9c881ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "apquestion.h"
#include "src/settings_interface.h"
#include <QDebug>

APQuestion::APQuestion(APObjectFields fields) : APPost(fields) {
    end_time = QDateTime::fromString(fields.question.end_time, Qt::ISODate);
    closed_time = QDateTime::fromString(fields.question.closed_time, Qt::ISODate);
    total_votes = fields.question.total_votes;

    for (APQuestionFields::Option elem : fields.question.poll_options) {
        options.push_back({elem.name, elem.votes});
    }

    qDebug() << options.size();
}

QString APQuestion::get_html_render(HtmlRenderDetails render_info) {
    QString html(APPost::get_html_render(render_info));
    html.append(get_html_template(QStringLiteral("appoll")));

    if (end_time.isValid()) {
        html.replace("{{end-time}}", render_info.locale->toString(
            SettingsInterface::quick_read_setting<AppSettingsTypes::Timezone>("ui/timezone") ? end_time.toUTC() : end_time.toLocalTime()
        ));
    }
    if (closed_time.isValid()) {
        html.replace("{{closed-time}}", render_info.locale->toString(
            SettingsInterface::quick_read_setting<AppSettingsTypes::Timezone>("ui/timezone") ? closed_time.toUTC() : closed_time.toLocalTime()
        ));
    }

    html.replace("{{total-votes}}", render_info.locale->toString(total_votes));
    html.replace("{{options}}", get_html_poll_options(render_info));

    return html;
}

QString APQuestion::get_html_poll_options(HtmlRenderDetails render_info) {
    QString full;

    for (PollOption option : options) {
        QString html(get_html_template(QStringLiteral("appoll_item")));
        html.replace("{{name}}", option.name);
        html.replace("{{votes}}", render_info.locale->toString(option.votes));
        html.replace("{{votes-percent}}", render_info.locale->toString((int)((float)option.votes/(float)total_votes * 100)));
        full.append(html);
    }

    return full;
}