use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.
the class QuizSubmissionQuestionListRecyclerAdapter method bindTheHolder.
public void bindTheHolder(final QuizSubmissionQuestion baseItem, RecyclerView.ViewHolder holder, int position) {
int courseColor = ColorKeeper.getOrGenerateColor(canvasContext);
if (position == super.getItemCount()) {
// submit button
SubmitButtonBinder.bind((SubmitButtonViewHolder) holder, getContext(), canvasContext, QuizSubmissionQuestionListRecyclerAdapter.this, new QuizSubmit() {
@Override
public void submitQuiz() {
QuizManager.postQuizSubmit(canvasContext, quizSubmission, true, new StatusCallback<QuizSubmissionResponse>() {
@Override
public void onResponse(@NonNull Response<QuizSubmissionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
if (type == ApiType.CACHE)
return;
// Submitted!
Toast.makeText(getContext(), R.string.quizSubmittedSuccessfully, Toast.LENGTH_SHORT).show();
// Go back to the startQuizFragment
((Activity) getContext()).onBackPressed();
Fragment fragment = ((NavigationActivity) getContext()).getTopFragment();
if (fragment instanceof QuizStartFragment) {
((QuizStartFragment) fragment).updateQuizInfo();
}
}
@Override
public void onFail(@Nullable Call<QuizSubmissionResponse> call, @NonNull Throwable error, @Nullable Response response) {
Toast.makeText(getContext(), R.string.quizSubmittedFailure, Toast.LENGTH_SHORT).show();
}
});
}
});
return;
}
switch(baseItem.getQuestionType()) {
case ESSAY:
case SHORT_ANSWER:
addEssayQuestion(baseItem, (QuizEssayViewHolder) holder, position, courseColor);
break;
case MUTIPLE_CHOICE:
case TRUE_FALSE:
addMultipleChoiceQuestion(baseItem, (QuizMultiChoiceViewHolder) holder, position, courseColor);
break;
case TEXT_ONLY:
QuizTextOnlyBinder.bind((QuizTextOnlyViewHolder) holder, baseItem);
break;
case MULTIPLE_ANSWERS:
addMultipleAnswerQuestion((QuizMultiChoiceViewHolder) holder, position, courseColor);
break;
case MATCHING:
addMatchingQuestion(baseItem, (QuizMatchingViewHolder) holder, position, courseColor);
break;
case FILE_UPLOAD:
addFileUploadQuestion(baseItem, (QuizFileUploadViewHolder) holder, position, courseColor);
break;
case NUMERICAL:
addNumericalQuestion(baseItem, (QuizNumericalViewHolder) holder, position, courseColor);
break;
case MULTIPLE_DROPDOWNS:
addMultipleDropdown(baseItem, (QuizMultipleDropdownViewHolder) holder, position, courseColor);
break;
}
}
use of com.instructure.canvasapi2.models.QuizSubmissionQuestion 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++;
}
}
}
use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.
the class QuizMultiAnswerBinder method bind.
public static void bind(final QuizMultiChoiceViewHolder holder, final QuizSubmissionQuestion quizSubmissionQuestion, final int courseColor, final int position, final boolean shouldLetAnswer, final Context context, final QuizPostMultiAnswers callback, final QuizToggleFlagState flagStateCallback) {
if (holder == null) {
return;
}
holder.question.loadUrl("about:blank");
holder.question.setBackgroundColor(Color.TRANSPARENT);
holder.question.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
super.onPageFinished(view, url);
}
});
holder.question.formatHTML(quizSubmissionQuestion.getQuestionText(), "");
holder.questionNumber.setText(quizSubmissionQuestion.getQuestionName() + " " + (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 answerContainer = (LinearLayout) inflater.inflate(R.layout.quiz_multi_choice_answer, null, false);
final CanvasWebView webView = (CanvasWebView) answerContainer.findViewById(R.id.html_answer);
webView.setClickable(false);
webView.setFocusableInTouchMode(false);
final TextView textView = (TextView) answerContainer.findViewById(R.id.text_answer);
final CheckBox checkBox = (CheckBox) answerContainer.findViewById(R.id.answer_checkbox);
if (!TextUtils.isEmpty(answer.getHtml())) {
textView.setVisibility(View.GONE);
final String html = StringUtilities.trimTrailingWhitespace(answer.getHtml()).toString();
webView.formatHTML(html, "");
webView.setBackgroundColor(Color.TRANSPARENT);
// we only care about marking the answers if they can actually answer
if (shouldLetAnswer) {
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// this may or may not be how we do the selection of things when we get the final UI
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
answerContainer.performClick();
return true;
}
return true;
}
});
}
} else if (!TextUtils.isEmpty(answer.getText())) {
webView.setVisibility(View.GONE);
textView.setText(answer.getText());
}
if (shouldLetAnswer) {
answerContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkBox.isChecked()) {
checkBox.setChecked(false);
answerContainer.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundLight));
callback.answerUnselected(holder.questionId, answer.getId());
} else {
checkBox.setChecked(true);
answerContainer.setBackgroundColor(context.getResources().getColor(R.color.canvasBackgroundMedium));
callback.answerSelected(holder.questionId, answer.getId());
}
}
});
}
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)));
}
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);
}
}
});
holder.answerContainer.addView(answerContainer);
if (index == quizSubmissionQuestion.getAnswers().length - 1) {
// if we're on the last answer remove the bottom divider
answerContainer.findViewById(R.id.divider).setVisibility(View.GONE);
} else {
answerContainer.findViewById(R.id.divider).setVisibility(View.VISIBLE);
index++;
}
}
}
use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.
the class QuizMultipleDropdownBinder method setPreviouslySelectedAnswer.
private static void setPreviouslySelectedAnswer(QuizSubmissionQuestion quizSubmissionQuestion, String answer, Spinner spinner, ArrayList<QuizSubmissionAnswer> list) {
if (quizSubmissionQuestion.getAnswer() != null) {
// set the one they selected last time
for (String map : ((LinkedTreeMap<String, String>) quizSubmissionQuestion.getAnswer()).keySet()) {
if (!answer.equals("null") && map.equals(answer)) {
String matchId = ((LinkedTreeMap<String, String>) quizSubmissionQuestion.getAnswer()).get(map);
// now see if we have a match in the list of matches
int listIndex = 0;
for (QuizSubmissionAnswer match : list) {
if (Long.toString(match.getId()).equals(matchId)) {
spinner.setSelection(listIndex);
break;
}
listIndex++;
}
}
}
}
}
use of com.instructure.canvasapi2.models.QuizSubmissionQuestion in project instructure-android by instructure.
the class QuizMultipleDropdownBinder method bind.
public static void bind(final QuizMultipleDropdownViewHolder 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 QuizPostMultipleDropdown callback, final QuizToggleFlagState flagStateCallback) {
if (holder == null) {
return;
}
setupViews(holder, quizSubmissionQuestion, position, context, embeddedWebViewCallback, webViewClientCallback);
LayoutInflater inflater = LayoutInflater.from(context);
// use a map to store the data, each blank id will have an array list of potential answers
HashMap<String, ArrayList<QuizSubmissionAnswer>> answerMap = new HashMap<>();
for (QuizSubmissionAnswer answer : quizSubmissionQuestion.getAnswers()) {
ArrayList<QuizSubmissionAnswer> potentialAnswers = answerMap.get(answer.getBlankId());
if (potentialAnswers == null) {
potentialAnswers = new ArrayList<>();
QuizSubmissionAnswer selectAnswer = new QuizSubmissionAnswer();
selectAnswer.setText(context.getString(R.string.quizMatchingDefaultDisplay));
potentialAnswers.add(selectAnswer);
}
potentialAnswers.add(answer);
answerMap.put(answer.getBlankId(), potentialAnswers);
}
if (answerMap.size() > 1) {
// we have more than one, update the string to reflect that
holder.chooseAnswer.setText(context.getString(R.string.choose_answers_below));
} else {
holder.chooseAnswer.setText(context.getString(R.string.choose_answer_below));
}
// add answers to the answer container
for (final String blankId : answerMap.keySet()) {
final LinearLayout answerWrapper = (LinearLayout) inflater.inflate(R.layout.quiz_multiple_dropdown_answer, null, false);
final TextView answerTextView = (TextView) answerWrapper.findViewById(R.id.text_answer);
final Spinner spinner = (Spinner) answerWrapper.findViewById(R.id.answer_spinner);
ArrayList<QuizSubmissionAnswer> list = new ArrayList<>();
for (QuizSubmissionAnswer dropdownAnswer : answerMap.get(blankId)) {
list.add(dropdownAnswer);
}
QuizMultipleDropdownSpinnerAdapter adapter = new QuizMultipleDropdownSpinnerAdapter(context, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
if (!TextUtils.isEmpty(blankId)) {
answerTextView.setText(blankId);
setPreviouslySelectedAnswer(quizSubmissionQuestion, blankId, 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<String, Long> answerMap = new HashMap<>();
if (((QuizSubmissionAnswer) adapterView.getSelectedItem()).getId() != 0) {
answerMap.put(blankId, ((QuizSubmissionAnswer) adapterView.getSelectedItem()).getId());
callback.postMultipleDropdown(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);
}
}
Aggregations