Compare commits

..

No commits in common. "master" and "aa" have entirely different histories.
master ... aa

11 changed files with 16 additions and 240 deletions

View File

@ -33,11 +33,7 @@
<artifactId>basiclti-util</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.xml.parsers</groupId>
<artifactId>jaxp-api</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>

View File

@ -46,7 +46,7 @@ public class CourseController {
String url, @RequestParam(name = "name", required = false, defaultValue = "")
String name, Model model) {
courseRepository.save(new Course(null, name, url, key, secret, null));
courseRepository.save(new Course(null, name, url, key, secret));
return "start"; //view
}

View File

@ -1,90 +0,0 @@
package com.wmi.lti.controllers;
import com.wmi.lti.model.course.Course;
import com.wmi.lti.model.course.CourseRepository;
import com.wmi.lti.model.grade.Grade;
import com.wmi.lti.model.grade.GradeRepository;
import com.wmi.lti.model.user.User;
import com.wmi.lti.model.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
@RequestMapping("/grades")
@RestController
public class GradeController {
@Autowired
private GradeRepository gradeRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private CourseRepository courseRepository;
@PostMapping("")
public String addCourse(@RequestParam(required = false) Map<String, String> args, HttpServletRequest request) throws IOException {
String gradeXML = extractPostRequestBody(request);
double gradeValue = Double.valueOf(gradeXML.split("<textString>")[1].split("</textString>")[0]);
String cell = gradeXML.split("<sourcedId>")[1].split("</sourcedId>")[0];
User user = userRepository.findByUsername(cell.replaceFirst("_.*", ""));
Course course = courseRepository.findFirstByName(cell.replaceFirst(".*_", ""));
Optional<Grade> gradeCheck = gradeRepository.findGradeByCourseAndUser(course, user);
Grade grade = gradeCheck.orElseGet(Grade::new);
grade.setCourse(course);
grade.setGrade(gradeValue);
grade.setUser(user);
gradeRepository.save(grade);
return response();
}
static String extractPostRequestBody(HttpServletRequest request) throws IOException {
if ("POST".equalsIgnoreCase(request.getMethod())) {
Scanner s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
return "";
}
private String response(){
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<imsx_POXEnvelopeResponse xmlns=\"http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0\">\n" +
" <imsx_POXHeader>\n" +
" <imsx_POXResponseHeaderInfo>\n" +
" <imsx_version>V1.0</imsx_version>\n" +
" <imsx_messageIdentifier>4560</imsx_messageIdentifier>\n" +
" <imsx_statusInfo>\n" +
" <imsx_codeMajor>success</imsx_codeMajor>\n" +
" <imsx_severity>status</imsx_severity>\n" +
" <imsx_messageRefIdentifier>999999123</imsx_messageRefIdentifier>\n" +
" <imsx_operationRefIdentifier>replaceResult</imsx_operationRefIdentifier>\n" +
" </imsx_statusInfo>\n" +
" </imsx_POXResponseHeaderInfo>\n" +
" </imsx_POXHeader>\n" +
" <imsx_POXBody>\n" +
" <replaceResultResponse />\n" +
" </imsx_POXBody>\n" +
"</imsx_POXEnvelopeResponse>";
}
}

View File

@ -2,9 +2,6 @@ package com.wmi.lti.controllers;
import com.wmi.lti.model.course.Course;
import com.wmi.lti.model.course.CourseRepository;
import com.wmi.lti.model.grade.Grade;
import com.wmi.lti.model.grade.GradeDTO;
import com.wmi.lti.model.grade.GradeRepository;
import com.wmi.lti.model.user.User;
import com.wmi.lti.model.user.UserPrincipal;
import com.wmi.lti.model.user.UserRepository;
@ -33,47 +30,32 @@ public class StartController {
@Autowired
private UserRepository userRepository;
@Autowired
private GradeRepository gradeRepository;
@GetMapping("")
public String start(Model model){
List<Course> courses = courseRepository.findAll();
List<Course> coursesToReturn = new ArrayList<>();
User user = userRepository.findByUsername(((UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
for (Course course : courses) {
LtiSigner signer = new LtiOauthSigner();
Map<String, String> parameters = new HashMap<>();
User user = userRepository.findByUsername(((UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
parameters.put("lti_message_type", "basic-lti-launch-request");
parameters.put("lti_version", "LTI-1p0");
parameters.put("resource_link_id", user.getUsername() + "_" + course.getCourse_key());
parameters.put("user_id", user.getId().toString());
parameters.put("lis_person_name_given", user.getUsername());
parameters.put("tool_consumer_instance_name", "wmi.lti");
//parameters.put("target", "frame");
parameters.put("lis_outcome_service_url", "http://localhost:8080/grades");
parameters.put("lis_result_sourcedid", user.getUsername() + "_" + course.getCourse_key());
try {
Map<String, String> signedParameters = signer.signParameters(parameters, course.getCourse_key(), course.getSecret(), course.getUrl(), "POST");
/* String link = course.getUrl();
link += "?oauth_nonce=" + signedParameters.get("oauth_nonce") + "&oauth_signature=" + signedParameters.get("oauth_signature") + "&oauth_consumer_key=" + signedParameters.get("oauth_consumer_key") + "&oauth_signature_method=" + signedParameters.get("oauth_signature_method") + "&oauth_timestamp=" + signedParameters.get("oauth_timestamp") + "&oauth_version=" + signedParameters.get("oauth_version") + "&lti_message_type=" + signedParameters.get("lti_message_type") + "&lti_version=" + signedParameters.get("lti_version") + "&resource_link_id=" + signedParameters.get("resource_link_id") + "&user_id=" + signedParameters.get("user_id") + "&lis_person_name_given=" + signedParameters.get("lis_person_name_given") + "&tool_consumer_instance_name=" + signedParameters.get("tool_consumer_instance_name") + "&target=" + signedParameters.get("target");
*/
String link = course.getUrl() + "?" + "oauth_nonce=" + signedParameters.get("oauth_nonce") + "&oauth_signature=" + signedParameters.get("oauth_signature") + "&oauth_consumer_key=" + signedParameters.get("oauth_consumer_key") + "&oauth_signature_method=" + signedParameters.get("oauth_signature_method") + "&oauth_timestamp=" + signedParameters.get("oauth_timestamp") + "&oauth_version=" + signedParameters.get("oauth_version") + "&lti_message_type=" + signedParameters.get("lti_message_type") + "&lti_version=" + signedParameters.get("lti_version") + "&resource_link_id=" + signedParameters.get("resource_link_id") + "&user_id=" + signedParameters.get("user_id") + "&lis_person_name_given=" + signedParameters.get("lis_person_name_given") + "&tool_consumer_instance_name=" + signedParameters.get("tool_consumer_instance_name");
course.setSecret(null);
course.setSignedParameters(signedParameters);
course.setCourse_key(null);
course.setUrl(link);
coursesToReturn.add(course);
} catch (Exception e) {
e.printStackTrace();
}
}
List<Grade> grades = gradeRepository.findAllByUser(user);
List<GradeDTO> gradeDTOS = new ArrayList<>();
for (Grade grade : grades){
gradeDTOS.add(new GradeDTO(grade.getCourse().getName(), grade.getGrade()* 100 + "%"));
}
model.addAttribute("courses", coursesToReturn);
model.addAttribute("grades", gradeDTOS);
return "start"; //view
}
}

View File

@ -5,8 +5,10 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@AllArgsConstructor
@ -35,9 +37,4 @@ public class Course {
@Setter
private String secret;
@Getter
@Setter
@Transient
private Map<String, String> signedParameters;
}

View File

@ -4,6 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface CourseRepository extends JpaRepository<Course, Long> {
Course findFirstByName(String name);
}

View File

@ -1,41 +0,0 @@
package com.wmi.lti.model.grade;
import com.wmi.lti.model.course.Course;
import com.wmi.lti.model.user.User;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Grade {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter
@Setter
private Long id;
@Getter
@Setter
@ManyToOne
private Course course;
@Getter
@Setter
@ManyToOne
private User user;
@Getter
@Setter
private Double grade;
@Getter
@Setter
private String description;
}

View File

@ -1,14 +0,0 @@
package com.wmi.lti.model.grade;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@AllArgsConstructor
@Getter
@Setter
public class GradeDTO {
private String courseName;
private String score;
}

View File

@ -1,14 +0,0 @@
package com.wmi.lti.model.grade;
import com.wmi.lti.model.course.Course;
import com.wmi.lti.model.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
public interface GradeRepository extends JpaRepository<Grade, Long> {
Optional<Grade> findGradeByCourseAndUser(Course course, User user);
List<Grade> findAllByUser(User user);
}

View File

@ -1,5 +1,5 @@
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3307/lti
spring.datasource.url=jdbc:mysql://localhost:3306/lti
spring.datasource.username=lti
spring.datasource.password=YnBDC0hqqhKaxt94

View File

@ -9,55 +9,15 @@
<link rel="stylesheet" th:href="@{/css/main.css}"/>
</head>
<body>
<style>
table {
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<tr th:each="course: ${courses}">
<form target = "_blank" th:action="${course.url}" method="POST">
<th:block th:each="parameter : ${course.signedParameters}">
<input type="hidden" th:name="${parameter.key}" th:value="${parameter.value}" />
</th:block>
<input th:value="${course.name}" type="submit"/>
<tr th:each="course : ${courses}">
<form th:action="${course.url}" method="POST">
<input type="submit">Launch Tool</input>
</form>
</tr>
<br><br><br>
<form th:action="@{/courses/addCourse}" method="GET">
<div><input type="submit" value="Add new content"/></div>
</form>
<br><br><br>
<H3>Grades</H3>
<table>
<tr>
<th>Course</th>
<th>Grade</th>
</tr>
<th:block th:each="grade: ${grades}">
<tr>
<td th:text="${grade.courseName}">...</td>
<td th:text="${grade.score}">...</td>
</tr>
</th:block>
</table>
</body>
</html>