diff --git a/api/polling/universitysync.php b/api/polling/universitysync.php new file mode 100644 index 0000000000000000000000000000000000000000..18b1655a5c1c4bd723c0d6c42c0bb309a8a43c84 --- /dev/null +++ b/api/polling/universitysync.php @@ -0,0 +1,75 @@ +<?php +require_once 'app/core/App.php'; +require_once 'app/core/Database.php'; +require_once 'app/models/soap.php'; +require_once 'config/config.php'; + +$soap = new SOAP("UniversityService?wsdl"); + +$response = $soap->doRequest("getAllUniversities", null); + +$db = new Database(); + +if(is_array($response->return)){ + for($i = 0; $i < count($response->return) ; $i++){ + $uni_name = $response->return[$i]->name; + $phpUniId = $response->return[$i]->phpUniId; + $restUniId = $response->return[$i]->restUniId; + + if($phpUniId == -1){ + $query = "SELECT * FROM university WHERE name = ?"; + $stmt = $db->setSTMT($query); + mysqli_stmt_bind_param($stmt,"s", $uni_name); + $exec = mysqli_stmt_execute($stmt); + /* If there is this instance do a soap request*/ + if($exec){ + $res = mysqli_stmt_get_result($stmt); + $row = mysqli_fetch_array($res); + $params = array("php_uni_id" => $row['university_id'], "rest_uni_id" => $restUniId); + $response = $soap->doRequest("setPHPId", $params); + }else{ + /* Create the instance in PHP*/ + $query = "INSERT INTO university (name) VALUES ?"; + $stmt = $db->setSTMT($query); + mysqli_stmt_bind_param($stmt,"s", $uni_name); + $exec = mysqli_stmt_execute($stmt); + if($exec){ + $id = mysqli_insert_id($db->getDatabase()); + $params = array("php_uni_id" => $id, "rest_uni_id" => $restUniId); + $response = $soap->doRequest("setPHPId", $params); + } + } + } + } +}else{ + $uni_name = $response->return->name; + $phpUniId = $response->return->phpUniId; + $restUniId = $response->return->restUniId; + + if($phpUniId == -1){ + $query = "SELECT * FROM university WHERE name = ?"; + $stmt = $db->setSTMT($query); + mysqli_stmt_bind_param($stmt,"s", $uni_name); + $exec = mysqli_stmt_execute($stmt); + /* If there is this instance do a soap request*/ + if($exec){ + $res = mysqli_stmt_get_result($stmt); + $row = mysqli_fetch_array($res); + $params = array("php_uni_id" => $row["university_id"], "rest_uni_id" => $restUniId); + $response = $soap->doRequest("setPHPId", $params); + }else{ + /* Create the instance in PHP*/ + $query = "INSERT INTO university (name) VALUES ?"; + $stmt = $db->setSTMT($query); + mysqli_stmt_bind_param($stmt,"s", $uni_name); + $exec = mysqli_stmt_execute($stmt); + if($exec){ + $id = mysqli_insert_id($db->getDatabase()); + $params = array("php_uni_id" => $id, "rest_uni_id" => $restUniId); + $response = $soap->doRequest("setPHPId", $params); + } + } + } +} + +?> \ No newline at end of file diff --git a/api/university/stats.php b/api/university/stats.php new file mode 100644 index 0000000000000000000000000000000000000000..62b905c2519dec3ed5489f2aad90818631d0b6b1 --- /dev/null +++ b/api/university/stats.php @@ -0,0 +1,13 @@ +<?php +require_once '../../app/core/App.php'; +require_once '../../app/core/Database.php'; +require_once '../../config/config.php'; + +$db = new Database(); +$query = "SELECT university_id, name, count(*) as count FROM university NATURAL JOIN student GROUP BY university_id"; +$stmt = $db->setSTMT($query); +$result = mysqli_stmt_execute($stmt); +$row = mysqli_fetch_all(mysqli_stmt_get_result($stmt), MYSQLI_ASSOC); + +echo json_encode($row); +?> \ No newline at end of file diff --git a/api/user/register.php b/api/user/register.php index e165a67c25ec2a31912de98953b78b64f3382993..93776dfd9913f204ff68b37accb43568740591f5 100644 --- a/api/user/register.php +++ b/api/user/register.php @@ -19,7 +19,7 @@ session_start(); if (!isset($_SESSION['role'])) { $student = new Student(); $token = bin2hex(random_bytes(16)); - $succ = $student->register($_POST['name'], "student", $_POST['email'], $_POST['password'], $token); + $succ = $student->register($_POST['name'], "student", $_POST['email'], $_POST['password'], $token, $_POST['university']); if ($succ === true) { $email = $_POST['email']; $name = $_POST['name']; diff --git a/app/controllers/admin.php b/app/controllers/admin.php index d4fb5391731a8b22f98c31d3e9bfb62409518d7c..3afe3022894ad3bfaa5db61031dea4026e607066 100644 --- a/app/controllers/admin.php +++ b/app/controllers/admin.php @@ -4,7 +4,7 @@ require_once 'app/core/Database.php'; require_once 'app/models/User.php'; require_once 'app/models/superadmin.php'; require_once 'config/config.php'; - +require_once './api/polling/universitysync.php'; class Admin extends Controller { @@ -32,19 +32,6 @@ class Admin extends Controller } } - public function university() - { - if ($_SESSION['role'] == 'super admin') { - $data['judul'] = 'Add University'; - $data['style'] = "/public/css/addDocument.css"; - $this->view('header/index', $data); - $this->view('navbar/index', $data); - $this->view('admin/addUniversity', $data); - } else { - header("Location: /page404"); - } - } - public function update() { diff --git a/app/controllers/register.php b/app/controllers/register.php index a8c12340f2614bc383090a03fad7e83c62e472ca..f9d6bba02cc07b47073f439c4bc214b26f542a15 100644 --- a/app/controllers/register.php +++ b/app/controllers/register.php @@ -1,5 +1,8 @@ <?php - +require_once 'app/core/App.php'; +require_once 'app/core/Database.php'; +require_once 'config/config.php'; +require_once './api/polling/universitysync.php'; class Register extends Controller { public function index() diff --git a/app/models/administrator.php b/app/models/administrator.php index 73f013f2133a7a8db88b3c8d3500d3e2cd9458f6..92501078b387ce5638aabd6b67e7518f52d9883b 100644 --- a/app/models/administrator.php +++ b/app/models/administrator.php @@ -19,7 +19,7 @@ class Administrator extends User{ mysqli_stmt_execute($stmt); } - public function register(string $name, string $role, string $email, string $password, string $token, string $university = ""){ + public function register(string $name, string $role, string $email, string $password, string $token, int $university = 0){ $this->name = $name; $this->role = $role; $this->email = $email; diff --git a/app/models/reviewer.php b/app/models/reviewer.php index 4a09fc48520eda25de4704f1799f561c47e83fef..49660b61ab74ffaf0fdc40f259f3e5d253ee65bd 100644 --- a/app/models/reviewer.php +++ b/app/models/reviewer.php @@ -18,7 +18,7 @@ class Reviewer extends User{ return mysqli_stmt_execute($stmt); } - public function register(string $name, string $role, string $email, string $password, string $token, string $university = ""){ + public function register(string $name, string $role, string $email, string $password, string $token, int $university = 0){ $this->name = $name; $this->role = $role; $this->email = $email; diff --git a/app/models/student.php b/app/models/student.php index c1926c7a34afe1e56e902b8b96e383188ff3ed3d..de16f11c8b7d907c02149ef39e5308fe713e6be4 100644 --- a/app/models/student.php +++ b/app/models/student.php @@ -14,20 +14,20 @@ class Student extends User { public function update($editVal){ $query = "UPDATE $this->table SET - university = ?, major = ?, level = ?, street = ?, city = ?, zipcode = ? + major = ?, level = ?, street = ?, city = ?, zipcode = ? WHERE user_id = ?"; $stmt = $this->db->setSTMT($query); - mysqli_stmt_bind_param($stmt, "sssssii", $editVal['university'], $editVal['major'], $editVal['level'], + mysqli_stmt_bind_param($stmt, "ssssii", $editVal['major'], $editVal['level'], $editVal['street'], $editVal['city'], $editVal['zipcode'], $editVal['user_id']); mysqli_stmt_execute($stmt); } - public function register(string $name, string $role, string $email, string $password, string $token, string $university){ + public function register(string $name, string $role, string $email, string $password, string $token, int $university){ $this->name = $name; $this->role = $role; $this->email = $email; @@ -46,9 +46,9 @@ class Student extends User { $this->userID = mysqli_insert_id($this->db->getDatabase()); /* INSERT INTO STUDENT */ - $query = "INSERT INTO student (user_id, university) values (?,?)"; + $query = "INSERT INTO student (user_id, university_id) values (?,?)"; $stmt = $this->db->setSTMT($query); - mysqli_stmt_bind_param($stmt, "is", $this->userID, $university); + mysqli_stmt_bind_param($stmt, "ii", $this->userID, $university); $insert = mysqli_stmt_execute($stmt); } return $insert; diff --git a/app/models/user.php b/app/models/user.php index 0a3c8c1505012869faf7f3b831a733b370322896..33de3238a792b1b102d59c1e4b8cd1114c8cf2b9 100644 --- a/app/models/user.php +++ b/app/models/user.php @@ -18,8 +18,9 @@ class User{ public function getUser(string $email, string $role){ $query = ""; if($role == "student"){ - $query = "select name, image, university, major, level, street, city, zipcode - from $this->table natural join student where email = ?"; + $query = "select user.name as name, university.name as university, image, major, level, street, city, zipcode + from $this->table natural join student inner join university on student.university_id = university.university_id + where email = ?"; }else if ($role == "admin"){ $query = "select name, image, organization from $this->table natural join administrator where email = ?"; }else if($role == 'reviewer'){ @@ -103,7 +104,7 @@ class User{ } } - public function register(string $name, string $role, string $email, string $password, string $token, string $university){ + public function register(string $name, string $role, string $email, string $password, string $token, int $university){ $this->name = $name; $this->role = $role; $this->email = $email; diff --git a/app/views/admin/addUniversity.php b/app/views/admin/addUniversity.php deleted file mode 100644 index b86187504de378cfc6f229ae6516213453a9565d..0000000000000000000000000000000000000000 --- a/app/views/admin/addUniversity.php +++ /dev/null @@ -1,16 +0,0 @@ -<div class="add-document"> - <h1>Add University</h1> - <div class="form"> - <form action="javascript:;" onsubmit="return submitForm()" enctype="multipart/form-data"> - <div class="input-container"> - <label for="file">University</label> - <input type="text" name="university" id="file" required/> - </div> - <div class="button-container"> - <a href="/dashboard" class="cancel-btn">Cancel</a> - <button type="submit" class="save-btn">Tambah Universitas</button> - </div> - </form> - </div> -</div> -<script src="../../../public/js/addUniversity.js"></script> \ No newline at end of file diff --git a/app/views/admin/addnewuser.php b/app/views/admin/addnewuser.php index c89c138fe708cbe47b78b5db5f08f5f7b71132de..bfdac6d7b7f3044fd743bfc2d69bfa1a8131b666 100644 --- a/app/views/admin/addnewuser.php +++ b/app/views/admin/addnewuser.php @@ -23,14 +23,14 @@ <select name="university" id="university"> <?php $db = new Database(); - $query = "SELECT name FROM university"; + $query = "SELECT university_id, name FROM university"; $stmt = $db->setSTMT($query); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); while( $row = mysqli_fetch_assoc($result) ) { ?> - <option value="<?php echo $row['name'];?>"><?php echo $row['name'];?></option> + <option value="<?php echo $row['university_id'];?>"><?php echo $row['name'];?></option> <?php } ?> </select> diff --git a/app/views/navbar/index.php b/app/views/navbar/index.php index 29a76ee995f7e983dea2771da9c8c2696d09363a..cf055c0e83ae21c8fe1ead98591114726aa6f255 100644 --- a/app/views/navbar/index.php +++ b/app/views/navbar/index.php @@ -59,7 +59,6 @@ <li><a href="/dashboard">Dashboard</a></li> <li><a href="/admin/add">Add User</a></li> <li><a href="/admin/list">List User</a></li> - <li><a href="/admin/university">Add University</a></li> <li><a href="/aboutus">About Us</a></li> <div class="profile"> <a class="dropbtn"> diff --git a/app/views/register/index.php b/app/views/register/index.php index a3a88fc0b40c01894275eeee8373cae6ef0075f3..f31aedfd1fd5ae9a6db7b221ce265f930107dc9d 100644 --- a/app/views/register/index.php +++ b/app/views/register/index.php @@ -14,6 +14,24 @@ </div> </div> </div> + <div class="TextField"> + <div class="LabelAndField"> + <div class="Label">University</div> + <select name="university" id="university" class="Field"> + <?php + $db = new Database(); + $query = "SELECT university_id, name FROM university"; + $stmt = $db->setSTMT($query); + mysqli_stmt_execute($stmt); + $result = mysqli_stmt_get_result($stmt); + + while( $row = mysqli_fetch_assoc($result) ) { + ?> + <option value="<?php echo $row['university_id'];?>"><?php echo $row['name'];?></option> + <?php } ?> + </select> + </div> + </div> <div class="TextField"> <div class="LabelAndField"> <div class="Label">Email Address</div> diff --git a/db/scholee.sql b/db/scholee.sql index 8844b0e16fe0f2671f0c18f8b89848cb3bc16b8f..8a72de701151ffc227ac5a1a026afa6a10facd00 100644 --- a/db/scholee.sql +++ b/db/scholee.sql @@ -3,8 +3,8 @@ -- https://www.phpmyadmin.net/ -- -- Host: db:3306 --- Generation Time: Nov 06, 2023 at 01:10 PM --- Server version: 8.1.0 +-- Generation Time: Nov 14, 2023 at 07:30 AM +-- Server version: 8.2.0 -- PHP Version: 8.2.8 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; @@ -32435,7 +32435,7 @@ INSERT INTO `scholarshiptype` (`user_id`, `scholarship_id`, `type`) VALUES CREATE TABLE `student` ( `user_id` int NOT NULL, - `university` varchar(255) NOT NULL, + `university_id` int NOT NULL, `major` varchar(100) NOT NULL DEFAULT '', `level` enum('Undergraduate','Postgraduate','Doctoral') DEFAULT 'Undergraduate', `street` varchar(255) NOT NULL DEFAULT '', @@ -32447,8 +32447,8 @@ CREATE TABLE `student` ( -- Dumping data for table `student` -- -INSERT INTO `student` (`user_id`, `university`, `major`, `level`, `street`, `city`, `zipcode`) VALUES -(12, 'Institut Teknologi Bandung', '', 'Undergraduate', '', '', 0); +INSERT INTO `student` (`user_id`, `university_id`, `major`, `level`, `street`, `city`, `zipcode`) VALUES +(7, 1, '', 'Undergraduate', '', '', 0); -- -------------------------------------------------------- @@ -32484,7 +32484,9 @@ CREATE TABLE `university` ( INSERT INTO `university` (`university_id`, `name`) VALUES (1, 'Institut Teknologi Bandung'), -(2, 'Universitas Indonesia'); +(2, 'Universitas Brawijaya'), +(3, 'Universitas Indonesia'), +(4, 'Universitas Padjadjaran'); -- -------------------------------------------------------- @@ -32514,7 +32516,7 @@ INSERT INTO `user` (`user_id`, `name`, `password`, `role`, `email`, `image`, `re (4, 'Matthew Mahendra', '$2y$10$RjI9wvx5NYCAuHsPDgYUuutX3m68mM3brAyGsU1uGT7SrNqXIQwwu', 'admin', '13521007@std.stei.itb.ac.id', 'placeholder.jpg', NULL, 1, NULL), (5, 'Henry Anand Septian R', '$2y$10$EJ8x0uNsacE8qR9FOa269u2sYVcXg3abkMO/LWk0EnW9qssaDqFoG', 'admin', '13521004@std.stei.itb.ac.id', 'placeholder.jpg', NULL, 1, NULL), (6, 'Reviewer', '$2y$10$Sit9M654QvqhZaoGKeqXYuzyqQMUCJKxD12AvPfFNbJE2ByqsNova', 'reviewer', 'reviewer@gmail.com', 'placeholder.jpg', NULL, 1, NULL), -(12, 'Matthew Mahendra', '$2y$10$3ps7xKz8RvZfF2MtgI6nku1DkKe/RQC9GCxT9CRQtcXl0N5RGDaBC', 'student', 'matthew.mahendra@gmail.com', 'placeholder.jpg', NULL, 1, NULL); +(7, 'Matthew Mahendra', '$2y$10$3ps7xKz8RvZfF2MtgI6nku1DkKe/RQC9GCxT9CRQtcXl0N5RGDaBC', 'student', 'matthew.mahendra@gmail.com', 'placeholder.jpg', NULL, 1, NULL); -- -- Indexes for dumped tables @@ -32569,7 +32571,7 @@ ALTER TABLE `scholarshiptype` -- ALTER TABLE `student` ADD PRIMARY KEY (`user_id`), - ADD KEY `university` (`university`); + ADD KEY `university_id` (`university_id`); -- -- Indexes for table `superadmin` @@ -32581,7 +32583,8 @@ ALTER TABLE `superadmin` -- Indexes for table `university` -- ALTER TABLE `university` - ADD PRIMARY KEY (`name`); + ADD PRIMARY KEY (`university_id`), + ADD UNIQUE KEY `name` (`name`); -- -- Indexes for table `user` @@ -32604,7 +32607,7 @@ ALTER TABLE `administrator` -- AUTO_INCREMENT for table `student` -- ALTER TABLE `student` - MODIFY `user_id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13; + MODIFY `user_id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8; -- -- AUTO_INCREMENT for table `superadmin` @@ -32612,11 +32615,17 @@ ALTER TABLE `student` ALTER TABLE `superadmin` MODIFY `user_id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; +-- +-- AUTO_INCREMENT for table `university` +-- +ALTER TABLE `university` + MODIFY `university_id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5; + -- -- AUTO_INCREMENT for table `user` -- ALTER TABLE `user` - MODIFY `user_id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14; + MODIFY `user_id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8; -- -- Constraints for dumped tables @@ -32671,7 +32680,7 @@ ALTER TABLE `scholarshiptype` -- ALTER TABLE `student` ADD CONSTRAINT `student_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE, - ADD CONSTRAINT `student_ibfk_2` FOREIGN KEY (`university`) REFERENCES `university` (`name`) ON DELETE RESTRICT ON UPDATE RESTRICT; + ADD CONSTRAINT `student_ibfk_2` FOREIGN KEY (`university_id`) REFERENCES `university` (`university_id`) ON DELETE RESTRICT ON UPDATE RESTRICT; -- -- Constraints for table `superadmin`