Skip to content

Commit

Permalink
Fixed issue: Various errors when using Postgres database
Browse files Browse the repository at this point in the history
  • Loading branch information
c-schmitz committed Mar 17, 2012
1 parent 9835aad commit 371cea7
Show file tree
Hide file tree
Showing 4 changed files with 372 additions and 364 deletions.
13 changes: 4 additions & 9 deletions application/helpers/admin/import_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3509,13 +3509,11 @@ function XMLImportSurvey($sFullFilepath,$sXMLdata=NULL,$sNewSurveyName=NULL,$iDe
switchMSSQLIdentityInsert('groups',true);
$insertdata['gid']=$aGIDReplacements[$oldgid];
}
$result = Groups::model()->insertRecords($insertdata) or safeDie($clang->gT("Error").": Failed to insert data<br />");
$newgid = Groups::model()->insertRecords($insertdata) or safeDie($clang->gT("Error").": Failed to insert data<br />");
$results['groups']++;

if (!isset($aGIDReplacements[$oldgid]))
{
$newgid=Yii::app()->db->createCommand('Select LAST_INSERT_ID()')->query()->read();
$newgid=$newgid['LAST_INSERT_ID()'];
$aGIDReplacements[$oldgid]=$newgid; // add old and new qid to the mapping array
}
else
Expand Down Expand Up @@ -3560,11 +3558,10 @@ function XMLImportSurvey($sFullFilepath,$sXMLdata=NULL,$sNewSurveyName=NULL,$iDe
}
if ($xssfilter)
XSSFilterArray($insertdata);
$result = Questions::model()->insertRecords($insertdata) or safeDie($clang->gT("Error").": Failed to insert data<br />");
$newqid = Questions::model()->insertRecords($insertdata) or safeDie($clang->gT("Error").": Failed to insert data<br />");
if (!isset($aQIDReplacements[$oldqid]))
{
$newqid=Yii::app()->db->createCommand('Select LAST_INSERT_ID()')->query()->read();
$aQIDReplacements[$oldqid]=$newqid['LAST_INSERT_ID()'];
$aQIDReplacements[$oldqid]=$newqid;
$results['questions']++;
}
else
Expand Down Expand Up @@ -3603,9 +3600,7 @@ function XMLImportSurvey($sFullFilepath,$sXMLdata=NULL,$sNewSurveyName=NULL,$iDe
}
if ($xssfilter)
XSSFilterArray($insertdata);
$result =Questions::model()->insertRecords($insertdata) or safeDie($clang->gT("Error").": Failed to insert data<br />");
$newsqid=Yii::app()->db->createCommand('Select LAST_INSERT_ID()')->query()->read();
$newsqid=$newsqid['LAST_INSERT_ID()'];
$newsqid =Questions::model()->insertRecords($insertdata) or safeDie($clang->gT("Error").": Failed to insert data<br />");
if (!isset($insertdata['qid']))
{
$aQIDReplacements[$oldsqid]=$newsqid; // add old and new qid to the mapping array
Expand Down
24 changes: 12 additions & 12 deletions application/helpers/frontend_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,7 @@ function buildsurveysession($surveyid,$previewGroup=false)
// first find all groups and their groups IDS
$criteria = new CDbCriteria;
$criteria->addColumnCondition(array('sid' => $surveyid, 'language' => $_SESSION['survey_'.$surveyid]['s_lang']));
$criteria->addCondition('randomization_group != ""');
$criteria->addCondition("randomization_group != ''");
$oData = Groups::model()->findAll($criteria);
foreach($oData as $aGroup)
{
Expand Down Expand Up @@ -2045,14 +2045,14 @@ function buildsurveysession($surveyid,$previewGroup=false)
}

// Defaults need to be set within Expression Manager so that it can process defaults comprised of equations
// // Prefill question/answer from defaultvalues
// foreach ($fieldmap as $field)
// {
// if (isset($field['defaultvalue']))
// {
// $_SESSION['survey_'.$surveyid][$field['fieldname']]=$field['defaultvalue'];
// }
// }
// // Prefill question/answer from defaultvalues
// foreach ($fieldmap as $field)
// {
// if (isset($field['defaultvalue']))
// {
// $_SESSION['survey_'.$surveyid][$field['fieldname']]=$field['defaultvalue'];
// }
// }
// Prefill questions/answers from command line params
$startingValues=array();
if (isset($_SESSION['survey_'.$surveyid]['insertarray']))
Expand Down Expand Up @@ -2094,9 +2094,9 @@ function buildsurveysession($surveyid,$previewGroup=false)
{
$_SESSION['survey_'.$surveyid][$field] = $value;
$startingValues[$field] = array (
'type'=>$type,
'value'=>$value,
);
'type'=>$type,
'value'=>$value,
);
}
}
}
Expand Down
280 changes: 143 additions & 137 deletions application/models/Groups.php
Original file line number Diff line number Diff line change
@@ -1,156 +1,162 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id$
* Files Purpose: lots of common functions
*/

class Groups extends CActiveRecord
{
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param string $class
* @return CActiveRecord
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}

/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{groups}}';
}

/**
* Returns the primary key of this table
*
* @access public
* @return string
*/
public function primaryKey()
{
return 'gid';
}

/**
* Defines the relations for this model
*
* @access public
* @return array
*/
public function relations()
/*
* LimeSurvey
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id$
* Files Purpose: lots of common functions
*/

class Groups extends CActiveRecord
{
return array('questions' => array(self::HAS_MANY, 'Questions', 'gid'));
}
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param string $class
* @return CActiveRecord
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}

/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{groups}}';
}

/**
* Returns the primary key of this table
*
* @access public
* @return string
*/
public function primaryKey()
{
return 'gid';
}

