-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnect.java
More file actions
235 lines (212 loc) · 9.92 KB
/
Connect.java
File metadata and controls
235 lines (212 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.assistments.connector.lti;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.binary.Base32;
import org.assistments.connector.domain.PartnerToAssistments;
import org.assistments.connector.domain.PartnerToAssistments.ColumnNames;
import org.assistments.connector.exception.ReferenceNotFoundException;
import org.assistments.connector.exception.TransferUserException;
import org.assistments.connector.service.AccountService;
import org.assistments.connector.service.AssignmentService;
import org.assistments.connector.service.StudentClassService;
import org.assistments.connector.service.impl.AccountServiceImpl;
import org.assistments.connector.service.impl.AssignmentServiceImpl;
import org.assistments.connector.service.impl.StudentClassServiceImpl;
import org.assistments.connector.utility.Constants;
import org.assistments.service.domain.ReferenceTokenPair;
import org.oscelot.lti.tp.Callback;
import org.oscelot.lti.tp.DataConnector;
import org.oscelot.lti.tp.ResourceLink;
import org.oscelot.lti.tp.ToolConsumer;
import org.oscelot.lti.tp.ToolProvider;
import org.oscelot.lti.tp.User;
import org.oscelot.lti.tp.dataconnector.None;
@WebServlet({ "/Connect", "/connect", "/connect/" })
public class Connect extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final String partnerRef = "Hien-Ref";
public Connect() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DataConnector dc = new None();
ToolConsumer toolConsumer = new ToolConsumer("testing.edu", dc, true);
toolConsumer.setName("Testing");
toolConsumer.setSecret("secret");
toolConsumer.save();
Callback doLaunch = new DoLaunch();
ToolProvider toolProvider = new ToolProvider(request, response, doLaunch, dc);
toolProvider.execute();
}
}
class DoLaunch implements Callback {
@Override
public boolean execute(ToolProvider toolProvider) {
String userType = "";
String onBehalf = "";
String userRef = "";
User ltiUser = toolProvider.getUser();
ResourceLink resourceLink = toolProvider.getResourceLink();
//Determine user role from LTI
if(ltiUser.isAdmin() || ltiUser.isStaff()) {
userType = Constants.TEACHER_ROLE;
// } else if ( ltiUser.isLearner() ) {
} else {
userType = Constants.STUDENT_ROLE;
}
String partnerExternalRef = toolProvider.getUser().getId();
org.assistments.service.domain.User user = Utility.getUserInfo(toolProvider);
AccountService as = new AccountServiceImpl(Utility.PARTNER_REFERENCE);
ReferenceTokenPair pair = null;
try {
pair = as.transferUser(user, Utility.EDMODO_SCHOOL_REF,
partnerExternalRef, "", "lti-user");
} catch (TransferUserException e1) {
throw new RuntimeException(e1);
}
userRef = pair.getExternalRef();
onBehalf = pair.getAccessToken();
String contextTitle = toolProvider.getRequest().getParameter("context_title");
String contextID = toolProvider.getRequest().getParameter("context_id");
// String url = toolProvider.getRequest().getRequestURL().toString();
toolProvider.getRequest().getRequestURI();
if(contextTitle == null || contextTitle.equals("")) {
if(contextID != null) {
contextTitle = contextID;
} else if(resourceLink.getTitle() != null && resourceLink.getTitle().equals("")) {
contextTitle = resourceLink.getTitle();
} else {
contextTitle = "Default Class";
}
}
contextID = resourceLink.getId();
HttpSession session = toolProvider.getRequest().getSession();
String appID = toolProvider.getRequest().getParameter("custom_appid");
if(appID == null || appID.equals("")) {
//tell teachers problem set id doesn't exist.
String msg = "Sorry.. There is a syntax error in custom parameters";
String instruction = "Teacher: Please make sure custom parameters that you typed in is correct. "
+ "It should be <i>appid=[problem set id]</i> <br><br>"
+ "Student: Tell your teacher this error. Your teacher should correct this error so you can start the assignment correctly.";
Utility.redirectToInstructionPage(toolProvider, session, msg, instruction);
return true;
}
appID = appID.trim();
//convert it into problem set number id
String problemSetID = Utility.decodeProblemSetString(appID);
if(problemSetID == null || problemSetID.equals("")) {
//tell teachers problem set id doesn't exist.
String msg = "Sorry.. The problem set doesn't exist";
String instruction = "Teacher: Please make sure the problem set id that you typed in is correct. It should start with PS.<br><br>"
+ "Student: Tell your teacher this error. Your teacher should correct this error so you can start the assignment correclty.";
Utility.redirectToInstructionPage(toolProvider, session, msg, instruction);
return true;
}
//If teacher, try to create class. Otherwise enroll in the class.
if(userType.equals(Constants.TEACHER_ROLE)) {
// String classRef = Utility.createClass(contextTitle, contextID, Utility.PARTNER_REFERENCE, onBehalf);
StudentClassService scs = new StudentClassServiceImpl(Utility.PARTNER_REFERENCE, onBehalf);
String classRef = scs.transferStudentClass(contextTitle, contextID, "", "lti-class");
//Create assignment if given an appID
if(problemSetID !=null && problemSetID.length()>0){
AssignmentService assignmentService = new AssignmentServiceImpl(Utility.PARTNER_REFERENCE, onBehalf);
partnerExternalRef = problemSetID + classRef;
String assignmentRef = null;
assignmentRef = assignmentService.transferClassAssignment(problemSetID, classRef, partnerExternalRef, "", "lti-assignment");
Base32 base32 = new Base32();
String reportRef = base32.encodeAsString(assignmentRef.getBytes());
toolProvider.setRedirectUrl(Constants.CONNECTOR_URL + "report/"+reportRef+"?ref=Hien");
}
} else if(userType.equals(Constants.STUDENT_ROLE)) {
//first check if the student class exists
String stuClassPartnerRef = contextID;
StudentClassService scs = new StudentClassServiceImpl(Utility.PARTNER_REFERENCE, onBehalf);
String classRef = null;
if(scs.isExternalStudentClassExists(stuClassPartnerRef)) {
//get student class external reference
PartnerToAssistments pta = null;
try {
pta = scs.find(ColumnNames.PARTNER_EXTERNAL_REFERENCE, stuClassPartnerRef).get(0);
} catch (org.assistments.connector.exception.ReferenceNotFoundException e) {
// this should never happen since it's sure that the class exists.
}
classRef = pta.getAssistmentsExternalRefernce();
} else {
//tell students the assignment hasn't been created yet.
String msg = "The assignment hasn't been created yet.";
String instruction = "Please tell your teacher that he/she "
+ "has to click the assignment after it is created. So you can start the assignment correctly.";
Utility.redirectToInstructionPage(toolProvider, session, msg, instruction);
return true;
}
String assignmentPartnerRef = problemSetID + classRef;
AssignmentService assignmentService = new AssignmentServiceImpl(Utility.PARTNER_REFERENCE, onBehalf);
String assignmentRef = null;
try {
assignmentRef = assignmentService.find(ColumnNames.PARTNER_EXTERNAL_REFERENCE, assignmentPartnerRef)
.get(0).getAssistmentsExternalRefernce();
} catch (ReferenceNotFoundException e) {
String msg = "The assignment hasn't been created yet.";
String instruction = "Please tell your teacher that he/she "
+ "has to click the assignment after it is created. So you can start the assignment correctly.";
Utility.redirectToInstructionPage(toolProvider, session, msg, instruction);
return true;
}
//put all parameter to servlet context
ServletContext sc = toolProvider.getRequest().getServletContext();
String ltiUserId = Utility.generateLTIUserId(userRef);
String resourceLinkId = Utility.generateResourceLinkId(userRef, assignmentRef);
sc.setAttribute(ltiUserId, ltiUser);
sc.setAttribute(resourceLinkId, resourceLink);
scs.enrollStudent(userRef, classRef);
//Goto assignment if given an appID
if(problemSetID !=null && problemSetID.length()>0){
// go to tutor
String onExit = Utility.generateStudentReportURL(userRef, assignmentRef, "lti");
try {
onExit = URLEncoder.encode(onExit, "UTF-8");
onExit = URLEncoder.encode(onExit, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
String fullName = user.getDisplayName();
String tutorURL = assignmentService.getAssignment(assignmentRef, onExit);
try {
tutorURL = URLEncoder.encode(Constants.ASSISSTments_URL, "UTF-8") + tutorURL;
fullName = URLEncoder.encode(fullName, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
String loginURL = Constants.LOGIN_URL;
String addressToGo = String.format("%1$s?partner=%2$s&access=%3$s&on_success=%4$s&on_failure=%5$s",
loginURL, Utility.PARTNER_REFERENCE, onBehalf, tutorURL, Utility.LOGIN_FAILURE);
try {
addressToGo = URLEncoder.encode(addressToGo, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
String tutorLink = Constants.CONNECTOR_HTTPS_URL + "tutor?tutor_link="
+ addressToGo + "&student_name="+fullName;
/*
try {
tutorLink = URLEncoder.encode(tutorLink, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}*/
toolProvider.setRedirectUrl(tutorLink);
}
}
return true;
}
}