Search in sources :

Example 1 with Poll

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

the class PublishPollActivity method setupCourseSpinner.

// /////////////////////////////////////////////////////////////////////////
// Helpers
// /////////////////////////////////////////////////////////////////////////
private void setupCourseSpinner(List<Course> courseList) {
    // We only want courses we're a teacher for if we're trying to publish a poll
    Iterator<Course> iterator = courseList.iterator();
    while (iterator.hasNext()) {
        if (!iterator.next().isTeacher()) {
            iterator.remove();
        }
    }
    if (courseList.size() == 0 || !courseList.get(0).getName().equals(getString(R.string.selectCourse))) {
        Course selectCourse = new Course();
        selectCourse.setId(Long.MIN_VALUE);
        selectCourse.setName(getString(R.string.selectCourse));
        courseList.add(0, selectCourse);
    }
    courseAdapter = new CourseSpinnerAdapter(this, android.R.layout.simple_spinner_dropdown_item, courseList);
    coursesSpinner.setAdapter(courseAdapter);
    coursesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Course course = (Course) parent.getAdapter().getItem(position);
            // Handle the loading cell.
            if (course == null || course.getEnrollments() == null) {
                return;
            }
            if (!isCourseTermActive(course)) {
                AppMsg.makeText(PublishPollActivity.this, getString(R.string.courseTermInactive), AppMsg.STYLE_WARNING).show();
                return;
            }
            currentCourse = course;
            // unselect all the selections, if we don't it still thinks some are selected when we go to another course
            for (int i = 0; i < sectionListView.getCount(); i++) {
                sectionListView.setItemChecked(i, false);
            }
            SectionManager.getAllSectionsForCourse(course.getId(), new StatusCallback<List<Section>>() {

                @Override
                public void onResponse(@NonNull retrofit2.Response<List<Section>> response, @NonNull com.instructure.canvasapi2.utils.LinkHeaders linkHeaders, @NonNull ApiType type) {
                    setupSectionAdapter(response.body());
                    ApplicationManager.saveSections(PublishPollActivity.this, response.body(), ((Course) coursesSpinner.getSelectedItem()).getId());
                }
            }, true);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}
Also used : LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) StatusCallback(com.instructure.canvasapi2.StatusCallback) BindView(butterknife.BindView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) CheckedTextView(android.widget.CheckedTextView) ListView(android.widget.ListView) Section(com.instructure.canvasapi2.models.Section) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) Response(retrofit2.Response) NonNull(android.support.annotation.NonNull) ApiType(com.instructure.canvasapi2.utils.ApiType) AdapterView(android.widget.AdapterView) Course(com.instructure.canvasapi2.models.Course)

Example 2 with Poll

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

the class PollSessionListFragment method generateCSV.

