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)));
}
Aggregations