/system/application/models/event_model.php

https://bitbucket.org/zhemel/cloudengine · PHP · 802 lines · 638 code · 59 blank · 105 comment · 54 complexity · ee552cfe6b39118a282a7428f5b860bb MD5 · raw file

  1. <?php
  2. /**
  3. * Model file for functions related to events in cloudstreams
  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 Cloudstream
  7. */
  8. class Event_model extends Model {
  9. function Event_model() {
  10. parent::Model();
  11. }
  12. /**
  13. * Add a cloudstream event
  14. *
  15. * @param string $follow_item_type The item type e.g. 'cloud', 'cloudscape', 'user'
  16. * @param integer $follow_item_id The ID of the
  17. * @param string $event_type The event type e.g. 'newcloud', 'newcloudscape',
  18. * 'newcomment', 'newfollow'
  19. * @param integer $event_item_id The ID of the item that the event pertains to
  20. * @param integer $timestamp The time of the event as a Unix timestamp
  21. * @return integer The ID of the event
  22. */
  23. function add_event($follow_item_type, $follow_item_id, $event_type, $event_item_id,
  24. $timestamp = false) {
  25. $event->follow_item_type = $follow_item_type;
  26. $event->follow_item_id = $follow_item_id;
  27. $event->event_type = $event_type;
  28. $event->event_item_id = $event_item_id;
  29. $event->timestamp = $timestamp;
  30. if (!$timestamp) {
  31. $event->timestamp = time();
  32. }
  33. $this->db->insert('event', $event);
  34. $event_id = $this->db->insert_id();
  35. return $event_id;
  36. }
  37. /**
  38. * Delete all events associated with a particular item
  39. *
  40. * @param string $item_type The item type e.g. 'cloud', 'cloudscape', 'user'
  41. * @param integer $item_id The ID of the item
  42. */
  43. function delete_events($item_type, $item_id) {
  44. switch($item_type) {
  45. case 'cloud':
  46. $this->CI = &get_instance();
  47. $this->CI->load->model('comment_model');
  48. $comments = $this->CI->comment_model->get_comments($item_id);
  49. foreach ($comments as $comment) {
  50. $this->db->delete('event',
  51. array('event_item_id' => $comment->comment_id,
  52. 'event_type' => 'comment'));
  53. }
  54. $this->db->delete('event', array('event_item_id' => $item_id,
  55. 'event_type' => 'cloud'));
  56. $this->db->delete('event', array('follow_item_id' => $item_id,
  57. 'follow_item_type' => 'cloud'));
  58. break;
  59. case 'cloudscape':
  60. $this->db->delete('event', array('event_item_id' => $item_id,
  61. 'event_type' => 'cloudscape'));
  62. $this->db->delete('event', array('follow_item_id' => $item_id,
  63. 'follow_item_type' => 'cloudscape'));
  64. break;
  65. case 'comment':
  66. $this->db->delete('event', array('event_item_id' => $item_id,
  67. 'event_type' => 'comment'));
  68. break;
  69. case 'user':
  70. $this->db->delete('event', array('follow_item_id' => $item_id,
  71. 'follow_item_type' => 'user'));
  72. break;
  73. case 'news':
  74. $this->db->delete('event', array('event_item_id' => $item_id,
  75. 'event_type' => 'news'));
  76. break;
  77. case 'news_comment':
  78. $this->db->delete('event', array('event_item_id' => $item_id,
  79. 'event_type' => 'news_comment'));
  80. break;
  81. case 'link':
  82. $this->db->delete('event', array('event_item_id' => $item_id,
  83. 'event_type' => 'link'));
  84. break;
  85. case 'reference':
  86. $this->db->delete('event', array('event_item_id' => $item_id,
  87. 'event_type' => 'reference'));
  88. break;
  89. case 'content':
  90. $this->db->delete('event', array('event_item_id'=>$item_id,
  91. 'event_type' => 'content'));
  92. break;
  93. case 'embed':
  94. $this->db->delete('event', array('event_item_id'=>$item_id,
  95. 'event_type' => 'embed'));
  96. break;
  97. }
  98. }
  99. /**
  100. * Get all events for the site's cloudstream
  101. *
  102. * @param integer $num Limit on number of events to get
  103. * @return array Array of events
  104. */
  105. function get_all($num = 20) {
  106. $query = $this->db->query("SELECT * FROM event WHERE event_type <> 'follow'
  107. AND event_type <> 'new_user'
  108. AND event_type <> 'login_attempt'
  109. AND event_type <> 'profile_edit'
  110. AND omit_from_site_cloudstream = 0
  111. ORDER BY timestamp DESC LIMIT $num");
  112. return $query->result();
  113. }
  114. /**
  115. * Get all events of a particular type for the site's cloudstream.
  116. *
  117. * @param string $type The event type e.g. 'newcloud', 'newcloudscape',
  118. * 'newcomment', 'newfollow'
  119. * @param integer $num Limit on number of events to get
  120. * @return array Array of events
  121. */
  122. function get_all_type($type, $num = 20) {
  123. $where = "";
  124. switch($type) {
  125. case 'cloud':
  126. $where = "event_type = 'cloud'";
  127. break;
  128. case 'cloudscape':
  129. $where = "event_type = 'cloudscape'";
  130. break;
  131. case 'comment':
  132. $where = "event_type = 'comment' OR event_type = 'news_comment'";
  133. break;
  134. case 'link':
  135. $where = "event_type = 'link'";
  136. break;
  137. case 'reference':
  138. $where = "event_type = 'reference'";
  139. break;
  140. case 'content':
  141. $where = "event_type = 'content' OR event_type = 'embed'";
  142. break;
  143. }
  144. $query = $this->db->query("SELECT * FROM event WHERE $where
  145. AND omit_from_site_cloudstream = 0
  146. ORDER BY timestamp DESC LIMIT $num");
  147. return $query->result();
  148. }
  149. /**
  150. * Get the events in a user's cloudstream i.e. for items that they are following
  151. *
  152. * @param integer $user_id The ID of the user
  153. * @param integer $num Limit on number of events to get
  154. * @return array Array of events
  155. */
  156. function get_events_for_following($user_id, $num, $type = '') {
  157. $where = "";
  158. switch($type) {
  159. case 'cloud': $where = "AND event_type = 'cloud'";
  160. break;
  161. case 'cloudscape': $where = "AND event_type = 'cloudscape'";
  162. break;
  163. case 'comment': $where = "AND (event_type = 'comment'
  164. OR event_type = 'news_comment')";
  165. break;
  166. case 'link': $where = "AND event_type = 'link'"; break;
  167. case 'reference': $where = "AND event_type = 'reference'"; break;
  168. case 'content': $where = "AND (event_type = 'content'
  169. OR event_type = 'embed')"; break;
  170. }
  171. $this->CI = &get_instance();
  172. $this->CI->load->model('user_model');
  173. // Get the cloudscapes followed by this user and get the related events
  174. $cloudscapes = $this->CI->user_model->get_following_cloudscapes($user_id);
  175. $cloudscape_where = "1=0";
  176. if ($cloudscapes) {
  177. foreach($cloudscapes as $cloudscape) {
  178. $cloudscape_ids[] = $cloudscape->cloudscape_id;
  179. }
  180. $cloudscape_where = "follow_item_type = 'cloudscape' AND follow_item_id
  181. IN (".implode(',', $cloudscape_ids).")";
  182. $cloud_query = "UNION SELECT e.* FROM event e INNER JOIN cloudscape_cloud c
  183. ON e.follow_item_id = c.cloud_id
  184. WHERE (e.follow_item_type = 'cloud' AND c.cloudscape_id
  185. IN (".implode(',', $cloudscape_ids).")) $where";
  186. }
  187. // Get the users followed by this user and the related events
  188. $users = $this->CI->user_model->get_following($user_id);
  189. $user_where = "1=0";
  190. if ($users) {
  191. foreach($users as $user) {
  192. $user_ids[] = $user->followed_user_id;
  193. }
  194. $user_where = "follow_item_type = 'user' AND follow_item_id
  195. IN (".implode(',', $user_ids).")";
  196. }
  197. // Get the clouds followed by this user and get the related events
  198. $clouds = $this->CI->user_model->get_following_clouds($user_id);
  199. $cloud_where = "1=0";
  200. if ($clouds) {
  201. foreach($clouds as $cloud) {
  202. $cloud_ids[] = $cloud->cloud_id;
  203. }
  204. $cloud_query2 = "UNION SELECT e.* FROM event e
  205. WHERE (e.follow_item_type = 'cloud' AND e.follow_item_id
  206. IN (".implode(',', $cloud_ids).")) $where";
  207. }
  208. // Combine the two queries
  209. $query = $this->db->query("SELECT * FROM event WHERE (($cloudscape_where)
  210. OR ($user_where)) $where
  211. $cloud_query $cloud_query2 ORDER BY timestamp DESC LIMIT $num");
  212. return $query->result();
  213. }
  214. /**
  215. * Get all events in the admin cloudstream
  216. *
  217. * @param integer $num Limit on number of events to get
  218. * @return array Array of events
  219. */
  220. function get_events_for_admin($num = 20) {
  221. $query = $this->db->query("SELECT * FROM event ORDER BY timestamp DESC LIMIT $num");
  222. return $query->result();
  223. }
  224. /**
  225. * Get all the events in a user's cloudstream i.e. events for actions by the user
  226. *
  227. * @param integer $user_id The ID of the user
  228. * @param integer $num Limit on number of events to get
  229. * @param string $type Filter by a particular event type e.g. 'newcloud',
  230. * 'newcloudscape',
  231. * 'newcomment', 'newfollow'. , If empty, show all events
  232. * @return array Array of events
  233. */
  234. function get_events_for_user($user_id, $num = 50, $type='') {
  235. $where = "";
  236. switch($type) {
  237. case 'cloud':
  238. $where = "AND event_type = 'cloud'";
  239. break;
  240. case 'cloudscape':
  241. $where = "AND event_type = 'cloudscape'";
  242. break;
  243. case 'comment':
  244. $where = "AND (event_type = 'comment' OR event_type = 'news_comment')";
  245. break;
  246. case 'link':
  247. $where = "AND event_type = 'link'";
  248. break;
  249. case 'reference':
  250. $where = "AND event_type = 'reference'";
  251. break;
  252. case 'content':
  253. $where = "AND (event_type = 'content' OR event_type = 'embed')";
  254. break;
  255. }
  256. if (is_numeric($user_id)) {
  257. $query = $this->db->query("SELECT * FROM event WHERE event_type <> 'follow'
  258. AND follow_item_type = 'user'
  259. AND follow_item_id = $user_id $where
  260. ORDER BY timestamp DESC LIMIT $num");
  261. return $query->result();
  262. } else {
  263. return false;
  264. }
  265. }
  266. /**
  267. * Get the events for a cloudscape's cloudstream
  268. *
  269. * @param integer $cloudscape_id The ID of the cloudscapte
  270. * @param integer $num Limit on number of events to get
  271. * @return array Array of events
  272. */
  273. function get_events_for_cloudscape($cloudscape_id, $num, $type = '') {
  274. $this->CI = &get_instance();
  275. $where = "";
  276. switch($type) {
  277. case 'cloud': $where = "AND event_type = 'cloud'"; break;
  278. case 'cloudscape': $where = "AND event_type = 'cloudscape'"; break;
  279. case 'comment': $where = "AND (event_type = 'comment'
  280. OR event_type = 'news_comment')"; break;
  281. case 'link': $where = "AND event_type = 'link'"; break;
  282. case 'reference': $where = "AND event_type = 'reference'"; break;
  283. case 'content': $where = "AND (event_type = 'content' OR event_type = 'embed')";
  284. break;
  285. }
  286. $cloudscape_where = "follow_item_type = 'cloudscape'
  287. AND follow_item_id = $cloudscape_id";
  288. $cloud_query = "UNION SELECT e.* FROM event e INNER JOIN cloudscape_cloud c
  289. ON e.follow_item_id = c.cloud_id WHERE (e.follow_item_type = 'cloud'
  290. AND c.cloudscape_id = $cloudscape_id) $where";
  291. $query = $this->db->query("SELECT * FROM event WHERE ($cloudscape_where) $where
  292. $cloud_query ORDER BY timestamp DESC LIMIT $num");
  293. return $query->result();
  294. }
  295. function event_category($event_type) {
  296. $category = '';
  297. switch($event_type) {
  298. case 'cloud':
  299. $category = 'cloud';
  300. break;
  301. case 'cloudscape':
  302. $category = 'cloudscape';
  303. break;
  304. case 'comment':
  305. $category = 'comment';
  306. break;
  307. case 'news':
  308. $category = 'comment';
  309. break;
  310. case 'follow':
  311. $category = 'user';
  312. break;
  313. case 'news_comment':
  314. $category = 'comment';
  315. break;
  316. case 'new_user':
  317. $category = 'user';
  318. break;
  319. case 'link':
  320. $category = 'link';
  321. break;
  322. case 'reference':
  323. $category = 'reference';
  324. break;
  325. case 'content':
  326. $category = 'extra';
  327. break;
  328. case 'embed':
  329. $category = 'extra';
  330. break;
  331. case 'profile_edit':
  332. $category = 'user';
  333. break;
  334. case 'login_attempt':
  335. $category = 'user';
  336. break;
  337. }
  338. return $category;
  339. }
  340. /**
  341. * Turn an event into an HTML string that can be displayed in a cloudstream
  342. *
  343. * @param obejct $event Details of the event
  344. * @return string The HTML string
  345. */
  346. function to_string($event, $simple = false) {
  347. $this->load->helper('format');
  348. $this->CI = &get_instance();
  349. $attr_author = array('rel'=>'author'); // Commonly used HTML attributes.
  350. switch ($event->event_type) {
  351. case 'cloud':
  352. if ($event->follow_item_type == 'user') {
  353. // New cloud created by user
  354. $cloud_id = $event->event_item_id;
  355. $this->CI->load->model('cloud_model');
  356. $cloud = $this->CI->cloud_model->get_cloud($cloud_id);
  357. $string = anchor("cloud/view/$cloud_id", $cloud->title).'<br />';
  358. if ($simple) {
  359. /*@i18n: Simplified/modified logic. */
  360. ///Translators: Cloudstreams - short and long event messages.
  361. // Add 'rel' for semantics/styling, http://whatwg.org/specs/web-apps/current-work/multipage/links.html#linkTypes
  362. $string .= '<em>'.t("created by !person",
  363. array('!person' => anchor('user/view/'.$cloud->id,
  364. $cloud->fullname, $attr_author))).'</em>';
  365. } else {
  366. $string .= '<em>'.t("new cloud created by !person",
  367. array('!person' => anchor('user/view/'.$cloud->id,
  368. $cloud->fullname, $attr_author))).'</em>';
  369. }
  370. } elseif ($event->follow_item_type = 'cloudscape') {
  371. // Cloud added to a cloudscape
  372. $cloud_id = $event->event_item_id;
  373. $cloudscape_id = $event->follow_item_id;
  374. $this->CI->load->model('cloud_model');
  375. $cloud = $this->CI->cloud_model->get_cloud($cloud_id);
  376. if (!$cloud->user_id) {
  377. return false;
  378. }
  379. else {
  380. $this->CI->load->model('cloudscape_model');
  381. $cloudscape = $this->CI->cloudscape_model->get_cloudscape($cloudscape_id);
  382. $user = $this->CI->cloudscape_model->get_cloud_added_user($cloud_id,
  383. $cloudscape_id);
  384. $string = anchor('cloud/view/'.$cloud->cloud_id, $cloud->title).'<br />';
  385. if ($user) {
  386. $string .= '<em>'.t("cloud added to the cloudscape !title by !person", array(
  387. '!person'=> anchor('user/view/'.$user->user_id,
  388. $user->fullname, $attr_author),
  389. '!title' => anchor('cloudscape/view/'.
  390. $cloudscape->cloudscape_id,
  391. $cloudscape->title))).'</em>';
  392. } else {
  393. $string .= '<em>'.t("cloud added to the cloudscape !title",
  394. array('!title' =>
  395. anchor('cloudscape/view/'.
  396. $cloudscape->cloudscape_id,
  397. $cloudscape->title))).'</em>';
  398. }
  399. }
  400. }
  401. break;
  402. case 'cloudscape':
  403. // New cloudscape created
  404. $this->CI->load->model('cloudscape_model');
  405. $cloudscape_id = $event->event_item_id;
  406. $cloudscape = $this->CI->cloudscape_model->get_cloudscape($cloudscape_id);
  407. $string = anchor("cloudscape/view/$cloudscape_id", $cloudscape->title).
  408. '<br />';
  409. if ($simple) {
  410. $string .= '<em>'.t("created by !person",
  411. array('!person' =>
  412. anchor('user/view/'.$cloudscape->user_id,
  413. $cloudscape->fullname,
  414. $attr_author))).'</em>';
  415. } else {
  416. $string .= '<em>'.t("new cloudscape created by !person",
  417. array('!person' =>
  418. anchor('user/view/'.$cloudscape->user_id,
  419. $cloudscape->fullname,
  420. $attr_author))).'</em>';
  421. }
  422. break; // Always break last - defensive.
  423. case 'comment':
  424. // New comment on a cloud
  425. $this->CI->load->model('comment_model');
  426. $comment_id = $event->event_item_id;
  427. $comment = $this->CI->comment_model->get_comment($comment_id);
  428. if (!$comment) {
  429. return false;
  430. }
  431. else {
  432. $truncated_comment = truncate_content(strip_tags($comment->body));
  433. $string = '<strong>'.'"'.$truncated_comment.'</strong><br />';
  434. if ($simple) {
  435. $string .= '<em>'.t("added to !cloud by !person",
  436. array('!cloud' => anchor('cloud/view/'.$comment->cloud_id,
  437. $comment->cloud_title),
  438. '!person'=> anchor('user/view/'.$comment->user_id,
  439. $comment->fullname,
  440. $attr_author))).'</em>';
  441. } else {
  442. $string .= '<em>'.t("new comment on the cloud !cloud by !person",
  443. array('!cloud' =>
  444. anchor('cloud/view/'.$comment->cloud_id,
  445. $comment->cloud_title),
  446. '!person'=>
  447. anchor('user/view/'.$comment->user_id,
  448. $comment->fullname, $attr_author))).'</em>';
  449. }
  450. }
  451. break;
  452. case 'news':
  453. // New blog post
  454. $this->CI->load->model('blog_model');
  455. $post_id = $event->follow_item_id;
  456. $news = $this->CI->blog_model->get_blog_post($post_id);
  457. $string = anchor("blog/view/$post_id", $news->title).'<br />';
  458. $string .= '<em>'.t("New blog post by !person",
  459. array('!person' =>
  460. anchor('user/view/'.$news->user_id,
  461. $news->fullname, $attr_author))).'</em>';
  462. break;
  463. case 'follow':
  464. // New follow
  465. $followed_id = $event->event_item_id;
  466. $following_id = $event->follow_item_id;
  467. $this->CI->load->model('user_model');
  468. $followed_user = $this->CI->user_model->get_user($followed_id);
  469. $following_user = $this->CI->user_model->get_user($following_id);
  470. $string = '<em>'.t("!following is now following !followed",
  471. array('!following'=>
  472. anchor('user/view/'.$following_user->user_id,
  473. $following_user->fullname, $attr_author),
  474. '!followed' =>
  475. anchor('user/view/'.$followed_user->user_id,
  476. $followed_user->fullname))).'</em>';
  477. break;
  478. case 'news_comment':
  479. $this->CI->load->model('blog_model');
  480. $this->CI->load->model('user_model');
  481. $comment_id = $event->event_item_id;
  482. $comment = $this->CI->blog_model->get_comment($comment_id);
  483. $truncated_comment = truncate_content(strip_tags($comment->body));
  484. $user = $this->CI->user_model->get_user($comment->user_id);
  485. $string = '<strong>'.'"'.$truncated_comment.'</strong><br />';
  486. $string .= '<em>'.t("!person commenting on the blog post !title",
  487. array('!person'=>
  488. anchor('user/view/'.$comment->user_id,
  489. $comment->fullname, $attr_author),
  490. '!title' =>
  491. anchor('blog/view/'.$comment->post_id,
  492. $comment->news_title))).'</em>';
  493. break;
  494. case 'new_user':
  495. $this->CI->load->model('user_model');
  496. $user_id = $event->event_item_id;
  497. $user = $this->CI->user_model->get_user($user_id);
  498. $string = '<em>'.t("!person has registered on !site-name!",
  499. array('!person' =>
  500. anchor('user/view/'.$user_id, $user->fullname) )).'</em>';
  501. break;
  502. case 'link':
  503. $this->CI->load->model('cloud_model');
  504. $this->CI->load->model('link_model');
  505. $link_id = $event->event_item_id;
  506. $link = $this->CI->link_model->get_link($link_id);
  507. if (!$link) {
  508. return false;
  509. }
  510. else {
  511. $cloud = $this->CI->cloud_model->get_cloud($link->cloud_id);
  512. $string = '';
  513. if ($link->title) {
  514. $string = '<strong>'.$link->title.' </strong><br />';
  515. }
  516. if ($simple) {
  517. $string .= '<em>'.t("added to !title by !person",
  518. array('!person'=>
  519. anchor('user/view/'.$link->user_id,
  520. $link->fullname, $attr_author),
  521. '!title' =>
  522. anchor('cloud/view/'.$cloud->cloud_id,
  523. $cloud->title))).'</em>';
  524. } else {
  525. $string .= '<em>'.t("new link on !title added by !person",
  526. array('!person'=>
  527. anchor('user/view/'.$link->user_id,
  528. $link->fullname,
  529. $attr_author),
  530. '!title' =>
  531. anchor('cloud/view/'.$cloud->cloud_id,
  532. $cloud->title))).'</em>';
  533. }
  534. }
  535. break;
  536. case 'reference':
  537. $this->CI->load->model('cloud_model');
  538. $reference_id = $event->event_item_id;
  539. $reference = $this->CI->cloud_model->get_reference($reference_id);
  540. if (!$reference) {
  541. return false;
  542. }
  543. else {
  544. $cloud = $this->CI->cloud_model->get_cloud($reference->cloud_id);
  545. $truncated_reference = truncate_content(
  546. strip_tags($reference->reference_text));
  547. $string = '<strong>'.'"'.$truncated_reference.'..."</em> </strong><br />';
  548. if ($simple) {
  549. $string .= '<em>'.t("on the cloud !title by !person",
  550. array('!person'=>
  551. anchor('user/view/'.$reference->user_id,
  552. $reference->fullname,
  553. $attr_author),
  554. '!title' =>
  555. anchor('cloud/view/'.$cloud->cloud_id,
  556. $cloud->title))).'</em>';
  557. } else {
  558. $string .= '<em>'.t("reference added to the cloud !title by !person",
  559. array('!person'=>
  560. anchor('user/view/'.$reference->user_id,
  561. $reference->fullname,
  562. $attr_author),
  563. '!title' =>
  564. anchor('cloud/view/'.$cloud->cloud_id,
  565. $cloud->title))).'</em>';
  566. }
  567. }
  568. break;
  569. case 'content':
  570. $this->CI->load->model('cloud_model');
  571. $this->CI->load->model('content_model');
  572. $content_id = $event->event_item_id;
  573. $content = $this->CI->content_model->get_content_item($content_id);
  574. if (!$content) {
  575. return false;
  576. }
  577. else {
  578. $truncated_content = truncate_content(strip_tags($content->body));
  579. $cloud = $this->CI->cloud_model->get_cloud($content->cloud_id);
  580. $string = '<strong>'.$truncated_content.'..."</strong><br />';
  581. if ($simple) {
  582. $string .= '<em>'.t("on !title by !person",
  583. array('!person'=>
  584. anchor('user/view/'.$content->user_id,
  585. $content->fullname,
  586. $attr_author),
  587. '!title' =>
  588. anchor('cloud/view/'.$cloud->cloud_id,
  589. $cloud->title))).'</em>';
  590. } else {
  591. $string .= '<em>'.t("new content on !title by !person",
  592. array('!person'=>
  593. anchor('user/view/'.$content->user_id,
  594. $content->fullname,
  595. $attr_author),
  596. '!title' =>
  597. anchor('cloud/view/'.$cloud->cloud_id,
  598. $cloud->title))).'</em>';
  599. }
  600. }
  601. break;
  602. case 'embed':
  603. $this->CI->load->model('cloud_model');
  604. $this->CI->load->model('embed_model');
  605. $embed_id = $event->event_item_id;
  606. $embed = $this->CI->embed_model->get_embed($embed_id);
  607. if (!$embed) {
  608. return false;
  609. }
  610. else {
  611. $cloud = $this->CI->cloud_model->get_cloud($embed->cloud_id);
  612. $string = '';
  613. if ($embed->title) {
  614. $string .= '<strong>'.$embed->title.'</strong><br />';
  615. }
  616. if ($simple) {
  617. $string .= '<em>'.t("new embedded content added to the cloud !title by !person",
  618. array('!person'=>
  619. anchor('user/view/'.$embed->user_id,
  620. $embed->fullname,
  621. $attr_author),
  622. '!title' =>
  623. anchor('cloud/view/'.$cloud->cloud_id,
  624. $cloud->title))).'</em>';
  625. } else {
  626. $string .= '<em>'.t("new embedded content added to the cloud !title by !person",
  627. array('!person'=> anchor('user/view/'.$embed->user_id,
  628. $embed->fullname,
  629. $attr_author),
  630. '!title' =>
  631. anchor('cloud/view/'.$cloud->cloud_id,
  632. $cloud->title))).'</em>';
  633. }
  634. }
  635. break;
  636. case 'profile_edit':
  637. $this->CI->load->model('user_model');
  638. $user_id = $event->event_item_id;
  639. $user = $this->CI->user_model->get_user($user_id);
  640. $string = '<em>'.t("!person editted their profile",
  641. array('!person' =>
  642. anchor('user/view/'.$user_id, $user->fullname) )).'</em>';
  643. break;
  644. case 'login_attempt':
  645. $this->CI->load->model('user_model');
  646. $user_id = $event->event_item_id;
  647. $user = $this->CI->user_model->get_user($user_id);
  648. $string = '<em>'.t("!person made an unsuccessful login attempt",
  649. array('!person' =>
  650. anchor('user/view/'.$user_id, $user->fullname) )).'</em>';
  651. break;
  652. break;
  653. }
  654. return $string;
  655. }
  656. /**
  657. * Turns an array of events into a suitable format for display on the site.
  658. * Strictly this code ought to be handled by views somehow.
  659. *
  660. * @param array $events The array of cloudstream events
  661. * @param boolean $simple Use the 'simple' format for events
  662. * @return string The formatted events
  663. */
  664. function display_format($events, $simple = false) {
  665. $this->load->helper('format');
  666. $last_event_item_id = false;
  667. $last_event_type = false;
  668. if ($events) {
  669. foreach ($events as $event) {
  670. // If somebody is following e.g. both a cloud and user that the item
  671. // pertains to then don't display the item twice
  672. if ($event->event_item_id != $last_event_item_id ||
  673. $event->event_type != $last_event_type) {
  674. $new_event = $this->event_model->to_string($event, $simple);
  675. if ($new_event) {
  676. $new_events[] = '<li class="'.$this->event_category($event->event_type).
  677. '">'.$new_event.' <em>'.time_ago($event->timestamp).'</em></li>';
  678. }
  679. $last_event_item_id = $event->event_item_id;
  680. $last_event_type = $event->event_type;
  681. }
  682. }
  683. }
  684. return $new_events;
  685. }
  686. /**
  687. * Format the array of events so that it's suitable for an RSS feed, or the API.
  688. *
  689. * @param array $events The array of cloudstream events
  690. * @return string The formatted events
  691. */
  692. function display_rss_format($events, $api=FALSE) {
  693. $new_events = NULL;
  694. if ($events) {
  695. $last_event_item_id = NULL;
  696. foreach($events as $event) {
  697. // If somebody is following e.g. both a cloud and user that the item
  698. // pertains to then don't display the item twice
  699. if ($event->event_item_id != $last_event_item_id ||
  700. $event->event_type != $last_event_type) {
  701. $description = $this->event_model->to_string($event);
  702. // Strip the links to get the title
  703. $modified_description = str_replace('</a>', '', $description);
  704. $modified_description = preg_replace('/<a[^>]+href[^>]+>/', '',
  705. $modified_description);
  706. $new_event->title = $modified_description;
  707. if ($api) { //API-specific.
  708. $new_event->title = strip_tags($new_event->title);
  709. $new_event->item_type = $event->event_type;
  710. $new_event->item_id= $event->event_item_id;
  711. $new_event->status = str_replace('<br />', '<small>',
  712. $description).'</small>';
  713. $new_event->created= $event->timestamp;
  714. $new_event->html_url =
  715. site_url("#$event->event_type-$event->event_item_id");
  716. } else { //RSS-specific.
  717. $new_event->description = $description;
  718. $new_event->timestamp= $event->timestamp;
  719. $new_event->category = $event->event_type;
  720. $new_event->link =
  721. site_url("#$event->event_type-$event->event_item_id");
  722. }
  723. switch ($event->event_type) {
  724. case 'cloud':
  725. case 'cloudscape': # Drop-through.
  726. if ($api) {
  727. $new_event->html_url =
  728. site_url("$event->event_type/view/$event->event_item_id");
  729. }
  730. $new_event->link =
  731. base_url("$event->event_type/view/$event->event_item_id");
  732. break;
  733. }
  734. $new_events[] = $new_event;
  735. unset($new_event);
  736. $last_event_item_id = $event->event_item_id;
  737. $last_event_type = $event->event_type;
  738. }
  739. }
  740. }
  741. return $new_events;
  742. }
  743. }