PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/models/favourite_model.php

https://bitbucket.org/zhemel/cloudengine
PHP | 285 lines | 176 code | 21 blank | 88 comment | 15 complexity | 4d78f0ad72f90f5b3596efcd5a5ec440 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Model file for functions related to favourites
  4. * @copyright 2009, 2010 The Open University. See CREDITS.txt
  5. * @license http://gnu.org/licenses/gpl-2.0.html GNU GPL v2
  6. * @package Favourites
  7. */
  8. class Favourite_model extends Model {
  9. function Favourite_model() {
  10. parent::Model();
  11. }
  12. /**
  13. * Add a favourite for an item by the specified user
  14. *
  15. * @param integer $user_id The ID of the user
  16. * @param integer $item_id The ID of the item
  17. * @param string $item_type e.g. 'cloud', 'cloudscape' or 'link'
  18. */
  19. function add_favourite($user_id, $item_id, $item_type) {
  20. if ($user_id && $item_id && $this->can_favourite_item($user_id, $item_id, $item_type)) {
  21. $this->db->insert('favourite', array('user_id' => $user_id,
  22. 'item_id' => $item_id,
  23. 'item_type' => $item_type,
  24. 'timestamp'=>time()));
  25. }
  26. }
  27. /**
  28. * Remove a favourite for an item and specifed user
  29. *
  30. * @param integer $user_id The ID of the user
  31. * @param integer $item_id The ID of the item
  32. * @param string $item_type e.g. 'cloud', 'cloudscape' or 'link'
  33. */
  34. function remove_favourite($user_id, $item_id, $item_type) {
  35. $this->db->delete('favourite', array('user_id' => $user_id, 'item_id' => $item_id,
  36. 'item_type' => $item_type));
  37. }
  38. /**
  39. * Determine if a user has favourited a specific item
  40. *
  41. * @param integer $user_id The ID of the user
  42. * @param integer $item_id The ID of the item
  43. * @param string $item_type e.g. 'cloud', 'cloudscape' or 'link'
  44. * @return boolean TRUE if they have favourited it, FALSE otherwise
  45. */
  46. function is_favourite($user_id, $item_id, $item_type) {
  47. $favourite = true;
  48. $this->db->where('user_id', $user_id);
  49. $this->db->where('item_id', $item_id);
  50. $this->db->where('item_type', $item_type);
  51. $query = $this->db->get('favourite');
  52. if ($query->num_rows() == 0) {
  53. $favourite = false;
  54. }
  55. return $favourite;
  56. }
  57. /**
  58. * Get the number of favourites for a specified item
  59. *
  60. * @param integer $item_id The ID of the item
  61. * @param integer The number of favourites
  62. */
  63. function get_total_favourites($item_id, $item_type) {
  64. $this->db->where('item_id', $item_id);
  65. $this->db->where('item_type', $item_type);
  66. $this->db->where('user.banned',0);
  67. $this->db->join('user', 'favourite.user_id = user.id');
  68. $query = $this->db->get('favourite');
  69. return $query->num_rows();
  70. }
  71. /**
  72. * Get the reputation for a user - this is equal to the total number of favourites on a
  73. * user's clouds, cloudscapes and links
  74. *
  75. * @param integer $user_id The ID of the user
  76. * @return integer The total number of votes
  77. */
  78. function get_reputation($user_id) {
  79. $user_id = (int) $user_id;
  80. $reputation = 0;
  81. // Get all the cloud favourites
  82. $query = $this->db->query("SELECT * FROM cloud c INNER JOIN favourite f
  83. ON f.item_id = c.cloud_id
  84. WHERE c.user_id = $user_id AND f.item_type = 'cloud'");
  85. $reputation += $query->num_rows();
  86. // Get all the cloudscape favourites
  87. $query = $this->db->query("SELECT * FROM cloudscape c INNER JOIN favourite f
  88. ON f.item_id = c.cloudscape_id
  89. WHERE c.user_id = $user_id
  90. AND f.item_type = 'cloudscape'");
  91. $reputation += $query->num_rows();
  92. // Get all the link favourites
  93. $query = $this->db->query("SELECT * FROM cloud_link l INNER JOIN favourite f
  94. ON f.item_id = l.link_id
  95. WHERE l.user_id = $user_id AND f.item_type = 'link'");
  96. $reputation += $query->num_rows();
  97. return $reputation;
  98. }
  99. /**
  100. * Determine if the user in question can vote i.e. if they are generally allowed to vote
  101. *
  102. * @param integer $user_id The ID of the user
  103. * @return boolean TRUE if they can vote, FALSE otherwise
  104. */
  105. function can_favourite($user_id) {
  106. $can_favourite = false;
  107. // Only allow users with a certain number of votes to vote themselves
  108. if ($this->get_reputation($user_id) > -1) {
  109. $can_favourite = true;
  110. }
  111. $this->CI = & get_instance();
  112. if ($this->CI->auth_lib->is_admin()) {
  113. $can_favourite = true;
  114. }
  115. return $can_favourite;
  116. }
  117. /**
  118. * Determine if the user in question is allowed to vote on this particular item
  119. *
  120. * @param integer $user_id The ID of the user
  121. * @param integer $item_id The ID of the item
  122. * @param string $item_type e.g. 'cloud', 'cloudscape' or 'link'
  123. */
  124. function can_favourite_item($user_id, $item_id, $item_type) {
  125. $can_favourite= true;
  126. // Check if reputation high enough to add favourites at all
  127. if (!$this->can_favourite($user_id)) {
  128. $can_favourite = false;
  129. }
  130. // If the user has already voted on this item, they can't vote again
  131. if ($this->is_favourite($user_id, $item_id, $item_type)) {
  132. $can_favourite = false;
  133. }
  134. if ($this->is_owner($user_id, $item_id, $item_type)) {
  135. $can_favourite = false;
  136. }
  137. return $can_favourite;
  138. }
  139. /**
  140. * Determine if a user is the owner of a particular item
  141. *
  142. * @param integer $user_id The ID of the user
  143. * @param integer $item_id The ID of the item
  144. * @param string $item_type e.g. 'cloud', 'cloudscape' or 'link'
  145. */
  146. function is_owner($user_id, $item_id, $item_type) {
  147. $is_owner = false;
  148. $this->CI = & get_instance();
  149. switch ($item_type) {
  150. case 'cloud':
  151. $this->CI->load->model('cloud_model');
  152. if ($this->CI->cloud_model->is_owner($item_id, $user_id)) {
  153. $is_owner = true;
  154. }
  155. break;
  156. case 'cloudscape':
  157. $this->CI->load->model('cloudscape_model');
  158. if ($this->CI->cloudscape_model->is_owner($item_id, $user_id)) {
  159. $is_owner = true;
  160. }
  161. break;
  162. case 'link':
  163. $this->CI->load->model('link_model');
  164. if ($this->CI->link_model->is_owner($item_id, $user_id)) {
  165. $is_owner = true;
  166. }
  167. break;
  168. }
  169. return $is_owner;
  170. }
  171. /**
  172. * Get the items of a particular type that a user has favourited
  173. *
  174. * @param integer $user_id The ID of the user
  175. * @param string $item_type e.g. 'cloud', 'cloudscape' or 'link'
  176. * @return array Array of items
  177. */
  178. function get_favourites($user_id, $item_type=NULL) {
  179. $user_id = (int) $user_id;
  180. switch ($item_type) {
  181. case 'cloud':
  182. $this->db->order_by('favourite.timestamp', 'desc');
  183. $this->db->where('favourite.user_id', $user_id);
  184. $this->db->where('favourite.item_type', 'cloud');
  185. $this->db->join('cloud', 'cloud.cloud_id = favourite.item_id');
  186. $query = $this->db->get('favourite');
  187. $items = $query->result();
  188. break;
  189. case 'cloudscape':
  190. $this->db->order_by('favourite.timestamp', 'desc');
  191. $this->db->where('favourite.user_id', $user_id);
  192. $this->db->where('favourite.item_type', 'cloudscape');
  193. $this->db->join('cloudscape', 'cloudscape.cloudscape_id = favourite.item_id');
  194. $query = $this->db->get('favourite');
  195. $items = $query->result();
  196. break;
  197. case NULL: //Recommended links are in the 'favourite' table - we don't want them.
  198. $query = $this->db->query("
  199. SELECT c.user_id,item_id,item_type,timestamp,created,title,summary
  200. FROM favourite AS f
  201. JOIN cloudscape AS c ON f.item_id = c.cloudscape_id
  202. WHERE f.user_id = $user_id
  203. AND (f.item_type = 'cloudscape')
  204. UNION
  205. SELECT f.user_id,item_id,item_type,timestamp,created,title,summary
  206. FROM favourite AS f
  207. JOIN cloud AS c ON f.item_id = c.cloud_id
  208. WHERE f.user_id = $user_id
  209. AND (f.item_type = 'cloud')
  210. ORDER BY timestamp");
  211. $items = $query->result();
  212. break;
  213. }
  214. return $items;
  215. }
  216. /**
  217. * Get a list of the users who have favourited a particular item
  218. *
  219. * @param integer $user_id The ID of the user
  220. * @param string $item_type e.g. 'cloud', 'cloudscape' or 'link'
  221. * @return array Array of users
  222. */
  223. function get_users_favourited($item_id, $item_type) {
  224. $this->db->order_by('favourite.timestamp', 'desc');
  225. $this->db->where('item_id', $item_id);
  226. $this->db->where('item_type', $item_type);
  227. $this->db->where('user.banned',0);
  228. $this->db->join('user', 'favourite.user_id = user.id');
  229. $this->db->join('user_picture', 'favourite.user_id = user_picture.user_id', 'left');
  230. $this->db->join('user_profile', 'user_profile.id = favourite.user_id');
  231. $query = $this->db->get('favourite');
  232. return $query->result();
  233. }
  234. /**
  235. * Get the most favourited items of a particular type
  236. *
  237. * @param string $item_type e.g. 'cloud', 'cloudscape' or 'link'
  238. * @param integer $num Number of items to get
  239. * @return array Array of items
  240. */
  241. function get_popular($item_type, $num) {
  242. $num = (int) $num;
  243. switch($item_type) {
  244. case 'cloud':
  245. $query = $this->db->query("SELECT c.cloud_id AS item_id, c.title,
  246. COUNT(*) AS total_favourites
  247. FROM cloud c INNER JOIN favourite f
  248. ON f.item_id = c.cloud_id
  249. WHERE item_type = 'cloud'
  250. GROUP BY c.cloud_id
  251. ORDER BY total_favourites DESC LIMIT $num");
  252. break;
  253. case 'cloudscape':
  254. $query = $this->db->query("SELECT c.cloudscape_id AS item_id, c.title,
  255. COUNT(*) AS total_favourites
  256. FROM cloudscape c INNER JOIN favourite f
  257. ON f.item_id = c.cloudscape_id
  258. WHERE item_type = 'cloudscape'
  259. GROUP BY c.cloudscape_id
  260. ORDER BY total_favourites DESC LIMIT $num");
  261. }
  262. return $query->result();
  263. }
  264. }