Exemplo n.º 1
0
 function copyToCentral($surveyid, $newarr, $mapped, $overwriteauto = false, $overwriteman = false, $createautomap = true)
 {
     $tokenid = Yii::app()->session['participantid'];
     //List of token_id's to add to participants table
     $duplicate = 0;
     $sucessfull = 0;
     $writearray = array();
     $attid = array();
     //Will store the CPDB attribute_id of new or existing attributes keyed by CPDB at
     $pid = "";
     /* Grab all the existing attribute field names from the tokens table */
     $arr = Yii::app()->db->createCommand()->select('*')->from("{{tokens_{$surveyid}}}")->queryRow();
     if (is_array($arr)) {
         $tokenfieldnames = array_keys($arr);
         $tokenattributefieldnames = array_filter($tokenfieldnames, 'filterForAttributes');
     } else {
         $tokenattributefieldnames = array();
     }
     /* Automatically mapped attribute names are named "attribute_cpdb_[some_number]" */
     foreach ($tokenattributefieldnames as $key => $value) {
         if ($value[10] == 'c') {
             $autoattid = substr($value, 15);
             $mapped[$autoattid] = $value;
         }
     }
     /* Create new CPDB attributes */
     if (!empty($newarr)) {
         foreach ($newarr as $key => $value) {
             /* $key is the fieldname from the token table (ie "attribute_1")
              * $value is the 'friendly name' for the attribute (ie "Gender")
              */
             $insertnames = array('attribute_type' => 'TB', 'visible' => 'Y');
             Yii::app()->db->createCommand()->insert('{{participant_attribute_names}}', $insertnames);
             $attid[$key] = Yii::app()->db->getCommandBuilder()->getLastInsertID('{{participant_attribute_names}}');
             /* eg $attid['attribute_1']='8372' */
             $insertnameslang = array('attribute_id' => $attid[$key], 'attribute_name' => urldecode($value), 'lang' => Yii::app()->session['adminlang']);
             Yii::app()->db->createCommand()->insert('{{participant_attribute_names_lang}}', $insertnameslang);
         }
     }
     /* Add the participants to the CPDB = Iterate through each $tokenid and create the new CPDB id*/
     foreach ($tokenid as $key => $tid) {
         if (is_numeric($tid) && $tid != "") {
             /* Get the data for this participant from the tokens table */
             $tobeinserted = Yii::app()->db->createCommand()->select('participant_id,firstname,lastname,email,language')->where('tid = :tid')->from('{{tokens_' . intval($surveyid) . '}}')->bindParam(":tid", $tid, PDO::PARAM_INT)->queryRow();
             /* See if there are any existing CPDB entries that match on firstname,lastname and email */
             $query = Yii::app()->db->createCommand()->select('*')->from('{{participants}}')->where('firstname = :firstname AND lastname = :lastname AND email = :email')->bindParam(":firstname", $tobeinserted['firstname'], PDO::PARAM_STR)->bindParam(":lastname", $tobeinserted['lastname'], PDO::PARAM_STR)->bindParam(":email", $tobeinserted['email'], PDO::PARAM_STR)->queryAll();
             /* If there is already an existing entry, add to the duplicate count */
             if (count($query) > 0) {
                 $duplicate++;
                 //HERE is where we can add "overwrite" feature to update attribute values for existing participants
                 if ($overwriteauto == "true") {
                     if (!empty($newarr)) {
                         foreach ($newarr as $key => $value) {
                             Participants::updateAttributeValueToken($surveyid, $query[0]['participant_id'], $attid[$key], $key);
                         }
                     }
                 }
                 if ($overwriteman == "true") {
                     /* Now add mapped attribute values */
                     if (!empty($mapped)) {
                         foreach ($mapped as $cpdbatt => $tatt) {
                             Participants::updateAttributeValueToken($surveyid, $query[0]['participant_id'], $cpdbatt, $tatt);
                         }
                     }
                 }
             } else {
                 /* Create entry in participants table */
                 $black = !empty($tobeinserted['blacklisted']) ? $tobeinserted['blacklised'] : 'N';
                 $pid = !empty($tobeinserted['participant_id']) ? $tobeinserted['participant_id'] : $this->gen_uuid();
                 $writearray = array('participant_id' => $pid, 'firstname' => $tobeinserted['firstname'], 'lastname' => $tobeinserted['lastname'], 'email' => $tobeinserted['email'], 'language' => $tobeinserted['language'], 'blacklisted' => $black, 'owner_uid' => Yii::app()->session['loginID']);
                 Yii::app()->db->createCommand()->insert('{{participants}}', $writearray);
                 //Update token table and insert the new UUID
                 $data = array("participant_id" => $pid);
                 Yii::app()->db->createCommand()->update('{{tokens_' . intval($surveyid) . '}}', $data, "tid = {$tid}");
                 /* Now add any new attribute values */
                 if (!empty($newarr)) {
                     foreach ($newarr as $key => $value) {
                         Participants::updateAttributeValueToken($surveyid, $pid, $attid[$key], $key);
                     }
                 }
                 /* Now add mapped attribute values */
                 if (!empty($mapped)) {
                     foreach ($mapped as $cpdbatt => $tatt) {
                         Participants::updateAttributeValueToken($surveyid, $pid, $cpdbatt, $tatt);
                     }
                 }
                 $sucessfull++;
                 /* Create a survey_link */
                 $data = array('participant_id' => $pid, 'token_id' => $tid, 'survey_id' => $surveyid, 'date_created' => date(DATE_W3C, time()));
                 Yii::app()->db->createCommand()->insert('{{survey_links}}', $data);
             }
         }
     }
     if (!empty($newarr)) {
         /* Rename the token attribute fields to a cpdb field, so in future
          * we know that it belongs to a CPDB field */
         foreach ($newarr as $key => $value) {
             $newname = 'attribute_cpdb_' . intval($attid[$key]);
             $fields = array($value => array('name' => $newname, 'type' => 'TEXT'));
             //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');
             $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]);
             $previousattribute = serialize($previousattribute);
             Yii::app()->db->createCommand()->update('{{surveys}}', array("attributedescriptions" => $previousattribute), 'sid = ' . $surveyid);
             //load description in the surveys table
         }
     }
     if (!empty($mapped)) {
         foreach ($mapped as $cpdbatt => $tatt) {
             if ($tatt[10] != 'c' && $createautomap == "true") {
                 // Change the fieldname in the token table to attribute_cpdb_[participant_attribute_id]
                 // so future mapping is done automatically
                 $newname = 'attribute_cpdb_' . $cpdbatt;
                 $fields = array($tatt => array('name' => $newname, 'type' => 'TEXT'));
                 Yii::app()->db->createCommand()->renameColumn('{{tokens_' . intval($surveyid) . '}}', $tatt, $newname);
                 Yii::app()->db->createCommand()->alterColumn('{{tokens_' . intval($surveyid) . '}}', $newname, 'TEXT');
                 $previousatt = Yii::app()->db->createCommand()->select('attributedescriptions')->from('{{surveys}}')->where("sid = :sid")->bindParam(":sid", $surveyid, PDO::PARAM_INT);
                 $previousattribute = $previousatt->queryRow();
                 $previousattribute = unserialize($previousattribute['attributedescriptions']);
                 $previousattribute[$newname] = $previousattribute[$tatt];
                 unset($previousattribute[$tatt]);
                 //Rename the token field the name of the participant_attribute
                 $attributedetails = ParticipantAttributeNames::getAttributeNames($cpdbatt);
                 $previousattribute[$newname]['description'] = $attributedetails[0]['attribute_name'];
                 $previousattribute = serialize($previousattribute);
                 //$newstring = str_replace($tatt, $newname, $previousattribute['attributedescriptions']);
                 Yii::app()->db->createCommand()->update('{{surveys}}', array("attributedescriptions" => $previousattribute), 'sid = ' . $surveyid);
             }
         }
     }
     $returndata = array('success' => $sucessfull, 'duplicate' => $duplicate, 'overwriteauto' => $overwriteauto, 'overwriteman' => $overwriteman);
     return $returndata;
 }