private void generateCSV() {
    String csv = "";
    csv += "Poll Title, Poll Session, Course Name, Section Name, User Name, Answer, Date\n";
    for (int i = 0; i < getItemCount(); i++) {
        PollSession pollSession = getItem(i);
        if (pollSession.getPoll_submissions() != null) {
            for (PollSubmission pollSubmission : pollSession.getPoll_submissions()) {
                // now add all the necessary stuff to the csv string
                csv += poll.getQuestion() + ",";
                csv += pollSession.getId() + ",";
                csv += courseMap.get(pollSession.getCourse_id()).getName() + ",";
                csv += sectionMap.get(pollSession.getCourse_section_id()).getName() + ",";
                csv += pollSubmission.getUser_id() + ",";
                // of just an id
                if (pollChoiceMap != null && pollChoiceMap.containsKey(pollSubmission.getPoll_choice_id())) {
                    csv += pollChoiceMap.get(pollSubmission.getPoll_choice_id()).getText() + ",";
                } else {
                    csv += pollSubmission.getPoll_choice_id() + ",";
                }
                csv += pollSubmission.getCreated_at() + "\n";
            }
        } else {
            csv += poll.getQuestion() + ",";
            csv += pollSession.getId() + ",";
            csv += courseMap.get(pollSession.getCourse_id()).getName() + ",";
            csv += sectionMap.get(pollSession.getCourse_section_id()).getName() + ",";
            csv += "" + ",";
            csv += "" + ",";
            csv += pollSession.getCreated_at() + "\n";
        }
    }
    // check to make sure there is external storage
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state)) {
        // it's not there, so the reset of this won't work. Let the user know.
        AppMsg.makeText(getActivity(), getString(R.string.errorGeneratingCSV), AppMsg.STYLE_ERROR).show();
        return;
    }
    File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), getString(R.string.generatedCSVFolderName));
    // Make sure the directory exists.
    boolean success = path.mkdirs();
    if (!success) {
        // return false)
        if (!path.isDirectory()) {
            // it's not a directory and wasn't created, so we need to return with an error
            AppMsg.makeText(getActivity(), getString(R.string.errorGeneratingCSV), AppMsg.STYLE_ERROR).show();
            return;
        }
    }
    Time now = new Time();
    now.setToNow();
    File file = new File(path, "csv_" + now.format3339(false) + ".csv");
    try {
        // write the string to a file
        FileWriter out = new FileWriter(file);
        out.write(csv);
        out.close();
    } catch (IOException e) {
        // Unable to create file
        AppMsg.makeText(getActivity(), getString(R.string.errorGeneratingCSV), AppMsg.STYLE_ERROR).show();
    }
    // file is generated, not share it
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    shareIntent.setType("text/csv");
    startActivity(Intent.createChooser(shareIntent, getString(R.string.shareCSV)));
}
Also used : PollSession(com.instructure.canvasapi2.models.PollSession) FileWriter(java.io.FileWriter) Time(android.text.format.Time) Intent(android.content.Intent) IOException(java.io.IOException) File(java.io.File) PollSubmission(com.instructure.canvasapi2.models.PollSubmission)

Example 3 with Poll

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

the class PollSessionListFragment method setupCallbacks.

