3 * @file lib.php Explode plugin
4 * This plugin is a test Explode sidebar widget
5 * @author Curverider <dave@curverider.co.uk>
8 include('explode_api_client.php');
11 * Explode page set up page setup
13 function explode_pagesetup() {
18 * Explode module initialisation.
20 function explode_init() {
21 global $CFG, $db,$function, $metatags, $template;
24 $CFG->templates
->variables_substitute
['explodesidebar'][]= "explode_sidebar";
26 //define some template items
27 $template['css'] .= file_get_contents($CFG->dirroot
. "mod/explode/css");
30 $metatags .= "<script type=\"text/javascript\" src=\"{$CFG->wwwroot}mod/explode/js/edit.js\"><!-- status js --></script>";
32 // Set up the database
33 $tables = $db->Metatables();
34 if (!in_array($CFG->prefix
. "explode", $tables))
36 if (file_exists($CFG->dirroot
. "mod/explode/$CFG->dbtype.sql"))
38 modify_database($CFG->dirroot
. "mod/explode/$CFG->dbtype.sql");
42 error("Error: Your database ($CFG->dbtype) is not yet fully supported by the Elgg explode functionality. See the mod/explode directory.");
45 print_continue($CFG->wwwroot
);
51 // if a user edits the number of friends they want - capture the result
52 if(optional_param("explode:add")) {
53 explode_sidebar_add(optional_param("explode_serviceName"), optional_param("explode_service_username"), optional_param("explode_numberToDisplay"));
56 // if a user want to delete a service
57 if(optional_param("explode:delete")) {
58 explode_sidebar_delete(optional_param("explode:service",0,PARAM_INT
), optional_param("explode:owner"), optional_param("explode:service_username"));
61 // if a user want to edit a service
62 if(optional_param("explode:edit")) {
63 explode_sidebar_edit(optional_param("explode_serviceName"), optional_param("explode_service_username"), optional_param("explode_numberToDisplay"), optional_param("explode_record_ident",0,PARAM_INT
));
72 * This function handle the display and add service functionality
73 * it produces a template keyword that sits in the sidebar portion
77 function explode_sidebar() {
78 global $CFG, $page_owner;
83 $body = "<li id=\"sidebar_external_friends\">";
85 // this adds the JS to collapse the friends sidebar widget if desired.
86 // would be nice to remember the setting but for now it just persist for the duration
87 // the user is on the page
89 $body .= "<a href=\"javascript:changeVisibility('friendsExternal', '/mod/explode/icons/', '')\" >";
91 // Displays the open/close icon beside the widget header
93 $body .= "<h2><img id=\"imgfriendsExternal\" src=\"/mod/explode/icons/minus.gif\" border=\"0\" name=\"close\"> " . __gettext("Friends elsewhere") . "</h2></a>";
95 //this is the div that is required by the colapse JS
97 $body .= "<div id=\"elementfriendsExternal\" style=\"display:block\">";
101 $get_results = get_records_sql("SELECT * FROM {$CFG->prefix}explode WHERE owner=$user");
105 //loop through each result returned for a particular user's services
106 foreach ($get_results as $returned) {
109 $service_id = $returned->service
; // the name of the external service selected
110 $username = $returned->service_username
; //username on that service
111 $user_count = $returned->number_to_display
; // the number of friends to display
112 $service_name = $returned->service_name
;
114 //use the Explode API function to get the users ident in explode
115 $results = users_viewbyusername($username, $service_id);
117 //loop through returned 'viewbyusername' result
118 foreach($results as $res) {
120 // check that the ident is not empty
121 if($res->ident
!= '') {
123 // API function to get all friends of a user
124 $friends = users_getfriends($res->ident
);
126 //var_export($friends);
131 // code which selects the correct icon to display
132 include('switch-icons.php');
134 //display the relevant icon for a service
135 $body .= "<div class=\"service\"><img src=\"/mod/explode/images/". $icon ."\"/></div>";
137 $body .= "<div class=\"external-friends\">";
139 //iterate through the array and pull out relevant data
140 foreach($friends->result
as $friend_res) {
142 if ($i == $user_count) {
143 break; // break out according to the users choice for display amount.
146 $z = 0; // counter to end the display div after two cycles
148 //check there are some details
149 if($friend_res->user
->ident
!= '') {
151 //itterate through the second array called user_property
152 foreach($friend_res->user_property
as $url){
153 $friend_url = $url->val
;
156 $body .= "<a href=\"$friend_url\"><img src=" . $friend_res->user
->iconurl
. " width=\"48\" height=\"48\"/></a>";
158 // this if statement just close the div and creates a new one
159 // after two icons are displayed
161 $body .= "</div><div class=\"external-friends\">";
164 $z++
; // counter to break the table cells - ATM it displays two to a row
171 }//end foreach friends
173 //if logged in and the owner
174 if($page_owner == $_SESSION['userid']) {
176 //include the edit form
177 include('edit-form.php');
179 //form to delete a service
181 <form action="" method="POST">
182 <input type="hidden" name="explode:service" value="$service_id" />
183 <input type="hidden" name="explode:owner" value="$page_owner" />
184 <input type="hidden" name="explode:service_username" value="$username" />
185 <input type="hidden" name="explode:delete" value="true" />
186 <input type="submit" name="submit" value="delete" id="delete-button"/>
191 $body .= "</div>"; //end external friends div
193 }//end of if statement
194 }//end foreach results
195 }//end foreach get results
196 }// end if get results
198 if($page_owner == $_SESSION['userid']) {
199 include('add-form.php');
202 $body .= "</div>"; // end the collapsable div
204 $body .= "<div id=\"explode-logo\"><a href=\"http://ex.plode.us\"><img src=\"{$CFG->prefix}mod/explode/images/logo_blue.gif\"/></a></div>";
213 * This function handles a users edits to the services they
217 function explode_sidebar_add($serviceName, $serviceUsername, $numberToDisplay) {
219 global $CFG, $page_owner;
222 // check all the correct variable are present
223 if (!empty($serviceName) && !empty($serviceUsername) && !empty($numberToDisplay)) {
225 //API function to get the service id from the user selection
226 $get_service = services_findservicesbyname($serviceName);
229 //itterate through the results
230 foreach ($get_service->result
as $res) {
231 $serviceID = $res->ident
; // get the named service's ID
235 // check the page owner is in fact the one logged in
236 if($page_owner == $_SESSION['userid']) {
239 //query used to check if this user has already got the service running
240 $check_results = get_records_sql("SELECT * FROM {$CFG->prefix}explode WHERE owner=$page_owner and service=$serviceID and service_username='$serviceUsername'");
242 //check the record doesn't already exists
243 if(!$check_results) {
245 $row_to_insert = new stdClass
;
246 $row_to_insert->owner
= $page_owner;
247 $row_to_insert->service
= $serviceID;
248 $row_to_insert->service_username
= $serviceUsername;
249 $row_to_insert->service_name
= $serviceName;
250 $row_to_insert->number_to_display
= $numberToDisplay;
252 $insertRecords = insert_record('explode',$row_to_insert);
261 // if the record updates the DB great, if not send back an error message
265 return 'Error, we were unable to save your select. Try again or contact the administrator.';
269 return 'An error occurred. Please try again or contact your site admin.';
275 * Function that deletes a service
278 function explode_sidebar_delete($service, $owner, $service_username) {
280 global $CFG, $page_owner;
282 // check the page owner is in fact the one logged in
283 if($owner == $_SESSION['userid']) {
285 //query used to check the record exists and get its ident for deletion
286 $get_ident = get_records_sql("SELECT * FROM {$CFG->prefix}explode WHERE owner=$owner and service=$service and service_username='$service_username'");
288 //check the record exists
291 foreach($get_ident as $results) {
292 $ident_delete = $results->ident
;
295 //delete the appropriate record
296 $deleteRecords = delete_records('explode','ident', $ident_delete);
303 function explode_sidebar_edit($service_name, $service_username, $number_to_display, $record_ident) {
305 global $CFG, $page_owner;
307 // check the page owner is in fact the one logged in
308 if($page_owner == $_SESSION['userid']) {
310 //API function to get the service id from the service_name
311 $get_service = services_findservicesbyname($service_name);
313 //iterate through the results
314 foreach ($get_service->result
as $res) {
315 $serviceID = $res->ident
; // get the named service's ID
318 //check the record exists
319 if($get_row_to_edit = get_record_sql("SELECT * FROM {$CFG->prefix}explode WHERE ident=$record_ident")){
321 $get_row_to_edit->service
= $serviceID;
322 $get_row_to_edit->service_username
= $service_username;
323 $get_row_to_edit->service_name
= $service_name;
324 $get_row_to_edit->number_to_display
= $number_to_display;
326 $updateRecords = update_record('explode',$get_row_to_edit);