Search in sources :

Example 1 with QuizSubmissionMatch

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

the class QuizMatchingBinder method bind.

public static void bind(final QuizMatchingViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int courseColor, final int position, final boolean shouldLetAnswer, final Context context, final CanvasWebView.CanvasEmbeddedWebViewCallback embeddedWebViewCallback, final CanvasWebView.CanvasWebViewClientCallback webViewClientCallback, final QuizPostMatching callback, final QuizToggleFlagState flagStateCallback) {
    if (holder == null) {
        return;
    }
    setupViews(holder, quizSubmissionQuestion, position, context, embeddedWebViewCallback, webViewClientCallback);
    LayoutInflater inflater = LayoutInflater.from(context);
    // add answers to the answer container
    int index = 0;
    for (final QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
        final LinearLayout answerWrapper = (LinearLayout) inflater.inflate(R.layout.quiz_matching_answer, null, false);
        final TextView answerTextView = (TextView) answerWrapper.findViewById(R.id.text_answer);
        final Spinner spinner = (Spinner) answerWrapper.findViewById(R.id.answer_spinner);
        ArrayList<QuizSubmissionMatch> list = new ArrayList<>();
        QuizSubmissionMatch firstMatch = new QuizSubmissionMatch();
        firstMatch.setText(context.getString(R.string.quizMatchingDefaultDisplay));
        list.add(firstMatch);
        for (QuizSubmissionMatch match : quizSubmissionQuestion.getMatches()) {
            list.add(match);
        }
        QuizMatchSpinnerAdapter adapter = new QuizMatchSpinnerAdapter(context, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        if (!TextUtils.isEmpty(answer.getHtml())) {
            answerTextView.setVisibility(View.GONE);
            setPreviouslySelectedAnswer(quizSubmissionQuestion, answer, spinner, list);
        } else if (!TextUtils.isEmpty(answer.getText())) {
            answerTextView.setText(answer.getText());
            setPreviouslySelectedAnswer(quizSubmissionQuestion, answer, spinner, list);
        }
        final Drawable courseColorFlag = ColorKeeper.getColoredDrawable(context, R.drawable.vd_bookmark_filled, courseColor);
        if (quizSubmissionQuestion.isFlagged()) {
            holder.flag.setImageDrawable(courseColorFlag);
        } else {
            holder.flag.setImageDrawable(ColorKeeper.getColoredDrawable(context, R.drawable.vd_navigation_bookmarks, context.getResources().getColor(R.color.defaultTextGray)));
        }
        if (shouldLetAnswer) {
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    // post the answer to the api
                    HashMap<Long, Integer> answerMap = new HashMap<>();
                    if (((QuizSubmissionMatch) adapterView.getSelectedItem()).getMatchId() != 0) {
                        answerMap.put(answer.getId(), ((QuizSubmissionMatch) adapterView.getSelectedItem()).getMatchId());
                        callback.postMatching(holder.questionId, answerMap);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                }
            });
            holder.flag.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (quizSubmissionQuestion.isFlagged()) {
                        // unflag it
                        holder.flag.setImageDrawable(ColorKeeper.getColoredDrawable(context, R.drawable.vd_navigation_bookmarks, context.getResources().getColor(R.color.defaultTextGray)));
                        flagStateCallback.toggleFlagged(false, quizSubmissionQuestion.getId());
                        quizSubmissionQuestion.setFlagged(false);
                    } else {
                        // flag it
                        holder.flag.setImageDrawable(courseColorFlag);
                        flagStateCallback.toggleFlagged(true, quizSubmissionQuestion.getId());
                        quizSubmissionQuestion.setFlagged(true);
                    }
                }
            });
        } else {
            holder.flag.setEnabled(false);
        }
        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 : HashMap(java.util.HashMap) Spinner(android.widget.Spinner) ArrayList(java.util.ArrayList) Drawable(android.graphics.drawable.Drawable) QuizMatchSpinnerAdapter(com.instructure.candroid.adapter.QuizMatchSpinnerAdapter) QuizSubmissionMatch(com.instructure.canvasapi2.models.QuizSubmissionMatch) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) View(android.view.View) AdapterView(android.widget.AdapterView) WebView(android.webkit.WebView) TextView(android.widget.TextView) LayoutInflater(android.view.LayoutInflater) QuizSubmissionAnswer(com.instructure.canvasapi2.models.QuizSubmissionAnswer) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView) LinearLayout(android.widget.LinearLayout)

