]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/classes/QnA_Vote.php
Merge branch 'master' into 1.0.x
[quix0rs-gnu-social.git] / plugins / QnA / classes / QnA_Vote.php
1 <?php
2 /**
3  * Data class to save users votes for
4  *
5  * PHP version 5
6  *
7  * @category QnA
8  * @package  StatusNet
9  * @author   Zach Copley <zach@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 votes on question and answers
36  *
37  * @category QnA
38  * @package  StatusNet
39  * @author   Zach Copley <zach@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 QnA_Vote extends Managed_DataObject
46 {
47     const UP   = 'http://activitystrea.ms/schema/1.0/like';
48     const DOWN = 'http://activityschema.org/object/dislike'; // Gar!
49
50     public $__table = 'qna_vote'; // table name
51     public $id;          // char(36) primary key not null -> UUID
52     public $question_id; // char(36) -> question.id UUID
53     public $answer_id;   // char(36) -> question.id UUID
54     public $type;        // tinyint -> vote: up (1) or down (-1)
55     public $profile_id;  // int -> question.id
56     public $created;     // datetime
57
58     /**
59      * Get an instance by key
60      *
61      * This is a utility method to get a single instance with a given key value.
62      *
63      * @param string $k Key to use to lookup
64      * @param mixed  $v Value to lookup
65      *
66      * @return QnA_Vote object found, or null for no hits
67      *
68      */
69     function staticGet($k, $v=null)
70     {
71         return Memcached_DataObject::staticGet('QnA_Vote', $k, $v);
72     }
73
74     /**
75      * Get an instance by compound key
76      *
77      * This is a utility method to get a single instance with a given set of
78      * key-value pairs. Usually used for the primary key for a compound key; thus
79      * the name.
80      *
81      * @param array $kv array of key-value mappings
82      *
83      * @return QnA_Vote object found, or null for no hits
84      *
85      */
86     function pkeyGet($kv)
87     {
88         return Memcached_DataObject::pkeyGet('QnA_Vote', $kv);
89     }
90
91     /**
92      * The One True Thingy that must be defined and declared.
93      */
94     public static function schemaDef()
95     {
96         return array(
97             'description' => 'For storing votes on questions and answers',
98             'fields' => array(
99                 'id' => array(
100                     'type'        => 'char',
101                     'length'      => 36,
102                     'not null'    => true,
103                     'description' => 'UUID of the vote'
104                 ),
105                 'question_id' => array(
106                     'type'        => 'char',
107                     'length'      => 36,
108                     'not null'    => true,
109                     'description' => 'UUID of question being voted on'
110                 ),
111                 'answer_id' => array(
112                     'type'        => 'char',
113                     'length'      => 36,
114                     'not null'    => true,
115                     'description' => 'UUID of answer being voted on'
116                 ),
117                 'vote'       => array('type' => 'int', 'size' => 'tiny'),
118                 'profile_id' => array('type' => 'int'),
119                 'created'    => array('type' => 'datetime', 'not null' => true),
120             ),
121             'primary key' => array('id'),
122             'indexes' => array(
123                 'profile_id_question_Id_index' => array(
124                     'profile_id',
125                     'question_id'
126                 ),
127                 'profile_id_question_Id_index' => array(
128                     'profile_id',
129                     'answer_id'
130                 )
131             )
132         );
133     }
134
135     /**
136      * Save a vote on a question or answer
137      *
138      * @param Profile  $profile
139      * @param QnA_Question the question being voted on
140      * @param QnA_Answer   the answer being voted on
141      * @param vote
142      * @param array
143      *
144      * @return Void
145      */
146     static function save($profile, $question, $answer, $vote)
147     {
148         $v = new QnA_Vote();
149         $v->id          = UUID::gen();
150         $v->profile_id  = $profile->id;
151         $v->question_id = $question->id;
152         $v->answer_id   = $answer->id;
153         $v->vote        = $vote;
154         $v->created     = common_sql_now();
155
156         common_log(LOG_DEBUG, "Saving vote: $v->id $v->vote");
157
158         $v->insert();
159     }
160 }