]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Poll/Poll_response.php
b466e9f592ac84ac3a3b82986e12facafed480ca
[quix0rs-gnu-social.git] / plugins / Poll / Poll_response.php
1 <?php
2 /**
3  * Data class to record responses to polls
4  *
5  * PHP version 5
6  *
7  * @category PollPlugin
8  * @package  StatusNet
9  * @author   Brion Vibber <brion@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing the poll options and such
36  *
37  * @category PollPlugin
38  * @package  StatusNet
39  * @author   Brion Vibber <brion@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45 class Poll_response extends Managed_DataObject
46 {
47     public $__table = 'poll_response'; // table name
48     public $id;          // char(36) primary key not null -> UUID
49     public $uri;          // varchar(255)
50     public $poll_id;     // char(36) -> poll.id UUID
51     public $profile_id;  // int -> profile.id
52     public $selection;   // int -> choice #
53     public $created;     // datetime
54
55     /**
56      * Get an instance by compound key
57      *
58      * This is a utility method to get a single instance with a given set of
59      * key-value pairs. Usually used for the primary key for a compound key; thus
60      * the name.
61      *
62      * @param array $kv array of key-value mappings
63      *
64      * @return Bookmark object found, or null for no hits
65      *
66      */
67     function pkeyGet($kv)
68     {
69         return Memcached_DataObject::pkeyGet('Poll_response', $kv);
70     }
71
72     /**
73      * The One True Thingy that must be defined and declared.
74      */
75     public static function schemaDef()
76     {
77         return array(
78             'description' => 'Record of responses to polls',
79             'fields' => array(
80                 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of the response'),
81                 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'UUID to the response notice'),
82                 'poll_id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'),
83                 'profile_id' => array('type' => 'int'),
84                 'selection' => array('type' => 'int'),
85                 'created' => array('type' => 'datetime', 'not null' => true),
86             ),
87             'primary key' => array('id'),
88             'unique keys' => array(
89                 'poll_uri_key' => array('uri'),
90                 'poll_response_poll_id_profile_id_key' => array('poll_id', 'profile_id'),
91             ),
92             'indexes' => array(
93                 'poll_response_profile_id_poll_id_index' => array('profile_id', 'poll_id'),
94             )
95         );
96     }
97
98     /**
99      * Get a poll response based on a notice
100      *
101      * @param Notice $notice Notice to check for
102      *
103      * @return Poll_response found response or null
104      */
105     static function getByNotice($notice)
106     {
107         return self::staticGet('uri', $notice->uri);
108     }
109
110     /**
111      * Get the notice that belongs to this response...
112      *
113      * @return Notice
114      */
115     function getNotice()
116     {
117         return Notice::staticGet('uri', $this->uri);
118     }
119
120     function bestUrl()
121     {
122         return $this->getNotice()->bestUrl();
123     }
124
125     /**
126      *
127      * @return Poll
128      */
129     function getPoll()
130     {
131         return Poll::staticGet('id', $this->poll_id);
132     }
133     /**
134      * Save a new poll notice
135      *
136      * @param Profile $profile
137      * @param Poll    $poll the poll being responded to
138      * @param int     $selection (1-based)
139      * @param array   $opts (poll responses)
140      *
141      * @return Notice saved notice
142      */
143     static function saveNew($profile, $poll, $selection, $options=null)
144     {
145         if (empty($options)) {
146             $options = array();
147         }
148
149         if (!$poll->isValidSelection($selection)) {
150             // TRANS: Client exception thrown when responding to a poll with an invalid option.
151             throw new ClientException(_m('Invalid poll selection.'));
152         }
153         $opts = $poll->getOptions();
154         $answer = $opts[$selection - 1];
155
156         $pr = new Poll_response();
157         $pr->id          = UUID::gen();
158         $pr->profile_id  = $profile->id;
159         $pr->poll_id     = $poll->id;
160         $pr->selection   = $selection;
161
162         if (array_key_exists('created', $options)) {
163             $pr->created = $options['created'];
164         } else {
165             $pr->created = common_sql_now();
166         }
167
168         if (array_key_exists('uri', $options)) {
169             $pr->uri = $options['uri'];
170         } else {
171             $pr->uri = common_local_url('showpollresponse',
172                                         array('id' => $pr->id));
173         }
174
175         common_log(LOG_DEBUG, "Saving poll response: $pr->id $pr->uri");
176         $pr->insert();
177
178         // TRANS: Notice content voting for a poll.
179         // TRANS: %s is the chosen option in the poll.
180         $content  = sprintf(_m('voted for "%s"'),
181                             $answer);
182         $link = '<a href="' . htmlspecialchars($poll->uri) . '">' . htmlspecialchars($answer) . '</a>';
183         // TRANS: Rendered version of the notice content voting for a poll.
184         // TRANS: %s a link to the poll with the chosen option as link description.
185         $rendered = sprintf(_m('voted for "%s"'), $link);
186
187         $tags    = array();
188
189         $options = array_merge(array('urls' => array(),
190                                      'rendered' => $rendered,
191                                      'tags' => $tags,
192                                      'reply_to' => $poll->getNotice()->id,
193                                      'object_type' => PollPlugin::POLL_RESPONSE_OBJECT),
194                                $options);
195
196         if (!array_key_exists('uri', $options)) {
197             $options['uri'] = $pr->uri;
198         }
199
200         $saved = Notice::saveNew($profile->id,
201                                  $content,
202                                  array_key_exists('source', $options) ?
203                                  $options['source'] : 'web',
204                                  $options);
205
206         return $saved;
207     }
208 }