rev.29 Add poll 7

Change the structure of the poll.
Fix errors.
This commit is contained in:
Visman 2020-11-21 18:55:18 +07:00
parent 54b228ccf1
commit d1e883d81b
11 changed files with 54 additions and 42 deletions

View file

@ -803,7 +803,6 @@ class Install extends Admin
'poll_type' => ['SMALLINT UNSIGNED', false, 0],
'poll_time' => ['INT(10) UNSIGNED', false, 0],
'poll_term' => ['TINYINT', false, 0],
'poll_votes' => ['INT(10) UNSIGNED', false, 0],
],
'PRIMARY KEY' => ['id'],
'INDEXES' => [

View file

@ -19,7 +19,7 @@ class Update extends Admin
{
const PHP_MIN = '7.3.0';
const LATEST_REV_WITH_DB_CHANGES = 28;
const LATEST_REV_WITH_DB_CHANGES = 29;
const LOCK_NAME = 'lock_update';
const LOCk_TTL = 1800;
@ -1153,4 +1153,26 @@ class Update extends Admin
return null;
}
/**
* rev.28 to rev.29
*/
protected function stageNumber28(array $args): ?int
{
$query = 'UPDATE ::poll AS pl
SET pl.qna_text=CONCAT(pl.votes, \'|\', pl.qna_text)
WHERE pl.field_id=0';
$this->c->DB->query($query);
$query = 'UPDATE ::poll AS pl, ::topics AS t
SET pl.votes=t.poll_votes
WHERE pl.field_id=0 AND pl.tid=t.id';
$this->c->DB->query($query);
$this->c->DB->dropField('topics', 'poll_votes');
return null;
}
}

View file

@ -242,7 +242,6 @@ class Edit extends Page
$topic->poll_term = $v->poll['hide_result']
? ($topic->poll_term ?: $this->c->config->i_poll_term)
: 0;
# $topic->poll_votes = 0;
$poll->__question = $v->poll['question'];
$poll->__answer = $v->poll['answer'];
@ -254,7 +253,6 @@ class Edit extends Page
$topic->poll_type = 0;
$topic->poll_time = 0;
$topic->poll_term = 0;
$topic->poll_votes = 0;
$this->c->polls->delete($poll);
}
@ -263,7 +261,6 @@ class Edit extends Page
$topic->poll_type = $v->poll['duration'] > 0 ? 1000 + $v->poll['duration'] : 1; // ???? перенести в модель poll?
$topic->poll_time = \time();
$topic->poll_term = $v->poll['hide_result'] ? $this->c->config->i_poll_term : 0;
$topic->poll_votes = 0;
$poll = $this->c->polls->create([
'tid' => $topic->id,

View file

@ -260,7 +260,6 @@ class Post extends Page
$topic->poll_type = $v->poll['duration'] > 0 ? 1000 + $v->poll['duration'] : 1; // ???? перенести в модель poll?
$topic->poll_time = $now;
$topic->poll_term = $v->poll['hide_result'] ? $this->c->config->i_poll_term : 0;
# $topic->poll_votes = ;
$poll = $this->c->polls->create([
'tid' => $topic->id,

View file

@ -231,7 +231,7 @@ trait PostFormTrait
$fieldset["poll[question][{$qid}]"] = [
'type' => 'text',
'maxlength' => '255',
'maxlength' => '240',
'caption' => __('Question text label'),
'value' => $vars['poll']['question'][$qid] ?? null,
'disabled' => $vars['pollNoEdit'] ?? null,
@ -249,7 +249,7 @@ trait PostFormTrait
for ($fid = 1; $fid <= $this->c->config->i_poll_max_fields; $fid++) {
$fieldset["poll[answer][{$qid}][{$fid}]"] = [
'type' => 'text',
'maxlength' => '255',
'maxlength' => '240',
'caption' => __('Answer %s label', $fid),
'value' => $vars['poll']['answer'][$qid][$fid] ?? null,
'disabled' => $vars['pollNoEdit'] ?? null,

View file

@ -205,9 +205,9 @@ trait PostValidatorTrait
'poll_enable' => 'checkbox|check_poll',
'poll.duration' => 'integer|min:0|max:366',
'poll.hide_result' => 'checkbox',
'poll.question.*' => 'string:trim|max:255',
'poll.question.*' => 'string:trim|max:240',
'poll.type.*' => 'integer|min:1|max:' . $this->c->config->i_poll_max_fields,
'poll.answer.*.*' => 'string:trim|max:255',
'poll.answer.*.*' => 'string:trim|max:240',
]);
}

View file

@ -36,6 +36,7 @@ class Load extends Action
'answer' => [],
'vote' => [],
'type' => [],
'total' => [],
];
while ($row = $stmt->fetch()) {
@ -43,8 +44,10 @@ class Load extends Action
$fid = $row['field_id'];
if (0 === $fid) {
$data['question'][$qid] = $row['qna_text'];
$data['type'][$qid] = $row['votes'];
list($type, $question) = \explode('|', $row['qna_text'], 2);
$data['question'][$qid] = $question;
$data['type'][$qid] = (int) $type;
$data['total'][$qid] = $row['votes'];
} else {
$data['answer'][$qid][$fid] = $row['qna_text'];
$data['vote'][$qid][$fid] = $row['votes'];

View file

@ -114,7 +114,7 @@ class Model extends DataModel
)
&& (
0 === $this->parent->poll_term
|| $this->parent->poll_term < $this->parent->poll_votes
|| $this->parent->poll_term < \max($this->total)
|| ! $this->isOpen
);
}

View file

@ -31,17 +31,14 @@ class Save extends Action
$vars = [
':tid' => $poll->tid,
];
$queryIn = 'INSERT INTO ::poll (tid, question_id, field_id, qna_text, votes)
VALUES (?i:tid, ?i:qid, ?i:fid, ?s:qna, ?i:votes)';
$queryU1 = 'UPDATE :poll
SET qna_text=?s:qna, votes=?i:votes
WHERE tid=?i:tid AND question_id=?i:qid AND field_id=?i:fid';
$queryU1 = 'UPDATE :poll
$queryIn = 'INSERT INTO ::poll (tid, question_id, field_id, qna_text)
VALUES (?i:tid, ?i:qid, ?i:fid, ?s:qna)';
$queryU1 = 'UPDATE ::poll
SET qna_text=?s:qna
WHERE tid=?i:tid AND question_id=?i:qid AND field_id=?i:fid';
$queryD1 = 'DELETE FROM :poll
$queryD1 = 'DELETE FROM ::poll
WHERE tid=?i:tid AND question_id IN(?ai:qids)';
$queryD2 = 'DELETE FROM :poll
$queryD2 = 'DELETE FROM ::poll
WHERE tid=?i:tid AND question_id=?i:qid AND field_id IN(?ai:fid)';
$modified = false;
@ -49,10 +46,9 @@ class Save extends Action
$oldType = $old->type;
foreach ($poll->question as $qid => $qna) {
$vars[':qid'] = $qid;
$vars[':fid'] = 0;
$vars[':qna'] = $qna;
$vars[':votes'] = $poll->type[$qid];
$vars[':qid'] = $qid;
$vars[':fid'] = 0;
$vars[':qna'] = $poll->type[$qid] . '|' . $qna;
if (! isset($oldQuestion[$qid])) {
$modified = true;
@ -67,12 +63,11 @@ class Save extends Action
$this->c->DB->exec($queryU1, $vars);
}
$vars[':votes'] = 0;
$oldAnswer = $old->answer[$qid] ?? [];
$oldAnswer = $old->answer[$qid] ?? [];
foreach ($poll->answer[$qid] as $fid => $qna) {
$vars[':fid'] = $fid;
$vars[':qna'] = $qna;
$vars[':fid'] = $fid;
$vars[':qna'] = $qna;
if (! isset($oldAnswer[$fid])) {
$modified = true;
@ -81,7 +76,7 @@ class Save extends Action
} elseif ($qna !== $oldAnswer[$fid]) {
$modified = true;
$this->c->DB->exec($queryU2, $vars);
$this->c->DB->exec($queryU1, $vars);
}
unset($oldAnswer[$fid]);
@ -126,22 +121,19 @@ class Save extends Action
$vars = [
':tid' => $poll->tid,
];
$query = 'INSERT INTO ::poll (tid, question_id, field_id, qna_text, votes)
VALUES (?i:tid, ?i:qid, ?i:fid, ?s:qna, ?i:votes)';
$query = 'INSERT INTO ::poll (tid, question_id, field_id, qna_text)
VALUES (?i:tid, ?i:qid, ?i:fid, ?s:qna)';
foreach ($poll->question as $qid => $qna) {
$vars[':qid'] = $qid;
$vars[':fid'] = 0;
$vars[':qna'] = $qna;
$vars[':votes'] = $poll->type[$qid];
$vars[':qid'] = $poll->type[$qid] . '|' . $qid;
$vars[':fid'] = 0;
$vars[':qna'] = $qna;
$this->c->DB->exec($query, $vars);
$vars[':votes'] = 0;
foreach ($poll->answer[$qid] as $fid => $qna) {
$vars[':fid'] = $fid;
$vars[':qna'] = $qna;
$vars[':fid'] = $fid;
$vars[':qna'] = $qna;
$this->c->DB->exec($query, $vars);
}

View file

@ -44,7 +44,7 @@ if (
}
$c->PUBLIC_URL = $c->BASE_URL . $forkPublicPrefix;
$c->FORK_REVISION = 28;
$c->FORK_REVISION = 29;
$c->START = $forkStart;
$c->DIR_APP = __DIR__;
$c->DIR_PUBLIC = $forkPublic;

View file

@ -33,7 +33,7 @@
</li>
@endforeach
</ol>
<p class="f-poll-total">{!! __('In total voted: %s', 0) !!}</p>
<p class="f-poll-total">{!! __('In total voted: %s', $poll->total[$q]) !!}</p>
</fieldset>
@endforeach
@if ($poll->canVote)