Search in sources :

Example 1 with PollSubmission

use of com.instructure.canvasapi2.models.PollSubmission 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)

Aggregations

Intent (android.content.Intent)1 Time (android.text.format.Time)1 PollSession (com.instructure.canvasapi2.models.PollSession)1 PollSubmission (com.instructure.canvasapi2.models.PollSubmission)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1