-
Notifications
You must be signed in to change notification settings - Fork 0
/
yesno.module
1171 lines (1060 loc) · 46.6 KB
/
yesno.module
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
/**
*Yes no module - An application allowing filtering of images based on yes no responses to questions
**/
//Some useful constants
define ("YESNO_DEFAULT_LIST_INTRO" , "<div class=\"yesno-list-info\">Shown below is a list of \"Yes/No\" projects that have been configured on this site.</div>
<div class=\"yesno-list-info\">Each project will step you through a series of images requesting you to provide a yes or no answer to a question based on what you can see in the image.</div>
<div class=\"yesno-list-info\">The title of each project is the question you will be asked to answer. Select a project that you fancy from the list below to start particpating.</div>");
define ("YESNO_DEFAULT_LIST_TITLE","Available yes/no projects");
/***************************** HOOKS ***********************************/
/**
* Implements hook_init()
* @return array
**/
function yesno_init()
{
}
/**
* Implements hook_node_info()
* @return array
**/
function yesno_node_info()
{
return array
(
"yesno" => array
(
"name" => t("Yes no question"),
"base" => "yesno",
"description" => "Use this content type to create a new yes/no question for the Yes No App",
"has_title" => FALSE,
"help" => "Enter the text of the question you wish to ask and the url from which to obtain source images"
)
);
}
/**
* Implements hook_form()
*/
function yesno_form($node,$formstate)
{
return node_content_form($node,$formstate);
}
/**
* Implements hook_form_alter - adds the values from the data bases array
*/
function yesno_form_alter(&$form,$formstate,$formid)
{
global $databases;
if($formid == "yesno_node_form")
{
$options = array(0=>"None");
foreach($databases as $connectionkey=>$dbkeys)
{
if(strtoupper($connectionkey) != "DEFAULT" )
{
foreach($dbkeys as $target=>$db)
{
if(strtoupper($target) == "DEFAULT")
{
$option = "$connectionkey";
//$options[$option] = $db["database"] . " on " . $db["host"];
$options[$option] = $option;
}
}
}
}
if(count($options) == 1)
{
$options[0] = "There are no external databases available in the settings.php file!";
$form["yesno_external_db"]["und"]["#default_value"] = array(0 => "None");
}
$form["yesno_external_db"]["und"]["#options"] = $options;
if($form["nid"])
{
$node = node_load($form["nid"]["#value"]);
if($node)
{
$a = field_get_items("node",$node,"yesno_external_db");
$form["yesno_external_db"]["und"]["#default_value"] = array($a[0]["value"]=>$a[0]["value"]);
}
}
else
{
$form["yesno_external_db"]["und"]["#default_value"] = array(0 => "None");
}
}
}
/**
* Implements hook_js_alter()
* Ensures a sufficiently up to date version of jQuery is use for pages mapped to this module
**/
function yesno_js_alter(&$javascript)
{
if(preg_match("/^yesno/",current_path()))
{
// Swap out jQuery to use an updated version of the library.
$javascript['misc/jquery.js']['data'] = drupal_get_path('module', 'yesno') . '/jquery-min.js';
if(@isset($javascript['misc/ui/jquery.ui.core.min.js']))
$javascript['misc/ui/jquery.ui.core.min.js']['data'] = drupal_get_path('module', 'yesno') . '/jquery-ui-browser-min.js';
else
{
//Add in the jquery ui
$javascript['misc/ui/jquery.ui.core.min.js'] = drupal_js_defaults();
$javascript['misc/ui/jquery.ui.core.min.js']['data'] = drupal_get_path('module', 'yesno') . '/jquery-ui-browser-min.js';
}
}
}
/**
* Implements hook_menu()
* @return array
**/
function yesno_menu()
{
$listtitle = variable_get('yesno_projectpage_title', '') ? variable_get('yesno_projectpage_title', '') : YESNO_DEFAULT_LIST_TITLE;
$items = array
(
'yesnolist'=> array
(
//The title can be set in the module configuration if it is not set fall back on the default
"title" => $listtitle ,
"page callback" => "_page_yesno_list",
"access callback" => TRUE
),
'yesnopage'=> array
(
"title" =>"A page for presenting a specific yes no question",
"page callback" => "_page_yesno",
"access callback" => TRUE,
"type" => MENU_CALLBACK,
),
'yesnonextimage'=> array
(
"title" => "A page for returning the next image to view",
"page callback" => "_page_yesno_nextimage",
"access callback" => TRUE,
"type" => MENU_CALLBACK,
),
'yesnosaveanswer'=> array
(
"title" => "An Ajax end point for saving the answer to a question",
"page callback" => "_page_yesno_saveanswer",
"access callback" => TRUE,
"type" => MENU_CALLBACK,
),
'yesnoacceptanswer'=> array
(
"title" => "An Ajax end point for saving the accepted answer to a question",
"page callback" => "_page_yesno_acceptanswer",
"access callback" => "_yesno_access_control",
"access arguments" => array(true,array("yesno_administrator")),
"type" => MENU_CALLBACK,
),
'yesnorejectanswer'=> array
(
"title" => "An Ajax end point for rejecting the answer to a question",
"page callback" => "_page_yesno_rejectanswer",
"access callback" => "_yesno_access_control",
"access arguments" => array(true,array("yesno_administrator")),
"type" => MENU_CALLBACK,
),
'yesnozoomify'=> array
(
"title" => "An Ajax endpoint for returning an on the fly generated tile",
"page callback" => "_page_yesno_zoomify",
"access callback" => TRUE,
"type" => MENU_CALLBACK,
),
'yesnoclearzoomcache'=>array
(
"title" => "An Ajax endpoint for clearing up on the fly generated tiles",
"page callback" => "_page_yesno_clearzoomcache",
"access callback" => TRUE,
"type" => MENU_CALLBACK,
),
'yesnostats'=> array
(
"title" => "An Ajax endpoint for returning user stats for a specific yes no question",
"page callback" => "_page_yesno_stats",
"access callback" => TRUE,
"type" => MENU_CALLBACK,
),
'yesnomoderate'=> array
(
"title" => "Review and accept or reject responses",
"page callback" => "_page_yesno_moderate",
"access callback" => "_yesno_access_control",
"access arguments" => array(true,array("yesno_administrator")),
"type" => MENU_CALLBACK,
),
'yesnodownload'=> array
(
"title" => "Download accepted reseponses for a node responses",
"page callback" => "_page_yesno_download",
"access callback" => "_yesno_access_control",
"acess arguments" => array(true,array("yesno_administrator")),
"type" => MENU_CALLBACK,
),
'admin/config/user-interface/yesnoconfig' =>array
(
"title" => "Yes no front page configuration",
"description" => "Allows the specification of the text to appear in the Project list presented by the YesNo module",
"page callback" => "drupal_get_form",
"page arguments" => array("_yesno_module_config"),
'access arguments' => array('administer site configuration'),
)
);
return $items;
}
/**Implements hook_node_presave
*
**/
function yesno_node_presave($node)
{
$node->title = $node->yesno_question["und"][0]["value"];
//Make sure the mode is never promoted
$node->promoted = 0;
}
/**
* Implements hook_node_update and makes sure any newly uploaded manifest file is processed into the manifest table
**/
function yesno_node_update($node)
{
_yesno_check_manifest($node);
}
/**
*implements Hook_node_insert and makes sure any newly uploaded manifest file is processed into the manifest table
**/
function yesno_node_insert($node)
{
_yesno_check_manifest($node);
}
/**
* Implements hook_validate and checks that a manifest for a new node has been specified.
* For an aleady saved node the manifest will only be specified if it is being changed.
**/
function yesno_validate($node,$form,$form_state)
{
if(!$node->nid)
{
$m = field_get_items("node",$node,"yesno_manifest_file");
if(!isset($m[0]["fid"]))
{
form_set_error("yesno_manifest_file","You must specify the manifest file for a new Yes/No question");
}
}
$t = _yesno_getThreshold($node);
$a = _yesno_getAutoacceptThreshold($node);
if($a > $t)
form_set_error("yesno_autoaccept_threshold","You must specify an autoaccept threshold less than or equal to the responss threshold.");
//Check to see if the autoupdate field is checked - if so make sure the necesary fields are set and run a connection test
$aa = _yesno_getAutoupdate($node);
if($aa)
{
list($dbname,$dbtable,$dbidfield,$dbfield,$dbyes) = _yesno_getAutoupdateOptions($node);
$err = 0;
{
if(!$dbname)
{
form_set_error("yesno_external_db","Because you have switched on the auto database update facility you must specify the name of the external data base to connect to");
$err = 1;
}
if(!$dbtable)
{
form_set_error("yesno_external_db_table","Because you have switched on the auto database update facility you must specify the name of the table in the external database that should be updated");
$err = 1;
}
if(!$dbidfield)
{
form_set_error("yesno_external_db_idfield","Because you have switched on the auto database update facility you must specify the name of the field in the selected table of the external database that should used to select the records to be updated");
$err = 1;
}
if(!$dbfield)
{
form_set_error("yesno_external_db_field","Because you have switched on the auto database update facility you must specify the name of the field in the selected table of the external database that should be updated");
$err = 1;
}
if(!$dbyes)
{
form_set_error("yesno_external_db_yes_value","Because you have switched on the auto database update facility you must specify the value to be set in the selected database field when a 'yes' response is obtained");
$err = 1;
}
}
if(!$err)
{
//No error so test out the connection
try
{
db_set_active($dbname);
$query = db_select($dbtable, "t")->fields("t",array($dbfield))->range(0,1)->condition("t.$dbidfield","%","like");
$query->execute();
db_set_active();
}
catch(Exception $e)
{
db_set_active();
form_set_error("yesno_external_db","Error connecting to and querying the specified external data base! Reason: {$e->getMessage()}");
}
}
}
}
/***************************** PAGE CALLBACKS ***********************************/
/**
* Page callback mapped to the url /yesnolist
* Creates a list of pages each page is set up to handle a different yes no question
* Following a link in the list will take you to the yes no page to start scoring
* @return array
*/
function _page_yesno_list()
{
global $user;
drupal_add_css(drupal_get_path("module","yesno") . '/yesno.css');
//Get all the yes no nodes
$sql = "select nid from {node} n where n.type = :type";
$result = db_query($sql,array(":type"=>"yesno"));
$qs = array();
$listcount = 0; //Counter for the number of questions presented to the user
foreach($result as $row)
{
$node = node_load($row->nid);
if(!$node)
continue;
//Get the threshold value for the current node
$t = _yesno_getThreshold($node);
if(! _yesno_getAllowAnonymous($node) and !user_is_logged_in())
{
//This question does not allow anonymous interaction and the user us not logged in so skip it
continue;
}
$listcount ++;
//Count the number of candidates
$query = db_select("yesno_manifest_entries","m")
->fields("m",array("url","objectid"))
->condition(db_or()->condition("s.responsecount",$t,"<")->isNull("s.responsecount"))
->condition("m.nodeid",$row->nid);
$query->leftJoin("yesno_response_summaries","s","m.objectidhash = s.objectidhash and m.nodeid = s.nodeid");
$numcandidates = $query->countQuery()->execute()->fetchField();
//Count the number of accepted responses
$numaccepted = db_select("yesno_response_summaries","s")->fields("s",array("accepted"))->condition("nodeid",$row->nid)->isNotNull("accepted")->countQuery()->execute()->fetchField();
//Get the question text
$q = _yesno_getQuestion($node);
if($numcandidates)
{
if(in_array("yesno_administrator",$user->roles))
{
$entry = "<li><a href=\"yesnopage/$row->nid\">$q</a> (<a href=\"node/$row->nid/edit\">re-configure</a>";
if($t > 1)
$entry .= ", <a href=\"yesnomoderate/$row->nid\">moderate results</a>";
$entry .= ")";
if($numaccepted)
$entry .= "<br/><a href=\"yesnodownload/$row->nid\">Download $numaccepted accepted results</a>";
$entry .= "</li>";
$qs[] = $entry;
}
else
{
$qs[] = "<li><a href=\"yesnopage/$row->nid\">$q</a></li>";
}
}
else
{
$qs[] = "<li>{$q[0]["value"]} - <span class=\"yesno-complete\">Task complete!</span></li>";
}
}
if($listcount)
{
$intro = variable_get('yesno_projectpage_intro', '') ? variable_get('yesno_projectpage_intro', '') : YESNO_DEFAULT_LIST_INTRO;
$markup = $intro . "<div id=\"yesno-interfacecontainer\"><ul>" . implode("",$qs) . "</ul></div>";
}
else
{
$markup = "<div id=\"yesno-interfacecontainer\">There are no projects available to you on this site.";
if(!user_is_logged_in())
$markup .= "<br/>You may find that if you log in you will be able to access some projects";
else
$markup .= "<br/>No projects have yet been configured.";
$markup .= "</div>";
}
return array("#markup"=>$markup);
}
/**
* Page callback mapped to the url /yesnolist
* Creates a list of pages each page is set up to handle a different yes no question
* @return array
*/
function _page_yesno($nodeid)
{
global $user;
//This will also add some info to the page identifying the question to be answered
//All the question handling will be done as a jquery widget
//Get the field data for the node
$node = node_load($nodeid);
if(!$node)
{
drupal_not_found();
return;
}
$q = field_get_items("node",$node,"yesno_question");
$h = field_get_items("node",$node,"body");
$h = field_view_value("node",$node,"body",$h[0]);
$m = field_get_items("node",$node,"yesno_viewmode");
$viewmode = $m[0]["value"];
//$m = field_view_value("node",$node,"yesno_viewmode",$m[0]);
$a = field_get_items("node",$node,"yesno_aspectratio");
//$a = field_view_value("node",$node,"yesno_aspectratio",$a[0]);
drupal_add_css(drupal_get_path("module","yesno") . '/yesno.css');
drupal_add_css(drupal_get_path("module","yesno") . '/jquery/jquery-ui.css');
//Add in the widget code
//drupal_add_js(drupal_get_path("module","yesno") . '/yesno.js');
//If we are in zoomify mode add in the zoomify java script
//if($viewmode > 0)
// drupal_add_js('/zoomify/RBGEZoomifyImageViewer.js');
$title = $q[0]["value"];
if(!preg_match("/\?$/",$title))
$title .= "?";
//Show the title
drupal_set_title($title);
$loggedin = user_is_logged_in() ? 1 : 0;
//Add in the onready handler to make sure the widget is activated
$onready = "jQuery(document).ready(function(){
var c = jQuery('#yesno-interfacecontainer');
c.yesnoform({
title:'',
drupalnode: $nodeid,
loggedin: $loggedin,
viewmode: $viewmode,
aspectratio: {$a[0]["value"]},
})
})";
drupal_add_js($onready,"inline");
//return array("#markup"=>"<div id=\"yesno-interfacecontainer\">{$q[0]["value"]}</div><div>{$h[0]["value"]}</div>");
//Add some stats to the page for the current user
$markup = "<div id=\"yesno-interfacecontainer\">
{$h["#markup"]}
</div>";
return array(
"#markup"=>$markup,
"#attached" => array(
"js"=>array(
drupal_get_path("module","yesno") . '/yesno.js',
drupal_get_path("module","yesno") . '/zoomify/RBGEZoomifyImageViewer.js', //This is not portable - probably needs to be specified in the node config
),
"library"=>array(
array('system', 'ui.widget'),
)
)
);
}
/**
* Page callback mapped to the url /yesnomoderate
* List all non accepted responses in a response set for a node that have reached their response threshold
* The interface pages the results
* For each result the break down of yes no and don't know responses is shown
* The page is only made available when the current node has a response threshold > 1
*/
function _page_yesno_moderate($nodeid)
{
drupal_add_css(drupal_get_path("module","yesno") . '/yesno.css');
drupal_add_css(drupal_get_path("module","yesno") . '/yesnomoderate.css');
drupal_add_css(drupal_get_path("module","yesno") . '/jquery/jquery-ui.css');
//Add in the widget code
drupal_add_js(drupal_get_path("module","yesno") . '/yesno.js'); //- this is required for the review function
drupal_add_js(drupal_get_path("module","yesno") . '/yesnomoderate.js');
$node = node_load($nodeid);
if(!$node)
{
drupal_not_found();
return;
}
$q = field_get_items("node",$node,"yesno_question");
$m = field_get_items("node",$node,"yesno_viewmode");
$a = field_get_items("node",$node,"yesno_aspectratio");
$t = _yesno_getThreshold($node);
dpm($t);
$viewmode = $m[0]["value"];
//If we are in zoomify mode add in the zoomify java script
if($viewmode > 0)
drupal_add_js('/zoomify/RBGEZoomifyImageViewer.js');
drupal_set_title(drupal_get_title() . " for question \"" . $q[0]["value"] . "\"");
$onready = "jQuery(document).ready(function(){
jQuery('.yesno-moderate-entry').yesnomoderate({drupalnode:$nodeid,viewmode:$viewmode,aspectratio: {$a[0]["value"]}});
})";
drupal_add_js($onready,"inline");
try
{
$query = db_select('yesno_response_summaries',"r")
->fields("r",array("yescount","nocount","dncount","id","objectid"))
->condition("r.nodeid",$nodeid)
->condition("r.responsecount",$t,">=")
->isNull("r.accepted")
->extend("PagerDefault");
//Probably shift this into the javascript with my own paging
$result = $query->execute();
$output = "";
$numrows = 0;
foreach($result as $row)
{
//work out which is the most frequent result and preselect that field
$pickfield = -2; // default to no selection
if(($row->yescount > $row->nocount) and ($row->yescount > $row->dncount))
$pickfield = 1;
if(($row->nocount > $row->yescount) and ($row->nocount > $row->dncount))
$pickfield = -1;
if(($row->dncount > $row->yescount) and ($row->dncount > $row->nocount))
$pickfield = 0;
$output .= "<div class=\"yesno-moderate-entry\" data-objectid=\"{$row->objectid}\">"
. "<div class=\"yesno-moderate-entry-title\">Responses for object with id: {$row->objectid} </div>";
_add_moderate_element($row->id, 1, "Yes: {$row->yescount} responses", $pickfield, $output);
_add_moderate_element($row->id, -1,"No: {$row->nocount} responses", $pickfield, $output);
_add_moderate_element($row->id, 0, "Don't know: {$row->dncount} responses", $pickfield, $output);
$output .= "<div class=\"yesno-accept-reject-container\">"
. "<div title=\"Accept selected result\" class=\"yesno-button yesno-button-accept\">Accept</div>"
. "<div title=\"Reject entire result\" class=\"yesno-button yesno-button-reject\">Reject</div>"
. "<div class=\"yesno-button yesno-button-review\">Review</div>"
. "</div>"
."</div>";
$numrows ++;
}
if(!$numrows)
$output .= "<div class=\"yesno-accept-reject-container\">There are no results requiring moderation at this time</div>";
$output.=theme("pager",array("rows"=>55));
return array("#markup"=>$output);
}
catch(PDOException $e)
{
dpm("Whoops {$e->getMessage()}");
}
}
/**
* Page callback mapped to the url /yesnonextimage
* Acts as a service end point to ajax calls from the yesno widget in response to a request for an image url
* @return json array containing the url of the image and the id of the object in the image
*/
function _page_yesno_nextimage($nodeid,$objectid = "")
{
$node = node_load($nodeid);
if(!$node)
{
echo json_encode(array("status"=>0,"message"=>"Request for image is for an umknown node"));
drupal_exit();
}
$t = _yesno_getThreshold($node);
//$filename = _yesno_manifest_name($node);
require_once "nextimage.php";
//getNextImageId($filename);
if(!$objectid)
getNextDbImageId($nodeid,$t);
else
getImageForDbObject($nodeid,$objectid);
drupal_exit();
}
/**
* Page callback mapped to the url /yesnozoomify
* Acts as a service end point for on the fly zoomify tiles arising from a yesno page
*/
function _page_yesno_zoomify($filename,$tilegroup,$tilename = "")
{
require "zoomify.php";
$d = variable_get('file_public_path', conf_path() . '/files');
//$d = drupal_get_path("module","yesno") . "/zoomcache";
$z = new zoomifier("$d/yesno/zoomcache");
$filename = str_replace("|","/",urldecode($filename));
//A request for the XML file will not include the tilegroup in the path
if(!$tilename)
$tilename = $tilegroup;
if(!$tilename)
{
echo "No tile specified!";
exit;
}
try
{
$z->getTile($filename,$tilename);
}
catch(Exception $e)
{
//Make sure any locks are cleared after an exception - this is kind of belt and braces since errors should result in locks being cleared by the zoomify object
$z->releaseLock();
//drupal_set_message($e->getMessage(),"error");
}
exit;
}
/**
* Page callback mapped to the url /yesnozoomify
* Acts as a service end point for on the fly zoomify tiles arising from a yesno page
*/
function _page_yesno_clearzoomcache($filename)
{
require "zoomify.php";
$d = variable_get('file_public_path', conf_path() . '/files');
//$d = drupal_get_path("module","yesno") . "/zoomcache";
$z = new zoomifier("$d/yesno/zoomcache");
$filename = str_replace("|","/",urldecode($filename));
$z->delCacheEntry($filename);
exit;
}
/**
* Page callback mapped to the url /yesno_saveanswer
* Acts as a service end point to ajax calls from the yesno widget in response to a request to save a user response
* @return json array indicating the status of the save request
*/
function _page_yesno_saveanswer($nodeid,$objectid,$answer)
{
global $user;
$node = node_load($nodeid);
if(!$node)
{
echo json_encode(array("status"=>0,"message"=>"Request to save response is for an unknown node"));
drupal_exit();
}
$userid = $user->uid;
//only take the first 10 characters to ensure that there is no integer overflow - without this the number id converted to a float and bad things happen?
$idhash = _yesno_id_hash($objectid);
try
{
db_insert('yesno_responses')->fields(array(
'nodeid'=>$nodeid,
'answer'=>$answer,
'objectid'=>$objectid,
'objectidhash'=> $idhash,
'userid'=>$userid,
))->execute();
//Update the reponse count for the appropriate record in the manifest file
//Because there may be hashing collisions need to check that we have the right one
$result = db_select('yesno_response_summaries',"m")->fields("m",array("responsecount","objectid","id"))->condition("m.nodeid",$nodeid)->condition("m.objectidhash",$idhash)->execute();
$cnt = -1;
$responseid = "";
while($row = $result->fetchAssoc())
{
if($row["objectid"] == $objectid);
{
$cnt = $row["responsecount"];
$responseid = $row["id"];
break;
}
}
//If there is no result then this must be a new record
if($cnt = -1)
$responseid = db_insert('yesno_response_summaries')->fields(array(
"responsecount"=>1,
"nodeid" => $nodeid,
"objectid"=>$objectid,
'objectidhash'=> $idhash,
))->execute();
else
db_update('yesno_response_summaries')->fields(array("responsecount"=>$cnt+1))->condition("id",$responseid)->execute();
//Update the appropriate answer count field
switch($answer)
{
case -1: $updatefield = "nocount"; break;
case 0: $updatefield = "dncount"; break;
case 1: $updatefield = "yescount"; break;
}
$cnt = db_select('yesno_response_summaries',"m")->fields("m",array($updatefield))->condition("id",$responseid)->execute()->fetchField();
if($cnt === FALSE)
$cnt = 0;
db_update('yesno_response_summaries')->fields(array($updatefield=>$cnt+1))->condition("id",$responseid)->execute();
//Check to see if the answer should be automatically accepted
$t = _yesno_getThreshold($node); //Get the threshold value for considering a response set to be complete
$a = _yesno_getAutoacceptThreshold($node); //Get the threshold that has to be achieved in order to automatically accept a response
$e = _yesno_getAutoupdate($node); //Check to see if an external data base should be automatically updated
$err = 0; //Error flag set when an error occurs during an autoupdate
$result = db_select('yesno_response_summaries',"m")->fields("m",array("responsecount","yescount","nocount","dncount","objectid","id"))->condition("m.nodeid",$nodeid)->condition("m.objectidhash",$idhash)->execute();
//Handle the possibility of collisions
while($row = $result->fetchAssoc())
{
if($row["objectid"] == $objectid);
{
$cnt = $row["responsecount"];
$yes = $row["yescount"];
$no = $row["nocount"];
$dn = $row["dncount"];
$responseid = $row["id"];
break;
}
}
//Has an autoaccept threshold been specified for this node
if($a)
{
//Does the ressponse count equal or exceed the the response threshold - ie is the question fully answered
if($cnt >= $t)
{
//Does either yes or no exceed all the other counts and equal or exceed the autoaccept threshold
if(($yes >= $a) and ($yes > $no) and ($yes > $dn))
{
db_update('yesno_response_summaries')->fields(array("accepted"=>1,"autoaccepted"=>1))->condition("id",$responseid)->execute();
}
if(($no >= $a) and ($no > $yes) and ($no > $dn))
db_update('yesno_response_summaries')->fields(array("accepted"=>-1,"autoaccepted"=>1))->condition("id",$responseid)->execute();
}
if($e)
$err = _yesno_autoupdate($node,$objectid,$answer);
}
$newmilestone = false;
$err = false;
//Non logged in users cannot reach a milestone
if(user_is_logged_in())
{
//Check to see if a milestone has been reached
$query = db_select("yesno_responses","r")
->fields('r', array("answer","objectid"))
->condition("r.nodeid",$nodeid)
->condition("r.userid",$userid);
//Get the total number of answers
$answers = $query->countQuery()->execute()->fetchField();
$m = field_get_items("node",$node,"yesno_subtask_milestone_value");
$milestone = $m[0]["value"];
if($answers and $milestone and !($answers % $milestone))
$newmilestone = true;
}
if(!$err)
echo json_encode(array("status"=>1,"newmilestone"=>$newmilestone));
else
echo json_encode(array("status"=>-1,"message"=>$err,"newmilestone"=>$newmilestone));
}
catch(PDOException $e)
{
echo json_encode(array("status"=>0,message=>"Could not save the response! Reason: {$e->getMessage()}"));
}
exit;
}
/**
* Page callback mapped to the url /yesno_acceptanswer
* Acts as a service end point to ajax calls from the yesno widget in response to a request to accept an answer via the moderation interface
* @return json array indicating the status of the save request
*/
function _page_yesno_acceptanswer($nodeid,$objectid,$answer)
{
$node = node_load($nodeid);
$e = 0;
if($node)
$e = _yesno_getAutoupdate($node);
try
{
db_update('yesno_response_summaries')->fields(array("accepted"=>$answer))->condition("objectid",$objectid)->execute();
$err = 0;
if($e)
$err = _yesno_autoupdate($node,$objectid,$answer);
if(!$err)
echo json_encode(array("status"=>1));
else
echo json_encode(array("status"=>-1,message=>$err));
}
catch(PDOException $e)
{
echo json_encode(array("status"=>0,message=>"Problem saving accepted response! Reason: {$e->getMessage()}"));
}
}
/**
* Page callback mapped to the url /yesno_rejectanswer
* Acts as a service end point to ajax calls from the yesno widget in response to a request to reject an answer via the moderation interface
* @return json array indicating the status of the save request
*/
function _page_yesno_rejectanswer($nodeid,$objectid,$answer)
{
try
{
//Update the response summary setting the response count to 0
//db_delete('yesno_response_summaries')->condition("objectid",$objectid)->condition("nodeid",$nodeid)->execute();
//Remove all the individual responses
//db_delete('yesno_responses')->condition("objectid",$objectid)->condition("nodeid",$nodeid)->execute();
db_update('yesno_response_summaries')->condition("objectid",$objectid)->condition("nodeid",$nodeid)->fields("responsecount",0)->execute();
echo json_encode(array("status"=>1));
}
catch(PDOException $e)
{
echo json_encode(array("status"=>0,"message"=>"Problem rejecting response! Reason: {$e->getMessage()}"));
}
}
/**
* Page callback mapped to the url /yesno_stats
* Acts as a service end point to ajax calls from the yesno widget in response to a request for user stats
* THIS MAY BE TRICKY SINCE I MAY NOT KNOW WHO THE CURRENT USER IS
* @return json array indicating the status of the save request
*/
function _page_yesno_stats($nodeid)
{
if(!$nodeid)
return;
global $user;
$userid = $user->uid;
require_once("yesnostats.php");
getdbStats($nodeid,$userid);
//exit;
}
/**
* Page callback mapped to the url /yesnodownload
* Returns a csv file listing all accepted reponses for the spciefied node
*/
function _page_yesno_download($nodeid)
{
header("Content-Type: text/csv");
$node = node_load($nodeid);
if(!$node)
{
echo json_encode(array("status"=>0,"message"=>"Request for download is for an umknown node"));
drupal_exit();
}
//Base the file name on the yes no question and the date
$q = _yesno_getQuestion($node);
$filename = str_replace("?","",str_replace(" ","_",$q)) . "_" . date("d-M-Y-H-i-s");
header("Content-Disposition: attachment; filename=\"$filename.csv\";");
$result = db_select("yesno_response_summaries","s")
->fields("s",array("objectid","accepted","autoaccepted","yescount","nocount","dncount"))
->condition("nodeid",$nodeid)
->isNotNull("accepted")
->execute();
echo("\"objectid\",\"accepted answer\",\"autoaccepted\",\"Yes count\",\"No count\",\"Don't know count\"\n");
foreach($result as $row)
{
echo("\"{$row->objectid}\",{$row->accepted},{$row->autoaccepted},{$row->yescount},{$row->nocount},{$row->dncount}\n");
}
drupal_exit();
}
function _yesno_module_config()
{
$fval = variable_get('yesno_projectpage_title', '') ? variable_get('yesno_projectpage_title', '') : YESNO_DEFAULT_LIST_TITLE;
$form["yesno_projectpage_title"] = array
(
'#type' => 'textfield',
'#title' => 'Yes/No project list page title',
'#description' => t('Title to display at the top of the project list page'),
'#default_value' => $fval,
);
$fval = variable_get('yesno_projectpage_intro', '') ? variable_get('yesno_projectpage_intro', '') : YESNO_DEFAULT_LIST_INTRO;
$form["yesno_projectpage_intro"] = array
(
'#type' => 'textarea',
'#title' => 'Yes/No project list page introductory text',
'#description' => t('Informative text to display between the title and the list of available projects'),
'#default_value' => $fval,
);
return system_settings_form($form);
}
/***************************** MODULE HELPER FUNCTIONS ***********************************/
/**
* Function to act as an access control call back check for ajax endpoints accessed via the menu system
**/
function _yesno_access_control($checkloggedin, $required_roles = array())
{
if($checkloggedin and !user_is_logged_in())
return false;
if(count($required_roles) and !user_is_logged_in())
return false;
global $user;
$result = true;
foreach($required_roles as $role)
{
if(!in_array($role,$user->roles))
{
$result = false;
break;
}
}
return $result;
}
/**
*Function to create and format a item to display in a list of responses requiring moderation
**/
function _add_moderate_element($groupid, $value, $label, $pickfield, &$output)
{
$selected = "";
$cclass = "";
if($value == $pickfield)
{
$selected = "checked=\"checked\"";
$cclass = "yesno-moderate-label-type$value";
}
$output .= "<div class=\"yesno-moderate-entry-element $cclass\"><span>$label</span><input $selected type=\"radio\" name=\"accept$groupid\" value=\"$value\"/></div>";
}
/**
*Common function for both node update and insert which updates the manifest for a node
**/
function _yesno_check_manifest($node)
{
//Make sure that I get the upto date data for the node
entity_get_controller('node')->resetCache(array($node->nid));
$newnode = node_load($node->nid);
if(!$node)
return;
$m = field_get_items("node",$newnode,"yesno_manifest_file");
if(isset($m[0]["uri"]))
{
$fn = drupal_realpath($m[0]["uri"]);
if(file_exists($fn))
{
$file = file_load($m[0]["fid"]);
//return $fn;
$ext = pathinfo($fn, PATHINFO_EXTENSION);
$base = pathinfo($fn, PATHINFO_FILENAME);
$root = pathinfo($fn, PATHINFO_DIRNAME);
switch(strtolower($ext))
{
case "zip": $z = new ZipArchive();
$z->open($fn);
//Assume there is only one file in the zip and this is the manifest
$info = $z->statIndex(0);
$fz = $z->getStream($info["name"]);
if($fz)
{
$fo = fopen("$root/$base.txt","w");
while (!feof($fz))
{
fwrite($fo,fread($fz, 2000));