-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuser-application-page.php
1029 lines (966 loc) · 43.7 KB
/
user-application-page.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
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
////////////////////////////////////////////////////////////////////////////////
// This is the admin page for managing the user's application, on their profile
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Application evaluation and finalization
////////////////////////////////////////////////////////////////////////////////
function ccgn_membership_council_voting_disabled_for ( $applicant_id ) {
$vote_counts = ccgn_application_votes_counts( $applicant_id );
return $vote_counts[ 'no' ] > CCGN_NUMBER_OF_VOTES_AGAINST_ALLOWED;
}
function ccgn_must_decline_membership ( $applicant_id ) {
$vouch_counts = ccgn_application_vouches_counts( $applicant_id );
$vote_counts = ccgn_application_votes_counts( $applicant_id );
return ( $vouch_counts[ 'no' ] > CCGN_NUMBER_OF_VOUCHES_AGAINST_ALLOWED )
|| ( $vote_counts[ 'no' ] > CCGN_NUMBER_OF_VOTES_AGAINST_ALLOWED );
}
function ccgn_can_activate_membership ( $applicant_id ) {
$vouch_counts = ccgn_application_vouches_counts( $applicant_id );
$vote_counts = ccgn_application_votes_counts( $applicant_id );
return ( $vouch_counts[ 'yes' ] >= CCGN_NUMBER_OF_VOUCHES_NEEDED )
&& ( $vote_counts[ 'yes' ] >= CCGN_NUMBER_OF_VOTES_NEEDED );
}
function ccgn_activate_and_notify_member ( $applicant_id ) {
ccgn_user_level_set_approved( $applicant_id );
ccgn_create_profile( $applicant_id );
ccgn_registration_email_application_approved( $applicant_id );
//ccgn_application_remove_avatar ( $applicant_id );
}
function ccgn_decline_and_notify_applicant ( $applicant_id ) {
ccgn_user_level_set_rejected( $applicant_id );
ccgn_registration_email_application_rejected( $applicant_id );
//ccgn_application_remove_avatar ( $applicant_id );
}
////////////////////////////////////////////////////////////////////////////////
// State formatting
////////////////////////////////////////////////////////////////////////////////
// Format the list of votes the member has received from their voters
function ccgn_application_users_page_vote_responses ( $applicant_id ) {
$result = '';
$votes = ccgn_application_votes ( $applicant_id );
foreach ($votes as $vote) {
$voter = get_user_by('ID', $vote['created_by']);
$reason = $vote[ CCGN_GF_VOTE_APPROVE_MEMBERSHIP_APPLICATION_REASON ];
$result .= '<div class="ccgn-box applicant">';
$result .= '<h4>'. $voter->display_name . '</h4>'
.'<p><strong>Voted:</strong>'
. $vote[
CCGN_GF_VOTE_APPROVE_MEMBERSHIP_APPLICATION
]
. '</p>';
if ( is_admin() && !empty($reason) ) {
$result .= '<div class="reason">'
. '<strong>Reason:</strong><br />'
. '<p class="reason-text">'.$reason.'</p>'
.'</div>';
}
$result .= '</div>';
}
return $result;
}
// Format the count of votes
function ccgn_application_users_page_vote_counts ( $applicant_id ) {
$counts = ccgn_application_votes_counts( $applicant_id );
return '<p><strong>Yes: </strong>'
. $counts['yes']
. '<p><strong>No: </strong>'
. $counts['no']
. '</p>';
}
function ccgn_registration_email_vouching_requests ( $applicant_id ) {
$vouchers_ids = ccgn_application_vouchers_users_ids ( $applicant_id );
foreach ( $vouchers_ids as $voucher_id ) {
// TODO: Check for active user etc.
ccgn_registration_email_vouching_request(
$applicant_id,
$voucher_id
);
}
}
////////////////////////////////////////////////////////////////////////////////
// Embedded form setup and handling
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Handle pre form results
//NOTE: WE DON'T OVERWRITE THESE FORM ENTRIES, WE ADD NEW ONES EACH TIME.
// EVERYTHING SHOULD BE LIKE THIS BUT THAT WAS A BAD DESIGN DECISION.
// Also note that this means we only have to check the creation date.
// - RobM.
function ccgn_application_users_page_pre_form_submit_handler ( $entry,
$form ) {
if ( $form[ 'title' ] == CCGN_GF_PRE_APPROVAL ) {
if ( ! ( ccgn_current_user_is_membership_council()
|| ccgn_current_user_is_final_approver() ) ) {
echo 'Must be Membership Council member.';
exit;
}
$applicant_id = $entry[ CCGN_GF_PRE_APPROVAL_APPLICANT_ID ];
$stage = ccgn_registration_user_get_stage( $applicant_id);
if ( ! in_array( $stage, CCGN_APPLICATION_STATE_CAN_BE_PRE_APPROVED )) {
echo 'User already pre-approved';
return;
}
$application_status = $entry[
CCGN_GF_PRE_APPROVAL_APPROVE_MEMBERSHIP_APPLICATION
];
if ( $application_status == CCGN_GF_PRE_APPROVAL_APPROVED_YES ) {
ccgn_user_level_set_pre_approved( $applicant_id );
ccgn_registration_email_vouching_requests( $applicant_id );
} elseif (
$application_status
== CCGN_GF_PRE_APPROVAL_APPROVED_UPDATE_DETAILS
) {
// Set state
ccgn_registration_user_set_stage(
$applicant_id,
CCGN_APPLICATION_STATE_UPDATE_DETAILS
);
// Notify user
$update_message = $entry[
CCGN_GF_PRE_APPROVAL_APPLICANT_MUST_UPDATE_DETAILS
];
ccgn_registration_email_to_applicant (
$applicant_id,
'ccgn-email-update-details',
$update_message
);
$update_details_meta = array(
'state' => 'none',
'updated' => 0,
'date' => date('Y-m-d H:i:s', strtotime('now')),
'done' => false
);
update_user_meta( $applicant_id, 'ccgn_applicant_update_details_state', $update_details_meta );
} else {
ccgn_user_level_set_rejected( $applicant_id );
ccgn_registration_email_application_rejected( $applicant_id );
}
}
}
// Format the pre approval form
function ccgn_application_users_page_pre_form ( $applicant_id ) {
// Slightly naughty - [][0] works fine here when there is no previous entry
$existing_entry = ccgn_entries_referring_to_user (
$applicant_id,
CCGN_GF_PRE_APPROVAL,
CCGN_GF_PRE_APPROVAL_APPLICANT_ID
)[0];
gravity_form(
CCGN_GF_PRE_APPROVAL,
false,
false,
false,
array(
CCGN_GF_PRE_APPROVAL_APPLICANT_ID_PARAMETER
=> $applicant_id,
// If the user is in the update details state, we show the
// information for this.
CCGN_GF_PRE_APPROVAL_APPROVE_MEMBERSHIP_APPLICATION_PARAMETER
=> $existing_entry[
CCGN_GF_PRE_APPROVAL_APPROVE_MEMBERSHIP_APPLICATION
],
CCGN_GF_PRE_APPROVAL_APPLICANT_MUST_UPDATE_DETAILS_PARAMETER
=> $existing_entry[
CCGN_GF_PRE_APPROVAL_APPLICANT_MUST_UPDATE_DETAILS
]
)
);
}
function ccgn_application_mc_review_form ( $applicant_id ) {
if ( ccgn_current_user_is_membership_council() || ccgn_current_user_is_final_approver() ) {
$existing_entry = ccgn_entries_referring_to_user (
$applicant_id,
CCGN_GF_MC_REVIEW,
CCGN_GF_MC_REVIEW_RESULT
)[0];
gravity_form(
CCGN_GF_MC_REVIEW,
false,
false,
false,
array(
CCGN_GF_MC_REVIEW_RESULT_PARAMETER
=> $existing_entry[
CCGN_GF_MC_REVIEW_RESULT
],
CCGN_GF_MC_REVIEW_NOTE_PARAMETER
=> $existing_entry[
CCGN_GF_MC_REVIEW_NOTE
],
CCGN_GF_MC_REVIEW_APPLICANT_ID_PARAMETER
=> $applicant_id
)
);
}
}
function ccgn_application_mc_review_submit_handler( $entry, $form ) {
if ( $form[ 'title' ] == CCGN_GF_MC_REVIEW ) {
$applicant_id = $entry[ CCGN_GF_MC_REVIEW_APPLICANT_ID ];
$user_meta = array(
'status' => 'user-reviewed',
'result' => $entry[CCGN_GF_MC_REVIEW_RESULT],
'note' => $entry[CCGN_GF_MC_REVIEW_NOTE],
'date' => date('Y-m-d H:i:s', strtotime('now')),
'user' => get_current_user_id()
);
update_user_meta($applicant_id, 'ccgn-user-mc-review', $user_meta);
if ( ccgn_current_user_is_membership_council() || ccgn_current_user_is_final_approver() ) {
$was_voted_no = get_user_meta( $applicant_id, 'ccgn-application-voted-no', true );
if ( $entry[ CCGN_GF_MC_REVIEW_RESULT ] == 'Yes' ) {
$choose_vouchers_entry = ccgn_application_vouchers($applicant_id);
$no_voucher = '';
$first_voucher = ccgn_vouches_for_applicant_by_voucher( $applicant_id, $choose_vouchers_entry[1] );
$second_voucher = ccgn_vouches_for_applicant_by_voucher( $applicant_id, $choose_vouchers_entry[2] );
if ( $was_voted_no['status'] ) {
$no_vote_entry_id = $was_voted_no['entry_id'];
$no_vote_restore = array(
'status' => false,
'entry_id' => false,
'date' => date('Y-m-d H:i:s', strtotime('now'))
);
update_user_meta($applicant_id, 'ccgn-application-voted-no', $no_vote_restore);
if (!empty($no_vote_entry_id)) {
GFAPI::delete_entry( $no_vote_entry_id );
}
ccgn_registration_user_set_stage( $applicant_id, CCGN_APPLICATION_STATE_VOUCHING );
} else {
ccgn_registration_user_set_stage( $applicant_id, CCGN_APPLICATION_STATE_UPDATE_VOUCHERS );
$update_date = date('Y-m-d H:i:s', strtotime('now'));
if ( $first_voucher[0][CCGN_GF_VOUCH_DO_YOU_VOUCH] == 'No' ) {
$vouch = $first_voucher[0];
} elseif ( $second_voucher[0][CCGN_GF_VOUCH_DO_YOU_VOUCH] == 'No' ) {
$vouch = $second_voucher[0];
}
$vouch[CCGN_GF_VOUCH_DO_YOU_VOUCH] = CCGN_GF_VOUCH_DO_YOU_VOUCH_REMOVED;
GFAPI::update_entry($vouch);
ccgn_set_entry_update_date($vouch, $update_date);
// Update the date on the Choose Vouchers form, resetting the timescale
// for updating voucher choices
ccgn_set_entry_update_date( $choose_vouchers_entry, $update_date );
ccgn_registration_email_to_applicant (
$applicant_id,
'ccgn-email-mc-review-update-vouchers',
$entry[ CCGN_GF_MC_REVIEW_NOTE ]
);
}
} elseif ( $entry[ CCGN_GF_MC_REVIEW_RESULT ] == 'No' ) {
ccgn_decline_and_notify_applicant( $applicant_id );
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Handle vote form results
function ccgn_application_users_page_vote_form_submit_handler ( $entry,
$form ) {
$applicant_id = $entry[ '4' ];
if (!empty($applicant_id)) {
if (! ( ccgn_current_user_is_membership_council()
|| ccgn_current_user_is_final_approver() ) ) {
echo 'Must be Membership Council member.';
exit;
} elseif ( ccgn_vouching_request_exists( $applicant_id,
get_current_user_id() ) ) {
// The user is Membership Council or Final Approver, but is also
// a Voucher for the application and therefore should not also
// Vote on it.
echo 'Cannot Vote on Application you are a Voucher for.';
exit;
}
$stage = ccgn_registration_user_get_stage( $applicant_id);
if ( $stage != CCGN_APPLICATION_STATE_VOUCHING ) {
echo 'User already post-approved (or updating vouchers)';
return;
}
if ($entry[CCGN_GF_VOTE_APPROVE_MEMBERSHIP_APPLICATION] == 'No') {
_ccgn_registration_user_set_stage( $applicant_id, CCGN_APPLICATION_STATE_REVIEW );
$user_vote_no = array(
'status' => true,
'entry_id' => $entry['id'],
'date' => date('Y-m-d H:i:s', strtotime('now'))
);
update_user_meta($applicant_id, 'ccgn-application-voted-no', $user_vote_no );
}
}
}
function ccgn_application_users_page_vote_form ( $applicant_id ) {
if ( ccgn_vouching_request_exists( $applicant_id,
get_current_user_id() ) ) {
echo _('<i>You have been asked to Vouch for this application, you therefore cannot Vote on it as well.</i>');
} else {
$entry = ccgn_application_vote_by_current_user ( $applicant_id );
if ( $entry === false ) {
gravity_form(
CCGN_GF_VOTE,
false,
false,
false,
array(
CCGN_GF_VOTE_APPLICANT_ID_PARAMETER
=> $applicant_id
)
);
} else {
echo _('<i>You have voted on this membership application</i>');
$status = $entry[
CCGN_GF_VOTE_APPROVE_MEMBERSHIP_APPLICATION
];
if ( $status == CCGN_GF_VOTE_APPROVED_YES ) {
echo _('<p>You voted yes.</p>');
} else {
echo _( '<p>You voted no.</p>' );
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Handle final approval form results
function ccgn_application_users_page_final_form_submit_handler( $entry,
$form ) {
if ( $form[ 'title' ] == CCGN_GF_FINAL_APPROVAL ) {
if (! ccgn_current_user_is_final_approver() ) {
echo 'Must be Final Approver.';
exit;
}
$applicant_id = $entry[ CCGN_GF_FINAL_APPROVAL_APPLICANT_ID ];
$stage = ccgn_registration_user_get_stage( $applicant_id);
if ( $stage != CCGN_APPLICATION_STATE_VOUCHING ) {
echo 'User state is bad';
return;
}
//FIXME: check vouch/vote counts
$result = $entry[
CCGN_GF_FINAL_APPROVAL_APPROVE_MEMBERSHIP_APPLICATION
];
if ( $result == CCGN_GF_FINAL_APPROVAL_APPROVED_YES ) {
if ( ccgn_user_is_individual_applicant( $applicant_id ) ) {
ccgn_activate_and_notify_member( $applicant_id );
} elseif ( ccgn_user_is_institutional_applicant( $applicant_id ) ) {
// Notify the applicant that they are at the legal stage
ccgn_registration_email_institution_legal ( $applicant_id );
ccgn_registration_user_set_stage (
$applicant_id,
CCGN_APPLICATION_STATE_LEGAL
);
// Notify legal that the applicant is at their stage
ccgn_registration_email_notify_legal_insititution_approved (
$applicant_id
);
} else {
echo 'User is not individual or institution???';
return;
}
} else {
ccgn_decline_and_notify_applicant ( $applicant_id );
}
}
}
// Format the final approval form with any required pre-sets
function ccgn_application_users_page_final_approval_form ( $applicant_id ) {
$entry = ccgn_final_approval_entry_for ( $applicant_id );
if ( $entry === false ) {
gravity_form(
CCGN_GF_FINAL_APPROVAL,
false,
false,
false,
array(
CCGN_GF_FINAL_APPROVAL_APPLICANT_ID_PARAMETER
=> $applicant_id
),
false,
99 // Avoid tabindex clash if this is showing alongside Vote form
);
$approve = ccgn_can_activate_membership ( $applicant_id )
&& ( ! ccgn_must_decline_membership ( $applicant_id ) );
$decline = true;
?>
<script>
jQuery('document').ready(function () {
<?php
if ( ! $approve ) {
?>
jQuery('.activate_membership input[value="Yes"]').attr('disabled',
true);
<?php
}
if ( ! $decline ) {
?>
jQuery('.activate_membership input[value="No"]').attr('disabled',
true);
<?php
}
?>
});
</script>
<?php
} else {
echo _('<i>This application has been finalised</i>');
$status = $entry[
CCGN_GF_FINAL_APPROVAL_APPROVE_MEMBERSHIP_APPLICATION
];
if ( $status == CCGN_GF_FINAL_APPROVAL_APPROVED_YES ) {
echo _('<p>It was approved.</p>');
} else {
echo _('<p>It was rejected.</p>');
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Institutional legal approval form
function ccgn_application_users_page_legal_approval_form_submit_handler (
$entry,
$form
) {
if ( $form[ 'title' ] == CCGN_GF_LEGAL_APPROVAL ) {
if (! ccgn_current_user_is_legal_team() ) {
echo 'Must be Legal Team Member.';
exit;
}
$applicant_id = $entry[ CCGN_GF_LEGAL_APPROVAL_APPLICANT_ID ];
$stage = ccgn_registration_user_get_stage( $applicant_id);
if ( $stage != CCGN_APPLICATION_STATE_LEGAL ) {
echo 'User state is bad';
return;
}
$result = $entry[
CCGN_GF_LEGAL_APPROVAL_APPROVE_MEMBERSHIP_APPLICATION
];
if ( $result == CCGN_GF_LEGAL_APPROVAL_APPROVED_YES ) {
if ( ccgn_user_is_institutional_applicant( $applicant_id ) ) {
ccgn_activate_and_notify_member ( $applicant_id );
} else {
echo 'User is not institution???';
return;
}
} else {
ccgn_decline_and_notify_applicant ( $applicant_id );
}
}
}
function ccgn_application_users_page_legal_approval_form ( $applicant_id ) {
$entry = ccgn_legal_approval_entry_for ( $applicant_id );
if ( $entry === false ) {
gravity_form(
CCGN_GF_LEGAL_APPROVAL,
false,
false,
false,
array(
CCGN_GF_LEGAL_APPROVAL_APPLICANT_ID_PARAMETER
=> $applicant_id
)
);
}
}
function ccgn_application_format_legal_approval ( $applicant_id, $state ) {
if(
! in_array(
$state,
CCGN_APPLICATION_STATE_LEGAL_APPROVAL_STATE_AVAILABLE
)
) {
return;
}
echo _('<h2>Legal Final Approval Of Institutional Application</h2>');
if ( $state === CCGN_APPLICATION_STATE_LEGAL) {
if ( ccgn_current_user_is_legal_team() ) {
ccgn_application_users_page_legal_approval_form ( $applicant_id );
} else {
echo _('<p>Legal final approval pending.</p>');
}
}
elseif ( in_array( $state, CCGN_APPLICATION_STATE_PAST_APPROVAL ) ) {
echo _('<i>Legal has resolved this membership application</i>');
$entry = ccgn_legal_approval_entry_for ( $applicant_id );
$status = $entry[
CCGN_GF_LEGAL_APPROVAL_APPROVE_MEMBERSHIP_APPLICATION
];
if ( $status == CCGN_GF_LEGAL_APPROVAL_APPROVED_YES ) {
echo _('<p>The Institution\'s application was resolved successfully.</p>');
} else {
echo _('<p>The Institution\'s application was <i>not</i> resolved successfully.</p>');
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Render the page
////////////////////////////////////////////////////////////////////////////////
function ccgn_application_user_page_render_change_vouchers ( $applicant_id,
$state ) {
if ( current_user_can( 'ccgn_pre_approve' ) ) {
if ( $state == CCGN_APPLICATION_STATE_VOUCHING ) {
echo '<p><a href="'
. ccgn_application_change_vouchers_page_url( $applicant_id )
. '" class="button">';
echo _('Change vouch requests for applicant.');
echo '</a></p>';
} else {
echo _('<p>Applicant is not currently being Vouched, cannot change Vouch Requests.</p>');
}
}
}
function ccgn_application_users_page_render_state ( $applicant_id, $state ) {
$reviewed = get_user_meta($applicant_id, 'ccgn-user-mc-review', true);
if ( !empty( $reviewed ) && ( ccgn_current_user_is_final_approver() || ccgn_current_user_is_membership_council() ) ) {
$user = get_user_by('ID', $reviewed['user']);
echo __('<h3>Reviewed by MC</h3>');
echo __('<p><strong>Vote:</strong> '.$reviewed['result'].'</p>');
echo __('<p><strong>Note:</strong> '.$reviewed['note'].'</p>');
echo __('<p><strong>date:</strong> '.$reviewed['date'].'</p>');
echo __('<p><strong>Reviewed by:</strong> '.$user->display_name.'</p>');
}
if ( in_array( $state, CCGN_APPLICATION_STATE_CAN_BE_PRE_APPROVED ) ) {
echo _('<h3>Pre-Approve</h3>');
ccgn_application_users_page_pre_form ( $applicant_id );
} elseif ( $state == CCGN_APPLICATION_STATE_VOUCHING ) {
if (ccgn_current_user_is_final_approver() || ccgn_current_user_is_membership_council()) {
if ( ccgn_application_can_be_voted( $applicant_id ) ) {
echo _('<h3>Vote</h3>');
ccgn_application_users_page_vote_form ( $applicant_id );
}
if (ccgn_current_user_is_final_approver()) {
echo _('<h3>Final Approval</h3>');
ccgn_application_users_page_final_approval_form( $applicant_id );
}
}
} elseif ( ccgn_membership_council_voting_disabled_for ( $applicant_id ) ) {
echo _('<h3>Voted Against</h3>');
echo _('<p>Membership Council Members voted against this application, so it cannot proceed.</p>');
} elseif ( $state == CCGN_APPLICATION_STATE_ON_HOLD ) {
echo _('<h3>On Hold</h3>');
echo _('<p>The application has been put on hold.</p>');
} elseif ( $state == '' ) {
echo _('<h2>New User.</h2>');
echo _("<p>They haven't completed an application yet.</p>");
}
if ( $state == CCGN_APPLICATION_STATE_REVIEW ) {
echo _('<h2>To be reviewed by Membership Comitee.</h2>');
echo _("<p>This application needs to be reviewed by the Membership Comitee.</p>");
ccgn_application_mc_review_form($applicant_id);
} else {
echo _('<h2>Application resolved.</h2>');
echo '<p>Status: ' . $state . '</p>';
// TODO: show relevant pre/post form here for admin rather than making
// them search for it manually.
}
}
function ccgn_application_users_page_render_details ( $applicant_id, $state ) {
echo _('<h1>Membership Application Details</h1>');
echo '<div id="alert-messages"></div>';
echo _('<h2>Details Provided By Applicant</h2>');
echo ccgn_user_page_applicant_profile_text( $applicant_id );
echo '<br /><h1 class="section-title">Vouchers information</h1>';
echo '<div class="applicant-columns">';
echo '<div class="ccgn-box">';
echo _('<h2>Vouchers Requested</h2>');
$voucher_choices = ccgn_application_vouchers ( $applicant_id );
echo '<p> <span class="dashicons dashicons-calendar-alt"></span> <b>Original request date:</b> '
. date('Y-m-d', strtotime($voucher_choices['date_created']))
. '</p>';
if (! is_null ( $voucher_choices[ 'date_updated' ] ) ) {
echo '<p> <span class="dashicons dashicons-calendar-alt"></span> <b>Updated request date:</b> '
. date('Y-m-d', strtotime($voucher_choices['date_updated']))
. '</p>';
}
echo ccgn_application_users_page_vouchers( $applicant_id );
echo '</div>';
if ( $state != CCGN_APPLICATION_STATE_RECEIVED ) {
echo '<div class="ccgn-box">';
echo _('<h2>Vouches Received</h2>');
echo ccgn_application_users_page_vouch_counts ( $applicant_id );
ccgn_application_user_page_render_change_vouchers(
$applicant_id,
$state
);
echo '</div>';
$clarification_mode = get_user_meta(get_current_user_id(), 'ccgn_need_to_clarify_vouch_reason', true);
if ( isset($_GET['clarification']) && $clarification_mode ) {
echo '</div>';
echo '<div class="applicant-columns">';
echo '<div id="voucher-clarification-container">';
echo '<a name="voucher-clarification"></a>';
echo '<h3>Clarification of your voucher</h3>';
echo '<div id="change-voucher-messages"></div>';
$form_id = RGFormsModel::get_form_id(CCGN_GF_VOUCH);
$search_criteria = array();
$search_criteria['field_filters'][]
= array(
'key' => 'created_by',
'value' => get_current_user_id(),
);
$search_criteria['field_filters'][]
= array(
'key' => CCGN_GF_VOUCH_APPLICANT_ID_FIELD,
'value' => $applicant_id,
);
$get_the_entries = GFAPI::get_entries(
$form_id,
$search_criteria,
array(
array(
'key' => 'date_created',
'direction' => 'ASC',
'is_numeric' => false
)
)
);
//echo '<pre>'; print_r($get_the_entries); echo '</pre>';
$entry_id = $get_the_entries[0]['id'];
echo wp_nonce_field('clarification_voucher', 'clarification_voucher_nonce', true, false);
echo '<p><textarea name="clarification_voucher" id="clarification_voucher" cols="50" rows="10">'. $get_the_entries[0]['4'] .'</textarea></p>';
echo '<button class="button button-primary" id="set-new-vouch-reason" data-entry-id="'.$entry_id.'">Set new reason</button>';
echo '</div>';
}
echo '</div><br>';
echo _('<h1 class="section-title">Vouchers list</h1>');
echo '<div class="applicant-columns">';
$vouchers = ccgn_application_users_page_vouch_responses_data(
$applicant_id,
true
);
foreach ($vouchers as $voucher) {
$asked = get_user_meta($voucher['id'], 'ccgn_need_to_clarify_vouch_reason', true);
$asked_info = get_user_meta($voucher['id'], 'ccgn_need_to_clarify_vouch_reason_applicant_status', true);
$user_is_asked_for_clarification = 0;
$applicant_votes = ccgn_application_votes_counts($applicant_id);
$asked_class = '';
$who_asked = '';
if (empty($asked_info['user_id'])) {
$log_user = ccgn_ask_clarification_log_get_id($applicant_id);
if (!empty($log_user)) {
foreach ($log_user as $entry) {
$asked_meta = get_user_meta($entry['voucher_id'], 'ccgn_need_to_clarify_vouch_reason_applicant_status', true);
if (($asked_meta['status'] == 1) && ($entry['voucher_id'] == $voucher['id'])) {
$user_is_asked_for_clarification = 1;
$asked_class = 'asked-box';
$who_asked = (!empty($asked_info['ask_user'])) ? get_user_by('ID',$asked_info['ask_user'])->display_name : $entry['ask_user_name'];
} else if (($asked_meta['status'] == 0) && ($entry['voucher_id'] == $voucher['id'])) {
$user_is_asked_for_clarification = 2;
$asked_class = 'asked-box-answered';
$who_asked = (!empty($asked_info['ask_user'])) ? get_user_by('ID',$asked_info['ask_user'])->display_name : $entry['ask_user_name'];
}
}
}
} else {
$log_user = ccgn_ask_clarification_log_get_id($asked_info['applicant_id']);
$who_asked = (!empty($asked_info['ask_user'])) ? get_user_by('ID',$asked_info['ask_user'])->display_name : $log_user[count($log_user)-1]['ask_user_name'];
$user_is_asked_for_clarification = $asked_info['status'];
}
echo '<div class="ccgn-box applicant '.$asked_class.'">';
echo '<h3 class="applicant-name">'.$voucher['name'].'</h3>';
echo '<span class="date">'.$voucher['date'].'</span>';
if ($user_is_asked_for_clarification) {
echo '<br><small><em>Asked for clarification</em></small>';
if (!empty($who_asked)) {
echo '<br><small><em><strong>Asked by:</strong> '.$who_asked.'</em></small>';
}
}
if ($voucher['vouched'] == 'Yes') {
echo '<p class="applicant-reason">' . $voucher['reason'] . '</p>';
} else {
if ($applicant_votes['yes'] < 5) {
echo '<p class="applicant-reason">' . $voucher['reason_no'] . '</p>';
}
}
echo '<p class="state"><strong>Vouched:</strong> '.$voucher['vouched'].'</p>';
if (($voucher['vouched'] == 'Yes') && (ccgn_current_user_is_final_approver($applicant_id) || ccgn_current_user_is_membership_council($applicant_id)) ) {
echo '<a href="#" onClick="$.askVoucher('.$voucher['id'].',\''.$voucher['name'].'\','.$applicant_id.')" class="button">Ask for clarification</a>';
}
if (current_user_can( 'administrator' ) && ($voucher['vouched'] == 'No')) {
echo '<br><a href="#" class="button-primary" style="background-color: #dc3232;" onClick="$.removeVoucher('.$voucher['id'].',\''.$voucher['name'].'\','.$applicant_id.')" class="button">Remove this voucher</a>';
}
echo '</div>';
}
echo '</div>';
add_thickbox();
echo '<div id="remove-voucher-modal" style="display:none;">';
echo '<h2>You are about to remove this voucher: <span class="name-display"></span></h2>';
echo '<p>That means that you think the vouch is not correct and the applicant needs to choose another voucher</p>';
echo '<p>this process will send an email to the applicant in order to choose a new voucher the status will be updated to "update-vouchers" </p>';
echo '<p>Are you sure you want to do this? </p>';
echo '<br>';
echo wp_nonce_field('remove_voucher', 'remove_voucher_nonce', true, false);
echo '<div class="buttons">';
echo '<button id="close-remove-voucher" class="button close-window">Close</button> ';
echo "<button id=\"remove-voucher-for-sure\" class=\"button button-primary ask-voucher-for-sure\">Yes, I'm sure</button>";
echo '</div>';
echo '</div>';
echo '<div id="ask-clarification-modal" style="display:none;">';
echo '<h2>You are about to ask for clarification to the voucher: <span class="name-display"></span></h2>';
echo '<p>That means you think the text supporting this application is not enough, is not clear or is not helpful for you to approve this application. If that is the case, you can ask the voucher to clarify.</p>';
echo '<div class="log-content" id="log-content-ask-voucher">';
echo '<p>This already was requested by: </p>';
echo '<div class="inner-scroll medium">';
echo '<ol class="log-entries" id="log-entry-ask-voucher">';
echo '</ol>';
echo '</div>';
echo '<p>There is no need to send this email again to the voucher. In case you consider that necessary, you can do it again.</p>';
echo '</div>';
echo '<p>Are you sure you want to do this? </p>';
echo '<br>';
echo wp_nonce_field('ask_voucher', 'ask_voucher_nonce', true, false);
echo '<div class="buttons">';
echo '<button id="close-ask-voucher" class="button close-window">Close</button> ';
echo "<button id=\"ask-voucher-for-sure\" class=\"button button-primary ask-voucher-for-sure\">Yes, I'm sure</button>";
echo '</div>';
echo '</div>';
echo '<div id="change-voucher-modal" style="display:none;">';
echo '<h2>You are about to change the current voucher: <span class="name-display"></span></h2>';
echo '<div class="gform_wrapper">';
gravity_form_enqueue_scripts( 41, false );
$choices = array();
$members = ccgn_registration_form_list_members(get_current_user_id());
foreach ($members as $member) {
$choices[] = array(
'text' => $member[1].' ('.$member[2].')',
'value' => $member[0],
'is_selected' => false
);
}
$field_properties = array(
'type' => 'select',
'enableEnhancedUI' => true,
'id' => 'changeVoucher',
'cssClass' => 'custom-select-changer',
'choices' => $choices
);
$field = GF_Fields::create($field_properties);
echo $field->get_field_input();
echo '</div>';
echo '<br>';
echo wp_nonce_field('change_voucher', 'change_voucher_nonce', true, false);
echo '<div class="buttons">';
echo '<button id="close-change-voucher" class="button close-window">Close</button> ';
echo "<button id=\"change-voucher-for-sure\" class=\"button button-primary change-voucher-for-sure\">Change</button>";
echo '</div>';
//echo '</p>';
echo '</div>';
} else {
echo '</div><br>';
}
echo _('<br><h1 class="section-title">Global Council Approval</h2>');
echo '<div class="applicant-columns">';
echo '<div class="ccgn-box">';
echo _('<h3>Votes Received</h3>');
echo ccgn_application_users_page_vote_counts ( $applicant_id );
echo '</div>';
echo '</div>';
echo _('<h3>Votes</h3>');
echo '<div class="applicant-columns">';
echo ccgn_application_users_page_vote_responses ( $applicant_id );
echo '</div>';
}
function ccgn_application_users_page () {
if ( ! ccgn_current_user_can_see_user_application_page () ) {
echo _( '<br />Sorry, you are not allowed to access this page.' );
}
$applicant_id = ccgn_request_applicant_id ();
if ( $applicant_id === false ) {
return;
}
$state = ccgn_registration_user_get_stage( $applicant_id );
ccgn_application_users_page_render_details( $applicant_id, $state );
ccgn_application_users_page_render_state( $applicant_id, $state );
if ( ccgn_user_is_institutional_applicant( $applicant_id ) ) {
ccgn_application_format_legal_approval( $applicant_id, $state );
}
}
function ccgn_application_user_application_page_url( $user_id ) {
return admin_url(
'admin.php?page=global-network-application&user_id='
. $user_id
);
}
////////////////////////////////////////////////////////////////////////////////
// Admin UI hooks
////////////////////////////////////////////////////////////////////////////////
function ccgn_application_users_menu () {
add_submenu_page(
null,
'Global Network Membership',
'Global Network Membership',
'ccgn_view_applications',
'global-network-application',
'ccgn_application_users_page'
);
}
function ccgn_hide_application_users_menu () {
remove_submenu_page(
'global-network-application-approval',
'ccgn-global-network-membership'
);
}
function ccgn_user_get_vouch_entry($applicant_id) {
$form_id = RGFormsModel::get_form_id(CCGN_GF_VOUCH);
$search_criteria = array();
$search_criteria['field_filters'][]
= array(
'key' => 'created_by',
'value' => get_current_user_id(),
);
$search_criteria['field_filters'][]
= array(
'key' => CCGN_GF_VOUCH_APPLICANT_ID_FIELD,
'value' => $applicant_id,
);
$get_the_entries = GFAPI::get_entries(
$form_id,
$search_criteria,
array(
array(
'key' => 'date_created',
'direction' => 'ASC',
'is_numeric' => false
)
)
);
$return = array(
'entry_id' => $get_the_entries[0]['id'],
'entry_text' => $get_the_entries[0]['4']
);
return $return;
}
// If the user is at the vouching / approval stage, link to this page
// from the user's entry in the User list page.
function ccgn_application_user_link( $actions, $user_object ) {
// Only show this if the user is at the pre-approval or vouching stages
if ( ccgn_current_user_can_see_user_application_page()
&& ccgn_user_is_new( $user_object->ID ) ) {
$actions['ccgn_application']
= '<a href="'
. ccgn_application_user_application_page_url(
$user_object->ID
)
. '">Approval</a>';
}
return $actions;
}
// Ajax function
// Executed in the UI when a MC member ask to a voucher for clarification
// @user_id : user to be notified
function ccgn_ajax_ask_voucher()
{
$user_id = $_POST['user_id'];
$applicant_id = $_POST['applicant_id'];
$current_user_ask = get_current_user_id();
if (check_ajax_referer('ask_voucher', 'sec') && (!empty($user_id))) {
ccgn_ask_email_vouching_request($applicant_id,$user_id);
//set user state to clarification of the reason to vouch applicant
$update_ask_status = array('status' => 1, 'applicant_id' => $applicant_id, 'user_id' => $user_id, 'ask_user' => $current_user_ask, 'time' => time());
update_user_meta($user_id, 'ccgn_need_to_clarify_vouch_reason_applicant_status', $update_ask_status );
update_user_meta($user_id,'ccgn_need_to_clarify_vouch_reason',1);
ccgn_ask_clarification_log_append($applicant_id,$user_id);
echo 'ok';
}
exit(0);
}
add_action('wp_ajax_nopriv_ask_voucher', 'ccgn_ajax_ask_voucher');
add_action('wp_ajax_ask_voucher', 'ccgn_ajax_ask_voucher');
function ccgn_ajax_remove_voucher()
{
$user_id = $_POST['user_id'];
$applicant_id = $_POST['applicant_id'];
if (check_ajax_referer('remove_voucher', 'sec') && (!empty($user_id))) {
$entries = ccgn_entries_referring_to_user($applicant_id,'Vouch For Applicant');
$entry_id = null;
foreach ($entries as $entry) {
if ($entry['form_id'] == 44) {
$entry_id = $entry['id'];
break;
}
}
$created_by_applicant = ccgn_entries_created_by_user($applicant_id,41);
foreach ($created_by_applicant as $form_entry) {
if ($form_entry['form_id'] == 41) {
if ($form_entry['1'] == $user_id ) {
GFAPI::update_entry_field($form_entry['id'], '1', '');
} elseif ($form_entry['2'] == $user_id) {
GFAPI::update_entry_field($form_entry['id'], '2', '');
}
}
}
GFAPI::delete_entry( $entry_id );
_ccgn_registration_user_set_stage( $applicant_id, 'update-vouchers' );
ccgn_registration_email_voucher_cannot( $applicant_id, $user_id );
echo 'ok';
}
exit(0);
}
add_action('wp_ajax_nopriv_remove_voucher', 'ccgn_ajax_remove_voucher');
add_action('wp_ajax_remove_voucher', 'ccgn_ajax_remove_voucher');
function ccgn_ajax_change_voucher()
{
$voucher_id = esc_attr($_POST['voucher_id']);
$applicant_id = esc_attr($_POST['applicant_id']);
$position = esc_attr($_POST['position']);
$new_voucher = esc_attr( $_POST['new_voucher'] );
if (check_ajax_referer('change_voucher', 'sec') && (!empty($new_voucher))) {
$form_id = RGFormsModel::get_form_id(CCGN_GF_CHOOSE_VOUCHERS);
$search_criteria = array();
$search_criteria['field_filters'][]
= array(
'key' => 'created_by',
'value' => $applicant_id,
);
$get_the_entries = GFAPI::get_entries(
$form_id,
$search_criteria,
array(
array(
'key' => 'date_created',
'direction' => 'ASC',
'is_numeric' => false
)
)
);
$entry_id = $get_the_entries[0]['id'];
$update_date = GFAPI::update_entry_field($entry_id, 'date_updated', date('Y-m-d H:m:s'));
$change_voucher_result = GFAPI::update_entry_field($entry_id, $position, $new_voucher);
if ($change_voucher_result) {
//change user state to "vouching". if we dont't do this, the applicant will not appear to the new voucher request list
_ccgn_registration_user_set_stage( $applicant_id, CCGN_APPLICATION_STATE_VOUCHING );
//send email to the new voucher
$send_mail = ccgn_registration_email_vouching_request(
$applicant_id,
$new_voucher
);
echo 'ok';
} else {
echo 'error';
}
} else {
echo 'error';
}
exit(0);
}