/**
* Defines the relations for this model
*
* @access public
* @return array
*/
public function relations()
{
return array('questions' => array(self::HAS_MANY, 'Questions', 'gid'));
}

function getAllRecords($condition=FALSE, $order=FALSE, $return_query = TRUE)
{
$query = Yii::app()->db->createCommand()->select('*')->from('{{groups}}');
function getAllRecords($condition=FALSE, $order=FALSE, $return_query = TRUE)
{
$query = Yii::app()->db->createCommand()->select('*')->from('{{groups}}');

if ($condition != FALSE)
{
$query->where($condition);
}
if ($condition != FALSE)
{
$query->where($condition);
}

if($order != FALSE)
{
$query->order($order);
}
if($order != FALSE)
{
$query->order($order);
}

return ( $return_query ) ? $query->queryAll() : $query;
}
return ( $return_query ) ? $query->queryAll() : $query;
}

function updateGroupOrder($sid,$lang,$position=0)
{
$data=Yii::app()->db->createCommand()->select('gid')
->where(array('and','sid=:sid','language=:language'))
->order('group_order, group_name ASC')
->from('{{groups}}')
->bindParam(':sid', $sid, PDO::PARAM_INT)
->bindParam(':language', $language, PDO::PARAM_STR)
->query();

$position = intval($position);
foreach($data->readAll() as $row)
function updateGroupOrder($sid,$lang,$position=0)
{
Yii::app()->db->createCommand()->update($this->tableName(),array('group_order' => $position),'gid='.$row['gid']);
$position++;
}
}
$data=Yii::app()->db->createCommand()->select('gid')
->where(array('and','sid=:sid','language=:language'))
->order('group_order, group_name ASC')
->from('{{groups}}')
->bindParam(':sid', $sid, PDO::PARAM_INT)
->bindParam(':language', $language, PDO::PARAM_STR)
->query();

$position = intval($position);
foreach($data->readAll() as $row)
{
Yii::app()->db->createCommand()->update($this->tableName(),array('group_order' => $position),'gid='.$row['gid']);
$position++;
}
}

public function insertRecords($data)
{
$group = new self;
foreach ($data as $k => $v)
$group->$k = $v;
return $group->save();
}
/**
* Insert an array into the groups table
* Returns false if insertion fails, otherwise the new GID
*
* @param array $data array_merge
*/
public function insertRecords($data)
{
$group = new self;
foreach ($data as $k => $v)
$group->$k = $v;
if (!$group->save()) return false;
else return $group->gid;
}

function getGroups($surveyid) {
$language = Survey::model()->findByPk($surveyid)->language;
return Yii::app()->db->createCommand()
->select(array('gid', 'group_name'))
->from($this->tableName())
->where(array('and', 'sid=:surveyid', 'language=:language'))
->order('group_order asc')
->bindParam(":language", $language, PDO::PARAM_STR)
->bindParam(":surveyid", $surveyid, PDO::PARAM_INT)
->query()->readAll();
}
function getGroups($surveyid) {
$language = Survey::model()->findByPk($surveyid)->language;
return Yii::app()->db->createCommand()
->select(array('gid', 'group_name'))
->from($this->tableName())
->where(array('and', 'sid=:surveyid', 'language=:language'))
->order('group_order asc')
->bindParam(":language", $language, PDO::PARAM_STR)
->bindParam(":surveyid", $surveyid, PDO::PARAM_INT)
->query()->readAll();
}

public static function deleteWithDependency($groupId, $surveyId)
{
$questionIds = Groups::getQuestionIdsInGroup($groupId);
Questions::deleteAllById($questionIds);
Assessment::model()->deleteAllByAttributes(array('sid' => $surveyId, 'gid' => $groupId));
return Groups::model()->deleteAllByAttributes(array('sid' => $surveyId, 'gid' => $groupId));
}
public static function deleteWithDependency($groupId, $surveyId)
{
$questionIds = Groups::getQuestionIdsInGroup($groupId);
Questions::deleteAllById($questionIds);
Assessment::model()->deleteAllByAttributes(array('sid' => $surveyId, 'gid' => $groupId));
return Groups::model()->deleteAllByAttributes(array('sid' => $surveyId, 'gid' => $groupId));
}

private static function getQuestionIdsInGroup($groupId) {
$questions = Yii::app()->db->createCommand()
->select('qid')
->from('{{questions}} q')
->join('{{groups}} g', 'g.gid=q.gid AND g.gid=:groupid AND q.parent_qid=0')
->group('qid')
->bindParam(":groupid", $groupId, PDO::PARAM_INT)
->queryAll();

$questionIds = array();
foreach ($questions as $question) {
$questionIds[] = $question['qid'];
private static function getQuestionIdsInGroup($groupId) {
$questions = Yii::app()->db->createCommand()
->select('qid')
->from('{{questions}} q')
->join('{{groups}} g', 'g.gid=q.gid AND g.gid=:groupid AND q.parent_qid=0')
->group('qid')
->bindParam(":groupid", $groupId, PDO::PARAM_INT)
->queryAll();

$questionIds = array();
foreach ($questions as $question) {
$questionIds[] = $question['qid'];
}

return $questionIds;
}

return $questionIds;
}

function getAllGroups($condition, $order=false)
{
$command = Yii::app()->db->createCommand()->where($condition)->select('*')->from($this->tableName());
if ($order != FALSE)
function getAllGroups($condition, $order=false)
{
$command->order($order);
$command = Yii::app()->db->createCommand()->where($condition)->select('*')->from($this->tableName());
if ($order != FALSE)
{
$command->order($order);
}
return $command->query();
}
return $command->query();
}
}
?>

0 comments on commit 371cea7

Please sign in to comment.