QnA_Vote.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * For storing votes on question and answers
  34. *
  35. * @category QnA
  36. * @package StatusNet
  37. * @author Zach Copley <zach@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  39. * @link http://status.net/
  40. *
  41. * @see DB_DataObject
  42. */
  43. class QnA_Vote extends Managed_DataObject
  44. {
  45. const UP = 'http://activitystrea.ms/schema/1.0/like';
  46. const DOWN = 'http://activityschema.org/object/dislike'; // Gar!
  47. public $__table = 'qna_vote'; // table name
  48. public $id; // char(36) primary key not null -> UUID
  49. public $question_id; // char(36) -> question.id UUID
  50. public $answer_id; // char(36) -> question.id UUID
  51. public $type; // tinyint -> vote: up (1) or down (-1)
  52. public $profile_id; // int -> question.id
  53. public $created; // datetime
  54. /**
  55. * The One True Thingy that must be defined and declared.
  56. */
  57. public static function schemaDef()
  58. {
  59. return array(
  60. 'description' => 'For storing votes on questions and answers',
  61. 'fields' => array(
  62. 'id' => array(
  63. 'type' => 'char',
  64. 'length' => 36,
  65. 'not null' => true,
  66. 'description' => 'UUID of the vote'
  67. ),
  68. 'question_id' => array(
  69. 'type' => 'char',
  70. 'length' => 36,
  71. 'not null' => true,
  72. 'description' => 'UUID of question being voted on'
  73. ),
  74. 'answer_id' => array(
  75. 'type' => 'char',
  76. 'length' => 36,
  77. 'not null' => true,
  78. 'description' => 'UUID of answer being voted on'
  79. ),
  80. 'vote' => array('type' => 'int', 'size' => 'tiny'),
  81. 'profile_id' => array('type' => 'int'),
  82. 'created' => array('type' => 'datetime', 'not null' => true),
  83. ),
  84. 'primary key' => array('id'),
  85. 'indexes' => array(
  86. 'profile_id_question_Id_index' => array(
  87. 'profile_id',
  88. 'question_id'
  89. ),
  90. 'profile_id_question_Id_index' => array(
  91. 'profile_id',
  92. 'answer_id'
  93. )
  94. )
  95. );
  96. }
  97. /**
  98. * Save a vote on a question or answer
  99. *
  100. * @param Profile $profile
  101. * @param QnA_Question the question being voted on
  102. * @param QnA_Answer the answer being voted on
  103. * @param vote
  104. * @param array
  105. *
  106. * @return Void
  107. */
  108. static function save($profile, $question, $answer, $vote)
  109. {
  110. $v = new QnA_Vote();
  111. $v->id = UUID::gen();
  112. $v->profile_id = $profile->id;
  113. $v->question_id = $question->id;
  114. $v->answer_id = $answer->id;
  115. $v->vote = $vote;
  116. $v->created = common_sql_now();
  117. common_log(LOG_DEBUG, "Saving vote: $v->id $v->vote");
  118. $v->insert();
  119. }
  120. }