-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminscript.js
More file actions
281 lines (256 loc) · 10.9 KB
/
adminscript.js
File metadata and controls
281 lines (256 loc) · 10.9 KB
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
Parse.initialize("vhsAtaDzRuFML4Movu78CiG1V6OSv1tYVhJdWMSW", "bkhWG2CWNnTUDqE3asOkGzNw1vrbBMyLy8YAUUgG");
$("#login").on("click", function () {
Parse.User.logIn($("#userName").val(), $("#pass").val(), {
//Parse.User.logIn("admin", "admin", {
success: function (user) {
// Allt gick bra, skicka vidare användare
window.location.href = "admin.html";
},
error: function (user, error) {
alert("Ditt användarnamn eller lösenord stämmer inte");
$("#userName").text("");
$("#password").text("");
// Något gick fel, visa error
}
});
});
var select = $("#category");
//Funktion för att hämta alla kategorier från databasen, dessa visas i listan när man ska skapa frågor
function getCategories() {
var Category = Parse.Object.extend("Category");
var query = new Parse.Query(Category);
var exist = false;
query.find({
success: function (results) {
for (var i = 0; i < results.length; i++) {
exist = false;
var object = results[i];
//Går igenom existerande options i select och lägger boolean true om den hämtade object redan finns som option.
$("#category option").each(function (i) {
if ($(this).text() == object.get("category")) {
exist = true;
}
});
//Om den hämtade object inte finns som option, lägg till.
if (!exist) {
var opt = document.createElement("option");
opt.value = object.get("category");
opt.innerHTML = object.get("category");
$("#category").append(opt);
}
}
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
//Om sidan är newQuestion.html så anropa getCategories
$(function () {
if ($("body").is($("#newQuestionPage"))) {
getCategories();
}
});
//Funktion för att spara en ny fråga/kategori
$("#saveQuestion").on("click", function () {
var QREntity = Parse.Object.extend("QR");
var qrEntity = new QREntity();
qrEntity.set("question", $("#newQuestion").val());
qrEntity.set("correct", $("#correctAnswer").val());
qrEntity.set("optionTwo", $("#alt2").val());
qrEntity.set("optionThree", $("#alt3").val());
qrEntity.set("optionFour", $("#alt4").val());
qrEntity.set("locationNbr", parseInt($("#locationNbr").val()));
qrEntity.set("qrId", $("#qrId").val());
qrEntity.save({
success: function (qrEntity) {
// Skapa en kategori
addCategoryWithQuestion();
},
error: function (qrEntity, error) {
// Något gick fel, felmeddelande här
console.log(error);
}
});
function addCategoryWithQuestion() {
var CatEntity = Parse.Object.extend("Category");
var catEntity = new CatEntity();
if (select.val() == "new") {
catEntity.set("category", $("#newCategory").val());
} else {
catEntity.set("category", select.val());
}
catEntity.set("question", qrEntity);
catEntity.save({
success: function (catEntity) {
// Visa att allt gick bra, popup fönster tex? Laddar om sidan efteråt
alert("Allt gick bra! Frågan skapades");
location.reload();
},
error: function (catEntity, error) {
// Något gick fel, felmeddelande här
console.log(error);
}
});
}
});
//Funktion för att hämta alla frågor för redigering
function removeQuestion() {
var Category = Parse.Object.extend("Category");
var query = new Parse.Query(Category);
//query.equalTo("Sport"); // Endast om vi vill hämta specifik kategori
query.include("question"); // För att inkludera allt i QR
query.find({
success: function (results) {
//alert("Successfully retrieved " + results.length);
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
var question = object.get("question");
//Hämtar ID för Category som frågan är kopplad till
var categoryID = object.id;
//Hämtar ID för QR där frågan finns.
var questionID = question.id;
//alert(object.get('category') + ' - ' + question.get('question'));
$("<div></div>").attr("id", "edit" + i).addClass("col-xs-12 col-sm-12 editQuestion").appendTo($("#editPanel"));
$("<p>" + question.get("question") + "</p>").addClass("question").appendTo($("#edit" + i));
$("<p>" + question.get("correct") + "</p>").appendTo($("#edit" + i));
$("<p>" + question.get("optionTwo") + "</p>").appendTo($("#edit" + i));
$("<p>" + question.get("optionThree") + "</p>").appendTo($("#edit" + i));
$("<p>" + question.get("optionFour") + "</p>").appendTo($("#edit" + i));
$("<button>Ta bort frågan </button>").val(categoryID + "," + questionID).appendTo($("#edit" + i)).on("click", function () {
var id = $(this).val();
var splitID = id.split(",")
//splitID[0] = categoryID
//splitID[1] = questionID
deleteFromDatabase(splitID[0], splitID[1]);
});
}
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
//Om sidan är editQuestion.html så anropa removeQuestion
$(function () {
if ($("body").is($("#editQuestionPage"))) {
removeQuestion();
}
});
function deleteFromDatabase(categoryID, questionID) {
//console.log(categoryID + " - " + questionID);
var question = Parse.Object.extend("QR");
var query = new Parse.Query(question);
query.get(questionID, {
success: function (myObj) {
// The object was retrieved successfully.
myObj.destroy({});
},
error: function (object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and description.
}
});
var category = Parse.Object.extend("Category");
var query2 = new Parse.Query(category);
query2.get(categoryID, {
success: function (myObj) {
// The object was retrieved successfully.
myObj.destroy({});
},
error: function (object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and description.
}
});
alert("Frågan är nu borttaget!");
location.reload();
}
var distance = $("#distance");
//Funktion för att hämta alla frågor från databasen, dessa visas i listan när man ska lägga till distanser
function distances() {
var question = Parse.Object.extend("QR");
var query = new Parse.Query(question);
query.find({
success: function (results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
var opt = document.createElement("option");
opt.value = object.id;
opt.innerHTML = object.get("locationNbr") + " - " + object.get("question");
$("#distance").append(opt);
}
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
//Om sidan är distance.html så anropa distances
$(function () {
if ($("body").is($("#addDistances"))) {
distances();
}
});
$("#saveDistances").on("click", function () {
//query.equalTo("objectId", distance.val());
//console.log(distance.val());
var QREntity = Parse.Object.extend("QR");
var qrEntity = new Parse.Query(QREntity);
//qrEntity.equalTo("objectId", distance.val());
qrEntity.find({
success: function (results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
if (object.id == distance.val()) {
object.set("distance1", parseInt(document.getElementById("distance1").value));
object.set("distance2", parseInt(document.getElementById("distance2").value));
object.set("distance3", parseInt(document.getElementById("distance3").value));
object.set("distance4", parseInt(document.getElementById("distance4").value));
object.set("distance5", parseInt(document.getElementById("distance6").value));
object.set("distance6", parseInt(document.getElementById("distance6").value));
object.set("distance7", parseInt(document.getElementById("distance7").value));
object.set("distance8", parseInt(document.getElementById("distance8").value));
object.set("distance9", parseInt(document.getElementById("distance9").value));
object.set("distance10", parseInt(document.getElementById("distance10").value));
object.save();
alert("Distanserna är nu sparade!");
location.reload();
}
}
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
});
$("#saveTotalQuestions").on("click", function () {
var totalQuestions = Parse.Object.extend("Total");
var query = new Parse.Query(totalQuestions);
var total = new totalQuestions();
query.find({
success: function (results) {
//alert("Successfully retrieved " + results.length);
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
object.destroy();
}
total.set("totalQuestions", parseInt($("#totalQuestions").val()));
total.save({
success: function (qrEntity) {
alert("Antal frågor är nu sparad!");
location.reload();
},
error: function (qrEntity, error) {
// Något gick fel, felmeddelande här
console.log(error);
}
});
},
error: function (error) {
alert("Error: " + error.code + " " + error.message);
}
});
});