/[drupal]/contributions/modules/usercomment/usercomment.module
ViewVC logotype

Contents of /contributions/modules/usercomment/usercomment.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Tue Sep 16 17:56:21 2008 UTC (14 months, 1 week ago) by gwen
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.4: +92 -85 lines
File MIME type: text/x-php
applying patch contributed by wmostrey to upgrade to drupal 6: http://drupal.org/node/234204
1 <?php
2 /* $Id: usercomment.module,v 1.4 2008/09/16 17:38:49 gwen Exp $ */
3 /**
4 * @file
5 *
6 * This module gives users administrative privileges on comments to nodes they own
7 */
8
9 /**
10 * Implementation of hook_comment()
11 */
12 function usercomment_comment(&$comment, $op) {
13 switch ($op) {
14 case 'insert':
15 case 'update':
16 if (usercomment_access_check($comment['cid'], 'approve', 'update')) {
17 global $user;
18
19 $node = node_load($comment['nid']);
20 $node->user = $node->user ? $node->user : user_load( array( "uid" => $node->uid ) );
21
22 $status_msg_php = variable_get('usercomment_msg_approval_queue_php', '');
23 if ($status_msg_php != '') {
24 $status_msg = eval($status_msg_php);
25 }
26 else {
27 $status_msg = variable_get('usercomment_msg_approval_queue', t("Your comment will be posted once it's been approved."));
28 $status_msg = t($status_msg, array(
29 '@poster' => $user->name,
30 '@node_owner' => $node->user->name,
31 '@subject' => $comment['subject'],
32 ));
33 }
34 drupal_set_message($status_msg);
35
36 db_query("UPDATE {comments} SET status = %d WHERE cid = %d", COMMENT_NOT_PUBLISHED, $comment['cid']);
37 if (! isset($node->user->usercomment_get_notifications) || $node->user->usercomment_get_notifications == 1) {
38 $subject = variable_get('usercomment_mail_subject', t('Someone posted a new comment!'));
39 $message = variable_get('usercomment_mail_message', usercomment_mail_message_default());
40 $replacements = array(
41 '@approver' => $node->name,
42 '@subject' => $comment['subject'],
43 '@comment' => $comment['comment'],
44 '@commenter' => $comment['name'],
45 '@link' => url('node/'. $node->nid, NULL, NULL, TRUE),
46 '@site' => variable_get("site_name", "Drupal"),
47 '@siteurl' => $GLOBALS["base_url"],
48 );
49 $subject = t($subject, $replacements);
50 $message = t($message, $replacements);
51 $site_mail = variable_get('site_mail', "");
52 if (!strlen($site_mail)) {
53 if (user_access('administer nodes')) {
54 drupal_set_message(t('You should create an administrator mail address for your site! <a href="%url">Do it here</a>.', array('%url' => url('admin/settings/site-information'))), 'error');
55 }
56 $site_mail = 'nobody@localhost';
57 }
58 drupal_mail('usercomment_mail', $node->user->mail, $subject, $message, $site_mail);
59 }
60 }
61 break;
62 }
63 }
64
65 /**
66 * Implementation of hook_help()
67 */
68 function usercomment_help($path, $arg) {
69 switch ($path) {
70 case 'admin/help#usercomment':
71 return t('<p>This module lets users delete comments on own nodes they create without giving them full comment administration access. Permissions are on a per node type basis, so it\'s a great way to, e.g., allow users to administer comments on their own blogs. Additionally, you can configure this module to force comments on selected node types to be approved before they get published. As with delete rights, this is administered by users so you don\'t have to do it yourself.</p>');
72 break;
73 case 'admin/modules#description':
74 return t('This module gives users some additional comment administration rights on content they create.');
75 break;
76 }
77 }
78
79 /**
80 * Implementation of hook_link()
81 */
82 function usercomment_link($type, $comment = NULL, $teaser = FALSE) {
83 $links = array();
84
85 // we're only adding links to comments, so return if not comment
86 if ($type != "comment") return $links;
87 if (usercomment_access_check($comment->cid)) {
88 $links['usercomment_delete'] = array(
89 'title' => t('delete'),
90 'href' => 'usercomment/delete/'. $comment->cid,
91 );
92 }
93 if (usercomment_access_check($comment->cid, 'approve', 'link')) {
94 $node = node_load($comment->nid);
95 if ($comment->status == COMMENT_NOT_PUBLISHED) {
96 $links['usercomment_approve'] = array(
97 'title' => t('approve'),
98 'href' => 'usercomment/approve/'. $comment->cid,
99 );
100 }
101 }
102 return $links;
103 }
104
105 /**
106 * Implementation of hook_menu()
107 */
108 function usercomment_menu() {
109 global $user;
110 $items = array();
111 $items['admin/content/comment/usercomment'] = array(
112 'title' => 'Approval email',
113 'page callback' => 'drupal_get_form',
114 'page arguments' => array('usercomment_admin_settings'),
115 'type' => MENU_LOCAL_TASK,
116 'access arguments' => array('administer comments'),
117 'weight' => 20,
118 );
119 $items['usercomment/delete'] = array(
120 'type' => MENU_CALLBACK,
121 'page callback' => 'comment_delete',
122 'access callback' => usercomment_menu_access,
123 'access arguments' => array('delete', $user),
124 'file path' => drupal_get_path('module', 'comment'),
125 'file' => 'comment.admin.inc',
126 );
127 $items['usercomment/approve'] = array(
128 'type' => MENU_CALLBACK,
129 'page callback' => 'usercomment_approve',
130 'page arguments' => array($comment, $node),
131 'access callback' => usercomment_menu_access,
132 'access arguments' => array('approve', $user),
133 );
134 return $items;
135 }
136
137 /**
138 * Define menu access permission
139 */
140 function usercomment_menu_access($op, $user) {
141 $cid = arg(2);
142 if(!empty($cid)) {
143 $comment = _comment_load($cid);
144 $node = node_load($comment->nid);
145 }
146 switch ($op) {
147 case 'approve':
148 return ($user->uid == $node->uid || user_access('administer comments'));
149 case 'delete':
150 return ($user->uid == $node->uid || usercomment_access_check($comment->cid));
151 }
152 }
153
154 /**
155 * Implementation of hook_nodeapi()
156 */
157 function usercomment_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
158 switch ($op) {
159 case 'view':
160 return usercomment_approval_form($node);
161 break;
162 }
163 }
164
165 /**
166 * Implementation of hook_perm()
167 */
168 function usercomment_perm() {
169 $perms = array();
170 $perms[] = 'post comments with no usercomment checking';
171 foreach (node_get_types() as $node) {
172 $perms[] = 'delete comments on own '. check_plain($node->type) .' content';
173 $perms[] = 'approve comments on own '. check_plain($node->type) .' content';
174 }
175 return $perms;
176 }
177
178 /**
179 * Implementation of hook_user()
180 */
181 function usercomment_user($op, &$edit, &$account, $category = NULL) {
182 switch ($op) {
183 case 'form':
184 if ($category == "account") {
185 $form['usercomment_settings'] = array(
186 '#type' => 'fieldset',
187 '#title' => t('Usercomment settings'),
188 '#weight' => 5,
189 '#collapsible' => 1,
190 );
191 foreach (node_get_types() as $node) {
192 if (user_access('approve comments on own '. $node->type .' content', $account )) {
193 $form['usercomment_settings']['usercomment_approve_'. $node->type] = array(
194 '#type' => 'checkbox',
195 '#title' => t('Skip '. $node->type .' approvals'),
196 '#default_value' => isset($edit['usercomment_approve_'. $node->type]) ? $edit['usercomment_approve_'. $node->type] : 1,
197 '#description' => t('If you check this, other non admin users will be able to post comments on your content without prior approval. Admin users, however, will be able to post comments without approval.'),
198 );
199 }
200 }
201 $form['usercomment_settings']['usercomment_get_notifications'] = array(
202 '#type' => 'checkbox',
203 '#title' => t('Receive notification emails'),
204 '#default_value' => isset($edit['usercomment_get_notifications']) ? $edit['usercomment_get_notifications'] : 1,
205 '#description' => t('Check here if you want to receive notification emails about new comments awaiting your approval.'),
206 );
207 }
208 return $form;
209 break;
210 }
211 }
212
213 /**
214 * This function returns the count of unapproved notes for a specified node
215 *
216 * @param $nid - numeric node id
217 */
218 function usercomment_count_unapproved($nid) {
219 $query = "SELECT count(cid) FROM {comments} WHERE status=%d AND nid=%d";
220 $args = array(COMMENT_NOT_PUBLISHED, $nid);
221 $count = db_result(db_query($query, $args));
222 return (int)$count;
223 }
224
225 /**
226 * This function determines whether the specified user needs to approve comments on the specified node type
227 *
228 * @param $nodetype - text string specifying the name of a node type
229 * @param $u - the user object if you have it, else a numeric uid - defaults to current user
230 *
231 * @return TRUE if the user must approve comments on the specified nodetype, FALSE if not
232 */
233 function usercomment_requires_approval($nodetype, $u = -1) {
234 // get the right user object
235 if (is_object($u)) {
236 $user_object =& $u;
237 }
238 elseif (is_numeric($u)) {
239 global $user;
240 if ($u == -1 || $u == $user->uid) {
241 $user_object =& $user;
242 }
243 else {
244 $user_object = user_load(array('uid' => $u));
245 }
246 }
247
248 if (is_object($user_object)) { // check the user's settings
249 $skip_approval_field = "usercomment_approve_{$nodetype}";
250 if (! isset($user_object->$skip_approval_field) || $user_object->$skip_approval_field == 1) {
251 return FALSE; // user either hasn't specified approvals or has said to skip approvals for this node type
252 }
253 else {
254 return TRUE;
255 }
256 }
257 else { // didn't get a valid user object
258 return FALSE;
259 }
260 }
261
262 /**
263 * This function checks access perms
264 */
265 function usercomment_access_check($cid, $op = 'delete', $type = '') {
266 global $user;
267 // admin users already have functionality so no need to show duplicate link
268 if ($op == "delete" && user_access('administer comments')) {
269 return FALSE;
270 }
271 elseif (user_access('post comments with no usercomment checking')) {
272 return FALSE;
273 }
274 else {
275 // get node info and see if node is owned by user or not
276 $comment = _comment_load($cid);
277 $thisnode = node_load($comment->nid);
278 switch ($op) {
279 case 'approve':
280 switch ($type) {
281 case 'update':
282 // if it's not node owner, check if node owner lets others
283 // post without approval
284 if ($thisnode->uid != $user->uid) {
285 static $access;
286 if (! isset($access)) {
287 if (! is_object($thisnode->user) ) {
288 $thisnode->user = user_load( array( "uid" => $thisnode->uid ) );
289 }
290 $access = usercomment_requires_approval($thisnode->type, $thisnode->user);
291 }
292 return $access;
293 }
294 $check = (user_access('approve comments on own'. $thisnode->type .' content') && $thisnode->uid != $user->uid);
295 break;
296 case 'link':
297 default:
298 $check = user_access('approve comments on own '. $thisnode->type .' content');
299 break;
300 }
301 break;
302 case 'delete':
303 default:
304 $check = (user_access('delete comments on own '. $thisnode->type .' content') && $thisnode->uid == $user->uid);
305 break;
306 }
307 if ($check) {
308 return TRUE;
309 }
310 else {
311 return FALSE;
312 }
313 }
314 }
315
316 /**
317 * This function generates the approval page
318 */
319 function usercomment_approve($comment, $node) {
320 $comment->status = COMMENT_PUBLISHED;
321 comment_save((array)$comment);
322
323 $status_msg_php = variable_get('usercomment_msg_approved_php', '');
324 if ($status_msg_php != '') {
325 $status_msg = eval($status_msg_php);
326 }
327 else {
328 $status_msg = variable_get('usercomment_msg_approved', t("Your comment has been approved."));
329 $status_msg = t($status_msg, array(
330 '@poster' => $user->name,
331 '@node_owner' => $node->user->name,
332 '@subject' => $comment->subject,
333 ));
334 }
335
336 drupal_set_message($status_msg);
337 drupal_goto('node/'. $node->nid);
338 }
339
340 /**
341 * This function is a modified copy of comment_render()
342 *
343 * I had to modify b/c comment_render() didn't let me get comments that
344 * weren't published
345 * not ideal, but it works
346 *
347 */
348 function usercomment_comment_render($node) {
349 $content = '';
350 $query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.thread, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d AND c.status = %d';
351 $query_args[] = $node->nid;
352 $query_args[] = COMMENT_NOT_PUBLISHED;
353 $result = db_query($query, $query_args);
354 while ($comment = db_fetch_object($result)) {
355 $comment = drupal_unpack($comment);
356 if (usercomment_access_check($comment->cid)) {
357 $links = usercomment_link('comment', $comment, FALSE);
358 }
359 else {
360 $links = module_invoke_all('link', 'comment', $comment, 0);
361 }
362 foreach (module_implements('link_alter') as $module) {
363 $function = $module .'_link_alter';
364 $function($node, $links);
365 }
366 $content .= theme('comment_view', $comment, $links);
367 }
368 return $content;
369 }
370
371 /**
372 * Generate the usercomment admin settings form
373 */
374 function usercomment_admin_settings() {
375 $form['mail'] = array(
376 '#type' => 'fieldset',
377 '#title' => t('Approval email'),
378 '#description' => t('This page is generated by the usercomment module. The following variables are available for use in email subject/message: @approver, @commenter, @title, @comment, @link, @site, @siteurl'),
379 );
380 $form['mail']['usercomment_mail_subject'] = array(
381 '#type' => 'textfield',
382 '#title' => t('Subject'),
383 '#default_value' => variable_get('usercomment_mail_subject', t('@commenter posted a new comment!')),
384 );
385 $form['mail']['usercomment_mail_message'] = array(
386 '#type' => 'textarea',
387 '#title' => t('Message'),
388 '#rows' => 10,
389 '#default_value' => variable_get('usercomment_mail_message', usercomment_mail_message_default()),
390 );
391 $form['usercomment_text'] = array(
392 '#type' => 'fieldset',
393 '#title' => t('Text options'),
394 );
395 $form['usercomment_text']['usercomment_msg_approval_queue'] = array(
396 '#type' => 'textfield',
397 '#size' => 80,
398 '#title' => t("Approval queue message"),
399 '#description' => t("Enter the status message a user will see after submitting a comment that requires approval prior to being published. The following variables are available for use in the status message: @poster, @node_owner, @subject."),
400 '#default_value' => variable_get('usercomment_msg_approval_queue', t("Your comment will be posted once it's been approved.")),
401 );
402 $msg_php = variable_get('usercomment_msg_approval_queue_php', NULL);
403 $form['usercomment_text']['usercomment_msg_approval_queue_php_fieldset'] = array(
404 '#type' => 'fieldset',
405 '#collapsible' => TRUE,
406 '#collapsed' => is_null($msg_php) ? FALSE : TRUE,
407 '#title' => t('PHP code'),
408 );
409 $form['usercomment_text']['usercomment_msg_approval_queue_php_fieldset']['usercomment_msg_approval_queue_php'] = array(
410 '#type' => 'textarea',
411 '#title' => t('Approval queue message code'),
412 '#default_value' => $msg_php,
413 '#cols' => 60,
414 '#rows' => 6,
415 '#description' => '<p>'. t('Advanced Usage Only: PHP code that returns the text of the status message. Should not include &lt;?php ?&gt; delimiters.') .'</p><p>'. t('Note: if set, this will override any Approval queue message text set above.') .'</p>',
416 );
417
418 $form['usercomment_text']['usercomment_msg_approved'] = array(
419 '#type' => 'textfield',
420 '#size' => 80,
421 '#title' => t("Approved comment message"),
422 '#description' => t("Enter the status message a user will see after approving a comment. The following variables are available for use in the status message: @poster, @node_owner, @subject."),
423 '#default_value' => variable_get('usercomment_msg_approved', t("Your comment has been approved.")),
424 );
425 $msg_php = variable_get('usercomment_msg_approved_php', NULL);
426 $form['usercomment_text']['usercomment_msg_approved_php_fieldset'] = array(
427 '#type' => 'fieldset',
428 '#collapsible' => TRUE,
429 '#collapsed' => is_null($msg_php) ? FALSE : TRUE,
430 '#title' => t('PHP code'),
431 );
432 $form['usercomment_text']['usercomment_msg_approved_php_fieldset']['usercomment_msg_approved_php'] = array(
433 '#type' => 'textarea',
434 '#title' => t('Approved comment message code'),
435 '#default_value' => $msg_php,
436 '#cols' => 60,
437 '#rows' => 6,
438 '#description' => '<p>'. t('Advanced Usage Only: PHP code that returns the text of the status message. Should not include &lt;?php ?&gt; delimiters.') .'</p><p>'. t('Note: if set, this will override any Approval queue message text set above.') .'</p>',
439 );
440 return system_settings_form($form);
441 }
442
443 /**
444 * This function defines the default message sent out by this module
445 */
446 function usercomment_mail_message_default() {
447 return <<<END
448 Hey @approver,
449
450 @commenter posted a new comment that needs to be approved.
451
452 @subject
453 @comment
454
455 You can approve or remove the comment here:
456 @link
457
458 Regards,
459 The @site team
460 END;
461 }
462
463 /**
464 * This function generates the approval form
465 */
466 function usercomment_approval_form($node) {
467 global $user;
468 if ($user->uid == $node->uid && user_access('approve comments on own '. $node->type .' content')) {
469 $content = usercomment_comment_render($node);
470 if (! empty($content)) {
471 return theme('usercomment', $content);
472 }
473 else {
474 return theme('usercomment_empty');
475 }
476 }
477 }
478
479 /**
480 * Implementation of hook_theme().
481 */
482 function usercomment_theme() {
483 return array(
484 'usercomment' => array(
485 'arguments' => array($content),
486 ),
487 );
488 }
489
490 /**
491 * Theme out the usercomment form
492 */
493 function theme_usercomment($content) {
494 $output = '<div class="usercomment">';
495 $output .= theme('box', 'Approve comments', theme('comment_wrapper', $content));
496 $output .= "</div>";
497 return $output;
498 }
499
500 /**
501 * Theme out the empty usercomment form
502 */
503 function theme_usercomment_empty() {
504 $output = '<div class="usercomment_empty">';
505 $output .= t('You have no comments to approve.');
506 $output .= "</div>";
507 return $output;
508 }

  ViewVC Help
Powered by ViewVC 1.1.2