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

Contents of /contributions/modules/og_public_access/og_public_access.module

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


Revision 1.4 - (show annotations) (download) (as text)
Tue Feb 6 20:17:32 2007 UTC (2 years, 9 months ago) by davidlesieur
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +28 -3 lines
File MIME type: text/x-php
Allow to exclude groups from moderation
1 <?php
2 // $Id: og_public_access.module,v 1.3 2007/02/05 03:26:50 davidlesieur Exp $
3
4 /**
5 * Implementation of hook_help().
6 */
7 function og_public_access_help($section) {
8 switch ($section) {
9 case 'admin/modules#description':
10 return t('Allows group administrators decide what posts are publicly accessible outside their groups.');
11 }
12 }
13
14 /**
15 * Implementation of hook_perm().
16 */
17 function og_public_access_perm() {
18 return array('moderate public access to content in groups administered by self');
19 }
20
21 /**
22 * Implementation of hook_menu().
23 */
24 function og_public_access_menu($may_cache) {
25 $items = array();
26
27 if ($may_cache) {
28 $items[] = array(
29 'path' => 'admin/og_public_access/moderate',
30 'callback' => 'og_public_access_moderate',
31 'access' => user_access('moderate public access to content in groups administered by self'),
32 'title' => t('moderate public access to group content'),
33 );
34 }
35
36 return $items;
37 }
38
39 /**
40 * Implementation of hook_settings().
41 */
42 function og_public_access_settings() {
43 $form['og_public_access_default_option'] = array(
44 '#type' => 'radios',
45 '#title' => t('Which action option should be selected by default?'),
46 '#options' => array(
47 'approve' => t('approve'),
48 'reject' => t('reject'),
49 'none' => t('no action'),
50 ),
51 '#default_value' => variable_get('og_public_access_default_option', 'none'),
52 );
53 $form['og_public_access_nodes_per_page'] = array(
54 '#type' => 'select',
55 '#title' => t('How many nodes should be displayed per page?'),
56 '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 50, 75, 100, 150, 200)),
57 '#default_value' => variable_get('og_public_access_nodes_per_page', 10),
58 );
59 $form['og_public_access_excluded'] = array(
60 '#type' => 'select',
61 '#title' => t('Should any group be ignored by public access moderation?'),
62 '#description' => t('Private content in all groups will appear in the public access moderation queue, except for groups selected in this list. If content is private in a selected group, it will remain private and never get into the moderation queue.'),
63 '#options' => _og_public_access_get_all_groups(),
64 '#multiple' => TRUE,
65 '#default_value' => variable_get('og_public_access_excluded', array()),
66 );
67
68 return $form;
69 }
70
71 /**
72 * Menu callback. Provide a queue with the latest posts that are private in the
73 * groups where the current user is an admin.
74 */
75 function og_public_access_moderate() {
76 $output = '';
77
78 _og_public_access_check_settings();
79
80 // Retrieve groups where user is admin
81 $groups = _og_public_access_get_admin_subscriptions();
82 if (count($groups) == 0) {
83 return '<p>'. t('You are not an administrator on any group.') .'</p>';
84 }
85 $gids = array_keys($groups);
86 $excluded = variable_get('og_public_access_excluded', array());
87 $gids = array_diff($gids, $excluded);
88 if (count($gids) == 0) {
89 return '<p>'. t('None of your groups needs moderation.') .'</p>';
90 }
91
92 // Retrieve nodes that are not publicly accessible in those groups. Using a
93 // temporary table avoids complicated joins.
94 db_query_temporary("SELECT n.nid, a.gid, n.created FROM {node} n INNER JOIN {node_access} a ON a.nid = n.nid WHERE n.status = 1 AND a.realm = 'og_subscriber' AND a.gid IN (". implode(',', $gids) .") AND a.grant_view = 1 AND NOT EXISTS (SELECT * FROM {node_access} na WHERE na.nid = n.nid AND na.realm = 'og_all' AND na.grant_view = 1)", 'temp_og_public_access');
95 foreach ($gids as $gid) {
96 // Ignore groups where the nodes are already publicly accessible
97 db_query("DELETE n FROM temp_og_public_access n, {node_access} na WHERE na.nid = n.nid AND n.gid = %d AND na.realm = 'og_public' AND na.grant_view = %d", $gid, $gid);
98 }
99 $node_results = pager_query(db_rewrite_sql("SELECT DISTINCT(n.nid) FROM temp_og_public_access n ORDER BY n.created DESC"), variable_get('og_public_access_nodes_per_page', 10));
100
101 // Build form
102 $form = array();
103 while ($r = db_fetch_object($node_results)) {
104 // Loading the other columns in this separate query for them not to be taken
105 // into account by the pager.
106 $results = db_query("SELECT * FROM temp_og_public_access WHERE nid = %d", $r->nid);
107 while ($r = db_fetch_object($results)) {
108 og_public_access_form_update($form, $r->nid, $r->gid, $groups);
109 }
110 }
111 $output = drupal_get_form('og_public_access_form', $form);
112 $output .= theme('pager');
113 return $output;
114 }
115
116 /**
117 * Update form for editing public access for node $nid in group $gid.
118 */
119 function og_public_access_form_update(&$form, $nid, $gid, $groups) {
120 $form['#theme'] = 'og_public_access_form';
121 $form['#tree'] = TRUE;
122
123 $node = node_load($nid);
124 if ($node && !og_is_group_type($node->type)) {
125 $teaser = node_view($node, TRUE, FALSE, FALSE);
126 $form[$node->nid]['preview'] = array(
127 '#type' => 'markup',
128 '#value' => $teaser
129 );
130 $form[$node->nid]['title'] = array(
131 '#type' => 'value',
132 '#value' => check_plain($node->title),
133 );
134 $form[$node->nid]['type'] = array(
135 '#type' => 'value',
136 '#value' => node_get_name($node),
137 );
138 $form[$node->nid][$gid]['ops'] = array(
139 '#type' => 'radios',
140 '#title' => $groups[$gid]['title'],
141 '#options' => array(
142 'approve' => t('approve'),
143 'reject' => t('reject'),
144 'none' => t('no action'),
145 ),
146 '#default_value' => variable_get('og_public_access_default_option', 'none'),
147 );
148 $form[$node->nid][$gid]['group'] = array(
149 '#type' => 'value',
150 '#value' => check_plain($groups[$gid]['title']),
151 );
152 $form['submit'] = array(
153 '#type' => 'submit',
154 '#value' => t('Submit'),
155 );
156 }
157 }
158
159 /**
160 * Form submit handler, which approves or rejects the node.
161 */
162 function og_public_access_form_submit($form_id, $form_values) {
163 foreach ($form_values as $nid => $node_values) {
164 if (is_numeric($nid)) {
165 foreach ($node_values as $gid => $values) {
166 if (is_numeric($gid)) {
167 switch ($values['ops']) {
168 case 'approve':
169 og_public_access_grant($nid, $gid);
170 drupal_set_message(t('The %type %title has been approved for public viewing in group %group.', array('%title' => theme('placeholder', $node_values['title']), '%type' => $node_values['type'], '%group' => $values['group'])));
171 break;
172
173 case 'reject':
174 og_public_access_reject($nid, $gid);
175 drupal_set_message(t('The %type %title has been rejected from group %group.', array('%title' => theme('placeholder', $node_values['title']), '%type' => $node_values['type'], '%group' => $values['group'])));
176 break;
177 }
178 }
179 }
180 }
181 }
182 }
183
184 /**
185 * Updates the node_access table to grant public access to node $nid in group $gid.
186 */
187 function og_public_access_grant($nid, $gid) {
188 db_query("DELETE FROM {node_access} WHERE nid = %d AND gid = %d AND realm = 'og_subscriber'", $nid, $gid);
189 db_query("INSERT INTO {node_access} (nid, gid, realm, grant_view) VALUES (%d, 0, 'og_public', %d)", $nid, $gid);
190 db_query("INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, 'og_subscriber', 1, 1, 1)", $nid, $gid);
191 cache_clear_all();
192 }
193
194 /**
195 * Updates the node_access table to remove node $nid from group $gid.
196 */
197 function og_public_access_reject($nid, $gid) {
198 db_query("DELETE FROM {node_access} WHERE nid = %d AND gid = %d AND realm = 'og_subscriber'", $nid, $gid);
199 cache_clear_all();
200
201 // TODO: what to do when node is neither in any group nor accessible in realm og_all?
202 }
203
204 /**
205 * Return group subscriptions where the current user is a group admin.
206 */
207 function _og_public_access_get_admin_subscriptions() {
208 global $user;
209 $subscriptions = array();
210 foreach ($user->og_groups as $gid => $subscription) {
211 if ($subscription['is_admin']) {
212 $subscriptions[$gid] = $subscription;
213 }
214 }
215 return $subscriptions;
216 }
217
218 /**
219 * Check whether visibility can effectively be moderated.
220 */
221 function _og_public_access_check_settings() {
222 if (variable_get('og_visibility', 0)) {
223 drupal_set_message(t('For group administrators to fully control the public visibility of content posted into their groups, the "%vis" option must be selected in the %settings.', array('%vis' => t('Visible only within the targeted groups'), '%settings' => l(t('og settings page'), 'admin/settings/og'))), 'error');
224 }
225 }
226
227 /**
228 * Utility function to retrieve all available groups.
229 */
230 function _og_public_access_get_all_groups() {
231 $groups = array();
232 $results = db_query(db_rewrite_sql("SELECT og.nid, n.title FROM {og} og INNER JOIN {node} n ON og.nid = n.nid WHERE n.status = 1 ORDER BY n.title ASC", 'og', 'nid'));
233 while ($g = db_fetch_object($results)) {
234 $groups[$g->nid] = $g->title;
235 }
236 return $groups;
237 }
238
239 /**
240 * Themes the public access control form.
241 */
242 function theme_og_public_access_form(&$form) {
243 $headers = array(
244 t('content'),
245 t('operations'),
246 );
247 $rows = array();
248 foreach (element_children($form) as $nid) {
249 // Only do this for nodes; not the submit button.
250 if (is_numeric($nid)) {
251 $row = array();
252 $row[] = array(
253 'data' => form_render($form[$nid]['preview']),
254 'style' => 'vertical-align:top;',
255 );
256 $group_ops = '';
257 foreach (element_children($form[$nid]) as $gid) {
258 // Only do this for group data
259 if (is_numeric($gid)) {
260 $group_ops .= form_render($form[$nid][$gid]['ops']);
261 }
262 }
263 $row[] = array(
264 'data' => $group_ops,
265 'style' => 'vertical-align:top;'
266 );
267 $rows[] = $row;
268 }
269 }
270 $output = theme('table', $headers, $rows);
271 $output .= form_render($form);
272
273 return $output;
274 }
275

  ViewVC Help
Powered by ViewVC 1.1.2