* SurveyProcessListener angefangen
 * SurveyAnswerCollection erzeugt
 * SurveyResponseCollection erzeugt

Diverse Anpassungen in survey.pdl

git-svn-id: https://svn.libreccm.org/ccm/trunk@357 8810af33-2d31-482b-a856-94f89814c4df
master
quasi 2010-02-23 09:31:57 +00:00
parent 3d0cf2aee4
commit b7f5b05a1e
6 changed files with 273 additions and 218 deletions

View File

@ -10,22 +10,22 @@ object type Survey extends ContentPage {
Boolean[0..1] responsesPublic = ct_surveys.responses_public; Boolean[0..1] responsesPublic = ct_surveys.responses_public;
component FormSection[1..1] form = join ct_surveys.form_id component FormSection[1..1] form = join ct_surveys.form_id
to bebop_form_sections.form_section_id; to bebop_form_sections.form_section_id;
component SurveyResponse[0..n] responses = join ct_surveys.survey_id
to ct_surveys_responses.survey_id;
reference key ( ct_surveys.survey_id ); reference key ( ct_surveys.survey_id );
} }
object type SurveyResponse extends ContentItem { object type SurveyResponse extends ContentItem {
Date[1..1] entryDate = ct_surveys_responses.entry_date DATE; Date[1..1] entryDate = ct_surveys_responses.entry_date DATE;
Survey[1..1] survey = join ct_surveys_responses.survey_id User[0..1] user = join ct_surveys_responses.user_id
to ct_surveys.survey_id;
User[1..1] user = join ct_surveys_responses.user_id
to users.user_id; to users.user_id;
component SurveyAnswer[1..n] answers = join ct_surveys_responses.response_id component SurveyAnswer[1..n] answers = join ct_surveys_responses.response_id
to ct_surveys_answers.response_id; to ct_surveys_answers.response_id;
// XXX hack to allow us to filter a data query // XXX hack to allow us to filter a data query
BigDecimal[1..1] surveyID = ct_surveys_responses.survey_id INTEGER; // BigDecimal[1..1] surveyID = ct_surveys_responses.survey_id INTEGER;
BigDecimal[1..1] userID = ct_surveys_responses.user_id INTEGER; // BigDecimal[1..1] userID = ct_surveys_responses.user_id INTEGER;
reference key (ct_surveys_responses.response_id); reference key (ct_surveys_responses.response_id);
} }

View File

