Search in sources :

Example 81 with Course

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

the class NotificationListFragment method addFragmentForStreamItem.

public static DialogFragment addFragmentForStreamItem(StreamItem streamItem, FragmentActivity activity, boolean fromWidget) {
    ParentFragment fragment = null;
    if (streamItem == null || activity == null) {
        return null;
    }
    String unsupportedLabel = null;
    switch(streamItem.getType()) {
        case SUBMISSION:
            if (streamItem.getAssignment() == null && streamItem.getCanvasContext() instanceof Course) {
                fragment = FragUtils.getFrag(AssignmentFragment.class, AssignmentFragment.Companion.createBundle(streamItem.getCanvasContext(), streamItem.getAssignmentId()));
            } else if (streamItem.getAssignment() != null && streamItem.getCanvasContext() instanceof Course) {
                // add an empty submission with the grade to the assignment so that we can see the score.
                Submission emptySubmission = new Submission();
                emptySubmission.setGrade(streamItem.getGrade());
                streamItem.getAssignment().setSubmission(emptySubmission);
                fragment = FragUtils.getFrag(AssignmentFragment.class, AssignmentFragment.Companion.createBundle((Course) streamItem.getCanvasContext(), streamItem.getAssignment()));
            }
            break;
        case ANNOUNCEMENT:
            if (streamItem.getCanvasContext() != null) {
                fragment = FragUtils.getFrag(DiscussionDetailsFragment.class, DiscussionDetailsFragment.makeBundle(streamItem.getCanvasContext(), streamItem.getDiscussionTopicId(), true));
            }
            break;
        case CONVERSATION:
            Conversation conversation = streamItem.getConversation();
            if (conversation != null) {
                // Check to see if the conversation has been deleted.
                if (conversation.isDeleted()) {
                    Toast.makeText(activity, R.string.deleteConversation, Toast.LENGTH_SHORT).show();
                    return null;
                }
                Bundle extras = InboxConversationFragment.createBundle(conversation, 0, null);
                fragment = FragUtils.getFrag(InboxConversationFragment.class, extras);
            }
            break;
        case DISCUSSION_TOPIC:
            if (streamItem.getCanvasContext() != null) {
                fragment = FragUtils.getFrag(DiscussionDetailsFragment.class, DiscussionDetailsFragment.makeBundle(streamItem.getCanvasContext(), streamItem.getDiscussionTopicId(), false));
            }
            break;
        case MESSAGE:
            if (streamItem.getAssignmentId() > 0) {
                if (streamItem.getCanvasContext() != null) {
                    fragment = FragUtils.getFrag(AssignmentFragment.class, AssignmentFragment.Companion.createBundle(activity, streamItem.getCanvasContext(), streamItem.getAssignmentId(), streamItem));
                }
            } else {
                fragment = FragUtils.getFrag(UnknownItemFragment.class, UnknownItemFragment.createBundle(streamItem.getCanvasContext(), streamItem));
            }
            break;
        case COLLABORATION:
            if (streamItem.getCanvasContext() != null) {
                unsupportedLabel = activity.getString(R.string.collaborations);
                fragment = UnSupportedTabFragment.createFragment(UnSupportedTabFragment.class, UnSupportedTabFragment.createBundle(streamItem.getCanvasContext(), Tab.COLLABORATIONS_ID, R.string.collaborations));
            }
            break;
        case CONFERENCE:
            if (streamItem.getCanvasContext() != null) {
                unsupportedLabel = activity.getString(R.string.conferences);
                fragment = UnSupportedTabFragment.createFragment(UnSupportedTabFragment.class, UnSupportedTabFragment.createBundle(streamItem.getCanvasContext(), Tab.CONFERENCES_ID, R.string.conferences));
            }
            break;
        default:
            if (streamItem.getCanvasContext() != null) {
                unsupportedLabel = streamItem.getType().toString();
                fragment = FragUtils.getFrag(UnSupportedFeatureFragment.class, UnSupportedFeatureFragment.createBundle(streamItem.getCanvasContext(), unsupportedLabel, streamItem.getUrl()));
            }
            break;
    }
    if (unsupportedLabel != null) {
        if (activity instanceof Navigation) {
            ((Navigation) activity).addFragment(fragment);
        }
    } else {
        if (activity instanceof Navigation && fragment != null) {
            ((Navigation) activity).addFragment(fragment);
        }
        if (fromWidget) {
            if (streamItem.getUrl() != null) {
                RouterUtils.routeUrl(activity, streamItem.getUrl(), false);
            } else {
                RouterUtils.routeUrl(activity, streamItem.getHtmlUrl(), false);
            }
        }
    }
    return null;
}
Also used : Navigation(com.instructure.interactions.Navigation) Submission(com.instructure.canvasapi2.models.Submission) Bundle(android.os.Bundle) Conversation(com.instructure.canvasapi2.models.Conversation) Course(com.instructure.canvasapi2.models.Course)

