Exemple #1
0
 /**
  * Query for n random submit times if 'random' option is set indicting number of random
  * values to return (n)
  * @param $formName
  * @return array|null array of n submit_times or null if not applicable
  */
 protected function queryRandomSubmitTimes($formName)
 {
     $submitTimes = null;
     if (isset($this->options['random'])) {
         $numRandom = intval($this->options['random']);
         if ($numRandom > 0) {
             // Digression: query for n unique random submit_time values
             $justSubmitTimes = new ExportBase();
             $justSubmitTimes->setOptions($this->options);
             $justSubmitTimes->setCommonOptions();
             unset($justSubmitTimes->options['random']);
             $justSubmitTimes->showColumns = array('submit_time');
             $jstSql = $justSubmitTimes->getPivotQuery($formName);
             $justSubmitTimes->setDataIterator($formName, 'submit_time');
             $justSubmitTimes->dataIterator->query($jstSql, $justSubmitTimes->rowFilter);
             $allSubmitTimes = null;
             while ($justSubmitTimes->dataIterator->nextRow()) {
                 $allSubmitTimes[] = $justSubmitTimes->dataIterator->row['submit_time'];
             }
             if (!empty($allSubmitTimes)) {
                 if (count($allSubmitTimes) < $numRandom) {
                     $submitTimes = $allSubmitTimes;
                     return $submitTimes;
                 } else {
                     shuffle($allSubmitTimes);
                     // randomize
                     $submitTimes = array_slice($allSubmitTimes, 0, $numRandom);
                     return $submitTimes;
                 }
             }
             return $submitTimes;
         }
         return $submitTimes;
     }
     return $submitTimes;
 }
 /**
  * Execute the query and set up the results iterator
  * @param string|array $formName (if array, must be array of string)
  * @param null|string $submitTimeKeyName
  * @return void
  */
 protected function setDataIterator($formName, $submitTimeKeyName = null)
 {
     $submitTimes = null;
     if (isset($this->options['random'])) {
         $numRandom = intval($this->options['random']);
         if ($numRandom > 0) {
             // Digression: query for n unique random submit_time values
             $justSubmitTimes = new ExportBase();
             $justSubmitTimes->setOptions($this->options);
             $justSubmitTimes->setCommonOptions();
             unset($justSubmitTimes->options['random']);
             $justSubmitTimes->showColumns = array('submit_time');
             $jstSql = $justSubmitTimes->getPivotQuery($formName);
             $justSubmitTimes->setDataIterator($formName, 'submit_time');
             $justSubmitTimes->dataIterator->query($jstSql, $justSubmitTimes->rowFilter);
             $allSubmitTimes = null;
             while ($justSubmitTimes->dataIterator->nextRow()) {
                 $allSubmitTimes[] = $justSubmitTimes->dataIterator->row['submit_time'];
             }
             if (!empty($allSubmitTimes)) {
                 if (count($allSubmitTimes) < $numRandom) {
                     $submitTimes = $allSubmitTimes;
                 } else {
                     shuffle($allSubmitTimes);
                     // randomize
                     $submitTimes = array_slice($allSubmitTimes, 0, $numRandom);
                 }
             }
         }
     }
     $sql = $this->getPivotQuery($formName, false, $submitTimes);
     $this->dataIterator = new CFDBQueryResultIterator();
     //        $this->dataIterator->fileColumns = $this->getFileMetaData($formName);
     $queryOptions = array();
     if ($submitTimeKeyName) {
         $queryOptions['submitTimeKeyName'] = $submitTimeKeyName;
     }
     if (!empty($this->rowFilter) && isset($this->options['limit'])) {
         // have data iterator apply the limit if it is not already
         // being applied in SQL directly, which we do when there are
         // no filter constraints.
         $queryOptions['limit'] = $this->options['limit'];
     }
     if (isset($this->options['unbuffered'])) {
         $queryOptions['unbuffered'] = $this->options['unbuffered'];
     }
     $this->dataIterator->query($sql, $this->rowFilter, $queryOptions);
     $this->dataIterator->displayColumns = $this->getColumnsToDisplay($this->dataIterator->columns);
 }