...
 
Commits (4)
......@@ -17,7 +17,7 @@
<div>Vous êtes connectés</div>
<s:property value="connectedUser.usertype"/>
<div class="">
<a href="login.html">Voir liste questionnaires</a>
<s:a class="btn btn-primary" action="questionnaireListAction">Voir liste questionnaires</s:a>
<a href="formulaire.html">Visualisation anciens résultats</a>
<a href="accueil.html">Retour accueil</a>
<a href="index.html">Se déconnecter</a>
......
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Formulaire</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="style/main.css" />
</head>
<body>
<div class="container">
<s:set var="questionIndex" value="questionNumber"/>
<h1><s:property value="currentQuestion.heading"/></h1>
<s:iterator value="currentAnswers" var="answer">
<div>
<s:url var="test" action="answerQuestion">
<s:param name="chosenAnswerId" value="%{#answer.id}"></s:param>
</s:url>
<a href="${test}" class="btn btn-secondary"><s:property value="%{#answer.heading}"/></a>
</div>
</s:iterator>
</div>
</body>
</html>
\ No newline at end of file
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Formulaire</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="style/main.css" />
</head>
<body>
<div class="container">
<h1>Active quizzes</h1>
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Quiz</th>
<th>Score</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<s:iterator value="questionnaireList" var="questionnaire">
<s:if test="%{#questionnaire.active==1}">
<tr>
<td><s:property value="%{#questionnaire.subject}"/></td>
<td>TODO</td>
<s:url var="test" action="doQuestionnaire">
<s:param name="questionnaireID" value="%{#questionnaire.id}"></s:param>
</s:url>
<a href="${test}" class="btn btn-secondary">Start</a>
</td>
</tr>
</s:if>
</s:iterator>
</tbody>
<tfoot>
<tr>
<th>Quiz</th>
<th>Score</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div>
</body>
</html>
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#example').DataTable();
});
</script>
......@@ -84,7 +84,7 @@ public class LoginAction extends ActionSupport implements SessionAware{
}
@Override
public void setSession(Map<String, Object> arg0) {
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
......
package action;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import db.ConnectionDB;
import db.QuestionnaireDAO;
import model.Answer;
import model.Question;
import model.Questionnaire;
public class parcoursAction extends ActionSupport implements SessionAware{
private static QuestionnaireDAO questionnaireDAO = new QuestionnaireDAO();
private Map<String, Object> session = ActionContext.getContext().getSession();
private Questionnaire currentQuestionnaire;
private String questionnaireID;
private Question currentQuestion;
private ArrayList<Answer> currentAnswers;
private int questionNumber;
private int chosenAnswerId;
private ArrayList<Integer> chosenAnswerIds;
private int numberOfGoodAnswers;
private int spentTime;
@Override
public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}
public String init() {
System.out.println("Bonjour");
try {
Connection connection = ConnectionDB.getConnexion();
Statement stmt= connection.createStatement();
HashMap<String, String> params = new HashMap<String, String>();
chosenAnswerIds = new ArrayList<Integer>();
params.put("id",questionnaireID);
currentQuestionnaire = questionnaireDAO.getNoneOptionnal(params);
numberOfGoodAnswers = 0;
session.put("currentQuestionnaire", currentQuestionnaire);
session.put("questionNumber", 0);
session.put("chosenAnswerIds", chosenAnswerIds);
session.put("numberOfGoodAnswers", numberOfGoodAnswers);
currentQuestion = currentQuestionnaire.getQuestions().get(questionNumber);
currentAnswers = currentQuestion.getAnswers();
questionNumber = 0;
return SUCCESS;
}catch(Exception e) {
e.printStackTrace();
}
return ERROR;
}
// FAIRE LE CHOIX
public String answerQuestion() {
try {
Connection connection = ConnectionDB.getConnexion();
Statement stmt= connection.createStatement();
HashMap<String, String> params = new HashMap<String, String>();
params.put("id",questionnaireID);
currentQuestionnaire = (Questionnaire)session.get("currentQuestionnaire");
questionNumber = (int)session.get("questionNumber");
chosenAnswerIds = (ArrayList<Integer>)session.get("chosenAnswerIds");
numberOfGoodAnswers = (int)session.get("numberOfGoodAnswers");
chosenAnswerIds.add(chosenAnswerId);
for(Answer a : currentQuestionnaire.getQuestions().get(questionNumber).getAnswers()) {
if(a.getId() == chosenAnswerId) {
if(a.isCorrect()) {
numberOfGoodAnswers++;
}
}
}
questionNumber++;
session.put("questionNumber", questionNumber);
session.put("chosenAnswerIds", chosenAnswerIds);
session.put("numberOfGoodAnswers", numberOfGoodAnswers);
currentQuestion = currentQuestionnaire.getQuestions().get(questionNumber);
currentAnswers = currentQuestion.getAnswers();
return SUCCESS;
}catch(Exception e) {
e.printStackTrace();
}
return ERROR;
}
/**
* @return the questionnaireID
*/
public String getQuestionnaireID() {
return questionnaireID;
}
/**
* @param questionnaireID the questionnaireID to set
*/
public void setQuestionnaireID(String questionnaireID) {
this.questionnaireID = questionnaireID;
}
/**
* @return the currentQuestionnaire
*/
public Questionnaire getCurrentQuestionnaire() {
return currentQuestionnaire;
}
/**
* @param currentQuestionnaire the currentQuestionnaire to set
*/
public void setCurrentQuestionnaire(Questionnaire currentQuestionnaire) {
this.currentQuestionnaire = currentQuestionnaire;
}
/**
* @return the currentQuestion
*/
public Question getCurrentQuestion() {
return currentQuestion;
}
/**
* @param currentQuestion the currentQuestion to set
*/
public void setCurrentQuestion(Question currentQuestion) {
this.currentQuestion = currentQuestion;
}
/**
* @return the questionNumber
*/
public int getQuestionNumber() {
return questionNumber;
}
/**
* @param questionNumber the questionNumber to set
*/
public void setQuestionNumber(int questionNumber) {
this.questionNumber = questionNumber;
}
public ArrayList<Answer> getCurrentAnswers() {
return currentAnswers;
}
public void setCurrentAnswers(ArrayList<Answer> currentAnswers) {
this.currentAnswers = currentAnswers;
}
public int getChosenAnswerId() {
return chosenAnswerId;
}
public void setChosenAnswerId(int chosenAnswerId) {
this.chosenAnswerId = chosenAnswerId;
}
/**
* @return the numberOfGoodAnswers
*/
public int getNumberOfGoodAnswers() {
return numberOfGoodAnswers;
}
/**
* @param numberOfGoodAnswers the numberOfGoodAnswers to set
*/
public void setNumberOfGoodAnswers(int numberOfGoodAnswers) {
this.numberOfGoodAnswers = numberOfGoodAnswers;
}
public int getSpentTime() {
return spentTime;
}
public void setSpentTime(int spentTime) {
this.spentTime = spentTime;
}
}
package action;
import java.util.Optional;
import org.apache.struts2.interceptor.SessionAware;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import db.ConnectionDB;
import db.QuestionnaireDAO;
import model.Questionnaire;
import model.User;
public class questionnaireListAction extends ActionSupport{
public class questionnaireListAction extends ActionSupport implements SessionAware{
private ArrayList<model.Questionnaire> questionnaireList;
private static QuestionnaireDAO questionnaireDAO = new QuestionnaireDAO();
private String questionnaireID;
private Map<String, Object> session;
public String questionnaireList() {
session = ActionContext.getContext().getSession();
try {
setQuestionnaireList(questionnaireDAO.getAll());
return SUCCESS;
if(((User) session.get("connectedUser")).getUsertype().equals("admin"))
return "success_admin";
else
return "success_intern";
}catch (Exception e) {
e.printStackTrace();
}
......@@ -29,6 +41,8 @@ public class questionnaireListAction extends ActionSupport{
return ERROR;
}
public String disableOrEnableQuestionnaire() {
try {
Connection connection = ConnectionDB.getConnexion();
......@@ -49,6 +63,21 @@ public class questionnaireListAction extends ActionSupport{
return ERROR;
}
public String questionnaireListPres() {
session = ActionContext.getContext().getSession();
try {
setQuestionnaireList(questionnaireDAO.getAll());
if(((User) session.get("connectedUser")).getUsertype().equals("admin"))
return "success_admin";
else
return "success_intern";
}catch (Exception e) {
e.printStackTrace();
}
return ERROR;
}
/**
* @return the questionnaireList
......@@ -71,6 +100,14 @@ public class questionnaireListAction extends ActionSupport{
public void setQuestionnaireID(String questionnaireID) {
this.questionnaireID = questionnaireID;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
......
......@@ -12,7 +12,7 @@ public class ConnectionDB {
if(connection == null){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sr03formulaires?autoReconnect=true&useSSL=false", "root", "");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sr03formulaires?autoReconnect=true&useSSL=false", "tylones", "mdp");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
......
......@@ -73,13 +73,37 @@ public class QuestionnaireDAO implements DAO<Questionnaire> {
if(params.containsKey("id")) {
try {
Connection connection = ConnectionDB.getConnexion();
String id = params.get("id");
String sql = "SELECT id, subject, active FROM questionnaire WHERE id = ?";
PreparedStatement st = connection.prepareStatement(sql);
st.setString(1, id);
ResultSet QuestionnaireRS = st.executeQuery();
if(QuestionnaireRS.next()) {
Questionnaire q = new Questionnaire(QuestionnaireRS.getString("subject"));
String id = params.get("id");
String sql = "SELECT id, subject, active FROM questionnaire WHERE id = ?";
PreparedStatement st = connection.prepareStatement(sql);
st.setString(1, id);
ResultSet QuestionnaireRS = st.executeQuery();
if(QuestionnaireRS.next()) {
Questionnaire q = new Questionnaire(QuestionnaireRS.getString("subject"));
sql = "SELECT id, heading, active, numorder FROM question WHERE questionnaire = ?";
st = connection.prepareStatement(sql);
st.setString(1, id);
ArrayList<Question> questionList = new ArrayList<Question>();
ResultSet QuestionRS = st.executeQuery();
if(QuestionRS.next()) {
Question question = new Question(QuestionRS.getString("heading"),
QuestionRS.getBoolean("active"), QuestionRS.getInt("numorder"));
questionList.add(question);
sql = "SELECT id, intitule, correct, active FROM answer WHERE question = ?";
PreparedStatement answersStmt = connection.prepareStatement(sql);
answersStmt.setString(1, QuestionRS.getString("id"));
ResultSet answerRS = answersStmt.executeQuery();
ArrayList<Answer> answers = new ArrayList<Answer>();
while(answerRS.next()) {
answers.add(new Answer(answerRS.getInt("id"), answerRS.getString("intitule"), answerRS.getBoolean("correct"), answerRS.getBoolean("active")));
}
question.setAnswers(answers);
questionList.add(question);
sql = "SELECT id, heading, active, numorder FROM question WHERE questionnaire = ?";
st = connection.prepareStatement(sql);
......
......@@ -4,6 +4,7 @@ public class Answer {
private String heading;
private boolean active;
private boolean correct;
private int id;
public Answer(String heading, boolean active, boolean correct) {
super();
......@@ -12,6 +13,14 @@ public class Answer {
this.correct = correct;
}
public Answer(int id, String heading, boolean active, boolean correct) {
super();
this.id = id;
this.heading = heading;
this.active = active;
this.correct = correct;
}
public String getHeading() {
return heading;
}
......@@ -30,5 +39,13 @@ public class Answer {
public void setCorrect(boolean correct) {
this.correct = correct;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
......@@ -52,5 +52,13 @@ public class Question {
public void setNumorder(int numorder) {
this.numorder = numorder;
}
public ArrayList<Answer> getAnswers() {
return answers;
}
public void setAnswers(ArrayList<Answer> answers) {
this.answers = answers;
}
}
......@@ -36,12 +36,21 @@
<result name="success">/userList.jsp</result>
</action>
<action name="questionnaireListAction" class="action.questionnaireListAction" method="questionnaireList">
<result name="success">/questionnaireList.jsp</result>
<result name="success_admin">/questionnaireList.jsp</result>
<result name="success_intern">/questionnaireListUser.jsp</result>
</action>
<action name="enableOrDisableQuestionnaire" class="action.questionnaireListAction" method="disableOrEnableQuestionnaire">
<result name="success" type="redirectAction">questionnaireListAction</result>
<result name="error" type="redirectAction">questionnaireListAction</result>
</action>
<action name="doQuestionnaire" class="action.parcoursAction" method="init">
<result name="success">/answerQuestion.jsp</result>
</action>
<action name="answerQuestion" class="action.parcoursAction" method="answerQuestion">
<result name="success">/answerQuestion.jsp</result>
</action>
</package>
</struts>
\ No newline at end of file