report_conversion_rate.php

Go to the documentation of this file.
00001 <?php
00002 
00003 if (!class_exists("report_conversion_rate")) {
00007     class Report_conversion_rate extends report_base
00008     {
00009 
00015         protected $_sThisTemplate = "report_conversion_rate.tpl";
00016 
00022         public function drawReport()
00023         {
00024             $oDb = oxDb::getDb();
00025 
00026             $oSmarty = $this->getSmarty();
00027             $sTimeFrom = $oDb->quote(date("Y-m-d H:i:s", strtotime($oSmarty->_tpl_vars['time_from'])));
00028             $sTimeTo = $oDb->quote(date("Y-m-d H:i:s", strtotime($oSmarty->_tpl_vars['time_to'])));
00029 
00030             // orders
00031             if ($oDb->getOne("select * from oxlogs where oxtime >= $sTimeFrom and oxtime <= $sTimeTo")) {
00032                 return true;
00033             }
00034 
00035             // orders
00036             if ($oDb->getOne("select 1 from oxorder where oxorderdate >= $sTimeFrom and oxorderdate <= $sTimeTo")) {
00037                 return true;
00038             }
00039         }
00040 
00044         public function visitor_month()
00045         {
00046             $myConfig = $this->getConfig();
00047             $oDb = oxDb::getDb();
00048 
00049             $aDataX = array();
00050             $aDataY = array();
00051 
00052             $dTimeTo = strtotime(oxRegistry::getConfig()->getRequestParameter("time_to"));
00053             $dTimeFrom = mktime(23, 59, 59, date("m", $dTimeTo) - 12, date("d", $dTimeTo), date("Y", $dTimeTo));
00054 
00055             $sTimeTo = $oDb->quote(date("Y-m-d H:i:s", $dTimeTo));
00056             $sTimeFrom = $oDb->quote(date("Y-m-d H:i:s", $dTimeFrom));
00057 
00058             // orders
00059             $sSQL = "select oxtime, count(*) as nrof from oxlogs where oxtime >= $sTimeFrom and oxtime <= $sTimeTo group by oxsessid";
00060             $aTemp = array();
00061             for ($i = 1; $i <= 12; $i++) {
00062                 $aTemp[date("m/Y", mktime(23, 59, 59, date("m", $dTimeFrom) + $i, date("d", $dTimeFrom), date("Y", $dTimeFrom)))] = 0;
00063             }
00064 
00065             $rs = $oDb->execute($sSQL);
00066             if ($rs != false && $rs->recordCount() > 0) {
00067                 while (!$rs->EOF) {
00068                     $aTemp[date("m/Y", strtotime($rs->fields[0]))]++;
00069                     $rs->moveNext();
00070                 }
00071             }
00072 
00073             $aDataX2 = array();
00074             $aDataX3 = array();
00075 
00076             foreach ($aTemp as $key => $value) {
00077                 $aDataX[$key] = $value;
00078                 $aDataX2[$key] = 0;
00079                 $aDataX3[$key] = 0;
00080                 $aDataY[] = $key;
00081             }
00082 
00083             // orders
00084             $sSQL = "select oxorderdate from oxorder where oxorderdate >= $sTimeFrom and oxorderdate <= $sTimeTo order by oxorderdate";
00085             $rs = $oDb->execute($sSQL);
00086             if ($rs != false && $rs->recordCount() > 0) {
00087                 while (!$rs->EOF) {
00088                     $sKey = date("m/Y", strtotime($rs->fields[0]));
00089                     if (isset($aDataX2[$sKey])) {
00090                         $aDataX2[$sKey]++;
00091                     }
00092                     $rs->moveNext();
00093                 }
00094             }
00095 
00096             header("Content-type: image/png");
00097 
00098             // New graph with a drop shadow
00099             $graph = new Graph(800, 600, "auto");
00100 
00101             $graph->setBackgroundImage($myConfig->getImageDir(true) . "/reportbgrnd.jpg", BGIMG_FILLFRAME);
00102 
00103             // Use a "text" X-scale
00104             $graph->setScale("textlin");
00105             $graph->setY2Scale("lin");
00106             $graph->y2axis->setColor("red");
00107 
00108             // Label align for X-axis
00109             $graph->xaxis->setLabelAlign('center', 'top', 'right');
00110 
00111             // Label align for Y-axis
00112             $graph->yaxis->setLabelAlign('right', 'bottom');
00113 
00114             $graph->setShadow();
00115             // Description
00116             $graph->xaxis->setTickLabels($aDataY);
00117 
00118 
00119             // Set title and subtitle
00120             $graph->title->set("Monat");
00121 
00122             // Use built in font
00123             $graph->title->setFont(FF_FONT1, FS_BOLD);
00124 
00125             $aDataFinalX = array();
00126             foreach ($aDataX as $dData) {
00127                 $aDataFinalX[] = $dData;
00128             }
00129 
00130             // Create the bar plot
00131             $l2plot = new LinePlot($aDataFinalX);
00132             $l2plot->setColor("navy");
00133             $l2plot->setWeight(2);
00134             $l2plot->setLegend("Besucher");
00135             //$l1plot->SetBarCenter();
00136             $l2plot->value->setColor("navy");
00137             $l2plot->value->setFormat('% d');
00138             $l2plot->value->hideZero();
00139             $l2plot->value->show();
00140 
00141             $aDataFinalX2 = array();
00142             foreach ($aDataX2 as $dData) {
00143                 $aDataFinalX2[] = $dData;
00144             }
00145 
00146             // Create the bar plot
00147             $l3plot = new LinePlot($aDataFinalX2);
00148             $l3plot->setColor("orange");
00149             $l3plot->setWeight(2);
00150             $l3plot->setLegend("Bestellungen");
00151             //$l1plot->SetBarCenter();
00152             $l3plot->value->setColor('orange');
00153             $l3plot->value->setFormat('% d');
00154             $l3plot->value->hideZero();
00155             $l3plot->value->show();
00156 
00157             //conversion rate graph
00158             $l1datay = array();
00159             for ($iCtr = 0; $iCtr < count($aDataFinalX); $iCtr++) {
00160                 if ($aDataFinalX[$iCtr] != 0 && $aDataFinalX2[$iCtr] != 0) {
00161                     $l1datay[] = 100 / ($aDataFinalX[$iCtr] / $aDataFinalX2[$iCtr]);
00162                 } else {
00163                     $l1datay[] = 0;
00164                 }
00165             }
00166 
00167             $l1plot = new LinePlot($l1datay);
00168             $l1plot->setColor("red");
00169             $l1plot->setWeight(2);
00170             $l1plot->setLegend("Conversion rate (%)");
00171             $l1plot->value->setColor('red');
00172             $l1plot->value->setFormat('% 0.2f%%');
00173             $l1plot->value->hideZero();
00174             $l1plot->value->show();
00175 
00176             // Create the grouped bar plot1
00177             $graph->addY2($l1plot);
00178             $graph->add($l2plot);
00179             $graph->add($l3plot);
00180 
00181             // Finally output the  image
00182             $graph->stroke();
00183 
00184         }
00185 
00189         public function visitor_week()
00190         {
00191             $myConfig = $this->getConfig();
00192             $oDb = oxDb::getDb();
00193 
00194             $aDataX = array();
00195             $aDataX2 = array();
00196             $aDataX3 = array();
00197             $aDataY = array();
00198 
00199             $sTimeTo = $oDb->quote(date("Y-m-d H:i:s", strtotime(oxRegistry::getConfig()->getRequestParameter("time_to"))));
00200             $sTimeFrom = $oDb->quote(date("Y-m-d H:i:s", strtotime(oxRegistry::getConfig()->getRequestParameter("time_from"))));
00201 
00202             $sSQL = "select oxtime, count(*) as nrof from oxlogs where oxtime >= $sTimeFrom and oxtime <= $sTimeTo group by oxsessid order by oxtime";
00203             $aTemp = array();
00204             $rs = $oDb->execute($sSQL);
00205             if ($rs != false && $rs->recordCount() > 0) {
00206                 while (!$rs->EOF) {
00207                     //$aTemp[date( "W", strtotime( $rs->fields[0]))]++;
00208                     $aTemp[oxRegistry::get("oxUtilsDate")->getWeekNumber($myConfig->getConfigParam('iFirstWeekDay'), strtotime($rs->fields[0]))]++;
00209                     $rs->moveNext();
00210                 }
00211             }
00212 
00213 
00214             // initializing
00215             list($iFrom, $iTo) = $this->getWeekRange();
00216             for ($i = $iFrom; $i < $iTo; $i++) {
00217                 $aDataX[$i] = 0;
00218                 $aDataX2[$i] = 0;
00219                 $aDataX3[$i] = 0;
00220                 $aDataY[] = "KW " . $i;
00221             }
00222 
00223             foreach ($aTemp as $key => $value) {
00224                 $aDataX[$key] = $value;
00225                 $aDataX2[$key] = 0;
00226                 $aDataX3[$key] = 0;
00227                 $aDataY[] = "KW " . $key;
00228             }
00229 
00230             // buyer
00231             $sSQL = "select oxorderdate from oxorder where oxorderdate >= $sTimeFrom and oxorderdate <= $sTimeTo order by oxorderdate";
00232             $rs = $oDb->execute($sSQL);
00233             if ($rs != false && $rs->recordCount() > 0) {
00234                 while (!$rs->EOF) {
00235                     $sKey = oxRegistry::get("oxUtilsDate")->getWeekNumber($myConfig->getConfigParam('iFirstWeekDay'), strtotime($rs->fields[0]));
00236                     if (isset($aDataX2[$sKey])) {
00237                         $aDataX2[$sKey]++;
00238                     }
00239                     $rs->moveNext();
00240                 }
00241             }
00242 
00243             // newcustomer
00244             $sSQL = "select oxtime, oxsessid from oxlogs where oxtime >= $sTimeFrom and oxtime <= $sTimeTo group by oxsessid order by oxtime";
00245             $rs = $oDb->execute($sSQL);
00246             if ($rs != false && $rs->recordCount() > 0) {
00247                 while (!$rs->EOF) {
00248                     $sKey = oxRegistry::get("oxUtilsDate")->getWeekNumber($myConfig->getConfigParam('iFirstWeekDay'), strtotime($rs->fields[0]));
00249                     if (isset($aDataX3[$sKey])) {
00250                         $aDataX3[$sKey]++;
00251                     }
00252                     $rs->moveNext();
00253                 }
00254             }
00255 
00256             header("Content-type: image/png");
00257 
00258             // New graph with a drop shadow
00259             $graph = new Graph(max(800, count($aDataX) * 80), 600);
00260 
00261             $graph->setBackgroundImage($myConfig->getImageDir(true) . "/reportbgrnd.jpg", BGIMG_FILLFRAME);
00262 
00263             // Use a "text" X-scale
00264             $graph->setScale("textlin");
00265             $graph->setY2Scale("lin");
00266             $graph->y2axis->setColor("red");
00267 
00268             // Label align for X-axis
00269             $graph->xaxis->setLabelAlign('center', 'top', 'right');
00270 
00271             // Label align for Y-axis
00272             $graph->yaxis->setLabelAlign('right', 'bottom');
00273 
00274             $graph->setShadow();
00275             // Description
00276             $graph->xaxis->setTickLabels($aDataY);
00277 
00278 
00279             // Set title and subtitle
00280             $graph->title->set("Woche");
00281 
00282             // Use built in font
00283             $graph->title->setFont(FF_FONT1, FS_BOLD);
00284 
00285             $aDataFinalX = array();
00286             foreach ($aDataX as $dData) {
00287                 $aDataFinalX[] = $dData;
00288             }
00289 
00290             // Create the bar plot
00291             $l2plot = new LinePlot($aDataFinalX);
00292             $l2plot->setColor("navy");
00293             $l2plot->setWeight(2);
00294             $l2plot->setLegend("Besucher");
00295             $l2plot->value->setColor("navy");
00296             $l2plot->value->setFormat('% d');
00297             $l2plot->value->hideZero();
00298             $l2plot->value->show();
00299 
00300             $aDataFinalX2 = array();
00301             foreach ($aDataX2 as $dData) {
00302                 $aDataFinalX2[] = $dData;
00303             }
00304 
00305             // Create the bar plot
00306             $l3plot = new LinePlot($aDataFinalX2);
00307             $l3plot->setColor("orange");
00308             $l3plot->setWeight(2);
00309             $l3plot->setLegend("Bestellungen");
00310             //$l1plot->SetBarCenter();
00311             $l3plot->value->setColor("orange");
00312             $l3plot->value->setFormat('% d');
00313             $l3plot->value->hideZero();
00314             $l3plot->value->show();
00315 
00316             //conversion rate graph
00317             $l1datay = array();
00318             for ($iCtr = 0; $iCtr < count($aDataFinalX); $iCtr++) {
00319                 if ($aDataFinalX[$iCtr] != 0 && $aDataFinalX2[$iCtr] != 0) {
00320                     $l1datay[] = 100 / ($aDataFinalX[$iCtr] / $aDataFinalX2[$iCtr]);
00321                 } else {
00322                     $l1datay[] = 0;
00323                 }
00324             }
00325             $l1plot = new LinePlot($l1datay);
00326             $l1plot->setColor("red");
00327             $l1plot->setWeight(2);
00328             $l1plot->setLegend("Conversion rate (%)");
00329             $l1plot->value->setColor('red');
00330             $l1plot->value->setFormat('% 0.4f%%');
00331             $l1plot->value->hideZero();
00332             $l1plot->value->show();
00333 
00334             // Create the grouped bar plot
00335             $graph->addY2($l1plot);
00336             $graph->add($l2plot);
00337             $graph->add($l3plot);
00338 
00339             // Finally output the  image
00340             $graph->stroke();
00341         }
00342     }
00343 }