function copytosurveyatt($surveyid, $mapped, $newcreate, $participantid, $overwriteauto = false, $overwriteman = false, $overwritest = false, $createautomap = true)
 {
     Yii::app()->loadHelper('common');
     $duplicate = 0;
     $sucessfull = 0;
     $participantid = explode(",", $participantid);
     //List of participant ids to add to tokens table
     if ($participantid[0] == "") {
         $participantid = array_slice($participantid, 1);
     }
     $number2add = sanitize_int(count($newcreate));
     //Number of tokens being created
     $tokenattributefieldnames = array();
     //Will contain descriptions of existing token attribute fields
     $tokenfieldnames = array();
     //Will contain the actual field names of existing token attribute fields
     $attributesadded = array();
     //Will contain the actual field name of any new token attribute fields
     $attributeidadded = array();
     //Will contain the description of any new token attribute fields
     $fieldcontents = array();
     //Will contain serialised info for the surveys.attributedescriptions field
     $surveyinfo = getSurveyInfo($surveyid);
     $defaultsurveylang = $surveyinfo['surveyls_language'];
     $arr = Yii::app()->db->createCommand()->select('*')->from("{{tokens_{$surveyid}}}")->queryRow();
     if (is_array($arr)) {
         $tokenfieldnames = array_keys($arr);
         $tokenattributefieldnames = array_filter($tokenfieldnames, 'filterForAttributes');
     }
     /* Rename an existing attribute in the token table to this attribute so
      * it maps automatically in future.
      *
      * Fieldname renamed from the $key of $mappeed to the $val of $mapped
      * */
     if ($createautomap == "true") {
         foreach ($mapped as $key => $val) {
             if (is_numeric($val)) {
                 $newname = 'attribute_cpdb_' . intval($val);
                 //Rename the field in the tokens_[sid] table
                 Yii::app()->db->createCommand()->renameColumn('{{tokens_' . intval($surveyid) . '}}', $key, $newname);
                 //Make the field a TEXT field
                 Yii::app()->db->createCommand()->alterColumn('{{tokens_' . intval($surveyid) . '}}', $newname, 'TEXT');
                 /* Update the attributedescriptions info */
                 $previousatt = Yii::app()->db->createCommand()->select('attributedescriptions')->from('{{surveys}}')->where("sid = " . $surveyid);
                 $patt = $previousatt->queryRow();
                 $previousattribute = unserialize($patt['attributedescriptions']);
                 $previousattribute[$newname] = $previousattribute[$key];
                 unset($previousattribute[$key]);
                 //Rename the token field the name of the participant_attribute
                 $attributedetails = ParticipantAttributeNames::getAttributeNames($val);
                 $previousattribute[$newname]['description'] = $attributedetails[0]['attribute_name'];
                 $previousattribute = serialize($previousattribute);
                 Yii::app()->db->createCommand()->update('{{surveys}}', array("attributedescriptions" => $previousattribute), 'sid = ' . $surveyid);
                 //load description in the surveys table
                 //Finally, update the $mapped key/val pair to reflect the new keyname of $newname
                 $mapped[$newname] = $mapped[$key];
                 unset($mapped[$key]);
             }
         }
     }
     foreach ($tokenattributefieldnames as $key => $value) {
         if ($value[10] == 'c') {
             $attid = substr($value, 15);
             $mapped[$value] = $attid;
         } elseif (is_numeric($value[10])) {
             $mapped[$key] = $value;
         }
     }
     if (!empty($newcreate)) {
         foreach ($newcreate as $key => $value) {
             $newfieldname = 'attribute_cpdb_' . $value;
             $fields[$newfieldname] = array('type' => 'VARCHAR', 'constraint' => '255');
             $attname = Yii::app()->db->createCommand()->select('{{participant_attribute_names_lang}}.attribute_name, {{participant_attribute_names_lang}}.lang')->from('{{participant_attribute_names}}')->join('{{participant_attribute_names_lang}}', '{{participant_attribute_names}}.attribute_id = {{participant_attribute_names_lang}}.attribute_id')->where('{{participant_attribute_names}}.attribute_id = :attrid ')->bindParam(":attrid", $value, PDO::PARAM_INT);
             $attributename = $attname->queryAll();
             foreach ($attributename as $att) {
                 $languages[$att['lang']] = $att['attribute_name'];
             }
             //Check first for the default survey language
             if (isset($languages[$defaultsurveylang])) {
                 $newname = $languages[$defaultsurveylang];
             } elseif (isset($language[Yii::app()->session['adminlang']])) {
                 $newname = $languages[Yii::app()->session['adminlang']];
             } else {
                 $newname = $attributename[0]['attribute_name'];
                 //Choose the first item in the list
             }
             $tokenattributefieldnames[] = $newfieldname;
             $fieldcontents[$newfieldname] = array("description" => $newname, "mandatory" => "N", "show_register" => "N");
             array_push($attributeidadded, 'attribute_cpdb_' . $value);
             array_push($attributesadded, $value);
         }
         //Update the attributedescriptions in the survey table to include the newly created attributes
         $previousatt = Yii::app()->db->createCommand()->select('attributedescriptions')->where("sid = :sid")->from('{{surveys}}')->bindParam(":sid", $surveyid, PDO::PARAM_INT);
         $previousattribute = $previousatt->queryRow();
         $previousattribute = unserialize($previousattribute['attributedescriptions']);
         foreach ($fieldcontents as $key => $val) {
             $previousattribute[$key] = $val;
         }
         $previousattribute = serialize($previousattribute);
         Yii::app()->db->createCommand()->update('{{surveys}}', array("attributedescriptions" => $previousattribute), 'sid = ' . intval($surveyid));
         // load description in the surveys table
         //Actually create the fields in the tokens table
         foreach ($fields as $key => $value) {
             Yii::app()->db->createCommand("ALTER TABLE {{tokens_{$surveyid}}} ADD COLUMN " . Yii::app()->db->quoteColumnName($key) . " " . $value['type'] . " ( " . intval($value['constraint']) . " )")->query();
             // add columns in token's table
         }
     }
     //Write each participant to the survey token table
     foreach ($participantid as $key => $participant) {
         $writearray = array();
         $participantdata = Yii::app()->db->createCommand()->select('firstname,lastname,email,language,blacklisted')->where('participant_id = "' . $participant . '"')->from('{{participants}}');
         $tobeinserted = $participantdata->queryRow();
         /* Search for matching participant name/email in the survey token table */
         $query = Yii::app()->db->createCommand()->select('*')->from('{{tokens_' . $surveyid . '}}')->where('firstname = "' . $tobeinserted['firstname'] . '" AND lastname = "' . $tobeinserted['lastname'] . '" AND email = "' . $tobeinserted['email'] . '"')->queryAll();
         if (count($query) > 0) {
             //Participant already exists in token table - don't copy
             $duplicate++;
             // Here is where we can put code for overwriting the attribute data if so required
             if ($overwriteauto == "true") {
                 //If there are new attributes created, add those values to the token entry for this participant
                 if (!empty($newcreate)) {
                     $numberofattributes = count($attributesadded);
                     for ($a = 0; $a < $numberofattributes; $a++) {
                         Participants::updateTokenAttributeValue($surveyid, $participant, $attributesadded[$a], $attributeidadded[$a]);
                     }
                 }
                 //If there are automapped attributes, add those values to the token entry for this participant
                 if (!empty($mapped)) {
                     foreach ($mapped as $key => $value) {
                         if ($key[10] == 'c') {
                             //We know it's automapped because the 11th letter is 'c'
                             Participants::updateTokenAttributeValue($surveyid, $participant, $value, $key);
                         }
                     }
                 }
             }
             if ($overwriteman == "true") {
                 //If there are any manually mapped attributes, add those values to the token entry for this participant
                 if (!empty($mapped)) {
                     foreach ($mapped as $key => $value) {
                         if ($key[10] != 'c' && $key[9] == '_') {
                             //It's not an auto field because it's 11th character isn't 'c'
                             Participants::updateTokenAttributeValue($surveyid, $participant, $value, $key);
                         }
                     }
                 }
             }
             if ($overwritest == "true") {
                 if (!empty($mapped)) {
                     foreach ($mapped as $key => $value) {
                         if (strlen($key) > 8 && $key[10] != 'c' && $key[9] != '_' || strlen($key) < 9) {
                             Participants::updateTokenAttributeValue($surveyid, $participant, $value, $key);
                         }
                     }
                 }
             }
         } else {
             //Create a new token entry for this participant
             $writearray = array('participant_id' => $participant, 'firstname' => $tobeinserted['firstname'], 'lastname' => $tobeinserted['lastname'], 'email' => $tobeinserted['email'], 'emailstatus' => 'OK', 'language' => $tobeinserted['language']);
             Yii::app()->db->createCommand()->insert('{{tokens_' . $surveyid . '}}', $writearray);
             $insertedtokenid = Yii::app()->db->getCommandBuilder()->getLastInsertID('{{tokens_' . $surveyid . '}}');
             $time = time();
             //Create a survey link for the new token entry
             $data = array('participant_id' => $participant, 'token_id' => $insertedtokenid, 'survey_id' => $surveyid, 'date_created' => date(DATE_W3C, $time));
             Yii::app()->db->createCommand()->insert('{{survey_links}}', $data);
             //If there are new attributes created, add those values to the token entry for this participant
             if (!empty($newcreate)) {
                 $numberofattributes = count($attributesadded);
                 for ($a = 0; $a < $numberofattributes; $a++) {
                     Participants::updateTokenAttributeValue($surveyid, $participant, $attributesadded[$a], $attributeidadded[$a]);
                 }
             }
             //If there are any automatically mapped attributes, add those values to the token entry for this participant
             if (!empty($mapped)) {
                 foreach ($mapped as $key => $value) {
                     Participants::updateTokenAttributeValue($surveyid, $participant, $value, $key);
                 }
             }
             $sucessfull++;
         }
     }
     $returndata = array('success' => $sucessfull, 'duplicate' => $duplicate, 'overwriteauto' => $overwriteauto, 'overwriteman' => $overwriteman);
     return $returndata;
 }