/
label_helper.php
230 lines (191 loc) · 7.51 KB
/
label_helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2011 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.
*
*/
//include_once("login_check.php");
//Security Checked: POST/GET/SESSION/DB/returnGlobal
function updateset($lid)
{
$clang = Yii::app()->lang;
// Get added and deleted languagesid arrays
$postlanguageids=Yii::app()->getRequest()->getPost('languageids');
$postlabel_name=Yii::app()->getRequest()->getPost('label_name');
$newlanidarray = explode(" ",trim($postlanguageids));
$oldlangidsarray = array();
$labelset = LabelSet::model()->findByAttributes(array('lid' => $lid));
$oldlangidsarray = explode(' ', $labelset->languages);
$addlangidsarray = array_diff($newlanidarray, $oldlangidsarray);
$dellangidsarray = array_diff($oldlangidsarray, $newlanidarray);
// If new languages are added, create labels' codes and sortorder for the new languages
$result = Label::model()->findAllByAttributes(array('lid' => $lid), array('order' => 'code, sortorder, assessment_value'));
if ($result)
foreach ($result as $row)
$oldcodesarray[$row['code']] = array('sortorder'=> $row['sortorder'], 'assessment_value'=> $row['assessment_value']);
if (isset($oldcodesarray) && count($oldcodesarray) > 0 )
foreach ($addlangidsarray as $addedlangid)
foreach ($oldcodesarray as $oldcode => $olddata)
$sqlvalues[]= array('lid' => $lid, 'code' => $oldcode, 'sortorder' => $olddata['sortorder'], 'language' => $addedlangid, 'assessment_value' => $olddata['assessment_value']);
if (isset($sqlvalues)) {
foreach ($sqlvalues as $sqlvalue) {
$label = new Label();
foreach ($sqlvalue as $name => $value) {
$label->setAttribute($name, $value);
}
$label->save();
}
}
// If languages are removed, delete labels for these languages
$criteria = new CDbCriteria;
$criteria->addColumnCondition(array('lid' => $lid));
$langcriteria = new CDbCriteria();
foreach ($dellangidsarray as $dellangid)
$langcriteria->addColumnCondition(array('language' => $dellangid), 'OR');
$criteria->mergeWith($langcriteria);
if (!empty($dellangidsarray))
$result = Label::model()->deleteAll($criteria);
// Update the label set itself
$labelset->label_name = $postlabel_name;
$labelset->languages = $postlanguageids;
$labelset->save();
}
/**
* Deletes a label set alog with its labels
*
* @param mixed $lid Label ID
* @return boolean Returns always true
*/
function deletelabelset($lid)
{
$query = "DELETE FROM {{labels}} WHERE lid=$lid";
$result = Yii::app()->db->createCommand($query)->execute();
$query = "DELETE FROM {{labelsets}} WHERE lid=$lid";
$result = Yii::app()->db->createCommand($query)->execute();
return true;
}
function insertlabelset()
{
//global $labelsoutput;
// $labelsoutput.= $_POST['languageids']; For debug purposes
$clang = Yii::app()->lang;
$postlanguageids=Yii::app()->getRequest()->getPost('languageids');
$postlabel_name=Yii::app()->getRequest()->getPost('label_name');
//postlabel_name = dbQuoteAll($postlabel_name,true);
//$postlanguageids = dbQuoteAll($postlanguageids,true);
$data = array(
'label_name' => $postlabel_name,
'languages' => $postlanguageids
);
//$query = "INSERT INTO ".db_table_name('labelsets')." (label_name,languages) VALUES ({$postlabel_name},{$postlanguageids})";
$result=LabelSet::model()->insertRecords($data);
if (!$result)
{
Yii::app()->session['flashmessage'] = $clang->gT("Inserting the label set failed.");
}
else
{
return $result;
}
}
function modlabelsetanswers($lid)
{
//global $labelsoutput;
$clang = Yii::app()->lang;
$ajax = false;
if (isset($_POST['ajax']) && $_POST['ajax'] == "1"){
$ajax = true;
}
if (!isset($_POST['method'])) {
$_POST['method'] = $clang->gT("Save");
}
$sPostData = Yii::app()->getRequest()->getPost('dataToSend');
$sPostData=str_replace("\t", '', $sPostData);
if (get_magic_quotes_gpc())
{
$data = json_decode(stripslashes($sPostData));
}
else
{
$data = json_decode($sPostData);
}
if ($ajax)
$lid = insertlabelset();
$aErrors=array();
if (count(array_unique($data->{'codelist'})) == count($data->{'codelist'}))
{
$query = "DELETE FROM {{labels}} WHERE lid = '$lid'";
$result = Yii::app()->db->createCommand($query)->execute();
foreach($data->{'codelist'} as $index=>$codeid){
$codeObj = $data->$codeid;
$actualcode = $codeObj->{'code'};
//$codeid = dbQuoteAll($codeid,true);
$assessmentvalue = (int)($codeObj->{'assessmentvalue'});
foreach($data->{'langs'} as $lang){
$strTemp = 'text_'.$lang;
$title = $codeObj->$strTemp;
$sortorder = $index;
$oLabel = new Label();
$oLabel->lid=$lid;
$oLabel->code=$actualcode;
$oLabel->title=$title;
$oLabel->sortorder=$sortorder;
$oLabel->assessment_value=$assessmentvalue;
$oLabel->language=$lang;
if($oLabel->validate())
{
$result=$oLabel->save();
}
else
{
$aErrors[]=$oLabel->getErrors();
}
}
}
if(count($aErrors))
{
Yii::app()->session['flashmessage'] = $clang->gT("Not all labels were updated successfully.");
}
else
{
Yii::app()->session['flashmessage'] = $clang->gT("Labels sucessfully updated");
}
}
else
{
Yii::app()->setFlashMessage($clang->gT("Can't update labels because you are using duplicated codes"),'error');
}
if ($ajax){ die(); }
}
/**
* Function rewrites the sortorder for a label set
*
* @param mixed $lid Label set ID
*/
function fixorder($lid) {
$clang = Yii::app()->lang;
$qulabelset = "SELECT * FROM {{labelsets}} WHERE lid=$lid";
$rslabelset = Yii::app()->db->createCommand($qulabelset)->query();
$rwlabelset=$rslabelset->read();
$lslanguages=explode(" ", trim($rwlabelset['languages']));
foreach ($lslanguages as $lslanguage)
{
$query = "SELECT lid, code, title, sortorder FROM {{labels}} WHERE lid=:lid and language=:lang ORDER BY sortorder, code";
$result = Yii::app()->createCommand($query)->query(array(':lid' => $lid, ':lang' => $lslanguage)); // or safeDie("Can't read labels table: $query // (lid=$lid, language=$lslanguage) "
$position=0;
foreach ($result->readAll() as $row)
{
$position=sprintf("%05d", $position);
$query2="UPDATE {{labels}} SET sortorder='$position' WHERE lid=".$row['lid']." AND code=".$row['code']." AND title=".$row['title']." AND language='$lslanguage' ";
$result2=Yii::app()->db->createCommand($query2)->execute();
$position++;
}
}
}