Proyectos de Subversion Android Microlearning - Inconcert

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 gabriel 1
package com.cesams.twogetskills.inconcert.activity;
2
 
3
import android.content.Intent;
4
import android.os.Bundle;
5
 
6
import com.cesams.twogetskills.inconcert.Constants;
7
import com.cesams.twogetskills.inconcert.fragment.QuizFailFragment;
8
import com.cesams.twogetskills.inconcert.fragment.QuizIntroFragment;
9
import com.cesams.twogetskills.inconcert.fragment.QuizMultipleAnswerFragment;
10
import com.cesams.twogetskills.inconcert.fragment.QuizPassFragment;
11
import com.cesams.twogetskills.inconcert.fragment.QuizRangeSimpleFragment;
12
import com.cesams.twogetskills.inconcert.fragment.QuizSingleAnswerFragment;
13
import com.cesams.twogetskills.inconcert.entity.Answer;
14
import com.cesams.twogetskills.inconcert.entity.Question;
15
import com.cesams.twogetskills.inconcert.entity.Quiz;
16
import com.cesams.twogetskills.inconcert.pojo.QuizResponse;
17
import com.cesams.twogetskills.inconcert.skeleton.IQuizActivity;
18
 
19
import androidx.appcompat.app.AppCompatActivity;
20
import androidx.fragment.app.Fragment;
21
import androidx.fragment.app.FragmentTransaction;
22
 
23
import android.os.CountDownTimer;
24
import android.view.View;
25
import android.widget.Button;
26
import android.widget.TextView;
27
 
28
import com.cesams.twogetskills.inconcert.R;
29
 
30
import java.text.SimpleDateFormat;
31
import java.util.ArrayList;
32
import java.util.Calendar;
33
import java.util.Date;
34
import java.util.HashMap;
35
 
