Admin: Fix the admin page

This commit is contained in:
Olivier PEREZ 2015-01-06 23:52:52 +01:00
parent a4f3041a73
commit 073bc9048d
10 changed files with 116 additions and 70 deletions

View File

@ -16,11 +16,23 @@
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/ */
namespace Framadate;
use Framadate\Services\AdminPollService;
use Framadate\Services\LogService;
use Framadate\Services\PollService;
use Framadate\Services\SuperAdminService;
use Framadate\Utils;
include_once __DIR__ . '/../app/inc/init.php'; include_once __DIR__ . '/../app/inc/init.php';
include_once __DIR__ . '/../bandeaux.php'; include_once __DIR__ . '/../bandeaux.php';
/* Services */
/*----------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$adminPollService = new AdminPollService($connect, $pollService, $logService);
$superAdminService = new SuperAdminService($connect);
// Ce fichier index.php se trouve dans le sous-repertoire ADMIN de Studs. Il sert à afficher l'intranet de studs // Ce fichier index.php se trouve dans le sous-repertoire ADMIN de Studs. Il sert à afficher l'intranet de studs
// pour modifier les sondages directement sans avoir reçu les mails. C'est l'interface d'aministration // pour modifier les sondages directement sans avoir reçu les mails. C'est l'interface d'aministration
// de l'application. // de l'application.
@ -29,39 +41,33 @@ include_once __DIR__ . '/../bandeaux.php';
Utils::print_header(_('Polls administrator')); Utils::print_header(_('Polls administrator'));
bandeau_titre(_('Polls administrator')); bandeau_titre(_('Polls administrator'));
$sondage=$connect->Execute('SELECT * FROM sondage'); $polls = $superAdminService->findAllPolls();
echo '<form action="' . Utils::get_server_name() . 'admin/index.php" method="POST">' . "\n";
echo'
<form action="' . Utils::get_server_name() . 'admin/index.php" method="POST">'."\n";
// Test et affichage du bouton de confirmation en cas de suppression de sondage // Test et affichage du bouton de confirmation en cas de suppression de sondage
while($dsondage = $sondage->FetchNextObject(false)) { foreach ($polls as $poll) {
if (Utils::issetAndNoEmpty('supprimersondage'.$dsondage->id_sondage) === true) { if (!empty($_POST['supprimersondage' . $poll->id])) {
echo ' echo '
<div class="alert alert-warning text-center"> <div class="alert alert-warning text-center">
<h3>'. _("Confirm removal of the poll ") .'"'.$dsondage->id_sondage.'</h3> <h3>' . _("Confirm removal of the poll ") . '"' . $poll->id . '</h3>
<p><button class="btn btn-default" type="submit" value="1" name="annullesuppression">' . _('Keep this poll!') . '</button> <p><button class="btn btn-default" type="submit" value="1" name="annullesuppression">' . _('Keep this poll!') . '</button>
<button type="submit" name="confirmesuppression'.$dsondage->id_sondage.'" value="1" class="btn btn-danger">'._('Remove this poll!').'</button></p> <button type="submit" name="confirmesuppression' . $poll->id . '" value="1" class="btn btn-danger">' . _('Remove this poll!') . '</button></p>
</div>'; </div>';
} }
// Traitement de la confirmation de suppression // Traitement de la confirmation de suppression
if (Utils::issetAndNoEmpty('confirmesuppression'.$dsondage->id_sondage) === true) { if (!empty($_POST['confirmesuppression' . $poll->id])) {
// On inclut la routine de suppression // On inclut la routine de suppression
$date = date('H:i:s d/m/Y'); $date = date('H:i:s d/m/Y');
if (Utils::remove_sondage($connect, $dsondage->id_sondage)) { $adminPollService->deleteEntirePoll($poll->id);
// ecriture des traces dans le fichier de logs
error_log($date . " SUPPRESSION: $dsondage->id_sondage\t$dsondage->format\t$dsondage->nom_admin\t$dsondage->mail_admin\n", 3, 'logs_studs.txt');
}
} }
} }
$sondage=$connect->Execute('SELECT * FROM sondage WHERE date_fin > DATE_SUB(now(), INTERVAL 3 MONTH) ORDER BY date_fin ASC'); $btn_logs = (is_readable('../' . LOG_FILE)) ? '<a role="button" class="btn btn-default btn-xs pull-right" href="' . Utils::get_server_name() . LOG_FILE . '">' . _("Logs") . '</a>' : '';
$nbsondages=$sondage->RecordCount();
$btn_logs = (is_readable('logs_studs.txt')) ? '<a role="button" class="btn btn-default btn-xs pull-right" href="'.str_replace('/admin','', Utils::get_server_name()).'admin/logs_studs.txt">'. _("Logs") .'</a>' : ''; echo '<p>' . count($polls) . ' ' . _("polls in the database at this time") . $btn_logs . '</p>' . "\n";
echo '<p>' . $nbsondages. ' ' . _("polls in the database at this time") . $btn_logs .'</p>'."\n";
// tableau qui affiche tous les sondages de la base // tableau qui affiche tous les sondages de la base
echo '<table class="table table-bordered"> echo '<table class="table table-bordered">
@ -77,34 +83,32 @@ echo '<table class="table table-bordered">
</tr>' . "\n"; </tr>' . "\n";
$i = 0; $i = 0;
while($dsondage = $sondage->FetchNextObject(false)) { foreach ($polls as $poll) {
/* possible en 1 bonne requête dans $sondage */ $nb_users = $pollService->countVotesByPollId($poll->id);
$subjects = $connect->Execute("SELECT * FROM sujet_studs WHERE id_sondage='$dsondage->id_sondage'");
$dsujets = $subjects->FetchObject(false);
$user_studs = $connect->Execute("SELECT * from user_studs WHERE id_sondage='$dsondage->id_sondage'");
$nb_users = $user_studs->RecordCount();
if ($poll->format === 'D') {
$format_html = '<span class="glyphicon glyphicon-calendar" aria-hidden="true"></span><span class="sr-only">'. _('Date').'</span>';
} else {
$format_html = '<span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span><span class="sr-only">'. _('Classic').'</span>';
}
echo ' echo '
<tr align="center"> <tr align="center">
<td>'.$dsondage->id_sondage.'</td> <td>' . $poll->id . '</td>
<td>'.$dsondage->format.'</td> <td>' . $format_html . '</td>
<td>'. stripslashes($dsondage->titre).'</td> <td>' . htmlentities($poll->title) . '</td>
<td>'.stripslashes($dsondage->nom_admin).'</td> <td>' . htmlentities($poll->admin_name) . '</td>
<td>'.stripslashes($dsondage->mail_admin).'</td>'; <td>' . htmlentities($poll->admin_mail) . '</td>';
if (strtotime($dsondage->date_fin) > time()) { if (strtotime($poll->end_date) > time()) {
echo '<td>'.date('d/m/y', strtotime($dsondage->date_fin)).'</td>'; echo '<td>' . date('d/m/y', strtotime($poll->end_date)) . '</td>';
} else { } else {
echo '<td><span class="text-danger">' echo '<td><span class="text-danger">' . date('d/m/y', strtotime($poll->end_date)) . '</span></td>';
. date('d/m/y', strtotime($dsondage->date_fin))
. '</span></td>';
} }
echo ' echo '
<td>' . $nb_users . '</td> <td>' . $nb_users . '</td>
<td><a href="' . Utils::getUrlSondage($dsondage->id_sondage) . '" class="btn btn-link" title="'. _('See the poll') .'"><span class="glyphicon glyphicon-eye-open"></span><span class="sr-only">' . _('See the poll') . '</span></a></td> <td><a href="' . Utils::getUrlSondage($poll->id) . '" class="btn btn-link" title="' . _('See the poll') . '"><span class="glyphicon glyphicon-eye-open"></span><span class="sr-only">' . _('See the poll') . '</span></a></td>
<td><a href="' . Utils::getUrlSondage($dsondage->id_sondage_admin, true) . '" class="btn btn-link" title="'. _('Change the poll') .'"><span class="glyphicon glyphicon-pencil"></span><span class="sr-only">' . _("Change the poll") . '</span></a></td> <td><a href="' . Utils::getUrlSondage($poll->admin_id, true) . '" class="btn btn-link" title="' . _('Change the poll') . '"><span class="glyphicon glyphicon-pencil"></span><span class="sr-only">' . _("Change the poll") . '</span></a></td>
<td><button type="submit" name="supprimersondage'.$dsondage->id_sondage.'" value="'. _('Remove the poll') .'" class="btn btn-link" title="'. _("Remove the poll") .'"><span class="glyphicon glyphicon-trash text-danger"></span><span class="sr-only">' . _('Remove the poll') . '</span></td> <td><button type="submit" name="supprimersondage' . $poll->id . '" value="' . _('Remove the poll') . '" class="btn btn-link" title="' . _("Remove the poll") . '"><span class="glyphicon glyphicon-trash text-danger"></span><span class="sr-only">' . _('Remove the poll') . '</span></td>
</tr>' . "\n"; </tr>' . "\n";
++$i; ++$i;
} }
@ -112,8 +116,3 @@ while($dsondage = $sondage->FetchNextObject(false)) {
echo '</table></form>' . "\n"; echo '</table></form>' . "\n";
bandeau_pied(true); bandeau_pied(true);
// si on annule la suppression, rafraichissement de la page
/*if (Utils::issetAndNoEmpty('annulesuppression') === true) {
// TODO
}*/

View File

@ -36,7 +36,7 @@ $editingVoteId = 0;
/* Services */ /* Services */
/*----------*/ /*----------*/
$logService = new LogService(LOG_FILE); $logService = new LogService();
$pollService = new PollService($connect, $logService); $pollService = new PollService($connect, $logService);
$adminPollService = new AdminPollService($connect, $pollService, $logService); $adminPollService = new AdminPollService($connect, $pollService, $logService);
$inputService = new InputService(); $inputService = new InputService();

View File

@ -274,4 +274,21 @@ class FramaDB {
return $prepared->fetchAll(); return $prepared->fetchAll();
} }
public function findAllPolls() {
$prepared = $this->prepare('SELECT * FROM ' . Utils::table('poll') . ' ORDER BY end_date ASC');
$prepared->execute([]);
return $prepared->fetchAll();
}
public function countVotesByPollId($poll_id) {
$prepared = $this->prepare('SELECT count(1) nb FROM ' . Utils::table('vote') . ' WHERE poll_id = ?');
$prepared->execute([$poll_id]);
$result = $prepared->fetch();
$prepared->closeCursor();
return $result->nb;
}
} }

