summaryrefslogtreecommitdiff
path: root/lib/VNDB/DB/Polls.pm
blob: 0b8ff81d8085cd7bb22c57001fb39cb8cd8b2874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

package VNDB::DB::Polls;

use strict;
use warnings;
use Exporter 'import';

our @EXPORT = qw|dbPollGet dbPollVote dbPollAdd dbPollEdit|;


# Options: id, tid, uid, what
# What: votes
sub dbPollGet {
  my($self, %o) = @_;
  $o{what} ||= '';
  $o{uid} ||= $self->authInfo->{id};

  my %where = (
    $o{id}  ? ('p.id = ?'  => $o{id})  :
    $o{tid} ? ('p.tid = ?' => $o{tid}) : (),
  );

  my @select = (
    qw|p.id p.question p.max_options p.preview p.recast|,
    $o{what} =~ /votes/ ?
      ('(SELECT COUNT(DISTINCT uid) FROM polls_votes pv WHERE pv.pid = p.id) AS votes') : (),
  );
  my $p = $self->dbRow(q|
    SELECT !s
      FROM polls p
      !W|,
    join(', ', @select), \%where
  );
  return $p unless %$p;

  my $options_query = $o{what} =~ /votes/ ?
    q|SELECT id, option, COUNT(pv.optid) AS votes
        FROM polls_options po
        LEFT JOIN polls_votes pv ON po.id = pv.optid
        WHERE po.pid = ? GROUP BY id ORDER BY id| :
    q|SELECT id, option
        FROM polls_options po
        WHERE po.pid = ? ORDER BY id|;
  $p->{options} = $self->dbAll($options_query, $p->{id});

  $p->{user} = $o{uid} ? [
    map $_->{optid}, @{$self->dbAll(q|
      SELECT optid FROM polls_votes
        WHERE pid = ? AND uid = ?|, $p->{id}, $o{uid})}
  ] : [];

  return $p;
}


sub dbPollVote {
  my($self, $id, %o) = @_;

  $self->dbExec('DELETE FROM polls_votes WHERE pid = ? AND uid = ?', $id, $o{uid});
  $self->dbExec('INSERT INTO polls_votes (pid, uid, optid) VALUES (?, ?, ?)',
    $id, $o{uid}, $_) for @{$o{options}};
}


sub dbPollAdd {
  my($self, %o) = @_;

  my $id = $self->dbRow(q|
    INSERT INTO polls (tid, question, max_options, preview, recast)
      VALUES (?, ?, ?, ?, ?) RETURNING id|,
    $o{tid}, $o{question}, $o{max_options}, $o{preview}, $o{recast}
  )->{id};

  $self->dbExec('INSERT INTO polls_options (pid, option) VALUES (?, ?)', $id, $_)
    for @{$o{options}};

  return $id;
}


sub dbPollEdit {
  my($self, $id, %o) = @_;

  my %set = map exists $o{$_} ? ("$_ = ?" => $o{$_}) : (),
    qw|question max_options preview recast|;

  $self->dbExec('UPDATE polls !H WHERE id = ?', \%set, $id);
  $self->dbExec('DELETE FROM polls_options WHERE pid = ?', $id);
  $self->dbExec('INSERT INTO polls_options (pid, option) VALUES (?, ?)', $id, $_)
    for @{$o{options}};
}


1;