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

Contents of /contributions/modules/custom_review/custom_review.module

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


Revision 1.8 - (show annotations) (download) (as text)
Sun Oct 12 20:50:51 2008 UTC (13 months, 2 weeks ago) by striky2
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +20 -16 lines
File MIME type: text/x-php
update for hook_blocks
1 <?php
2 // $Id$
3
4 /**
5 * @file custom_review.module
6 *
7 * This module provides a way to make reviews (which are actually cck-types we can call reviewers) accessible from
8 * a block on other nodes.
9 *
10 */
11
12
13 /**
14 * Caches the array for binding reviewer_node_types and reviewed_node_types.
15 */
16 global $_custom_review_reviewers;
17
18 /****************************/
19 /*** Module hooks section ***/
20 /****************************/
21
22 /**
23 * Implementation of hook_help().
24 */
25 function custom_review_help($path, $arg) {
26 switch ($path) {
27 case 'admin/help#custom_review':
28 case 'admin/modules#description':
29 return t('allow people to do custom typed (cck-types) review on nodes');
30 }
31 }
32 /**
33 * Implementation of hook_menu().
34 */
35 function custom_review_menu() {
36 $items['admin/settings/custom_review'] = array(
37 'title' => 'Custom review',
38 'description' => 'Custom review bind',
39 'page callback' => 'drupal_get_form',
40 'page arguments' => array('custom_review_binding_form'),
41 'access arguments' => user_access('administer custom review')
42 );
43
44 return $items;
45 }
46 /**
47 * Implementation of hook_nodeapi().
48 */
49 function custom_review_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
50 global $_custom_review_reviewers;
51 global $user;
52
53 if (empty($_custom_review_reviewers)) {
54 $_custom_review_reviewers = custom_review_get_reviewers();
55 }
56
57 if ($op == 'delete') {
58 //be responsible, if a node reviewee is deleted, also delete the reviews!
59 db_query("DELETE FROM {custom_reviews} WHERE nid = %d", $node->nid);
60 }
61
62 if (array_key_exists($node->type, $_custom_review_reviewers)) {
63 switch ($op) {
64 case 'delete':
65 if (is_numeric($node->nid)) {
66 //here nid is actually a rid.
67 $reviewed_id = custom_review_get_reviewed_node($node->nid);
68 db_query("DELETE FROM {custom_reviews} WHERE rid = %d", $node->nid);
69 drupal_goto('node/'. $reviewed_id);
70 }
71 break;
72 case 'insert':
73 db_query("INSERT INTO {custom_reviews} (rid, nid) VALUES (%d, %d)", $node->nid, arg(3));
74 drupal_set_message(t('Your review has been posted'));
75 drupal_goto('node/'. arg(3));
76 break;
77 case 'update':
78 $reviewed_id = custom_review_get_reviewed_node($node->nid);
79 drupal_goto('node/'. $reviewed_id);
80 break;
81 case 'view':
82 //find node reviewed nid
83 global $base_url;
84 drupal_add_css(drupal_get_path('module', 'custom_review') .'/custom_review.css');
85 $node_reviewed = node_load(custom_review_get_reviewed_node($node->nid));
86 $node_reviewed = node_build_content($node_reviewed, TRUE, $page);
87 //dump($node_reviewed);
88 $node->content['reviewed_node'] = array(
89 '#prefix' => "<div id='reviewed-node-". $node_reviewed->nid ."' class='reviewed-node'>
90 <a href='$base_url/node/$node_reviewed->nid' class='reviewed-link'>",
91 '#suffix' => "</a></div>",
92 '#value' => l($node_reviewed->title, 'node/'. $node_reviewed->nid) . drupal_render($node_reviewed->content),
93 '#weight' => -5,
94 );
95 }
96 }
97 }
98 /**
99 * Implementation of hook_perm()
100 *
101 * There is no need of create custom review perm, because of the create content (node type)
102 * already existing perm
103 */
104 function custom_review_perm() {
105 return array('administer custom review');
106 }
107 /**
108 * Implementation of hook_block
109 */
110 function custom_review_block() {
111 global $user;
112
113 $blocks['actions'] = array(
114 'info' => t('Custom review: links to reviews'),
115 'description' => t('My reviews'),
116 'cache' => BLOCK_NO_CACHE // Not worth caching.
117 );
118
119 return $blocks;
120 }
121 /**
122 * Implementation of hook_block
123 */
124 function custom_review_block_actions() {
125 global $user;
126 if ($menu = menu_tree()) {
127 $block['subject'] = '';
128 $block['content'] = custom_review_build_special_form();
129 return $block;
130 }
131 }
132
133 /**
134 * Implementation of hook_form_alter()
135 *
136 * @param $form_id
137 * @param &$form
138 */
139 function custom_review_form_alter(&$form, &$form_state, $form_id) {
140 global $_custom_review_reviewers;
141
142 if (empty($_custom_review_reviewers)) {
143 $_custom_review_reviewers = custom_review_get_reviewers();
144 }
145
146 $short_type = substr($form_id, 0, -10); // 'short_type_node_form' => 'short_type'
147 if (array_key_exists($short_type, $_custom_review_reviewers)) {
148 //dump($form_state);
149
150 //if add form: take the argument url passed from reviewed node
151 //if edit form: take the reviewed from the review id
152 if (arg(1) == 'add') {
153 $reviewed_id = arg(3);
154 }
155 else if (arg(2) == 'edit') {
156 $reviewed_id = custom_review_get_reviewed_node($form['nid']['#value']);
157 }
158
159 $node = node_load($reviewed_id);
160 $node = node_build_content($node, TRUE, FALSE);
161 $content = drupal_render($node->content);
162 $form['#validate'][] = 'custom_review_form_validate';
163 $form['custom_review_node'] = array(
164 '#type' => 'fieldset',
165 '#weight' => -10,
166 '#title' => t("Currently reviewing: ") . t($node->title),
167 '#collapsible' => TRUE,
168 '#collapsed' => TRUE,
169 );
170 $form['custom_review_node']['node_reviewed'] = array(
171 '#value' => $content,
172 );
173 $form['cancel'] = array(
174 '#type' => 'submit',
175 '#value' => t('Cancel'),
176 '#weight' => -9,
177 );
178 }
179 }
180 /**
181 * Implementation of hook_form_validate()
182 *
183 * @param $form_id
184 * @param $form_values
185 */
186 function custom_review_form_validate($form, &$form_state) {
187 // Go back to the initial node if user_cancel
188 global $_custom_review_reviewers;
189
190 if (empty($_custom_review_reviewers)) {
191 $_custom_review_reviewers = custom_review_get_reviewers();
192 }
193
194 $short_type = substr($form_id, 0, -10); // 'short_type_node_form' => 'short_type'
195 if (array_key_exists($short_type, $_custom_review_reviewers) && is_numeric(arg(3))) {
196 if ($form_states['values']['op'] == t('Cancel')) {
197 drupal_set_message(t('Cancel review operation'));
198 drupal_goto('node/'. arg(3));
199 }
200 }
201 }
202
203
204
205 /****************************/
206 /*** Module Forms section ***/
207 /****************************/
208
209 /**
210 * Creates the form to be used in the block "actions"
211 *
212 * @return $form
213 */
214 function custom_review_block_action_form(&$form_state) {
215 $form = array();
216
217 $form['custom_reviews'] = array(
218 '#type' => fieldset,
219 '#title' => t('Custom reviews'),
220 '#collapsible' => TRUE,
221 '#collapsed' => FALSE,
222 );
223 if (!empty($options['add'])) {
224 $form['custom_reviews']['add_reviews'] = array(
225 '#type' => 'select',
226 '#options' => $form_state['add'],
227 );
228 $form['custom_reviews']['add_submit'] = array(
229 '#type' => 'submit',
230 '#value' => t('Add review'),
231 );
232 }
233 if (!empty($options['edit'])) {
234 $form['custom_reviews']['edit_reviews'] = array(
235 '#type' => 'select',
236 '#options' => $form_state['edit'],
237 );
238 $form['custom_reviews']['edit_submit'] = array(
239 '#type' => 'submit',
240 '#value' => t('Edit review'),
241 );
242 }
243
244 return $form;
245 }
246 /**
247 * On submission of the form block_action...
248 *
249 * @param unknown_type $form_values
250 */
251 function custom_review_block_action_form_submit($form, &$form_state) {
252 global $user;
253
254 if (!$user->uid) {
255 drupal_goto('user/register');
256 }
257 if (($form_state['values']['op'] == t('Add review')) && user_access("create ". $form_state['add_reviews'] ." content")) {
258 //the user is trying to add a custom review
259 drupal_goto('node/add/'. $form_state['values']['add_reviews'] .'/'. arg(1));
260 }
261 else if (($form_state['values'] == t('Edit review')) && user_access("edit own ". _custom_review_node_type($form_state['values']['edit_reviews']) ." content")) {
262 //the user is trying to edit one of its custom review
263 drupal_goto('node/'. $form_state['values']['edit_reviews'] .'/edit/'. arg(1));
264 }
265 }
266 /**
267 * Administration form to bind reviewers node types to reviewed node types...
268 *
269 * @param $form
270 */
271 function custom_review_binding_form(&$form_state) {
272 global $_custom_review_reviewers;
273
274 if (empty($_custom_review_reviewers)) {
275 $_custom_review_reviewers = custom_review_get_reviewers();
276 }
277
278 $form['#prefix'] = "This form allows you to plug some node-types as node - reviewers for other node - types.<br/>
279 Usually, you need first to create specific CCK - types, and then to plug these types onto some other nodes, using
280 this form.";
281
282 foreach (node_get_types('names') as $key => $val) {
283 $form['custom_review_bind_'. $key] = array(
284 '#type' => 'fieldset',
285 '#title' => t('Specify reviewed types for the reviewer tool: '. $val),
286 '#collapsible' => TRUE,
287 '#collapsed' => !array_key_exists($key, $_custom_review_reviewers),
288 );
289 $form['custom_review_bind_'. $key]['custom_review_reviewer_bind_'. $key] = array(
290 '#prefix' => "<div style='float:left;'>",
291 '#type' => 'checkbox',
292 '#title' => 'Use reviewer: '. $val,
293 '#default_value' => array_key_exists($key, $_custom_review_reviewers),
294 '#suffix' => "</div>",
295 );
296
297 $custom_reviewed = array();
298 if ($_custom_review_reviewers[$key]) {
299 foreach (node_get_types('names') as $key_node_type => $val_node_type) {
300 if (in_array($key_node_type, $_custom_review_reviewers[$key])) {
301 $custom_reviewed[$key_node_type] = $key_node_type;
302 }
303 else {
304 $custom_reviewed[$key_node_type] = 0;
305 }
306 }
307 }
308
309 $form['custom_review_bind_'. $key]['custom_review_reviewed_bind_'. $key] = array(
310 '#prefix' => "<div style='float:left;margin-left:20px;'>",
311 '#type' => 'checkboxes',
312 '#title' => 'To review',
313 '#default_value' => $custom_reviewed,
314 '#options' => node_get_types('names'),
315 '#suffix' => "</div>",
316 );
317 $form['custom_review_bind_'. $key]['submit_'. $key] = array(
318 '#prefix' => "<div style='float:left;margin-left:50px;margin-top:30px;'>",
319 '#name' => 'submit',
320 '#type' => 'submit',
321 '#value' => t('Submit binding'),
322 '#suffix' => "</div>",
323 );
324 }
325
326 $form['submit'] = array(
327 '#name' => 'submit',
328 '#type' => 'submit',
329 '#value' => t('Submit binding') ,
330 );
331
332 return $form;
333 }
334 /**
335 * Submit action for administrators
336 *
337 * @param $form_values
338 */
339 function custom_review_binding_form_submit($form, &$form_state) {
340 global $_custom_review_reviewers;
341
342 if (empty($_custom_review_reviewers)) {
343 $_custom_review_reviewers = custom_review_get_reviewers();
344 }
345
346 $created_reviews = array();
347 if (empty($form_state['values'])) {
348
349 }
350 else {
351 foreach ($form_state['values'] as $key_form_value => $val_form_value) {
352 $reviewed_array_name = '';
353 $reviewer_name = '';
354 if (strstr($key_form_value, 'custom_review_reviewer_bind_')) {
355 //it's a reviewer
356 $reviewed_array_name = str_replace('_reviewer_bind', '_reviewed_bind', $key_form_value);
357 $reviewer_name = substr($key_form_value, 28);
358 }
359 if ($reviewer_name && $val_form_value) {
360 //it's a reviewer not null
361 foreach ($form_state['values'][$reviewed_array_name] as $key_reviewed_type => $val_reviewed_type) {
362 if ($val_reviewed_type) {
363 //its a reviewer-reviewed not null
364 //check if insertion is required
365 if (array_key_exists($reviewer_name, $_custom_review_reviewers) && in_array($key_reviewed_type, $_custom_review_reviewers[$reviewer_name])) {
366 //if reviewer_node_type/reviewed_node_type is already in the database ($custom_review) :
367 //do nothing
368 }
369 else {
370 //insert only one item
371 $sql = db_query("INSERT INTO {custom_review_types} VALUES ('%s','%s')", $reviewer_name, $key_reviewed_type);
372 }
373 }
374 else {
375 //its a reviewer not null && a reviewed null
376 //check if deletion is required
377 if (array_key_exists($reviewer_name, $_custom_review_reviewers) && in_array($key_reviewed_type, $_custom_review_reviewers[$reviewer_name])) {
378 //if reviewer_node_type/reviewed_node_type is in the database ($custom_review) :
379 //delete only one item
380 $sql = db_query("DELETE FROM {custom_review_types} WHERE reviewer_node_type = '%s'
381 AND reviewed_node_type = '%s'", $reviewer_name, $key_reviewed_type);
382 }
383 }
384 }
385 }
386 else if ($reviewer_name && !$val_val_value) {
387 //it's a reviewer null
388 //check if deletion is required
389 if (array_key_exists($reviewer_name, $_custom_review_reviewers)) {
390 //if reviewer_node_type is in the database ($custom_review) : delete (may delete many items)
391 $sql = db_query("DELETE FROM {custom_review_types} WHERE reviewer_node_type = '%s'", $reviewer_name);
392 }
393 }
394 }
395 }
396 drupal_set_message('Modifications done');
397 }
398
399
400 /****************************/
401 /**** Business functions ****/
402 /****************************/
403
404 /**
405 * Get the node being reviewed
406 *
407 * @param $node
408 * the review
409 * @return nid of the node being reviewed
410 */
411 function custom_review_get_reviewed_node($node) {
412 if (is_object($node)) {
413 $node = $node->nid;
414 }
415 $sql = db_query("SELECT nid FROM {custom_reviews} WHERE rid = %d", $node);
416 $result = db_fetch_object($sql);
417
418 return $result->nid;
419 }
420 /**
421 * Get all the reviewers names
422 *
423 * @return $_custom_review_reviewers
424 * $_custom_review_reviewers is also a global var which is written by this function
425 */
426 function custom_review_get_reviewers() {
427 $sql = db_query("SELECT * FROM {custom_review_types} ORDER BY reviewer_node_type");
428 $_custom_review_reviewers = array();
429 while ($result = db_fetch_array($sql)) {
430 $_custom_review_reviewers[$result['reviewer_node_type']][] = $result['reviewed_node_type'];
431 }
432 return $_custom_review_reviewers;
433 }
434 /**
435 * Give all the possible reviewer types on a node,
436 * and also declares if the user has already used the reviewer type for making his review
437 *
438 * @param $nid
439 * @param $uid
440 * [optional]: if not specified, take the current's user account
441 */
442 function custom_review_user_can_review_node($nid, $uid = 0) {
443 if (!$uid) {
444 global $user;
445 $uid = $user->uid;
446 }
447 //get all the possible reviewer types
448
449 $reviewers = custom_review_node_get_reviewers($nid);
450 if (is_array($reviewers) && !empty($reviewers)) {
451 foreach ($reviewers as $key => $val) {
452 $sql = "SELECT CR.rid as rid, N.title as title FROM {node} N, {custom_reviews} CR WHERE
453 CR.nid = %d AND CR.rid = N.nid AND N.uid = %d AND N.type = '%s'";
454 $result = db_fetch_object(db_query($sql, $nid, $uid, $val));
455 $reviewers[$key] = $result;
456 }
457 }
458
459 return $reviewers;
460 }
461 /**
462 * Give all the possible reviewers types of a node (or node type)
463 *
464 * @param $node
465 * @return an array of possible reviewers
466 */
467 function custom_review_node_get_reviewers($node) {
468 if (is_numeric($node)) {
469 $sql = db_query("SELECT custom_review_types.reviewer_node_type FROM {custom_review_types}, {node}
470 WHERE node.nid = %d AND node.type = custom_review_types.reviewed_node_type", $node);
471 }
472 elseif (is_object($node)) {
473 $sql = db_query("SELECT custom_review_types.reviewer_node_type FROM {custom_review_types}, {node}
474 WHERE node.nid = %d AND node.type = custom_review_types.reviewed_node_type", $node->nid);
475 }
476 else if (is_string($node)) { //node->type!
477 $sql = db_query("SELECT reviewer_node_type FROM {custom_review_types} WHERE reviewed_node_type = '%s'", $node);
478 }
479
480 $reviewers = array();
481 while ($result = db_fetch_array($sql)) {
482 $reviewers[$result['reviewer_node_type']] = $result['reviewer_node_type'];
483 }
484 return $reviewers;
485 }
486 /**
487 * Explains if a node is reviewable or not
488 *
489 * @param $node
490 * (nid)
491 * @return boolean
492 */
493 function custom_review_node_is_reviewable($node) {
494 if (is_numeric($node)) {
495 $result = db_fetch_object(db_query("SELECT COUNT(custom_review_types.reviewed_node_type) as num FROM {node},
496 {custom_review_types} WHERE node.nid = %d AND node.type = custom_review_types.reviewed_node_type", $node));
497 return $result->num;
498 }
499 if (is_object($node)) {
500 return custom_review_node_is_reviewable($node->nid);
501 }
502 return FALSE;
503 }
504 /**
505 * Check if a node is a reviewer type or not
506 *
507 * @param $node
508 * @return boolean
509 * @deprecated
510 */
511 function custom_review_is_reviewer_type($node) {
512 global $_custom_review_reviewers;
513
514 if (empty($_custom_review_reviewers)) {
515 $_custom_review_reviewers = custom_review_get_reviewers();
516 }
517 if (is_numeric($node)) {
518 //to be completed, need a query to get the node type from its nid
519 }
520 else if (is_object($node)) {
521 if (is_string($node->type)) {
522 return custom_review_is_reviewer_type($node->type);
523 }
524 if (is_numeric($node->nid)) {
525 return custom_review_is_reviewer_type($node->nid);
526 }
527 }
528 else if (is_string($node)) {
529 return array_key_exists($node, $_custom_review_reviewers);
530 }
531
532 return FALSE;
533 }
534
535
536 /*****************************/
537 /** Internal module section **/
538 /*****************************/
539 /**
540 * Render the special form
541 */
542 function custom_review_build_special_form() {
543 if ((is_numeric(arg(1))) && (custom_review_node_is_reviewable(arg(1)))) {
544 $options = array();//add/edit reviews
545
546 foreach (custom_review_user_can_review_node(arg(1)) as $reviewer => $review) {
547 if (is_object($review)) {
548 //the user has already done a review with this reviewer on this node
549 $options['edit'][$review->rid] = (strlen($review->title) < 18) ? $review->title : substr($review->title, 0, 15) ."...";
550 }
551 else {
552 $reviewer_title = db_fetch_object(db_query("SELECT name FROM {node_type} WHERE type = '%s'", $reviewer));
553 $reviewer_title = (strlen($reviewer_title->name) < 18) ? $reviewer_title->name : substr($reviewer_title->name, 0, 15) ."...";
554 $options['add'][$reviewer] = $reviewer_title;
555 }
556 }
557 return $content = drupal_get_form('custom_review_block_action_form', $options);
558 }
559 return '';
560 }
561 /**
562 * Render a node type
563 *
564 * @param $nid
565 * @return type
566 */
567 function _custom_review_node_type($nid) {
568 $sql = db_query("SELECT type FROM {node} WHERE nid = %d", $nid);
569 $result = db_fetch_object($sql);
570 return $result->type;
571 }
572 /**
573 * Render a node reviewed original nid (reviewed node)
574 *
575 * @param $rid
576 * @return nid
577 */
578 function _custom_review_get_nid($rid) {
579 $sql = db_query("SELECT custom_reviews.nid as nid FROM {custom_reviews} WHERE custom_reviews.rid = %d", $rid);
580 $result = db_fetch_object($sql);
581
582 return $result->nid;
583 }

  ViewVC Help
Powered by ViewVC 1.1.2