-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.php
123 lines (105 loc) · 5.83 KB
/
init.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
<meta charset="UTF-8">
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include("config/config.php");
$connection = mysqli_connect($host,$db_user,$db_password);
if(!$connection->set_charset("utf8"))
printf("Error loading character set utf8: %s\n", mysqli_error($connection));
else
printf("Current character set: %s\n", $connection->character_set_name());
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$query = "DROP DATABASE ".$db_name;
mysqli_query($connection,$query) or die(mysqli_error($connection));
mysqli_query($connection,"CREATE DATABASE IF NOT EXISTS ".$db_name." CHARACTER SET utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT COLLATE utf8_general_ci") or die(mysqli_error($connection));
echo "<b> database created </b><br>";
mysqli_select_db($connection,$db_name) or die(mysqli_error($connection));
$sql = "CREATE TABLE IF NOT EXISTS users(ID int NOT NULL AUTO_INCREMENT,primary key (id),username CHAR(20), password CHAR(50),
email CHAR(25), type INT, adviser_code TEXT, code char(50), phone_number CHAR(20), name CHAR(15), family_name CHAR(25)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
echo "<b> User table created </b><br>";
$sql = "CREATE TABLE IF NOT EXISTS user_exams(username CHAR(20), exam_name TEXT, answered BOOLEAN DEFAULT 0)";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
echo "=> user_exams table created <br>";
$sql = "CREATE TABLE IF NOT EXISTS choices(ID int NOT NULL AUTO_INCREMENT,primary key (id), choice_count INT, exam_name TEXT(30), choice1 CHAR(15), choice2 CHAR(15), choice3 CHAR(15), choice4 CHAR(15), choice5 CHAR(15))";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
echo "<b> choice table created </b><br>";
$sql = "CREATE TABLE IF NOT EXISTS scores(ID int not NULL AUTO_INCREMENT, primary key(id), exam_name TEXT, user_id INT, score INT)";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
$sql = "CREATE TABLE IF NOT EXISTS exams_list(exam_name TEXT)";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
$flow = fopen("metadata/flow/flow.txt","r");
$sql = "CREATE TABLE flow_questions(ID int NOT NULL AUTO_INCREMENT, primary key (id), content TEXT(150))";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
if($flow)
{
while(($line = fgets($flow)) !== false)
{
$sql = "INSERT INTO flow_questions(content) VALUES('$line')";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
}
}
else
die("flow file not found!"); //todo: to add customer agar bood dar db pak shavad va ezafe shavad
$sql = "CREATE TABLE flow_values(ID int NOT NULL AUTO_INCREMENT, primary key (id), flow_question_id INT, user_id INT, value BOOLEAN DEFAULT 0)";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
$state = fopen("metadata/flow/state.txt","r");
$sql = "CREATE TABLE user_states(ID int NOT NULL AUTO_INCREMENT, primary key (id), content TEXT(150))";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
if($state)
{
while(($line = fgets($state)) !== false)
{
$sql = "INSERT INTO user_states(content) VALUES('$line')";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
}
}
else
die("state file not found!");
$sql = "CREATE TABLE user_state_values(ID int NOT NULL AUTO_INCREMENT, primary key (id), user_state_id INT, user_id INT)";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
$sql = "CREATE TABLE users_custom_state(user_id INT, content TEXT)";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
$files = scandir("metadata/exams");
foreach ($files as $name)
{
if($name == "." or $name == "..")
continue;
$lines = file("metadata/exams/".$name);
$exam_name = trim($lines[0]);
$exam_scores_name = $exam_name.'_scores';
$choice_count = $lines[1];
$choices = [];
mysqli_query($connection,"DROP TABLE ".$exam_name); //or die(mysqli_error($connection));
mysqli_query($connection,"DROP TABLE ".$exam_scores_name);
$sql = "CREATE TABLE IF NOT EXISTS $exam_name(ID int NOT NULL AUTO_INCREMENT, primary key (id), question_content TEXT(150), choice1_score INT DEFAULT 0, choice2_score INT DEFAULT 0, choice3_score INT DEFAULT 0, choice4_score INT DEFAULT 0, choice5_score INT DEFAULT 0)";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
$sql = "INSERT INTO exams_list (exam_name) VALUES('$exam_name')";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
$sql = "DELETE FROM choices WHERE exam_name='$exam_name'";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
echo "=> removed choices row with exam_name: ".$exam_name."<br>";
for($i = 2; $i < 2+$choice_count; $i++)
$choices[] = $lines[$i];
for($i = 0; $i < 5; $i++)
if(!isset($choices[$i]))
$choices[$i] = NULL;
$sql = "INSERT INTO choices(choice_count,exam_name,choice1,choice2,choice3,choice4,choice5) VALUES('$choice_count','$exam_name','$choices[0]','$choices[1]','$choices[2]','$choices[3]','$choices[4]')";
mysqli_query($connection,$sql) or die(mysqli_error($connection));
echo "=> added ".$choice_count." choices for exam: ".$exam_name."<br>";
$question_start_index = 2+$choice_count;
for($i = $question_start_index; $i < count($lines); $i += $choice_count+1)
{
$scores = [];
for($j = 0; $j < $choice_count; $j++)
$scores[$j] = $lines[$i+$j+1];
for($j=0; $j < 5; $j++)
if(!isset($scores[$j]))
$scores[$j] = NULL;
$sql = "INSERT INTO $exam_name(question_content,choice1_score,choice2_score,choice3_score,choice4_score,choice5_score) VALUES('$lines[$i]','$scores[0]','$scores[1]','$scores[2]','$scores[3]','$scores[4]')";
mysqli_query($connection,$sql) or die(mysqli_error());
}
echo "=> ".((count($lines) - $question_start_index)/($choice_count+1))." questions added to exam ".$exam_name."<br>";
}
?>