-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.php
1651 lines (1399 loc) · 47.3 KB
/
funcs.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// funcs.php
// Functions for use in ExCode Tool
session_start();
$coderName = $_SESSION['coder_name'];
$coderID = $_SESSION['coder_id'];
/****************************
Coder Functions
****************************/
function is_loggedin(){
global $coderID;
if($coderID==null) {
return false;
}
else {
return true;
}
}
function is_admin($coderID) {
db_connect();
$sql = "SELECT is_admin FROM coders WHERE coder_id='$coderID'";
$admin_val = mysql_fetch_array(mysql_query($sql)) or die(mysql_error());
if($admin_val['is_admin'] == 1) {
return true;
}
else {
return false;
}
}
function is_guest() {
global $coderID;
if($coderID == 22){
return true;
}
else {
return false;
}
}
function validate_login($coderName,$password) {
db_connect();
$result = mysql_query("SELECT * FROM coders WHERE coder_name='$coderName' AND coder_pass='$password'") or die(mysql_error());
$validUnPw = mysql_num_rows($result);
if($validUnPw > 0){
$coder_array = mysql_fetch_array($result);
return $coder_array;
}
else {
return false;
}
}
function get_coder_name($id){
db_connect();
$sql = "SELECT coder_name FROM coders WHERE coder_id=$id LIMIT 1";
$nameArr = mysql_fetch_array(mysql_query($sql));
return $nameArr['coder_name'];
}
function get_all_coders() {
db_connect();
$sql = "SELECT coder_id, coder_name FROM coders";
$result = mysql_query($sql);
$i = 0;
while($coder = mysql_fetch_array($result)) {
$id = $coder['coder_id'];
$all_coders[$id] = $coder['coder_name'];
$i++;
}
return $all_coders;
}
// User Stats
function get_number_items_coded($id,$timeStart=0,$timeEnd=0) {
db_connect();
if($timeStart == 0){
$sql = "SELECT * FROM codings WHERE coder_id=$id AND coded_value!=-1";
$count = mysql_num_rows(mysql_query($sql));
return $count;
}
else {
$sql = "SELECT * FROM codings WHERE coder_id=$id AND coded_value!=-1 AND timestamp > $timeStart AND timestamp < $timeEnd";
$count = mysql_num_rows(mysql_query($sql));
return $count;
}
}
function get_total_coding_time($id=0) {
if($id>0) {
$sql = "SELECT SUM(coding_time) FROM codingtimes WHERE coder_id=$id";
}
else {
$sql = "SELECT SUM(coding_time) FROM codingtimes";
}
$countArr = mysql_fetch_array(mysql_query($sql));
$totalTime = $countArr['SUM(coding_time)'];
if($totalTime > 0){
return $totalTime;
}
else {return 0;}
}
function get_range_coding_time($id=0,$start,$end) {
if($id>0) {
$sql = "SELECT SUM(coding_time) FROM codingtimes WHERE coder_id=$id AND timestamp > $start AND timestamp < $end";
}
else {
$sql = "SELECT SUM(coding_time) FROM codingtimes WHERE timestamp > $start AND timestamp < $end";
}
$countArr = mysql_fetch_array(mysql_query($sql));
$totalTime = $countArr['SUM(coding_time)'];
if($totalTime > 0){
return $totalTime;
}
else {return 0;}
}
function get_coding_rate($id=0) {
db_connect();
if($id>0) {
$sql = "SELECT coding_time FROM codingtimes WHERE coder_id=$id";
}
else {
$sql = "SELECT coding_time FROM codingtimes";
}
$count = mysql_num_rows(mysql_query($sql));
$totalTime = get_total_coding_time($id);
return ($totalTime/$count);
}
/****************************
Admin Functions
****************************/
function get_admins($non_admin=false) {
db_connect();
if($non_admin) {
$is_admin = 0;
}
else {
$is_admin = 1;
}
$sql = "SELECT * FROM coders WHERE is_admin = '$is_admin'";
$the_admins = mysql_query($sql) or die(mysql_error());
$i = 0;
while($admin = mysql_fetch_array($the_admins)){
$admin_array[$i] = $admin;
$i++;
}
return $admin_array;
}
/****************************
Coding Stats Functions
****************************/
function get_weekly_quota() {
return 1000; // manually set for now
}
function get_weekly_ranked_coders($offset=0) {
db_connect();
// get the timestampe for the start of this week
$isoWeekStartTime = strtotime(date('o-\\WW')); // {isoYear}-W{isoWeekNumber}
$isoWeekEndTime = $isoWeekStartTime + (60*60*24*7);
$rankedCoders = array();
$i = 0;
$sql = "SELECT coder_name,coder_id FROM coders";
$theCoders = mysql_query($sql);
while ($coder = mysql_fetch_array($theCoders)) {
$coderID = $coder['coder_id'];
/* print_r($coder);
echo "<br />"; */
$numItems = get_number_items_coded($coderID,$isoWeekStartTime,$isoWeekEndTime);
if($numItems > 0){
$rankedCoders[$i]['name'] = $coder["coder_name"];
$rankedCoders[$i]['numItems'] = $numItems;
$rankedCoders[$i]['id'] = $coderID;
$i++;
}
}
usort($rankedCoders, 'sort_by_num_items');
return $rankedCoders;
}
function get_ranked_coders($rangeStart=0,$rangeEnd=0) {
db_connect();
$rankedCoders = array();
$i = 0;
$sql = "SELECT coder_name,coder_id FROM coders";
$theCoders = mysql_query($sql);
while ($coder = mysql_fetch_array($theCoders)) {
$coderID = $coder['coder_id'];
/* print_r($coder);
echo "<br />"; */
$numItems = get_number_items_coded($coderID);
$rankedCoders[$i]['name'] = $coder["coder_name"];
$rankedCoders[$i]['numItems'] = $numItems;
$rankedCoders[$i]['id'] = $coderID;
$i++;
}
usort($rankedCoders, 'sort_by_num_items');
return $rankedCoders;
}
function count_total_codings($type,$project="") {
db_connect();
if($project== "") {
$sql = "SELECT id FROM codings WHERE item_type=$type";
}
else {
$project_parameters = get_project_parameters($type,$project);
$cats = "'";
$cats .= join("', '",$project_parameters['cats']);
$cats .= "'";
$pulls = join(',',$project_parameters['pulls']);
$q_or_a = $project_parameters['q_or_a'];
$items = $project_parameters['items'];
$q_or_a_id = $project_parameters['q_or_a_id'];
$sql = "SELECT DISTINCT c.id
FROM codings c
LEFT JOIN $q_or_a qa ON c.q_or_a_id = qa.$q_or_a_id
LEFT JOIN $items i ON i.item_id = c.item_id
WHERE ";
if($type>2) { $sql .= "i.item_cat IN ($cats) AND "; }
$sql .= "qa.pull_id IN ($pulls) AND
c.item_type=$type";
}
// only check coding validity until coding table can be cleaned of invalid answer precodings
if($type == 2) {
if($project== ""){
$sql.= " AND valid > 0";
}
else {
$sql.= " AND c.valid > 0";
}
}
$count = mysql_num_rows(mysql_query($sql));
return $count;
}
function count_uncoded($type=0,$project="") {
db_connect();
if($project== "") {
$sql = "SELECT id FROM codings WHERE item_type=$type AND coded_value<0";
}
else {
$project_parameters = get_project_parameters($type,$project);
$cats = "'";
$cats .= join("', '",$project_parameters['cats']);
$cats .= "'";
$pulls = join(',',$project_parameters['pulls']);
$q_or_a = $project_parameters['q_or_a'];
$items = $project_parameters['items'];
$q_or_a_id = $project_parameters['q_or_a_id'];
$sql = "SELECT c.id
FROM codings c
LEFT JOIN $q_or_a qa ON c.q_or_a_id = qa.$q_or_a_id
LEFT JOIN $items i ON i.item_id = c.item_id
WHERE ";
if($type>2) { $sql .= "i.item_cat IN ($cats) AND "; }
$sql .= "qa.pull_id IN ($pulls) AND
c.item_type=$type AND c.coded_value<0";
}
// only check coding validity until coding table can be cleaned of invalid answer precodings
if($type == 2) {
if($project== ""){
$sql.= " AND valid > 0";
}
else {
$sql.= " AND c.valid > 0";
}
}
$count = mysql_num_rows(mysql_query($sql));
return $count;
}
// Get valid, precoded questions/answers that have not been added to stage 2, 3, or 4 coding
function get_valid_unadded($stage) {
if($stage == 4) {
$table = "answers";
$id = "answer_id";
}
else {
// default to Questions (stage 2 = precoding answers for valid questions, stage 3 = coings for valid questions)
$table = "questions";
$id = "question_id";
}
$sql_qa = "SELECT $id FROM $table WHERE valid='1'";
$result_qa = mysql_query($sql_qa) or die(mysql_error());
while($the_id = mysql_fetch_array($result_qa)){
$valid[] = $the_id["$id"];
}
$sql_codings = "SELECT DISTINCT q_or_a_id FROM codings WHERE item_type = '$stage'";
$result_codings = mysql_query($sql_codings) or die(mysql_error());
while($id_codings = mysql_fetch_array($result_codings)){
if($stage == 2){
$start = strpos($id_codings['q_or_a_id'], "_");
$the_q_or_a_id = substr($id_codings['q_or_a_id'],$start+1);
}
else {
$the_q_or_a_id = $id_codings['q_or_a_id'];
}
$added[] = $the_q_or_a_id;
}
if($precode){
$added = array_unique($added);
}
$unadded = array_diff($valid,$added);
return $unadded;
}
/****************************
Coding Functions
****************************/
function is_stage_done($type,$project="") {
if(count_uncoded($type,$project) > 0){
return false;
}
else { return true; }
}
function is_valid_project($project) {
db_connect();
if($project == ""){
return false;
}
$sql = "SELECT name FROM projects WHERE id = '$project'";
$result = mysql_query($sql) or die ("MySQL error: ".mysql_error());
if(mysql_num_rows($result) > 0){
return true;
}
else {
return false;
}
}
function get_coding_items($type,$project="") {
if($type == "Q") {
$table = "questionitems";
$key_type = "q_cat";
}
elseif($type == "A") {
$table = "answeritems";
$key_type = "a_cat";
}
if($project == "" || $project == "all") {
$sql = "SELECT item_id, item_name, item_desc, item_cat, val_disp FROM $table";
}
else {
$sql = "SELECT DISTINCT i.item_id, i.item_name, i.item_desc, i.item_cat, i.val_disp FROM $table i LEFT JOIN projectkeys p ON i.item_cat = p.value WHERE p.key_type = '$key_type' AND p.proj_id = $project";
}
$result = mysql_query($sql) or die ("MySQL error: ".mysql_error());
while ($item = mysql_fetch_array($result)) {
$item['code_num'] = 1;
$items[] = $item;
}
return $items;
}
function get_project_info() {
db_connect();
$sql = "SELECT id, name, proj_desc FROM projects";
$result = mysql_query($sql) or die ("MySQL error: ".mysql_error());
while($row = mysql_fetch_array($result)){
$projects[] = $row;
}
return $projects;
}
function get_project_parameters($type,$project) {
if($type == '1' || $type == '3'){
$qa_cat = "q_cat";
$q_or_a = "questions";
$q_or_a_id = "question_id";
$items = "questionitems";
}
else {
$qa_cat = "a_cat";
$q_or_a = "answers";
$q_or_a_id = "answer_id";
$items = "answeritems";
}
$pull_sql = "SELECT value FROM projectkeys WHERE proj_id = '$project' AND key_type = 'pull_id'";
$pull_result = mysql_query($pull_sql) or die ("MySQL error: ".mysql_error());
while($pull_row = mysql_fetch_array($pull_result)){
$project_pulls[] = $pull_row['value'];
}
$cat_sql = "SELECT value FROM projectkeys WHERE proj_id = '$project' AND key_type = '$qa_cat'";
$cat_result = mysql_query($cat_sql) or die ("MySQL error: ".mysql_error());
while($cat_row = mysql_fetch_array($cat_result)){
$project_cats[] = $cat_row['value'];
}
$parameters['pulls'] = $project_pulls;
$parameters['cats'] = $project_cats;
$parameters['q_or_a'] = $q_or_a;
$parameters['items'] = $items;
$parameters['q_or_a_id'] = $q_or_a_id;
return $parameters;
}
function cluster_in_project($project,$cluster,$q_or_a) {
$sql = sprintf("SELECT id FROM projectkeys WHERE key_type = '%s_cat' AND value='%s' AND proj_id='%s'",$q_or_a,$cluster,$project);
$result = mysql_query($sql) or die ("MySQL error: ".mysql_error());
if(mysql_num_rows($result) > 0){
return true;
}
else {
return false;
}
}
function get_cluster_items($cluster,$q_or_a) {
if($q_or_a == "q"){
$items_table = "questionitems";
}
else {
$items_table = "answeritems";
}
$sql = "SELECT item_id, item_name, item_desc, val_disp FROM $items_table WHERE item_cat = '$cluster'";
$result = mysql_query($sql) or die ("MySQL error: ".mysql_error());
while($row = mysql_fetch_array($result)){
$cluster_items[] = $row;
}
return $cluster_items;
}
function get_category_info($q_or_a, $precode=false) {
if($q_or_a == "q"){
$cat_table = "questioncategories";
}
else {
$cat_table = "answercategories";
}
if($precode) {
$not_precoding = "";
}
else {
$not_precoding = "WHERE cat_name NOT LIKE '%PRECOD%'";
}
$sql = "SELECT cat_id, cat_name, cat_desc FROM $cat_table $not_precoding";
$result = mysql_query($sql) or die ("MySQL error: ".mysql_error());
while($row = mysql_fetch_array($result)){
$categories[] = $row;
}
return $categories;
}
function get_coding_category($type,$codingId) {
# select from the proper category for the type
if($type == "1" || $type == "3") {
$itemTable = "questionitems";
$catTable = "questioncategories";
if($type == "1") {
$codingId = "-1"; // this is an override so that we use the special "check/none of the above" c2 coding cat for Question Precoding
}
}
elseif($type == "2" || $type == "4") {
$itemTable = "answeritems";
$catTable = "answercategories";
if($type == "2") {
$codingId = "-1"; // // this is an override so that we use the special "check/none of the above" c2 coding cat for Answer Precoding
}
}
$catId = mysql_fetch_array(mysql_query("SELECT item_cat FROM $itemTable WHERE item_id = '$codingId'" ));
$theCatId = $catId ['item_cat'];
$catInfo = mysql_fetch_array(mysql_query("SELECT * FROM $catTable WHERE cat_id='$theCatId'"));
return $catInfo;
}
function get_new_coding($type,$coderID,$project="") {
db_connect();
$limit = count_uncoded($type);
$tries = 0;
do{
// grab a random uncoded item
if($project == "") {
$sql = "SELECT id,item_id,q_or_a_id,item_type FROM codings WHERE coded_value=-1 AND item_type=$type order by RAND() limit 1";
}
else {
$project_parameters = get_project_parameters($type,$project);
$cats = "'";
$cats .= join("', '",$project_parameters['cats']);
$cats .= "'";
$pulls = join(',',$project_parameters['pulls']);
$q_or_a = $project_parameters['q_or_a'];
$items = $project_parameters['items'];
$q_or_a_id = $project_parameters['q_or_a_id'];
$sql = "SELECT c.id,c.item_id,c.q_or_a_id,c.item_type
FROM codings c
LEFT JOIN $q_or_a qa ON c.q_or_a_id = qa.$q_or_a_id
LEFT JOIN $items i ON i.item_id = c.item_id
WHERE ";
if($type>2) { $sql .= "i.item_cat IN ($cats) AND "; }
$sql .= "qa.pull_id IN ($pulls) AND
c.coded_value=-1 AND c.item_type=$type order by RAND() limit 1";
}
$codingItem = mysql_fetch_array(mysql_query($sql)) or die ("MySQL error in get_new_coding: ".mysql_error());
$thisQorAid = $codingItem['q_or_a_id'];
$thisItemId = $codingItem['item_id'];
$thisItemType = $codingItem['item_type'];
// test to make sure it hasn't been coded by this user yet
$dupTestSql = "SELECT id FROM codings WHERE coded_value>-1 AND q_or_a_id='$thisQorAid' AND item_type=$thisItemType AND item_id=$thisItemId AND coder_id=$coderID";
$tries++;
if($tries > $limit){
break;
}
} while(mysql_num_rows(mysql_query($dupTestSql)) > 0);
if($tries <= $limit){
return $codingItem;
}
else {
// this means that this user has coded all the items they can in this stage
return false;
}
}
function get_cat_coding_form($type,$catId,$QorAId) {
$now = time();
$form_str = "<input type=\"hidden\" name=\"timestamp\" value=\"$now\" />\n";
if($type == "1" || $type == "3") {
$catTable = "questioncategories";
$itemsTable = "questionitems";
$valuesTable = "qitemvalues";
}
elseif($type == "2" || $type == "4") {
$catTable = "answercategories";
$itemsTable = "answeritems";
$valuesTable = "aitemvalues";
}
# for some categories (clusters) a description will be relivant
$cluster_desc_sql = "SELECT cat_desc FROM $catTable WHERE cat_id = '$catId'";
$cluster_desc_result = mysql_query($cluster_desc_sql) or die ("MySQL error: ".mysql_error());
$cluster_desc = mysql_fetch_array($cluster_desc_result);
if($cluster_desc['cat_desc'] != "") {
echo "<span class='sm_desc'>";
echo $cluster_desc['cat_desc'];
echo "</span><br /><br />";
}
# get all the info for each item in the category
$item_info_sql = "SELECT item_id, item_name, item_desc, val_disp FROM $itemsTable WHERE item_cat='$catId'";
$item_result = mysql_query($item_info_sql) or die ("MySQL error: ".mysql_error());
$num_items = mysql_num_rows($item_result);
$form_str .= "<input type=\"hidden\" name=\"num_items\" value=\"$num_items\" />\n";
$item_num = 0;
$nota_flag = false;
while($item = mysql_fetch_array($item_result)) {
#for each item in the category grab a coding
# there should be a valid coding for every item in the category based on earlier validations
$this_item_id = $item['item_id'];
$this_item_disp = $item['val_disp'];
$this_item_name = $item['item_name'];
$this_item_desc = $item['item_desc'];
if($this_item_disp == "single_check") {
# a category with items containing a single value, marked with a checkbox. A "none of the above" checkbox will be added automatically
$nota_flag = true;
$form_str .= "<input type=\"hidden\" name=\"single_check_flag\" value=\"true\" /> ";
$form_str .= "<input type=\"hidden\" name=\"id_$item_num\" value=\"$this_item_id\" /> ";
$form_str .= "<input type=\"checkbox\" name=\"item_$item_num\" value=\"$this_item_id\" /> ";
}
$form_str .= "<span class='code_lbl'>$this_item_name</span><br />";
$form_str .= "<span class='sm_desc'>$this_item_desc</span><br /><br />";
# get id for codeditem to store value for this coding item
$coding_sql = "SELECT id FROM codings WHERE q_or_a_id='$QorAId' AND item_type='$type' AND item_id='$this_item_id' AND coded_value < 0";
$coding_result = mysql_query($coding_sql) or die ("MySQL error: ".mysql_error());
$codingitem = mysql_fetch_array($coding_result);
$this_coding_id = $codingitem['id'];
# get item values for this coding item
$value_sql = "SELECT item_val, val_desc FROM $valuesTable WHERE item_id='$this_item_id'";
$value_result = mysql_query($value_sql) or die ("MySQL error: ".mysql_error());
// if there is a "None of the Above" option, set a hidden flag
if($this_item_disp == "check_nota") {
$form_str .= "<input type=\"hidden\" name=\"nota_flag\" value=\"true\" /> ";
$form_str .= "<input type=\"hidden\" name=\"nota_item_id\" value=\"$this_item_id\" /> ";
$form_str .= "<input type=\"hidden\" name=\"nota_item_table\" value=\"$valuesTable\" /> ";
}
if($this_item_disp == "rad") {
# radio button display of values
while($value = mysql_fetch_array($value_result)){
//print_r($value);
$the_val = $value['item_val'];
$form_str .= "<input type=\"radio\" name=\"item_$item_num\" value=\"$the_val\" /> ";
$form_str .= $value['val_desc']."<br>\n";
}
}
elseif($this_item_disp == "check_nota") {
# check box display values. This is used to map a single item's values to existing items behind the scenes
while($value = mysql_fetch_array($value_result)){
$the_val = $value['item_val'];
$form_str .= "<input type=\"checkbox\" name=\"item_".$item_num."[]\" value=\"".$value['item_val']."\" /> ";
$form_str .= $value['val_desc']."<br>\n";
if($the_val > 0) {
$coding_sql = "SELECT id FROM codings WHERE q_or_a_id='$QorAId' AND item_type='$type' AND item_id='$the_val' AND coded_value < 0";
$coding_result = mysql_query($coding_sql) or die ("MySQL error: ".mysql_error());
$codingitem = mysql_fetch_array($coding_result);
$this_coding_id = $codingitem['id'];
$form_str .= "<input type=\"hidden\" name=\"id_$the_val\" value=\"$this_coding_id\" /><br />\n";
}
}
}
# add flag options
$form_str .= "<span class='sm_text'><a href='#' class='flag_toggle_$item_num'>flag this coding</a></span>\n";
$form_str .= "<script type='text/javascript'>";
$form_str .= '$(document).ready(function(){$("div.flag_box_'.$item_num.'").hide(); $("a.flag_toggle_'.$item_num.'").click(function(){$("div.flag_box_'.$item_num.'").slideToggle("slow"); return false;});});';
$form_str .= "</script>";
$form_str .= "<div class='flag_box flag_box_$item_num'>\n";
$form_str .= "<span class='sm_text'>(check at least one box)</span><br />";
$form_str .= "<input type='checkbox' name='flag_discuss_$item_num' /> discuss<br />\n";
$form_str .= "<input type='checkbox' name='flag_unsure_$item_num' /> unsure<br />\n";
$form_str .= "<textarea name='flag_comment_$item_num' class='flag_comment' rows='2' cols='30'></textarea>\n";
$form_str .= "</div><br />\n";
if($this_item_disp != "check_nota") {
$form_str .= "<input type=\"hidden\" name=\"id_$item_num\" value=\"$this_coding_id\" /><br />\n";
}
$item_num++;
}
if($nota_flag) {
$form_str .= "<input type=\"checkbox\" name=\"item_nota\" value=\"nota\" /> ";
$form_str .= "<span class='code_lbl'>None of the Above</span><br />";
}
return $form_str;
}
/****************************
Data Functions
****************************/
function get_coded_data($stage=0,$project="") {
db_connect();
$all_coders = get_all_coders();
if($stage > 0) {
switch($stage) {
case 1:
if($project == "all" || $project == "") {
$sql = "SELECT question_id, subject, content, category FROM questions";
}
else {
$project_parameters = get_project_parameters($stage,$project);
$pulls = join(',',$project_parameters['pulls']);
$sql = "SELECT question_id, subject, content, category FROM questions WHERE pull_id IN ($pulls)";
}
$result = mysql_query($sql) or die(mysql_error());
$i = 0;
while($question = mysql_fetch_array($result)) {
$qID = $question['question_id'];
$coded_questions[$i]['questionID'] = $qID;
$coded_questions[$i]['q_subject'] = $question['subject'];
$coded_questions[$i]['q_content'] = $question['content'];
$coded_questions[$i]['category'] = $question['category'];
$q_sql = "SELECT item_id, coded_value, coder_id FROM codings WHERE q_or_a_id ='$qID' AND item_id < 5";
$q_result = mysql_query($q_sql) or die(mysql_error());
$j_1 = 0;
$nw_1 = 0;
$n_1 = 0;
$hw_1 = 0;
while($coding = mysql_fetch_array($q_result)) {
switch($coding['item_id']) {
case 1:
if($j_1 == 0) {
$coded_questions[$i]['Joke1'] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]['Joke1_Coder'] = $all_coders[$this_coderID];
$j_1++;
}
else {
$coded_questions[$i]['Joke2'] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]['Joke2_Coder'] = $all_coders[$this_coderID];
}
break;
case 2:
if($n_1 == 0) {
$coded_questions[$i]['Nonsense1'] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]['Nonsense1_Coder'] = $all_coders[$this_coderID];
$n_1++;
}
else {
$coded_questions[$i]['Nonsense2'] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]['Nonsense2_Coder'] = $all_coders[$this_coderID];
}
break;
case 3:
if($hw_1 == 0) {
$coded_questions[$i]['Homework1'] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]['Homework1_Coder'] = $all_coders[$this_coderID];
$hw_1++;
}
else {
$coded_questions[$i]['Homework2'] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]['Homework2_Coder'] = $all_coders[$this_coderID];
}
break;
case 4:
if($nw_1 == 0) {
$coded_questions[$i]['NotWhy1'] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]['NotWhy1_Coder'] = $all_coders[$this_coderID];
$nw_1++;
}
else {
$coded_questions[$i]['NotWhy2'] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]['NotWhy2_Coder'] = $all_coders[$this_coderID];
}
break;
}
}
$i++;
}
return $coded_questions;
break;
case 2:
/*
// Written to deal with lots of codings, not needed yet but save for later use
$count_sql = "SELECT count(*) FROM answers";
$count_result = mysql_query($count_sql) or die(mysql_error());
$count = mysql_fetch_array($count_result);
$row_count = $count[0];
$limit = 1000;
$offset = 0;
while($offset < $row_count) {
$sql = "SELECT answer_id, question_id, content FROM answers LIMIT $limit OFFSET $offset";
$offset += $limit;
}
*/
if($project == "all" || $project == "") {
$sql = "SELECT DISTINCT a.answer_id, a.question_id, a.content FROM answers a LEFT JOIN questions q ON q.question_id = a.question_id WHERE q.valid = '1'";
}
else {
$project_parameters = get_project_parameters($stage,$project);
$pulls = join(',',$project_parameters['pulls']);
$sql = "SELECT DISTINCT a.answer_id, a.question_id, a.content FROM answers a LEFT JOIN questions q ON q.question_id = a.question_id WHERE q.valid = '1' AND a.pull_id IN ($pulls)";
}
$result = mysql_query($sql) or die(mysql_error());
$i = 0;
while($answer = mysql_fetch_array($result)) {
$aID = $answer['answer_id'];
$qID = $answer['question_id'];
$coded_answers[$i]['answerID'] = $aID;
$coded_answers[$i]['questionID'] = $qID;
$coded_answers[$i]['a_content'] = $answer['content'];
$q_sql = "SELECT subject, content, category FROM questions WHERE question_id='$qID'";
$question = mysql_fetch_array(mysql_query($q_sql));
$coded_answers[$i]['q_subject'] = $question['subject'];
$coded_answers[$i]['q_content'] = $question['content'];
$coded_answers[$i]['category'] = $question['category'];
$a_sql = "SELECT item_id, coded_value, coder_id FROM codings WHERE q_or_a_id ='$aID' AND item_id < 4";
$a_result = mysql_query($a_sql) or die(mysql_error());
$j_1 = 0;
$n_1 = 0;
$r_1 = 0;
while($coding = mysql_fetch_array($a_result)) {
switch($coding['item_id']) {
case 1:
if($j_1 == 0) {
$coded_answers[$i]['Joke1'] = $coding['coded_value'];
$coderID = $coding['coder_id'];
$coded_answers[$i]['Joke1_coder'] = $all_coders[$coderID];
$j_1++;
}
else {
$coded_answers[$i]['Joke2'] = $coding['coded_value'];
$coderID = $coding['coder_id'];
$coded_answers[$i]['Joke2_coder'] = $all_coders[$coderID];
}
break;
case 2:
if($n_1 == 0) {
$coded_answers[$i]['Nonsense1'] = $coding['coded_value'];
$coderID = $coding['coder_id'];
$coded_answers[$i]['Nonsense1_coder'] = $all_coders[$coderID];
$n_1++;
}
else {
$coded_answers[$i]['Nonsense2'] = $coding['coded_value'];
$coderID = $coding['coder_id'];
$coded_answers[$i]['Nonsense2_coder'] = $all_coders[$coderID];
}
break;
case 3:
if($r_1 == 0) {
$coded_answers[$i]['Redirect1'] = $coding['coded_value'];
$coderID = $coding['coder_id'];
$coded_answers[$i]['Redirect1_coder'] = $all_coders[$coderID];
$r_1++;
}
else {
$coded_answers[$i]['Redirect2'] = $coding['coded_value'];
$coderID = $coding['coder_id'];
$coded_answers[$i]['Redirect2_coder'] = $all_coders[$coderID];
}
break;
}
}
$i++;
}
return $coded_answers;
break;
case 3:
$question_items = get_coding_items("Q",$project);
//print_r($question_items);
if($project == "all" || $project == "") {
$sql = "SELECT question_id, subject, content, category FROM questions WHERE valid = '1'";
}
else {
$project_parameters = get_project_parameters($stage,$project);
$pulls = join(',',$project_parameters['pulls']);
$sql = "SELECT question_id, subject, content, category FROM questions WHERE valid = '1' AND pull_id IN ($pulls)";
}
$result = mysql_query($sql) or die(mysql_error());
$i = 0;
while($question = mysql_fetch_array($result)) {
$qID = $question['question_id'];
$coded_questions[$i]['questionID'] = $qID;
$coded_questions[$i]['q_subject'] = $question['subject'];
$coded_questions[$i]['q_content'] = $question['content'];
$coded_questions[$i]['category'] = $question['category'];
$min_item_id = 5; // stage 3 items have item_id greater than 4
$q_sql = "SELECT item_id, coded_value, coder_id FROM codings WHERE q_or_a_id ='$qID' AND item_id >= $min_item_id";
$q_result = mysql_query($q_sql) or die(mysql_error());
while($coding = mysql_fetch_array($q_result)) {
$the_item_key = 0;
foreach($question_items as $key => $item) {
if($item['item_id'] == $coding['item_id']) {
$the_item_key = $key;
break;
}
}
$item_index = $question_items[$the_item_key]['item_name'].$question_items[$the_item_key]['code_num'];
$coder_index = $item_index."_Coder";
$coded_questions[$i]["$item_index"] = $coding['coded_value'];
$this_coderID = $coding['coder_id'];
$coded_questions[$i]["$coder_index"] = $all_coders[$this_coderID];
$question_items[$the_item_key]['code_num'] = $question_items[$the_item_key]['code_num'] + 1;
}
$x = 0;
$q_len = count($question_items);
while($x < $q_len) {
$question_items[$x]['code_num'] = 1;
$x++;
}
$i++;
}
//print_r($coded_questions);
return $coded_questions;
break;
case 4:
$answer_items = get_coding_items("A",$project);
if($project == "all" || $project == "") {
$sql = "SELECT answer_id, question_id, content FROM answers WHERE valid = '1'";
}
else {
$project_parameters = get_project_parameters($stage,$project);
$pulls = join(',',$project_parameters['pulls']);
$sql = "SELECT answer_id, question_id, content FROM answers WHERE valid = '1' AND pull_id IN ($pulls)";
}
$result = mysql_query($sql) or die(mysql_error());
$i = 0;
while($answer = mysql_fetch_array($result)) {
$aID = $answer['answer_id'];
$qID = $answer['question_id'];
$coded_answers[$i]['answerID'] = $aID;
$coded_answers[$i]['questionID'] = $qID;
$coded_answers[$i]['a_content'] = $answer['content'];
$q_sql = "SELECT subject, content, category FROM questions WHERE question_id='$qID'";
$question = mysql_fetch_array(mysql_query($q_sql));
$coded_answers[$i]['q_subject'] = $question['subject'];
$coded_answers[$i]['q_content'] = $question['content'];
$coded_answers[$i]['category'] = $question['category'];