Example 82 with Course

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

the class CourseManager_Test method getCourse.

public static void getCourse(long courseId, StatusCallback<Course> callback) {
    // TODO:
    Response response = new Response.Builder().request(new Request.Builder().url("https://test.com").build()).code(200).message("todo").protocol(Protocol.HTTP_1_0).body(ResponseBody.create(MediaType.parse("application/json"), "todo".getBytes())).addHeader("content-type", "application/json").build();
    Course course = new Course();
    course.setId(courseId);
    retrofit2.Response<Course> response1 = retrofit2.Response.success(course, response);
    callback.onResponse(response1, new LinkHeaders(), ApiType.CACHE);
}
Also used : GradingPeriodResponse(com.instructure.canvasapi2.models.GradingPeriodResponse) Response(okhttp3.Response) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) Request(okhttp3.Request) Course(com.instructure.canvasapi2.models.Course)

Example 83 with Course

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

the class QuizStartFragment method setupCallbacks.

private void setupCallbacks() {
    webViewClientCallback = new CanvasWebView.CanvasWebViewClientCallback() {

        @Override
        public void openMediaFromWebView(String mime, String url, String filename) {
            openMedia(mime, url, filename);
        }

        @Override
        public void onPageFinishedCallback(WebView webView, String url) {
        }

        @Override
        public void onPageStartedCallback(WebView webView, String url) {
        }

        @Override
        public boolean canRouteInternallyDelegate(String url) {
            return RouterUtils.canRouteInternally(null, url, ApiPrefs.getDomain(), false);
        }

        @Override
        public void routeInternallyCallback(String url) {
            RouterUtils.canRouteInternally(getActivity(), url, ApiPrefs.getDomain(), true);
        }
    };
    embeddedWebViewCallback = new CanvasWebView.CanvasEmbeddedWebViewCallback() {

        @Override
        public void launchInternalWebViewFragment(String url) {
            InternalWebviewFragment.Companion.loadInternalWebView(getActivity(), getNavigation(), InternalWebviewFragment.Companion.createBundle(course, url, false));
        }

        @Override
        public boolean shouldLaunchInternalWebViewFragment(String url) {
            return true;
        }
    };
    quizSubmissionTimeCanvasCallback = new StatusCallback<QuizSubmissionTime>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionTime> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (type == ApiType.CACHE)
                return;
            QuizStartFragment.this.quizSubmissionTime = quizSubmissionTime;
            QuizManager.getQuizSubmissions(course, quiz.getId(), true, quizSubmissionResponseCanvasCallback);
        }
    };
    quizSubmissionResponseCanvasCallback = new StatusCallback<QuizSubmissionResponse>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (type == ApiType.CACHE)
                return;
            final QuizSubmissionResponse quizSubmissionResponse = response.body();
            // since this is a student app, make sure they only have their own submissions (if they're siteadmin it'll be different)
            final ArrayList<QuizSubmission> submissions = new ArrayList<>();
            final User user = ApiPrefs.getUser();
            if (user != null) {
                for (QuizSubmission submission : quizSubmissionResponse.getQuizSubmissions()) {
                    if (submission.getUserId() == user.getId()) {
                        submissions.add(submission);
                    }
                }
            }
            quizSubmissionResponse.setQuizSubmissions(submissions);
            if (quizSubmissionResponse.getQuizSubmissions() == null || quizSubmissionResponse.getQuizSubmissions().size() == 0) {
                // No quiz submissions, let the user start the quiz.
                // They haven't turned it in yet, so don't show the turned-in view
                quizTurnedInContainer.setVisibility(View.GONE);
                shouldStartQuiz = true;
                next.setVisibility(View.VISIBLE);
                next.setEnabled(true);
            } else {
                // We should have at least 1 submission
                quizSubmission = quizSubmissionResponse.getQuizSubmissions().get(quizSubmissionResponse.getQuizSubmissions().size() - 1);
                next.setEnabled(true);
                final boolean hasUnlimitedAttempts = quiz.getAllowedAttempts() == -1;
                // Teacher can manually unlock a quiz for an individual student
                final boolean teacherUnlockedQuizAttempts = quizSubmission.isManuallyUnlocked();
                final boolean hasMoreAttemptsLeft = quizSubmission.getAttemptsLeft() > 0;
                final boolean canTakeQuizAgain = hasUnlimitedAttempts | teacherUnlockedQuizAttempts | hasMoreAttemptsLeft;
                if (quiz.getHideResults() == Quiz.HIDE_RESULTS_TYPE.ALWAYS && !canTakeQuizAgain) {
                    // Don't let the user see the questions if they've exceeded their attempts
                    next.setVisibility(View.GONE);
                } else if (quiz.getHideResults() == Quiz.HIDE_RESULTS_TYPE.AFTER_LAST_ATTEMPT && !canTakeQuizAgain) {
                    // They can only see the results after their last attempt, and that hasn't happened yet
                    next.setVisibility(View.GONE);
                }
                // -1 allowed attempts == unlimited
                if (quizSubmission.getFinishedAt() != null && !canTakeQuizAgain) {
                    // They've finished the quiz and they can't take it anymore; let them see results
                    next.setVisibility(View.VISIBLE);
                    next.setText(getString(R.string.viewQuestions));
                    shouldLetAnswer = false;
                } else {
                    // They are allowed to take the quiz...
                    next.setVisibility(View.VISIBLE);
                    if (quizSubmission.getFinishedAt() != null) {
                        shouldStartQuiz = true;
                        next.setText(getString(R.string.takeQuizAgain));
                    } else {
                        // Let the user resume their quiz
                        next.setText(getString(R.string.resumeQuiz));
                    }
                }
                if (quizSubmission.getFinishedAt() != null) {
                    quizTurnedIn.setText(getString(R.string.turnedIn));
                    quizTurnedInDetails.setText(DateHelper.getDateTimeString(getActivity(), quizSubmission.getFinishedAt()));
                    // The user has turned in the quiz, let them see the results
                    viewResults.setVisibility(View.VISIBLE);
                } else {
                    quizTurnedInContainer.setVisibility(View.GONE);
                }
                // Weird hack where if the time expires and the user hasn't submitted it doesn't let you start the quiz
                if (quizSubmission.getWorkflowState() == QuizSubmission.WORKFLOW_STATE.UNTAKEN && (quizSubmission.getEndAt() != null && (quizSubmissionTime != null && quizSubmissionTime.getTimeLeft() > 0))) {
                    next.setEnabled(false);
                    // submit the quiz for them
                    QuizManager.submitQuiz(course, quizSubmission, true, new StatusCallback<QuizSubmissionResponse>() {

                        @Override
                        public void onResponse(@NonNull Response<QuizSubmissionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                            if (type == ApiType.CACHE)
                                return;
                            // the user has turned in the quiz, let them see the results
                            viewResults.setVisibility(View.VISIBLE);
                            next.setEnabled(true);
                            shouldStartQuiz = true;
                            next.setText(getString(R.string.takeQuizAgain));
                            QuizSubmissionResponse quizResponse = response.body();
                            // Since this is a student app, make sure they only have their own submissions (if they're siteadmin it'll be different)
                            final ArrayList<QuizSubmission> submissions = new ArrayList<>();
                            final User user = ApiPrefs.getUser();
                            if (user != null) {
                                for (QuizSubmission submission : quizResponse.getQuizSubmissions()) {
                                    if (submission.getUserId() == user.getId()) {
                                        submissions.add(submission);
                                    }
                                }
                            }
                            quizResponse.setQuizSubmissions(submissions);
                            if (quizResponse.getQuizSubmissions() != null && quizResponse.getQuizSubmissions().size() > 0) {
                                quizSubmission = quizResponse.getQuizSubmissions().get(quizResponse.getQuizSubmissions().size() - 1);
                            }
                        }
                    });
                }
                // If the user can only see results once and they have seen it, don't let them view the questions
                if (quiz.isOneTimeResults() && quizSubmission.hasSeenResults()) {
                    next.setVisibility(View.GONE);
                }
                if (quiz.isLockedForUser()) {
                    shouldStartQuiz = false;
                    next.setText(getString(R.string.assignmentLocked));
                }
            }
            populateQuizInfo();
            canvasLoading.setVisibility(View.GONE);
        }

        @Override
        public void onFail(@Nullable Call<QuizSubmissionResponse> call, @NonNull Throwable error, @Nullable Response response) {
            canvasLoading.setVisibility(View.GONE);
            // on quizzes.
            if (response != null && response.code() == 401) {
                populateQuizInfo();
                // there is a not authorized error, so don't let them start the quiz
                next.setVisibility(View.GONE);
            }
        }
    };
    quizStartResponseCallback = new StatusCallback<QuizSubmissionResponse>() {

        @Override
        public void onResponse(@NonNull Response<QuizSubmissionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (response.code() == 200 && type == ApiType.API) {
                // We want to show the quiz here, but we need to get the quizSubmissionId first so our
                // api call for the QuizQuestionsFragment knows which questions to get
                StatusCallback<QuizSubmissionResponse> quizSubmissionResponseCallback = new StatusCallback<QuizSubmissionResponse>() {

                    @Override
                    public void onResponse(@NonNull Response<QuizSubmissionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
                        QuizSubmissionResponse quizSubmissionResponse = response.body();
                        if (quizSubmissionResponse != null && quizSubmissionResponse.getQuizSubmissions() != null && quizSubmissionResponse.getQuizSubmissions().size() > 0) {
                            quizSubmission = quizSubmissionResponse.getQuizSubmissions().get(quizSubmissionResponse.getQuizSubmissions().size() - 1);
                            if (quizSubmission != null) {
                                showQuiz();
                            } else {
                                getLockedMessage();
                            }
                        }
                    }
                };
                QuizManager.getFirstPageQuizSubmissions(course, quiz.getId(), false, quizSubmissionResponseCallback);
            }
        }

        @Override
        public void onFail(@Nullable Call<QuizSubmissionResponse> call, @NonNull Throwable error, @Nullable Response response) {
            if (response != null && response.code() == 403) {
                // Forbidden
                // Check to see if it's because of IP restriction or bad access code or either
                getLockedMessage();
            }
        }
    };
    quizStartSessionCallback = new StatusCallback<ResponseBody>() {
    };
}
Also used : User(com.instructure.canvasapi2.models.User) ArrayList(java.util.ArrayList) QuizSubmission(com.instructure.canvasapi2.models.QuizSubmission) ApiType(com.instructure.canvasapi2.utils.ApiType) NonNull(android.support.annotation.NonNull) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) WebView(android.webkit.WebView) QuizSubmissionTime(com.instructure.canvasapi2.models.QuizSubmissionTime) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) StatusCallback(com.instructure.canvasapi2.StatusCallback) ResponseBody(okhttp3.ResponseBody) Response(retrofit2.Response) QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse) CanvasWebView(com.instructure.pandautils.views.CanvasWebView) QuizSubmissionResponse(com.instructure.canvasapi2.models.QuizSubmissionResponse)

