Skip to content

Commit

Permalink
SAK-42569 Polls: Allow creating options in batch (sakaiproject#7390)
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Pellicer authored Oct 1, 2019
1 parent 17adf30 commit 8418fe1
Show file tree
Hide file tree
Showing 14 changed files with 458 additions and 10 deletions.
10 changes: 10 additions & 0 deletions polls/tool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${sakai.poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${sakai.poi.version}</version>
</dependency>
<!-- RSF dependencies -->
<dependency>
<groupId>org.sakaiproject.rsf</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ new_poll_submit=Save
new_poll_saveoption=Save and add options
new_poll_option_title=Options
new_poll_option_add=Add option
new_poll_option_add_batch=Add multiple options
new_poll_option_batch_info=You can upload an Excel or a CSV file with all the options in the first column. The rest of the columns will be ignored.
new_poll_option_edit=Edit
new_poll_option_delete=Delete
new_poll_cancel=Cancel
Expand All @@ -61,6 +63,7 @@ warn_poll_has_votes=You are editing a poll that has already been voted on
new_poll_noperms=you do not have permission to add new polls in this site

new_option_title=Add an Option
new_option_batch_title=Add multiple options
edit_option_title=Edit an Option

delete_option_confirm=Remove
Expand Down Expand Up @@ -111,6 +114,7 @@ error_tofew_votes.voteCollection=You have selected too few options. (Minimum: {0
error_exact_required.voteCollection=You must select the required number of options. (Required: {0})
error_novote.voteCollection=You have selected no options. You must select at least {0}
error_no_text=Your poll must have a question
error_batch_options=You have to provide a valid file to create multiple options. Ensure the file size is accepted and is a valid Excel or CSV file.
min_greater_than_max=Minimum number of options must less than or equal to the Maximum options
close_before_open=Your poll can't close before it opens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ new_poll_submit=Desa
new_poll_saveoption=Desa i afegeix les opcions
new_poll_option_title=Opcions
new_poll_option_add=Afegeix una opci\u00f3
new_poll_option_add_batch=Afegeix m\u00faltiples opcions
new_poll_option_batch_info=Pots pujar un fitxer Excel o CSV amb totes les opcions en la primera columna. La resta de columnes s'ignoraran.
new_poll_option_edit=Edita
new_poll_option_delete=Suprimeix
new_poll_cancel=Cancel\u00b7la
Expand All @@ -60,6 +62,7 @@ warn_poll_has_votes=Esteu editant una votaci\u00f3 que ja ha rebut respostes
new_poll_noperms=no teniu autoritzaci\u00f3 per afegir votacions noves en aquest espai
new_option_title=Afegeix una opci\u00f3
new_option_batch_title=Afegeix m\u00faltiples opcions
edit_option_title=Edita les opcions
delete_option_confirm=Suprimeix
Expand Down Expand Up @@ -110,6 +113,7 @@ error_tofew_votes.voteCollection=Heu seleccionat menys opcions de les requerides
error_exact_required.voteCollection=Heu de seleccionar el n\u00famero d''opcions obligat\u00f2ries. (Obligatori\: {0})
error_novote.voteCollection=No heu seleccionat cap opci\u00f3. N''heu de seleccionar almenys {0}
error_no_text=La votaci\u00f3 ha d'incloure una pregunta
error_batch_options=Deus proporcionar un arxiu v\u00e0lid per carregar m\u00faltiples opcions. Assegura que l'arxiu t\u00e9 un tamany acceptat y \u00e9s un arxiu Excel o CSV v\u00e0lid.
min_greater_than_max=El nombre d'opcions m\u00ednim ha de ser menor o igual al nombre m\u00e0xim d'opcions
close_before_open=No podeu tancar la votaci\u00f3 abans d'obrir-la
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ new_poll_submit=Guardar
new_poll_saveoption=Guardar y a\u00f1adir opciones
new_poll_option_title=Opciones
new_poll_option_add=A\u00f1adir opci\u00f3n
new_poll_option_add_batch=A\u00f1adir m\u00faltiples opciones
new_poll_option_batch_info=Puedes subir un fichero Excel o CSV con todas las opciones en la primera columna. El resto de columnas ser\u00e1n ignoradas.
new_poll_option_edit=Editar
new_poll_option_delete=Borrar
new_poll_cancel=Cancelar
Expand All @@ -60,6 +62,7 @@ warn_poll_has_votes=Esta editando una encuesta en la que ya se ha votado
new_poll_noperms=No tiene permiso para a\u00f1adir nuevas encuestas en este sitio

new_option_title=A\u00f1adir opciones
new_option_batch_title=A\u00f1adir m\u00faltiples opciones
edit_option_title=Editar opciones

delete_option_confirm=Borrar
Expand Down Expand Up @@ -110,6 +113,7 @@ error_tofew_votes.voteCollection=Ha seleccionado menos opciones de las requerida
error_exact_required.voteCollection=Debe seleccionar el n\u00famero de opciones requerido. (Requerido\: {0})
error_novote.voteCollection=No ha seleccionado opciones. Debe escoger al menos {0}
error_no_text=Su encuesta debe tener una pregunta
error_batch_options=Debes proporcionar un fichero v\u00e1lido para cargar m\u00faltiples opciones. Asegura que el fichero tiene un tama\u00f1o aceptado y es un fichero Excel o CSV v\u00e1lido.
min_greater_than_max=El m\u00ednimo n\u00famero de opciones debe ser menor o igual que el m\u00e1ximo de opciones
close_before_open=Su encuesta no se puede cerrar antes de abrirla

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ new_poll_submit=Gorde
new_poll_saveoption=Gorde eta gehitu aukerak
new_poll_option_title=Aukerak
new_poll_option_add=Gehitu aukera
new_option_batch_title=Erantsi aukera bat baino gehiago.
error_batch_options=Aukera bat baino gehiago kargatzeko balio duen fitxategi bat eman behar duzu. Ziurtatu Excel edo CSV motako baliozko fitxategi bat dela eta onartzeko moduko tamaina duela.
new_poll_option_edit=Editatu
new_poll_option_delete=Ezabatu
new_poll_cancel=Utzi
Expand All @@ -60,6 +62,7 @@ warn_poll_has_votes=Editatzen ari zaren zundaketari botoak eman zaizkio hemen\:
new_poll_noperms=ez duzu baimenik zundaketa berriak gehitzeko gune honetan

new_option_title=Gehitu aukera bat
new_option_batch_title=Erantsi aukera bat baino gehiago.
edit_option_title=Editatu aukera bat

delete_option_confirm=Kendu
Expand Down Expand Up @@ -110,6 +113,7 @@ error_tofew_votes.voteCollection=Aukera gutxiegi hautatu duzu. (Gutxienez\: {0})
error_exact_required.voteCollection=Eskatzen den aukera zenbakia hautatu behar duzu. (Zenbakia\: {0})
error_novote.voteCollection=Ez duzu aukerarik hautatu. Gutxienez {0} hautatu behar duzu
error_no_text=Zure zundaketak galdera bat eduki behar du
error_batch_options=Aukera bat baino gehiago kargatzeko balio duen fitxategi bat eman behar duzu. Ziurtatu Excel edo CSV motako baliozko fitxategi bat dela eta onartzeko moduko tamaina duela.
min_greater_than_max=Gutxienezko aukera kopuruak izan behar du Gehienezko aukera kopuruaren berdina edo txikiagoa
close_before_open=Zure zundaketa ezin da itxi ireki baino lehenago

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**********************************************************************************
Copyright (c) 2019 Apereo Foundation
Licensed under the Educational Community License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://opensource.org/licenses/ecl2
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**********************************************************************************/

package org.sakaiproject.poll.tool.params;

import uk.org.ponder.rsf.viewstate.SimpleViewParameters;

public class OptionBatchViewParameters extends SimpleViewParameters {
public String pollId;

public OptionBatchViewParameters() {
}

public OptionBatchViewParameters(String viewId) {
this.viewID = viewId;
}

public OptionBatchViewParameters(String viewId, String pollId) {
this.viewID = viewId;
this.pollId = pollId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.sakaiproject.poll.tool.params;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -30,7 +31,9 @@
import java.util.Map;
import java.util.Set;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import org.sakaiproject.poll.logic.ExternalLogic;
import org.sakaiproject.poll.logic.PollListManager;
Expand All @@ -39,6 +42,7 @@
import org.sakaiproject.poll.model.Poll;
import org.sakaiproject.poll.model.Vote;
import org.sakaiproject.poll.model.VoteCollection;
import org.sakaiproject.poll.tool.util.OptionsFileConverterUtil;
import org.sakaiproject.poll.util.PollUtils;
import org.sakaiproject.util.FormattedText;

Expand Down Expand Up @@ -83,6 +87,9 @@ public class PollToolBean {
//how to handle orphaned votes when deleting an option
private String handleOrphanVotes;

@Setter
public Map<String, MultipartFile> multipartMap;

public Map perms = null;
public void setRoleperms(Map perms)
{
Expand Down Expand Up @@ -288,13 +295,93 @@ public String proccessActionAddOption() {
manager.saveOption(option);
log.debug("Succesuly save option with id" + option.getId());

if ("option".equals(submissionStatus))
return "option";
else
return "Saved";
String action = "Saved";
switch(submissionStatus) {
case "option":
action = "option";
break;
case "batch":
action = "batch";
break;
default:
break;
}
return action;
}

public String processActionAddOptionBatch() {
log.debug("processActionAddOptionBatch");
if ("cancel".equals(submissionStatus)){
log.debug("processActionAddOptionBatch: cancel");
return "cancel";
}

Long pollId = option.getPollId();
if (pollId == null) {
messages.addMessage(new TargettedMessage("error_batch_options","no file"));
throw new IllegalArgumentException("error_batch_options");
}

MultipartFile file = null;

if (multipartMap.size() > 0) {
log.debug("The multipartMap is not empty so retrieving the file.");
// user specified a file, create it
file = multipartMap.values().iterator().next();
//Clear the map to get new file items.
multipartMap.clear();
}

boolean fileError = false;
if (file != null) {
log.debug("File uploaded successfully with contentType {}, size {} and name {}", file.getContentType(), file.getSize(), file.getOriginalFilename());
try{
switch(file.getContentType()){
case "application/octet-stream":
case "text/plain":
case "text/csv":
case "application/vnd.ms-excel":
case "application/msexcel":
case "application/x-msexcel":
case "application/x-ms-excel":
case "application/x-excel":
case "application/x-dos_ms_excel":
case "application/xls":
case "application/x-xls":
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
List<String> optionsList = OptionsFileConverterUtil.convertInputStreamToOptionList(file.getInputStream());
for(String optionString : optionsList){
Option newOption = new Option();
newOption.setPollId(pollId);
newOption.setText(PollUtils.cleanupHtmlPtags(optionString));
newOption.setOptionOrder(manager.getOptionsForPoll(pollId).size());
manager.saveOption(newOption);
log.debug("Option with id {} successfully saved.", newOption.getId());
}
break;
default:
log.warn("File mimetype not accepted {}.", file.getContentType());
throw new IOException();
}
} catch(Exception ex) {
log.warn("Error converting the input file into options.", ex);
fileError = true;
}
} else {
log.warn("The uploaded file object is null.");
fileError = true;
}

if (fileError) {
log.debug("There was a problem processing the file.");
messages.addMessage(new TargettedMessage("error_batch_options", "no file"));
throw new IllegalArgumentException("error_batch_options");
}

log.debug("processActionAddOptionBatch: Saved");
return "Saved";
}


public Poll proccessActionDeleteOption() {
log.info("about to delete option " + option.getId());
Long pollId = option.getPollId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.sakaiproject.poll.model.Poll;
import org.sakaiproject.poll.model.Vote;
import org.sakaiproject.poll.tool.params.OptionViewParameters;
import org.sakaiproject.poll.tool.params.OptionBatchViewParameters;
import org.sakaiproject.poll.tool.params.PollViewParameters;
import org.sakaiproject.poll.tool.params.VoteBean;
import org.sakaiproject.util.FormattedText;
Expand Down Expand Up @@ -202,6 +203,8 @@ public void fillComponents(UIContainer tofill, ViewParameters viewparams,
UIMessage.make(actionBlock,"options-title","new_poll_option_title");
UIInternalLink.make(actionBlock,"option-add",UIMessage.make("new_poll_option_add"),
new OptionViewParameters(PollOptionProducer.VIEW_ID, null, poll.getPollId().toString()));
UIInternalLink.make(actionBlock,"option-add-batch",UIMessage.make("new_poll_option_add_batch"),
new OptionBatchViewParameters(PollOptionBatchProducer.VIEW_ID, poll.getPollId().toString()));

List<Vote> votes = pollVoteManager.getAllVotesForPoll(poll);
if (votes != null && votes.size() > 0 ) {
Expand Down Expand Up @@ -355,6 +358,7 @@ public List<NavigationCase> reportNavigationCases() {
togo.add(new NavigationCase(null, new SimpleViewParameters(VIEW_ID)));
togo.add(new NavigationCase("added", new SimpleViewParameters(PollToolProducer.VIEW_ID)));
togo.add(new NavigationCase("option", new OptionViewParameters(PollOptionProducer.VIEW_ID, null, null)));
togo.add(new NavigationCase("option_batch", new OptionViewParameters(PollOptionBatchProducer.VIEW_ID, null, null)));
togo.add(new NavigationCase("cancel", new SimpleViewParameters(PollToolProducer.VIEW_ID)));
return togo;
}
Expand Down
Loading

0 comments on commit 8418fe1

Please sign in to comment.