Index: user_quota.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/user_quota/user_quota.module,v retrieving revision 1.17 diff -u -r1.17 user_quota.module --- user_quota.module 19 Dec 2008 23:56:11 -0000 1.17 +++ user_quota.module 1 Jan 2009 19:03:42 -0000 @@ -1,4 +1,5 @@ 'User quota', 'description' => 'View and manage user quotas.', - 'page callback' => 'user_quota_settings', + 'page callback' => 'user_quota_view', 'access arguments' => array('administer site configuration'), 'type' => MENU_NORMAL_ITEM, ); - - $item['admin/user/user_quota/manage'] = array( + + $items['admin/user/user_quota/view'] = array( + 'title' => 'View user quota', + 'description' => 'View user quotas.', + 'access arguments' => array('administer site configuration'), + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => 0 + ); + + $items['admin/user/user_quota/manage'] = array( 'title' => 'Manage a quota', 'description' => 'Manage the quota for a user or add a new quota for a user', 'page callback' => 'user_quota_manage', 'access arguments' => array('administer site configuration'), - 'type' => MENU_NORMAL_ITEM, + 'type' => MENU_LOCAL_TASK, + 'weight' => 1 ); - return ($item); + $items['admin/user/user_quota/settings'] = array( + 'title' => 'Settings', + 'description' => 'Settings for the User Quota module', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('user_quota_settings'), + 'access arguments' => array('administer site configuration'), + 'type' => MENU_LOCAL_TASK, + 'weight' => 2 + ); + + $items['user/%user_uid_optional/quota'] = array( + 'title' => 'User quota', + 'page callback' => 'user_quota_user_own_quota', + 'page arguments' => array(1), + 'access callback' => 'user_edit_access', + 'access arguments' => array(1), + 'type' => MENU_LOCAL_TASK, + 'weight' => 5, + ); + return $items; } /** @@ -35,7 +64,7 @@ * * @return string The content to display in the browser. */ -function user_quota_settings() { +function user_quota_view() { $output = t('

This page shows an overall report of quotas for all users. Click on the "Details and manage" link for a specific user to see the history of their quota. You can also create a new quota for a user.

', array('!uri' => url('admin/user/user_quota/manage/'))); $limit = 100; @@ -68,34 +97,14 @@ $output .= drupal_get_form('user_quota_manage_form', $account); // Get current quota limits. - $current = db_query("SELECT current_limit, type FROM {user_quota} WHERE uid = %d", $uid); - $current_header = array(array('data' => t('Quota')), - array('data' => t('Content type')) - ); - while ($current_result = db_fetch_object($current)) { - $current_rows[] = array($current_result->current_limit, check_plain($current_result->type)); - } - if (isset($current_rows)) { - $output .= t('

Current quotas

'); - $output .= theme('table', $current_header, $current_rows); - } - + // Qmod: moved to re-useable helper function + $output = t('

Current quotas

'); + $output .= user_quota_show_user_quota(FALSE, $uid); + // Get historic quota alterations. - $history = db_query("SELECT quota, type, uqh.created, message, u.name, altering_uid FROM {user_quota_history} uqh LEFT JOIN {users} u ON uqh.altering_uid = u.uid WHERE uqh.uid = %d ORDER BY created DESC", $uid); - $history_header = array(array('data' => t('Quota')), - array('data' => t('Content type')), - array('data' => t('Date')), - array('data' => t('Altering user')), - array('data' => t('Message')), - - ); - while ($history_result = db_fetch_object($history)) { - $history_rows[] = array($history_result->quota, check_plain($history_result->type), format_date($history_result->created), l($history_result->name, 'user/'. $history_result->altering_uid), check_markup($history_result->message)); - } - if (isset($history_rows)) { - $output .= t('

Quota modification history

'); - $output .= theme('table', $history_header, $history_rows); - } + // Qmod: moved to re-useable helper function + $output .= t('

Quota modification history

'); + $output .= user_quota_show_user_history($uid); } else { drupal_set_message(t("Invalid URL argument, proceeding with the default form.")); @@ -149,8 +158,8 @@ '#size' => 60, '#maxlength' => 64, '#required' => TRUE, - '#description' => t('Negative values will decrease the quota. It is possible for a user to have a negative quota.'), - ); + '#description' => t('Negative values will decrease the quota. It is possible for a user to have a negative quota. For unlimited quotas, please enter a value higher than %value', array('%value' => variable_get('user_quota_unlimited_value', 1000))) + ); // An optional message $form['user_quota_form']['message'] = array( @@ -159,7 +168,7 @@ '#description' => t('Provide a motivation for your change.'), '#cols' => 60, '#rows' => 2, - '#description' => t('Negative values will decrease the quota. It is possible for a user to have a negative quota.'), + ); // And go! @@ -192,6 +201,53 @@ } /** +* Admin settings +* +*/ +function user_quota_settings() { + $form['user_quota_unlimited_value'] = array( + '#type' => 'textfield', + '#title' => t('Set unlimited value'), + '#default_value' => variable_get('user_quota_unlimited_value', 1000), + '#description' => t('Any value above this number will give the user unlimited quotas.'), + '#width' => 60, + ); + + $form['user_quota_blocked_msg'] = array( + '#type' => 'textarea', + '#title' => t('Empty message'), + '#default_value' => variable_get('user_quota_blocked_msg', 'You currently may not create another %type. You must first gain more credits.'), + '#description' => t('Message for when a users\' quota is 0. Use the token %type to indicate a particular type such as \'page\', \'story\', etc.'), + '#cols' => 60, + '#rows' => 1, + ); + return system_settings_form($form); +} + +function user_quota_settings_validate($form, &$form_state) { + // Is it a non-zero number? + if (!is_numeric($form['user_quota_unlimited_value']['#value']) || $form['user_quota_unlimited_value']['#value'] == 0) { + form_set_error('quota', t('Please enter a non-zero number')); + } +} + +/** + A page callback to show quota to user in account +*/ +function user_quota_user_own_quota($account) { + + // Get current quota limits. + $output = t('

Current quotas

'); + $output .= user_quota_show_user_quota(TRUE, $account->uid); + + // Get historic quota alterations. + $output .= t('

Quota history

'); + $output .= user_quota_show_user_history($account->uid); + + return $output; +} + +/** * Implementation of hook_nodeapi(). * */ @@ -200,18 +256,28 @@ if (empty($node->nid) && ($op == 'prepare' || $op == 'validate')) { global $user; user_quota_check_limit($node, $user); + //Qmod: Exit node creation without showing form? } elseif ($op == 'insert') { // Decrement any records by the value set during form_alter - // Slightly sneaky use of UPDATE: if they are not in a quota it will affect 0 rows. - db_query("UPDATE {user_quota} SET current_limit = current_limit - %d WHERE uid = %d AND type = '%s'", $node->user_quota_decrement, $node->uid, $node->type); - if (db_affected_rows()) { + //Qmod: don't decrement if unlimited + if (user_quota_get_limit($node->type) != 'unlimited') { + // Slightly sneaky use of UPDATE: if they are not in a quota it will affect 0 rows. + //Qmod: in this update, if they are not in a quota, they can't post. Blocked in $op = 'prepare'. + db_query("UPDATE {user_quota} SET current_limit = current_limit - %d WHERE uid = %d AND type = '%s'", $node->user_quota_decrement, $node->uid, $node->type); + //Qmod: prepair for history log + $decrement = -$node->user_quota_decrement; + } + else { + $decrement = 0; + } + // If rows are affected then they have a quota, so log it in the history table. - global $user; + //Qmod: All posts are logged for consistancy. Unlimited users get 0 decrement. + global $user; $message = t('Created @type content @title.', array('@user-name' => $user->name, '@type' => $node->type, '!uri' => url('node/'. $node->nid), '@title' => $node->title)); - db_query("INSERT INTO {user_quota_history} (uid, quota, type, created, message, altering_uid) VALUES (%d, %d, '%s', %d, '%s', %d)", $node->uid, -$node->user_quota_decrement, $node->type, time(), $message, $user->uid); - } - } + db_query("INSERT INTO {user_quota_history} (uid, quota, type, created, message, altering_uid) VALUES (%d, %d, '%s', %d, '%s', %d)", $node->uid, $decrement, $node->type, time(), $message, $user->uid); + } } /** @@ -231,7 +297,7 @@ return $limit; } else { - form_set_error('', t('You currently may not create another %type. You must first gain more credits.', array('%type' => $node->type))); + form_set_error('', t(variable_get('user_quota_blocked_msg', 'You currently may not create another %type. You must first gain more credits.'), array('%type' => $node->type))); } } @@ -241,10 +307,15 @@ } $limit = db_result(db_query("SELECT current_limit FROM {user_quota} WHERE uid = %d AND type = '%s'", $user->uid, $type)); if (is_numeric($limit)) { - return $limit; + if ($limit >= variable_get('user_quota_unlimited_value', 1000)) { + return 'unlimited'; + } + else { + return $limit; + } } else { - return 'unlimited'; + return 0; } } @@ -277,4 +348,183 @@ if (!db_affected_rows()) { db_query("INSERT INTO {user_quota} (uid, current_limit, type) VALUES (%d, %d, '%s')", $uid, $limit, $type); } -} \ No newline at end of file +} + +/** + * Implementation of hook_block(). + */ +function user_quota_block($op='list', $delta=0) { + switch ($op) { + + case 'list' : + $block[0]["info"] = t('User Quota'); + return $block; + + case 'view' : + $block_header = array(); + $block_rows = user_quota_per_user_quota(TRUE); + $block_content = theme('table', $block_header, $block_rows); + $block['subject'] = 'Content Quota'; + $block['content'] = $block_content; + return $block; + } +} +/** +* Helper function for per user quota for active user for use in listings. +*/ +function user_quota_per_user_quota($access = FALSE, $uid = NULL) { + global $user; + $uid = ($uid)? $uid : $user->uid; + $results = db_query("SELECT current_limit, type FROM {user_quota} WHERE uid = %d", $uid); + $num_rows = FALSE; + $per_user_quota = array(); + while ($value = db_fetch_object($results)) { + $limit = ($value->current_limit >= variable_get('user_quota_unlimited_value', 1000))? 'Unlimited': $value->current_limit; + if ($access) { //show only nodes which have user access + if (node_access('create', $value->type)) { + $per_user_quota[] = array(ucfirst($value->type), $limit); + $num_rows = TRUE; + } + } + else { + $per_user_quota[] = array(ucfirst($value->type), $limit); + $num_rows = TRUE; + } + } + + if (!$num_rows) { //no db entry>>>no quotas assigned. + $per_user_quota[] = array('No quotas assigned'); + } + return $per_user_quota; +} + +/** +* Helper function to show user quota in table +*/ +function user_quota_show_user_quota($access = FALSE, $this_uid = NULL) { + $current_header = array(array('data' => t('Content type')), array('data' => t('Quota'))); + $current_rows = user_quota_per_user_quota($access, $this_uid); + //no headings if no content + if ($current_rows == array('No quotas assigned')) { + $current_header = array(); + } + if (isset($current_rows)) { + $output = theme('table', $current_header, $current_rows); + } + return $output; +} + +/** +* Helper function to show user history +*/ +function user_quota_show_user_history($this_uid) { + $history = db_query("SELECT quota, type, uqh.created, message, u.name, altering_uid FROM {user_quota_history} uqh LEFT JOIN {users} u ON uqh.altering_uid = u.uid WHERE uqh.uid = %d ORDER BY created DESC", $this_uid); + $history_header = array(array('data' => t('Quota')), + array('data' => t('Content type')), + array('data' => t('Date')), + array('data' => t('Altering user')), + array('data' => t('Message')), + + ); + while ($history_result = db_fetch_object($history)) { + $history_rows[] = array($history_result->quota, check_plain($history_result->type), format_date($history_result->created), l($history_result->name, 'user/'. $history_result->altering_uid), check_markup($history_result->message)); + } + if (isset($history_rows)) { + $output = theme('table', $history_header, $history_rows); + } + else { + $output = 'No history information'; + } + return $output; +} + +/** +* Implementation of hook_action_info(). +*/ +function user_quota_action_info() { + return array( + 'user_quota_add_quota_action' => array( + 'description' => t('Add User Quota'), + 'type' => 'user', + 'configurable' => TRUE, + 'hooks' => array('any' => TRUE), + ), + ); +} + +/** +* Do action add quota +* +*/ +function user_quota_add_quota_action(&$object, $context = array()) { + if (isset($object->uid)) { + $uid = $object->uid; + } + elseif (isset($context['uid'])) { + $uid = $context['uid']; + } + else { + global $user; + $uid = $user->uid; + } + $limit = $context['action_limit']; + $type = $context['action_type']; + $message= $context['action_message']; + user_quota_set_user_quota($uid, $limit, $type, $message, 1); +} + +/** +* Set up action variables. +*/ +function user_quota_add_quota_action_form() { + // The content type to limit + $types = node_get_types(); + foreach ($types as $machine => $type) { + $options[$machine] = $type->name; + } + $form['action_type'] = array( + '#type' => 'select', + '#title' => t('Content type to limit'), + '#options' => $options, + '#default_value' => isset($context['action_type']) ? $context['action_type'] : '', + ); + + // The limit number + $form['action_limit'] = array( + '#type' => 'textfield', + '#title' => t('Amount to alter quota (add or decrease)'), + '#default_value' => 0, + '#size' => 60, + '#maxlength' => 64, + '#required' => TRUE, + '#default_value' => isset($context['action_limit']) ? $context['action_limit'] : '', + '#description' => t('Negative values will decrease the quota. It is possible for a user to have a negative quota. For unlimited quotas, please enter a value higher than %value', array('%value' => variable_get('user_quota_unlimited_value', 1000))) + ); + + // An optional message + $form['action_message'] = array( + '#type' => 'textarea', + '#title' => t('Modification message'), + '#description' => t('Provide a motivation for your change.'), + '#cols' => 60, + '#rows' => 2, + '#default_value' => isset($context['action_message']) ? $context['action_message'] : '', + + ); + return $form; +} + +function user_quota_add_quota_action_validate($form, &$form_state) { + // Is it a non-zero number? + if (!is_numeric($form_state['values']['action_limit']) || $form_state['values']['action_limit'] == 0) { + form_set_error('quota', t('Please enter a non-zero number')); + } +} + +function user_quota_add_quota_action_submit($form, $form_state) { + return array( + 'action_type' => $form_state['values']['action_type'], + 'action_limit' => $form_state['values']['action_limit'], + 'action_message' => $form_state['values']['action_message'], + ); +} \ No newline at end of file