@Override
public void setupCallbacks() {
    pollSessionCallback = new StatusCallback<PollSessionResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollSessionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            List<PollSession> pollSessions = response.body().getPollSessions();
            sessionNextUrl = linkHeaders.nextUrl;
            if (pollSessions != null) {
                for (PollSession pollSession : pollSessions) {
                    addItem(pollSession);
                    SectionManager.getSection(pollSession.getCourse_id(), pollSession.getCourse_section_id(), sectionCallback, true);
                }
            }
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    sectionCallback = new StatusCallback<Section>() {

        @Override
        public void onResponse(@NonNull Response<Section> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            sectionMap.put(response.body().getId(), response.body());
            notifyDataSetChanged();
        }
    };
    pollChoiceCallback = new StatusCallback<PollChoiceResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollChoiceResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            List<PollChoice> pollChoices = response.body().getPollChoices();
            if (pollChoices != null) {
                pollChoiceArrayList.addAll(pollChoices);
            }
            // or generate the CSV, depending on which action they selected
            if (!StatusCallback.moreCallsExist(linkHeaders)) {
                if (fromGenerateCSV) {
                    // generate a map from the array list of poll choices
                    for (PollChoice choice : pollChoiceArrayList) {
                        pollChoiceMap.put(choice.getId(), choice);
                    }
                    generateCSV();
                } else {
                    AddQuestionFragment addQuestionFragment = new AddQuestionFragment();
                    // populate the current data with the bundle
                    Bundle bundle = new Bundle();
                    bundle.putParcelable(Constants.POLL_BUNDLE, poll);
                    bundle.putParcelableArrayList(Constants.POLL_CHOICES, pollChoiceArrayList);
                    addQuestionFragment.setArguments(bundle);
                    ((FragmentManagerActivity) getActivity()).swapFragments(addQuestionFragment, AddQuestionFragment.class.getSimpleName(), R.anim.slide_in_from_bottom, 0, 0, R.anim.slide_out_to_bottom);
                }
            } else {
                // otherwise, get the next group of poll choices.
                PollsManager.getNextPagePollChoices(linkHeaders.nextUrl, pollChoiceCallback, false);
            }
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    responseCanvasCallback = new StatusCallback<ResponseBody>() {

        @Override
        public void onFail(@Nullable Call<ResponseBody> call, @NonNull Throwable error, @Nullable Response response) {
            AppMsg.makeText(getActivity(), getString(R.string.errorDeletingPollSession), AppMsg.STYLE_ERROR).show();
            // we didn't actually delete anything, but we removed the item from the list to make the animation smoother, so now
            // lets get the poll sessions again
            reloadData();
        }
    };
}
Also used : PollSession(com.instructure.canvasapi2.models.PollSession) PollChoice(com.instructure.canvasapi2.models.PollChoice) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) Bundle(android.os.Bundle) PollChoiceResponse(com.instructure.canvasapi2.models.PollChoiceResponse) Section(com.instructure.canvasapi2.models.Section) ResponseBody(okhttp3.ResponseBody) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) Response(retrofit2.Response) PollChoiceResponse(com.instructure.canvasapi2.models.PollChoiceResponse) ApiType(com.instructure.canvasapi2.utils.ApiType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Poll

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

the class QuestionListFragment method setupCallbacks.

@Override
public void setupCallbacks() {
    pollCallback = new StatusCallback<PollResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            nextUrl = linkHeaders.nextUrl;
            if (response.body().getPolls().size() == 0) {
                displayEmptyState();
            } else {
                List<Poll> polls = response.body().getPolls();
                for (Poll poll : polls) {
                    // add all the polls to a list. we'll use the list later to populate the
                    // different groups after we get some session information about each poll
                    pollList.add(poll);
                    PollsManager.getFirstPagePollSessions(poll.getId(), pollSessionCallback, true);
                }
            }
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    responseCanvasCallback = new StatusCallback<ResponseBody>() {

        @Override
        public void onResponse(@NonNull Response<ResponseBody> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (response.code() == 204) {
                if (pollToDelete != null) {
                    // reset it so we don't try to remove it from the list again
                    pollToDelete = null;
                }
            }
        }

        @Override
        public void onFail(@Nullable Call<ResponseBody> call, @NonNull Throwable error, @Nullable Response response) {
            AppMsg.makeText(getActivity(), getString(R.string.errorDeletingPoll), AppMsg.STYLE_ERROR).show();
            // we didn't actually delete anything, but we removed the item from the list to make the animation smoother, so now
            // lets get the polls again
            reloadData();
        }
    };
    pollSessionCallback = new StatusCallback<PollSessionResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollSessionResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            List<PollSession> pollSessions = response.body().getPollSessions();
            for (PollSession session : pollSessions) {
                if (session.is_published()) {
                    openSessions.put(session.getPoll_id(), session);
                    // we only care about there being one active poll session
                    break;
                } else {
                    closedSessions.put(session.getPoll_id(), session);
                }
            }
            // and add it to the "active" group
            for (Poll poll : pollList) {
                if (openSessions.containsKey(poll.getId())) {
                    removeItem(poll);
                    addItem(getString(R.string.active), poll);
                } else // if the poll doesn't have an open session or any closed sessions, it is still in the draft state
                if (!closedSessions.containsKey(poll.getId())) {
                    removeItem(poll);
                    addItem(getString(R.string.draft), poll);
                } else {
                    removeItem(poll);
                    addItem(getString(R.string.inactive), poll);
                }
            }
            expandAllGroups();
            if (linkHeaders.nextUrl != null) {
                PollsManager.getNextPagePollSessions(linkHeaders.nextUrl, pollSessionCallback, true);
            }
            notifyDataSetChanged();
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
    pollChoiceCallback = new StatusCallback<PollChoiceResponse>() {

        @Override
        public void onResponse(@NonNull Response<PollChoiceResponse> response, @NonNull LinkHeaders linkHeaders, @NonNull ApiType type) {
            if (getActivity() == null || type.isCache())
                return;
            List<PollChoice> pollChoices = response.body().getPollChoices();
            if (pollChoices != null) {
                pollChoiceArrayList.addAll(pollChoices);
            }
            // or generate the CSV, depending on which action they selected
            if (!StatusCallback.moreCallsExist(linkHeaders)) {
                AddQuestionFragment addQuestionFragment = new AddQuestionFragment();
                // populate the current data with the bundle
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constants.POLL_BUNDLE, selectedPoll);
                bundle.putParcelableArrayList(Constants.POLL_CHOICES, pollChoiceArrayList);
                addQuestionFragment.setArguments(bundle);
                ((FragmentManagerActivity) getActivity()).swapFragments(addQuestionFragment, AddQuestionFragment.class.getSimpleName());
            } else {
                // otherwise, get the next group of poll choices.
                PollsManager.getNextPagePollChoices(linkHeaders.nextUrl, pollChoiceCallback, true);
            }
        }

        @Override
        public void onFinished(ApiType type) {
            if (swipeRefreshLayout != null) {
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    };
}
Also used : PollSession(com.instructure.canvasapi2.models.PollSession) LinkHeaders(com.instructure.canvasapi2.utils.LinkHeaders) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) Bundle(android.os.Bundle) PollChoiceResponse(com.instructure.canvasapi2.models.PollChoiceResponse) ResponseBody(okhttp3.ResponseBody) PollSessionResponse(com.instructure.canvasapi2.models.PollSessionResponse) PollResponse(com.instructure.canvasapi2.models.PollResponse) Response(retrofit2.Response) PollChoiceResponse(com.instructure.canvasapi2.models.PollChoiceResponse) ApiType(com.instructure.canvasapi2.utils.ApiType) PollResponse(com.instructure.canvasapi2.models.PollResponse) Poll(com.instructure.canvasapi2.models.Poll) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Poll

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

the class PublishPollActivity method hasSectionUnpublished.

// check to see if there is a section that is unpublished
private boolean hasSectionUnpublished() {
    // we'll just use the count of open polls vs. sections in the course. If there are the same
    // amount of sections and sessions, then there all the poll sessions are published
    int sectionCount = sectionAdapter.getCount();
    if (sectionAdapter.getItem(0).getId() == Long.MIN_VALUE) {
        sectionCount--;
    }
    // now get the open sessions for this course
    int courseSessionCount = 0;
    if (currentCourse != null) {
        for (PollSession pollSession : openPollSessions) {
            if (pollSession.getCourse_id() == currentCourse.getId()) {
                courseSessionCount++;
            }
        }
    }
    if (courseSessionCount < sectionCount) {
        return true;
    } else {
        return false;
    }
}
Also used : PollSession(com.instructure.canvasapi2.models.PollSession)

Aggregations

PollSession (com.instructure.canvasapi2.models.PollSession)7 Bundle (android.os.Bundle)4 Poll (com.instructure.canvasapi2.models.Poll)3 PollChoice (com.instructure.canvasapi2.models.PollChoice)3 PollSessionResponse (com.instructure.canvasapi2.models.PollSessionResponse)3 ApiType (com.instructure.canvasapi2.utils.ApiType)3 LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)3 Response (retrofit2.Response)3 ListView (android.widget.ListView)2 FragmentManagerActivity (com.instructure.androidpolling.app.activities.FragmentManagerActivity)2 PollChoiceResponse (com.instructure.canvasapi2.models.PollChoiceResponse)2 Section (com.instructure.canvasapi2.models.Section)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ResponseBody (okhttp3.ResponseBody)2 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 Time (android.text.format.Time)1 View (android.view.View)1 LayoutAnimationController (android.view.animation.LayoutAnimationController)1