-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgroupsFunctions.php
354 lines (350 loc) · 15.2 KB
/
groupsFunctions.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
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
<?php
//include js
function utt_group_scripts(){
//include groupScripts
wp_enqueue_script( 'groupScripts', plugins_url('js/groupScripts.js', __FILE__) );
//localize groupScripts
wp_localize_script( 'groupScripts', 'groupStrings', array(
'deleteForbidden' => __( 'Delete is forbidden while completing the form!', 'UniTimetable' ),
'deleteGroup' => __( 'Are you sure that you want to delete this Group?', 'UniTimetable' ),
'groupDeleted' => __( 'Group deleted successfully!', 'UniTimetable' ),
'groupNotDeleted' => __( 'Failed to delete Group. Check if Group is used by a Lecture.', 'UniTimetable' ),
'editForbidden' => __( 'Edit is forbidden while completing the form!', 'UniTimetable' ),
'editGroup' => __( 'Edit Group', 'UniTimetable' ),
'cancel' => __( 'Cancel', 'UniTimetable' ),
'periodVal' => __( 'Please select a Period.', 'UniTimetable' ),
'semesterVal' => __( 'Please select Semester.', 'UniTimetable' ),
'subjectVal' => __( 'Please select Subject.', 'UniTimetable' ),
'nameVal' => __( 'Please avoid using special characters and do not use long names.', 'UniTimetable' ),
'insertGroup' => __( 'Insert Group', 'UniTimetable' ),
'reset' => __( 'Reset', 'UniTimetable' ),
'group' => __( 'Group', 'UniTimetable' ),
'failAdd' => __( 'Failed to add Groups. Check if there are another Groups with the same attributes.', 'UniTimetable' ),
'successAdd' => __( 'Groups added successfully!', 'UniTimetable' ),
'failEdit' => __( 'Failed to edit Group. Check if there is another Group with the same attributes.', 'UniTimetable' ),
'successEdit' => __( 'Group successfully edited!', 'UniTimetable' ),
));
}
//groups page
function utt_create_groups_page(){
//group form
?>
<div class="wrap">
<h2 id="groupTitle"> <?php _e("Insert Group","UniTimetable"); ?> </h2>
<form action="" name="groupForm" method="post">
<input type="hidden" name="groupID" id="groupID" value=0 />
<div class="element firstInRow">
<?php _e("Period:","UniTimetable"); ?><br/>
<select name="period" id="period" class="dirty">
<option value="0"><?php _e("- select -","UniTimetable"); ?></option>
<?php
global $wpdb;
//show registered periods
$periodsTable=$wpdb->prefix."utt_periods";
$periods = $wpdb->get_results( "SELECT * FROM $periodsTable ORDER BY year DESC");
//translate periods semester
foreach($periods as $period){
if($period->semester == "W"){
$semester = __("W","UniTimetable");
}else{
$semester = __("S","UniTimetable");
}
echo "<option value='$period->periodID'>$period->year $semester</option>";
}
?>
</select>
</div>
<div class="element">
<?php _e("Semester:","UniTimetable"); ?><br/>
<select name="semester" id="semester" class="dirty" onchange="loadSubjects(0);">
<option value="0"><?php _e("- select -","UniTimetable"); ?></option>
<?php
//show semester numbers
for( $i=1 ; $i<11 ; $i++ ){
echo "<option value='$i'>$i</option>";
}
?>
</select>
</div>
<div class="element firstInRow">
<?php _e("Subject:","UniTimetable"); ?><br/>
<!-- load subjects when semester number is selected -->
<div id="subjects">
<select name="subject" id="subject" class="dirty">
<option value='0'><?php _e("- select -","UniTimetable"); ?></option>
</select>
</div>
</div>
<div class="element">
<!-- select number of groups to be created -->
<?php _e("Number of Groups:","UniTimetable"); ?><br/>
<select name="groupsNumber" id="groupsNumber" class="dirty">
<?php
for($i=1;$i<16;$i++){
echo "<option value=$i>$i</option>";
}
?>
</select>
</div>
<div class="element firstInRow groupsName">
<?php _e("Name of Groups (Prefix):","UniTimetable"); ?><br/>
<!-- prefix of groups' names -->
<input type="text" name="groupsName" id="groupsName" class="dirty" value="<?php _e("Group","UniTimetable"); ?>"/>
</div>
<div class="element counterStart">
<?php _e("Counter Starτ:","UniTimetable"); ?>
<!-- starting number of groups that will be created -->
<select name="counterStart" id="counterStart" class="dirty">
<?php
for($i=1;$i<16;$i++){
echo "<option value=$i>$i</option>";
}
?>
</select>
</div>
<div id="secondaryButtonContainer">
<input type="submit" value="<?php _e("Submit","UniTimetable"); ?>" id="insert-updateGroup" class="button-primary"/>
<a href='#' class='button-secondary' id="clearGroupForm"><?php _e("Reset","UniTimetable"); ?></a>
</div>
</form>
<!-- place to view messages -->
<div id="messages"></div>
<!-- filters to filter shown groups -->
<span id="filter1">
<?php _e("Period:","UniTimetable"); ?>
<select name="periodFilter" id="periodFilter" onchange="viewGroups();">
<?php
$periodsTable=$wpdb->prefix."utt_periods";
$periods = $wpdb->get_results( "SELECT * FROM $periodsTable ORDER BY year DESC");
//current date
$date = date("Y-m-d");
echo "<option value=''>".__("- select -","UniTimetable")."</option>";
foreach($periods as $period){
//echo current and next year and select current
$startWinter = $period->year."-09-01";
$nextYear = $period->year +1;
$endWinter = $nextYear . "-03-01";
$startSpring = $period->year . "-03-01";
$selected = "";
if($date >= $startWinter && $date < $endWinter && $period->semester == "W"){
$selected = "selected='selected'";
$periodID = $period->periodID;
}else if($date >= $startSpring && $date<$startWinter && $period->semester == "S"){
$selected = "selected='selected'";
$periodID = $period->periodID;
}
if($period->semester == "W"){
$semester = __("W","UniTimetable");
}else{
$semester = __("S","UniTimetable");
}
echo "<option value='$period->periodID' $selected>$period->year $semester</option>";
}
?>
</select>
</span>
<span id="filter2">
<?php _e("Semester:","UniTimetable"); ?>
<select name="semesterFilter" id="semesterFilter" onchange="viewGroups();">
<option value="0"><?php _e("All","UniTimetable"); ?></option>
<?php
for($i=1;$i<11;$i++){
//select 1st semester
if($i == 1){
$selected = "selected='selected'";
}else{
$selected = "";
}
echo "<option value='$i' $selected>$i</option>";
}
?>
</select>
</span>
<!-- place to view inserted groups -->
<div id="groupsResults">
<?php utt_view_groups(); ?>
</div>
<?php
}
//ajax response view groups
add_action('wp_ajax_utt_view_groups','utt_view_groups');
function utt_view_groups(){
global $wpdb;
//get filter values
if(isset($_GET['period_id'])){
$periodID = $_GET['period_id'];
$semester = $_GET['semester'];
//if first time loaded, show results for current period and 1st semester
}else{
$periodsTable=$wpdb->prefix."utt_periods";
$periods = $wpdb->get_results( "SELECT * FROM $periodsTable ORDER BY year DESC");
$date = date("Y-m-d");
foreach($periods as $period){
$startWinter = $period->year."-09-01";
$nextYear = $period->year +1;
$endWinter = $nextYear . "-03-01";
$startSpring = $period->year . "-03-01";
if($date >= $startWinter && $date < $endWinter && $period->semester == "W"){
$periodID = $period->periodID;
}else if($date >= $startSpring && $date<$startWinter && $period->semester == "S"){
$periodID = $period->periodID;
}
}
$semester = 1;
}
//show registered groups
$groupsTable = $wpdb->prefix."utt_groups";
$subjectsTable = $wpdb->prefix."utt_subjects";
//if not selected period, show nothing
if($periodID == ""){
$periodID=0;
}
//if not selected semester, show for all semesters
if($semester==0){
$safeSql = $wpdb->prepare("SELECT * FROM $groupsTable, $subjectsTable WHERE $groupsTable.subjectID=$subjectsTable.subjectID AND periodID=%d ORDER BY title, type, groupName",$periodID);
}else{
$safeSql = $wpdb->prepare("SELECT * FROM $groupsTable, $subjectsTable WHERE $groupsTable.subjectID=$subjectsTable.subjectID AND periodID=%d AND semester=%d ORDER BY title, type, groupName",$periodID,$semester);
}
$groups = $wpdb->get_results($safeSql);
?>
<!-- show table of groups -->
<table class="widefat bold-th">
<thead>
<tr>
<th><?php _e("Subject","UniTimetable"); ?></th>
<th><?php _e("Group","UniTimetable"); ?></th>
<th><?php _e("Actions","UniTimetable"); ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th><?php _e("Subject","UniTimetable"); ?></th>
<th><?php _e("Group","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($groups as $group){
if($bgcolor == 1){
$addClass = "class='grey'";
$bgcolor = 2;
}else{
$addClass = "class='white'";
$bgcolor = 1;
}
if($group->type == "T"){
$type = __("T","UniTimetable");
}else if($group->type == "L"){
$type = __("L","UniTimetable");
}else{
$type = __("PE","UniTimetable");
}
//a record
echo "<tr id='$group->groupID' $addClass><td>$group->title $type</td><td>$group->groupName</td>
<td><a href='#' onclick='deleteGroup($group->groupID);' class='deleteGroup'><img id='edit-delete-icon' src='".plugins_url('icons/delete_icon.png', __FILE__)."'/> ".__("Delete","UniTimetable")."</a>
<a href='#' onclick=\"editGroup($group->groupID,$group->periodID,$group->semester,$group->subjectID,'$group->groupName');\" class='editGroup'><img id='edit-delete-icon' src='".plugins_url('icons/edit_icon.png', __FILE__)."'/> ".__("Edit","UniTimetable")."</a></td></tr>";
}
?>
</tbody>
</table>
<?php
die();
}
//ajax response insert-update group
add_action('wp_ajax_utt_insert_update_group','utt_insert_update_group');
function utt_insert_update_group(){
global $wpdb;
//data to be inserted/updated
$groupID=$_GET['group_id'];
$periodID=$_GET['period_id'];
$subjectID=$_GET['subject_id'];
$groupName=$_GET['group_name'];
$counterStart=$_GET['counter_start'];
$groupsNumber=$_GET['groups_number'];
$groupsTable=$wpdb->prefix."utt_groups";
$success = 0;
// if groupID is 0, it is insert
if($groupID==0){
//transaction, so if an insert fails, it rolls back
$wpdb->query('START TRANSACTION');
for($i=1;$i<=$groupsNumber;$i++){
//name is generated by prefix(groupName) and a number, starting from counterstart
$nameUsed = $groupName.$counterStart;
$safeSql = $wpdb->prepare("INSERT INTO $groupsTable (periodID, subjectID, groupName) VALUES (%d,%d,%s)",$periodID,$subjectID,$nameUsed);
$success = $wpdb->query($safeSql);
$counterStart ++;
if($success != 1){
//if an insert fails, for breaks
$success = 0;
break;
}
}
//if every insert succeeds, commit transaction
if($success == 1){
$wpdb->query('COMMIT');
echo 1;
//else rollback
}else{
$wpdb->query('ROLLBACK');
echo 0;
}
//it is edit
}else{
$safeSql = $wpdb->prepare("UPDATE $groupsTable SET periodID=%d, subjectID=%d, groupName=%s WHERE groupID=%d ",$periodID,$subjectID,$groupName,$groupID);
$success = $wpdb->query($safeSql);
if ($success==1){
echo 1;
}else{
echo 0;
}
}
die();
}
//ajax response delete group
add_action('wp_ajax_utt_delete_group', 'utt_delete_group');
function utt_delete_group(){
global $wpdb;
$groupsTable=$wpdb->prefix."utt_groups";
$safeSql = $wpdb->prepare("DELETE FROM `$groupsTable` WHERE groupID=%d",$_GET['group_id']);
$success = $wpdb->query($safeSql);
//if success is 1, delete succeeded
echo $success;
die();
}
//load subjects to combo-box when semester number is selected
add_action('wp_ajax_utt_load_groupsubjects','utt_load_groupsubjects');
function utt_load_groupsubjects(){
//semester number selected
$semester = $_GET['semester'];
//if edit, select the stored subject
$selected = $_GET['selected'];
global $wpdb;
$subjectsTable = $wpdb->prefix."utt_subjects";
$safeSql = $wpdb->prepare("SELECT * FROM $subjectsTable WHERE semester=%d ORDER BY title;",$semester);
$subjects = $wpdb->get_results($safeSql);
echo "<select name='subject' id='subject' class='dirty'>";
echo "<option value='0'>".__("- select -","UniTimetable")."</option>";
foreach($subjects as $subject){
//if edit, select the stored subject
if($selected==$subject->subjectID){
$select = "selected='selected'";
}else{
$select = "";
}
//translate subject type
if($subject->type == "T"){
$type = __("T","UniTimetable");
}else if($subject->type == "L"){
$type = __("L","UniTimetable");
}else{
$type = __("PE","UniTimetable");
}
echo "<option value='$subject->subjectID' $select>$subject->title $type</option>";
}
echo "</select>";
die();
}
?>