Skip to content

Commit

Permalink
Add the administration of the Poll system
Browse files Browse the repository at this point in the history
Ref #210
  • Loading branch information
Xety committed Apr 23, 2017
1 parent 41af6ac commit 41bf6ea
Show file tree
Hide file tree
Showing 23 changed files with 938 additions and 131 deletions.
50 changes: 50 additions & 0 deletions config/Routes/admin.php
Expand Up @@ -163,6 +163,56 @@
]
);

//Polls Routes.
$routes->connect(
'/polls/edit/:slug.:id',
[
'controller' => 'polls',
'action' => 'edit',
],
[
'_name' => 'polls-edit',
'routeClass' => 'SlugRoute',
'pass' => [
'id',
'slug'
],
'id' => '[0-9]+'
]
);

$routes->connect(
'/polls/delete/:slug.:id',
[
'controller' => 'polls',
'action' => 'delete',
],
[
'_name' => 'polls-delete',
'routeClass' => 'SlugRoute',
'pass' => [
'id',
'slug'
],
'id' => '[0-9]+'
]
);

$routes->connect(
'/polls/answers/delete/:id',
[
'controller' => 'pollsAnswers',
'action' => 'delete',
],
[
'_name' => 'polls-answers-delete',
'pass' => [
'id'
],
'id' => '[0-9]+'
]
);

//Groups Routes.
$routes->connect(
'/groups/edit/:id',
Expand Down
3 changes: 2 additions & 1 deletion config/form-templates.php
Expand Up @@ -3,5 +3,6 @@
'error' => '<div class="text-danger">{{content}}</div>',
'radioWrapper' => '<div class="radio radio-primary">{{input}}{{label}}</div>',
'checkboxWrapper' => '<div class="checkbox checkbox-primary">{{label}}</div>',
'nestingLabel' => '<label{{attrs}}>{{text}}</label>'
'nestingLabel' => '<label{{attrs}}>{{text}}</label>',
'dateWidget' => '{{day}}{{month}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}'
];
4 changes: 2 additions & 2 deletions src/Controller/Admin/AttachmentsController.php
Expand Up @@ -112,7 +112,7 @@ public function edit()
$attachment = $this->BlogAttachments
->find()
->where([
'id' => $this->request->id
'id' => $this->request->getAttribute('params')['id']
])
->first();

Expand Down Expand Up @@ -183,7 +183,7 @@ public function delete()
$attachment = $this->BlogAttachments
->find()
->where([
'id' => $this->request->id
'id' => $this->request->getAttribute('params')['id']
])
->first();

Expand Down
39 changes: 39 additions & 0 deletions src/Controller/Admin/PollsAnswersController.php
@@ -0,0 +1,39 @@
<?php
namespace App\Controller\Admin;

use App\Controller\AppController;

class PollsAnswersController extends AppController
{
/**
* Delete an answer from a poll.
*
* @return \Cake\Network\Response
*/
public function delete()
{
$this->loadModel('PollsAnswers');

$answer = $this->PollsAnswers
->find()
->where([
'id' => $this->request->getAttribute('params')['id']
])
->first();

//Check if the answer is found.
if (empty($answer)) {
$this->Flash->error(__d('admin', 'This answer doesn\'t exist or has been deleted.'));

return $this->redirect(['action' => 'index']);
}

if ($this->PollsAnswers->delete($answer)) {
$this->Flash->success(__d('admin', 'This answer has been deleted successfully.'));
} else {
$this->Flash->error(__d('admin', 'En error occured while deleting this answers.'));
}

return $this->redirect($this->referer());
}
}
230 changes: 230 additions & 0 deletions src/Controller/Admin/PollsController.php
@@ -0,0 +1,230 @@
<?php
namespace App\Controller\Admin;

use App\Controller\AppController;
use Cake\Routing\Router;

