root/devel/mod/commentwall/lib.php

Revision 1613, 13.8 kB (checked in by misja, 4 months ago)

Applied attachment:ticket:383:commentwall.diff, fixes #383

  • Property svn:eol-style set to native
Line 
1 <?php
2     /**
3      * @file lib.php Comment wall plugin
4      * This plugin is a replacement for the comment wall widget.
5      * This replacement widget enables users to reply to another user's comment wall etc.
6      * @see Issue 98 for details
7      * @author Marcus Povey <marcus@dushka.co.uk>
8      */
9     
10     /**
11      * Comment wall initialisation.
12      */
13     function commentwall_init()
14     {
15         global $CFG, $db,$function, $metatags, $template, $METATABLES;
16
17         // Add meta tags
18         $metatags .= "<script type=\"text/javascript\" src=\"{$CFG->wwwroot}mod/commentwall/commentwall.js\"><!-- commentwall js --></script>";
19         
20         // Define some templates
21         templates_add_context('commentwallobject', 'mod/commentwall/template');
22         templates_add_context('commentwallfooter', 'mod/commentwall/footer');
23         templates_add_context('css', 'mod/commentwall/css');
24         
25         // Set up the database
26         $tables = $METATABLES;
27         if (!in_array($CFG->prefix . "commentwall", $tables))
28         {
29             if (file_exists($CFG->dirroot . "mod/commentwall/$CFG->dbtype.sql"))
30             {
31                 modify_database($CFG->dirroot . "mod/commentwall/$CFG->dbtype.sql");
32                 //reload system
33                 header_redirect($CFG->wwwroot);
34
35             }
36             else
37             {
38                 error("Error: Your database ($CFG->dbtype) is not yet fully supported by the Elgg commentwall. See the mod/commentwall directory.");
39             }
40     
41             print_continue($CFG->wwwroot);
42             exit;
43         }
44         
45         // Add configuration options
46         $function['userdetails:edit:details'][] = $CFG->dirroot . "mod/commentwall/lib/commentwall_settings.php";
47     }
48     
49     /**
50      * Comment wall page setup
51      */
52     function commentwall_pagesetup()
53     {
54         
55     }
56     
57     /** HACK: Output the given code as document.write */
58     function commentwall_todocwrite($text)
59     {
60         $body = "";
61         foreach (explode("\n",addslashes($text)) as $line)
62             $body .= "document.write(\"" . trim($line) . "\");\n";
63
64         return $body;
65     }
66     
67     /**
68      * Retrieve the wall for a given userid.
69      *
70      * @return mixed Array of comment objects, else returns false.
71      * @param unknown_type $userid The user / wall we are retrieving
72      * @param unknown_type $limit Limit on the search
73      * @param unknown_type $offset Offset
74      */
75     function commentwall_getwall($userid, $limit = 3, $offset = 0)
76     {
77         global $CFG;
78         
79         $query = "SELECT * from {$CFG->prefix}commentwall where wallowner=$userid order by posted desc limit $offset,$limit";
80         
81  // echo $query;
82         
83         return get_records_sql($query);
84     }
85
86
87     /**
88      * Retrieve the wall-to-wall for a given pair of userids.
89      *
90      * @return mixed Array of comment objects, else returns false.
91      * @param unknown_type $userid The user / wall we are retrieving
92      * @param unknown_type $limit Limit on the search
93      * @param unknown_type $offset Offset
94      */
95         function commentwall_getwalltowall($userid, $otherid, $limit = 10, $offset = 0)
96     {
97         global $CFG;
98         
99         $query = "SELECT * FROM " . $CFG->prefix . "commentwall " .
100                          "WHERE (wallowner=" . $userid . " AND comment_owner=" . $otherid . ") OR ".
101                  "(wallowner=" . $otherid . " AND comment_owner=" . $userid . ") ".
102                  "ORDER BY posted desc LIMIT " . $offset . "," . $limit;
103         
104         //echo $query;
105         
106         return get_records_sql($query);
107     }
108
109     
110     /**
111      * Add a comment to a wall.
112      * @param unknown_type $wall_id Which wall to post to
113      * @param unknown_type $poster_id
114      * @param unknown_type $text
115      */
116     function commentwall_addcomment($wall_id, $poster_id, $text)
117     {
118       global $CFG;
119       
120       $newcomment = new stdClass;
121       $newcomment->wallowner = $wall_id;
122       $newcomment->comment_owner = $poster_id;
123       $newcomment->content = $text;
124       $newcomment->posted = time();
125       
126       if ($newcomment->ident = insert_record("commentwall", $newcomment)) {
127             if ($newcomment->comment_owner != $newcomment->wallowner) {
128           
129                 $message = __gettext(sprintf("You have received a comment from %s on your comment wall:", user_name($newcomment->comment_owner), stripslashes($object_title)));
130                 $message .= "\n\n" . stripslashes($newcomment->content) . "\n\n";
131                 $message .= __gettext(sprintf("To reply on %s's comment wall, click here: %s", user_name($newcomment->comment_owner), $CFG->wwwroot . user_info("username", $newcomment->comment_owner) . "/profile/")) . "\n";
132                 $message .= __gettext(sprintf("To see other comments on your wall, click here: %s", $CFG->wwwroot . user_info("username", $newcomment->wallowner) . "/profile/"));
133                 $message = wordwrap($message);
134                 
135                 message_user($wall_id, $newcomment->comment_owner__gettext(sprintf("%s has posted to your comment wall", user_name($newcomment->comment_owner))), $message);
136             }
137             $newcomment = plugin_hook("commentwall","publish",$newcomment);
138             return $newcomment->ident;
139         }
140         
141         $newcomment->ident = insert_record("commentwall", $newcomment);
142         $newcomment = plugin_hook("commentwall","publish",$newcomment);
143         return $newcomment->ident;
144     }
145     
146     /**
147      * Delete the given object id.
148      *
149      * @param unknown_type $object_id
150      */
151     function commentwall_deletecomment($object_id)
152     {
153         global $CFG;
154         
155         // Pull object
156         $comment = get_record_sql("SELECT * from {$CFG->prefix}commentwall where ident=$object_id");
157
158         // Check ownership (if you are either the wall owner or the comment owner you can delete this)
159         if ((commentwall_permissions_check($comment->wallowner)) || (commentwall_permissions_check($comment->comment_owner)))
160         {
161             if (!delete_records("commentwall", "ident", $comment->ident))
162                 plugin_hook('commentwall','delete',$comment);
163                 return false;
164                 
165             return true;
166         }
167         
168         return false;
169     }
170     
171     /**
172      * Reply to a given comment.
173      * @param unknown_type $comment_id
174      * @param unknown_type $wall_id Which wall to post to
175      * @param unknown_type $poster_id
176      * @param unknown_type $text
177      */
178     function commentwall_replyto($comment_id, $wall_id, $poster_id, $text)
179     {
180         global $CFG;
181         
182         // Extract the comment we are replying to
183         $comment = get_record_sql("SELECT * from {$CFG->prefix}commentwall where ident=$comment_id");
184         
185         $newcomment = new stdClass;
186             $newcomment->wallowner = $wall_id;
187             $newcomment->comment_owner = $poster_id;
188             $newcomment->content = $text;
189             $newcomment->posted = time();
190         
191         return insert_record("commentwall", $newcomment, true);
192         
193     }
194     
195     /**
196      * Display post form.
197      */
198     function commentwall_post_form($wall_owner, $replyto = -1, $specialmode = false, $suffix = "", $returnurl = "")
199     {
200         global $CFG;
201         
202         if ($returnurl=="")
203             $returnurl = urlencode($_SERVER['REQUEST_URI']);
204     $form_key = elggform_key_get("commentwall_{$wall_owner}_post");
205         $buttontxt = __gettext("Post comment");
206         
207         $frm_elements_common = <<< END
208             <input type="hidden" name="action" value="commentwall::post" />
209             <input type="hidden" name="owner" value="$wall_owner" />
210             <input type="hidden" name="wallowner" value="$wall_owner" />
211             <input type="hidden" name="reply" value="$replyto" />
212             <input type="hidden" name="comment_owner" value="{$_SESSION['userid']}" />       
213             <input type="hidden" name="return_url" value="$returnurl" />
214       <input type="hidden" name="form_key" value="$form_key" />
215             <textarea name="text"></textarea>
216 END;
217
218         $html = <<< END
219             <div id="commentwall_form_$replyto">
220                 
221                     <form id="commentwall_post_frm_$replyto$suffix" action="{$CFG->wwwroot}mod/commentwall/do_action.php" method="POST">
222                         $frm_elements_common
223                         <br /><input type="submit" name="$buttontxt" value="$buttontxt" />
224                     </form>   
225             </div>
226 END;
227         
228         return $html;
229     }
230     
231     /**
232      * Display a comment.
233      *
234      * @param unknown_type $comment_obj
235      */
236     function commentwall_displaycomment($comment_obj)
237     {
238         global $CFG;
239         
240         $html = "";
241         
242         $owner_username = user_info("name", $comment_obj->wallowner);
243         $comment_owner_username = ($comment_obj->comment_owner != 0 ? user_info("name", $comment_obj->comment_owner) : __gettext("Anonymous User"));   
244         $icon = ($comment_obj->comment_owner != 0 ? user_info('icon',$comment_obj->comment_owner) : -1);
245         $userlogo = user_icon_html($comment_obj->comment_owner,60,true); // $CFG->wwwroot.'_icon/user/'.$icon.'/w/50';
246         $userlink = ($comment_obj->comment_owner != 0 ? $CFG->wwwroot . user_info("username", $comment_obj->comment_owner) . "/" : "");
247             
248         $date = date("l jS F Y, g:ia" ,$comment_obj->posted);
249         $text = nl2br($comment_obj->content);
250         
251         $replytowall = __gettext("Post reply");
252         $replytootherwall = sprintf(__gettext("%s's wall"), $comment_owner_username);
253         $walltowall = __gettext("Wall-to-wall");
254         $delete = __gettext("Delete");
255         
256         $doaction = "{$CFG->wwwroot}mod/commentwall/do_action.php?owner=" . $comment_obj->wallowner. "&return_url=" .urlencode($_SERVER['REQUEST_URI']);
257
258         $replybar = "";
259         if (isloggedin())
260         {
261             //$replybar .= "<a href=\"#commentwall_form_-1\">$replytowall</a>";
262           if (($comment_obj->wallowner != $comment_obj->comment_owner) && ($comment_obj->comment_owner != 0)) {
263                 $replybar .= "<a href=\"{$CFG->wwwroot}mod/commentwall/index.php?owner={$comment_obj->comment_owner}&wallowner={$comment_obj->comment_owner}&comment_owner={$_SESSION['userid']}&reply={$comment_obj->ident}&return_url=" .urlencode($_SERVER['REQUEST_URI'])."\">$replytootherwall</a> | ";     
264                 $replybar .= "<a href=\"{$CFG->wwwroot}mod/commentwall/walltowall.php?owner={$comment_obj->wallowner}&other={$comment_obj->comment_owner}&return_url=" .urlencode($_SERVER['REQUEST_URI'])."\">$walltowall</a> | ";
265           }
266           if ((commentwall_permissions_check($comment_obj->comment_owner))
267               || (commentwall_permissions_check($comment_obj->wallowner)))
268             $replybar.= "<a href=\"$doaction&action=commentwall::delete&ident={$comment_obj->ident}\">$delete</a>";
269         }
270
271
272         $html = templates_draw(
273             array(
274                 'context' => "commentwallobject",
275                 'userlogo' => $userlogo,
276                 'userlink' => $userlink,
277                 'usertxt' => $comment_owner_username,
278                 'date' => $date,
279                 'text' => $text,
280                 'replybar' => $replybar
281             )
282         );
283         
284         return $html;
285     }
286     
287     /**
288      * @param $wall List of objects.
289      */
290     function commentwall_display_footer($owner, $limit = 3, $offset = 0)
291     {
292         global $CFG;
293         
294         $html = "";
295         
296         $count = get_record_sql("SELECT count(ident) as ident from {$CFG->prefix}commentwall where wallowner=$owner order by posted desc");
297         $count = $count->ident;
298
299         $qs = $_SERVER['REDIRECT_URL'];
300         if ($qs == "") $qs = $_SERVER['PHP_SELF'];
301         
302         // See if we need to display a next button
303         $nextbutton = "";
304         if ($count - $offset > $limit)
305             $nextbutton = "<a href=\"$qs?owner=$owner&offset=" . ($offset+$limit) . "\">" . __gettext("Back") . "</a>";
306             
307         // See if we need to display a prev button
308         $prevbutton = "";
309         if (floor($offset / $limit) > 0)
310             $prevbutton = "<a href=\"$qs?owner=$owner&offset=" . ($offset-$limit) . "\">" . __gettext("Forward") . "</a>";
311         
312         return templates_draw(
313         array(
314         'context' => 'commentwallfooter',
315         'nextbutton' => $nextbutton,
316         'prevbutton' => $prevbutton
317         )
318         );
319     }
320     
321     /**
322      * Display a comment wall.
323      *
324      * @param unknown_type $wall
325      */
326     function commentwall_displaywall_html($wall,$showalltxt = false, $owner)
327     {
328         global $CFG;
329
330         $html = "";
331         
332         // Get access permissions
333         $access = user_flag_get("commentwall_access", $owner);
334         if (!$access) $access = "LOGGED_IN"; // If no access controls set then assume public
335         
336         if (
337             ($owner == $_SESSION['userid']) ||                              // Display if the current user owns it
338             ($access == "PUBLIC") ||                                        // Display if public
339             ( ($access == "LOGGED_IN") && (isloggedin()) ) ||               // If user needs to be logged in, check it
340             ( ($access == "FRIENDS_ONLY") && (isfriend($_SESSION['userid'],$owner))) ||                         // If you can
341             ( ($access == "PRIVATE") && ($owner == $_SESSION['userid']) )   // If access is private then ensure that $owner is the current logged in user
342         )
343         {
344             // $owner = page_owner();
345             $html = "<div id=\"commentwall_title\"><h2>" . sprintf(__gettext("Write on %s's comment wall"), user_info("name", $owner)) . "</h2></div>";
346
347             if (($showalltxt) && ($wall))
348             {
349                 $html .= "<div id=\"commentwall_more\"><a href=\"{$CFG->wwwroot}mod/commentwall/index.php?owner=$owner\">" . __gettext("See all...") . "</a></div>";
350             }
351
352
353             $html .= commentwall_post_form($owner);
354             
355             if (!$wall) {
356                 $html .= "<p>" . __gettext("No comments on this wall, why not be the first?") . "</p>";
357                 return $html;
358             }
359         
360         }
361         
362         if (is_array($wall)) {
363             foreach ($wall as $w)
364                 $html .= commentwall_displaycomment($w);
365         }
366         
367         return $html;
368     }
369     
370     /**
371      * Render the profile on a profile.
372      *
373      * @param unknown_type $owner
374      */
375     function commentwall_displayonprofile($owner, $limit = 3, $offset = 0)
376     {
377         global $CFG;
378         $html = "";   
379         
380         $wall = commentwall_getwall($owner, $limit, $offset);
381         
382         $html = "<div id=\"commentwall\">";
383         
384         $html .= commentwall_displaywall_html($wall, true, $owner);
385     
386         $html .= "</div>";
387         
388         return $html;
389     }
390     
391 /**
392  * Determines whether or not the current user has permission to do something with the comment.
393  *
394  * @param $owner The owner of the comment.
395  * @return boolean True or false.
396  */
397 function commentwall_permissions_check($owner)
398 {
399     global $CFG;
400     
401     if (isloggedin()) {
402         if ($owner == $_SESSION['userid'] || user_flag_get('admin',$_SESSION['userid'])) {
403             return true;
404         }
405         if (user_info("user_type",$owner) == "community") {
406             if (record_exists('users','ident',$owner,'owner',$_SESSION['userid'],'user_type','community')) {
407                 return true;
408             }
409             if (count_records_sql('SELECT count(u.ident) FROM '.$CFG->prefix.'friends f
410                                              JOIN '.$CFG->prefix.'users u ON u.ident = f.friend
411                                              WHERE u.ident = ? AND f.owner = ? AND u.user_type = ?',
412                                   array($owner,$_SESSION['userid'],'community'))) {
413                 return true;
414             }
415         }
416     }
417     
418     return false;
419 }
420 ?>
421
Note: See TracBrowser for help on using the browser.