Skip to content

Commit

Permalink
Dev Fixed Scrutinizer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
c-schmitz committed Dec 12, 2017
1 parent 306cc98 commit 2b8dcc0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function getMenuWidget($surveyId = null, $showLoader = false)
$params = array(
'sa' => 'clearAllNotifications',
);
if ($surveyId) {
if ($surveyId!==null) {
$params['surveyId'] = $surveyId;
}
$data['clearAllNotificationsUrl'] = Yii::app()->createUrl('admin/notification', $params);
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/admin/limereplacementfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function index()
$questionlist = $this->_getQuestionList($action, $gid, $qid, $fieldmap, $fieldtype, $surveyformat);
$childQuestions = $this->_getChildQuestions($questionlist);
}

$data = [];
$data['countfields'] = count($replacementFields);
asort($replacementFields);
$data['replFields'] = $replacementFields;
Expand Down
64 changes: 34 additions & 30 deletions application/controllers/admin/participantsaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function openModalParticipantPanel()
break;
default:
// Unknown modal target
assert(false);
safeDie('Unknown method');
break;
}
}
Expand Down Expand Up @@ -305,7 +305,7 @@ public function displayParticipants()
$request = Yii::app()->request;
$participantParam = $request->getPost('Participant');
if ($participantParam) {
$model->attributes = $participantParam;
$model->setAttributes($participantParam, false);
}
$searchcondition = $request->getPost('searchcondition');
$searchparams = array();
Expand Down Expand Up @@ -368,26 +368,27 @@ public function deleteParticipant()
}

if (is_array($participantIds)) {
$participantIds = join($participantIds, ',');
$participantIds = implode(',', $participantIds);
}

// Deletes from participants only
$deletedParticipants = null;
if ($selectoption == 'po') {
$deletedParticipants = Participant::model()->deleteParticipants($participantIds);
}
// Deletes from central and token table
else if ($selectoption == 'ptt') {
Participant::model()->deleteParticipantToken($participantIds);
$deletedParticipants = Participant::model()->deleteParticipantToken($participantIds);
}
// Deletes from central , token and assosiated responses as well
else if ($selectoption == 'ptta') {
Participant::model()->deleteParticipantTokenAnswer($participantIds);
$deletedParticipants = Participant::model()->deleteParticipantTokenAnswer($participantIds);
} else {
// Internal error
throw new InvalidArgumentException('Unknown select option: '.$selectoption);
}

