liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
/* Controller associated with pages rendering the survey.
 * Used by SurveyPage, ResultsPage, TakeSurvey
 */
global virtual with sharing class ViewSurveyController {
    public String  surveyName                   { get; set; }
    public String  surveyHeader                 { get; set; }
    public String  surveyId                     { get;
        set {
            this.surveyId = value;
            init();
        }
    }
    public String renderSurveyPreview           { get; set; }
    public String questionName                  { get; set; }
    public String questionType                  { get; set; }
    public Boolean questionRequired             { get; set; }
    public List<SelectOption> singleOptions     { get; set; }
    public List<question> allQuestions          { get; set; }
    public List<String> responses               { get; set; }
    public Integer allQuestionsSize             { get; set; }
    public String templateURL                   { get; set; }
    public String surveyThankYouText            { get; set; }
    public String surveyContainerCss            { get; set; }
    public String surveyThankYouURL             { get; set; }
    public String caseId                        { get; set; }
    public String contactId                     { get; set; }
    public String anonymousAnswer               { get; set; }
    public List<SelectOption> anonymousOrUser   { get; set; }
    public Boolean isInternal                   { get; set; }
    public String baseURL                       { get; set; }
    
    public String userId                        { get; set; }
    public String userName                      { get; set; }
    public String surveyTakerId                 { get; set; }
    public Boolean thankYouRendered             { get; set; }
    public List<String> newOrder                { get; set; }
    
    private String noSave;
    
//------------------------------------------------------------------------------//  
    /* Retrieves the list of questions, the survey name, after retrieving the 
       necessary parameters from the url.
    */
    public ViewSurveyController(ApexPages.StandardController stdController) {
        // Get url parameters
        surveyId = Apexpages.currentPage().getParameters().get('id');
        caseId   = Apexpages.currentPage().getParameters().get('caId');
        contactId = Apexpages.currentPage().getParameters().get('cId');
        noSave = Apexpages.currentPage().getParameters().get('noSave');
    
        if (caseId == null || caseId.length() < 5) {
            caseId = 'none';
        }
        if (contactId == null || contactId.length() < 5) {
            contactId = 'none';
        }           
        // By default the preview is not showing up
        renderSurveyPreview = 'false';
 
        init();
    } 
    
    public ViewSurveyController(viewShareSurveyComponentController controller) {
        surveyId = Apexpages.currentPage().getParameters().get('id');
        caseId   = Apexpages.currentPage().getParameters().get('caId');
        contactId = Apexpages.currentPage().getParameters().get('cId'); 
        if (caseId == null || caseId.length() < 5) {
            caseId = 'none';
        }
        if (contactId == null || contactId.length() < 5) {
            contactId = 'none';
        }       
        // By default the preview is not showing up
        renderSurveyPreview = 'false';
        init();
    }
    
    public void init() {
        if (surveyId != null) { 
            // Retrieve all necessary information to be displayed on the page
            allQuestions = new List<question>();
            setupQuestionList();
            setSurveyNameAndThankYou(surveyId);
            anonymousOrUser = new List<SelectOption>();
            anonymousOrUser.add(new SelectOption('Anonymous',System.Label.LABS_SF_Anonymous));
            anonymousOrUser.add(new SelectOption('User','User ' + UserInfo.getFirstName() + ' ' + UserInfo.getLastName()));
            anonymousAnswer = 'Anonymous';
            isInternal =true;
            newOrder = new List<String>();
            String urlBase = URL.getSalesforceBaseUrl().toExternalForm();
            baseURL = urlBase;
            
            userId = UserInfo.getUserId();
            userName = UserInfo.getName();
            
            String profileId = UserInfo.getProfileId();
            try {
                Profile p = [select Id, UserType from Profile where Id=:profileId];
                if (p.UserType == 'Guest')
                {
                    isInternal = false;
                }
                else
                {
                    isInternal = true;
                }
            } catch (Exception e) {
                isInternal = false;
            }
            
            thankYouRendered=false;
        }
    }
 
//------------------------------------------------------------------------------//  
    private static Testmethod void testViewSurveyController() {
        SurveyTestingUtil tu = new SurveyTestingUtil();
        Apexpages.currentPage().getParameters().put('id',tu.surveyId);  
        Apexpages.Standardcontroller stc;   
        ViewSurveyController vsc = new ViewSurveyController(stc);
        vsc.init();
        System.assert(vsc.allQuestionsSize == 4);
        System.assert(tu.surveyId != null);
        
        
        vsc.submitResults();
        for (question q : vsc.allQuestions)
        {
            q.selectedOption = String.valueof(2);
            q.choices = String.valueof(2);
            q.selectedOptions = new List<String>();
            q.selectedOptions.add(String.valueof(2));
            vsc.submitResults();            
        }
        System.assertEquals(true, vsc.thankYouRendered);
        
        
        //test something
    }
//----------------------------------------------------------------------------//    
    
    /* Called during the setup of the page. 
       Retrieve questions and responses from DB and inserts them in 2 lists. */
    public Integer setupQuestionList() {
        getAQuestion();
        return allQuestions.size();
    }
    
    
    /** Sets the survey's name variable
     *  param: sID   The survey ID as specified in the DB
     */
    public void setSurveyNameAndThankYou(String sId) {
        Survey__c s = [SELECT Name, Id, URL__c, Thank_You_Text__c, thankYouText__c, thankYouLink__c, Survey_Header__c, Survey_Container_CSS__c FROM Survey__c WHERE Id =:sId];
        surveyName = s.Name;
        surveyHeader = s.Survey_Header__c;
        templateURL = s.URL__c+'id='+sId;//+'&cId={!Contact.Id}'+'&caId='+'{!Case.id}';
        surveyThankYouText = s.Thank_You_Text__c;
        if (surveyThankYouText == null)
        {
            surveyThankYouText = System.Label.LABS_SF_Survey_Submitted_Thank_you;
        }
        surveyThankYouURL = s.thankYouLink__c;
        surveyContainerCss = s.Survey_Container_CSS__c;
    }
    
//------------------------------------------------------------------------------//   
    public Pagereference updateSurveyName() {
        Survey__c s = [SELECT Name, Id, URL__c, thankYouText__c, thankYouLink__c FROM Survey__c WHERE Id =:surveyId];
        s.Name = surveyName;
        try {
            update s;
        } catch (Exception e) {
            Apexpages.addMessages(e);
        }
        return null;
    }
 
    private static Testmethod void testUpdateSurveyName() {
        SurveyTestingUtil tu = new SurveyTestingUtil();
        Apexpages.currentPage().getParameters().put('id',tu.surveyId);
        Apexpages.Standardcontroller stc; 
        ViewSurveyController vsc = new ViewSurveyController(stc);
        vsc.surveyName = 'new name';
        system.assert(vsc.updateSurveyName() == null);
    }
//------------------------------------------------------------------------------//
    public Pagereference updateSurveyThankYouAndLink() {
        Survey__c s = [SELECT Name, Id, URL__c, thankYouText__c, thankYouLink__c FROM Survey__c WHERE Id =:surveyId];
        s.thankYouText__c = surveyThankYouText;
        s.thankYouLink__c = surveyThankYouURL;
        try {
            update s;
        } catch(Exception e) {
            Apexpages.addMessages(e);
        }
        return null;
    }
  
    private static Testmethod void testupdateSurveyThankYouAndLink() {
        SurveyTestingUtil tu = new SurveyTestingUtil();
        Apexpages.currentPage().getParameters().put('id',tu.surveyId);
        Apexpages.Standardcontroller stc; 
        ViewSurveyController vsc = new ViewSurveyController(stc);
        vsc.surveyThankYouText = 'new stuff';
        vsc.surveyThankYouURL = 'more new stff';
        system.assert(vsc.updateSurveyThankYouAndLink() == null);
    }
//------------------------------------------------------------------------------//
    /** When requested from the page - when the user clicks on 'Update Order' -
      this function will reorganize the list so that it is displayed in the new order
    */
    public Pagereference refreshQuestionList() {
        setupQuestionList();
        return null;
    }
//------------------------------------------------------------------------------//
 
//------------------------------------------------------------------------------//
    private static boolean checkRequired(String response, Survey_Question__c question) {
        if (question.Required__c == true) {
            if (response == null || response =='NO RESPONSE') {
                return false;
            }
        }
        return true;
    } 
 
    /** Redirects the page that displays the detailed results of the survey, 
       from all users who took the survey.
     */
    public PageReference resultPage() {
        return new PageReference('/apex/ResultsPage?id='+surveyId);
    }
 
//------------------------------------------------------------------------------//  
 
//------------------------------------------------------------------------------//  
    /** 
     */
    public List<String> getResponses() {
        List<SurveyQuestionResponse__c> qr = [Select Survey_Question__c, SurveyTaker__c, Response__c, Name From SurveyQuestionResponse__c limit 100];
        List<String> resp = new List<String>();
        for (SurveyQuestionResponse__c r : qr) {
            resp.add(r.Response__c);
        }
        
        return resp;
    }  
 
    /** Class: question
     *  Retrieves the question information and puts it in the question object
     */      
    public class question {
        public Survey_Question__c sq           { get; set; }
        public String   name                   { get; set; }
        public String   id                     { get; set; }
        public String   question               { get; set; }
        public String   orderNumber            { get; set; }
        public String   choices                { get; set; }
        public String   minLabel               { get; set; }
        public String   maxLabel               { get; set; }
        public String   selectedOption         { get; set; }
        public List<String> selectedOptions    { get; set; }
        public List<SelectOption> singleOptions{ get; set; }
        public List<SelectOption> multiOptions { get; set; }
        public List<SelectOption> rowOptions   { get; set; }
        public List<SelectOption> pickOptions  { get; set; }
        public List<SelectOption> scaleOptions { get; set; }
        
        public Boolean  required               { get; set; }
        public String   questionType           { get; set; }    
        public String   surveyName             { get; set; }
        public String   renderFreeText         { get; set; }
        public String   renderSelectRadio      { get; set; }
        public String   renderSelectCheckboxes { get; set; } 
        public String   renderSelectRow        { get; set; }
        public String   renderSingleText       { get; set; }
        public String   renderPicklist         { get; set; }
        public String   renderScale            { get; set; }
        public String   renderMail             { get; set; }
        public String   renderPhone            { get; set; }
        
        public List<String> responses          { get; set; }
        //public String   singleOptionsForChart  { get; set; }
        public String   qResultsForChart       { get; set; } 
        public List<String> strList            { get; set; } // The question's option as a list of string
        public List<Integer> resultsCounts     { get; set; } // The count of each response to a question's choices
        public boolean  noData                 { get; set; }
          
        /** Fills up the question object
         *  param:    Survey_Question__c 
         */
        public question(Survey_Question__c sq) {
            this.sq = sq;
            name = sq.Name;
            id = sq.Id;
            question = sq.Question__c;
            orderNumber = String.valueOf(sq.OrderNumber__c+1);
            choices = sq.Choices__c;
            minLabel = sq.MinLabel__c;
            maxLabel = sq.MaxLabel__c;
            required = sq.Required__c;
            questionType = sq.Type__c;
            //singleOptionsForChart = ' ';
            selectedOption = '';
            selectedOptions = new List<String>();
            clearAllRenderFlg();
            
            if (sq.Type__c == 'Single Select--Vertical') {
                renderSelectRadio = 'true';
                singleOptions = stringToSelectOptions(choices, false);
            }
            else if (sq.Type__c == 'Multi-Select--Vertical') {
                renderSelectCheckboxes = 'true';
                multiOptions = stringToSelectOptions(choices, false);
            }
            else if (sq.Type__c == 'Single Select--Horizontal') {
                renderSelectRow = 'true';
                rowOptions = stringToSelectOptions(choices, false);
            }
            else if (sq.Type__c == 'Free Text') {
                renderFreeText='true';
            }
            else if (sq.Type__c == 'Picklist') {
                renderPicklist = 'true';
                pickOptions = stringToSelectOptions(choices, true);
            }
            else if (sq.Type__c == 'Single Text') {
                renderSingleText = 'true';
            }
            else if (sq.Type__c == 'Scale') {
                renderScale = 'true';
                scaleOptions = stringToSelectOptions(choices, false);
            }
            else if (sq.Type__c == 'Mail') {
                renderMail = 'true';
            }
            else if (sq.Type__c == 'Phone') {
                renderPhone = 'true';
            }
            //responses= getResponses();
        }
        
        private void clearAllRenderFlg() {
            renderSelectRadio      = 'false';
            renderSelectCheckboxes = 'false';
            renderSelectRow        = 'false';
            renderPickList         = 'false';
            renderFreeText         = 'false';
            renderSingleText       = 'false';
            renderScale            = 'false';
            renderMail             = 'false';
            renderPhone            = 'false';
        }
        
        /** Splits up the string as given by the user and adds each option
         *  to a list to be displayed as option on the Visualforce page
         *  param: str   String as submitted by the user
         *  returns the List of SelectOption for the visualforce page
         */  
        private List<SelectOption> stringToSelectOptions(String str, Boolean isPicklist) {
            if (String.isBlank(str) == true) {
                return new List<SelectOption>();
            }
            strList = str.split('\\r|\n');
 
            List<SelectOption> returnVal = new List<SelectOption>();
            if (isPicklist) {
                returnVal.add(new SelectOption('', System.Label.LABS_SF_SELECTTYPE));
            }
            
            for(String s: strList) {
                if (String.isBlank(s) == false) {
                    returnVal.add(new SelectOption(s,s));
                    //if (s != 'null' && s!= null) {
                        //String sBis = s.replace(' ', '%20');
                        //singleOptionsForChart += s.trim()+'|';
                    //}
                }
            }
            //singleOptionsForChart = singleOptionsForChart.substring(0, singleOptionsForChart.length()-1);
            return returnVal;
        } 
    }
    
    /** Fills up the List of questions to be displayed on the Visualforce page
     */   
    public List<question> getAQuestion() {
        
        List<Survey_Question__c> allQuestionsObject = 
                [Select s.Type__c, s.Id, s.Survey__c, s.Required__c, s.Question__c, 
                        s.OrderNumber__c, s.Name, s.Choices__c,
                        s.MinLabel__c, s.MaxLabel__c, s.Mail__c, s.Phone__c
                   From Survey_Question__c s 
                  WHERE s.Survey__c =: surveyId ORDER BY s.OrderNumber__c];
        allQuestions = new List<question>();
        
        Double old_OrderNumber = 0;
        Double new_OrderNumber;
        Double difference = 0;
        /* Make sure that the order number follow each other (after deleting a question, orders might not do so) */
        for (Survey_Question__c q : allQuestionsObject) { 
            new_OrderNumber = q.OrderNumber__c;
            difference = new_OrderNumber - old_OrderNumber - 1;
            if (difference > 0) {
                Double dd = double.valueOf(difference);
                Integer newOrderInt = dd.intValue();
                q.OrderNumber__c -= Integer.valueOf(newOrderInt); 
            }
            old_OrderNumber = q.OrderNumber__c;
            question theQ = new question(q);
            allQuestions.add(theQ);
        }
        allQuestionsSize = allQuestions.size();
        return allQuestions;
    }
    
    public void submitResults() {
        // なにもしない
        if (noSave == '1') {
            thankYouRendered=true;
            return;
        }
        
        List <SurveyQuestionResponse__c> sqrList = new List<SurveyQuestionResponse__c>();
        
        for (question q : allQuestions)
        {
            SurveyQuestionResponse__c sqr = new SurveyQuestionResponse__c();
            if (q.renderSelectRadio == 'true')
            {
                if (q.required && (String.isBlank(q.selectedOption)))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                sqr.Response__c = q.selectedOption;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderFreeText == 'true') {
                if (q.required && q.choices == '')
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                sqr.Response__c = q.choices;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderSelectCheckboxes == 'true') {
                if (q.required && (q.selectedOptions == null || q.selectedOptions.size() == 0))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                
                for (String opt : q.selectedOptions)
                {
                    sqr = new SurveyQuestionResponse__c();
                    sqr.Response__c = opt;
                    sqr.Survey_Question__c = q.Id;
                    sqrList.add(sqr);
                }
            }
            else if (q.renderSelectRow == 'true') {
                if (q.required && (String.isBlank(q.selectedOption)))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                sqr.Response__c = q.selectedOption;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderSingleText == 'true') {
                if (q.required && q.choices == '')
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                sqr.Response__c = q.choices;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderPicklist == 'true') {
                if (q.required && (String.isBlank(q.selectedOption)))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                sqr.Response__c = q.selectedOption;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderScale == 'true') {
                if (q.required && (String.isBlank(q.selectedOption)))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                sqr.Response__c = q.selectedOption;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderMail == 'true') {
                if (q.required && String.isBlank(q.sq.Mail__c))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                sqr.Response__c = q.sq.Mail__c;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
            else if (q.renderPhone == 'true') {
                if (q.required && String.isBlank(q.sq.Phone__c))
                {
                    Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_EnterRequiredFields));
                    return;
                }
                if (Pattern.matches('^[+]{0,1}[-0-9 #\\*\\(\\)]{1,30}$', q.sq.Phone__c) == false) {
                    q.sq.Phone__c.addError(System.Label.LABS_SF_WrongPhone);
                    return;
                }
                sqr.Response__c = q.sq.Phone__c;
                sqr.Survey_Question__c = q.Id;
                sqrList.add(sqr);
            }
        }
        if (AddSurveyTaker()) {
            for (SurveyQuestionResponse__c sqr : sqrList) {
                sqr.SurveyTaker__c = surveyTakerId;
            }
            insert sqrList;
            thankYouRendered=true;
        }
    }
    
    private Boolean AddSurveyTaker() {
        String userId;
        
        if (surveyId == null)
        {
            return false;
        }
        if (caseId == null || caseId.toUpperCase() == 'NONE'|| caseId.length() < 5) {
            caseId = null;
        }
        if (contactId == null || contactId.toUpperCase() == 'NONE'|| contactId.length() < 5) {
            contactId = null;
        }
        if (anonymousAnswer != 'Anonymous') {
            userId = UserInfo.getUserId();
        }
        else {
            userId = null;
        }
        
        if (anonymousAnswer != 'Anonymous' && (contactId != null || caseId != null)) {
            List<SurveyTaker__c> check = [Select Contact__c, Survey__c, Case__c, User__c From SurveyTaker__c Where Contact__c=:contactId and Survey__c=:surveyId and Case__c = :caseId and User__c=:UserId];
            if (check != null && check.size() > 0) {
                Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.LABS_SF_You_have_already_taken_this_survey));
                return false;       
            }
        }
        
        
        SurveyTaker__c st = new SurveyTaker__c();
        st.Contact__c = contactId;
        st.Survey__c = surveyId;
        st.Taken__c = 'false';
        st.Case__c = caseId;
        st.User__c = userId;
        insert st;  
        surveyTakerId = st.Id;
        return true;
    }
}