aboutsummaryrefslogtreecommitdiffstats
path: root/src/activitypub/apquestion.cpp
blob: d519947a66324ab4b7c72e31288eb8feb835eafb (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
#include "apquestion.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("appoll"));

    if (end_time.isValid()) {
        // TODO: add a UI setting for configuring the display of local time or UTC time.
        html.replace("{{end-time}}", render_info.locale->toString(end_time.toLocalTime()));
    }
    if (closed_time.isValid()) {
        // TODO: add a UI setting for configuring the display of local time or UTC time.
        html.replace("{{closed-time}}", render_info.locale->toString(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("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;
}