WIP> admin: Add availability to add a slot to a poll
This commit is contained in:
parent
21ce86e2b7
commit
94e87a3182
@ -291,6 +291,16 @@ if (isset($_POST['add_slot'])) {
|
||||
$smarty->display('add_slot.tpl');
|
||||
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
|
||||
$slots = $pollService->allSlotsByPollId($poll_id);
|
||||
|
@ -33,7 +33,7 @@ class FramaDB {
|
||||
function areTablesCreated() {
|
||||
$result = $this->pdo->query('SHOW TABLES');
|
||||
$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) {
|
||||
@ -127,6 +127,35 @@ class FramaDB {
|
||||
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.
|
||||
*
|
||||
|
@ -106,5 +106,30 @@ class AdminPollService {
|
||||
$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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,16 +16,15 @@
|
||||
</div>
|
||||
</div>
|
||||
<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">
|
||||
<input type="text" id="newhour" name="newhour" class="form-control" />
|
||||
<input type="text" id="newmoment" name="newmoment" class="form-control" />
|
||||
</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 type="submit" name="confirm_add_slot" class="btn btn-success">{_('Add a column')}</button>
|
||||
</div>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
</form>
|
||||
{/block}
|
Loading…
Reference in New Issue
Block a user