Example 84 with Course

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

the class ScheduleListFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mRootView = getLayoutInflater().inflate(R.layout.fragment_list_syllabus, container, false);
    mToolbar = mRootView.findViewById(R.id.toolbar);
    mAdapterToFragmentCallback = new AdapterToFragmentCallback<ScheduleItem>() {

        @Override
        public void onRowClicked(ScheduleItem scheduleItem, int position, boolean isOpenDetail) {
            Navigation navigation = getNavigation();
            if (navigation != null) {
                ParentFragment fragment;
                if (scheduleItem.getAssignment() != null) {
                    fragment = FragUtils.getFrag(AssignmentFragment.class, AssignmentFragment.Companion.createBundle((Course) getCanvasContext(), scheduleItem.getAssignment()));
                } else if (scheduleItem.getItemType() == ScheduleItem.Type.TYPE_SYLLABUS) {
                    fragment = FragUtils.getFrag(SyllabusFragment.class, SyllabusFragment.Companion.createBundle((Course) getCanvasContext(), scheduleItem));
                } else {
                    fragment = FragUtils.getFrag(CalendarEventFragment.class, CalendarEventFragment.createBundle(getCanvasContext(), scheduleItem));
                }
                navigation.addFragment(fragment);
            }
        }

        @Override
        public void onRefreshFinished() {
            setRefreshing(false);
        }
    };
    mRecyclerAdapter = new SyllabusRecyclerAdapter(getContext(), getCanvasContext(), mAdapterToFragmentCallback);
    configureRecyclerView(mRootView, getContext(), mRecyclerAdapter, R.id.swipeRefreshLayout, R.id.emptyPandaView, R.id.listView);
    return mRootView;
}
Also used : SyllabusRecyclerAdapter(com.instructure.candroid.adapter.SyllabusRecyclerAdapter) ScheduleItem(com.instructure.canvasapi2.models.ScheduleItem) Navigation(com.instructure.interactions.Navigation) Course(com.instructure.canvasapi2.models.Course)

