Search in sources :

Example 6 with QuizSubmissionQuestion

use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.

the class QuizQuestionDialog method onActivityCreated.

// /////////////////////////////////////////////////////////////////////////
// LifeCycle
// /////////////////////////////////////////////////////////////////////////
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ArrayList<Long> questionIds = (ArrayList<Long>) getArguments().getSerializable(Const.QUIZ_QUESTION_IDS);
    final ArrayList<QuizSubmissionQuestion> submissionQuestions = getArguments().getParcelableArrayList(Const.QUIZ_QUESTIONS);
    Course course = getArguments().getParcelable(Const.COURSE);
    ArrayList<QuizSubmissionQuestion> flaggedQuestions = new ArrayList<>();
    boolean hasFlagged;
    hasFlagged = false;
    for (QuizSubmissionQuestion quizSubmissionQuestion : submissionQuestions) {
        if (quizSubmissionQuestion.isFlagged()) {
            hasFlagged = true;
            flaggedQuestions.add(quizSubmissionQuestion);
        }
    }
    adapter = new QuestionAdapter(flaggedQuestions, submissionQuestions, questionIds, course, hasFlagged);
    listView.setAdapter(adapter);
    // expand all the groups
    for (int i = 0; i < adapter.getGroupCount(); i++) {
        listView.expandGroup(i);
    }
    listView.setGroupIndicator(null);
    listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
            return true;
        }
    });
    listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
            // dismiss the dialog
            dismiss();
            // scroll to the correct index (position - 1). We passed the layoutManager into the constructor so we can use it here
            int position = submissionQuestions.indexOf(adapter.getChild(groupPosition, childPosition));
            layoutManager.scrollToPosition(position);
            return true;
        }
    });
}
Also used : ArrayList(java.util.ArrayList) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) ExpandableListView(android.widget.ExpandableListView) SuppressLint(android.annotation.SuppressLint) QuizSubmissionQuestion(com.instructure.canvasapi2.models.QuizSubmissionQuestion) Course(com.instructure.canvasapi2.models.Course) ExpandableListView(android.widget.ExpandableListView)

Example 7 with QuizSubmissionQuestion

use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.

the class QuizMatchingBinder method setPreviouslySelectedAnswer.

private static void setPreviouslySelectedAnswer(QuizSubmissionQuestion quizSubmissionQuestion, QuizSubmissionAnswer answer, Spinner spinner, ArrayList<QuizSubmissionMatch> list) {
    if (quizSubmissionQuestion.getAnswer() != null) {
        // the api returns an ArrayList of LinkedTreeMaps
        for (LinkedTreeMap<String, String> map : ((ArrayList<LinkedTreeMap<String, String>>) quizSubmissionQuestion.getAnswer())) {
            int answerId = Integer.parseInt(map.get(Const.QUIZ_ANSWER_ID));
            if (answerId == answer.getId()) {
                if (map.get(Const.QUIZ_MATCH_ID) != null && !map.get(Const.QUIZ_MATCH_ID).equals("null")) {
                    int matchId = Integer.parseInt(map.get(Const.QUIZ_MATCH_ID));
                    // now see if we have a match in the list of matches
                    int listIndex = 0;
                    for (QuizSubmissionMatch match : list) {
                        if (match.getId() == matchId) {
                            spinner.setSelection(listIndex);
                            break;
                        }
                        listIndex++;
                    }
                }
            }
        }
    }
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) QuizSubmissionMatch(com.instructure.canvasapi2.models.QuizSubmissionMatch)

Example 8 with QuizSubmissionQuestion

use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.

the class QuizMultiChoiceBinder method bind.

