/**
  * RPC routine to export statistics of a survey to a user.
  * Returns string - base64 encoding of the statistics.
  *
  * @access public
  * @param string $sSessionKey Auth credentials
  * @param int $iSurveyID Id of the Survey
  * @param string $docType Type of documents the exported statistics should be
  * @param string $sLanguage Optional language of the survey to use
  * @param string $graph Create graph option
  * @return string Base64 encoded string with the statistics file
  */
 public function export_statistics($sSessionKey, $iSurveyID, $docType = 'pdf', $sLanguage = null, $graph = '0')
 {
     Yii::app()->loadHelper('admin/statistics');
     $tempdir = Yii::app()->getConfig("tempdir");
     if (!$this->_checkSessionKey($sSessionKey)) {
         return array('status' => 'Invalid session key');
     }
     $oSurvey = Survey::model()->findByPk($iSurveyID);
     if (!isset($oSurvey)) {
         return array('status' => 'Error: Invalid survey ID');
     }
     if (Survey::model()->findByPk($iSurveyID)->owner_id != $_SESSION['loginID']) {
         return array('status' => 'Error: No Permission');
     }
     $aAdditionalLanguages = array_filter(explode(" ", $oSurvey->additional_languages));
     if (is_null($sLanguage) || !in_array($sLanguage, $aAdditionalLanguages)) {
         $sLanguage = $oSurvey->language;
     }
     $oAllQuestions = Questions::model()->findAllByAttributes(array('sid' => $iSurveyID, 'parent_qid' => '0', 'language' => $sLanguage));
     $aSummary = createCompleteSGQA($iSurveyID, $oAllQuestions, $sLanguage);
     switch ($docType) {
         case 'pdf':
             $sTempFile = generate_statistics($iSurveyID, $aSummary, $aSummary, $graph, $docType, 'F', $sLanguage);
             $sResult = file_get_contents($sTempFile);
             unlink($sTempFile);
             break;
         case 'xls':
             $sTempFile = generate_statistics($iSurveyID, $aSummary, $aSummary, '0', $docType, 'F', $sLanguage);
             $sResult = file_get_contents($sTempFile);
             unlink($sTempFile);
             break;
         case 'html':
             $sResult = generate_statistics($iSurveyID, $aSummary, $aSummary, '0', $docType, 'DD', $sLanguage);
             break;
     }
     return base64_encode($sResult);
 }
 /**
  * RPC routine to export statistics of a survey to a user.
  * Returns string - base64 encoding of the statistics.
  *
  * @access public
  * @param string $sSessionKey Auth credentials
  * @param int $iSurveyID Id of the Survey
  * @param string $docType Type of documents the exported statistics should be
  * @param string $sLanguage Optional language of the survey to use
  * @param string $graph Create graph option
  * @param int|array $groupIDs An OPTIONAL array (ot a single int) containing the groups we choose to generate statistics from
  * @return string Base64 encoded string with the statistics file
  */
 public function export_statistics($sSessionKey, $iSurveyID, $docType = 'pdf', $sLanguage = null, $graph = '0', $groupIDs = null)
 {
     Yii::app()->loadHelper('admin/statistics');
     $tempdir = Yii::app()->getConfig("tempdir");
     if (!$this->_checkSessionKey($sSessionKey)) {
         return array('status' => 'Invalid session key');
     }
     $oSurvey = Survey::model()->findByPk($iSurveyID);
     if (!isset($oSurvey)) {
         return array('status' => 'Error: Invalid survey ID');
     }
     if (!Permission::model()->hasSurveyPermission($iSurveyID, 'statistics', 'read')) {
         return array('status' => 'Error: No Permission');
     }
     $aAdditionalLanguages = array_filter(explode(" ", $oSurvey->additional_languages));
     if (is_null($sLanguage) || !in_array($sLanguage, $aAdditionalLanguages)) {
         $sLanguage = $oSurvey->language;
     }
     $oAllQuestions = Question::model()->getQuestionList($iSurveyID, $sLanguage);
     if (!isset($oAllQuestions)) {
         return array('status' => 'No available data');
     }
     if ($groupIDs != null) {
         if (is_int($groupIDs)) {
             $groupIDs = array($groupIDs);
         }
         if (is_array($groupIDs)) {
             //check that every value of the array belongs to the survey defined
             $aGroups = QuestionGroup::model()->findAllByAttributes(array('sid' => $iSurveyID));
             foreach ($aGroups as $group) {
                 $validGroups[] = $group['gid'];
             }
             $groupIDs = array_intersect($groupIDs, $validGroups);
             if (empty($groupIDs)) {
                 return array('status' => 'Error: Invalid group ID');
             }
             foreach ($oAllQuestions as $key => $aQuestion) {
                 if (!in_array($aQuestion['gid'], $groupIDs)) {
                     unset($oAllQuestions[$key]);
                 }
             }
         } else {
             return array('status' => 'Error: Invalid group ID');
         }
     }
     if (!isset($oAllQuestions)) {
         return array('status' => 'No available data');
     }
     usort($oAllQuestions, 'groupOrderThenQuestionOrder');
     $aSummary = createCompleteSGQA($iSurveyID, $oAllQuestions, $sLanguage);
     $helper = new statistics_helper();
     switch ($docType) {
         case 'pdf':
             $sTempFile = $helper->generate_statistics($iSurveyID, $aSummary, $aSummary, $graph, $docType, 'F', $sLanguage);
             $sResult = file_get_contents($sTempFile);
             unlink($sTempFile);
             break;
         case 'xls':
             $sTempFile = $helper->generate_statistics($iSurveyID, $aSummary, $aSummary, '0', $docType, 'F', $sLanguage);
             $sResult = file_get_contents($sTempFile);
             unlink($sTempFile);
             break;
         case 'html':
             $sResult = $helper->generate_statistics($iSurveyID, $aSummary, $aSummary, '0', $docType, 'DD', $sLanguage);
             break;
     }
     return base64_encode($sResult);
 }
 /**
  * RPC routine to export statistics of a survey to a user.
  * Returns string - base64 encoding of the statistics.
  *
  * @access public
  * @param string $sSessionKey Auth credentials
  * @param int $iSurveyID Id of the Survey
  * @param string $docType Type of documents the exported statistics should be
  * @param string $sLanguage Optional language of the survey to use
  * @param string $graph Create graph option
  * @param int|array $groupIDs An OPTIONAL array (ot a single int) containing the groups we choose to generate statistics from
  * @return string Base64 encoded string with the statistics file
  */
 public function export_statistics($sSessionKey, $iSurveyID, $docType = 'pdf', $sLanguage = null, $graph = '0', $groupIDs = null)
 {
     Yii::app()->loadHelper('admin/statistics');
     $tempdir = Yii::app()->getConfig("tempdir");
     if (!$this->_checkSessionKey($sSessionKey)) {
         return array('status' => 'Invalid session key');
     }
     $oSurvey = Survey::model()->findByPk($iSurveyID);
     if (!isset($oSurvey)) {
         return array('status' => 'Error: Invalid survey ID');
     }
     if (Survey::model()->findByPk($iSurveyID)->owner_id != $_SESSION['loginID']) {
         return array('status' => 'Error: No Permission');
     }
     $aAdditionalLanguages = array_filter(explode(" ", $oSurvey->additional_languages));
     if (is_null($sLanguage) || !in_array($sLanguage, $aAdditionalLanguages)) {
         $sLanguage = $oSurvey->language;
     }
     if ($groupIDs != null) {
         if (is_int($groupIDs)) {
             $groupIDs = array($groupIDs);
         }
         if (is_array($groupIDs)) {
             //check that every value of the array belongs to the survey defined
             $aGroups = Groups::model()->findAllByAttributes(array('sid' => $iSurveyID));
             foreach ($aGroups as $group) {
                 $validGroups[] = $group['gid'];
             }
             $groupIDs = array_intersect($groupIDs, $validGroups);
             if (empty($groupIDs)) {
                 return array('status' => 'Error: Invalid group ID');
             }
             //and then get all the questions for these groups
             $criteria = new CDbCriteria();
             $criteria->addInCondition('gid', $groupIDs);
             $criteria->addCondition('sid = ' . $iSurveyID);
             $criteria->addCondition('parent_qid = 0');
             $criteria->addCondition('language = :lang');
             $criteria->params[':lang'] = $sLanguage;
             $oAllQuestions = Questions::model()->findAll($criteria);
         } else {
             return array('status' => 'Error: Invalid group ID');
         }
     } else {
         $oAllQuestions = Questions::model()->findAllByAttributes(array('sid' => $iSurveyID, 'parent_qid' => '0', 'language' => $sLanguage));
     }
     usort($oAllQuestions, 'groupOrderThenQuestionOrder');
     $aSummary = createCompleteSGQA($iSurveyID, $oAllQuestions, $sLanguage);
     $helper = new statistics_helper();
     switch ($docType) {
         case 'pdf':
             $sTempFile = $helper->generate_statistics($iSurveyID, $aSummary, $aSummary, $graph, $docType, 'F', $sLanguage);
             $sResult = file_get_contents($sTempFile);
             unlink($sTempFile);
             break;
         case 'xls':
             $sTempFile = $helper->generate_statistics($iSurveyID, $aSummary, $aSummary, '0', $docType, 'F', $sLanguage);
             $sResult = file_get_contents($sTempFile);
             unlink($sTempFile);
             break;
         case 'html':
             $sResult = $helper->generate_statistics($iSurveyID, $aSummary, $aSummary, '0', $docType, 'DD', $sLanguage);
             break;
     }
     return base64_encode($sResult);
 }