Example 85 with Course

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

the class ModuleUtilityTest method testGetFragment_externalurl_externaltool.

@Test
public void testGetFragment_externalurl_externaltool() {
    String url = "https://instructure.com";
    ModuleItem moduleItem = new ModuleItem();
    moduleItem.setType("ExternalUrl");
    moduleItem.setId(4567);
    moduleItem.setTitle("Hello");
    moduleItem.setHtml_url(url);
    Course course = new Course();
    Bundle expectedBundle = new Bundle();
    expectedBundle.putSerializable(Const.CANVAS_CONTEXT, course);
    expectedBundle.putString(Const.INTERNAL_URL, "https://instructure.com?display=borderless");
    expectedBundle.putString(Const.ACTION_BAR_TITLE, "Hello");
    expectedBundle.putBoolean(Const.AUTHENTICATE, true);
    expectedBundle.putBoolean(com.instructure.pandautils.utils.Const.IS_EXTERNAL_TOOL, true);
    expectedBundle.putBoolean(com.instructure.pandautils.utils.Const.IS_UNSUPPORTED_FEATURE, true);
    ParentFragment parentFragment = callGetFragment(moduleItem, course, null);
    assertNotNull(parentFragment);
    assertEquals(InternalWebviewFragment.class, parentFragment.getClass());
    assertEquals(expectedBundle.toString(), parentFragment.getArguments().toString());
    // test external tool type
    moduleItem.setType("ExternalTool");
    parentFragment = callGetFragment(moduleItem, course, null);
    assertNotNull(parentFragment);
    assertEquals(InternalWebviewFragment.class, parentFragment.getClass());
    assertEquals(expectedBundle.toString(), parentFragment.getArguments().toString());
}
Also used : ModuleItem(com.instructure.canvasapi2.models.ModuleItem) ParentFragment(com.instructure.candroid.fragment.ParentFragment) Bundle(android.os.Bundle) Course(com.instructure.canvasapi2.models.Course) Test(org.junit.Test)

Aggregations

Course (com.instructure.canvasapi2.models.Course)106 Test (org.junit.Test)70 ArrayList (java.util.ArrayList)39 Enrollment (com.instructure.canvasapi2.models.Enrollment)29 RestBuilder (com.instructure.canvasapi2.builders.RestBuilder)17 RestParams (com.instructure.canvasapi2.builders.RestParams)17 View (android.view.View)13 Bundle (android.os.Bundle)10 TextView (android.widget.TextView)10 NonNull (android.support.annotation.NonNull)8 AdapterView (android.widget.AdapterView)8 ModuleItem (com.instructure.canvasapi2.models.ModuleItem)8 ParentFragment (com.instructure.candroid.fragment.ParentFragment)7 CanvasContext (com.instructure.canvasapi2.models.CanvasContext)7 ImageView (android.widget.ImageView)6 LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)6 List (java.util.List)6 BindView (butterknife.BindView)5 RecyclerView (android.support.v7.widget.RecyclerView)4 StatusCallback (com.instructure.canvasapi2.StatusCallback)4