Skip to content

Commit

Permalink
Merge branch 'main' into setup-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesbrandenburger committed Feb 18, 2024
2 parents 3335305 + 663c5ea commit d4a7ee2
Show file tree
Hide file tree
Showing 20 changed files with 496 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package de.htwg_konstanz.mobilelearning.helper;
package de.htwg_konstanz.mobilelearning.helper.moodle;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
class MoodleCourse {
public Integer id;
public class MoodleCourse {
public String id;
public String fullname;
public String shortname;
public String summary;
Expand All @@ -16,7 +16,7 @@ class MoodleCourse {
public Integer enddate;

public MoodleCourse(
@JsonProperty("id") Integer id,
@JsonProperty("id") String id,
@JsonProperty("fullname") String fullname,
@JsonProperty("shortname") String shortname,
@JsonProperty("summary") String summary,
Expand All @@ -37,6 +37,10 @@ public MoodleCourse(
this.enddate = enddate;
}

public String getId() {
return id;
}

@Override
public String toString() {
return "MoodleCourse{" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.htwg_konstanz.mobilelearning.helper;
package de.htwg_konstanz.mobilelearning.helper.moodle;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
Expand All @@ -17,28 +17,32 @@ public class MoodleInterface {
private Integer userId;

public MoodleInterface(String username,
String password) {
String password) {
this.username = username;
this.password = password;
}

public Boolean login() {

ObjectMapper mapper = new ObjectMapper();

try {

// get the token from
// https://moodle.htwg-konstanz.de/moodle/login/token.php?username=USERNAME&password=PASSWORD&service=SERVICESHORTNAME
CloseableHttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet("https://moodle.htwg-konstanz.de/moodle/login/token.php?username=" + this.username + "&password=" + this.password + "&service=moodle_mobile_app");
MoodleTokenResponse tokenResponse = mapper.readValue(client.execute(request).getEntity().getContent(), MoodleTokenResponse.class);
HttpGet request = new HttpGet("https://moodle.htwg-konstanz.de/moodle/login/token.php?username="
+ this.username + "&password=" + this.password + "&service=moodle_mobile_app");
MoodleTokenResponse tokenResponse = mapper.readValue(client.execute(request).getEntity().getContent(),
MoodleTokenResponse.class);
this.token = tokenResponse.token;
// System.out.println("Successfully logged in as " + this.username + " with token " + this.token.substring(0, 5) + "...");
// System.out.println("Successfully logged in as " + this.username + " with
// token " + this.token.substring(0, 5) + "...");

// get user id
String wsFunction = "core_webservice_get_site_info";
request = new HttpGet("https://moodle.htwg-konstanz.de/moodle/webservice/rest/server.php?wstoken=" + this.token + "&wsfunction=" + wsFunction + "&moodlewsrestformat=json");
request = new HttpGet("https://moodle.htwg-konstanz.de/moodle/webservice/rest/server.php?wstoken="
+ this.token + "&wsfunction=" + wsFunction + "&moodlewsrestformat=json");
String response = EntityUtils.toString(client.execute(request).getEntity());
MoodleUserIdResponse userIdResponse = mapper.readValue(response, MoodleUserIdResponse.class);
Integer userId = userIdResponse.userid;
Expand All @@ -47,13 +51,14 @@ public Boolean login() {

// get all courses
wsFunction = "core_enrol_get_users_courses";
request = new HttpGet("https://moodle.htwg-konstanz.de/moodle/webservice/rest/server.php?wstoken=" + this.token + "&wsfunction=" + wsFunction + "&userid=" + userId + "&moodlewsrestformat=json");
request = new HttpGet("https://moodle.htwg-konstanz.de/moodle/webservice/rest/server.php?wstoken="
+ this.token + "&wsfunction=" + wsFunction + "&userid=" + userId + "&moodlewsrestformat=json");
response = EntityUtils.toString(client.execute(request).getEntity());
MoodleCourse[] courses = mapper.readValue(response, MoodleCourse[].class);
this.courses = List.of(courses);
// System.out.println("Got courses: ");
// System.out.println(this.courses);

} catch (Exception e) {
System.out.println("Error while logging into moodle: " + e.getMessage());
return false;
Expand All @@ -70,6 +75,13 @@ public List<MoodleCourse> getCourses() {
return this.courses;
}

// if token is not set login first
if (this.token == null) {
if (!this.login()) {
return List.of();
}
}

ObjectMapper mapper = new ObjectMapper();

try {
Expand All @@ -88,7 +100,7 @@ public List<MoodleCourse> getCourses() {

} catch (Exception e) {
System.out.println("Error while getting courses from moodle: " + e.getMessage());
return null;
return List.of();
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.htwg_konstanz.mobilelearning.helper;
package de.htwg_konstanz.mobilelearning.helper.moodle;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.htwg_konstanz.mobilelearning.helper;
package de.htwg_konstanz.mobilelearning.helper.moodle;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class Course implements Serializable {
public String name;
public String description;
public List<ObjectId> owners;
public List<ObjectId> students;
public String key;
public String moodleCourseId;

// feedback
public List<FeedbackForm> feedbackForms;
Expand All @@ -39,11 +41,13 @@ public Course(String name, String description) {
this.name = name;
this.description = description;
this.owners = new ArrayList<ObjectId>();
this.students = new ArrayList<ObjectId>();
this.feedbackForms = new ArrayList<FeedbackForm>();
this.feedbackQuestions = new ArrayList<FeedbackQuestion>();
this.quizForms = new ArrayList<QuizForm>();
this.quizQuestions = new ArrayList<QuizQuestion>();
this.key = "";
this.moodleCourseId = "";
}

// id
Expand Down Expand Up @@ -89,6 +93,41 @@ public boolean isOwner(User user) {
return this.owners.contains(user.getId());
}

// students
public List<ObjectId> getStudents() {
return this.students;
}

public void addStudent(ObjectId student) {
if (!this.students.contains(student)) {
this.students.add(student);
}
}

public void removeStudent(ObjectId student) {
try {
this.students.remove(student);
} catch (Exception e) {
System.out.println("not in list");
}
}

public void setStudents(List<ObjectId> students) {
this.students = students;
}

public boolean isStudent(String userId) {
return this.students.contains(new ObjectId(userId));
}

public boolean isStudent(ObjectId userId) {
return this.students.contains(userId);
}

public boolean isStudent(User user) {
return this.students.contains(user.getId());
}

// description
public String getDescription() {
return this.description;
Expand Down Expand Up @@ -291,6 +330,14 @@ public String getKey() {
return this.key;
}

public void setMoodleCourseId(String moodleCourseId) {
this.moodleCourseId = moodleCourseId;
}

public String getMoodleCourseId() {
return this.moodleCourseId;
}

public static Course fromApiCourse(ApiCourse apiCourse) throws IllegalArgumentException {

// validate input
Expand All @@ -310,6 +357,7 @@ public static Course fromApiCourse(ApiCourse apiCourse) throws IllegalArgumentEx
// create course
Course course = new Course(apiCourse.getName(), apiCourse.getDescription());
course.setKey(apiCourse.getKey());
course.setMoodleCourseId(apiCourse.getMoodleCourseId());

// create the feedback forms
for (ApiFeedbackForm apiFeedbackForm : apiCourse.getFeedbackForms()) {
Expand Down Expand Up @@ -344,6 +392,7 @@ public void updateFromApiCourse(ApiCourse apiCourse) {
// update course
this.setName(apiCourse.getName());
this.setDescription(apiCourse.getDescription());
this.setMoodleCourseId(apiCourse.getMoodleCourseId());

// update feedback forms
for (ApiFeedbackForm apiFeedbackForm : apiCourse.getFeedbackForms()) {
Expand All @@ -368,4 +417,6 @@ public void updateFromApiCourse(ApiCourse apiCourse) {
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class User {
public String username;
public String password;
public List<String> roles;
public List<ObjectId> courses;

public User() {
}
Expand All @@ -23,6 +24,7 @@ public User(String email, String name, String username, String password) {
this.username = username;
this.password = password;
this.roles = new ArrayList<String>();
this.courses = new ArrayList<ObjectId>();

// check if email, name and username have ": " in it and if so, only take the part after it
if (this.email.contains(": ")) {
Expand Down Expand Up @@ -129,4 +131,40 @@ public void setId(ObjectId id) {
this.id = id;
}

// courses
public List<ObjectId> getCourses() {
return this.courses;
}

public void setCourses(List<ObjectId> courses) {
this.courses = courses;
}

public void addCourse(ObjectId course) {
if (!this.courses.contains(course)) {
this.courses.add(course);
}
}

public void removeCourse(ObjectId course) {
try {
this.courses.remove(course);
} catch (Exception e) {
System.out.println("not in list");
}
}

public boolean hasCourse(ObjectId course) {
return this.courses.contains(course);
}

public boolean hasCourse(String courseId) {
return this.courses.contains(new ObjectId(courseId));
}

public void clearCourses() {
this.courses.clear();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@



import java.util.List;

import org.bson.types.ObjectId;

import de.htwg_konstanz.mobilelearning.models.Course;
import de.htwg_konstanz.mobilelearning.models.Form;
import de.htwg_konstanz.mobilelearning.models.auth.User;
import de.htwg_konstanz.mobilelearning.models.feedback.FeedbackForm;
import de.htwg_konstanz.mobilelearning.models.quiz.QuizForm;
import io.quarkus.mongodb.panache.PanacheMongoRepository;
Expand All @@ -26,6 +29,10 @@ public Course findByQuizFormConnectCode(Integer connectCode) {
return find("quizForms.connectCode", connectCode).firstResult();
}

public Course findByMoodleCourseId(String moodleCourseId) {
return find("moodleCourseId", moodleCourseId).firstResult();
}

public Course findByFormConnectCode(Integer connectCode) {
// TODO: make this more efficient
Course course = findByFeedbackFormConnectCode(connectCode);
Expand Down Expand Up @@ -73,4 +80,12 @@ public Course findByKey(String key) {
return find("key", key).firstResult();
}

public List<Course> listAllForStudent(User user) {
return find("students", user.getId()).list();
}

public List<Course> listAllForOwner(User user) {
return find("owners", user.getId()).list();
}

}
Loading

0 comments on commit d4a7ee2

Please sign in to comment.