/**
  * Export to csv using optional search/filter
  * 
  * @param type $search
  */
 private function csvExport($search = null)
 {
     Yii::app()->loadHelper('export');
     $attid = ParticipantAttributeNames::model()->getVisibleAttributes();
     //If super admin all the participants will be visible
     if (Yii::app()->session['USER_RIGHT_SUPERADMIN']) {
         $iUserID = null;
     } else {
         $iUserID = Yii::app()->session['loginID'];
     }
     $query = Participants::model()->getParticipants(0, 0, $attid, null, $search, $iUserID);
     if (!$query) {
         return false;
     }
     // Field names in the first row
     $fields = array('participant_id', 'firstname', 'lastname', 'email', 'language', 'blacklisted', 'owner_uid');
     $outputarray = array();
     // The array to be passed to the export helper to be written to a csv file
     $outputarray[0] = $fields;
     //fields written to output array
     // If attribute fields are selected, add them to the output
     $queryId = Yii::app()->request->getQuery('id');
     if (!is_null($queryId) && $queryId != "null") {
         $iAttributeId = explode(",", $queryId);
         foreach ($iAttributeId as $key => $value) {
             $fields[] = 'a' . $value;
             $attributename = ParticipantAttributeNames::model()->getAttributeNames($value);
             $outputarray[0][] = $attributename[0]['attribute_name'];
         }
     }
     $fieldKeys = array_flip($fields);
     foreach ($query as $field => $aData) {
         $outputarray[] = array_intersect_key($aData, $fieldKeys);
     }
     CPDBExport($outputarray, "central_" . time());
 }
 /**
  * Export to csv using optional search/filter
  * 
  * @param type $search  CDCriteria?
  * @paran mixed $mAttributeIDs Empty array for no attributes, or array of attribute IDs or null for all attributes 
  */
 private function csvExport($search = null, $aAttributeIDs = null)
 {
     Yii::app()->loadHelper('export');
     //If super admin all the participants will be visible
     if (Permission::model()->hasGlobalPermission('superadmin', 'read')) {
         $iUserID = null;
     } else {
         $iUserID = Yii::app()->session['loginID'];
     }
     $query = Participant::model()->getParticipants(0, 0, $aAttributeIDs, null, $search, $iUserID);
     if (!$query) {
         return false;
     }
     // Field names in the first row
     $fields = array('participant_id', 'firstname', 'lastname', 'email', 'language', 'blacklisted', 'owner_uid');
     $outputarray = array();
     // The array to be passed to the export helper to be written to a csv file
     $outputarray[0] = $fields;
     //fields written to output array
     // If attribute fields are selected, add them to the output
     if ($aAttributeIDs == null) {
         $aAttributes = ParticipantAttributeName::model()->getAllAttributes();
     } else {
         foreach ($aAttributeIDs as $value) {
             if ($value == 0) {
                 continue;
             }
             $fields[] = 'a' . $value;
             $attributename = ParticipantAttributeName::model()->getAttributeNames($value);
             $outputarray[0][] = $attributename[0]['attribute_name'];
         }
     }
     $fieldKeys = array_flip($fields);
     foreach ($query as $field => $aData) {
         $outputarray[] = array_intersect_key($aData, $fieldKeys);
     }
     CPDBExport($outputarray, "central_" . time());
 }
 /**
  * Responsible for reading the CSV file line by line, check for duplicate participants
  * invalid participants and invalid attributes and copy them to the central table
  * Also responsible for creation of new attribute and mapping of old attribute to attribute in csv
  */
 function exporttocsv()
 {
     Yii::app()->loadHelper('export');
     $searchconditionurl = Yii::app()->request->getPost('searchcondition');
     $searchcondition = basename($searchconditionurl);
     if (Yii::app()->session['USER_RIGHT_SUPERADMIN']) {
         if ($searchcondition != 'getParticipants_json') {
             $condition = explode("||", $searchcondition);
             $query = Participants::model()->getParticipantsSearchMultiple($condition, 0, 0);
         } else {
             $query = Participants::model()->getParticipantsWithoutLimit();
         }
     } else {
         $iUserID = Yii::app()->session['loginID'];
         // else only the
         $query = Participants::model()->getParticipantsOwner($iUserID);
     }
     if (!$query) {
         return false;
     }
     // Field names in the first row
     $fields = array('participant_id', 'firstname', 'lastname', 'email', 'language', 'blacklisted', 'owner_uid');
     $i = 0;
     $outputarray = array();
     // The array to be passed to the export helper to be written to a csv file
     foreach ($fields as $field) {
         $outputarray[0][$i] = $field;
         //fields written to output array
         $i++;
     }
     if (Yii::app()->request->getQuery('id') == "null") {
         $i = 1;
         $j = 0;
         foreach ($query as $field => $aData) {
             foreach ($fields as $field) {
                 $outputarray[$i][$j] = $aData[$field];
                 $j++;
             }
             $i++;
         }
         CPDBExport($outputarray, "central_" . time());
     } else {
         $iAttributeId = explode(",", Yii::app()->request->getQuery('id'));
         foreach ($iAttributeId as $key => $value) {
             $attributename = ParticipantAttributeNames::model()->getAttributeNames($value);
             $outputarray[0][$i] = $attributename[0]['attribute_name'];
             $i++;
         }
         $i = 1;
         $j = 0;
         // Fetching the table data
         foreach ($query as $field => $aData) {
             foreach ($fields as $field) {
                 $outputarray[$i][$j] = $aData[$field];
                 $j++;
             }
             foreach ($iAttributeId as $key => $value) {
                 $answer = ParticipantAttributeNames::model()->getAttributeValue($aData['participant_id'], $value);
                 if (isset($answer['value'])) {
                     $outputarray[$i][$j] = $answer['value'];
                     $j++;
                 } else {
                     $outputarray[$i][$j] = "";
                     $j++;
                 }
             }
             $i++;
         }
         CPDBExport($outputarray, "central_" . time());
     }
 }