Simple status box for the sidebar.
[elgg_plugins.git] / river / lib.php
blob6ae02ee4ef22529b989bc15d267105b3de049b88
1 <?php
2 /*
3 Event log and river plugin for Elgg
4 Initial author: Marcus Povey <marcus@dushka.co.uk>
6 Notes:
7 This is a little rough and ready. If you want to add events to the river you need to register
8 a handle and listen to various publish / delete /create events applicable to your module.
10 For compatibility I would suggest that you place the listen_for_event calls inside a function exists
11 test. This will allow handle the situation where the river plugin is not present.
13 eg.
15 if (function_exists('river_save_event'))
17 listen_for_event('file','publish', 'file_river_hook');
18 listen_for_event('file','delete', 'file_river_hook');
22 function river_init()
24 global $CFG, $db, $template;
26 // Set up the database
27 $tables = $db->Metatables();
28 if (!in_array($CFG->prefix . "river", $tables))
30 if (file_exists($CFG->dirroot . "mod/river/$CFG->dbtype.sql"))
32 modify_database($CFG->dirroot . "mod/river/$CFG->dbtype.sql");
34 else
36 error("Error: Your database ($CFG->dbtype) is not yet fully supported by the Elgg river. See the mod/river directory.");
39 print_continue("index.php");
40 exit;
43 // Test for widget support
44 if (!function_exists('widget_init'))
46 error("Widget support isn't installed. Please download and install the widgets module to mod/widget.");
47 exit;
50 // Register widget
51 $CFG->widgets->list[] = array(
52 'name' => gettext("River widget"),
53 'description' => gettext("Display a list of recent activity."),
54 'type' => "river::river"
57 $template['css'] .= file_get_contents($CFG->dirroot . "mod/river/css.css");
61 function river_pagesetup()
65 /** Save a river event to the database */
66 function river_save_event($userid, $object_id, $object_owner, $object_type, $string, $access = "PUBLIC")
68 $entry = new stdClass;
69 $entry->userid = $userid;
70 $entry->object_id = $object_id;
71 $entry->object_owner = $object_owner;
72 $entry->object_type = $object_type;
73 $entry->access = $access;
74 $entry->ts = time();
75 $entry->string = $string;
77 return insert_record('river', $entry);
80 /** Get the display data */
81 function river_get_river($userid, $limit = 10, $starttime = "", $endtime = "")
83 global $CFG;
85 // Get mutual friend network
87 // My friends
88 $query = "SELECT friend from {$CFG->prefix}friends where owner=$userid";
89 //echo "$query\n";
90 $myfriends = get_records_sql($query);
92 $list = "";
93 foreach ($myfriends as $friend)
94 $list .= $friend->friend . ",";
95 $list = trim($list, " ,");
97 // Get a subset who also have me as a friend
98 $query = "SELECT owner from {$CFG->prefix}friends where owner in ($list) and friend=$userid";
99 //echo "$query\n";
100 $theirfriends = get_records_sql($query);
102 $list2 = "";
103 foreach ($theirfriends as $friend)
104 $list2 .= $friend->owner . ",";
106 $list = "$list2";
108 $list2 = trim($list2, " ,");
109 $list .= $userid; // We also want to include myself ( to the first list not the second).
111 if ($list2=="") return false;
113 // Time range
114 if ($starttime!="")
115 $starttime = "and ts >= $starttime";
117 if ($endtime!="")
118 $endtime = "and ts <= $endtime";
120 // Fix limit
121 if ($limit == 0)
122 $limit = "";
123 else
124 $limit = "limit $limit";
126 // Do access
127 $access = " and (access='PUBLIC' ";
128 if (isloggedin()) $access .= " or access='LOGGED_IN' ";
129 if ($userid == $_SESSION['userid']) $access .= " or access='PRIVATE' ";
130 $access .= ") ";
132 $query = "select * from {$CFG->prefix}river where 1 $starttime $endtime $access and (userid in ($list) or object_owner in ($list2)) order by ts desc $limit";
133 //echo "$query\n";
134 return get_records_sql($query);
138 /** Display the widget */
139 function river_widget_display($widget)
141 global $CFG;
143 // Pull number of river entries
144 $river_num_display = widget_get_data("river_num_display",$widget->ident);
145 if ($river_num_display == "") $river_num_display = 10;
147 $userid = page_owner(); // user id is page owner
148 $name = user_info("name", $userid);
150 $river = river_get_river($userid, $river_num_display);
152 $body = "";
153 if ($river)
155 $d1 = "";
156 $d2 = "";
158 foreach ($river as $r)
160 $d2 = date("j-n-y", $r->ts); // Set second marker
162 $type = str_replace(":", "_", $r->object_type);
164 if (strcmp($d1,$d2)!=0) $body .= "<p class=\"widget_river_date\">". date("l jS F Y", $r->ts) . "</p>";
165 $body .= "<div class=\"widget_river_type_$type\"><div class=\"widget_river_text\"> " . __gettext($r->string) . "<span class=\"widget_river_time\">" . date("g:ia", $r->ts) . "</span></div></div>";
167 $d1 = date("j-n-y", $r->ts); // Now set first marker to date
170 $body .= "<div class=\"widget_river_rss\"><a href=\"{$CFG->wwwroot}mod/river/rss.php?owner=$userid\"><img src=\"{$CFG->wwwroot}mod/template/icons/rss.png\" border=\"0\" /></a></div>";
172 else
174 $body = __gettext("This user hasn't done anything yet.");
177 //$ttl = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td align=\"left\">". $name . __gettext("'s River") . "</td><td align=\"right\"><a href=\"{$CFG->wwwroot}mod/river/rss.php?owner=" . page_owner() . "\"><img src=\"{$CFG->wwwroot}mod/template/icons/rss.png\" border=\"0\" /></a></td></tr></table>";
179 return array('title'=> $name . __gettext("'s River"),'content'=>$body);
180 //return array('title'=> $ttl,'content'=>$body);
183 /** Edit the widget */
184 function river_widget_edit($widget)
186 global $CFG;
188 $num_display = widget_get_data("river_num_display", $widget->ident);
189 if (!$num_display) $num_display = 10;
191 $body = "<h2>". __gettext("River Widget") . "</h2>";
192 $body .= "<p>" . __gettext("This widget displays a river of events to do with a user.") . "</p>";
194 $body .= "<p><select name=\"widget_data[river_num_display]\">\n";
195 for ($n = 5; $n <50; $n += 5)
197 if ($n == $num_display) {
198 $selected= "selected=\"selected\"";
199 } else {
200 $selected= "";
203 $body .= "<option value=\"" . $n . "\" $selected>" . $n. "</option>\n";
206 $body .= "</select></p>\n";
208 // Return HTML
209 return $body;
212 /** Register a hook to return the friendly name of an object */
213 function river_register_friendlyname_hook($object_type, $function)
215 global $CFG;
217 if (!isset($CFG->river_friendlyname_hooks))
218 $CFG->river_friendlyname_hooks = array();
220 $CFG->river_friendlyname_hooks[$object_type][] = $function;
223 /** Look through registed hooks for something that will return a friendly string identifying $object_id of type $object_type */
224 function river_get_friendly_id($object_type, $object_id)
226 global $CFG;
228 $fn = "";
230 if (isset($CFG->river_friendlyname_hooks[$object_type]))
232 foreach ($CFG->river_friendlyname_hooks[$object_type] as $function)
234 if (is_callable($function))
235 $fn = $function( $object_type, $object_id);
238 if ($fn=="")
239 $fn = $object_type . " " . $object_id;
242 return $fn;
245 /** Simple function to get the URL of a user from their userid */
246 function river_get_userurl($userid)
248 global $CFG;
250 return $CFG->wwwroot . user_info("username", $userid) . "/";
253 /* FUture notes:
254 - collections on river so that you can filter them