Skip to content

Commit

Permalink
feat: add ability to delete survey pages and duplicate survey questions
Browse files Browse the repository at this point in the history
Signed-off-by: Oleh Astappiev <[email protected]>
  • Loading branch information
astappiev committed Mar 6, 2024
1 parent 4dbe70d commit 4c77813
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 109 deletions.
3 changes: 2 additions & 1 deletion src/main/java/de/l3s/learnweb/resource/survey/SurveyDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,9 @@ public SurveyQuestion map(final ResultSet rs, final StatementContext ctx) throws
class SurveyQuestionOptionMapper implements RowMapper<SurveyQuestionOption> {
@Override
public SurveyQuestionOption map(final ResultSet rs, final StatementContext ctx) throws SQLException {
SurveyQuestionOption answer = new SurveyQuestionOption(rs.getInt("question_id"));
SurveyQuestionOption answer = new SurveyQuestionOption();
answer.setId(rs.getInt("option_id"));
answer.setQuestionId(rs.getInt("question_id"));
answer.setDeleted(rs.getBoolean("deleted"));
answer.setValue(rs.getString("value"));
return answer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public void onMovePage(SurveyPage page, int direction) {
resource.getPages().sort(Comparator.comparingInt(SurveyPage::getOrder));
}

public void onDeletePage(SurveyPage page) {
page.setDeleted(true);
surveyDao.savePage(page);
resource.getPages().remove(page);
}

public List<SurveyPage> getPages() {
return resource.getPages().stream().filter(page -> !page.isDeleted()).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void onQuestionChange(SurveyQuestion question) {
}

public void onQuestionOptionAdd(SurveyQuestion question) {
question.getOptions().add(new SurveyQuestionOption(question.getId()));
question.getOptions().add(new SurveyQuestionOption());
}

public void onQuestionOptionChange(SurveyQuestion question, SurveyQuestionOption answer) {
Expand Down Expand Up @@ -109,4 +109,12 @@ public void onDeleteQuestion(SurveyPage page, SurveyQuestion question) {
surveyDao.saveQuestion(question);
page.getQuestions().remove(question);
}

public void onDuplicateQuestion(SurveyPage page, SurveyQuestion question) {
SurveyQuestion duplicate = new SurveyQuestion(question);
duplicate.setOrder(page.getQuestions().size());
duplicate.setPageId(page.getId());
surveyDao.saveQuestion(duplicate);
page.getQuestions().add(duplicate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ public SurveyQuestion(SurveyQuestion question) {
this.maxLength = question.maxLength;
this.type = question.type;

for (SurveyQuestionOption answer : question.getOptions()) {
answer.setId(0);
options.add(answer);
}
question.getOptions().stream()
.filter(option -> !option.isDeleted())
.forEach(option -> options.add(new SurveyQuestionOption(option.getValue())));
}

public QuestionType getType() {
Expand All @@ -111,7 +110,7 @@ public void setType(QuestionType type) {
this.type = type;

if (type.options && this.getOptions().isEmpty()) {
this.getOptions().add(new SurveyQuestionOption(id));
this.getOptions().add(new SurveyQuestionOption());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public class SurveyQuestionOption implements HasId, Deletable, Serializable {
private boolean deleted;
private String value;

public SurveyQuestionOption(int questionId) {
this.questionId = questionId;
public SurveyQuestionOption() {
}

public SurveyQuestionOption(String value) {
this.value = value;
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/de/l3s/learnweb/lang/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,10 @@ survey.mod_add_header = Add header
survey.mod_add_page = Add page
survey.mod_add_question = Add question
survey.mod_cant_edit = You can't edit this survey.
survey.mod_duplicate = Duplicate
survey.mod_delete_option = Delete option
survey.mod_delete_question = Delete question
survey.mod_delete_page = Delete page
survey.mod_delete_question_confirmation = Are you sure you want to delete this question?
survey.mod_description_text = Description
survey.mod_placeholder_text = Placeholder (example text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,21 @@

<h:panelGroup id="questions" layout="block">
<ui:repeat id="qi" value="#{page.questions}" var="question" varStatus="questionStatus">
<h:form id="question_edit" styleClass="survey-question bg-body-secondary mb-3 d-flex flex-nowrap" rendered="#{!question.deleted}">
<div class="d-flex flex-column justify-content-center bg-primary">
<p:commandButton id="a" alt="#{msg['move_up']}" styleClass="ui-button-flat text-dark mx-0" icon="fas fa-angle-up" rendered="#{!questionStatus.first}"
actionListener="#{surveyPageEditBean.onMoveQuestion(page, question, -1)}" update="questions" process="@this"/>
<p:commandButton id="b" alt="#{msg['move_down']}" styleClass="ui-button-flat text-dark mx-0" icon="fas fa-angle-down" rendered="#{!questionStatus.last}"
action="#{surveyPageEditBean.onMoveQuestion(page, question, 1)}" update="questions" process="@this"/>
</div>

<div class="flex-grow-1 p-3">
<div class="mb-3 ui-fluid">
<ui:decorate template="/WEB-INF/templates/blocks/survey/snippet_question_edit.xhtml">
<ui:param name="question" value="#{question}"/>
</ui:decorate>
</div>
<div class="d-flex flex-nowrap justify-content-end">
<p:commandButton value="#{msg['delete']}" actionListener="#{surveyPageEditBean.onDeleteQuestion(page, question)}" immediate="true" id="c" update="questions" styleClass="ui-button-danger ui-button-flat">
<p:confirm message="#{msg['survey.mod_delete_question_confirmation']}"/>
</p:commandButton>
</div>
</div>
</h:form>
<ui:decorate template="/WEB-INF/templates/blocks/survey/snippet_question_edit.xhtml">
<ui:param name="page" value="#{page}"/>
<ui:param name="question" value="#{question}"/>
<ui:param name="questionStatus" value="#{questionStatus}"/>
</ui:decorate>
</ui:repeat>
</h:panelGroup>

<div class="d-flex flex-nowrap">
<p:commandButton form="page_edit" action="#{surveyPageEditBean.onAddQuestion(page)}" styleClass="ui-button-success ui-button-flat" value="#{msg['survey.mod_add_question']}"
process="@this" immediate="true" update="questions"/>
<p:commandButton form="page_edit" action="#{surveyPageEditBean.onAddHeader(page)}" styleClass="ui-button-success ui-button-flat" value="#{msg['survey.mod_add_header']}"
process="@this" immediate="true" update="questions"/>
<p:commandButton form="page_edit" styleClass="ui-button-success ui-button-flat" value="#{msg['survey.mod_add_question']}"
action="#{surveyPageEditBean.onAddQuestion(page)}" process="@this" immediate="true" update="questions"/>
<p:commandButton form="page_edit" styleClass="ui-button-success ui-button-flat" value="#{msg['survey.mod_add_header']}"
action="#{surveyPageEditBean.onAddHeader(page)}" process="@this" immediate="true" update="questions"/>
<p:commandButton form="page_edit" styleClass="ms-auto ui-button-danger ui-button-flat" value="#{msg['survey.mod_delete_page']}"
action="#{surveyEditBean.onDeletePage(page)}" process="@this" immediate="true" update="pages" rendered="#{not empty pageStatus}"/>
</div>
</div>
</f:subview>
Expand Down
Loading

0 comments on commit 4c77813

Please sign in to comment.