36
public class QuizActivity extends AppCompatActivity implements IQuizActivity {
37
 
38
    private final static String TAG = "C2GS - QuizActivity";
39
    private final static String PREFIX_FRAG = "FRAG";
40
    private Quiz quiz;
41
    private String quizUuid;
42
    private ArrayList<Question> questions;
43
    private HashMap<String, ArrayList<Answer>> answers;
44
    private ArrayList<QuizResponse> quizResponses;
45
    private TextView textViewScore;
46
    private TextView textViewQuestion;
47
    private TextView textChronometer;
48
    private HashMap<String, Fragment> fragmentHashMap;
49
    private Button btnPrevious;
50
    private Button btnNext;
51
    private Button btnStart;
52
    private Button btnFinish;
53
    private Button btnExit;
54
    private int questionIdxActive;
55
    private int fragmentIdxActive;
56
    private int totalPoints;
57
    private CountDownTimer countDownTimer;
58
 
59
    @Override
60
    protected void onCreate(Bundle savedInstanceState) {
61
        super.onCreate(savedInstanceState);
62
        setContentView(R.layout.activity_quiz);
63
 
64
        questionIdxActive = 0;
65
        fragmentIdxActive = 0;
66
        questions = new ArrayList<>();
67
        answers = new HashMap<>();
68
        quizResponses = new ArrayList<>();
69
        fragmentHashMap = new HashMap<>();
70
 
71
        //companyUuid = getIntent().getStringExtra("companyUuid");
72
        //topicUuid = getIntent().getStringExtra("topicUuid");
73
        //capsuleUuid = getIntent().getStringExtra("capsuleUuid");
74
       // slideUuid = getIntent().getStringExtra("slideUuid");
75
        quizUuid = getIntent().getStringExtra("quizUuid");
76
        //userUuid = getIntent().getStringExtra("userUuid");
77
 
78
        quiz = new Quiz();
79
        quiz.setCompanyUuid(getIntent().getStringExtra("quiz_company_uuid"));
80
        quiz.setUuid(getIntent().getStringExtra("quiz_uuid"));
81
        quiz.setName(getIntent().getStringExtra("quiz_name"));
82
        quiz.setText(getIntent().getStringExtra("quiz_text"));
83
        quiz.setFailed(getIntent().getStringExtra("quiz_failed"));
84
        quiz.setPoints(getIntent().getIntExtra("quiz_points", 0));
85
        quiz.setMinimumPointsRequired(getIntent().getIntExtra("quiz_minimum_points_required", 0));
86
        quiz.setMaxTime(getIntent().getIntExtra("quiz_max_time", 0));
87
 
88
 
89
 
90
        int maxAnswer = 0;
91
        int maxQuestion = getIntent().getIntExtra("questions", 0);
92
        Question question;
93
        Answer answer;
94
        ArrayList<Answer> questionAnswers;
95
        for(int i = 1; i <= maxQuestion; i++){
96
            question = new Question();
97
            question.setQuizUuid(quizUuid);
98
            question.setUuid(getIntent().getStringExtra("question" + i + "_uuid"));
99
            question.setText(getIntent().getStringExtra("question" + i + "_text"));
100
            question.setPoints(getIntent().getIntExtra("question" + i + "_points", 0));
101
            question.setMaxlength(getIntent().getIntExtra("question" + i + "_max_length", 0));
102
            question.setType(getIntent().getStringExtra("question" + i + "_type"));
103
 
104
            questions.add(question);
105
 
106
            maxAnswer = getIntent().getIntExtra("question" + i + "_answers", 0);
107
 
108
 
109
            questionAnswers = new ArrayList<>();
110
            for(int j = 1; j <= maxAnswer; j++ ) {
111
                answer = new Answer();
112
                answer.setQuestionUuid(question.getUuid());
113
                answer.setUuid(getIntent().getStringExtra("question" + i + "_answer_uuid" + j));
114
                answer.setText(getIntent().getStringExtra("question" + i + "_answer_text" + j));
115
                answer.setPoints(getIntent().getIntExtra("question" + i + "_answer_points" + j, 0));
116
                answer.setCorrect(getIntent().getStringExtra("question" + i + "_answer_correct" + j));
117
 
118
                questionAnswers.add(answer);
119
            }
120
 
121
 
122
            answers.put(question.getUuid(), questionAnswers);
123
        }
124
 
125
 
126
        textViewScore = (TextView) findViewById(R.id.activity_quiz_textview_score);
127
        textViewScore.setText("0");
128
 
129
        textViewQuestion = (TextView) findViewById(R.id.activity_quiz_textview_question_count);
130
        textViewQuestion.setText(String.valueOf(questions.size()));
131
 
132
        textChronometer = (TextView) findViewById(R.id.activity_quiz_textview_chronometer);
133
        textChronometer.setText("00:00");
134
 
135
        btnPrevious = (Button) findViewById(R.id.activity_quiz_button_previous);
136
        btnPrevious.setVisibility(View.INVISIBLE);
137
 
138
        btnPrevious.setOnClickListener(new View.OnClickListener() {
139
            @Override
140
            public void onClick(View view) {
141
                if (questionIdxActive > 0) {
142
                    questionIdxActive--;
143
                }
144
                showQuestions();
145
            }
146
        });
147
 
148
        btnNext = (Button) findViewById(R.id.activity_quiz_button_next);
149
        btnNext.setVisibility(View.INVISIBLE);
150
        btnNext.setOnClickListener(new View.OnClickListener() {
151
            @Override
152
            public void onClick(View view) {
153
                if (questionIdxActive < (questions.size() - 1)) {
154
                    questionIdxActive++;
155
                    showQuestions();
156
                }
157
            }
158
        });
159
 
160
        btnExit = (Button) findViewById(R.id.activity_quiz_button_exit);
161
        btnExit.setVisibility(View.INVISIBLE);
162
        btnExit.setOnClickListener(new View.OnClickListener() {
163
            @Override
164
            public void onClick(View view) {
165
               if(totalPoints >= quiz.getMinimumPointsRequired()) {
166
                   exitQuizPass();
167
               } else {
168
                   exitQuizFail();
169
               }
170
            }
171
        });
172
 
173
        btnStart = (Button) findViewById(R.id.activity_quiz_button_start);
174
        btnStart.setVisibility(View.VISIBLE);
175
        btnStart.setOnClickListener(new View.OnClickListener() {
176
            @Override
177
            public void onClick(View view) {
178
                Calendar calendar = Calendar.getInstance();
179
                Date date = calendar.getTime();
180
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
181
                String dateOn = simpleDateFormat.format(date);
182
 
183
                long millisInFuture = quiz.getMaxTime() * 60000;
184
 
185
                countDownTimer = new CountDownTimer(millisInFuture, 1000) {
186
 
187
                    public void onTick(long millisUntilFinished) {
188
                        long minutes = millisUntilFinished / 60000;
189
                        long seconds = (millisUntilFinished - (minutes * 60000)) / 1000;
190
 
191
                        String sMinutes = minutes < 10 ? "0" + minutes : String.valueOf(minutes);
192
                        String sSeconds = seconds < 10 ? "0" + seconds : String.valueOf(seconds);
193
 
194
                        textChronometer.setText(sMinutes + ":" + sSeconds);
195
 
196
                    }
197
 
198
                    public void onFinish() {
199
                        textChronometer.setText("--:--");
200
                        finishQuiz();
201
                    }
202
                }.start();
203
 
204
                btnStart.setVisibility(View.INVISIBLE);
205
 
206
                if (questions.size() == 1) {
207
                    btnFinish.setVisibility(View.VISIBLE);
208
                } else {
209
                    btnNext.setVisibility(View.VISIBLE);
210
                }
211
 
212
                showQuestions();
213
            }
214
 
215
        });
216
 
217
        btnFinish = (Button) findViewById(R.id.activity_quiz_button_finish);
218
        btnFinish.setVisibility(View.INVISIBLE);
219
        btnFinish.setOnClickListener(new View.OnClickListener() {
220
            @Override
221
            public void onClick(View view) {
222
                finishQuiz();
223
 
224
            }
225
        });
226
 
227
        invokeFragment(Constants.IDX_QUIZ_FRAGMENT_INTRO);
228
 
229
 
230
    }
231
 
232
    @Override
233
    public int getTotalPoints() {
234
        return totalPoints;
235
    }
236
 
237
 
238
 
239
    @Override
240
    public void exitQuizFail()
241
    {
242
        Intent intent = new Intent();
243
        intent.putExtra("completed", false);
244
        intent.putExtra("requestCode", Constants.REQUEST_CODE_QUIZ);
245
        setResult(RESULT_CANCELED, intent);
246
        finish();
247
    }
248
 
249
    @Override
250
    public void exitQuizPass()
251
    {
252
        Intent intent = new Intent();
253
        intent.putExtra("completed", true);
254
        intent.putExtra("requestCode", Constants.REQUEST_CODE_QUIZ);
255
        setResult(RESULT_OK, intent);
256
        finish();
257
    }
258
 
259
    public void finishQuiz()
260
    {
261
        if(countDownTimer != null) {
262
            countDownTimer.cancel();
263
        }
264
        textChronometer.setText("--:--");
265
 
266
        totalPoints = 0;
267
        for(int i = 0; i < quizResponses.size(); i++)
268
        {
269
            totalPoints += quizResponses.get(i).getPoints();
270
        }
271
 
272
        textViewScore.setText(totalPoints + " / " + quiz.getPoints());
273
 
274
        btnFinish.setVisibility(View.INVISIBLE);
275
        btnExit.setVisibility(View.VISIBLE);
276
        if(totalPoints >= quiz.getMinimumPointsRequired()) {
277
            //Pass
278
 
279
            invokeFragment(Constants.IDX_QUIZ_FRAGMENT_PASS);
280
        } else {
281
            //Fail
282
            invokeFragment(Constants.IDX_QUIZ_FRAGMENT_FAIL);
283
        }
284
    }
285
 
286
    public void showQuestions()
287
    {
288
        btnStart.setVisibility(View.INVISIBLE);
289
        btnFinish.setVisibility(View.INVISIBLE);
290
        btnNext.setVisibility(View.INVISIBLE);
291
        btnPrevious.setVisibility(View.INVISIBLE);
292
 
293
        if(questionIdxActive == 0) {
294
 
295
            if(questions.size() > 1) {
296
                btnNext.setVisibility(View.VISIBLE);
297
            } else {
298
                btnFinish.setVisibility(View.VISIBLE);
299
            }
300
        } else {
301
            if(questionIdxActive == (questions.size() - 1)) {
302
                btnFinish.setVisibility(View.VISIBLE);
303
            } else {
304
                btnNext.setVisibility(View.VISIBLE);
305
            }
306
        }
307
 
308
        int score = 0;
309
        for(int i = 0; i < quizResponses.size(); i++)
310
        {
311
            score += quizResponses.get(i).getPoints();
312
        }
313
 
314
        textViewQuestion.setText((1 + questionIdxActive) + " / " + questions.size()  );
315
        textViewScore.setText(String.valueOf(score));
316
 
317
 
318
        Question question = questions.get(questionIdxActive);
319
        if(question.getType().equals(Constants.QUESTION_TYPE_RANGE_1_10)) {
320
            invokeFragment(Constants.IDX_QUIZ_FRAGMENT_QUESTION_RANGE_SIMPLE);
321
        } else if(question.getType().equals(Constants.QUESTION_TYPE_RANGE_1_6)) {
322
            invokeFragment(Constants.IDX_QUIZ_FRAGMENT_QUESTION_RANGE_SIMPLE);
323
        } else if(question.getType().equals(Constants.QUESTION_TYPE_RANGE_1_5)) {
324
            invokeFragment(Constants.IDX_QUIZ_FRAGMENT_QUESTION_RANGE_SIMPLE);
325
        } else if(question.getType().equals(Constants.QUESTION_TYPE_SINGLE_SELECTION)) {
326
            invokeFragment(Constants.IDX_QUIZ_FRAGMENT_QUESTION_SIMPLE);
327
        } else if(question.getType().equals(Constants.QUESTION_TYPE_MULTIPLE_SELECTION)) {
328
            invokeFragment(Constants.IDX_QUIZ_FRAGMENT_QUESTION_MULTIPLE);
329
        }
330
 
331
    }
332
 
333
 
334
    @Override
335
    public void onBackPressed() {
336
 
337
        return;
338
          /*
339
        Intent intent = new Intent();
340
        setResult(RESULT_CANCELED, intent);
341
        finish();*/
342
    }
343
 
344
    @Override
345
    protected void onDestroy() {
346
        super.onDestroy();
347
 
348
 
349
    }
350
 
351
 
352
    @Override
353
    public Quiz getQuiz()
354
    {
355
        return quiz;
356
    }
357
 
358
    @Override
359
    public ArrayList<Question> getQuestions()
360
    {
361
        return questions;
362
    }
363
 
364
    @Override
365
    public ArrayList<Answer> getAnswers(String questionUuid)
366
    {
367
        if(answers.containsKey(questionUuid)) {
368
            return answers.get(questionUuid);
369
        } else {
370
            return null;
371
        }
372
 
373
 
374
    }
375
 
376
    @Override
377
    public ArrayList<QuizResponse> getQuizResponses()
378
    {
379
        return quizResponses;
380
    }
381
 
382
    @Override
383
    public Question getQuestionActive()
384
    {
385
        return questions.get(questionIdxActive);
386
    }
387
 
388
 
389
    public void invokeFragment(int fragmentIdxActiveNuevo)
390
    {
391
        String fragmentKeyActual 	= PREFIX_FRAG + fragmentIdxActive;
392
        String fragmentKeyNuevo 	= PREFIX_FRAG + fragmentIdxActiveNuevo;
393
        fragmentIdxActive = fragmentIdxActiveNuevo;
394
 
395
        Fragment fragment;
396
       // if(!fragmentKeyActual.equalsIgnoreCase(fragmentKeyNuevo)) {
397
            if(fragmentHashMap.containsKey(fragmentKeyActual)) {
398
                fragment = fragmentHashMap.get(fragmentKeyActual);
399
                if(fragment != null) {
400
                    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
401
                    fragmentTransaction.hide(fragment);
402
                    fragmentTransaction.commit();
403
                }
404
            }
405
        //}
406
 
407
        boolean add = false;
408
        fragment = null;
409
 
410
 
411
        switch(fragmentIdxActiveNuevo) {
412
 
413
            case Constants.IDX_QUIZ_FRAGMENT_QUESTION_RANGE_SIMPLE :
414
                if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
415
                    fragment = fragmentHashMap.get(fragmentKeyNuevo);
416
                } else {
417
                    add = true;
418
                    fragment = new QuizRangeSimpleFragment(this);
419
                }
420
                break;
421
 
422
            case Constants.IDX_QUIZ_FRAGMENT_QUESTION_SIMPLE :
423
                if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
424
                    fragment = fragmentHashMap.get(fragmentKeyNuevo);
425
                } else {
426
                    add = true;
427
                    fragment = new QuizSingleAnswerFragment(this);
428
                }
429
                break;
430
 
431
            case Constants.IDX_QUIZ_FRAGMENT_QUESTION_MULTIPLE :
432
                if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
433
                    fragment = fragmentHashMap.get(fragmentKeyNuevo);
434
                } else {
435
                    add = true;
436
                    fragment = new QuizMultipleAnswerFragment(this);
437
                }