@ -10,11 +10,12 @@ import com.arsdigita.domain.DataObjectNotFoundException;
import com.arsdigita.formbuilder.PersistentForm; import com.arsdigita.formbuilder.PersistentForm;
import com.arsdigita.kernel.User;
import java.util.Date; import java.util.Date;
/** /**
* A survey content type that represents a survey. This is * A survey content type that represents a survey. This is partially based on
* based on the simplesurvey application. * the simplesurvey application and CT FormItem.
* *
* @author Sören Bernstein * @author Sören Bernstein
* *
@ -23,6 +24,8 @@ public class Survey extends ContentPage {
/** PDL property name for formSection */ /** PDL property name for formSection */
public static final String FORM = "form"; public static final String FORM = "form";
/** PDL property name for surveyResponses */
public static final String RESPONSES = "responses";
/** PDL property name for startDate */ /** PDL property name for startDate */
public static final String START_DATE = "startDate"; public static final String START_DATE = "startDate";
/** PDL property name for endDate */ /** PDL property name for endDate */
@ -43,7 +46,6 @@ public class Survey extends ContentPage {
return s_config; return s_config;
} }
*/ */
/** /**
* Default constructor. This creates a new (empty) Survey. * Default constructor. This creates a new (empty) Survey.
**/ **/
@ -98,17 +100,6 @@ public class Survey extends ContentPage {
super(type); super(type);
} }
/**
* For new content items, sets the associated content type if it
* has not been already set.
*/
/* @Override
public void beforeSave() {
super.beforeSave();
Assert.exists(getContentType(), ContentType.class);
}
*/
/** /**
* This will handle the mandatory FormSection. If there is no * This will handle the mandatory FormSection. If there is no
* FormSection set it will create an empty new form and assign it * FormSection set it will create an empty new form and assign it
@ -123,7 +114,9 @@ public class Survey extends ContentPage {
form.setAdminName(getName()); form.setAdminName(getName());
setAssociation(FORM, form); setAssociation(FORM, form);
} }
/* /*
// Preset the responsesPublic
if (getResponsesPublic() == null) { if (getResponsesPublic() == null) {
setResponsesPublic(false); setResponsesPublic(false);
} }
@ -166,39 +159,31 @@ public class Survey extends ContentPage {
set(RESPONSES_PUBLIC, responsesPublic); set(RESPONSES_PUBLIC, responsesPublic);
} }
/* Class methods *********************************************************/ public SurveyResponse addResponse() {
public static Survey retrieve(BigDecimal id) SurveyResponse surveyResponse = new SurveyResponse();
throws DataObjectNotFoundException { addResponse(surveyResponse);
return surveyResponse;
Survey survey = new Survey(id);
return survey;
} }
public static Survey retrieve(DataObject obj) { protected void addResponse(SurveyResponse surveyResponse) {
Survey survey = new Survey(obj); add(RESPONSES, surveyResponse);
return survey;
} }
/*
public SurveyResponseCollection getResponses() { public SurveyResponseCollection getResponses() {
return SurveyResponse.retrieveBySurvey(this); return new SurveyResponseCollection ((DataCollection) get(RESPONSES));
} }
public SurveyResponseCollection getUserResponses(User user) { public SurveyResponseCollection getResponses(User user) {
return SurveyResponse.retrieveBySurvey(this, user); return new SurveyResponseCollection ((DataCollection) get(RESPONSES), user);
} }
public boolean hasUserResponded(User user) { public boolean hasResponses() {
SurveyResponseCollection responses = getUserResponses(user); return !this.getResponses().isEmpty();
}
if (responses.next()) { public boolean hasResponses(User user) {
responses.close(); return !this.getResponses(user).isEmpty();
return true;
} }
return false;
}
*/
/* /*
public DataQuery getLabelDataQuery() { public DataQuery getLabelDataQuery() {

View File

@ -0,0 +1,41 @@
package com.arsdigita.cms.contenttypes;
import com.arsdigita.domain.DomainCollection;
import com.arsdigita.persistence.DataCollection;
/**
*
* @author Sören Bernstein
*/
public class SurveyAnswerCollection extends DomainCollection {
/**
* Creates a new instance of SurveyAnswerCollection
*/
public SurveyAnswerCollection(DataCollection dataCollection) {
super(dataCollection);
// m_dataCollection.addOrder(ORDER);
}
// Get the label
public String getLabel() {
return (String) m_dataCollection.get(SurveyAnswer.LABEL);
}
// Get the widget
public String getWidget() {
return (String) m_dataCollection.get(SurveyAnswer.WIDGET);
}
// Get the value
public String getValue() {
return (String) m_dataCollection.get(SurveyAnswer.VALUE);
}
public SurveyAnswer getSurveyAnswer() {
return new SurveyAnswer(m_dataCollection.getDataObject());
}
}

View File

