reviews.module in Reviews 7
Same filename and directory in other branches
This file defines all the necessary hooks and functions to create a system for enabling and authoring content reviews on a per content type basis.
File
reviews.moduleView source
<?php
/**
* @file
* This file defines all the necessary hooks and functions to create
* a system for enabling and authoring content reviews on a per
* content type basis.
*/
// Ensure that the file containing all the basic API functions is included.
module_load_include('inc', 'reviews', 'includes/reviews.api');
/**
* Implements hook_views_api().
*/
function reviews_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'reviews') . '/views',
);
}
/**
* Implements hook_permission().
*/
function reviews_permission() {
return array(
'administer reviews' => array(
'title' => t('Administer settings for the reviews system.'),
),
'moderate reviews' => array(
'title' => t('Moderate reviews'),
'description' => t('Allow user to moderate pending reviews.'),
),
'read reviews' => array(
'title' => t('Read reviews.'),
'description' => t('Allow user to read reviews for the node being displayed.'),
),
'add reviews' => array(
'title' => t('Leave reviews.'),
'description' => t('Allow a user to leave a review of the node being displayed.'),
),
'edit reviews' => array(
'title' => t('Edit reviews.'),
'description' => t('Allow a user to edit their own reviews.'),
),
);
}
/**
* Implements hook_menu().
*/
function reviews_menu() {
$items = array();
$items['admin/content/reviews'] = array(
'title' => 'Reviews',
'description' => t('Administer node reviews.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'reviews_list',
),
'access arguments' => array(
'moderate reviews',
),
'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
'file' => 'includes/reviews.admin.inc',
);
$items['admin/content/reviews/list'] = array(
'title' => 'List Reviews',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/content/reviews/moderate'] = array(
'title' => 'Moderate Reviews',
'description' => t('Moderate reviews left by users.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'reviews_moderation',
),
'access arguments' => array(
'moderate reviews',
),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/reviews.admin.inc',
);
$items['admin/content/reviews/approve/%'] = array(
'title' => 'Approve Review',
'page callback' => 'reviews_approve_review',
'page arguments' => array(
4,
),
'access arguments' => array(
'moderate reviews',
),
'type' => MENU_CALLBACK,
'file' => 'includes/reviews.admin.inc',
);
$items['admin/config/content/reviews'] = array(
'title' => 'Reviews',
'description' => 'Administer reviews.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'reviews_settings',
),
'access arguments' => array(
'administer reviews',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/reviews.admin.inc',
);
$items['node/%reviews_enabled/reviews'] = array(
'title callback' => 'reviews_get_title',
'title arguments' => array(
1,
),
'page callback' => 'reviews_get_reviews',
'page arguments' => array(
1,
),
'access arguments' => array(
'read reviews',
),
'file' => 'includes/reviews.pages.inc',
'type' => MENU_LOCAL_TASK,
);
$items['node/%reviews_enabled/add-review'] = array(
'title' => 'Add Review',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'reviews_add_review',
1,
),
'access arguments' => array(
'add reviews',
),
'file' => 'includes/reviews.pages.inc',
'type' => variable_get('reviews_link_type', 'tab') == 'tab' ? MENU_LOCAL_TASK : MENU_CALLBACK,
);
$items['node/%/edit-review/%'] = array(
'title' => 'Edit Review',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'reviews_edit_review',
1,
3,
),
'access callback' => 'reviews_has_edit_permission',
'file' => 'includes/reviews.pages.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Callback function to check if user is logged in and has
* edit review permissions.
*/
function reviews_has_edit_permission() {
return user_access('edit reviews') && user_is_logged_in();
}
/**
* Implements hook_theme().
*/
function reviews_theme($existing, $type, $theme, $path) {
if ($type == 'module') {
return array(
'reviews_no_reviews' => array(
'render_element' => 'element',
'variables' => array(
'nid' => NULL,
),
'template' => 'reviews-no-reviews',
'path' => drupal_get_path('module', 'reviews') . '/theme',
),
'reviews' => array(
'render_element' => 'element',
'variables' => array(
'nid' => NULL,
'review_count' => NULL,
'pending_count' => NULL,
'reviews' => array(),
'pager' => NULL,
),
'template' => 'reviews',
'path' => drupal_get_path('module', 'reviews') . '/theme',
),
'block_reviews' => array(
'render_element' => 'element',
'variables' => array(
'nid' => NULL,
'reviews' => array(),
),
'template' => 'block-reviews',
'path' => drupal_get_path('module', 'reviews') . '/theme',
),
'block_my_reviews' => array(
'render_element' => 'element',
'variables' => array(
'reviews' => array(),
),
'template' => 'block-my-reviews',
'path' => drupal_get_path('module', 'reviews') . '/theme',
),
'review' => array(
'render_element' => 'element',
'variables' => array(
'nid' => NULL,
'index' => NULL,
'total_reviews' => NULL,
'review' => array(),
),
'template' => 'review',
'path' => drupal_get_path('module', 'reviews') . '/theme',
),
'block_review' => array(
'render_element' => 'element',
'variables' => array(
'nid' => NULL,
'index' => NULL,
'total_reviews' => NULL,
'review' => array(),
),
'template' => 'block-review',
'path' => drupal_get_path('module', 'reviews') . '/theme',
),
);
}
return array();
}
/**
* Menu callback.
* Returns the argument passed form the URL if content type allows reviews
* and reviews are enabled globally and the current user has not already
* reviewed the node.
*/
function reviews_user_reviewed_load($arg) {
global $user;
// Check if reviews are enabled, if not return FALSE immediately.
if (!reviews_reviews_enabled()) {
return FALSE;
}
// Load the node to find it's type.
$node = node_load($arg);
$node_type = $node->type;
// Get the Array containing which content types are allowed reviews.
$reviewable_ctypes = variable_get('reviews_enabled_content_types', array());
// Check if reviews are allowed for the node type, if not return FALSE.
if (isset($reviewable_ctypes[$node_type]) && $reviewable_ctypes[$node_type] === 0) {
return FALSE;
}
// Now check to see if the user has already reviewed this node, if they have
// return FALSE.
$user_reviewed = reviews_check_user_review($node->nid, $user->uid);
if ($user_reviewed) {
return FALSE;
}
// If all aobve checks are OK we return TRUE and present the user with the
// review option.
return $arg;
}
/**
* Menu callback.
* Returns FALSE if content type does not allow reviews, otherwise
* returns the srgument passed from the URL.
*/
function reviews_enabled_load($arg) {
// Check if reviews are enabled, if not return FALSE immediately.
if (!reviews_reviews_enabled()) {
return FALSE;
}
// Load the node to find it's type.
$node = node_load($arg);
$node_type = $node->type;
// Get the Array containing which content types are allowed reviews.
$reviewable_ctypes = variable_get('reviews_enabled_content_types', array());
// Check if reviews are allowed for the node type, if not return FALSE.
$allow_reviews_for_ctype = FALSE;
if (isset($reviewable_ctypes[$node_type]) && $reviewable_ctypes[$node_type] === $node_type) {
$allow_reviews_for_ctype = TRUE;
}
if (!$allow_reviews_for_ctype) {
return FALSE;
}
return $arg;
}
/**
* Implements hook_block_info().
*/
function reviews_block_info() {
$blocks['reviews'] = array(
'info' => t('Reviews'),
'description' => t('Add a block to the node page to show reviews for that node.'),
'status' => FALSE,
);
$blocks['my_reviews'] = array(
'info' => t('My Reviews'),
'description' => t('Add a block to display the reviews made by the current user.'),
'status' => FALSE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function reviews_block_view($delta) {
$block = array();
switch ($delta) {
case 'reviews':
$block['subject'] = t('Reviews');
$block['content'] = reviews_get_reviews_block();
break;
case 'my_reviews':
$block['subject'] = t('My Reviews');
$block['content'] = reviews_get_my_reviews_block();
break;
}
return $block;
}
/**
* Helper function to return content for Reviews block.
*/
function reviews_get_reviews_block() {
$path = $_GET['q'];
if (drupal_match_path($path, "node/*")) {
$nid = arg(1);
// Get the sort order for reviews.
if (variable_get('reviews_sort_order', 0) == 0) {
$sort = 'ASC';
}
else {
$sort = 'DESC';
}
$reviews = db_select('reviews', 'r')
->fields('r')
->condition('nid', $nid, '=')
->condition('status', 1, '=')
->orderBy('created', $sort)
->range(0, 5)
->execute()
->fetchAll();
if (count($reviews) != 0) {
return theme('block_reviews', array(
'nid' => $nid,
'reviews' => $reviews,
));
}
else {
return '';
}
}
else {
return '';
}
}
/**
* Helper function to return content for My Reviews block.
*/
function reviews_get_my_reviews_block() {
global $user;
// Get the sort order for reviews.
if (variable_get('reviews_sort_order', 0) == 0) {
$sort = 'ASC';
}
else {
$sort = 'DESC';
}
$reviews = db_select('reviews', 'r')
->fields('r')
->condition('uid', $user->uid, '=')
->condition('status', 1, '=')
->orderBy('created', $sort)
->range(0, 5)
->execute()
->fetchAll();
if (count($reviews) != 0) {
return theme('block_my_reviews', array(
'reviews' => $reviews,
));
}
else {
return '';
}
}
/**
* Implements hook_pathauto.
*/
function reviews_pathauto($op) {
$settings = new stdClass();
$settings->module = 'reviews';
$settings->groupheader = t('Reviews Paths');
$settings->patterndescr = t('Default path pattern');
$settings->patterndefault = 'content/[node:title]/reviews';
$settings->token_type = 'node';
$settings->patternitems = array(
'reviews' => 'Node Reviews',
);
$settings->batch_update_callback = 'reviews_pathauto_bulkupdate';
return $settings;
}
/**
* Helper function to create the alias.
*/
function reviews_create_alias($node, $op) {
module_load_include('inc', 'pathauto');
pathauto_create_alias('reviews', $op, 'node/' . $node->nid . '/reviews', array(
'node' => $node,
), 'reviews');
}
/**
* Implements hook_node_view().
*/
function reviews_node_view($node, $view_mode, $langcode) {
if (variable_get('reviews_link_type', 'tab') == 'link') {
if ($view_mode == 'full' && reviews_user_reviewed_load($node->nid)) {
$review_dest = 'node/' . $node->nid . '/add-review';
$options = array();
if (user_access('add reviews')) {
$dest = $review_dest;
$text = t('Add review');
}
else {
$dest = 'user';
$options['query'] = array(
'destination' => $review_dest,
);
$text = t('Login to add review');
}
$node->content['add_review_link'] = array(
'#markup' => '<div class="reviews add-review-link">' . l($text, $dest, $options) . '</div>',
'#weight' => 100,
);
}
}
}
Functions
Name | Description |
---|---|
reviews_block_info | Implements hook_block_info(). |
reviews_block_view | Implements hook_block_view(). |
reviews_create_alias | Helper function to create the alias. |
reviews_enabled_load | Menu callback. Returns FALSE if content type does not allow reviews, otherwise returns the srgument passed from the URL. |
reviews_get_my_reviews_block | Helper function to return content for My Reviews block. |
reviews_get_reviews_block | Helper function to return content for Reviews block. |
reviews_has_edit_permission | Callback function to check if user is logged in and has edit review permissions. |
reviews_menu | Implements hook_menu(). |
reviews_node_view | Implements hook_node_view(). |
reviews_pathauto | Implements hook_pathauto. |
reviews_permission | Implements hook_permission(). |
reviews_theme | Implements hook_theme(). |
reviews_user_reviewed_load | Menu callback. Returns the argument passed form the URL if content type allows reviews and reviews are enabled globally and the current user has not already reviewed the node. |
reviews_views_api | Implements hook_views_api(). |