Commit 95dddef2 authored by Volker Krause's avatar Volker Krause
Browse files

Send survey data back to clients.

parent b0c5a0a7
......@@ -126,11 +126,18 @@ void ProviderPrivate::submitFinished()
auto reply = qobject_cast<QNetworkReply*>(q->sender());
Q_ASSERT(reply);
qDebug() << Q_FUNC_INFO << reply->error() << reply->readAll();
if (reply->error() != QNetworkReply::NoError)
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "failed to submit user feedback:" << reply->errorString();
return;
}
lastSubmitTime = QDateTime::currentDateTime();
const auto obj = QJsonDocument::fromJson(reply->readAll()).object();
if (obj.contains(QStringLiteral("survey"))) {
const auto surveyObj = obj.value(QStringLiteral("survey")).toObject();
qDebug() << "got survey: " << surveyObj.value(QStringLiteral("url"));
}
}
......@@ -162,7 +169,6 @@ void Provider::submit()
auto url = d->serverUrl;
url.setPath(url.path() + QStringLiteral("/receiver/submit"));
qDebug() << Q_FUNC_INFO << url << d->jsonData();
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
auto reply = d->networkAccessManager->post(request, d->jsonData());
......
......@@ -50,8 +50,8 @@ public slots:
private:
friend class ProviderPrivate;
ProviderPrivate * const d;
Q_PRIVATE_SLOT(d, void aboutToQuit());
Q_PRIVATE_SLOT(d, void submitFinished());
Q_PRIVATE_SLOT(d, void aboutToQuit())
Q_PRIVATE_SLOT(d, void submitFinished())
};
}
......
......@@ -172,6 +172,19 @@ public function surveysByProductName($product)
return $surveys;
}
/** 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']);
if ($res === FALSE)
$this->fatalDbError();
$surveys = array();
foreach ($res as $row)
array_push($surveys, $row);
return $surveys;
}
/** Add a new survey for a product given by id. */
public function addSurvey($productId, $survey)
{
......
......@@ -29,7 +29,6 @@ function post_submit()
// load JSON data sent by the client
$rawPostData = file_get_contents('php://input');
$data = json_decode($rawPostData, true);
print_r($data);
// look up product
$db = new DataStore();
......@@ -50,9 +49,15 @@ function post_submit()
die('Failed to record data.');
// read survey from db
$responseData = array();
$surveys = $db->activeSurveysForProduct($product);
if (sizeof($surveys) > 0) {
// TODO pick one somehow, and only send relevant data fields
$responseData['survey'] = $surveys[0];
}
echo(json_encode($responseData));
// send survey data
echo("Big Brother is alive!");
}
}
......
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