-
Notifications
You must be signed in to change notification settings - Fork 4
/
database.js
166 lines (145 loc) · 4.83 KB
/
database.js
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
var database_link = "https://api.mlab.com/api/1/databases/hackroll/collections";
var lesson_collection_link = "https://api.mlab.com/api/1/databases/hackroll/collections/lesson";
var attendance_collection_link = "https://api.mlab.com/api/1/databases/hackroll/collections/lesson_attendance";
var result_collection_link = "https://api.mlab.com/api/1/databases/hackroll/collections/result";
var student_collection_link = "https://api.mlab.com/api/1/databases/hackroll/collections/student";
var apiKey = "apiKey=6158q4S1oyjh0rE3MDtYdYoqKYCQSPIL";
var domain = "";
var lesson_id ="5a6c42cec2ef1663f11a7fa7";
var lesson ="";
// create_new_lesson();
//create new lesson
function create_new_lesson()
{
var lesson = document.getElementById("lesson").value;
var class_time = document.getElementById("time").value;
var class_date = getTodayDate();
// var unique_code = generateUniqueCode();
var classroom = document.getElementById("classroom").value;
var new_lesson = get_lesson_json(lesson, class_date, class_time, classroom, "open");
$.ajax({
url: lesson_collection_link.concat("?" + apiKey),
data: new_lesson,
type: "POST",
contentType: "application/json",
success: function(return_data)
{
window.location.href="qrCode.html?lesson_id=" + return_data._id["$oid"];
}
});
}
//create new attendance
function create_new_attendance()
{
var student_name = document.getElementById("Name").value;
var student_id = document.getElementById("number").value;
var new_attendance = get_attendance_json("5a6c42cec2ef1663f11a7fa7", student_id, student_name);
$.ajax({
url: attendance_collection_link.concat("?" + apiKey),
data: new_attendance,
type: "POST",
contentType: "application/json",
success: function(return_data)
{
//return single attendance obj
}
});
}
//get attendance by lesson id
function get_attendance_by_id(lesson_id)
{
var get_attendance_link = attendance_collection_link.concat('?q={"lesson_id" : "' + lesson_id + '"}&' + apiKey);
$.ajax({
url: get_attendance_link.concat(apiKey),
type: "GET",
contentType: "application/json",
success: function(return_data)
{
compare_student_lists(return_data);
}
});
}
//compare student lists
// function compare_student_lists(return_data)
// {
// $.ajax({
// url: student_collection_link.concat(apiKey),
// type: "GET",
// contentType: "application/json",
// success: function(student_list)
// {
// List<JSONObject> studentList = student_list;
// List<JSONObject> attendance = return_data;
// Collections.sort(studentList);
// Collections.sort(attendance);
// var j = 0;
// for(var i = 0; i < studentList; i++) {
// if(studentList[j] !== attendance[i]) {
// var no_attendance = studentList[j];
// j++;
// $.ajax({
// url: result_collection_link.concat("?" + apiKey),
// data: no_attendance,
// type: "POST",
// contentType: "application/json",
// success: function(return_data)
// {
// }
// });
// }j++;
// }
// }
// })
// }
//create lesson json obj
function get_lesson_json(lesson, date, time, classroom, status)
{
var lesson_json = JSON.stringify(
{
"lesson" : lesson,
"date" : date,
"time" : time,
"classroom" : classroom,
"status" : status
});
return lesson_json;
}
//create attendance json object
function get_attendance_json(lesson_id, student_number, student_name)
{
var attendance_json = JSON.stringify(
{
"lesson_id" : lesson_id,
"student_number" : student_number,
"student_name" : student_name
});
return attendance_json;
}
// function generateUniqueCode()
// {
// return '_' + Math.random().toString(36).substr(2, 9);
// }
function getQueryParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function getTodayDate()
{
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = dd + '/' + mm + '/' + yyyy;
return today;
}