? ad_auction_channel_volume.patch
Index: ad_auction.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ad_auction/ad_auction.module,v
retrieving revision 1.6
diff -u -p -r1.6 ad_auction.module
--- ad_auction.module	24 Sep 2010 22:33:16 -0000	1.6
+++ ad_auction.module	31 Dec 2010 20:28:55 -0000
@@ -23,7 +23,7 @@ function ad_auction_menu() {
 		'description' => t('Settings for ad_auction.'),
 		'access arguments' => array('administer site configuration'),
 		'type' => MENU_NORMAL_ITEM
-	);   
+	);
 	return $items;
 }
 function ad_auction_perm() {
@@ -80,6 +80,12 @@ function ad_auction_settings_form() {
 	'#description'	 => t('The default price of the ad slot.'),
     '#default_value' => variable_get('ad_auction_default_price', 50),
   );
+	$form['ad_auction_channel_volume'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Auction Channel Volume'),
+		'#description'	 => t('The number of ads_slots to create for each ad_channel.'),
+    '#default_value' => variable_get('ad_auction_channel_volume', 1),
+  );
   $form['ad_auction_ad_slot_text'] = array(
     '#type'          => 'textarea',
     '#title'         => t('Ad Slot Text'),
@@ -106,7 +112,7 @@ function ad_auction_settings_form() {
 	'#description'	 => t('Enter the nid of the ad that is to be used as default when no ad is purchased for an ad slot.'),
     '#default_value' => variable_get('ad_auction_default_ad', 0),
   );
-  
+
   //$form['#validate'][] = 'ad_auction_settings_validate';
   $form['#submit'][] = 'ad_auction_settings_submit';
   return system_settings_form($form);
@@ -146,7 +152,7 @@ function ad_auction_form_alter(&$form, &
   if($form_id == "ad_channel_admin_confirm_delete_channel") {
     $form['#validate'][] = 'ad_auction_delete_channel_validate';
   }
-  if($form_id=="ad_node_form") { 
+  if($form_id=="ad_node_form") {
     //  Non admin users do not get to see these fields
     if(!user_access('administer ads') && $user->uid > 1) {
 	  $form['schedule']['#access'] = FALSE;
@@ -156,9 +162,9 @@ function ad_auction_form_alter(&$form, &
 	  $form['taxonomy']['#access'] = FALSE;  // ad group
 	  if(arg(1)=="add") $form['adstatus']['#access'] = FALSE;  // there is no point in showing status on add
 	}
-  }  
+  }
   // When an ad channel is created we need to create an ad slot product
-  if($form_id=="ad_channel_admin_channel") {  
+  if($form_id=="ad_channel_admin_channel") {
   //db_query("ALTER TABLE {ad_auction} ADD group_tid int (11) NOT NULL");
     // We must assign each channel to a group so that it gets displayed in a block
 	$result = db_query("SELECT t.* FROM {term_data} t LEFT JOIN {vocabulary} v ON t.vid=v.vid WHERE v.module='ad'");
@@ -171,7 +177,7 @@ function ad_auction_form_alter(&$form, &
 	  $have_groups = FALSE;
 	}
 	else {
-	  
+
 	  $form['ad_auction_ad_group'] = array(
 	    '#type' => 'select',
 	    '#options' => $groups,
@@ -207,7 +213,7 @@ function ad_auction_nodeapi(&$node, $op,
 	break;
 	case "view":
 	  //drupal_set_message(print_r($node,1));
-	  if($node->type=="ad_slot") {	    
+	  if($node->type=="ad_slot") {
 	    $ad_slot = db_fetch_object(db_query("SELECT * FROM {ad_auction} a LEFT JOIN {ad_auction_cycles} c ON a.cid=c.cid WHERE a.nid=%d", $node->nid));
 		$block = db_fetch_object(db_query("SELECT * FROM {term_data} WHERE tid=%d", $ad_slot->group_tid));
 		$channel = db_fetch_object(db_query("SELECT * FROM {ad_channel} WHERE chid=%d", $ad_slot->chid));
@@ -225,10 +231,10 @@ function ad_auction_nodeapi(&$node, $op,
 		  '#value' => $content,
 		);
 	  }
-	  
+
 	  //drupal_set_message('auction start '.date("j-M-Y", ad_auction_get_start('auction')));
 	  //drupal_set_message('advert start '.date("j-M-Y", ad_auction_get_start('advert')));
-	
+
 	break;
     case "insert":
 	  if($node->type=="ad_slot") {
@@ -271,7 +277,7 @@ function ad_auction_cron() {
     $curr_cycle = _ad_auction_current_cycle();
     $result = db_query("SELECT * FROM {ad_auction} WHERE cid=%d", $curr_cycle->cid);
     while($ad = db_fetch_object($result)) {
-	  
+
 	  if($ad->ad_nid > 0) {
 	    $advert = node_load($ad->ad_nid);
 		$recipient = user_load($advert->uid);
@@ -282,7 +288,7 @@ function ad_auction_cron() {
 		if ($send['result'] != TRUE) {
 		  //watchdog('ad_auction', 'Advert end alert to !mail failed', array('!mail' => $recipient->mail), WATCHDOG_ERROR);
 		}
-		
+
 	  }
 	}
     //drupal_set_message('current cycle '.$curr_cycle->cid);
@@ -292,25 +298,25 @@ function ad_auction_cron() {
     if(!$curr_cycle->cid > 0) {
 	  $ad_start = time();
 	  //$end = ad_auction_get_advert_end($start);
-	  $ad_end = ad_auction_calculate_date(variable_get('ad_auction_ad_expiry_period_units', 1), variable_get('ad_auction_ad_expiry_period_duration', 'month') , $ad_start);	
+	  $ad_end = ad_auction_calculate_date(variable_get('ad_auction_ad_expiry_period_units', 1), variable_get('ad_auction_ad_expiry_period_duration', 'month') , $ad_start);
 	  // Set an arbitrary auction expire date to trigger creation of slots
 	  $auction_expire = time() - 60*60*24;
       db_query("INSERT INTO {ad_auction_cycles} SET start=%d, expire=%d, auction_expire=%d, started=1", $ad_start, $ad_end, $auction_expire);
 	  $cid = db_last_insert_id('ad_auction_cycles', 'cid');
-	
+
       $curr_cycle = db_fetch_object(db_query("SELECT * FROM {ad_auction_cycles} WHERE cid=%d", $cid));
       //drupal_set_message('current cycle '.print_r($curr_cycle,1));
     }
-    // We need to create a next cycle if not exists. 
+    // We need to create a next cycle if not exists.
     $next_cycle = _ad_auction_next_cycle();
     if(!$next_cycle->cid > 0) {
 	  $next_cycle = _ad_auction_create_next_cycle($curr_cycle);
     }
- 
+
     //drupal_set_message('next cycle starts '.format_date($next_cycle->start));
     // we need to create a bid cycle if not exists.  This cycle has an auction start time which is less than the current time
     $bid_cycle = _ad_auction_curr_bid_cycle();
-  
+
     if(!$bid_cycle->cid > 0) {
       //drupal_set_message('no bid cycle '.print_r($bid_cycle,1));
       $created = FALSE;
@@ -342,12 +348,12 @@ function ad_auction_cron() {
        */
     //drupal_set_message('expire time '.format_date($curr_cycle->expire).' time now '.format_date(time()));
     if($curr_cycle->cid > 0 && $curr_cycle->expire < time()) {
-      // First we expire all ads removing all associations between ads and groups or channels.  
+      // First we expire all ads removing all associations between ads and groups or channels.
 	  //drupal_set_message('expire old ads from '.$curr_cycle->cid);
       //$result = db_query("SELECT * FROM {ad_auction} WHERE cid=%d AND chid!=%d", $cycle->cid, variable_get('ad_auction_default_channel', 0));
       $result = db_query("SELECT * FROM {ad_auction} WHERE cid=%d", $curr_cycle->cid);
       while($ad = db_fetch_object($result)) {
-	  
+
 	    if($ad->ad_nid > 0) {
 	      $advert = node_load($ad->ad_nid);
 		  // Revert ad back to approved and clear the channel and group settings
@@ -363,18 +369,18 @@ function ad_auction_cron() {
 		  if ($send['result'] != TRUE) {
 		    //watchdog('ad_auction', 'Advert end alert to !mail failed', array('!mail' => $recipient->mail), WATCHDOG_ERROR);
 	 	  }
-		
+
 	    }
 	    //  We have a problem, Houston
 	    else {
-	  
+
 	    }
 	  }
 	  //  This cycle is now complete and we update the record
 	  db_query("UPDATE {ad_auction_cycles} SET expired=1 WHERE cid=%d", $curr_cycle->cid);
-	
+
 	  // Now we activate new ones
-	
+
 	  //$cycle = db_fetch_object(db_query("SELECT * FROM {ad_auction_cycles} WHERE expired=0 AND started=0 AND expire>%d", time()));
       //$result = db_query("SELECT * FROM {ad_auction} WHERE cid=%d AND chid!=%d", $next_cycle->cid, variable_get('ad_auction_default_channel', 0));
 	  watchdog('ad_auction', 'Next cycle '.print_r($next_cycle,1).' starting now');
@@ -392,11 +398,11 @@ function ad_auction_cron() {
 	      //variable_get('ad_auction_ad_expiry_period', '')
 	      //$advert->activated = $ad->start;
 	      //  We change the autoexpire to fit the new ad slot.  The assumption here is that no slots already exist that have an expiry date beyond that which we are applying here.
-	    
+
 	      //drupal_set_message('ad slot purchase '.print_r($ad,1));
 	      //drupal_set_message('ad slot purchase '.print_r($advert,1));
 	      node_save($advert);
-		
+
 		  $recipient = user_load($advert->uid);
 		  watchdog('ad_auction', 'Advert '.$advert->nid.' activated, for user '.$advert->uid. ' email '.$recipient->mail);
 		  $params['recipient'] = $recipient;
@@ -408,7 +414,7 @@ function ad_auction_cron() {
 	    }
 	    //  We have a problem, Houston
 	    else {
-	  
+
 	    }
 	  }
 	  // The next cycle now becomes the current cycle
@@ -441,36 +447,39 @@ function _ad_auction_create_next_cycle($
 	  //$end = ad_auction_get_advert_end($start);
 	  //drupal_set_message('units '.-variable_get('ad_auction_auction_period_units', 1));
 	  $ad_end = ad_auction_calculate_date(variable_get('ad_auction_ad_expiry_period_units', 1), variable_get('ad_auction_ad_expiry_period_duration', 1) , $ad_start);
-	  //  To get auction start lets go back from the ad start 
+	  //  To get auction start lets go back from the ad start
 	  $auction_start = ad_auction_calculate_date(-variable_get('ad_auction_auction_period_units', 1), variable_get('ad_auction_auction_period_duration', 1), $ad_start);
 	  //drupal_set_message('auction start '.format_date($auction_start));
 	  $auction_expire = $auction_start+variable_get('ad_auction_expiry_days', 25)*24*60*60;
 	  //drupal_set_message('auction expire '.format_date($auction_expire));
-	
+
       db_query("INSERT INTO {ad_auction_cycles} SET start=%d, expire=%d, auction_start=%d, auction_expire=%d", $ad_start, $ad_end, $auction_start, $auction_expire);
 	  $cid = db_last_insert_id('ad_auction_cycles', 'cid');
-	
+
       $next_cycle = db_fetch_object(db_query("SELECT * FROM {ad_auction_cycles} WHERE cid=%d", $cid));
 	  return $next_cycle;
-	  
+
 }
 function ad_auction_create_ad_slots($bid_cycle) {
-    //$result = db_query("SELECT * FROM {ad_auction} WHERE cid=%d AND chid!=%d", time(), variable_get('ad_auction_default_channel', 0));
-    $result = db_query("SELECT chid, name, description FROM {ad_channel} WHERE chid<>%d",  variable_get('ad_auction_default_channel', 0));
-    while ($channel = db_fetch_object($result)) {
-	  //  Create new ad slot
-	  $node->title = $channel->name;
-	  $node->type = 'ad_slot';
-	  $node->uid = 1;
-	  $node->cid = $bid_cycle->cid;
-	  //drupal_set_message('group tid '. variable_get('ad_auction_channel_'.$channel->chid.'_group', 0));
-        // This is an auction. Write the info to uc_auctions.
-	  node_save($node);
-	  db_query("INSERT INTO {ad_auction} SET nid=%d, chid=%d, group_tid=%d, cid=%d", $node->nid, $channel->chid, variable_get('ad_auction_channel_'.$channel->chid.'_group', 0), $bid_cycle->cid);
-	  unset($node);
-    }
-    db_query("UPDATE {ad_auction_cycles} SET slots_created=1 WHERE cid=%d", $bid_cycle->cid);
-
+	//$result = db_query("SELECT * FROM {ad_auction} WHERE cid=%d AND chid!=%d", time(), variable_get('ad_auction_default_channel', 0));
+	$result = db_query("SELECT chid, name, description FROM {ad_channel} WHERE chid<>%d",  variable_get('ad_auction_default_channel', 0));
+	$ad_volume = variable_get('ad_auction_channel_volume',1);
+	while ($channel = db_fetch_object($result)) {
+		//create a configurable number of ad_slots for each channel
+		for ($i=0; $i<$ad_volume; $i++) {
+			//  Create new ad slot
+			$node->title = $channel->name;
+			$node->type = 'ad_slot';
+			$node->uid = 1;
+			$node->cid = $bid_cycle->cid;
+			//drupal_set_message('group tid '. variable_get('ad_auction_channel_'.$channel->chid.'_group', 0));
+					// This is an auction. Write the info to uc_auctions.
+			node_save($node);
+			db_query("INSERT INTO {ad_auction} SET nid=%d, chid=%d, group_tid=%d, cid=%d", $node->nid, $channel->chid, variable_get('ad_auction_channel_'.$channel->chid.'_group', 0), $bid_cycle->cid);
+			unset($node);
+		}
+	}
+	db_query("UPDATE {ad_auction_cycles} SET slots_created=1 WHERE cid=%d", $bid_cycle->cid);
 }
 function ad_auction_mail($key, &$message, $params) {
   switch($key) {
@@ -497,7 +506,7 @@ function ad_auction_mail($key, &$message
 		'body'  => t('An advertisement display cycle ended on !date and a new one has started which ends on !end.' , array('!start' => format_date($params->start), '!end' => format_date($params->end->autoactivate))),
 		//'footer' => messaging_message_part('pdesk-general-notifications', 'footer', $send_method),
 	  );
-  }	  
+  }
 }
 /**
  * Implementation of hook_views_api().
@@ -545,7 +554,7 @@ function ad_auction_admin_channel_submit
   else {
     $urls = '';
   }
-  
+
   switch ($form_state['values']['op']) {
     case t('Create'):
       db_query("INSERT INTO {ad_channel} (name, description, conid, weight, display, no_channel_percent, urls, groups, inventory) VALUES('%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d)", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['conid'], $form_state['values']['weight'], $form_state['values']['display'], $form_state['values']['no_channel_percent'], serialize($urls), serialize($form_state['values']['groups']), $form_state['values']['inventory']);
@@ -608,7 +617,7 @@ function ad_auction_get_start($type='auc
   //drupal_set_message(variable_get('ad_auction_auction_period', ''));
     //$auction_start = ad_auction_calculate_date(variable_get('ad_auction_auction_period_units', 1), variable_get('ad_auction_auction_period_duration', 'month')
     switch(variable_get('ad_auction_auction_period_duration', '')) {
-      case 'month': 
+      case 'month':
 	    // if the granularity is in months then we look for the start of the next month.
 	    $curr_month = date('m');
 	    $curr_year = date('Y');
@@ -648,7 +657,7 @@ function ad_auction_get_advert_end($star
 	  else $month++;
 	  return strtotime($year.'/'.$month.'/01');
   }
- 
+
 }
 function ad_auction_info_block() {
   $curr_cycle = _ad_auction_current_cycle();
@@ -677,7 +686,7 @@ function ad_auction_info_block() {
     $output .= '<li>Auction start date: '.format_date($bid_cycle->auction_start). '</li>';
     $output .= '<li>Auction end date: '.format_date($bid_cycle->auction_expire). '</li>';
     $output .= '</ul>';
-  } 
+  }
   else $output .= '<div>'.t('New bid cycle has not been created yet.  Cron has still to run').'</div>';
   return $output;
 }
@@ -713,7 +722,7 @@ function ad_auction_pane($op, &$arg1, $a
 			  //'#prefix' => '<div class="hide">',
 			  //'#suffix' => '</div>',
 			);
-		  
+
 		  }
 	    }
 	  }
@@ -777,7 +786,7 @@ function ad_auction_calculate_date($dura
   }
 
   $start_time = (!is_null($start_time)) ? $start_time : time();
-  
+
   return strtotime($duration .' '. $granularity, $start_time);
 }
 function _ad_auction_current_cycle() {
@@ -791,5 +800,5 @@ function _ad_auction_next_cycle() {
 function _ad_auction_curr_bid_cycle() {
   // The current bid cycle is identified by a cycle that has not started or ended and in which the auction has started but not ended.
   //drupal_set_message('time now '.format_date(1278984887));
-  return db_fetch_object(db_query("SELECT * FROM {ad_auction_cycles} WHERE expired=0 AND started=0 AND auction_start<%d AND auction_expire>%d", time(), time()));
+  return db_fetch_object(db_query("SELECT * FROM {ad_auction_cycles} WHERE expired=0 AND started=0 AND auction_start<=%d AND auction_expire>%d", time(), time()));
 }
\ No newline at end of file