438
                break;
439
 
440
            case Constants.IDX_QUIZ_FRAGMENT_PASS :
441
                if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
442
                    fragment = fragmentHashMap.get(fragmentKeyNuevo);
443
                } else {
444
                    add = true;
445
                    fragment = new QuizPassFragment(this);
446
                }
447
                break;
448
 
449
            case Constants.IDX_QUIZ_FRAGMENT_FAIL :
450
                if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
451
                    fragment = fragmentHashMap.get(fragmentKeyNuevo);
452
                } else {
453
                    add = true;
454
                    fragment = new QuizFailFragment(this);
455
                }
456
                break;
457
 
458
            default :
459
                if(fragmentHashMap.containsKey(fragmentKeyNuevo)) {
460
                    fragment = fragmentHashMap.get(fragmentKeyNuevo);
461
                } else {
462
                    add = true;
463
                    fragment = new QuizIntroFragment();
464
                }
465
                break;
466
 
467
 
468
        }
469
 
470
        if(add) {
471
            fragmentHashMap.put(fragmentKeyNuevo, fragment);
472
 
473
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
474
            fragmentTransaction.add(R.id.activity_quiz_fragment_container, fragment, fragmentKeyNuevo);
475
            fragmentTransaction.commit();
476
        }
477
 
478
 
479
        if(fragment != null) {
480
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
481
            fragmentTransaction.show(fragment);
482
            fragmentTransaction.commit();
483
        }
484
 
485
    }
486
 
487
 
488
}