Skip to content

Commit

Permalink
LSNBLDR-190; sync total entries when copying question item
Browse files Browse the repository at this point in the history
git-svn-id: https://source.sakaiproject.org/svn/lessonbuilder/trunk@118632 66ffb92e-73f9-0310-93c1-f5514f145a0a
  • Loading branch information
clhedrick committed Jan 23, 2013
1 parent 3354efd commit ac15185
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,6 @@ public class PageData {

public void incrementQRCount(long questionId, long responseId);

public void syncQRTotals(SimplePageItem item);

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -1018,7 +1019,7 @@ public SimplePageItem copyItem(SimplePageItem old) {
// phase 2 of copy after save, we need item number here
public SimplePageItem copyItem2(SimplePageItem old, SimplePageItem item) {

// currently nothing to do
syncQRTotals(item);

return item;
}
Expand Down Expand Up @@ -1074,5 +1075,30 @@ public void incrementQRCount(long questionId, long responseId) {
SqlService.dbWrite("update lesson_builder_qr_totals set count = count + 1 where questionId = ? and responseId = ?", fields);
}

public void syncQRTotals(SimplePageItem item) {
if (item.getType() != SimplePageItem.QUESTION || ! item.getAttribute("questionType").equals("multipleChoice"))
return;

}
Map<Long, SimplePageQuestionResponseTotals> oldTotals = new HashMap<Long, SimplePageQuestionResponseTotals>();
List<SimplePageQuestionResponseTotals> oldQrTotals = findQRTotals(item.getId());
for (SimplePageQuestionResponseTotals total: oldQrTotals)
oldTotals.put(total.getResponseId(), total);

for (SimplePageQuestionAnswer answer: findAnswerChoices(item)) {
Long id = answer.getId();
if (oldTotals.get(id) != null)
oldTotals.remove(id); // in both old and new, done with it
else {
// in new but not old, add it
SimplePageQuestionResponseTotals total = makeQRTotals(item.getId(), id);
quickSaveItem(total);
}
}

// entries that were in old list but not new one, remove them
for (Long rid: oldTotals.keySet()) {
deleteItem(oldTotals.get(rid));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -771,11 +771,14 @@ private boolean makePage(Element element, String oldServer, String siteId, Strin
simplePageToolDao.quickSaveItem(item);
itemMap.put(itemId, item.getId());

// this needs item id, so it has to be done here
// these needs item id, so it has to be done here
// save item ID to object id. This will allow references to be fixed up.
// object id identifies the Sakai object in the old site. The fixup will
// find the object in the new site and fix up the item. Hence we need
// a mapping of item ID to object id.

simplePageToolDao.syncQRTotals(item);

if (type == SimplePageItem.ASSIGNMENT || type == SimplePageItem.ASSESSMENT || type == SimplePageItem.FORUM) {
String objectid = itemElement.getAttribute("objectid");
if (objectid != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5390,6 +5390,10 @@ public void setAddAnswerData(String data) {

/** Used for both adding and updating questions on a page. */
public String updateQuestion() {
if (!itemOk(itemId)) {
setErrMessage(messageLocator.getMessage("simplepage.permissions-general"));
return "permission-failed";
}
if(!canEditPage()) {
setErrMessage(messageLocator.getMessage("simplepage.permissions-general"));
return "failure";
Expand Down Expand Up @@ -5419,12 +5423,6 @@ public String updateQuestion() {
if(questionType.equals("shortanswer")) {
item.setAttribute("questionAnswer", questionAnswer);
}else if(questionType.equals("multipleChoice")) {
// set showing old total entries. set of answerids for which we have total entries
Map<Long, SimplePageQuestionResponseTotals> oldTotals = new HashMap<Long, SimplePageQuestionResponseTotals>();
List<SimplePageQuestionResponseTotals> oldQrTotals = simplePageToolDao.findQRTotals(item.getId());
for (SimplePageQuestionResponseTotals total: oldQrTotals)
oldTotals.put(total.getResponseId(), total);

Long max = simplePageToolDao.maxQuestionAnswer(item);
simplePageToolDao.clearQuestionAnswers(item);

Expand All @@ -5445,22 +5443,12 @@ public String updateQuestion() {
String text = fields[2];

Long id = simplePageToolDao.addQuestionAnswer(item, answerId, text, correct);

if (oldTotals.get(id) != null)
oldTotals.remove(id); // in both old and new, done with it
else {
// in new but not old, add it
SimplePageQuestionResponseTotals total = simplePageToolDao.makeQRTotals(item.getId(), id);
simplePageToolDao.quickSaveItem(total);
}

}

item.setAttribute("questionShowPoll", String.valueOf(questionShowPoll));

// entries that were in old list but not new one, remove them
for (Long rid: oldTotals.keySet()) {
simplePageToolDao.deleteItem(oldTotals.get(rid));
}
simplePageToolDao.syncQRTotals(item);

}

Expand Down

0 comments on commit ac15185

Please sign in to comment.