@ -5,6 +5,7 @@ import com.arsdigita.domain.DataObjectNotFoundException;
import com.arsdigita.formbuilder.PersistentLabel; import com.arsdigita.formbuilder.PersistentLabel;
import com.arsdigita.formbuilder.PersistentWidget; import com.arsdigita.formbuilder.PersistentWidget;
import com.arsdigita.kernel.User; import com.arsdigita.kernel.User;
import com.arsdigita.persistence.DataCollection;
import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.DataObject;
import com.arsdigita.persistence.OID; import com.arsdigita.persistence.OID;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -33,17 +34,28 @@ public class SurveyResponse extends ContentItem {
public static final String BASE_DATA_OBJECT_TYPE = "com.arsdigita.cms.contenttypes.SurveyResponse"; public static final String BASE_DATA_OBJECT_TYPE = "com.arsdigita.cms.contenttypes.SurveyResponse";
/** /**
* Default constructor. This creates a new (empty) Survey. * Default constructor. This creates a new SurveyResponse. There can't be a
* SurveyResponse without a proper Survey object
*
* @param survey The <code>survey</code> for this SurveyResponse.
**/ **/
public SurveyResponse() { public SurveyResponse() {
this(BASE_DATA_OBJECT_TYPE); this(BASE_DATA_OBJECT_TYPE);
// Save the date
setEntryDate();
// Save the corresponding survey
// setSurvey(survey);
// XXX hack - see pdl file
// set(USER + "ID", user.getID());
} }
/** /**
* Constructor. The contained <code>DataObject</code> is retrieved * Constructor. The contained <code>DataObject</code> is retrieved
* from the persistent storage mechanism with an <code>OID</code> * from the persistent storage mechanism with an <code>OID</code>
* specified by <i>id</i> and * specified by <i>id</i>.
* <code>Address.BASE_DATA_OBJECT_TYPE</code>.
* *
* @param id The <code>id</code> for the retrieved * @param id The <code>id</code> for the retrieved
* <code>DataObject</code>. * <code>DataObject</code>.
@ -87,35 +99,37 @@ public class SurveyResponse extends ContentItem {
} }
/* accessors *****************************************************/ /* accessors *****************************************************/
public void addAnswer(PersistentLabel label, PersistentWidget widget, String value) { private void setEntryDate() {
set(ENTRY_DATE, new Date());
SurveyAnswer answer = SurveyAnswer.create(label, widget, value);
add(ANSWERS, answer);
} }
public Date getEntryDate() { public Date getEntryDate() {
return (Date) get(ENTRY_DATE); return (Date) get(ENTRY_DATE);
} }
/* Class methods **********************************************************/ private void setSurvey(Survey survey) {
public static SurveyResponse create(Survey survey, User user) { // set(SURVEY, survey);
SurveyResponse response = new SurveyResponse();
response.setup(survey, user);
return response;
}
protected void setup(Survey survey, User user) {
set(ENTRY_DATE, new Date());
// XXX hack - see pdl file
set(USER + "ID", user.getID());
set(SURVEY + "ID", survey.getID()); set(SURVEY + "ID", survey.getID());
} }
public static SurveyResponse retrieve(DataObject obj) { public Survey getSurvey() {
return new SurveyResponse(obj); return (Survey) get(SURVEY);
} }
public void addAnswer(PersistentLabel label, PersistentWidget widget, String value) {
SurveyAnswer answer = SurveyAnswer.create(label, widget, value);
add(ANSWERS, answer);
}
public SurveyAnswerCollection getAnswers() {
return new SurveyAnswerCollection ((DataCollection) get(ANSWERS));
}
public boolean hasAnswers() {
return !this.getAnswers().isEmpty();
}
/* Class methods **********************************************************/
/* /*
public static SurveyResponseCollection retrieveBySurvey(Survey survey) { public static SurveyResponseCollection retrieveBySurvey(Survey survey) {
DataCollection responses = DataCollection responses =

View File

@ -0,0 +1,44 @@
package com.arsdigita.cms.contenttypes;
import com.arsdigita.domain.DomainCollection;
import com.arsdigita.kernel.User;
import com.arsdigita.persistence.DataCollection;
import java.util.Date;
/**
*
* @author Sören Bernstein
*/
public class SurveyResponseCollection extends DomainCollection {
/**
* Creates a new instance of SurveyResponseCollection
*/
public SurveyResponseCollection(DataCollection dataCollection) {
super(dataCollection);
m_dataCollection.addOrder(SurveyResponse.ENTRY_DATE);
}
public SurveyResponseCollection(DataCollection dataCollection, User user) {
this(dataCollection);
// m_dataCollection.addFilter(SurveyResponse.USER);
}
// Get the entry date
public Date getEntryDate() {
return (Date) m_dataCollection.get(SurveyResponse.ENTRY_DATE);
}
// Get the user
public User getUser() {
return (User) m_dataCollection.get(SurveyResponse.USER);
}
public SurveyResponse getSurveyResponse() {
return new SurveyResponse(m_dataCollection.getDataObject());
}
}

View File

@ -1,21 +1,3 @@
/*
* Copyright (C) 2002-2004 Red Hat Inc. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package com.arsdigita.cms.contenttypes.ui; package com.arsdigita.cms.contenttypes.ui;
import com.arsdigita.bebop.event.FormProcessListener; import com.arsdigita.bebop.event.FormProcessListener;
@ -46,6 +28,7 @@ import java.util.Iterator;
import com.arsdigita.formbuilder.util.FormBuilderUtil; import com.arsdigita.formbuilder.util.FormBuilderUtil;
import com.arsdigita.kernel.Kernel;
import com.arsdigita.kernel.User; import com.arsdigita.kernel.User;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -53,18 +36,18 @@ import java.math.BigDecimal;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
* The process lister that processes a survey response entered by a user. * A process listener to save the responses from a user to the database.
* *
* @author Sören Bernstein
*/ */
public class SurveyProcessListener public class SurveyProcessListener implements FormProcessListener {
implements FormProcessListener {
public static final String SURVEY_ID_NAME = "__ss_survey_id__"; public static final String SURVEY_ID = "SurveyID";
public static final String RESPONSE_ID = "__ss_response_id__"; // public static final String RESPONSE_ID = "__ss_response_id__";
public static final BigDecimal THE_PUBLIC_USER = new BigDecimal(-200); // public static final BigDecimal THE_PUBLIC_USER = new BigDecimal(-200);
private static final String KNOWLEDGE_TEST = "knowledge_test"; // private static final String KNOWLEDGE_TEST = "knowledge_test";
protected RequestLocal m_persistentForm = new RequestLocal(); // protected RequestLocal m_persistentForm = new RequestLocal();
private RequestLocal m_nameQuestionMap = new RequestLocal(); // private RequestLocal m_nameQuestionMap = new RequestLocal();
private RequestLocal m_response; private RequestLocal m_response;
private static org.apache.log4j.Logger s_log = private static org.apache.log4j.Logger s_log =
Logger.getLogger(SurveyProcessListener.class.getName()); Logger.getLogger(SurveyProcessListener.class.getName());
@ -79,81 +62,70 @@ public class SurveyProcessListener
public void process(FormSectionEvent event) { public void process(FormSectionEvent event) {
Survey survey = null;
SurveyResponse surveyResponse = null;
// Get the form data
FormData formData = event.getFormData(); FormData formData = event.getFormData();
PageState ps = event.getPageState();
BigDecimal surveyID = (BigDecimal) formData.get(SURVEY_ID_NAME); // Read the needed information to create a new response and create
BigDecimal responseID = (BigDecimal) formData.get(RESPONSE_ID); // a new instance of SurveyResponse to store this information
m_response.set(ps, responseID); BigDecimal surveyID = (BigDecimal) formData.get(formData.getParameter(SURVEY_ID));
Survey survey = (Survey) FormBuilderUtil.instantiateObjectOneArg(Survey.class.getName(), surveyID);
SurveyResponse response = null;
try { try {
response = (SurveyResponse) DomainObjectFactory.newInstance(new OID(SurveyResponse.class.getName(), responseID));
} catch (DataObjectNotFoundException ex) {
// s_log.warn("Can't create this object" + responseID);
}
//Let's not save the data twice in the case of a double-click // Try to get the corresponding Survey object
if (response.questionsAnswered()) { survey = new Survey(surveyID);
} catch (DataObjectNotFoundException ex) {
// Strange, there is no survey with this id. Someone is messing aroound
s_log.warn("Can't find survey object with ID " + surveyID + ". Someone is messing around.");
// Abort processing
return; return;
} }
m_persistentForm.set(ps, survey.getForm()); // Get the user
User user = (User) Kernel.getContext().getParty();
// Get the responding user // Create the new SurveyResponse object
User user = KernelHelper.getCurrentUser(ps.getRequest()); surveyResponse = survey.addResponse();
// Process the answers by iteration over the form widget parameters
Iterator parameterIterator = formData.getParameters().iterator();
while (parameterIterator.hasNext()) {
ParameterData parameterData = (ParameterData) parameterIterator.next();
addAnswer(surveyResponse, parameterData.getName(), parameterData.getValue());
// Use the generic user "The Public" if the user is not registered
if (user == null) {
try {
user = User.retrieve(THE_PUBLIC_USER);
} catch (DataObjectNotFoundException e) {
s_log.error("Public User does not exist.");
} }
} }
// Iterate over the widget parameters and insert the answers to the survey response private void addAnswer(SurveyResponse surveyResponse, Object name, Object value) {
Iterator parameterIter = formData.getParameters().iterator();
while (parameterIter.hasNext()) {
s_log.warn("Found some formData");
ParameterData parameterData = (ParameterData) parameterIter.next();
String parameterName = (String) parameterData.getName();
Object parameterValue = parameterData.getValue();
if (parameterValue instanceof java.lang.String[]) {
// This is a multi-answer question - iterate over the
// answer values and add them one by one
String[] valueArray = (String[]) parameterValue;
for (int i = 0; i < valueArray.length; ++i) {
// addAnswer(response, ps, valueArray[i], parameterName);
// Test if value is a string array
if(value instanceof String[]) {
// This is a multi-answer question, so iterate over the answers
for (int i = 0; i < ((String[]) value).length; i++) {
addAnswer(surveyResponse, name, ((String[]) value)[i]);
} }
} else { } else {
// Single answer question // Create new SurveyAnswer object
// surveyResponse.addAnswer(,, (String) value);
// addAnswer(response, ps, parameterValue, parameterName);
} }
} }
// Save the survey response to the database
response.save();
// saveScore(survey, response);
}
/* /*
private void saveScore(Survey survey, SurveyResponse response) { PageState ps = event.getPageState();
BigDecimal responseID = (BigDecimal) formData.get(RESPONSE_ID);
m_response.set(ps, responseID);
m_persistentForm.set(ps, survey.getForm());
String query;
if (survey.getQuizType().equals(KNOWLEDGE_TEST)) {
query = "com.arsdigita.simplesurvey.saveScore";
} else {
query = "com.arsdigita.simplesurvey.saveAssessmentScore";
}
DataOperation dao = SessionManager.getSession().retrieveDataOperation(query);
dao.setParameter("responseID", response.getID());
dao.execute();
}
*/ */
/* /*
private void addAnswer(SurveyResponse surveyResponse, private void addAnswer(SurveyResponse surveyResponse,
@ -233,5 +205,4 @@ public class SurveyProcessListener
return question; return question;
} }
*/ */
} }