]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/classes/QnA_Vote.php
Plugins with classes that extend Managed_DataObject get better code reuse
[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 compound key
60      *
61      * This is a utility method to get a single instance with a given set of
62      * key-value pairs. Usually used for the primary key for a compound key; thus
63      * the name.
64      *
65      * @param array $kv array of key-value mappings
66      *
67      * @return QnA_Vote object found, or null for no hits
68      *
69      */
70     function pkeyGet($kv)
71     {
72         return Memcached_DataObject::pkeyGet('QnA_Vote', $kv);
73     }
74
75     /**
76      * The One True Thingy that must be defined and declared.
77      */
78     public static function schemaDef()
79     {
80         return array(
81             'description' => 'For storing votes on questions and answers',
82             'fields' => array(
83                 'id' => array(
84                     'type'        => 'char',
85                     'length'      => 36,
86                     'not null'    => true,
87                     'description' => 'UUID of the vote'
88                 ),
89                 'question_id' => array(
90                     'type'        => 'char',
91                     'length'      => 36,
92                     'not null'    => true,
93                     'description' => 'UUID of question being voted on'
94                 ),
95                 'answer_id' => array(
96                     'type'        => 'char',
97                     'length'      => 36,
98                     'not null'    => true,
99                     'description' => 'UUID of answer being voted on'
100                 ),
101                 'vote'       => array('type' => 'int', 'size' => 'tiny'),
102                 'profile_id' => array('type' => 'int'),
103                 'created'    => array('type' => 'datetime', 'not null' => true),
104             ),
105             'primary key' => array('id'),
106             'indexes' => array(
107                 'profile_id_question_Id_index' => array(
108                     'profile_id',
109                     'question_id'
110                 ),
111                 'profile_id_question_Id_index' => array(
112                     'profile_id',
113                     'answer_id'
114                 )
115             )
116         );
117     }
118
119     /**
120      * Save a vote on a question or answer
121      *
122      * @param Profile  $profile
123      * @param QnA_Question the question being voted on
124      * @param QnA_Answer   the answer being voted on
125      * @param vote
126      * @param array
127      *
128      * @return Void
129      */
130     static function save($profile, $question, $answer, $vote)
131     {
132         $v = new QnA_Vote();
133         $v->id          = UUID::gen();
134         $v->profile_id  = $profile->id;
135         $v->question_id = $question->id;
136         $v->answer_id   = $answer->id;
137         $v->vote        = $vote;
138         $v->created     = common_sql_now();
139
140         common_log(LOG_DEBUG, "Saving vote: $v->id $v->vote");
141
142         $v->insert();
143     }
144 }