public function actionIndex() { // banners to show $banners_arr = array('superbanner' => array('percentage' => 50, 'count' => 0), 'superbanner2' => array('percentage' => 100, 'count' => 0), 'superbanner3' => array('percentage' => 50, 'count' => 10)); $banner_stat = array(); $output = ''; foreach ($banners_arr as $banner => $data) { $banner_data = AdtestModule::prepareBanner($banner, $data['percentage'], $data['count']); if ($banner_data) { $view_file = $this->getViewFile('/banners/' . $banner_data['name']); if ($view_file) { for ($i = 0; $i < $banner_data['count']; $i++) { $output .= $this->renderPartial('/banners/' . $banner_data['name'], null, true); } } } $banner_stat[] = $banner_data; } $this->render('index', array('data' => $output)); // save views foreach ($banner_stat as $banner_data) { $viewed_model = Adtest::model()->find('banner = ?', array($banner_data['name'])); if (!$viewed_model) { $viewed_model = new Adtest(); $viewed_model->viewed = 0; $viewed_model->banner = $banner_data['name']; } $viewed_model->viewed += $banner_data['count']; $viewed_model->save(); } }
public static function prepareBanner($name, $percentage = 100, $count = 0) { if (!$name) { return false; } $params = Yii::app()->controller->module->getParams(); if ($count == 0) { $params->{$name} ? $count = $params->{$name} : ($count = 0); } $viewed_model = Adtest::model()->find('banner = ?', array($name)); if ($viewed_model) { $viewed = $viewed_model->viewed; } else { $viewed = 0; } if ($viewed < $count) { $count -= $viewed; } else { $count = 0; } $count = round($count * $percentage / 100); return array('name' => $name, 'count' => $count); }