if ($deletedParticipants == 0) {
if ($deletedParticipants === 0) {
ls\ajax\AjaxHelper::outputError(gT('No participants deleted'));
} else {
ls\ajax\AjaxHelper::outputSuccess(gT('Participant deleted'));
Expand Down Expand Up @@ -577,7 +578,7 @@ public function updateParticipant($aData, array $extraAttributes = array())
}

$participant->attributes = $aData;
$success['participant'] = $participant->save();
$participant->save();

foreach ($extraAttributes as $htmlName => $attributeValue) {
list(,$attribute_id) = explode('_', $htmlName);
Expand Down Expand Up @@ -625,7 +626,7 @@ public function addParticipant($aData, array $extraAttributes = array())
ls\ajax\AjaxHelper::outputError('Could not add new participant: '.$result);
} else {
// "Impossible"
assert(false);
safeDie('Could not add participant.');
}
} else {
ls\ajax\AjaxHelper::outputNoPermission();
Expand Down Expand Up @@ -662,30 +663,31 @@ public function attributeMapCSV()
$sFilePath = Yii::app()->getConfig('tempdir').DIRECTORY_SEPARATOR.$sRandomFileName;
$aPathinfo = pathinfo($_FILES['the_file']['name']);
$sExtension = $aPathinfo['extension'];
$bMoveFileResult = false;
if ($_FILES['the_file']['error'] == 1 || $_FILES['the_file']['error'] == 2) {
$bMoveFileResult = null; // Scrutinizer does not understand that this block halt execution
$filterblankemails = null; // Same
Yii::app()->setFlashMessage(sprintf(gT("Sorry, this file is too large. Only files up to %01.2f MB are allowed."), getMaximumFileUploadSize() / 1024 / 1024), 'error');
Yii::app()->getController()->redirect(array('admin/participants/sa/importCSV'));
Yii::app()->end();
} elseif (strtolower($sExtension) == 'csv') {
$bMoveFileResult = @move_uploaded_file($_FILES['the_file']['tmp_name'], $sFilePath);
$filterblankemails = Yii::app()->request->getPost('filterbea');
} else {
$bMoveFileResult = null; // Scrutinizer does not understand that this block halt execution
$filterblankemails = null; // Same
Yii::app()->setFlashMessage(gT("This is not a .csv file."), 'error');
Yii::app()->getController()->redirect(array('admin/participants/sa/importCSV'));
Yii::app()->end();
}

if (!$bMoveFileResult) {
if ($bMoveFileResult===false) {
Yii::app()->setFlashMessage(gT("An error occurred uploading your file. This may be caused by incorrect permissions for the application /tmp folder."), 'error');
Yii::app()->getController()->redirect(array('admin/participants/sa/importCSV'));
Yii::app()->end();
} else {
$regularfields = array('firstname', 'participant_id', 'lastname', 'email', 'language', 'blacklisted', 'owner_uid');
$oCSVFile = fopen($sFilePath, 'r');
if ($oCSVFile===false)
{
safeDie('File not found.');
}
$aFirstLine = fgets($oCSVFile);
rewind($oCSVFile);

Expand All @@ -708,7 +710,7 @@ public function attributeMapCSV()
}
$fieldlist[] = $value;
}
$iLineCount = count(array_filter(array_filter(file($sFilePath), 'trim')));
$iLineCount = count(array_filter(array_filter((array)file($sFilePath), 'trim')));

$attributes = ParticipantAttributeName::model()->model()->getCPDBAttributes();
$aData = array(
Expand Down Expand Up @@ -773,7 +775,7 @@ public function uploadCSV()
$mappedarray = array();
}
/* Adjust system settings to read file with MAC line endings */
@ini_set('auto_detect_line_endings', true);
@ini_set('auto_detect_line_endings', '1');
/* Open the uploaded file into an array */
$tokenlistarray = file($sFilePath);

Expand All @@ -800,6 +802,13 @@ public function uploadCSV()
if (!isset($uploadcharset)) {
$uploadcharset = 'auto';
}
$allowedfieldnames = array('participant_id', 'firstname', 'lastname', 'email', 'language', 'blacklisted');
$aFilterDuplicateFields = array('firstname', 'lastname', 'email');
if (!empty($mappedarray)) {
foreach ($mappedarray as $key => $value) {
array_push($allowedfieldnames, strtolower($value));
}
}
foreach ($tokenlistarray as $buffer) {
//Iterate through the CSV file line by line
$buffer = @mb_convert_encoding($buffer, "UTF-8", $uploadcharset);
Expand All @@ -808,13 +817,7 @@ public function uploadCSV()
//first line, which contains field names, not values to import
// Pick apart the first line
$buffer = removeBOM($buffer);
$allowedfieldnames = array('participant_id', 'firstname', 'lastname', 'email', 'language', 'blacklisted');
$aFilterDuplicateFields = array('firstname', 'lastname', 'email');
if (!empty($mappedarray)) {
foreach ($mappedarray as $key => $value) {
array_push($allowedfieldnames, strtolower($value));
}
}

//For Attributes
switch ($separator) {
case 'comma':
Expand Down Expand Up @@ -1151,7 +1154,7 @@ public function storeBlacklistValues()
{
$values = Array('blacklistallsurveys', 'blacklistnewsurveys', 'blockaddingtosurveys', 'hideblacklisted', 'deleteblacklisted', 'allowunblacklist');
foreach ($values as $value) {
if ($find = SettingGlobal::model()->findByPk($value)) {
if (SettingGlobal::model()->findByPk($value)) {
SettingGlobal::model()->updateByPk(
$value,
array(
Expand All @@ -1172,7 +1175,8 @@ public function storeBlacklistValues()
/**
* AJAX Method to change the blacklist status of a participant
* Requires POST with 'participant_id' (varchar) and 'blacklist' (boolean)
* @return json-encoded array with 'success' (boolean) and 'newValue' ('Y' || 'N')
* Echos JSON-encoded array with 'success' (boolean) and 'newValue' ('Y' || 'N')
* @return void
*/
public function changeblackliststatus()
{
Expand Down Expand Up @@ -1336,6 +1340,7 @@ public function editAttributeName()
$AttributeNameLanguages = Yii::app()->request->getPost('ParticipantAttributeNameLanguages');
$ParticipantAttributeNamesDropdown = Yii::app()->request->getPost('ParticipantAttributeNamesDropdown');
$operation = Yii::app()->request->getPost('oper');
$success = [];
if ($operation === 'edit') {
$ParticipantAttributNamesModel = ParticipantAttributeName::model()->findByPk($AttributeNameAttributes['attribute_id']);
$success[] = $ParticipantAttributNamesModel->saveAttribute($AttributeNameAttributes);
Expand Down Expand Up @@ -1523,6 +1528,7 @@ public function getAttribute_json()
}

/* Go through the empty attributes and build an entry in the output for them */
$outputs = [];
foreach ($attributenotdone as $row) {
$outputs[$i] = array("", $iParticipantId."_".$row['attribute_id'], $row['attribute_type'], $row['attribute_id'], $row['attribute_name'], "");
if ($row['attribute_type'] == "DD") {
Expand Down Expand Up @@ -1625,6 +1631,7 @@ public function saveAttribute()

/* New attribute value */
if (Yii::app()->request->getPost('attribute_value_name_1') || Yii::app()->request->getPost('attribute_value_name_1') == "0") {
$aDatavalues = [];
$i = 1;
$attvaluename = 'attribute_value_name_'.$i;
while (array_key_exists($attvaluename, $_POST) && $_POST[$attvaluename] != "") {
Expand Down Expand Up @@ -1685,7 +1692,7 @@ public function sharePanel()
{
$model = new ParticipantShare();
if (Yii::app()->request->getParam('ParticipantShare')) {
$model->attributes = Yii::app()->request->getParam('ParticipantShare');
$model->setAttributes(Yii::app()->request->getParam('ParticipantShare'),false);
}
// data to be passed to view
$aData = array(
Expand Down Expand Up @@ -1815,9 +1822,7 @@ public function getSurveyInfo_json()
$i = 0;
foreach ($records as $row) {
$oSurvey = Survey::model()->with(array('languagesettings'=>array('condition'=>'surveyls_language=language')))->findByAttributes(array('sid' => $row['survey_id']));
foreach ($oSurvey->languagesettings as $oLanguageSetting) {
$surveyname = $oLanguageSetting->surveyls_title;
}
$surveyname = $oSurvey->languagesettings[0]->surveyls_title;
$surveylink = "";
/* Check permissions of each survey before creating a link*/
if (!Permission::model()->hasSurveyPermission($row['survey_id'], 'tokens', 'read')) {
Expand Down Expand Up @@ -1888,7 +1893,6 @@ public function getSearchIDs()
public function getParticipantsResults_json()
{
$searchcondition = Yii::app()->request->getpost('searchcondition');
$finalcondition = array();
$condition = explode("||", $searchcondition);
$search = Participant::model()->getParticipantsSearchMultipleCondition($condition);
$this->getParticipants_json($search);
Expand Down Expand Up @@ -1926,7 +1930,7 @@ public function getParticipants_json($search = null)
$iUserID = Yii::app()->session['loginID'];
}
$aData->records = Participant::model()->getParticipantsCount($attid, $search, $iUserID);
$aData->total = ceil($aData->records / $limit);
$aData->total = (int)ceil($aData->records / $limit);
if ($page > $aData->total) {
$page = $aData->total;
}
Expand Down
12 changes: 8 additions & 4 deletions application/models/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,14 +942,15 @@ public function filterParticipantIDs($aParticipantIDs)
* Deletes CPDB participants identified by their participant ID from token tables
*
* @param string $sParticipantsIDs
* @return void
* @return integer Number of deleted participants
*/
public function deleteParticipantToken($sParticipantsIDs)
{
/* This function deletes the participant from the participants table,
the participant from any tokens table they're in (using the survey_links table to find them)
and then all the participants attributes. */
$aParticipantsIDChunks = array_chunk(explode(",", $sParticipantsIDs), 100);
$iDeletedParticipants=0;
foreach ($aParticipantsIDChunks as $aParticipantsIDs) {
$aParticipantsIDs = $this->filterParticipantIDs($aParticipantsIDs);
$aSurveyIDs = Yii::app()->db->createCommand()
Expand All @@ -966,8 +967,9 @@ public function deleteParticipantToken($sParticipantsIDs)
}
}
}
$this->deleteParticipants($sParticipantsIDs, false);
$iDeletedParticipants += $this->deleteParticipants($sParticipantsIDs, false);
}
return $iDeletedParticipants;
}

/**
Expand All @@ -977,12 +979,13 @@ public function deleteParticipantToken($sParticipantsIDs)
* and then all the participants attributes.
*
* @param string $sParticipantsIDs
* @return void
* @return integer Number of deleted participants
*/
public function deleteParticipantTokenAnswer($sParticipantsIDs)
{
$aParticipantsIDs = explode(",", $sParticipantsIDs);
$aParticipantsIDs = $this->filterParticipantIDs($aParticipantsIDs);
$iDeletedParticipants=0;

foreach ($aParticipantsIDs as $row) {
/** @var SurveyLink[] $tokens */
Expand Down Expand Up @@ -1027,8 +1030,9 @@ public function deleteParticipantTokenAnswer($sParticipantsIDs)
}
}
}
$this->deleteParticipants($sParticipantsIDs, false);
$iDeletedParticipants += $this->deleteParticipants($sParticipantsIDs, false);
}
return $iDeletedParticipants;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion application/models/ParticipantAttributeName.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ public function saveAttributeLanguages($data)
/**
* @param array $data
*/
public public function storeAttributeValues($data)
public function storeAttributeValues($data)
{
foreach ($data as $record) {
Yii::app()->db->createCommand()->insert('{{participant_attribute_values}}', $record);
Expand Down

0 comments on commit 2b8dcc0

Please sign in to comment.