-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavailable_classes.js
48 lines (40 loc) · 1.66 KB
/
available_classes.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
"use strict";
//this initializes the global datastucture that holds current students in each class and the current groups.
var mysql = require('mysql');
var dbconfig = require('./config_database');
var hash = require('./hashes');
var ids = {}
var connection = mysql.createConnection(dbconfig.connection);
connection.connect();
connection.query("USE " + dbconfig.database);
var available_classes = {};
exports.available_classes = available_classes;
var classQuery = "SELECT * FROM " + dbconfig.class_table;
connection.query(classQuery, function(err, rows, fields){
if (err)
throw err;
for (var i in rows){
hash.insert_id_and_hash(rows[i].class_id, rows[i].hashed_id)
ids[rows[i].class_id] = rows[i].hashed_id;
available_classes[rows[i].hashed_id] = {}
available_classes[rows[i].hashed_id]["class_name"] = rows[i].class_name;
available_classes[rows[i].hashed_id]["user"] = {};
available_classes[rows[i].hashed_id]["settings"] = {};
}//creates an array for
});
var groupQuery = "SELECT * FROM " + dbconfig.group_table;
connection.query(groupQuery, function(err, rows, fields){
if (err)
throw err;
for (var j in rows) {
var hash = ids[rows[j].class_id];
if (hash in available_classes) {
available_classes[hash][rows[j].group_id] = {};
available_classes[hash][rows[j].group_id]["deleted"] = false;
available_classes[hash][rows[j].group_id]["students"] = [];
available_classes[hash][rows[j].group_id]["colors"] = "";
}
}
});
//query the database for classes and groups
connection.end();