View Javadoc
1   package com.germinus.merlin.controller.assignments;
2   
3   import java.util.Date;
4   
5   import javax.portlet.RenderRequest;
6   import javax.portlet.RenderResponse;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  import org.springframework.beans.factory.InitializingBean;
11  import org.springframework.web.portlet.ModelAndView;
12  import org.springframework.web.portlet.mvc.AbstractController;
13  
14  import com.germinus.merlin.manager.AssignmentManager;
15  import com.germinus.merlin.manager.AssignmentResultManager;
16  import com.germinus.merlin.manager.CourseManager;
17  import com.germinus.merlin.model.assignment.Assignment;
18  import com.germinus.merlin.model.assignment.AssignmentInteractive;
19  import com.germinus.merlin.model.assignment.AssignmentResult;
20  import com.germinus.merlin.util.IMerlinUtil;
21  import com.germinus.liferay.util.IUserUtil;
22  
23  /**
24   * This controller process the render view to details page.
25   * Return a assignment object to fill the fields in details page. This controler also
26   * show the results of a assignment if exists.
27   * @author Rui Quintas
28   * @version 1.0
29   * @since 1.0
30   * @author Jesús Rodríguez Martínez Gérminus XXI
31   * @version 2.0
32   * @since 2.0
33   */
34  public class AssignmentDetailsController extends AbstractController implements InitializingBean {
35  
36  	private IUserUtil userUtil;
37  	private IMerlinUtil merlinUtil;
38  	private AssignmentManager assignmentManager;
39  	private AssignmentResultManager assignmentResultManager;
40  	private CourseManager courseManager;
41  	
42      private static final Log log = LogFactory
43  	.getLog(AssignmentDetailsController.class);
44      
45      
46      public void afterPropertiesSet() throws Exception {
47         
48      }
49      
50      /**
51  	 * Process the render request and return a ModelAndView object which the
52  	 * DispatcherPortlet will render. Show the assignments details and the assignmentResults referent to the
53  	 * user log in.
54  	 * @param request
55       * @param response
56       * @throws Exception
57  	 * @return ModelAndView
58  	 */
59  	public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {
60  		log.debug("View Details controller");
61  		
62  		String username = userUtil.getUserName(request, response);
63  		if (username==null){
64  			 return new ModelAndView("errors/notAuthorized");
65  		}
66  		
67  		//get the course with the groupId of the current community
68  		long courseId = userUtil.getComunityGroupId(request, response); 
69  		
70  		//get the user type: student or teacher
71  		String userType= merlinUtil.getUserType(request, response, courseId);
72  		
73  		//get the assignment id from parameter
74  	    Assignment assignment;
75  	    //log.debug("\n\n\n\nAssignmentId que buscamos: "+assignmentId+"\n\n\n\n\n");
76  	    assignment = assignmentManager.searchAssignment(request,response);
77  	
78  		int numResults = assignment.getResults().size();
79  		request.setAttribute("numResults", numResults);
80  		String assignmentType = request.getParameter("type");
81  	    if (userType=="teacher"){
82  	    	if (assignmentType.compareTo("Online Activity")==0){
83  				return new ModelAndView("assignments/detailsAssignment/detailsTeacherAssignmentOnline","assignment",assignment);
84  			}else
85  				if (assignmentType.compareTo("Upload a Single File")==0){
86  					return new ModelAndView("assignments/detailsAssignment/detailsTeacherAssignmentUploadSingleFile","assignment",assignment);
87  				}else
88  					if (assignmentType.compareTo("Offline Activity")==0){
89  						return new ModelAndView("assignments/detailsAssignment/detailsTeacherAssignmentOffline", "assignment",assignment);
90  					} else //assignment type error
91  						return new ModelAndView("errors/error", "", null);
92  		}
93  		else {
94  			if (userType=="student"){
95  				AssignmentResult assignmentResult=assignmentResultManager.searchAssignmentResult(request,response);
96  				//get the type of assignment and save it in the request
97  				request.setAttribute("result", assignmentResult);
98  				String teacherName= new String("");
99  				if (assignmentResult!=null && assignmentResult.getTeachers()!=null) { 
100 					teacherName = userUtil.getUserName(assignmentResult.getTeachers().getId().getUserid());
101 				}
102 				request.setAttribute("teacherName",teacherName);
103 				if (assignmentType.compareTo("Online Activity")==0){
104 					if (makeSubmissionsPermited((AssignmentInteractive) assignment, assignmentResult)){
105 						return new ModelAndView("assignments/detailsAssignment/detailsStudentAssignmentOnline","assignment",assignment);
106 					}
107 					else {
108 						return new ModelAndView("assignments/detailsAssignment/detailsStudentAssignmentOnlineSubmited","assignment",assignment);
109 					}
110 				}else
111 					if (assignmentType.compareTo("Upload a Single File")==0){
112 						if (makeSubmissionsPermited((AssignmentInteractive) assignment, assignmentResult)){
113 							return new ModelAndView("assignments/detailsAssignment/detailsStudentAssignmentUploadSingleFile","assignment",assignment);
114 						}
115 						else {
116 							return new ModelAndView("assignments/detailsAssignment/detailsStudentAssignmentUploadSingleFileSubmited","assignment",assignment);
117 						}
118 					}else
119 						if (assignmentType.compareTo("Offline Activity")==0){
120 							return new ModelAndView("assignments/detailsAssignment/detailsStudentAssignmentOffline", "assignment",assignment);
121 						} else //assignment type error
122 							return new ModelAndView("errors/error", "", null);
123 			}
124 			else //guest user, user without teacher or student role-> this never will happen
125 					return new ModelAndView("assignments/errors/notAuthorized", "",null);
126 		}
127 	}
128 
129 	private boolean makeSubmissionsPermited(AssignmentInteractive assignment, AssignmentResult assignmentResult) {
130 		Date actualDate = new Date();
131 		if (assignment.getBegintime().before(actualDate)){
132 			if (assignment.getEndtime().after(actualDate) || !assignment.getPreventLateSubmissions()) {
133 				if (assignmentResult==null || (assignment.getResubmit()!=null && assignment.getResubmit())) {
134 					return true;
135 				}
136 			}
137 		}
138 		return false;
139 	}
140 
141 	/**
142 	 * 
143 	 * @return assignmentManager
144 	 */
145 	public AssignmentManager getAssignmentManager() {
146 		return assignmentManager;
147 	}
148 
149 	/**
150 	 * @param assignmentManager the assignmentManager to set
151 	 */
152 	public void setAssignmentManager(AssignmentManager assignmentsManagement) {
153 		this.assignmentManager = assignmentsManagement;
154 	}
155 
156 	/**
157 	 * @return the courseManager
158 	 */
159 	public CourseManager getCourseManager() {
160 		return courseManager;
161 	}
162 
163 	/**
164 	 * @param courseManager the courseManager to set
165 	 */
166 	public void setCourseManager(CourseManager courseManager) {
167 		this.courseManager = courseManager;
168 	}
169     
170 	/**
171 	 * @return the liferayUtil
172 	 */
173 	public IUserUtil getUserUtil() {
174 		return userUtil;
175 	}
176 
177 	/**
178 	 * @param liferayUtil the liferayUtil to set
179 	 */
180 	public void setUserUtil(IUserUtil userUtil) {
181 		this.userUtil = userUtil;
182 	}
183 
184 	/**
185 	 * @return the merlinUtil
186 	 */
187 	public IMerlinUtil getMerlinUtil() {
188 		return merlinUtil;
189 	}
190 
191 	/**
192 	 * @param merlinUtil the merlinUtil to set
193 	 */
194 	public void setMerlinUtil(IMerlinUtil merlinUtil) {
195 		this.merlinUtil = merlinUtil;
196 	}
197 
198 	public AssignmentResultManager getAssignmentResultManager() {
199 		return assignmentResultManager;
200 	}
201 
202 	public void setAssignmentResultManager(
203 			AssignmentResultManager assignmentResultManager) {
204 		this.assignmentResultManager = assignmentResultManager;
205 	}
206 }