View File

@ -8,10 +8,7 @@ namespace Framadate\Services;
*/ */
class LogService { class LogService {
private $output; function __construct() {
function __construct($output) {
$this->output = $output;
} }
/** /**
@ -21,7 +18,7 @@ class LogService {
* @param $message string some message * @param $message string some message
*/ */
function log($tag, $message) { function log($tag, $message) {
error_log(date('Ymd His') . ' [' . $tag . '] ' . $message . "\n", 3, $this->output); error_log(date('Ymd His') . ' [' . $tag . '] ' . $message . "\n", 3, LOG_FILE);
} }
} }

View File

@ -75,6 +75,10 @@ class PollService {
return $this->connect->insertComment($poll_id, $name, $comment); return $this->connect->insertComment($poll_id, $name, $comment);
} }
public function countVotesByPollId($poll_id) {
return $this->connect->countVotesByPollId($poll_id);
}
function computeBestChoices($votes) { function computeBestChoices($votes) {
$result = []; $result = [];
foreach ($votes as $vote) { foreach ($votes as $vote) {

View File

@ -0,0 +1,29 @@
<?php
namespace Framadate\Services;
use Framadate\FramaDB;
/**
* The class provides action for application administrators.
*
* @package Framadate\Services
*/
class SuperAdminService {
private $connect;
function __construct(FramaDB $connect) {
$this->connect = $connect;
}
/**
* Return the list of all polls.
*
* @return array All the polls
*/
public function findAllPolls() {
return $this->connect->findAllPolls();
}
}

View File

@ -27,7 +27,7 @@ include_once __DIR__ . '/app/inc/init.php';
/* Service */ /* Service */
/*---------*/ /*---------*/
$logService = new LogService(LOG_FILE); $logService = new LogService();
$pollService = new PollService($connect, $logService); $pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp']); $mailService = new MailService($config['use_smtp']);
$purgeService = new PurgeService($connect, $logService); $purgeService = new PurgeService($connect, $logService);

View File

@ -28,7 +28,7 @@ include_once __DIR__ . '/app/inc/init.php';
/* Service */ /* Service */
/*---------*/ /*---------*/
$logService = new LogService(LOG_FILE); $logService = new LogService();
$pollService = new PollService($connect, $logService); $pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp']); $mailService = new MailService($config['use_smtp']);
$purgeService = new PurgeService($connect, $logService); $purgeService = new PurgeService($connect, $logService);

View File

@ -36,7 +36,7 @@ $poll = null;
/* Services */ /* Services */
/*----------*/ /*----------*/
$logService = new LogService(LOG_FILE); $logService = new LogService();
$pollService = new PollService($connect, $logService); $pollService = new PollService($connect, $logService);
/* PAGE */ /* PAGE */

View File

@ -36,7 +36,7 @@ $editingVoteId = 0;
/* Services */ /* Services */
/*----------*/ /*----------*/
$logService = new LogService(LOG_FILE); $logService = new LogService();
$pollService = new PollService($connect, $logService); $pollService = new PollService($connect, $logService);
$inputService = new InputService(); $inputService = new InputService();
$mailService = new MailService($config['use_smtp']); $mailService = new MailService($config['use_smtp']);