Skip to content

Commit

Permalink
Updated feature: Port the delete questiongroup functionality to the Y…
Browse files Browse the repository at this point in the history
…ii framework (by Yaxar Maxson)

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_yii@11676 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
Pieter Jan Speelmans committed Dec 18, 2011
1 parent 5f575e6 commit 56b91d8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 59 deletions.
55 changes: 18 additions & 37 deletions application/controllers/admin/questiongroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function run($sa)
$this->route('import', array());
elseif ($sa == 'organize')
$this->route('organize', array('surveyid'));
elseif ($sa == 'delete')
$this->route('delete', array());
}

/**
Expand Down Expand Up @@ -303,49 +305,28 @@ function insert($surveyid)


/**
* questiongroup::delete()
* Function responsible for deleting a question group.
* @return
* Action to delete a question group.
*/
function delete()
{
$action = $this->input->post("action");
$surveyid = $this->input->post("sid");
$gid = $this->input->post("gid");
$clang = $this->limesurvey_lang;
if ($action == "delgroup" && bHasSurveyPermission($surveyid, 'surveycontent','delete'))
{
$this->load->helper('database');
$query = "SELECT qid FROM ".$this->db->dbprefix."groups g, ".$this->db->dbprefix."questions q WHERE g.gid=q.gid AND g.gid=$gid AND q.parent_qid=0 group by qid";
if ($result = db_execute_assoc($query)) // Checked
{
foreach ($result->result_array() as $row)
$surveyId = sanitize_int($_GET['surveyid']);
if (bHasSurveyPermission($surveyId, 'surveycontent', 'delete')) {
$groupId = sanitize_int($_GET['gid']);
$clang = $this->getController()->lang;

if (isset($_GET['sa']) && $_GET['sa'] == 'delete') {
$iGroupsDeleted = Groups::deleteWithDependency($groupId, $surveyId);

if ($iGroupsDeleted !== 1) {
fixSortOrderGroups($surveyId);
Yii::app()->user->setFlash('flashmessage', $clang->gT('The question group was deleted.'));
}
else
{
db_execute_assoc("DELETE FROM ".$this->db->dbprefix."conditions WHERE qid={$row['qid']}"); // Checked
db_execute_assoc("DELETE FROM ".$this->db->dbprefix."question_attributes WHERE qid={$row['qid']}"); // Checked
db_execute_assoc("DELETE FROM ".$this->db->dbprefix."answers WHERE qid={$row['qid']}"); // Checked
db_execute_assoc("DELETE FROM ".$this->db->dbprefix."questions WHERE qid={$row['qid']} or parent_qid={$row['qid']}"); // Checked
db_execute_assoc("DELETE FROM ".$this->db->dbprefix."defaultvalues WHERE qid={$row['qid']}"); // Checked
db_execute_assoc("DELETE FROM ".$this->db->dbprefix."quota_members WHERE qid={$row['qid']}");
Yii::app()->user->setFlash('flashmessage', $clang->gT('Group could not be deleted'));
}
$this->getController()->redirect($this->getController()->createUrl('admin/survey/view/' . $surveyId));
}
$query = "DELETE FROM ".$this->db->dbprefix."assessments WHERE sid=$surveyid AND gid=$gid";
$result = db_execute_assoc($query) ; //or safe_die($connect->ErrorMsg()); // Checked

$query = "DELETE FROM ".$this->db->dbprefix."groups WHERE sid=$surveyid AND gid=$gid";
$result = db_execute_assoc($query); // or safe_die($connect->ErrorMsg()); // Checked
if ($result)
{
$gid = "";
$groupselect = getgrouplist($gid,$surveyid);
fixSortOrderGroups($surveyid);
$this->session->set_userdata('flashmessage', $clang->gT("The question group was deleted."));
}
else
{
$this->session->set_userdata('flashmessage', $this->limesurvey_lang->gT("Group could not be deleted"));
}
redirect(site_url('admin/survey/view/'.$surveyid));
}
}

Expand Down
27 changes: 25 additions & 2 deletions application/models/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Groups extends CActiveRecord
*/
public static function model()
{
return parent::model(__CLASS__);
}
return parent::model(__CLASS__);
}

/**
* Returns the setting's table name to be used by the model
Expand Down Expand Up @@ -86,5 +86,28 @@ function getGroups($surveyid) {
->bindParam(":language", $language, PDO::PARAM_STR)
->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));
}

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')->queryAll();

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

return $questionIds;
}
}
?>
54 changes: 34 additions & 20 deletions application/models/Questions.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,32 +126,46 @@ function getSubQuestions($parent_qid)
->order('question_order asc')
->query();
}
function getQuestionsWithSubQuestions($iSurveyID, $sLanguage, $sCondition=FALSE)


function getQuestionsWithSubQuestions($iSurveyID, $sLanguage, $sCondition = FALSE)
{
$dbprefix = Yii::app()->db->tablePrefix;
$command = Yii::app()->db->createCommand()
->select($dbprefix.'questions.*, q.qid as sqid, q.title as sqtitle, q.question as sqquestion, '.$dbprefix.'groups.*')
->from($this->tableName())
->leftJoin($dbprefix.'questions q', "q.parent_qid = {$dbprefix}questions.qid AND q.language = {$dbprefix}questions.language")
->join($dbprefix.'groups', "{$dbprefix}groups.gid = {$dbprefix}questions.gid AND {$dbprefix}questions.language = {$dbprefix}groups.language");
$command->where("({$dbprefix}questions.sid = '$iSurveyID' AND {$dbprefix}questions.language = '$sLanguage' AND {$dbprefix}questions.parent_qid = 0)");
if ($sCondition != FALSE)
{
$command->where("({$dbprefix}questions.sid = '$iSurveyID' AND {$dbprefix}questions.language = '$sLanguage' AND {$dbprefix}questions.parent_qid = 0) AND " . $sCondition);
$dbprefix = Yii::app()->db->tablePrefix;
$command = Yii::app()->db->createCommand()
->select($dbprefix . 'questions.*, q.qid as sqid, q.title as sqtitle, q.question as sqquestion, ' . $dbprefix . 'groups.*')
->from($this->tableName())
->leftJoin($dbprefix . 'questions q', "q.parent_qid = {$dbprefix}questions.qid AND q.language = {$dbprefix}questions.language")
->join($dbprefix . 'groups', "{$dbprefix}groups.gid = {$dbprefix}questions.gid AND {$dbprefix}questions.language = {$dbprefix}groups.language");
$command->where("({$dbprefix}questions.sid = '$iSurveyID' AND {$dbprefix}questions.language = '$sLanguage' AND {$dbprefix}questions.parent_qid = 0)");
if ($sCondition != FALSE) {
$command->where("({$dbprefix}questions.sid = '$iSurveyID' AND {$dbprefix}questions.language = '$sLanguage' AND {$dbprefix}questions.parent_qid = 0) AND " . $sCondition);
}
$command->order("{$dbprefix}groups.group_order asc, {$dbprefix}questions.question_order asc");
$command->order("{$dbprefix}groups.group_order asc, {$dbprefix}questions.question_order asc");

return $command->query()->readAll();
}
function insertRecords($data)

function insertRecords($data)
{
$questions = new self;
foreach ($data as $k => $v)
$questions->$k = $v;
return $questions->save();
}
foreach ($data as $k => $v)
$questions->$k = $v;
return $questions->save();
}

public static function deleteAllById($questionsIds)
{
if (!is_array($questionsIds)) {
$questionsIds = array($questionsIds);
}

Yii::app()->db->createCommand()->delete(Conditions::model()->tableName(), array('in', 'qid', $questionsIds));
Yii::app()->db->createCommand()->delete(Question_attributes::model()->tableName(), array('in', 'qid', $questionsIds));
Yii::app()->db->createCommand()->delete(Answers::model()->tableName(), array('in', 'qid', $questionsIds));
Yii::app()->db->createCommand()->delete(Questions::model()->tableName(), array('in', 'parent_qid', $questionsIds));
Yii::app()->db->createCommand()->delete(Questions::model()->tableName(), array('in', 'qid', $questionsIds));
Yii::app()->db->createCommand()->delete(Defaultvalues::model()->tableName(), array('in', 'qid', $questionsIds));
Yii::app()->db->createCommand()->delete(Quota_members::model()->tableName(), array('in', 'qid', $questionsIds));
}
}
?>

0 comments on commit 56b91d8

Please sign in to comment.