You are here

function uc_coupon_count_usage in Ubercart Discount Coupons 6

Same name and namespace in other branches
  1. 7.3 uc_coupon.module \uc_coupon_count_usage()
  2. 7.2 uc_coupon.module \uc_coupon_count_usage()

Count usage of a coupon.

Parameters

$cid: The coupon id to count.

$uid: (optional) The user id to count. Defaults to the current user.

Return value

An associative array containing:

  • codes: An associative array of code => usage count.
  • user: The usage count by the specified (or current) user.
4 calls to uc_coupon_count_usage()
uc_coupon_add_form in ./uc_coupon.admin.inc
Coupon add/edit form.
uc_coupon_handler_field_codes::pre_render in views/uc_coupon_handler_field_codes.inc
Expand the coupon codes for each coupon in the result set.
uc_coupon_purchase_view in uc_coupon_purchase/uc_coupon_purchase.pages.inc
Display a list of purchased coupons.
uc_coupon_validate in ./uc_coupon.module
Validate a coupon, and optionally calculate the order discount.

File

./uc_coupon.module, line 333
Provides discount coupons for Ubercart.

Code

function uc_coupon_count_usage($cid, $uid = NULL) {
  global $user;
  $weight = uc_order_status_data(variable_get('uc_coupon_used_order_status', 'processing'), 'weight');
  $usage = array(
    'codes' => array(),
  );
  $result = db_query("SELECT uco.code, COUNT(*) AS uses FROM {uc_coupons_orders} AS uco\n    LEFT JOIN {uc_orders} AS uo ON uco.oid = uo.order_id\n    LEFT JOIN {uc_order_statuses} AS uos ON uo.order_status = uos.order_status_id\n    WHERE uos.weight >= %d AND uco.cid = %d GROUP BY uco.code", $weight, $cid);
  while ($row = db_fetch_object($result)) {
    $usage['codes'][$row->code] = $row->uses;
  }
  if (is_null($uid)) {
    $uid = $user->uid;
  }
  $usage['user'] = db_result(db_query("SELECT COUNT(*) FROM {uc_coupons_orders} AS uco\n    LEFT JOIN {uc_orders} AS uo ON uco.oid = uo.order_id\n    LEFT JOIN {uc_order_statuses} AS uos ON uo.order_status = uos.order_status_id\n    WHERE uos.weight >= %d AND uco.cid = %d AND uo.uid = %d", $weight, $cid, $uid));

  // Allow other modules to implement usage counts.
  drupal_alter('uc_coupon_usage', $usage, $cid, $uid);
  return $usage;
}