PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/system/application/models/link_model.php

https://bitbucket.org/zhemel/cloudengine
PHP | 247 lines | 130 code | 25 blank | 92 comment | 14 complexity | 6c6957376d198a51f12b171364822c1a 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 links on clouds
  4. *
  5. * @copyright 2009, 2010 The Open University. See CREDITS.txt
  6. * @license http://gnu.org/licenses/gpl-2.0.html GNU GPL v2
  7. * @package Links
  8. */
  9. class Link_model extends Model {
  10. function Link_model() {
  11. parent::Model();
  12. }
  13. /**
  14. * Add a new link to a cloud
  15. *
  16. * @param integer $cloud_id THE ID of the cloud
  17. * @param string $url The URL of the link
  18. * @param string $title The link title
  19. * @param integer $user_id The ID of the user adding the link
  20. * @param boolean $moderate TRUE if the link needs moderation, FALSE otherwise
  21. * @return integer The ID of the new link
  22. */
  23. function add_link($cloud_id, $url, $title, $user_id, $moderate) {
  24. $link->cloud_id = $cloud_id;
  25. $link->url = $url;
  26. $link->title = $title;
  27. $link->user_id = $user_id;
  28. $link->timestamp = time();
  29. $link->moderate = $moderate;
  30. $link->type = $this->get_link_type($url);
  31. $this->db->insert('cloud_link', $link);
  32. $link_id = $this->db->insert_id();
  33. if (!$moderate) {
  34. $this->approve_link($link_id);
  35. }
  36. $this->CI=& get_instance();
  37. $this->CI->load->model('cloud_model');
  38. $this->CI->cloud_model->update_in_search_index($cloud_id);
  39. return $link_id;
  40. }
  41. /**
  42. * Determine if a URL has already been added as a link for a cloud
  43. *
  44. * @param integer $cloud_id The ID of the cloud
  45. * @param integer $url The link URL
  46. * @return boolean TRUE if duplicate, FALSE otherwise
  47. */
  48. function is_duplicate($cloud_id, $url) {
  49. $duplicate = FALSE;
  50. $this->db->where('url', $url);
  51. $this->db->where('cloud_id', $cloud_id);
  52. $query = $this->db->get('cloud_link');
  53. if ($query->num_rows() > 0) {
  54. $duplicate = TRUE;
  55. }
  56. return $duplicate;
  57. }
  58. /**
  59. * Update a link
  60. *
  61. * @param integer $link_id The ID of the link
  62. * @param string $url The URL of the link
  63. * @param string $title The link title
  64. */
  65. function update_link($link_id, $url, $title) {
  66. $link->url = $url;
  67. $link->title = $title;
  68. $link->type = $this->get_link_type($url);
  69. $this->db->update('cloud_link', $link, array('link_id'=>$link_id));
  70. }
  71. /**
  72. * Determine if a specified user has permission to edit a specified link
  73. *
  74. * @param integer $user_id The ID of the user
  75. * @param integer $link_id The ID of the link
  76. * @return boolean TRUE if they have permission, FALSE otherwise
  77. */
  78. function has_edit_permission($user_id, $link_id) {
  79. $permission = FALSE;
  80. if ($user_id) {
  81. $link = $this->get_link($link_id);
  82. if ($user_id == $link->user_id || $this->auth_lib->is_admin()) {
  83. $permission = TRUE;
  84. }
  85. }
  86. return $permission;
  87. }
  88. /**
  89. * Display an error page if specified user does not have permission to edit the
  90. * specified link
  91. *
  92. * @param integer $user_id The ID of the user
  93. * @param integer $link_id The ID of the link
  94. */
  95. function check_edit_permission($user_id, $link_id) {
  96. if (!$this->has_edit_permission($user_id, $link_id)) {
  97. show_error(t("You do not have edit permission for that link."));
  98. }
  99. }
  100. /**
  101. * Determine the type of link (e.g. if it is a cloud, cloudscape or 'external' (i.e.
  102. * neither cloud nor cloudscape) from the URL
  103. *
  104. * @param string $url The link URL
  105. * @return string The type of link - either 'external', 'cloud' or 'cloudscape'
  106. */
  107. function get_link_type($url) {
  108. $type = 'external';
  109. if (strpos($url, base_url().'cloud/view') === 0) {
  110. $type = 'cloud';
  111. }
  112. if (strpos($url, base_url().'cloudscape/view') === 0) {
  113. $type = 'cloudscape';
  114. }
  115. return $type;
  116. }
  117. /**
  118. * Remove a link
  119. *
  120. * @param integer $link_id The ID of the link
  121. */
  122. function delete_link($link_id) {
  123. $this->db->delete('cloud_link', array('link_id' => $link_id));
  124. $this->load->model('event_model');
  125. $event_model = new event_model();
  126. $event_model->delete_events('link', $link_id);
  127. }
  128. /**
  129. * Approve a link
  130. *
  131. * @param integer $link_id The ID of the link
  132. */
  133. function approve_link($link_id) {
  134. $this->db->where('link_id', $link_id);
  135. $this->db->update('cloud_link', array('moderate'=>0));
  136. $link = $this->get_link($link_id);
  137. $this->load->model('event_model');
  138. $event_model = new event_model();
  139. $event_model->add_event('cloud', $link->cloud_id, 'link', $link_id);
  140. $event_model->add_event('user', $link->user_id, 'link', $link_id);
  141. }
  142. /**
  143. * Get the links for a specific cloud
  144. *
  145. * @param integer $cloud_id The ID of the cloud
  146. * @return array Array of links
  147. */
  148. function get_links($cloud_id) {
  149. $cloud_id = (int) $cloud_id;
  150. if (is_int($cloud_id)) {
  151. $query = $this->db->query("SELECT l.url, l.title, count(f.item_id) AS total,
  152. l.link_id, l.type, up.id AS user_id, up.fullname,
  153. l.timestamp FROM cloud_link l
  154. LEFT OUTER JOIN favourite f ON f.item_id = l.link_id
  155. AND f.item_type = 'link'
  156. INNER JOIN user_profile up ON up.id = l.user_id
  157. INNER JOIN user u on u.id = up.id
  158. WHERE l.cloud_id = $cloud_id
  159. AND u.banned = 0
  160. GROUP BY l.link_id ORDER BY total DESC, timestamp ASC");
  161. return $query->result();
  162. }
  163. }
  164. /**
  165. * Get a link
  166. *
  167. * @param integer $link_id The ID of the link
  168. * @return object The details of the link
  169. */
  170. function get_link($link_id) {
  171. $this->db->where('link_id', $link_id);
  172. $this->db->where('user.banned',0);
  173. $this->db->join('user', 'user.id = cloud_link.user_id');
  174. $this->db->join('user_profile', 'user_profile.id = cloud_link.user_id');
  175. $query = $this->db->get('cloud_link');
  176. if ($query->num_rows() != 0 ) {
  177. $link = $query->row();
  178. }
  179. return $link;
  180. }
  181. /**
  182. * Get links requiring moderation
  183. *
  184. * @return array Array of links
  185. */
  186. function get_links_for_moderation() {
  187. $this->db->where('cloud_link.moderate', 1);
  188. $this->db->join('user_profile', 'user_profile.id = cloud_link.user_id');
  189. $this->db->order_by('timestamp', 'asc');
  190. $query = $this->db->get('cloud_link');
  191. return $query->result();
  192. }
  193. /**
  194. * Make a link the primary link for the cloud
  195. *
  196. * @param integer $cloud_id The ID of the cloud
  197. * @param integer $link_id The ID of the link
  198. */
  199. function make_link_primary($cloud_id, $link_id) {
  200. $link = $this->get_link($link_id);
  201. $this->db->where('cloud_id', $cloud_id);
  202. $this->db->update('cloud', array('primary_url'=>$link->url));
  203. $this->delete_link($link_id);
  204. }
  205. /**
  206. * Determine if a user is the owner of a link
  207. *
  208. * @param integer $link_id The ID of the link
  209. * @param integer $user_id The ID of the user
  210. * @return boolean TRUE if the user is the owner, FALSE otherwise
  211. */
  212. function is_owner($link_id, $user_id) {
  213. $owner = FALSE;
  214. $this->db->where('link_id', $link_id);
  215. $this->db->where('user_id', $user_id);
  216. $query = $this->db->get('cloud_link');
  217. if ($query->num_rows() > 0) {
  218. $owner = TRUE;
  219. }
  220. return $owner;
  221. }
  222. }