public static void bind(final QuizMultiChoiceViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int position, final Context context, final CanvasWebView.CanvasEmbeddedWebViewCallback embeddedWebViewCallback, final CanvasWebView.CanvasWebViewClientCallback webViewClientCallback) {
    if (holder == null) {
        return;
    }
    holder.question.loadUrl("about:blank");
    holder.question.setBackgroundColor(Color.TRANSPARENT);
    holder.question.setCanvasWebViewClientCallback(webViewClientCallback);
    if (context instanceof Activity) {
        holder.question.addJavascriptInterface(new WebAppInterface(((Activity) context), holder.question), "MyApp");
    } else if (context instanceof ContextThemeWrapper) {
        holder.question.addJavascriptInterface(new WebAppInterface((Activity) (((ContextThemeWrapper) context).getBaseContext()), holder.question), "MyApp");
    }
    holder.question.formatHTML(quizSubmissionQuestion.getQuestionText(), "");
    holder.question.setCanvasEmbeddedWebViewCallback(embeddedWebViewCallback);
    holder.questionNumber.setText(context.getString(R.string.question) + " " + (position + 1));
    holder.questionId = quizSubmissionQuestion.getId();
    LayoutInflater inflater = LayoutInflater.from(context);
    // are any in there
    if (holder.answerContainer.getChildCount() > 0) {
        holder.answerContainer.removeAllViews();
    }
    // add answers to the answer container
    int index = 0;
    for (final QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
        final LinearLayout answerWrapper = (LinearLayout) inflater.inflate(R.layout.quiz_multi_choice_answer, null, false);
        final CanvasWebView webView = (CanvasWebView) answerWrapper.findViewById(R.id.html_answer);
        webView.setClickable(false);
        webView.setFocusableInTouchMode(false);
        final TextView textView = (TextView) answerWrapper.findViewById(R.id.text_answer);
        final CheckBox checkBox = (CheckBox) answerWrapper.findViewById(R.id.answer_checkbox);
        if (!TextUtils.isEmpty(answer.getHtml())) {
            textView.setVisibility(View.GONE);
            final String html = answer.getHtml();
            webView.loadHtml(html, "");
            webView.setBackgroundColor(Color.TRANSPARENT);
            if (quizSubmissionQuestion.getAnswer() != null) {
                if (Long.parseLong((String) quizSubmissionQuestion.getAnswer()) == answer.getId()) {
                    answerWrapper.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
                    // mark this one as selected
                    checkBox.setChecked(true);
                }
            }
        } else if (!TextUtils.isEmpty(answer.getText())) {
            webView.setVisibility(View.GONE);
            textView.setText(answer.getText());
            if (quizSubmissionQuestion.getAnswer() != null) {
                if (!TextUtils.isEmpty((String) quizSubmissionQuestion.getAnswer()) && Long.parseLong((String) quizSubmissionQuestion.getAnswer()) == answer.getId()) {
                    // mark this one as selected
                    checkBox.setChecked(true);
                    answerWrapper.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
                }
            }
        }
        holder.answerContainer.addView(answerWrapper);
        if (index == quizSubmissionQuestion.getAnswers().length - 1) {
            // if we're on the last answer remove the bottom divider
            answerWrapper.findViewById(R.id.divider).setVisibility(View.GONE);
        } else {
            answerWrapper.findViewById(R.id.divider).setVisibility(View.VISIBLE);
            index++;
        }
    }
}
Also used : CanvasWebView(com.instructure.pandautils.views.CanvasWebView) ContextThemeWrapper(android.view.ContextThemeWrapper) CheckBox(android.widget.CheckBox) LayoutInflater(android.view.LayoutInflater) QuizSubmissionAnswer(com.instructure.canvasapi2.models.QuizSubmissionAnswer) Activity(android.app.Activity) TextView(android.widget.TextView) LinearLayout(android.widget.LinearLayout)

Example 9 with QuizSubmissionQuestion

use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.

the class QuizQuestionsFragment method setupCallbacks.

private void setupCallbacks() {
    quizFileUploadListener = new QuizFileUploadListener() {

        @Override
        public void onFileUploadClicked(long quizQuestionId, int position) {
            Bundle bundle = UploadFilesDialog.createQuizFileBundle(quizQuestionId, course.getId(), quiz.getId(), position);
            UploadFilesDialog.show(getFragmentManager(), bundle, new Function1<Integer, Unit>() {

                @Override
                public Unit invoke(Integer integer) {
                    return null;
                }
            });
        }
    };
    quizSubmissionQuestionResponseCanvasCallback = new StatusCallback<QuizSubmissionQuestionResponse>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionQuestionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (type == ApiType.CACHE)
                return;
            List<QuizSubmissionQuestion> questions = response.body().getQuizSubmissionQuestions();
            // sort the questions based on position that is part of the model object
            if (questions != null) {
                Collections.sort(questions, new Comparator<QuizSubmissionQuestion>() {

                    @Override
                    public int compare(QuizSubmissionQuestion lh, QuizSubmissionQuestion rh) {
                        if (lh != null && rh != null) {
                            if (lh.getPosition() < rh.getPosition()) {
                                return -1;
                            } else if (lh.getPosition() > rh.getPosition()) {
                                return 1;
                            }
                        }
                        return 0;
                    }
                });
            }
            if (quizQuestionAdapter == null) {
                quizQuestionAdapter = new QuizSubmissionQuestionListRecyclerAdapter(getActivity(), questions, course, shouldLetAnswer, quizSubmission, quizFileUploadListener);
            } else {
                quizQuestionAdapter.addAll(questions);
            }
            // cache the views after we view them
            recyclerView.setItemViewCacheSize(response.body().getQuizSubmissionQuestions().size());
            recyclerView.setAdapter(quizQuestionAdapter);
            if (linkHeaders.nextUrl != null) {
                QuizManager.getNextPageSubmissionQuestions(linkHeaders.nextUrl, false, quizSubmissionQuestionResponseCanvasCallback);
            }
        }
    };
    submitQuizCallback = new StatusCallback<QuizSubmissionResponse>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (type == ApiType.CACHE)
                return;
            switch(auto_submit_reason) {
                case TIMED_QUIZ:
                    showToast(R.string.submitReasonTimedQuiz);
                    break;
                case DUE_DATE:
                    showToast(R.string.quizSubmittedSuccessfully);
                    break;
                case LOCK_DATE:
                    showToast(R.string.submitReasonLockAt);
                    break;
                default:
                    showToast(R.string.quizSubmittedSuccessfully);
            }
            if (chronometer != null) {
                chronometer.stop();
            }
            if (countDownTimer != null) {
                countDownTimer.cancel();
            }
            getActivity().onBackPressed();
        }
    };
    quizSubmissionTimeCanvasCallback = new StatusCallback<QuizSubmissionTime>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionTime> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (type == ApiType.CACHE)
                return;
            mQuizSubmissionTime = response.body();
            if (shouldLetAnswer && isAdded()) {
                if (quiz.getTimeLimit() == 0 && quiz.getDueAt() == null && quiz.getLockAt() == null) {
                    normalTimer();
                } else if (quiz.getTimeLimit() > 0) {
                    timeLimitCountDown(mQuizSubmissionTime.getTimeLeft());
                } else if (quiz.getDueAt() != null && (quiz.getLockAt() == null || quiz.getDueAt().before(quiz.getLockAt()))) {
                    // if we have a due date, we want to give them an option to submit it when it gets to that point, but only if the due date is before the lock date
                    auto_submit_reason = AUTO_SUBMIT_REASON.DUE_DATE;
                    countDownForSubmit(mQuizSubmissionTime.getTimeLeft() * MILLISECOND);
                } else if (quiz.getLockAt() != null) {
                    auto_submit_reason = AUTO_SUBMIT_REASON.LOCK_DATE;
                    countDownForSubmit(mQuizSubmissionTime.getTimeLeft() * MILLISECOND);
                }
            } else {
                int minutes = (int) Math.ceil((double) (quizSubmission.getTimeSpent()) / 60);
                chronometer.setVisibility(View.GONE);
                timer.setText(String.format(Locale.getDefault(), getString(R.string.timeSpentFormat), minutes));
            }
        }
    };
}
Also used : QuizSubmissionTime(com.instructure.canvasapi2.models.QuizSubmissionTime) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) Bundle(android.os.Bundle) QuizFileUploadListener(com.instructure.candroid.interfaces.QuizFileUploadListener) Function1(kotlin.jvm.functions.Function1) Comparator(java.util.Comparator) QuizSubmissionQuestion(com.instructure.canvasapi2.models.QuizSubmissionQuestion) ApiType(com.instructure.canvasapi2.utils.ApiType) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse) List(java.util.List) ArrayList(java.util.ArrayList) QuizSubmissionQuestionListRecyclerAdapter(com.instructure.candroid.adapter.QuizSubmissionQuestionListRecyclerAdapter) QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse)