Example 2 with QuizSubmissionMatch

use of com.instructure.canvasapi2.models.QuizSubmissionMatch 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 3 with QuizSubmissionMatch

use of com.instructure.canvasapi2.models.QuizSubmissionMatch 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.getMatchId() == matchId) {
                            spinner.setSelection(listIndex);
                            break;
                        }
                        listIndex++;
                    }
                }
            }
        }
    }
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) QuizSubmissionMatch(com.instructure.canvasapi2.models.QuizSubmissionMatch)

Example 4 with QuizSubmissionMatch

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

the class QuizMatchingBinder method bind.

public static void bind(final QuizMatchingViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int position, final Context context, final CanvasWebView.CanvasEmbeddedWebViewCallback embeddedWebViewCallback, final CanvasWebView.CanvasWebViewClientCallback webViewClientCallback) {
    if (holder == null) {
        return;
    }
    setupViews(holder, quizSubmissionQuestion, position, context, embeddedWebViewCallback, webViewClientCallback);
    LayoutInflater inflater = LayoutInflater.from(context);
    // add answers to the answer container
    int index = 0;
    for (final QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
        final LinearLayout answerWrapper = (LinearLayout) inflater.inflate(R.layout.quiz_matching_answer, null, false);
        final TextView answerTextView = (TextView) answerWrapper.findViewById(R.id.text_answer);
        final Spinner spinner = (Spinner) answerWrapper.findViewById(R.id.answer_spinner);
        ArrayList<QuizSubmissionMatch> list = new ArrayList<>();
        QuizSubmissionMatch firstMatch = new QuizSubmissionMatch();
        firstMatch.setText(context.getString(R.string.quizMatchingDefaultDisplay));
        list.add(firstMatch);
        for (QuizSubmissionMatch match : quizSubmissionQuestion.getMatches()) {
            list.add(match);
        }
        QuizMatchSpinnerAdapter adapter = new QuizMatchSpinnerAdapter(context, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        if (!TextUtils.isEmpty(answer.getHtml())) {
            answerTextView.setVisibility(View.GONE);
            setPreviouslySelectedAnswer(quizSubmissionQuestion, answer, spinner, list);
        } else if (!TextUtils.isEmpty(answer.getText())) {
            answerTextView.setText(answer.getText());
            setPreviouslySelectedAnswer(quizSubmissionQuestion, answer, spinner, list);
        }
        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 : Spinner(android.widget.Spinner) LayoutInflater(android.view.LayoutInflater) QuizSubmissionAnswer(com.instructure.canvasapi2.models.QuizSubmissionAnswer) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) QuizMatchSpinnerAdapter(com.instructure.teacher.adapters.QuizMatchSpinnerAdapter) QuizSubmissionMatch(com.instructure.canvasapi2.models.QuizSubmissionMatch) LinearLayout(android.widget.LinearLayout)

Aggregations

QuizSubmissionMatch (com.instructure.canvasapi2.models.QuizSubmissionMatch)4 LayoutInflater (android.view.LayoutInflater)2 LinearLayout (android.widget.LinearLayout)2 Spinner (android.widget.Spinner)2 TextView (android.widget.TextView)2 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)2 QuizSubmissionAnswer (com.instructure.canvasapi2.models.QuizSubmissionAnswer)2 ArrayList (java.util.ArrayList)2 Drawable (android.graphics.drawable.Drawable)1 View (android.view.View)1 WebView (android.webkit.WebView)1 AdapterView (android.widget.AdapterView)1 QuizMatchSpinnerAdapter (com.instructure.candroid.adapter.QuizMatchSpinnerAdapter)1 CanvasWebView (com.instructure.pandautils.views.CanvasWebView)1 QuizMatchSpinnerAdapter (com.instructure.teacher.adapters.QuizMatchSpinnerAdapter)1 HashMap (java.util.HashMap)1