Skip to content

Commit

Permalink
Add some more improvements for feedback tool
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohutai committed Feb 15, 2018
1 parent 14bc3f5 commit 4d3de0f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 21 deletions.
24 changes: 24 additions & 0 deletions src/Constant/FeedbackStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bolt\Extension\TwoKings\IsUseful\Constant;

/**
*
*/
class FeedbackStatus {

const UNREAD = 'new'; // because `NEW` is not allowed
const READ = 'read';
const DONE = 'done';
const REMOVED = 'removed'; // hidden for normal list

/**
* Returns a list of constants used in this class.
*/
public static function getConstants()
{
$class = new \ReflectionClass(__CLASS__);
return $class->getConstants();
}

}
51 changes: 38 additions & 13 deletions src/Controller/BackendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bolt\Extension\TwoKings\IsUseful\Controller;

use Bolt\Controller\Base;
use Bolt\Extension\TwoKings\IsUseful\Constant\FeedbackStatus;
use Silex\Application;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -51,10 +52,11 @@ public function addRoutes(ControllerCollection $ctr)
;

$ctr
->get('/markasread/{id}', [$this, 'markFeedback'])
->get('/status/{id}/{status}', [$this, 'setFeedbackStatus'])
->assert('id', '\d+')
->assert('status', 'new|read|done|removed')
->before([$this, 'before'])
->bind('is_useful.feedback.markasread')
->bind('is_useful.feedback.status')
;