Example 10 with QuizSubmissionQuestion

use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.

the class QuizManager_Test method getQuizSubmissionQuestions.

public static void getQuizSubmissionQuestions(long quizSubmissionId, StatusCallback<QuizSubmissionQuestionResponse> callback) {
    // TODO:
    Response response = new Response.Builder().code(200).message("todo").protocol(Protocol.HTTP_1_0).body(ResponseBody.create(MediaType.parse("application/json"), "todo".getBytes())).addHeader("content-type", "application/json").build();
    QuizSubmissionQuestionResponse quizSubmissionQuestionResponse = new QuizSubmissionQuestionResponse();
    List<QuizSubmissionQuestion> quizQuestions = new ArrayList<>();
    QuizSubmissionQuestion question = new QuizSubmissionQuestion();
    question.setQuestionText("TEST");
    question.setQuestionType("essay_question");
    quizQuestions.add(question);
    quizSubmissionQuestionResponse.setQuizSubmissionQuestions(quizQuestions);
    retrofit2.Response<QuizSubmissionQuestionResponse> response1 = retrofit2.Response.success(quizSubmissionQuestionResponse, response);
    callback.onResponse(response1, new LinkHeaders(), ApiType.CACHE);
}
Also used : QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse) Response(okhttp3.Response) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) QuizSubmissionQuestionResponse(com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse) ArrayList(java.util.ArrayList) QuizSubmissionQuestion(com.instructure.canvasapi2.models.QuizSubmissionQuestion)

Aggregations

TextView (android.widget.TextView)8 QuizSubmissionAnswer (com.instructure.canvasapi2.models.QuizSubmissionAnswer)8 LayoutInflater (android.view.LayoutInflater)7 LinearLayout (android.widget.LinearLayout)7 QuizSubmissionQuestion (com.instructure.canvasapi2.models.QuizSubmissionQuestion)6 CanvasWebView (com.instructure.pandautils.views.CanvasWebView)6 ArrayList (java.util.ArrayList)6 View (android.view.View)5 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)5 StatusCallback (com.instructure.canvasapi2.StatusCallback)5 QuizSubmissionQuestionResponse (com.instructure.canvasapi2.models.QuizSubmissionQuestionResponse)5 QuizSubmissionResponse (com.instructure.canvasapi2.models.QuizSubmissionResponse)5 LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)5 Drawable (android.graphics.drawable.Drawable)4 CheckBox (android.widget.CheckBox)4 QuizSubmissionMatch (com.instructure.canvasapi2.models.QuizSubmissionMatch)4 ApiType (com.instructure.canvasapi2.utils.ApiType)4 Activity (android.app.Activity)3 NonNull (android.support.annotation.NonNull)3 WebView (android.webkit.WebView)3