-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathteachersFunctions.php
152 lines (144 loc) · 6.32 KB
/
teachersFunctions.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
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
<?php
//include js
function utt_teacher_scripts(){
//include teacher scripts
wp_enqueue_script( 'teacherScripts', plugins_url('js/teacherScripts.js', __FILE__) );
//localize teacher scripts
wp_localize_script( 'teacherScripts', 'teacherStrings', array(
'deleteForbidden' => __( 'Delete is forbidden while completing the form!', 'UniTimetable' ),
'deleteRecord' => __( 'Are you sure that you want to delete this record?', 'UniTimetable' ),
'teacherDeleted' => __( 'Teacher deleted successfully!', 'UniTimetable' ),
'teacherNotDeleted' => __( 'Failed to delete Teacher. Check if Teacher is connected with a Lecture.', 'UniTimetable' ),
'editForbidden' => __( 'Edit is forbidden while completing the form!', 'UniTimetable' ),
'editTeacher' => __( 'Edit Teacher', 'UniTimetable' ),
'cancel' => __( 'Cancel', 'UniTimetable' ),
'surnameVal' => __( 'Surname field is required. Please avoid using special characters.', 'UniTimetable' ),
'nameVal' => __( 'Please avoid using special characters at Name field.', 'UniTimetable' ),
'insertTeacher' => __( 'Insert Teacher', 'UniTimetable' ),
'reset' => __( 'Reset', 'UniTimetable' ),
'failAdd' => __( 'Failed to add Teacher. Check if the Teacher already exists.', 'UniTimetable' ),
'successAdd' => __( 'Teacher successfully added!', 'UniTimetable' ),
'failEdit' => __( 'Failed to edit Teacher. Check if the Teacher already exists.', 'UniTimetable' ),
'successEdit' => __( 'Teacher successfully edited!', 'UniTimetable' ),
));
}
//teachers page
function utt_create_teachers_page(){
//teachers form
?>
<div class="wrap" >
<h2 id="teacherTitle"><?php _e('Insert Teacher','UniTimetable'); ?></h2>
<form action="" name="teacherForm" method="post">
<input type="hidden" name="teacherid" id="teacherid" value=0 />
<?php _e("Surname:","UniTimetable"); ?><br/>
<input type="text" name="lastname" id="lastname" class="dirty" required placeholder="<?php _e("Required","UniTimetable"); ?>"/>
<br/>
<?php _e("Name:","UniTimetable"); ?><br/>
<input type="text" name="firstname" id="firstname" class="dirty"/>
<br/>
<div id="secondaryButtonContainer">
<input type="submit" value="<?php _e("Submit","UniTimetable"); ?>" id="insert-updateTeacher" class="button-primary"/>
<a href='#' class='button-secondary' id="clearTeacherForm"><?php _e("Reset","UniTimetable"); ?></a>
</div>
</form>
<!-- place to view messages -->
<div id="messages"></div>
<!-- place to view table with registered teachers -->
<div id="teachersResults">
<?php utt_view_teachers(); ?>
</div>
</div>
<?php
}
add_action('wp_ajax_utt_view_teachers', 'utt_view_teachers');
function utt_view_teachers(){
global $wpdb;
$teachersTable=$wpdb->prefix."utt_teachers";
//show registered teachers
$teachers = $wpdb->get_results("SELECT * FROM $teachersTable ORDER BY surname");
?>
<!-- table with registered teachers -->
<table class="widefat bold-th">
<thead>
<tr>
<th>ID</th>
<th><?php _e("Surname","UniTimetable"); ?></th>
<th><?php _e("Name","UniTimetable"); ?></th>
<th><?php _e("Actions","UniTimetable"); ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th><?php _e("Surname","UniTimetable"); ?></th>
<th><?php _e("Name","UniTimetable"); ?></th>
<th><?php _e("Actions","UniTimetable"); ?></th>
</tr>
</tfoot>
<tbody>
<?php
//show grey and white records in order to be more recognizable
$bgcolor = 1;
foreach($teachers as $teacher){
if($bgcolor == 1){
$addClass = "class='grey'";
$bgcolor = 2;
}else{
$addClass = "class='white'";
$bgcolor = 1;
}
//a record
echo "<tr id='$teacher->teacherID' $addClass><td>$teacher->teacherID</td><td>$teacher->surname</td><td>$teacher->name</td><td><a href='#' onclick='deleteTeacher($teacher->teacherID);' class='deleteTeacher'><img id='edit-delete-icon' src='".plugins_url('icons/delete_icon.png', __FILE__)."'/> ".__("Delete","UniTimetable")."</a> <a href='#' onclick=\"editTeacher($teacher->teacherID, '$teacher->surname', '$teacher->name');\" class='editTeacher'><img id='edit-delete-icon' src='".plugins_url('icons/edit_icon.png', __FILE__)."'/> ".__("Edit","UniTimetable")."</a></td></tr>";
}
?>
</tbody>
</table>
<?php
die();
}
//ajax response delete teacher
add_action('wp_ajax_utt_delete_teacher', 'utt_delete_teacher');
function utt_delete_teacher(){
global $wpdb;
$teachersTable=$wpdb->prefix."utt_teachers";
$safeSql = $wpdb->prepare("DELETE FROM $teachersTable WHERE teacherID= %d ", $_GET['teacher_id']);
$success = $wpdb->query($safeSql);
//if success is 1, delete succeeded
echo $success;
die();
}
//ajax response insert-update teacher
add_action('wp_ajax_utt_insert_update_teacher','utt_insert_update_teacher');
function utt_insert_update_teacher(){
global $wpdb;
//data
$firstname=$_GET['teacher_name'];
$lastname=$_GET['teacher_surname'];
$teacherid=$_GET['teacher_id'];
$teachersTable=$wpdb->prefix."utt_teachers";
//insert
if($teacherid==0){
$safeSql = $wpdb->prepare("INSERT INTO $teachersTable (name, surname) VALUES (%s,%s)",$firstname,$lastname);
$success = $wpdb->query($safeSql);
if($success == 1){
//success
echo 1;
}else{
//fail
echo 0;
}
//edit
}else{
$safeSql = $wpdb->prepare("UPDATE $teachersTable SET name=%s, surname=%s WHERE teacherID=%d; ",$firstname,$lastname,$teacherid);
$success = $wpdb->query($safeSql);
if($success == 1){
//success
echo 1;
}else{
//fail
echo 0;
}
}
die();
}
?>