return $ctr;
Expand Down Expand Up @@ -83,7 +85,16 @@ public function before(Request $request, Application $app)
*/
public function indexGet(Application $app, Request $request)
{
$stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful`");
$status = FeedbackStatus::REMOVED;

$sql = "SELECT `bolt_is_useful`.*, COUNT(`bolt_is_useful_feedback`.`is_useful_id`) AS count";
$sql .= " FROM `bolt_is_useful`";
$sql .= " LEFT JOIN `bolt_is_useful_feedback` ON `bolt_is_useful`.`id` = `bolt_is_useful_feedback`.`is_useful_id`";
$sql .= " AND `bolt_is_useful_feedback`.`status` != :status";
$sql .= " GROUP BY `bolt_is_useful`.`id`";
$stmt = $app['db']->prepare($sql);
$stmt->bindParam('status', $status);
// $stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful`");
$stmt->execute();
$data = $stmt->fetchAll();

Expand All @@ -98,7 +109,10 @@ public function indexGet(Application $app, Request $request)
*/
public function unreadGet(Application $app, Request $request)
{
$stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `read` = 0");
$status = FeedbackStatus::UNREAD;

$stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `status` = :status");
$stmt->bindParam('status', $status);
$stmt->execute();
$feedback = $stmt->fetchAll();

Expand All @@ -123,8 +137,11 @@ public function view(Application $app, Request $request, $id)

// check iff empty

$stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `is_useful_id` = :id AND `hide` = 0");
$status = FeedbackStatus::REMOVED;

$stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `is_useful_id` = :id AND `status` != :status");
$stmt->bindParam('id', $id);
$stmt->bindParam('status', $status);
$stmt->execute();
$feedback = $stmt->fetchAll();

Expand All @@ -140,11 +157,15 @@ public function view(Application $app, Request $request, $id)
*/
public function deleteFeedback(Application $app, Request $request, $id)
{
$status = FeedbackStatus::REMOVED;

// (1) Remove
$stmt = $app['db']->prepare("UPDATE `bolt_is_useful_feedback` SET `hide` = 1 WHERE `id` = :id");
$stmt = $app['db']->prepare("UPDATE `bolt_is_useful_feedback` SET `status` = :status WHERE `id` = :id");
$stmt->bindParam('status', $status);
$stmt->bindParam('id', $id);
$stmt->execute();

/*
// (2) Fetch
$stmt = $app['db']->prepare("SELECT * FROM `bolt_is_useful_feedback` WHERE `id` = :id");
$stmt->bindParam('id', $id);
Expand Down Expand Up @@ -188,20 +209,24 @@ public function deleteFeedback(Application $app, Request $request, $id)
$stmt->bindParam('totals', $totals);
$stmt->bindParam('ips', $ips);
$stmt->execute();
//*/

return $this->redirect( $request->headers->get('referer') );

// return $this->redirect();
}

/**
* Mark feedback by ID as read
*
*/
public function markFeedback(Application $app, Request $request, $id)
public function setFeedbackStatus(Application $app, Request $request, $id, $status)
{
$stmt = $app['db']->prepare("UPDATE `bolt_is_useful_feedback` SET `read` = 1 WHERE `id` = :id");
$stmt->bindParam('id', $id);
$stmt->execute();
if (in_array($status, FeedbackStatus::getConstants())) {
$stmt = $app['db']->prepare("UPDATE `bolt_is_useful_feedback` SET `status` = :status WHERE `id` = :id");
$stmt->bindParam('id', $id);
$stmt->bindParam('status', $status);
$stmt->execute();
} else {
// todo: invalid status
}

return $this->redirect( $request->headers->get('referer') );
}
Expand Down
5 changes: 3 additions & 2 deletions src/Table/IsUsefulFeedbackTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ protected function addColumns()
$this->table->addColumn('message' , 'text' , ['notnull' => false]);
$this->table->addColumn('url' , 'string' , ['notnull' => false]);
$this->table->addColumn('datetime' , 'datetime', ['notnull' => false]);
$this->table->addColumn('read' , 'integer' , ['notnull' => false, 'default' => 0, ]);
$this->table->addColumn('hide' , 'integer' , ['notnull' => false, 'default' => 0, ]);
// $this->table->addColumn('read' , 'integer' , ['notnull' => false, 'default' => 0, ]);
// $this->table->addColumn('hide' , 'integer' , ['notnull' => false, 'default' => 0, ]);
$this->table->addColumn('status' , 'string' , ['default' => 'new']);
}

/**
Expand Down
23 changes: 20 additions & 3 deletions templates/backend/feedback.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,45 @@
</ul>
{% endif %}

{% set css_status_mapping = {
new : 'label label-default',
read : 'label label-info',
done : 'label label-success',
removed : 'label label-danger'
} %}

{% if feedback is not empty %}
<table data-table>
<thead>
<th>IP</th>
<th>Datetime</th>
<th>Message</th>
<th>Status</th>
<th>Actions</th>
</thead>
<tbody>
{% for item in feedback %}
{% set ip = item.ip %}
{% set datetime = item.datetime %}
{% set message = item.message %}
{% set deleteLink = path('is_useful.feedback.delete', { id: item.id }) %}
{% set markLink = path('is_useful.feedback.markasread', { id: item.id }) %}
{% set status = item.status %}
{% set css_status = css_status_mapping[status] %}
{% set deleteLink = path('is_useful.feedback.delete', { id: item.id }) %}
{% set statusReadLink = path('is_useful.feedback.status', { id: item.id, status: 'read' }) %}
{% set statusDoneLink = path('is_useful.feedback.status', { id: item.id, status: 'done' }) %}
<tr>
<td>{{ ip }}</td>
<td>{{ datetime }}</td>
<td>{{ message }} </td>
<td><span class="{{ css_status }}">{{ status }}</span></td>
<td>
<a class="btn btn-tertiary" href="{{ deleteLink }}" onclick="return confirm('Are you sure?')">Delete</a>
<a class="btn btn-secondary" href="{{ markLink }}">Mark as read</a>
{% if status != 'read' %}
<a class="btn btn-secondary" href="{{ statusReadLink }}">Mark as read</a>
{% endif %}
{% if status != 'done' %}
<a class="btn btn-secondary" href="{{ statusDoneLink }}">Mark as done</a>
{% endif %}
</td>
</tr>
{% endfor %}
Expand Down
8 changes: 7 additions & 1 deletion templates/backend/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
{% set totalsTotal = totalsNo + totalsYes %}
{% set percentageNo = (totalsNo / totalsTotal) * 100 %}
{% set percentageYes = (totalsYes / totalsTotal) * 100 %}
{% set totalFeedback = item.count %}
{% setcontent record = "#{contenttype}/#{contentid}" %}
<tr>
<td>
Expand All @@ -46,7 +47,12 @@
<td>{{ percentageYes|number_format(2,'.','') }}%</td>
<td>{{ totalsTotal }}</td>
<td>
<a class="btn btn-tertiary" href="{{ path('is_useful.view', { id: item.id }) }}">View</a>
{% if totalFeedback > 0 %}
<a class="btn btn-tertiary" href="{{ path('is_useful.view', { id: item.id }) }}">
View
<span class="badge">{{ totalFeedback }}</span>
</a>
{% endif %}
</td>
</tr>
{% set cumulativeNo = cumulativeNo + totalsNo %}
Expand Down
6 changes: 4 additions & 2 deletions templates/backend/unread.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
{% set datetime = item.datetime %}
{% set message = item.message %}
{% set deleteLink = path('is_useful.feedback.delete', { id: item.id }) %}
{% set markLink = path('is_useful.feedback.markasread', { id: item.id }) %}
{% set statusReadLink = path('is_useful.feedback.status', { id: item.id, status: 'read' }) %}
{% set statusDoneLink = path('is_useful.feedback.status', { id: item.id, status: 'done' }) %}
<tr>
<td>{{ ip }}</td>
<td>{{ datetime }}</td>
<td>{{ message }} </td>
<td>
<a class="btn btn-tertiary" href="{{ deleteLink }}" onclick="return confirm('Are you sure?')">Delete</a>
<a class="btn btn-secondary" href="{{ markLink }}">Mark as read</a>
<a class="btn btn-secondary" href="{{ statusReadLink }}">Mark as read</a>
<a class="btn btn-secondary" href="{{ statusDoneLink }}">Mark as done</a>
</td>
</tr>
{% endfor %}
Expand Down

0 comments on commit 4d3de0f

Please sign in to comment.