-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedititem.php
489 lines (478 loc) · 17 KB
/
edititem.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
require_once("includes/config.php");
require_once("includes/getuser.php");
require_once("includes/isauthor.php");
require_once("includes/issuperauthor.php");
require_once("includes/limitstring.php");
require_once("includes/quote_smart.php");
require_once("includes/ODBCDateToTextDate.php");
?>
<?php
function addItem($text, $title, $lastModified, $questionID)
{
global $db_connection; //makes this available within the function
//Create new question with values from this form
$iItems = " INSERT INTO Items
VALUES(0,$text,$title,$lastModified)";
$result_query = @mysqli_query($db_connection, $iItems);
$itemID = mysqli_insert_id($db_connection);
if (($result_query == false))
{
echo "problem inserting into Items" . mysqli_error($db_connection);
$bSuccess = false;
}
//Now need to add it to QuestionItems
//first work out position to add it to
$qMaxPosition = " SELECT MAX(position) as maxPosition
FROM QuestionItems
WHERE questionID = $questionID";
$result_query = @mysqli_query($db_connection, $qMaxPosition);
if (($result_query == false))
{
echo "problem querying qMaxPosition" . mysqli_error($db_connection);
}
$rowMaxPosition = mysqli_fetch_array($result_query);
$maxPosition = $rowMaxPosition[maxPosition];
if ($maxPosition == "")
{
//there are no existing items in this question
$position = 0;
}
else
{
//there are existing items in this question so increment
$position = $maxPosition + 1;
}
$iQuestionItems = " INSERT INTO QuestionItems
VALUES(0,$questionID,$itemID,$position,1)";
$result_query = @mysqli_query($db_connection, $iQuestionItems);
if (($result_query == false))
{
echo "problem insering into QuestionItems" . mysqli_error($db_connection);
$bSuccess = false;
}
}
if ((isset($_POST['bUpdate'])&& $_POST['bUpdate']!= "")||(isset($_POST['bCreate'])&& $_POST['bCreate']!= "")||(isset($_POST['bUpload'])&& $_POST['bUpload']!= ""))
{
if(isset($_POST['hItemID']))
{
$surveyID = $_POST['hSurveyID'];
$blockID = $_POST['hBlockID'];
$sectionID = $_POST['hSectionID'];
$questionID = $_POST['hQuestionID'];
$itemID = $_POST['hItemID'];
if($_POST['rSeparator']==1)
{
$strSeparator = ",";
}
else
{
$strSeparator = chr(9);
}
if($itemID=="upload")
{
$aItemRows = explode(chr(10),$_POST['tText']);
}
else
{
$aItemRows[0] = $_POST['tTitle'] . $strSeparator . $_POST['tText'];
}
$validationProblem = false;
$i=0;
while($i<count($aItemRows) && (!isset($validationProblem) || $validationProblem==false))
{
//on this first run through the data, we are simply validating it
//it's worth doing this to prevent half the data being written before finding an error
$aItemComponents = explode($strSeparator,$aItemRows[$i]);
$updateText = quote_smart($db_connection, $aItemComponents[1]);
$updateTitle = quote_smart($db_connection, $aItemComponents[0]);
//**********************************************************
//Server-side validation of data entered -
//**********************************************************
/* Checking and ensure that the item title does not already exist in the database */
$sql_title_check = mysqli_query($db_connection, "SELECT title FROM Items
WHERE title=$updateTitle " . ($itemID !="add" && $itemID !="upload"?"AND itemID <> $itemID ":""));
//that last if statement allows edited item to have the same name as it had before
//but prevents added items having the same name as an existing item.
$title_check = mysqli_num_rows($sql_title_check);
if($title_check > 0 || $updateTitle == "" || $updateText=="" || count($aItemComponents)!=2)
{
$validationProblem = true;
}
//**********************************************************
//End server-side validation of data entered
//**********************************************************
$i++;
}
if(!isset($validationProblem) || $validationProblem != true)
{
$i=0;
while($i<count($aItemRows) && (!isset($validationProblem) || $validationProblem==false))
{
//on this second run through, now that initial validation is complete, we will actually write it to the database
$aItemComponents = explode($strSeparator,$aItemRows[$i]);
$updateText = quote_smart($db_connection, $aItemComponents[1]);
$updateTitle = quote_smart($db_connection, $aItemComponents[0]);
$updateLastModified = "CURDATE()";
//Still need to check for repeat item titles as there could be repeats within the data pasted in
/* Checking and ensure that the item title does not already exist in the database */
$sql_title_check = mysqli_query($db_connection, "SELECT title FROM Items
WHERE title=$updateTitle " . ($itemID !="add" && $itemID !="upload"?"AND itemID <> $itemID ":""));
//that last if statement allows edited item to have the same name as it had before
//but prevents added items having the same name as an existing item.
$title_check = mysqli_num_rows($sql_title_check);
if($title_check > 0)
{
$validationProblem = true;
}
//Validated - go ahead with updating/writing
if($itemID =="add" || $itemID=="upload")
{
addItem($updateText, $updateTitle, $updateLastModified, $questionID);
}
else
{
///Update section with values from this form
$uItems = " UPDATE Items
SET text = $updateText,
title = $updateTitle,
lastModified = $updateLastModified
WHERE itemID = $itemID";
$result_query = @mysqli_query($db_connection, $uItems);
if (($result_query == false))
{
echo "problem updating Items" . mysqli_error($db_connection);
$bSuccess = false;
}
}
$i++;
}
//send the user back to schedulesurvey.php after they have added
header("Location: https://" . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF'])
. "/" . "editquestion.php?surveyID=$surveyID&blockID=$blockID§ionID=$sectionID&questionID=$questionID");
exit();
}
}
}
else if (isset($_GET['surveyID'])&&isset($_GET['blockID'])&&isset($_GET['sectionID'])&&isset($_GET['questionID'])&&isset($_GET['itemID']))
{
$surveyID = $_GET['surveyID'];
$blockID = $_GET['blockID'];
$sectionID = $_GET['sectionID'];
$questionID = $_GET['questionID'];
$itemID = $_GET['itemID'];
}
else
{
echo "<h1>Warning</h1>
<p>This page requires data from another page.</p>";
exit();
}
if($itemID=="add")
{
$btnSubmitName = "bCreate";
$btnSubmitText = "Create item";
$pageTitleText = "Create item";
}
elseif($itemID=="upload")
{
$btnSubmitName = "bUpload";
$btnSubmitText = "Upload items";
$pageTitleText = "Upload items";
}
else
{
$btnSubmitName = "bUpdate";
$btnSubmitText = "Update item";
$pageTitleText = "Edit item";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?php echo $pageTitleText ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="css/SurveyStyles.css">
<link rel="stylesheet" type="text/css" href="css/msdstyle2.css" media="screen"/>
<link href="css/msdprint.css" rel="stylesheet" type="text/css" media="print"/>
<script language="JavaScript" src="script/validate.js"></script>
<script language="JavaScript" src="script/OptionTransfer.js"></script>
<script language="javascript" type="text/javascript">
//enables OK/update button if input has changed
function ControlHasChanged(btnSubmit)
{
document.getElementById(btnSubmit).disabled = false;
document.getElementById('btnPreviewSurvey').disabled = true;
//setting a cookie which regsiters any changes since last submit button press
if (cookieStatus()=="on") setCookie("savedData", "false");
}
// function to set a cookie
function setCookie(cookieName, cookieValue)
{
var currentCookie = cookieName + "=" + escape(cookieValue);
document.cookie = currentCookie;
}
// function to get a cookie
function getCookie(cookieName)
{
var dc = document.cookie;
var prefix = cookieName + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin = begin + 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
//checks whether user has cookies turned on or off
function cookieStatus()
{
setCookie("testCookie", "testValue");
var testCookieValue = getCookie("testCookie");
if (testCookieValue != "testValue")
{
return "off";
}
else
{
return "on";
}
}
//exactly what it says on the tin
function CheckSaved(text)
{
if (cookieStatus()=="on")
{
var AllSaved = getCookie("savedData");
if (AllSaved!="true")
{
return text;
}
else
{
return;
}
}
else
{
return;
}
}
function goTo(URL)
{
window.location.href = URL;
}
//Validation of input (called by frm1 OnSubmit())
function ValidateForm(theForm)
{
<?php
if($itemID!="upload")
{
echo"if(!validText(document.getElementById(\"tTitle\"),\"item name\",true))return false;";
}
elseif($itemID=="upload")
{
echo"if(!document.getElementById(\"rSeparatorTab\").checked && !document.getElementById(\"rSeparatorComma\").checked)
{
alert(\"Please choose a separator type\");
document.getElementById(\"rSeparatorTab\").focus();
return false;
}";
}
?>
if(!validText(document.getElementById("tText"),"item text",true))return false;
if (cookieStatus()=="on") setCookie("savedData", "true");
return true;
}
</script>
</head>
<body onBeforeUnload="return CheckSaved('You have made changes to the item properties. Please save them before moving away from this page')">
<?php
//Get info about survey
$qSurveys = "SELECT title
FROM Surveys
WHERE surveyID = $surveyID";
$qResSurvey = mysqli_query($db_connection, $qSurveys);
$rowSurvey = mysqli_fetch_array($qResSurvey);
$surveyTitle = $rowSurvey['title'];
//Get info about block
$qBlocks = "SELECT title
FROM Blocks
WHERE blockID = $blockID";
$qResBlock = mysqli_query($db_connection, $qBlocks);
$rowBlock = mysqli_fetch_array($qResBlock);
$blockTitle = $rowBlock['title'];
//Get info about section
$qSections = " SELECT title
FROM Sections
WHERE sectionID = $sectionID";
$qResSections = mysqli_query($db_connection, $qSections);
$rowSection = mysqli_fetch_array($qResSections);
$sectionTitle = $rowSection['title'];
//get info about question
$qQuestions = " SELECT title
FROM Questions
WHERE questionID = $questionID";
$qResQuestions = mysqli_query($db_connection, $qQuestions);
$rowQuestion = mysqli_fetch_array($qResQuestions);
$questionTitle = $rowQuestion['title'];
//Get info about item
if($itemID!="add" && $itemID!="upload")
{
$qItems = " SELECT title, text, lastModified
FROM Items
WHERE itemID = $itemID";
$qResItems = mysqli_query($db_connection, $qItems);
$rowItem = mysqli_fetch_array($qResItems);
$itemTitle = $rowItem['title'];
$itemText = $rowItem['text'];
$itemLastModified = $rowItem['lastModified'];
}
elseif(isset($validationProblem) && $validationProblem == true)
{
$itemTitle = $_POST['tTitle'];
$itemText = $_POST['tText'];
}
else
{
$itemTitle = "New item";
$itemText = "New item";
}
if(IsAuthor($heraldID))
{
require_once("includes/html/adminheadernew.html");
require_once("includes/html/BreadCrumbsHeader.html");
echo "
<a href=\"admin.php\">Administration</a> >
<a href=\"editsurvey.php?surveyID=$surveyID\">Edit survey: ".limitString($surveyTitle,30)."</a> >
<a href=\"editblock.php?surveyID=$surveyID&blockID=$blockID\">Edit block: ".limitString($blockTitle,30)."</a> >
<a href=\"editsection.php?surveyID=$surveyID&blockID=$blockID§ionID=$sectionID\">Edit section: ".limitString($sectionTitle,30)."</a> >
<a href=\"editquestion.php?surveyID=$surveyID&blockID=$blockID§ionID=$sectionID&questionID=$questionID\">Edit question: ".limitString($questionTitle,30)."</a> >
$pageTitleText: ".limitString($itemTitle,30);
require_once("includes/html/BreadCrumbsFooter.html");
}
else
{
require_once("includes/html/header.html");
echo "You do not have the necessary permissions to view this page";
exit();
}
echo "<a name=\"maintext\" id=\"maintext\"></a>
<h1>$pageTitleText: $questionTitle</h1>
<form id=\"frmEdit\" name=\"frmEdit\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" onSubmit=\"return ValidateForm(this)\">
<h2>Item properties:</h2>
<div class=\"questionNormal\">";
if(isset($validationProblem) && $validationProblem==true)
{
echo "<span class=\"errorMessage\"><strong>Please fix the following errors:</strong><br/>";
if($title_check > 0)
{
echo "This item name ('$updateTitle') is already in use in our database. Please enter a different name (e.g. prefix with the name of your survey) and try again.<br/>";
}
if($updateTitle == "")
{
echo "You must enter a name for this item. Please enter a name and try again.<br/>";
}
if($updateText == "")
{
echo "You must enter some text for this item. Please enter some text and try again.<br/>";
}
if(count($aItemComponents)!=2)
{
echo "There is a problem with the data you are uploading at around line $i, please check that you have specified your separator correctly and that each item begins on a new line.<br/>";
}
echo "</span><br/>";
}
echo"
<table class=\"normal_3\" summary=\"\">";
if($itemID=="upload")
{
//uploading items
echo "
<tr>
<td valign=\"top\">Paste items to be uploaded here:</td>
<td>
<textarea id=\"tText\" name=\"tText\" rows=\"10\" cols=\"35\" onChange=\"ControlHasChanged('$btnSubmitName')\">$itemText</textarea>
</td>
</tr>
<tr>
<td>Separators:</td>
<td>
<input type=\"radio\" id=\"rSeparatorTab\" name=\"rSeparator\" value=\"0\" size=\"50\"><label for=\"rSeparatorTab\">Tab</label>
<input type=\"radio\" id=\"rSeparatorComma\" name=\"rSeparator\" value=\"1\" size=\"50\"><label for=\"rSeparatorComma\">Comma</label>
</td>
</tr>";
}
else
{
echo "
<tr>
<td>Name:</td>
<td>
<input type=\"text\" id=\"tTitle\" name=\"tTitle\" value=\"$itemTitle\" size=\"50\" onChange=\"ControlHasChanged('$btnSubmitName')\">
</td>
</tr>
<tr>
<td>Last modified:</td>
<td>".(isset($itemLastModified)?ODBCDateToTextDateShort($itemLastModified):'Not set')."</td>
</tr>
<tr>
<td valign=\"top\">Item text:</td>
<td>
<textarea id=\"tText\" name=\"tText\" rows=\"5\" cols=\"35\" onChange=\"ControlHasChanged('$btnSubmitName')\">$itemText</textarea>
</td>
</tr>";
}
echo"
<tr>
<td clospan=\"2\">
<input type=\"hidden\" value=\"$surveyID\" id=\"hSurveyID\" name=\"hSurveyID\">
<input type=\"hidden\" value=\"$blockID\" id=\"hBlockID\" name=\"hBlockID\">
<input type=\"hidden\" value=\"$sectionID\" id=\"hSectionID\" name=\"hSectionID\">
<input type=\"hidden\" value=\"$questionID\" id=\"hQuestionID\" name=\"hQuestionID\">
<input type=\"hidden\" value=\"$itemID\" id=\"hItemID\" name=\"hItemID\">
<input type=\"submit\" value=\"$btnSubmitText\" id=\"$btnSubmitName\" name=\"$btnSubmitName\">
<input type=\"button\" id=\"btnPreviewSurvey\" name=\"btnPreviewSurvey\" value=\"Preview\" onClick=\"window.open('previewsurvey.php?surveyID=$surveyID','previewWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,scrollbars=yes')\">
</td>
</tr>
";
echo " </table>";
echo " </div>";
//bit of javascript to make sure that right option for start and finish dates is chosen
echo " <script language=\"JavaScript\">";
echo " document.getElementById('$btnSubmitName').disabled = true;";
echo " document.getElementById('btnPreviewSurvey').disabled = false;";
echo " setCookie(\"savedData\", \"true\");";
echo " </script>";
?>
<?php
//Breadcrumb
if(IsAuthor($heraldID))
{
require_once("includes/html/BreadCrumbsHeader.html");
echo "
<a href=\"admin.php\">Administration</a> >
<a href=\"editsurvey.php?surveyID=$surveyID\">Edit survey: ".limitString($surveyTitle,30)."</a> >
<a href=\"editblock.php?surveyID=$surveyID&blockID=$blockID\">Edit block: ".limitString($blockTitle,30)."</a> >
<a href=\"editsection.php?surveyID=$surveyID&blockID=$blockID§ionID=$sectionID\">Edit section: ".limitString($sectionTitle,30)."</a> >
<a href=\"editquestion.php?surveyID=$surveyID&blockID=$blockID§ionID=$sectionID&questionID=$questionID\">Edit question: ".limitString($questionTitle,30)."</a> >
$pageTitleText: ".limitString($itemTitle,30);
require_once("includes/html/BreadCrumbsFooter.html");
}
//footer - including contact info.
require_once("includes/html/footernew.html");
require_once("includes/instructions/inst_edititem.php");
?>
</body>
</html>