QnA_Vote.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Data class to save users votes for
  18. *
  19. * @category QnA
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * For storing votes on question and answers
  28. *
  29. * @copyright 2011 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. *
  32. * @see DB_DataObject
  33. */
  34. class QnA_Vote extends Managed_DataObject
  35. {
  36. const UP = 'http://activitystrea.ms/schema/1.0/like';
  37. const DOWN = 'http://activityschema.org/object/dislike'; // Gar!
  38. public $__table = 'qna_vote'; // table name
  39. public $id; // char(36) primary key not null -> UUID
  40. public $question_id; // char(36) -> question.id UUID
  41. public $answer_id; // char(36) -> question.id UUID
  42. public $type; // tinyint -> vote: up (1) or down (-1)
  43. public $profile_id; // int -> question.id
  44. public $created; // datetime
  45. /**
  46. * The One True Thingy that must be defined and declared.
  47. */
  48. public static function schemaDef()
  49. {
  50. return array(
  51. 'description' => 'For storing votes on questions and answers',
  52. 'fields' => array(
  53. 'id' => array(
  54. 'type' => 'char',
  55. 'length' => 36,
  56. 'not null' => true,
  57. 'description' => 'UUID of the vote'
  58. ),
  59. 'question_id' => array(
  60. 'type' => 'char',
  61. 'length' => 36,
  62. 'not null' => true,
  63. 'description' => 'UUID of question being voted on'
  64. ),
  65. 'answer_id' => array(
  66. 'type' => 'char',
  67. 'length' => 36,
  68. 'not null' => true,
  69. 'description' => 'UUID of answer being voted on'
  70. ),
  71. 'vote' => array('type' => 'int', 'size' => 'tiny'),
  72. 'profile_id' => array('type' => 'int'),
  73. 'created' => array('type' => 'datetime', 'not null' => true),
  74. ),
  75. 'primary key' => array('id'),
  76. 'indexes' => array(
  77. 'qna_vote_profile_id_question_id_idx' => array(
  78. 'profile_id',
  79. 'question_id'
  80. ),
  81. 'qna_vote_profile_id_question_id_idx' => array(
  82. 'profile_id',
  83. 'answer_id'
  84. )
  85. )
  86. );
  87. }
  88. /**
  89. * Save a vote on a question or answer
  90. *
  91. * @param Profile $profile
  92. * @param QnA_Question the question being voted on
  93. * @param QnA_Answer the answer being voted on
  94. * @param vote
  95. * @param array
  96. *
  97. * @return Void
  98. */
  99. public static function save($profile, $question, $answer, $vote)
  100. {
  101. $v = new QnA_Vote();
  102. $v->id = UUID::gen();
  103. $v->profile_id = $profile->id;
  104. $v->question_id = $question->id;
  105. $v->answer_id = $answer->id;
  106. $v->vote = $vote;
  107. $v->created = common_sql_now();
  108. common_log(LOG_DEBUG, "Saving vote: $v->id $v->vote");
  109. $v->insert();
  110. }
  111. }