WIP> admin: Add availability to add a slot to a poll

This commit is contained in:
Olivier Perez [a570709] 2014-12-22 14:18:33 +01:00
parent 21ce86e2b7
commit 94e87a3182
4 changed files with 68 additions and 5 deletions

View File

@ -291,6 +291,16 @@ if (isset($_POST['add_slot'])) {
$smarty->display('add_slot.tpl'); $smarty->display('add_slot.tpl');
exit; exit;
} }
if (isset($_POST['confirm_add_slot'])) {
$newdate = filter_input(INPUT_POST, 'newdate', FILTER_DEFAULT);
$newmoment = filter_input(INPUT_POST, 'newmoment', FILTER_DEFAULT);
if ($adminPollService->addSlot($poll_id, $newdate, $newmoment)) {
$message = new Message('success', _('Column added.'));
} else {
$message = new Message('danger', _('Failed to add the column.'));
}
}
// Retrieve data // Retrieve data
$slots = $pollService->allSlotsByPollId($poll_id); $slots = $pollService->allSlotsByPollId($poll_id);

View File

@ -33,7 +33,7 @@ class FramaDB {
function areTablesCreated() { function areTablesCreated() {
$result = $this->pdo->query('SHOW TABLES'); $result = $this->pdo->query('SHOW TABLES');
$schemas = $result->fetchAll(\PDO::FETCH_COLUMN); $schemas = $result->fetchAll(\PDO::FETCH_COLUMN);
return !empty(array_diff($schemas, ['comments', 'sondage', 'sujet_studs', 'user_studs'])); return 0 != count(array_diff($schemas, ['comments', 'sondage', 'sujet_studs', 'user_studs']));
} }
function prepare($sql) { function prepare($sql) {
@ -127,6 +127,35 @@ class FramaDB {
return $prepared->execute([$index, $index + 2, $poll_id]); return $prepared->execute([$index, $index + 2, $poll_id]);
} }
/**
* Find the slot into poll for a given datetime.
*
* @param $poll_id int The ID of the poll
* @param $datetime int The datetime of the slot
* @return mixed Object The slot found, or null
*/
function findSlotByPollIdAndDatetime($poll_id, $datetime) {
$prepared = $this->prepare('SELECT * FROM sujet_studs WHERE id_sondage = ? AND SUBSTRING_INDEX(sujet, \'@\', 1) = ?');
$prepared->execute([$poll_id, $datetime]);
$slot = $prepared->fetch();
$prepared->closeCursor();
return $slot;
}
/**
* Insert a new slot into a given poll.
*
* @param $poll_id int The ID of the poll
* @param $slot mixed The value of the slot
* @return bool true if action succeeded
*/
function insertSlot($poll_id, $slot) {
$prepared = $this->prepare('INSERT INTO sujet_studs (id_sondage, sujet) VALUES (?,?)');
return $prepared->execute([$poll_id, $slot]);
}
/** /**
* Update a slot into a poll. * Update a slot into a poll.
* *

View File

@ -106,5 +106,30 @@ class AdminPollService {
$this->connect->commit(); $this->connect->commit();
} }
public function addSlot($poll_id, $newdate, $newmoment) {
$ex = explode('/', $newdate);
$datetime = mktime(0,0,0, $ex[1], $ex[0], $ex[2]);
$slot = $this->connect->findSlotByPollIdAndDatetime($poll_id, $datetime);
if ($slot != null) {
// Update found slot
$moments = explode('@', $slot->sujet)[1];
foreach ($moments as $moment) {
if ($moment == $newmoment) {
return false;
}
}
// TODO Implements
} else {
// TODO Found index of insertion, in order to update user votes
$this->connect->insertSlot($poll_id, $datetime . '@' . $newmoment);
}
return true;
}
} }

View File

@ -16,16 +16,15 @@
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="newhour" class="col-md-4">{_("Time")}</label> <label for="newmoment" class="col-md-4">{_("Time")}</label>
<div class="col-md-8"> <div class="col-md-8">
<input type="text" id="newhour" name="newhour" class="form-control" /> <input type="text" id="newmoment" name="newmoment" class="form-control" />
</div> </div>
</div> </div>
<div class="pull-right"> <div class="form-group">
<button class="btn btn-default" type="submit" name="back">{_('Back to the poll')}</button> <button class="btn btn-default" type="submit" name="back">{_('Back to the poll')}</button>
<button type="submit" name="confirm_add_slot" class="btn btn-success">{_('Add a column')}</button> <button type="submit" name="confirm_add_slot" class="btn btn-success">{_('Add a column')}</button>
</div> </div>
<div class="clearfix"></div>
</div> </div>
</form> </form>
{/block} {/block}