class PollsController extends AppController
{
/**
* Display all Polls.
*
* @return \Cake\Network\Response
*/
public function index()
{
$this->loadModel('Polls');

$this->paginate = [
'maxLimit' => 15
];

$polls = $this->Polls
->find()
->contain([
'Users' => function ($q) {
return $q->find('short');
},
'PollsAnswers',
'BlogArticles'
])
->order([
'Polls.created' => 'desc'
]);

$polls = $this->paginate($polls);
$this->set(compact('polls'));
}

/**
* Create a Poll.
*
* @return \Cake\Network\Response|void
*/
public function add()
{
$this->loadModel('BlogArticles');
$this->loadModel('Polls');
$this->loadModel('PollsAnswers');

$poll = $this->Polls->newEntity($this->request->getParsedBody());
$articles = $this->Polls->BlogArticles->find('list');

if ($this->request->is('post')) {
$article = $this->BlogArticles
->find()
->contain([
'Polls'
])
->where([
'BlogArticles.id' => $poll->article_id
])
->first();

//Check if the article has already a poll
if (!is_null($article->poll)) {
$this->Flash->error(
__d(
'admin',
'This article has already a poll, you can edit it <a href="{0}" class="btn btn-sm btn-danger-outline">here</a>.',
Router::url(['_name' => 'polls-edit', 'id' => $article->poll->id, 'slug' => $article->poll->name])
)
);

return $this->redirect(['action' => 'index']);
}

$pollAnswers = [];

foreach ($poll->answers as $answers => $answer) {
$answer = trim($answer);

if (!empty($answer)) {
$entity = $this->Polls->PollsAnswers->newEntity();
$entity->response = $answer;
array_push($pollAnswers, $entity);
}
}

if (empty($pollAnswers)) {
$this->Flash->error(__d('admin', 'You must add at least one answer for this poll.'));
$this->set(compact('poll', 'articles'));

return;
}
$poll->user_id = $this->Auth->user('id');
$poll->polls_answers = $pollAnswers;
$poll->unsetProperty('anwsers');

if ($poll = $this->Polls->save($poll)) {
$this->Flash->success(__d('admin', 'Your poll has been created successfully !'));

return $this->redirect(['action' => 'index']);
}
}

$this->set(compact('poll', 'articles'));
}

/**
* Edit a Poll.
*
* @return \Cake\Network\Response
*/
public function edit()
{
$this->loadModel('Polls');
$this->loadModel('BlogArticles');

$poll = $this->Polls
->find()
->contain([
'PollsAnswers'
])
->where([
'id' => $this->request->getAttribute('params')['id']
])
->first();

//Check if the poll is found.
if (empty($poll)) {
$this->Flash->error(__d('admin', 'This poll doesn\'t exist or has been deleted.'));

return $this->redirect(['action' => 'index']);
}

if ($this->request->is(['put', 'post'])) {
$this->Polls->patchEntity($poll, $this->request->getParsedBody());
$articles = $this->Polls->BlogArticles->find('list');

//Check if the article has already a poll
$article = $this->BlogArticles
->find()
->contain([
'Polls'
])
->where([
'BlogArticles.id' => $poll->article_id
])
->first();

//Check if the article has already a poll
if (!is_null($article->poll) && $article->poll->id != $this->request->getAttribute('params')['id']) {
$this->Flash->error(
__d(
'admin',
'This article has already a poll, you can edit it <a href="{0}" class="btn btn-sm btn-danger-outline">here</a>.',
Router::url(['_name' => 'polls-edit', 'id' => $article->poll->id, 'slug' => $article->poll->name])
)
);

return $this->redirect(['action' => 'index']);
}

$poll->polls_answers = [];

foreach ($poll->answers as $answers => $answer) {
$answer = trim($answer);

if (!empty($answer)) {
$entity = $this->Polls->PollsAnswers->newEntity();
$entity->response = $answer;
array_push($poll->polls_answers, $entity);
}
}

if (empty($poll->polls_answers)) {
$this->Flash->error(__d('admin', 'You must add at least one answer for this poll.'));
$this->set(compact('poll', 'articles'));

return;
}

$poll->user_id = $this->Auth->user('id');
$poll->unsetProperty('anwsers');

if ($poll = $this->Polls->save($poll)) {
$this->Flash->success(__d('admin', 'Your poll has been edited successfully !'));
} else {
$this->Flash->error(__d('admin', 'An error occurred while editing the poll.'));
}

return $this->redirect(['action' => 'index']);
}

$articles = $this->Polls->BlogArticles->find('list');
$this->set(compact('poll', 'articles'));
}

/**
* Delete a Poll.
*
* @return \Cake\Network\Response
*/
public function delete()
{
$this->loadModel('Polls');

$poll = $this->Polls
->find()
->where([
'id' => $this->request->getAttribute('params')['id']
])
->first();

//Check if the poll is found.
if (empty($poll)) {
$this->Flash->error(__d('admin', 'This poll doesn\'t exist or has been deleted.'));

return $this->redirect(['action' => 'index']);
}

if ($this->Polls->delete($poll)) {
$this->Flash->success(__d('admin', 'This poll has been deleted successfully !'));
} else {
$this->Flash->error(__d('admin', 'Unable to delete this poll.'));
}

return $this->redirect(['action' => 'index']);
}
}
18 changes: 0 additions & 18 deletions src/Model/Entity/Poll.php
Expand Up @@ -3,24 +3,6 @@

use Cake\ORM\Entity;

/**
* Poll Entity
*
* @property int $id
* @property int $user_id
* @property string $name
* @property bool $multiple_choice
* @property bool $is_display
* @property int $user_count
* @property bool $is_timed
* @property \Cake\I18n\Time $end_date
* @property \Cake\I18n\Time $created
* @property \Cake\I18n\Time $modified
*
* @property \App\Model\Entity\User $user
* @property \App\Model\Entity\PollsUser[] $polls_users
* @property \App\Model\Entity\PollsAnswer[] $polls_answers
*/
class Poll extends Entity
{

Expand Down
6 changes: 5 additions & 1 deletion src/Model/Entity/PollsAnswer.php
Expand Up @@ -35,6 +35,10 @@ class PollsAnswer extends Entity
*/
protected function _getPercentage()
{
return ($this->user_count * 100) / $this->poll->user_count;
if ($this->poll->user_count != 0) {
return ($this->user_count * 100) / $this->poll->user_count;
}

return $this->poll->user_count;
}
}

0 comments on commit 41bf6ea

Please sign in to comment.