Index: lams_gradebook/.classpath =================================================================== diff -u -r72f92afa07d5c5d0e6051081a1d0e1890ce934d5 -r0aad94a28574176ae783da37b50e4108c0882e90 --- lams_gradebook/.classpath (.../.classpath) (revision 72f92afa07d5c5d0e6051081a1d0e1890ce934d5) +++ lams_gradebook/.classpath (.../.classpath) (revision 0aad94a28574176ae783da37b50e4108c0882e90) @@ -16,6 +16,6 @@ - + Index: lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/gradebookApplicationContext.xml =================================================================== diff -u -rce9f4e0c6d1e5814bf0dcb0fa16ae888a8c854a3 -r0aad94a28574176ae783da37b50e4108c0882e90 --- lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/gradebookApplicationContext.xml (.../gradebookApplicationContext.xml) (revision ce9f4e0c6d1e5814bf0dcb0fa16ae888a8c854a3) +++ lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/gradebookApplicationContext.xml (.../gradebookApplicationContext.xml) (revision 0aad94a28574176ae783da37b50e4108c0882e90) @@ -37,6 +37,7 @@ PROPAGATION_REQUIRED + PROPAGATION_REQUIRED PROPAGATION_REQUIRED PROPAGATION_REQUIRED,readOnly PROPAGATION_REQUIRED,readOnly Index: lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/service/GradebookService.java =================================================================== diff -u -r15e70deb1c79e4c3098eb11b84287bb31c8f64c9 -r0aad94a28574176ae783da37b50e4108c0882e90 --- lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/service/GradebookService.java (.../GradebookService.java) (revision 15e70deb1c79e4c3098eb11b84287bb31c8f64c9) +++ lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/service/GradebookService.java (.../GradebookService.java) (revision 0aad94a28574176ae783da37b50e4108c0882e90) @@ -469,9 +469,39 @@ } } + @Override + public void recalculateTotalMarksForLesson(Long lessonId) throws Exception { + Lesson lesson = lessonDAO.getLesson(lessonId); + if (lesson == null) { + return; + } + + Map userToGradebookUserLessonMap = getUserToGradebookUserLessonMap(lesson, null); + + //update for all users in activity + Set users = lesson.getAllLearners(); + for (User user : users) { + Integer userId = user.getUserId(); + GradebookUserLesson gradebookUserLesson = userToGradebookUserLessonMap.get(userId); + + Double totalMark = gradebookDAO.getGradebookUserActivityMarkSum(lessonId, userId); + if (totalMark != null) { + + if (totalMark > 0 && gradebookUserLesson == null) { + throw new Exception("An error detected: user (userId:" + userId + ") has total mark that equals to " + + totalMark + " but no assocciated gradebookUserLesson exist "); + } + + if (gradebookUserLesson != null) { + gradebookUserLesson.setMark(totalMark); + gradebookDAO.insertOrUpdate(gradebookUserLesson); + } + } + } + } @Override - public void updateUserMarksForActivity(Activity activity) { + public void recalculateGradebookMarksForActivity(Activity activity) { Long activityId = activity.getActivityId(); Lesson lesson = lessonDAO.getLessonForActivity(activityId); Index: lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/service/IGradebookService.java =================================================================== diff -u -r15e70deb1c79e4c3098eb11b84287bb31c8f64c9 -r0aad94a28574176ae783da37b50e4108c0882e90 --- lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/service/IGradebookService.java (.../IGradebookService.java) (revision 15e70deb1c79e4c3098eb11b84287bb31c8f64c9) +++ lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/service/IGradebookService.java (.../IGradebookService.java) (revision 0aad94a28574176ae783da37b50e4108c0882e90) @@ -124,7 +124,17 @@ * * @param activity */ - void updateUserMarksForActivity(Activity activity); + void recalculateGradebookMarksForActivity(Activity activity); + + /** + * Recalculates total marks for all users in a lesson. Then stores that mark in a gradebookUserLesson. Doesn't + * affect anyhow gradebookUserActivity objects. If total mark is positive but there is no gradebookUserLesson + * available - throws exception. + * + * @param lessonId + * @throws Exception + */ + void recalculateTotalMarksForLesson(Long lessonId) throws Exception; /** * Updates a user's lesson mark, this will make it desynchronised with the aggregated marks from the activities Index: lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/web/servlet/RecalculateTotalMarksForLessonServlet.java =================================================================== diff -u --- lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/web/servlet/RecalculateTotalMarksForLessonServlet.java (revision 0) +++ lams_gradebook/src/java/org/lamsfoundation/lams/gradebook/web/servlet/RecalculateTotalMarksForLessonServlet.java (revision 0aad94a28574176ae783da37b50e4108c0882e90) @@ -0,0 +1,102 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +package org.lamsfoundation.lams.gradebook.web.servlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; +import org.lamsfoundation.lams.gradebook.service.IGradebookService; +import org.lamsfoundation.lams.lesson.Lesson; +import org.lamsfoundation.lams.lesson.service.ILessonService; +import org.lamsfoundation.lams.util.WebUtil; +import org.lamsfoundation.lams.util.audit.IAuditService; +import org.lamsfoundation.lams.web.util.AttributeNames; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; + +/** + * Recalculates total marks for all users in a lesson. Then stores that mark in a gradebookUserLesson. Doesn't + * affect anyhow gradebookUserActivity objects. Servlet accepts only one parameter - "lessonID". + * + * @author Andrey Balan + */ +@SuppressWarnings("serial") +public class RecalculateTotalMarksForLessonServlet extends HttpServlet { + + private static Logger log = Logger.getLogger(RecalculateTotalMarksForLessonServlet.class); + + private static IAuditService auditService; + private static ILessonService lessonService; + private static IGradebookService gradebookService; + + @Override + public void init() throws ServletException { + WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); + auditService = (IAuditService) ctx.getBean("auditService"); + lessonService = (ILessonService) ctx.getBean("lessonService"); + gradebookService = (IGradebookService) ctx.getBean("gradebookService"); + } + + @Override + @SuppressWarnings("unchecked") + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + PrintWriter out = response.getWriter(); + + Long lessonId = WebUtil.readLongParam(request, AttributeNames.PARAM_LESSON_ID); + Lesson lesson = lessonService.getLesson(lessonId); + if (lesson == null) { + log.error("RecalculateTotalMarksForLessonServlet: lesson not found " + lessonId); + out.println("ERROR: lesson not found " + lessonId); + out.close(); + return; + } + + try { + gradebookService.recalculateTotalMarksForLesson(lessonId); + } catch (Throwable e) { + String errorMsg = "Error occured " + e.getMessage() + e.getCause(); + out.println(errorMsg); + out.close(); + return; + } + + String msg = "Total marks have been successfully recalculated for lessonId " + lessonId; + auditService.log("RecalculateTotalMarksForLessonServlet", msg); + out.println(msg); + out.close(); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doGet(request, response); + } +} \ No newline at end of file Index: lams_gradebook/web/WEB-INF/web.xml =================================================================== diff -u -rf1c3611a9167e5d9daee6dd37d9c7d743c96f7c0 -r0aad94a28574176ae783da37b50e4108c0882e90 --- lams_gradebook/web/WEB-INF/web.xml (.../web.xml) (revision f1c3611a9167e5d9daee6dd37d9c7d743c96f7c0) +++ lams_gradebook/web/WEB-INF/web.xml (.../web.xml) (revision 0aad94a28574176ae783da37b50e4108c0882e90) @@ -130,6 +130,11 @@ 1 + + + recalculateTotalMarksForLesson + org.lamsfoundation.lams.gradebook.web.servlet.RecalculateTotalMarksForLessonServlet + action @@ -140,6 +145,11 @@ Connector /ckeditor/filemanager/browser/default/connectors/jsp/connector + + + recalculateTotalMarksForLesson + /recalculateTotalMarksForLesson + 500