forked from adithbharadwaj/ethereum-DApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
576 lines (424 loc) · 19.6 KB
/
index.html
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<!DOCTYPE html>
<html>
<head>
<title>student details</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src = "abi.js"></script>
</head>
<body style="background: #ddd;">
<h1 style="text-align: center;">Details Form</h1>
<p style="padding-bottom: 50px;"></p>
<!-- details form. here, the teacher (only teacher) can enter the details and add a new student. -->
<form id="contact-form" method="post" role="form">
<div class="container" >
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Name *</label>
<input id="nam" type="text" name="nam" class="form-control" placeholder="Please enter your name *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Student Address *</label>
<input id="student_address" type="text" name="student_address" class="form-control" required="required" data-error="Address is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Parent *</label>
<input id="parent" type="text" name="parent" class="form-control" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_phone">Parent Address *</label>
<input id="parent_address" type="text" name="parent_address" class="form-control">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Course *</label>
<input id="course" type="text" name="course" class="form-control" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Marks *</label>
<input id="marks" type="text" name="marks" class="form-control" required="required" data-error="Lastname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Attendance *</label>
<input id="attendance" type="text" name="attendance" class="form-control" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_phone">Proficiency *</label>
<input id="profeciency" type="text" name="profeciency" class="form-control">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<p style="padding-top: 20px;"></p>
<!-- SUBMIT button. when a teacher clicks on submit, the details of that student is sent to the ethereum backend. -->
<div class="row">
<div class="col-md-12">
<input type="submit" class="btn btn-success" id="submit" >
</div>
</div>
<p style="padding-bottom: 100px;"></p>
</div>
</form>
<div class="container">
<button class="btn btn-primary" id="add_std" onclick="location.href = 'add_empl.html'; ">Add employer</button>
<button class="btn btn-success" id="add_teach" href = "add_teacher.html" onclick="location.href = 'add_teacher.html'; ">Add teacher</button>
<button class="btn btn-danger" id="add_std" onclick="location.href = 'edit.html'; ">Edit</button>
<p style="padding-top: 30px;"> </p>
</div>
<p style="padding-top: 30px;"> </p>
<!-- a container(wrapper) to display the names of the students that have been added.
the list of students is dynamically displayed. when you click on the name of a particular
student, the details of that student are displayed.
-->
<div class="container">
<h3 style="padding-left: 30px;">Student list</h3>
<div class = "row">
<div class="col-lg-2">
<p style="padding-top: 30px;"> </p>
<!-- when we click on submit, the name of that student is appended to this div. -->
<div id="list_items">
</div>
</div>
</div>
</div>
<!-- A panel to display the details of the student. -->
<p style="padding-top: 50px;"> </p>
<div class="container">
<h2 style="text-align: center;">Details</h2>
<p style="padding-top: 50px;"></p>
<div class="panel-group">
<!-- every span tag has a unique id. when you click on the name of a particular student, the details
are displayed in the span tag.
-->
<div class="panel panel-primary">
<div class="panel-heading">Name</div>
<div class = "panel-body"><span id = "name_span"></span></div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Teacher</div>
<div class="panel-body"><span id="teach_span"></span></div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Parent</div>
<div class="panel-body"><span id="par_span"></span></div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Homework count</div>
<div class="panel-body"><span id="hw_span"></span></div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Course</div>
<div class="panel-body"><span id="course_span"></span></div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Marks</div>
<div class="panel-body"><span id="mar_span"></span></div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Attendance</div>
<div class="panel-body"><span id="att_span"></span></div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Employer</div>
<div class="panel-body"><span id="emp_span"></span></div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Proficiency</div>
<div class="panel-body"><span id="pro_span"></span></div>
</div>
<p style="padding-top: 50px;"></p>
<!-- EMPLOY button. the employer can click on this button to employ a particular student. -->
<button type="button" class="btn btn-success" onclick="employ()" id = "emp">Employ</button>
<!-- HOMEWORK button. the teacher can click on this button to assign homework to a student. -->
<button type="button" class="btn btn-success" onclick="givehw()" id = "emp">Give HW</button>
<!-- FINISH HW button. the student can click on this button to finish all homework. -->
<button type="button" class="btn btn-success" onclick="finish_hw()" id = "emp">Finish HW</button>
<!-- javascript and web3 functions are inside this script. -->
<script type="text/javascript">
// The Application Binary Interface is the standard way to interact with contracts in the Ethereum ecosystem, both from outside the blockchain // and for contract-to-contract interaction. Data is encoded according to its type, as described in this specification.
// we get the abi by compiling the code on the remix ide.
// the address of the smart contract. we use remix to deploy the smart contract on MetaMask on our private network.
// We can then get the address of the smart contract on Ganache.
// (we deploy the smart contract in our private network using metamask on port 8545. in ganache, we should go to
// settings and change the port to 8545. )
//var address = '0x49d2955b20949e344ac61c156e7a6635527c1702';
// webObject is used to interact with the solidity code from the front-end (html/js)
var webObject; // webObject is a global variable so that it can be used in all the functions. the webObject is
// created every time the page loads (inside the onload function).
// this function gets executed every time the page loads.
window.addEventListener('load', function() {
// we check if metamask has already injected an instance of web3.
if (typeof web3 !== 'undefined') {
//Use Mist/MetaMask's provider
web3 = new Web3(web3.currentProvider);
} else {
// if the user hasn't installed metamask, we prompt the user to install it.
alert("Install Metamask");
// here, we create our own instance of web3 on our localhost listening to port 8545.
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
// we set the default account(owner) to the first account (index = 0) on metamask.
// this is the current account used in MetaMask.
web3.eth.defaultAccount = web3.eth.accounts[0];
// we link the web3 with the abi of the contract.
var mbst = web3.eth.contract(abi);
// we set the address of the contract to the webObject so that it can locate the contract.
try{
webObject = mbst.at(address);
}
catch(e){
console.log(e);
}
// we call the update_view() function which updates the page with the list of students that are already\
// in the database.
update_view();
})
// global variables.
var name = "";
var student_address = "";
var teacher = "";
var parent = "";
var parent_address = "";
var course = "";
var marks = "";
var attendance = "";
var profeciency = "";
var num = 0;
var currentStudentPeeps;
var currentStudentParams;
var currentStudentIndex=-1;
// function to reload the page everytime a new student is created so that we can display the names of all the
// students in the on load function.
$('#contact-form').submit(function () {
recieve();
return true;
});
// function to get the details of the student from the form and send the details to the ethereum backend.
function recieve(){
// getting the values of the input from the form.
name = document.getElementById('nam').value;
student_address = document.getElementById('student_address').value;
parent = document.getElementById('parent').value;
parent_address = document.getElementById('parent_address').value;
course = document.getElementById('course').value;
marks = document.getElementById('marks').value;
attendance = document.getElementById('attendance').value;
profeciency = document.getElementById('profeciency').value;
try {
// we use the webObject to call the create students function in our smart contract.
// we pass the appropriate parameters to the function (the detaials we get from the form).
webObject.createStudent( Number(attendance),
Number(marks),
String(student_address),
String(parent_address),
String(parent),
String(name),
String(course),
String(profeciency),
{ from: web3.eth.defaultAccount, gas: 3000000, value: web3.toWei(0.01)},
// callback function.
// this function gets the return value of the function in the smart contract(solidity)
// the return value is stored in the variable res
// res can only be accessed inside the scope of this function.
// it cannot be set to a global variable and accessed outside the function.
// this function should be added at the end of every function call from
// the webObject.
// it also gets an error stored in err if there is some error.
function(err, res) {
if(!err)
alert("student created.");
else
console.log(err);
return false;
});
} catch (e) {
alert(e);
}
add_student();
}
// this function prompts the user to submit the transaction on metamask
// if the transaction is not submitted, then the student won't be added
// so, it is important for the user to first submit the transaction on MetaMask
// before clicking on ok.
function add_student() {
alert('Submit on MetaMask before clicking on OK !! ');
update_view();
}
//
function update_view() {
// we are dynamically creating elements and adding them to the view.
// this is the reference of the parent div in html.
var parent = document.getElementById('list_items');
// this is the unordered list (html tag).
var listview = document.createElement('ul');
listview.setAttribute('id', 'view');
parent.appendChild(listview);
// calling the getSize function in the smart contract using a callback function.
// this function returns the size of the student array (in other words, the number
// of students in the database)
// the size is stored in the res variable. this cannot be accessed outside this function.
// therefore, we have added a nested function that calls getStudent inside this getsize function
webObject.getSize(function(err, res) {
if(!err);
//alert("successCreate");
else
console.log(err);
// a for loop that runs for 'size' times. to iterate over every student
// in the database.
for (i= 0; i< res; i++) {
try {
// we use the webObject to call the getStudent function which returns the
// details of the students in the database.
webObject.getStudentPeeps(i,
// the callback function that stores the return value
// of the getStudent function(in the smart contract)
// in the variable res1.
// res1 is an array that contains all the details of a
// particular student.
function(err1, res1) {
if(!err1)
;
else
console.log(err1);
// dynamically creating a list item
var el = document.createElement('li');
// giving the id to the list element.
// this id is treated as the index to access
// the particular student in the smart contract array.
el.setAttribute('id', num);
el.innerHTML = res1[0]; // res[0] contains the name.
listview.appendChild(el);
// setting an onlckick listener to the dynamically added
// name of the student.
el.onclick = function() {
get_details(this); // get_detail is called onclick.
};
num = num + 1; // incrementing the id.
}
);
} catch (e) {
console.log(e);
}
}
}
);
}
// function to get the details of the student specified by the id.
function get_details(el){
currentStudentIndex=el.id;
webObject.getStudentPeeps(el.id, function(err, res) { // callback function
if(!err)
setStudentPeeps(res);
else
console.log(err);
});
webObject.getStudentParams(el.id, function(err, res) { // callback function
if(!err)
setStudentParams(res);
else
console.log(err);
});
update_details_view();
}
function setStudentPeeps(result) {
currentStudentPeeps=result;
}
function setStudentParams(result) {
currentStudentParams=result;
}
function update_details_view() {
// dynamically displaying the details.
// the details are stored in the 'res' array.
document.getElementById('name_span').innerHTML = currentStudentPeeps[0];
document.getElementById('teach_span').innerHTML = currentStudentPeeps[2];
document.getElementById('par_span').innerHTML = currentStudentPeeps[4];
document.getElementById('hw_span').innerHTML = currentStudentParams[0];
document.getElementById('course_span').innerHTML = currentStudentParams[1];
document.getElementById('mar_span').innerHTML = currentStudentParams[2];
document.getElementById('att_span').innerHTML = currentStudentParams[3];
document.getElementById('emp_span').innerHTML = currentStudentParams[4];
document.getElementById('pro_span').innerHTML = currentStudentParams[5];
}
// function to employ the student.
function employ(){
if(currentStudentIndex===-1)
alert("Select student first!");
else {
webObject.employStudent(currentStudentIndex,
{ from: web3.eth.defaultAccount, gas: 3000000, value: web3.toWei(0.01)},
function(err, res) {
if(!err)
alert("Student Employment requested");
else {
console.log(err);
alert("Not an employer");
}
})
}
}
// function to assign homework to the student.
function givehw(){
if(currentStudentIndex===-1)
alert("Select student first!");
else {
webObject.newHW(currentStudentIndex,
function(err, res) {
if(!err)
alert("Homework assigned");
else {
console.log(err);
alert("Not the Student's teacher");
}
})
}
}
// function to finish homework.
function finish_hw(){
if(currentStudentIndex===-1)
alert("Select student first!");
else {
webObject.finishHW(currentStudentIndex,
{from: web3.eth.defaultAccount, gas: 3000000},
function(err, res) {
if(!err)
alert("Homework Done");
else {
console.log(err);
alert("Not the right student");
}
})
}
}
</script>
</div>
</div>
</body>
</html>