-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignmentUpload.php
75 lines (63 loc) · 3.69 KB
/
assignmentUpload.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
<?php
session_start();
require "connection.php";
if (isset($_SESSION["teacher"])) {
$subject = $_POST["subject"];
$class = $_POST["class"];
$deadline = $_POST["deadline"];
$type = $_POST["type"];
if ($subject == 0) {
echo ("Please select subject");
} else if ($class == 0) {
echo ("Please select class");
} else if (empty($deadline)) {
echo ("Please enter assignment deadline");
} else {
if (isset($_FILES["doc"])) {
$doc = $_FILES["doc"];
$allowedFileExtentions = array("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/pdf", "text/plain");
$fileExtention = $doc["type"];
if (in_array($fileExtention, $allowedFileExtentions)) {
$newFileExtention;
if ($fileExtention == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
$newFileExtention = ".docx";
} else if ($fileExtention == "application/pdf") {
$newFileExtention = ".pdf";
} else if ($fileExtention == "text/plain") {
$newFileExtention = ".txt";
}
$classResultset = Database::search("SELECT * FROM `grade` WHERE `id`='" . $class . "'");
$classData = $classResultset->fetch_assoc();
$subjectResultset = Database::search("SELECT * FROM `subject` WHERE `id`='" . $subject . "'");
$subjectData = $subjectResultset->fetch_assoc();
$teacherHasSubjectResultset = Database::search("SELECT * FROM `teacher_has_subject` WHERE `subject_id`='" . $subject . "' AND `grade_id`='" . $class . "' AND `teacher_email`='" . $_SESSION["teacher"]["email"] . "'");
$teacherHasSubjectData = $teacherHasSubjectResultset->fetch_assoc();
$classRoomResultset = Database::search("SELECT * FROM `classroom` WHERE `grade_id`='" . $class . "' AND `teacher_email`='" . $_SESSION["teacher"]["email"] . "'");
$classRoomData = $classRoomResultset->fetch_assoc();
$assignmentResultset = Database::search("SELECT * FROM `assignment` WHERE `classroom_id`='" . $classRoomData["id"] . "' AND `teacher_has_subject_id`='" . $teacherHasSubjectData["id"] . "' AND `type`='1'");
$assignmentData = $assignmentResultset->fetch_assoc();
$tdate = new DateTime();
$tz = new DateTimeZone("Asia/Colombo");
$tdate->setTimezone($tz);
$currentDate = $tdate->format("Y-m-d H:i:s");
function generateUniqId($startNum, $idLenght)
{
$uniqId = uniqid();
$newUniqId = substr($uniqId, intval($startNum), intval($idLenght));
return $newUniqId;
}
$assUniqId = generateUniqId(5, 4);
$filename = "resources/assignments/" . date("Y") . "_" . $classData["name"] . "_" . $subjectData["name"] . "_Assignment" . $assUniqId . $newFileExtention;
move_uploaded_file($doc["tmp_name"], $filename);
Database::insertUpdateDelete("INSERT INTO `assignment` (`name`,`deadline`,`classroom_id`,`teacher_has_subject_id`,`type`) VALUES ('" . $filename . "','" . $currentDate . "','" . $classRoomData["id"] . "','" . $teacherHasSubjectData["id"] . "','1')");
echo ("Assignment Uploaded");
} else {
echo ("Unsupported File Type");
}
} else {
echo ("Something Went Wrong");
}
}
} else {
echo ("Something Went Wrong");
}