Skip to content

Commit

Permalink
Dev Some Scrutinizer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
c-schmitz committed Dec 13, 2017
1 parent 1c02fe2 commit 86777b7
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 66 deletions.
5 changes: 4 additions & 1 deletion application/core/LSWebUser.php
@@ -1,6 +1,9 @@
<?php
Yii::import('application.helpers.Hash', true);

/**
* @property integer $id The user ID
*/
class LSWebUser extends CWebUser
{
protected $sessionVariable = 'LSWebUser';
Expand Down Expand Up @@ -45,7 +48,7 @@ public function getFlashes($delete = true)

/**
* @param string $key
* @param string $defaultValue
* @param mixed $defaultValue
* @return mixed|null
*/
public function getState($key, $defaultValue = null)
Expand Down
109 changes: 57 additions & 52 deletions application/core/plugins/ExportR/RSyntaxWriter.php
Expand Up @@ -7,8 +7,8 @@ class RSyntaxWriter extends Writer
private $maxLength = 25500; // From old code, max length of string fields

/**
* The open filehandle
*/
* The open filehandle
*/
protected $handle = null;
protected $customFieldmap = array();
protected $headers = array();
Expand Down Expand Up @@ -42,6 +42,7 @@ public function init(SurveyObj $survey, $sLanguageCode, FormattingOptions $oOpti
// R specific stuff
Yii::app()->loadHelper("export");
$tmpFieldmap = SPSSFieldMap($survey->id);
$fieldmap = [];
foreach ($tmpFieldmap as $field => $values) {
$fieldmap[$values['title']] = $values;
if (array_key_exists('sql_name', $values)) {
Expand Down Expand Up @@ -84,17 +85,18 @@ protected function outputRecord($headers, $values, FormattingOptions $oOptions)
public function close()
{
$errors = '';
foreach ($this->headers as $id => $title) {
foreach ($this->headers as $id => $title) {
$field = $this->customFieldmap[$title];
$i = $id + 1;
if ($field['SPSStype'] == 'DATETIME23.2') {
$field['size'] = '';
$field['size'] = '';
}

if ($field['LStype'] == 'N' || $field['LStype'] == 'K') {
$field['size'] .= '.'.($field['size'] - 1);
}

$type='';
switch ($field['SPSStype']) {
case 'F':
$type = "numeric";
Expand All @@ -107,73 +109,76 @@ public function close()
$type = "character";
//@TODO set $type to format for date
break;
default:
safeDie('Unknown type');
break;
}

$this->out("# LimeSurvey Field type: {$field['SPSStype']}");
$this->out("data[, ".$i."] <- "
$this->out("# LimeSurvey Field type: {$field['SPSStype']}");
$this->out("data[, ".$i."] <- "
. "as.$type(data[, ".$i."])");
$this->out('attributes(data)$variable.labels['.$i.'] <- "'
$this->out('attributes(data)$variable.labels['.$i.'] <- "'
. addslashes(
htmlspecialchars_decode(
mb_substr(
stripTagsFull(
$field['VariableLabel']
), 0, $this->maxLength
)
htmlspecialchars_decode(
mb_substr(
stripTagsFull(
$field['VariableLabel']
), 0, $this->maxLength
)
)
)
. '"');

// Create the value Labels!
if (isset($field['answers'])) {
$answers = $field['answers'];

//print out the value labels!
$str = 'data[, '.$i.'] <- factor(data[, '.$i.'], levels=c(';
foreach ($answers as $answer) {
if ($field['SPSStype'] == "F" && isNumericExtended($answer['code'])) {
$str .= "{$answer['code']},";
} else {
$str .= "\"{$answer['code']}\",";
}
}

$str = mb_substr($str, 0, -1);
$str .= '),labels=c(';
// Create the value Labels!
if (isset($field['answers'])) {
$answers = $field['answers'];

foreach ($answers as $answer) {
$str .= '"'.addslashes($answer['value']).'", ';
//print out the value labels!
$str = 'data[, '.$i.'] <- factor(data[, '.$i.'], levels=c(';
foreach ($answers as $answer) {
if ($field['SPSStype'] == "F" && isNumericExtended($answer['code'])) {
$str .= "{$answer['code']},";
} else {
$str .= "\"{$answer['code']}\",";
}
}

$str = mb_substr($str, 0, -2);
$str = mb_substr($str, 0, -1);
$str .= '),labels=c(';

if ($field['scale'] !== '' && $field['scale'] == 2) {
$scale = ", ordered=TRUE";
} else {
$scale = "";
}
foreach ($answers as $answer) {
$str .= '"'.addslashes($answer['value']).'", ';
}

$this->out("{$str}){$scale})");
$str = mb_substr($str, 0, -2);

if ($field['scale'] !== '' && $field['scale'] == 2) {
$scale = ", ordered=TRUE";
} else {
$scale = "";
}

//Rename the Variables (in case somethings goes wrong, we still have the OLD values
if (isset($field['sql_name'])) {
$ftitle = $field['title'];
if (!preg_match("/^([a-z]|[A-Z])+.*$/", $ftitle)) {
$ftitle = "q_".$ftitle;
}
$this->out("{$str}){$scale})");
}

$ftitle = str_replace(array("-", ":", ";", "!"), array("_hyph_", "_dd_", "_dc_", "_excl_"), $ftitle);
//Rename the Variables (in case somethings goes wrong, we still have the OLD values
if (isset($field['sql_name'])) {
$ftitle = $field['title'];
if (!preg_match("/^([a-z]|[A-Z])+.*$/", $ftitle)) {
$ftitle = "q_".$ftitle;
}

if ($ftitle != $field['title']) {
$errors .= "# Variable name was incorrect and was changed from {$field['title']} to $ftitle .\n";
}
$ftitle = str_replace(array("-", ":", ";", "!"), array("_hyph_", "_dd_", "_dc_", "_excl_"), $ftitle);

$this->out("names(data)[".$i."] <- "
. "\"".$ftitle."\""); // <AdV> added \n
} else {
$this->out("#sql_name not set");
if ($ftitle != $field['title']) {
$errors .= "# Variable name was incorrect and was changed from {$field['title']} to $ftitle .\n";
}

$this->out("names(data)[".$i."] <- "
. "\"".$ftitle."\""); // <AdV> added \n
} else {
$this->out("#sql_name not set");
}
} // end foreach
if (!empty($errors)) {
$this->out($errors);
Expand Down
5 changes: 1 addition & 4 deletions application/helpers/admin/export/ExcelWriter.php
Expand Up @@ -14,17 +14,14 @@ class ExcelWriter extends Writer
private $rowCounter;
private $forceDownload = true;

//Indicates if the Writer is outputting to a file rather than sending via HTTP.
private $outputToFile = false;

/**
* The presence of a filename will cause the writer to output to
* a file rather than send.
*
* @param string $filename
* @return ExcelWriter
*/
public function __construct($filename = null)
public function __construct()
{
require_once(APPPATH.'/third_party/xlsx_writer/xlsxwriter.class.php');
$this->separator = '~|';
Expand Down
7 changes: 6 additions & 1 deletion application/helpers/admin/export/JsonWriter.php
Expand Up @@ -25,13 +25,18 @@ public function init(SurveyObj $survey, $sLanguageCode, FormattingOptions $oOpti
echo $sStartOutput;
} elseif ($oOptions->output == 'file') {
$this->file = fopen($this->filename, 'w');
fwrite($this->file, $sStartOutput);
if ($this->file!==false) {
fwrite($this->file, $sStartOutput);
} else {
safeDie('Could not open JSON file');
}
}

}

protected function outputRecord($headers, $values, FormattingOptions $oOptions)
{
$aJson = [];
$aJson[$values[0]] = array_combine($headers, $values);
$sJson = json_encode($aJson);
Yii::log($this->havePrev, 'info', 'info');
Expand Down
Expand Up @@ -24,6 +24,12 @@ abstract class QuestionPluginBase extends PluginBase
* @param string $id
*/

/**
*
*
* @param PluginManager $manager
* @param integer $id
*/
public function __construct(PluginManager $manager, $id)
{
parent::__construct($manager, $id);
Expand Down Expand Up @@ -66,4 +72,3 @@ protected function registerCss($fileName)


}
?>
2 changes: 1 addition & 1 deletion application/models/Label.php
Expand Up @@ -108,7 +108,7 @@ public function getLabelCodeInfo($lid)
return Yii::app()->db->createCommand()->select('code, title, sortorder, language, assessment_value')->order('language, sortorder, code')->where('lid=:lid')->from($this->tableName())->bindParam(":lid", $lid, PDO::PARAM_INT)->query()->readAll();
}

function insertRecords($data)
public function insertRecords($data)
{
$lbls = new self;
foreach ($data as $k => $v) {
Expand Down
4 changes: 2 additions & 2 deletions application/models/QuotaMember.php
Expand Up @@ -103,7 +103,7 @@ public function getMemberInfo()
case "A":
case "B":
$temp = explode('-', $this->code);
$sFieldName = $this->sid->sid.'X'.$this->question->gid.'X'.$this->qid.$temp[0];
$sFieldName = $this->sid.'X'.$this->question->gid.'X'.$this->qid.$temp[0];
$sValue = $temp[1];
break;
default:
Expand Down Expand Up @@ -132,7 +132,7 @@ public function getMemberInfo()

}

function insertRecords($data)
public function insertRecords($data)
{
$members = new self;
foreach ($data as $k => $v) {
Expand Down
4 changes: 2 additions & 2 deletions application/models/Response.php
Expand Up @@ -42,7 +42,7 @@ public static function create($surveyId, $scenario = 'insert')
* @param integer $sQID The question ID - optional - Default 0
* @return array
*/
public function getFiles($sQID = 0)
public function getFiles()
{
$survey = Survey::model()->findByPk($this->dynamicId);
$questions = Question::model()->findAllByAttributes(array('sid' => $this->dynamicId, 'type' => '|', 'language'=>$survey->language));
Expand Down Expand Up @@ -120,7 +120,7 @@ public function deleteFiles()
* Delete uploaded files for this response AND modify
* response data to reflect all changes.
* Keep comment and title of file, but remove name/filename.
* @return string[] Name of files that could not be removed.
* @return array Number of successfully moved files and names of files that could not be removed/failed
*/
public function deleteFilesAndFilename()
{
Expand Down
4 changes: 2 additions & 2 deletions application/models/SavedControl.php
Expand Up @@ -53,7 +53,7 @@ public static function model($class = __CLASS__)
}


function getAllRecords($condition = false)
public function getAllRecords($condition = false)
{
if ($condition != false) {
$this->db->where($condition);
Expand Down Expand Up @@ -97,7 +97,7 @@ public function deleteSomeRecords($condition)
return $record->deleteAll($criteria);
}

function insertRecords($data)
public function insertRecords($data)
{
return $this->db->insert('saved_control', $data);
}
Expand Down

0 comments on commit 86777b7

Please sign in to comment.