Commit 61e24d68 authored by Volker Krause's avatar Volker Krause
Browse files

Add the option to activate/deactive surveys.

Also provides the infrastructure for editing surveys on the server.
parent 2846b80c
......@@ -77,6 +77,23 @@ QNetworkReply* RESTClient::post(const QString& command, const QByteArray& data)
return reply;
}
QNetworkReply* RESTClient::put(const QString &command, const QByteArray &data)
{
Q_ASSERT(isConnected());
auto url = m_serverInfo.url();
url.setPath(url.path() + QStringLiteral("/analytics/") + command);
QNetworkRequest request(url);
const auto authToken = m_serverInfo.userName().toUtf8() + ':' + m_serverInfo.password().toUtf8();
request.setRawHeader("Authorization", "Basic " + authToken.toBase64());
request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
auto reply = m_networkAccessManager->put(request, data);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
emit errorMessage(reply->errorString());
});
return reply;
}
QNetworkReply* RESTClient::deleteResource(const QString& command)
{
Q_ASSERT(isConnected());
......
......@@ -41,6 +41,7 @@ public:
QNetworkReply* get(const QString &command);
QNetworkReply* post(const QString &command, const QByteArray &data);
QNetworkReply* put(const QString &command, const QByteArray &data);
QNetworkReply* deleteResource(const QString &command);
signals:
......
......@@ -36,6 +36,7 @@ public:
QString name;
QUrl url;
int id = -1;
bool active = false;
};
}
......@@ -76,12 +77,23 @@ void Survey::setUrl(const QUrl& url)
d->url = url;
}
bool Survey::isActive() const
{
return d->active;
}
void Survey::setActive(bool active)
{
d->active = active;
}
QByteArray Survey::toJson() const
{
QJsonObject obj;
obj.insert(QStringLiteral("id"), id());
obj.insert(QStringLiteral("name"), name());
obj.insert(QStringLiteral("url"), url().toString());
obj.insert(QStringLiteral("active"), isActive());
return QJsonDocument(obj).toJson();
}
......@@ -100,6 +112,7 @@ QVector<Survey> Survey::fromJson(const QByteArray &data)
survey.setId(id.toString().toInt());
survey.setName(obj.value(QStringLiteral("name")).toString());
survey.setUrl(QUrl(obj.value(QStringLiteral("url")).toString()));
survey.setActive(obj.value(QStringLiteral("active")).toBool());
surveys.push_back(survey);
}
return surveys;
......
......@@ -48,6 +48,9 @@ public:
QUrl url() const;
void setUrl(const QUrl& url);
bool isActive() const;
void setActive(bool enabled);
QByteArray toJson() const;
static QVector<Survey> fromJson(const QByteArray &data);
private:
......
......@@ -64,7 +64,7 @@ void SurveyModel::reload()
int SurveyModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 2;
return 3;
}
int SurveyModel::rowCount(const QModelIndex& parent) const
......@@ -85,19 +85,47 @@ QVariant SurveyModel::data(const QModelIndex& index, int role) const
case 0: return survey.name();
case 1: return survey.url().toString();
}
} else if (role == Qt::CheckStateRole) {
if (index.column() == 2)
return m_surveys.at(index.row()).isActive() ? Qt::Checked : Qt::Unchecked;
} else if (role == SurveyRole) {
return QVariant::fromValue(m_surveys.at(index.row()));
}
return {};
}
bool SurveyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.column() == 2 && role == Qt::CheckStateRole) {
auto &survey = m_surveys[index.row()];
survey.setActive(value.toInt() == Qt::Checked);
auto reply = m_restClient->put(QStringLiteral("surveys/") + QString::number(survey.id()), survey.toJson());
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
qDebug() << reply->readAll();
reload();
});
emit dataChanged(index, index);
return true;
}
return false;
}
QVariant SurveyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case 0: return tr("Name");
case 1: return tr("URL");
case 2: return tr("Active");
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
Qt::ItemFlags SurveyModel::flags(const QModelIndex &index) const
{
auto f = QAbstractTableModel::flags(index);
if (index.column() == 2)
return f | Qt::ItemIsUserCheckable;
return f;
}
......@@ -45,7 +45,9 @@ public:
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
RESTClient *m_restClient = nullptr;
......
......@@ -79,6 +79,9 @@ public function get_surveys($product)
$db = new DataStore();
$res = $db->surveysByProductName($product);
for ($i = 0; $i < sizeof($res); $i++) {
$res[$i]['active'] = $res[$i]['active'] == "1"; // fixup sqlite bool representation
}
echo(json_encode($res));
}
......@@ -100,6 +103,21 @@ public function post_surveys($product)
echo("Survey created for product $product.");
}
/** Edit an existing survey. */
public function put_surveys($surveyId)
{
$surveyId = intval($surveyId);
if ($surveyId < 0)
die("Invalid survey id.");
$surveyData = file_get_contents('php://input');
$surveyData = json_decode($surveyData, true);
$db = new DataStore();
$db->updateSurvey($surveyId, $surveyData);
echo("Survery updated.");
}
/** Delete survey. */
public function delete_surveys($survey)
{
......
......@@ -175,8 +175,7 @@ public function surveysByProductName($product)
/** List all active surveys for a given product. */
public function activeSurveysForProduct($product)
{
// TODO filter by active flag
$res = $this->db->query('SELECT * FROM surveys WHERE productId = ' . $product['id']);
$res = $this->db->query('SELECT * FROM surveys WHERE productId = ' . $product['id'] . ' AND active = 1');
if ($res === FALSE)
$this->fatalDbError();
$surveys = array();
......@@ -196,6 +195,18 @@ public function addSurvey($productId, $survey)
$this->fatalDbError();
}
/** Update an existing survey with id @p $surveyId. */
public function updateSurvey($surveyId, $surveyData)
{
$res = $this->db->exec('UPDATE surveys SET'
. ' name = ' . $this->db->quote($surveyData['name']) . ','
. ' url = ' . $this->db->quote($surveyData['url']) . ','
. ' active = ' . ($surveyData['active'] ? 1 : 0)
. ' WHERE id = ' . $surveyId);
if ($res === FALSE)
$this->fatalDbError();
}
/** Delete a survey given its internal id. */
public function deleteSurvey($surveyId)
{
......
......@@ -13,6 +13,11 @@
{
"version": 3,
"sql": ["CREATE TABLE surveys (id INTEGER PRIMARY KEY AUTOINCREMENT, productId INTEGER REFERENCES products (id), name VARCHAR, url VARCHAR NOT NULL)" ]
},
{
"version": 4,
"sql": ["ALTER TABLE surveys ADD COLUMN active INTEGER DEFAULT 0"]
}
]
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment