-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupsFunctions.php
417 lines (390 loc) · 16.3 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
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
<?php
//include js
function utt_group_scripts(){
//include groupScripts
wp_enqueue_script( 'groupScripts', plugins_url('js/groupScripts.js?v=1576043133', __FILE__) );
//localize groupScripts
wp_localize_script( 'groupScripts', 'groupStrings', array(
'deleteForbidden' => __( 'Formu doldururken silmek yasaktır. Lütfen sayfayı yenileyiniz.', 'UniTimetable' ),
'deleteGroup' => __( 'Bu Şubeyi silmek istediğinize emin misiniz?', 'UniTimetable' ),
'groupDeleted' => __( 'Şube başarıyla silindi!', 'UniTimetable' ),
'groupNotDeleted' => __( 'Şube silinemedi. Çizelgede yerleştirilmiş halde bulunan şubeler silinemez. Lütfen önce şubeyi çizelgeden kaldırınız.', 'UniTimetable' ),
'editForbidden' => __( 'Formu doldururken düzenleme yapmak yasaktır! Lütfen sayfayı yenileyiniz.', 'UniTimetable' ),
'editGroup' => __( 'Şubeyi düzenle', 'UniTimetable' ),
'cancel' => __( 'İptal', 'UniTimetable' ),
'periodVal' => __( 'Lütfen dönem seçin.', 'UniTimetable' ),
'semesterVal' => __( 'Lütfen bölüm seçin.', 'UniTimetable' ),
'subjectVal' => __( 'Lütfen ders seçin.', 'UniTimetable' ),
'nameVal' => __( 'Lütfen özel karakterler kullanmaktan kaçının ve uzun isimler kullanmayın.', 'UniTimetable' ),
'insertGroup' => __( 'Şube ekle', 'UniTimetable' ),
'reset' => __( 'Sıfırla', 'UniTimetable' ),
'group' => __( 'Şube', 'UniTimetable' ),
'failAdd' => __( 'Şubeler eklenemedi. Aynı özelliklere sahip başka şubeler olup olmadığını kontrol edin.', 'UniTimetable' ),
'successAdd' => __( 'Şubeler başarıyla eklendi!', 'UniTimetable' ),
'failEdit' => __( 'Şube düzenlenemedi. Aynı özelliklere sahip başka bir şube olup olmadığını kontrol edin.', 'UniTimetable' ),
'successEdit' => __( 'Şube başarıyla düzenlendi!', 'UniTimetable' ),
));
wp_enqueue_script( 'sumojs', plugins_url('sumo/jquery.sumoselect.js?v=1576159000', __FILE__) );
wp_enqueue_style( 'sumocss', plugins_url('sumo/sumoselect.css?v=1576159000', __FILE__) );
}
//groups page
function utt_create_groups_page(){
//group form
?>
<div class="wrap">
<h2 id="groupTitle"> <?php _e("Şube Ekle","UniTimetable"); ?> </h2>
<form action="" name="groupForm" method="post" style="display: flex; flex-direction: column;">
<input type="hidden" name="groupID" id="groupID" value=0 />
<div class="element firstInRow">
<?php _e("Dönem:","UniTimetable"); ?><br/>
<select name="period" id="period" class="dirty">
<option value="0"><?php _e("- seç -","UniTimetable"); ?></option>
<?php
global $wpdb;
global $departments;
//show registered periods
$periodsTable=$wpdb->prefix."utt_periods";
$periods = $wpdb->get_results( "SELECT * FROM $periodsTable ORDER BY periodID DESC");
foreach($periods as $period)
{
echo "<option value='$period->periodID'>$period->period</option>";
}
?>
</select>
</div>
<div class="element">
<?php _e("Bölüm:","UniTimetable"); ?><br/>
<select name="semester" id="semester" class="dirty" onchange="loadSubjects(0);" data-search="true">
<option value="0"><?php _e("- seç -","UniTimetable"); ?></option>
<?php
//show semester numbers
for( $i=1 ; $i<count($departments)+1; $i++ ){
echo "<option value='$i'>$i) ".$departments[$i-1]."</option>";
}
?>
</select><br/>
</div>
<div class="element firstInRow">
<?php _e("Ders:","UniTimetable"); ?><br/>
<!-- load subjects when semester number is selected -->
<div id="subjects">
<select name="subject" id="subject" class="dirty" onchange="loadGetGroupNumber(0);">
<option value='0'><?php _e("- seç -","UniTimetable"); ?></option>
</select>
</div>
</div>
<div class="element firstInRow groupsName">
<?php _e("Şube Adı:","UniTimetable"); ?><br/>
<!-- prefix of groups' names NOT ANYMORE it's name of the new entry from now on -->
<input type="text" name="groupsName" id="groupsName" class="notdirty" value="<?php _e("Şube","UniTimetable"); ?> "/>
</div>
<?php /*
<div class="element">
<!-- select number of groups to be created -->
<?php _e("Şube Numarası:","UniTimetable"); ?><br/>
<select name="groupsNumber" id="groupsNumber" class="dirty">
<?php
for($i=1;$i<=5;$i++){
echo "<option value=$i>$i</option>";
}
?>
</select>
</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("Ekle","UniTimetable"); ?>" id="insert-updateGroup" class="button-primary"/>
<a href='#' class='button-secondary' id="clearGroupForm"><?php _e("Sıfırla","UniTimetable"); ?></a>
</div>
</form>
<!-- place to view messages -->
<div id="messages"></div>
<!-- filters to filter shown groups -->
<span id="filter1">
<?php _e("Dönem:","UniTimetable"); ?>
<select name="periodFilter" id="periodFilter" onchange="viewGroups();">
<?php
$periodsTable=$wpdb->prefix."utt_periods";
$periods = $wpdb->get_results( "SELECT * FROM $periodsTable ORDER BY periodID ASC");
//current date
$date = date("Y-m-d");
echo "<option value=''>".__("- seç -","UniTimetable")."</option>";
$it_pers = 0;
$periods = array_reverse($periods);
foreach($periods as $period)
{
$selected = "";
if($it_pers == 0)
{
$selected = "selected='selected'";
$it_pers = 1;
}
echo "<option value='$period->periodID' $selected>$period->period</option>";
}
?>
</select>
</span>
<span id="filter2">
<?php _e("Bölüm:","UniTimetable"); ?>
<select name="semesterFilter" id="semesterFilter" onchange="viewGroups();" data-search="true">
<option value="0" selected='selected'><?php _e("Hepsi","UniTimetable"); ?></option>
<?php
for($i=1;$i<count($departments)+1;$i++){
//select 1st semester
if($i == 1){
$selected = "";
}else{
$selected = "";
}
echo "<option value='$i' $selected>$i) ".$departments[$i-1]."</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;
global $departments;
/*$wpdb->suppress_errors = false;
$wpdb->show_errors = true;*/
//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 period 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;
$safeSql = $wpdb->prepare("SELECT * FROM $groupsTable, $subjectsTable WHERE $groupsTable.subjectID=$subjectsTable.subjectID ORDER BY title, type, groupName");
}
else 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);
}
//echo "{".$wpdb->print_error()."}";
$groups = $wpdb->get_results($safeSql);
?>
<!-- show table of groups -->
<table class="widefat bold-th">
<thead>
<tr>
<th><?php _e("Bölüm","UniTimetable"); ?></th>
<th><?php _e("Ders","UniTimetable"); ?></th>
<th><?php _e("Şube","UniTimetable"); ?></th>
<th><?php _e("Eylemler","UniTimetable"); ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th><?php _e("Bölüm","UniTimetable"); ?></th>
<th><?php _e("Ders","UniTimetable"); ?></th>
<th><?php _e("Şube","UniTimetable"); ?></th>
<th><?php _e("Eylemler","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/U","UniTimetable");
}else{
$type = __("PE","UniTimetable");
}
//a record
echo "<tr id='$group->groupID' $addClass>
<td>".$departments[$group->semester -1]."</td>
<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__)."'/> ".__("Sil","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__)."'/> ".__("Düzenle","UniTimetable")."</a></td></tr>";
}
?>
</tbody>
</table>
<?php
die();
}
add_action('wp_ajax_utt_get_group_number','utt_get_group_number');
function utt_get_group_number()
{
global $wpdb;
$groupsTable = $wpdb->prefix."utt_groups";
//debug
//$wpdb->suppress_errors = false;
//$wpdb->show_errors = true;
$periodID=$_GET['period_id'];
$subjectID=$_GET['subject_id'];
$prev_groups_query = $wpdb->prepare( "SELECT COUNT(*) num FROM $groupsTable WHERE periodID=%d AND subjectID=%d", $periodID, $subjectID);
$prev_groups = $wpdb->get_results($prev_groups_query);
//print_r($prev_groups);
$prev_group_num = $prev_groups[0]->num;
echo $prev_group_num;
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;
//debug
/*$wpdb->suppress_errors = false;
$wpdb->show_errors = true;*/
//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)
{
//get the number of previously-created groups with the same features
//$safeSql_chk = $wpdb->prepare("SELECT NUM(*) AS num FROM $groupsTable WHERE $groupsTable.subjectID=%d AND periodID=%d AND groupName=%s",$subjectID,$periodID,$nameUsed);
//$groups_chk = $wpdb->get_results($safeSql_chk);
//$groups_num = $groups_chk->num;
//transaction, so if an insert fails, it rolls back
$wpdb->query('START TRANSACTION');
//for($i=($groups_num+1);$i<=$groupsNumber;$i++)
//{
//name is generated by prefix(groupName) and a number, starting from counterstart
$nameUsed = $groupName;//$groupName." ".$groupsNumber;//$i
$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' onchange='loadGetGroupNumber(0);'>";
echo "<option value='0'>".__("- seç -","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 = __("Teori","UniTimetable");
}else if($subject->type == "L"){
$type = __("Lab/Uyg","UniTimetable");
}else{
$type = __("PE","UniTimetable");
}
echo "<option value='$subject->subjectID' $select>$subject->title $type</option>";
}
echo "</select>";
die();
}
?>