function getnotaddedAttributes($attributeid) { $notin = array(); foreach ($attributeid as $row) { $notin[] = $row; } $criteria = new CDbCriteria(); $criteria->addNotInCondition('t.attribute_id', $attributeid); $records = ParticipantAttributeNames::model()->with('participant_attribute_names_lang')->findAll($criteria); foreach ($records as $row) { //Iterate through each attribute $thisname = ""; $thislang = ""; foreach ($row->participant_attribute_names_lang as $names) { //Iterate through each language version of this attribute if ($thisname == "") { $thisname = $names->attribute_name; $thislang = $names->lang; } //Choose the first item by default if ($names->lang == Yii::app()->session['adminlang']) { $thisname = $names->attribute_name; $thislang = $names->lang; } //Override the default with the admin language version if found } $output[] = array('attribute_id' => $row->attribute_id, 'attribute_type' => $row->attribute_type, 'attribute_display' => $row->visible, 'attribute_name' => $thisname, 'lang' => $thislang); } return $output; }
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; }
function attributeMapToken() { Yii::app()->loadHelper('common'); $this->getController()->_js_admin_includes(Yii::app()->getConfig('adminscripts') . "attributeMapToken.js"); $this->getController()->_css_admin_includes(Yii::app()->getConfig('adminstyleurl') . "attributeMapToken.css"); $iSurveyId = Yii::app()->request->getQuery('sid'); $attributes = ParticipantAttributeNames::model()->getAttributes(); $tokenattributefieldnames = getTokenFieldsAndNames($iSurveyId, TRUE); $selectedattribute = array(); $selectedcentralattribute = array(); $alreadymappedattid = array(); $alreadymappedattdisplay = array(); $alreadymappedattnames = array(); $i = 0; $j = 0; foreach ($tokenattributefieldnames as $key => $value) { if (is_numeric($key[10])) { $selectedattribute[$value['description']] = $key; } else { $attributeid = substr($key, 15); $continue = false; foreach ($attributes as $attribute) { if ($attribute['attribute_id'] == $attributeid) { $continue = true; } } if ($continue) { array_push($alreadymappedattid, $attributeid); array_push($alreadymappedattdisplay, $key); $alreadymappedattnames[$key] = $value['description']; } else { $selectedattribute[$value['description']] = $key; } } } foreach ($attributes as $row) { if (!in_array($row['attribute_id'], $alreadymappedattid)) { $selectedcentralattribute[$row['attribute_id']] = $row['attribute_name']; } } $aData = array('attribute' => $selectedcentralattribute, 'tokenattribute' => $selectedattribute, 'alreadymappedattributename' => $alreadymappedattdisplay, 'alreadymappedattdescription' => $alreadymappedattnames); $this->_renderWrappedTemplate('participants', 'attributeMapToken', $aData); }
/** * Show dialogs and create a new tokens table */ function _newtokentable($iSurveyId) { $clang = $this->getController()->lang; Yii::import('application.helpers.admin.token_helper', true); if (Yii::app()->request->getPost('createtable') == "Y" && hasSurveyPermission($iSurveyId, 'surveyactivation', 'update')) { createTokenTable($iSurveyId); $this->_renderWrappedTemplate('token', array('message' => array('title' => $clang->gT("Token control"), 'message' => $clang->gT("A token table has been created for this survey.") . " (\"" . Yii::app()->db->tablePrefix . "tokens_{$iSurveyId}\")<br /><br />\n" . "<input type='submit' value='" . $clang->gT("Continue") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/index/surveyid/{$iSurveyId}") . "', '_top')\" />\n"))); } elseif (returnGlobal('restoretable') == "Y" && Yii::app()->request->getPost('oldtable') && hasSurveyPermission($iSurveyId, 'surveyactivation', 'update')) { //Rebuild attributedescription value for the surveys table $table = Yii::app()->db->schema->getTable(Yii::app()->request->getPost('oldtable')); $fields = array_filter(array_keys($table->columns), 'filterForAttributes'); foreach ($fields as $fieldname) { $name = $fieldname; if ($fieldname[10] == 'c') { //This belongs to a cpdb attribute $cpdbattid = substr($fieldname, 15); $data = ParticipantAttributeNames::model()->getAttributeName($cpdbattid, Yii::app()->session['adminlang']); $name = $data['attribute_name']; } $fieldcontents[$fieldname] = array('description' => $name, 'mandatory' => 'N', 'show_register' => 'N'); } Survey::model()->updateByPk($iSurveyId, array('attributedescriptions' => serialize($fieldcontents))); Yii::app()->db->createCommand()->renameTable(Yii::app()->request->getPost('oldtable'), Yii::app()->db->tablePrefix . "tokens_" . intval($iSurveyId)); //Add any survey_links from the renamed table Survey_links::model()->rebuildLinksFromTokenTable($iSurveyId); $this->_renderWrappedTemplate('token', array('message' => array('title' => $clang->gT("Import old tokens"), 'message' => $clang->gT("A token table has been created for this survey and the old tokens were imported.") . " (\"" . Yii::app()->db->tablePrefix . "tokens_{$iSurveyId}" . "\")<br /><br />\n" . "<input type='submit' value='" . $clang->gT("Continue") . "' onclick=\"window.open('" . $this->getController()->createUrl("admin/tokens/index/surveyid/{$iSurveyId}") . "', '_top')\" />\n"))); LimeExpressionManager::SetDirtyFlag(); // so that knows that token tables have changed } else { $this->getController()->loadHelper('database'); $result = Yii::app()->db->createCommand(dbSelectTablesLike("{{old_tokens_" . intval($iSurveyId) . "_%}}"))->queryAll(); $tcount = count($result); if ($tcount > 0) { foreach ($result as $rows) { $oldlist[] = reset($rows); } $aData['oldlist'] = $oldlist; } $thissurvey = getSurveyInfo($iSurveyId); $aData['thissurvey'] = $thissurvey; $aData['surveyid'] = $iSurveyId; $aData['tcount'] = $tcount; $aData['databasetype'] = Yii::app()->db->getDriverName(); $this->_renderWrappedTemplate('token', 'tokenwarning', $aData); } }