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

Contents of /contributions/modules/og_node_approval/og_node_approval.module

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


Revision 1.2 - (show annotations) (download) (as text)
Fri Dec 5 13:52:31 2008 UTC (11 months, 2 weeks ago) by mradcliffe
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -3 lines
File MIME type: text/x-php
  * i don't need a global $user here, don't use it, probably put it out of habit from some other module.
1 <?php
2 // $Id: og_node_approval.module,v 1.1 2008/11/07 00:50:06 mradcliffe Exp $
3
4 /**
5 * @File
6 * Organic groups node approval
7 *
8 *
9 * Matthew Radcliffe <mradcliffe@kosada.com>
10 *
11 */
12
13
14 /**
15 * hook_help implementation
16 */
17 function og_node_approval_help($path,$arg)
18 {
19 switch($path)
20 {
21 case 'admin/help#og_node_approval':
22 return '<p>'.t('Allows group members to approve or not approve nodes.');
23 }
24 } // function og_node_approval_help
25
26
27 /**
28 * hook_views_api implementation
29 */
30 function og_node_approval_views_api()
31 {
32 return array( 'api' => 2, 'path' => drupal_get_path('module','og_node_approval') );
33 } // function og_node_approval_views_api
34
35
36 /**
37 * hook_menu implementation
38 */
39 function og_node_approval_menu()
40 {
41 $items['admin/settings/og_node_approval'] = array(
42 'title' => t('Organic groups Node Approval'),
43 'description' => t('Change how nodes are approved and which node types are approved.'),
44 'page callback' => 'drupal_get_form',
45 'page arguments' => array('og_node_approval_admin_settings'),
46 'access callback' => 'user_access',
47 'access arguments' => array('administer site configuration'),
48 'type' => MENU_NORMAL_ITEM
49 );
50
51 return $items;
52 } // function og_node_approval_menu
53
54
55 /**
56 * og_node_approval_admin_settings -- admin settings form
57 * @return $form the form to return
58 */
59 function og_node_approval_admin_settings()
60 {
61 $types = node_get_types('types');
62
63 foreach ($types as $type)
64 {
65 $ntypes[$type->type] = t($type->name);
66 }
67
68 print_r(variable_get('approval_node_types',array('Page')));
69
70 $form['og_node_approval']['approval_type'] = array(
71 '#type' => 'radios',
72 '#title' => t('Approval Type'),
73 '#description' => t('Select the type of approval when all members approve a node'),
74 '#options' => array(0 => 'Do Nothing', 1 => 'Publish Node', 2 => 'Use node_status Table'),
75 '#default_value' => variable_get('approval_type',2),
76 );
77
78 $form['og_node_approval']['approval_node_types'] = array(
79 '#type' => 'checkboxes',
80 '#title' => t('Node Types'),
81 '#description' => t('Select the node types that you want to be under group node approval'),
82 '#options' => $ntypes,
83 '#default_value' => variable_get('approval_node_types',array('Page')),
84 );
85
86 return system_settings_form($form);
87 } // function og_node_approval_admin_settings
88
89 /**
90 * og_node_approval_form --
91 */
92 function og_node_approval_form()
93 {
94 $forms['og_node_approval'] = array(
95 'callback' => '_og_node_approval_approve_form',
96 'callback arguments' => array(1),
97 );
98
99 return $forms;
100 }
101
102
103 /**
104 * hook_form_alter implementation -- we need to alter the comment form and hide it so it's not duplicated.
105 */
106 function og_node_approval_form_alter(&$form, $form_state, $form_id)
107 {
108 if ($form_id <> 'comment_form' || arg(0) == 'comment' )
109 return;
110
111 /* we do not need to hide the comment form if we're all approved */
112
113 $node = arg(1);
114 $users = _og_node_approval_get_approvers($node->nid);
115
116 $i = 1;
117 foreach ($users as $usr)
118 {
119 if ($usr->status < 1)
120 {
121 $i = 0;
122 break 1;
123 }
124 }
125 if ($i == 0)
126 return;
127
128 $form['comment']['#required'] = 'FALSE';
129 $form['comment']['#type'] = 'hidden';
130
131 } // function og_node_approval_form_alter
132
133
134 /**
135 * hook_comment implementation
136 */
137 function og_node_approval_comment(&$comment, $op)
138 {
139 $options = array(0 => t('Reject'), 1 => t('Approve'));
140
141 switch($op)
142 {
143 case 'insert':
144 db_query("UPDATE {og_node_approval} SET status = %d WHERE nid = %d and uid = %d",$comment['approval'],$comment['nid'],$comment['uid']);
145 drupal_set_message(t('You have '.$options[$comment['approval']].'d this document.'),'warning');
146
147 // find out if we are all approved.
148 $approve_node = variable_get('approval_type',0);
149 if ( $approve_node > 0 )
150 {
151 $apps = _og_node_approval_get_approvers($comment['nid']);
152
153 $node_appr = 1;
154 foreach ($apps as $my_appr)
155 {
156 if ($my_appr->status <> 1)
157 {
158 $node_appr = 0;
159 break 1;
160 }
161 }
162 if ($approve_node == 2)
163 db_query("UPDATE {node_status} set status = %d WHERE nid = %d",$node_appr,$comment['ni']);
164 else if ($approve_node == 1)
165 db_query("UPDATE {node} set status = %d WHERE nid = %d",$node_appr,$comment['nid']);
166 }
167 break;
168 }
169
170 } // function og_node_approval_comment
171
172
173 /**
174 * hook_nodeapi implementation -- actions performed on nodes
175 * insert -- insert group members into og_node_approval table, and insert into node_status table if used.
176 * delete -- delete above
177 * update -- add/remove group members from og_node_approval based on update of node.
178 * presave -- load up the correct node_status if we're using that table
179 * load -- load node data
180 * view -- prepare data for theme
181 */
182 function og_node_approval_nodeapi(&$node, $op)
183 {
184 $nodetypes = variable_get('approval_node_types',array());
185
186 if ( is_numeric($nodetypes[$node->type]) )
187 return;
188
189 switch ($op)
190 {
191 case 'presave':
192 if (!$node->og_groups)
193 break;
194 if (variable_get('approval_type',2) == 1)
195 {
196 $users = _og_node_approval_get_approvers($node->nid);
197 foreach ($users as $usr)
198 {
199 if ($usr->status > 0)
200 break 2;
201 }
202 $node->status = 0;
203 }
204 break;
205
206 case 'insert':
207 if ( !$node->og_groups )
208 break;
209 $approvers = array();
210 foreach( $node->og_groups as $gid)
211 {
212 $res = db_query("select * from {og_uid} ou where ou.nid = %d and ou.uid not in (select uid from {users_roles} where rid = 3) ",$gid);
213
214 while ( $usr = db_fetch_object($res) )
215 $approvers[$usr->uid] = $usr->uid;
216 }
217
218 foreach ($approvers as $my_uid)
219 db_query("insert into {og_node_approval} values (%d,%d,-1)",$node->nid,$my_uid);
220
221 if (variable_get('approval_type',2) == 2)
222 db_query("insert into {node_status} values (%d,0)",$node->nid);
223
224 break;
225
226 case 'update':
227 if ( !$node->og_groups )
228 break;
229 $approvers = array();
230 foreach( $node->og_groups as $gid)
231 {
232 $res = db_query("select * from {og_uid} ou where ou.nid = %d and ou.uid not in (select uid from {users_roles} where rid = 3) ",$gid);
233
234 while ( $usr = db_fetch_object($res) )
235 $approvers[$usr->uid] = $usr->uid;
236 }
237 _og_node_approval_update($node,$approvers);
238 break;
239
240 case 'load':
241 // okay let's load our data :)
242 $res = db_query("select ona.status, ona.uid from {og_node_approval} ona where ona.nid = %d order by ona.status",$node->nid);
243
244 $approvers = array();
245
246 while ( $usr = db_fetch_object($res) )
247 $approvers[$usr->uid] = $usr->status;
248
249 $my_content = array( 'approvers' => $approvers );
250 unset($approvers);
251 return $my_content; // we really should break then return values...
252 break;
253
254 case 'view':
255 $curstat = -1;
256 $atts = array();
257 $states = array( -1 => 'Pending', 0 => 'Not Approved', 1 => 'Approved');
258 if ( count($node->approvers) == 0 )
259 break;
260 foreach ($node->approvers as $usrid => $status)
261 {
262 $att = user_load($usrid);
263 if ( function_exists(profile_load_profile) )
264 profile_load_profile($att);
265 if ($curstat <> $status)
266 $curstat = $status;
267
268 $atts[$curstat][$usrid]['title'] = $att->name;
269 $atts[$curstat][$usrid]['href'] = 'user/'.$usrid;
270 }
271
272 foreach ($atts as $state => $att)
273 {
274 $node->content[$state] = array (
275 '#value' => '<div><span>'.$states[$state].'</span></div>'.theme('links',$att,$attributes = array('class' => 'links og_node_approval'.$state)),
276 '#weight' => 10
277 );
278 }
279
280 $is_curnode = 0;
281
282 if ( is_numeric(arg(1)) )
283 $pagenid = arg(1);
284
285 if ( $pagenid == $node->nid )
286 $is_curnode = 1;
287
288 /* COMMENT + APPROVE FORM */
289 if (/*! in_array('site administrator', array_values($user->roles))*/ $is_curnode == 1 && (isset($node->content[-1]) || isset($node->content[0])) )
290 {
291 $ona_form = drupal_get_form('_og_node_approval_approve_form');
292 $node->content['comment_form'] = array( '#value' => $ona_form, '#weight' => 15);
293 }
294
295 break;
296
297 case 'delete':
298 db_query("delete from {og_node_approval} where nid = %d",$node->nid);
299 if (variable_get('approval_type',2) == 2)
300 db_query("delete from {node_status} where nid = %d",$node->nid);
301 break;
302 }
303
304 } // function og_node_approval_nodeapi
305
306 function _og_node_approval_get_userlist()
307 {
308 $opt = array();
309
310 $res = db_query("SELECT * FROM {users} u WHERE u.status = 1 AND u.uid NOT IN (SELECT ur.uid from {users_roles} ur where ur.uid = u.uid ) ORDER BY u.name");
311
312 while ( $user = db_fetch_object($res) )
313 $opt[$user->uid] = $user->name;
314
315 return $opt;
316 } // function _og_node_approval_get_userlist
317
318
319 /**
320 * _og_node_approval_update -- do add/remove operations on list of group members abes od n the changes that our node updater made.
321 * @param $node reference to the node
322 * @param $appr new list of approvers
323 */
324 function _og_node_approval_update(&$node,$appr)
325 {
326 /* first let's get our current attendees */
327 $curr_app = _og_node_approval_get_approvers($node->nid);
328
329 if ( is_numeric($curr_app) )
330 {
331 drupal_set_message('ERROR: No nid associated with this document? ','error');
332 return 0;
333 }
334
335 foreach($appr as $my_uid)
336 {
337 if (! isset($curr_app[$my_uid]) ) // we have a new attendee to add
338 $newapp[] = $my_uid;
339 }
340
341 if ( count($curr_app) > 0 )
342 {
343 foreach($curr_app as $my_uid => $blah)
344 {
345 if ( ! isset($appr[$my_uid]) ) // we're removing an attendee
346 $del_app[] = $my_uid;
347 }
348 }
349 $node_approval = variable_get('approval_type',2);
350
351 if ( count($newapp) > 0 )
352 {
353 foreach($newapp as $my_uid)
354 {
355 db_query("insert into {og_node_approval} values (%d,%d,-1)",$node->nid,$my_uid);
356 if ($node_approval == 2)
357 db_query("update {node_status} set status = 0 where nid = %d",$node->nid);
358 else if ($node_approval == 1)
359 db_query("update {node} set status = 0 where nid = %d",$node->nid);
360 }
361 }
362
363 if ( count($del_app) > 0 )
364 {
365 foreach($del_app as $my_uid)
366 {
367 db_query("delete from {og_node_approval} where uid = %d and nid = %d",$my_uid,$node->nid);
368 drupal_set_message('removed uid '.$my_uid.' from node approval','warning');
369
370 if ($node_approval <> 0)
371 {
372 $users = _og_node_approval_get_approvers($node->nid);
373 foreach ($users as $usr)
374 {
375 if ($usr->status < 1)
376 {
377 if ($node_approval == 2)
378 db_query("update {node_status} set status = 0 where nid = %d",$node->nid);
379 else if ($node_approval == 1)
380 db_query("update {node} set status = 0 where nid = %d",$node->nid);
381 }
382 }
383 }
384 }
385 }
386
387 } // function _og_node_approval_update
388
389
390 /**
391 * _og_node_approval_get_approvers -- return a list of group members for a given node
392 * @param $nid node identifier
393 */
394 function _og_node_approval_get_approvers($nid)
395 {
396 if (! is_numeric($nid) )
397 return 0;
398 $res = db_query("select * from {og_node_approval} ona where ona.nid = %d",$nid);
399
400 while ( $usr = db_fetch_object($res) )
401 $tmp[$usr->uid] = $usr;
402 return $tmp;
403
404 } // function _og_node_approval_get_approvers
405
406
407
408 /**
409 * hook_og implementation -- we need to be a good og module and not leave bits and pieces behind when someone is removed from a group. Also add new group members to the approver list of current nodes.
410 * user insert -- when a user is added to a group
411 * user delete -- when a user is removed from a group
412 */
413 function og_node_approval_og($op, $gid, $uid, $args)
414 {
415 $user = user_load($uid);
416
417 switch($op)
418 {
419 case 'user insert':
420 // we need to get all events for this group after today.
421 $docs = _og_node_approval_get_current($gid);
422 foreach ( $docs as $doc )
423 {
424 db_query("insert into {og_node_approval} values (%d,%d,-1)",$doc->nid,$uid);
425 }
426 break;
427 case 'user delete':
428 // we need to get all events for this group after today.
429 $docs = _og_node_approval_get_current($gid);
430 foreach ( $docs as $doc )
431 {
432 db_query("delete from {og_node_approval} where nid = %d and uid = %d",$doc->nid,$uid);
433 }
434 break;
435 }
436
437 } // function og_node_approval_og
438
439
440 /**
441 * _og_node_approval_get_current -- get group posts for a particular group
442 * @param $gid the node identifier of the group
443 * @return $tmp return an array of node objects
444 */
445 function _og_node_approval_get_current($gid)
446 {
447 $tmp = array();
448 if ( !is_numeric($gid) )
449 return 0;
450
451 $ntypes = variable_get('approval_node_types',array());
452
453 if ( variable_get('approval_type',2) == 1 ) // only return unpublished nodes waiting for review
454 $qry = 'select n.nid from {node} n INNER JOIN {og_ancestry} oa ON n.nid = oa.nid AND n.status <> 1 AND oa.group_nid = %d';
455 else if ( variable_get('approval_type',2) == 2 ) // only return unapproved nodes
456 $qry = 'select n.nid from {node} n INNER JOIN {og_ancestry} oa ON n.nid = oa.nid AND oa.group_nid = %d INNER JOIN {node_status} ns ON n.nid = ns.nid AND ns.status <> 1';
457 else
458 $qry = 'select n.nid from {node} n INNER JOIN {og_ancestry} oa ON n.nid = oa.nid AND oa.group_nid = %d AND n.status = 1';
459
460 $res = db_query($qry,$gid);
461
462 while ( $node = db_fetch_object($res) )
463 {
464 if ( isset($ntypes[$node->type]) ) // let's make sure we're a valid node type
465 $tmp[] = $node;
466 }
467
468 return $tmp;
469 } // function _og_node_approval_get_current
470
471
472 /**
473 * og_node_approval_approve -- set approval status on a node and update node status if all approvers have approved the node.
474 * @param $node a node object
475 * @param $status the status being set
476 */
477 function og_node_approval_approve($node,$status = 1)
478 {
479 global $user;
480 $uid = $user->uid;
481
482 $appr = _og_node_approval_get_approvers($node->nid);
483
484 if ( !isset($appr[$uid]) )
485 {
486 drupal_set_message('You do not have access to approve this document.','error');
487 return '';
488 }
489
490 $appr[$uid]->status = $status;
491
492 db_query("update {og_node_approval} set status = %d where nid = %d and uid = %d",$status,$node->nid,$uid);
493 drupal_set_message(t('You have reviewed this document. Thank you.'),'info');
494 $appr[$uid]->status = $status;
495 // find out if we are all approved.
496 $approve_node = variable_get('approval_type',2);
497 if ( $approve_node > 0 )
498 {
499 $apps = _og_node_approval_get_approvers($node->nid);
500
501 $node_appr = 1;
502 foreach ($apps as $my_appr)
503 {
504 if ($my_appr->status <> 1)
505 {
506 $node_appr = 0;
507 break 1;
508 }
509 }
510 if ($approve_node == 2)
511 db_query("UPDATE {node_status} set status = %d WHERE nid = %d",$node_appr,$node->nid);
512 else if ($approve_node == 1)
513 db_query("UPDATE {node} set status = %d WHERE nid = %d",$node_appr,$node->nid);
514 }
515
516 return '';
517 } // function og_node_approval_approve
518
519
520 /**
521 * og_node_approval_approve_access -- access callback to see if we have appropriate access to approve
522 * @param $node the node object
523 */
524 function og_node_approval_approve_access($node)
525 {
526 global $user;
527
528 if ( $node->type <> 'doc' )
529 return FALSE;
530
531 $appr = _og_node_approval_get_approvers($node->nid);
532
533 // print t('type = %ntype and user app status = %status',array('%ntype' => $node->type,'%status' => $appr[$user->uid]->status));
534
535 if ( !isset($appr[$user->uid]) || $appr[$user->uid]->status == 1 )
536 return FALSE;
537
538 return TRUE;
539 } // function og_node_approval_access
540
541
542 /**
543 * _og_node_approval_approve_form -- the node approval form
544 * @return $form a proper drupal form array
545 */
546 function _og_node_approval_approve_form()
547 {
548 $nid = arg(1);
549
550 $form['og_node_approval'] = array(
551 '#type' => 'fieldset',
552 '#title' => t('Review This Node'),
553 '#description' => t('To Approve node, select Approve, then click submit.<br> To Reject node, select Reject, provide comments, then click submit.'),
554 '#collapsed' => FALSE,
555 '#collapsible' => TRUE,
556 '#weight' => -10
557 );
558
559 $form['og_node_approval']['approval_status'] = array(
560 '#type' => 'select',
561 '#title' => t('My Approval'),
562 '#description' => t(''),
563 '#options' => array( 0 => 'Reject', 1 => 'Approve'),
564 '#required' => TRUE,
565 );
566
567 $form['og_node_approval']['comment'] = array(
568 '#type' => 'textarea',
569 '#title' => t('Comments'),
570 '#description' => t(''),
571 '#default_value' => '',
572 '#required' => FALSE,
573 );
574
575 $form['submit'] = array(
576 '#type' => 'submit',
577 '#value' => t('Submit'),
578 '#weight' => 0,
579 );
580
581 $form['og_node_approval']['nid'] = array(
582 '#type' => 'hidden',
583 '#value' => $nid,
584 );
585
586 $form['#method'] = 'post';
587 $form['#submit'][] = '_og_node_approval_approve_form_submit';
588 $form['#validate'] = array('_og_node_approval_approve_form_validate');
589
590 return $form;
591 } // function _og_node_approval_approve_form
592
593
594 /**
595 * _og_node_approval_approve_form_submit -- the submit function for the _og_node_approval_approve_form form
596 * @param $form reference to the form
597 * @param $form_state reference to the form_state
598 */
599 function _og_node_approval_approve_form_submit(&$form, &$form_state)
600 {
601 global $user;
602 $aprstatus = array( 0 => 'Reject', 1 => 'Approve');
603 $node = node_load($form_state['values']['nid']);
604
605 if ( ! isset($node->nid) )
606 drupal_set_message('Cannot find this node! nid = '.$form_state['values']['nid'],'error');
607
608 og_node_approval_approve($node,$form_state['values']['approval_status']);
609
610 /* We get to redo comment forms, yay */
611 if ( $form_state['values']['comment'] <> '' )
612 {
613 $subj = $aprstatus[$form_state['values']['approval_status']];
614
615 $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $form_state['values']['nid']));
616 $max = rtrim($max,'/');
617
618 $thread = int2vancode(vancode2int($max) + 1) .'/';
619
620 db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, 0, %d, '%s', '%s', %d, 'none', %d, %d, '%s', '%s', '', '')", $form_state['values']['nid'], $user->uid, $subj, $form_state['values']['comment'],1,time(),0,$thread,$user->name);
621
622 $my_cid = db_last_insert_id('comments', 'cid');
623 // Add an entry to the watchdog log.
624 watchdog('content', 'Comment: added %subject.', array('%subject' => $subj), WATCHDOG_NOTICE, l(t('view'), 'node/'. $form_state['values']['nid'], array('fragment' => 'comment-'. $my_cid)));
625 }
626
627 } // function _og_node_approval_approve_form_submit
628
629
630 /**
631 * _og_node_approval_approve_form_validate -- the validation function for the _og_node_approval_approve form
632 * @param $form form reference
633 * @param $form_state form_state reference
634 */
635 function _og_node_approval_approve_form_validate(&$form, &$form_state)
636 {
637 if ( ($form_state['values']['approval_status'] == 0) && strlen($form_state['values']['comment']) < 10 )
638 form_set_error('mycomment',t('You must enter comments if you are rejecting a node.'));
639 } // function _og_node_approval_approve_form_validate

  ViewVC Help
Powered by ViewVC 1.1.2