def find_history(self, username, page=0, page_size=20): db = get_db() query = db.run( f'MATCH (a:Author)-[:TEACHES]->(c:Course)<-[e:ENROLLED]-(u:User),\ (c:Course)-[:CONDUCTED_IN]->(l:Language),\ (c:Course)-[:PROVIDED_BY]->(p:Provider)\ WHERE (u.username = "******")\ RETURN c.course_id, c.description, c.title, c.photo_link, c.direct_link,\ collect(DISTINCT a.author) as authors , l.language, p.provider, \ e.enrollment_date, e.completion_date, e.status SKIP {page} LIMIT {page_size}' ) result_array = [] for item in query: result_array += [{ "course_id": item[0], "description": item[1], "title": item[2], "photo_link": item[3], "direct_link": item[4], "authors": item[5], "language": item[6], "provider": item[7], "enrollment_date": item[8], "completion_date": item[9], "status": item[10] }] close_db() return result_array
def requires_prerequisite(self, username, course_id): """Returns boolean indicating if the user needs a prerequisite for this course""" db = get_db() prerequisites = '''MATCH (c:Course)-[r:REQUIRES]->(c2:Course) WHERE c.course_id = $course_id RETURN c''' prerequisites_results = db.run(prerequisites, course_id=course_id).single() # If the course has no prereq allow the user to sign up if prerequisites_results == None: return False # If it has prereq ensures they've taken them. query = '''MATCH (c:Course)-[r:REQUIRES]->(c2:Course)<-[r2:ENROLLED]-(u:User) WHERE u.username = $username AND r2.status = 'COMPLETE D' AND c.course_id = $course_id RETURN c ''' result = db.run(query, course_id=course_id, username=username).single() return True if result == None else False
def find_all(self, page=0, page_size=20): db = get_db() courses = db.run( f"MATCH (a:Author)-[:TEACHES]->(c:Course)-[:RELATED_TO]->(k:Knowledge),\ (c:Course)-[:CONDUCTED_IN]->(l:Language), (c:Course)-[:PROVIDED_BY]->(p:Provider)\ RETURN c.course_id, c.description, c.title, c.photo_link, c.direct_link,\ collect(DISTINCT a.author) as authors, collect(DISTINCT k.knowledge) as tags,\ l.language, p.provider SKIP {page} LIMIT {page_size}").records() result_array = [] for item in courses: result_array += [{ "course_id": item[0], "description": item[1], "title": item[2], "photo_link": item[3], "direct_link": item[4], "authors": item[5], "tags": item[6], "language": item[7], "provider": item[8] }] close_db() return result_array
def find_by_id(self, id): db = get_db() # Load and build course using a given course_id course = db.run( '''MATCH(c: Course {course_id: $course_id}), (a: Author)-[:TEACHES] -> (c), (c)-[:CONDUCTED_IN] -> (l: Language), (c)-[:PROVIDED_BY] -> (p: Provider) OPTIONAL MATCH(c)-[:RELATED_TO] -> (k: Knowledge) RETURN c.course_id, c.description, c.title, c.photo_link, c.direct_link, collect(DISTINCT a.author) as authors, collect(DISTINCT k.knowledge) as tags, l.language, p.provider''', course_id=id).single() if course != None: formatted_course = { "course_id": course[0], "description": course[1], "title": course[2], "photo_link": course[3], "direct_link": course[4], "authors": ', '.join(course[5]), "tags": ', '.join(course[6]) if course[6] != None else [], "language": course[7], "provider": course[8] } return formatted_course else: None
def find(self, query='', page=0, page_size=20): db = get_db() courses = db.run( f'MATCH (c:Course), (a:Author) WHERE (c.title =~".*(?i){query}.*" OR a.author =~".*(?i){query}.*")\ WITH c, a MATCH (a)-[:TEACHES]->(c), \ (c)-[:CONDUCTED_IN]->(l:Language), (c)-[:PROVIDED_BY]->(p:Provider) \ OPTIONAL MATCH (c)-[:RELATED_TO]->(k:Knowledge) \ RETURN c.course_id, c.description, c.title, c.photo_link, c.direct_link, \ collect(DISTINCT a.author) as authors, collect(DISTINCT k.knowledge) as tags, \ l.language, p.provider SKIP {page} LIMIT {page_size}').records() result_array = [] for item in courses: result_array += [{ "course_id": item[0], "description": item[1], "title": item[2], "photo_link": item[3], "direct_link": item[4], "authors": ', '.join(item[5]), "tags": ', '.join(item[6]), "language": item[7], "provider": item[8] }] close_db() return result_array
def get_enrolled_users(self, course_id): db = get_db() results = db.run(''' MATCH (u:User)-[r:ENROLLED]->(c:Course) WHERE c.course_id = $course_id RETURN u.name, u.username, r.enrollment_date, r.completion_date, r.status, u.university ''', course_id=course_id).records() users = [] for user in results: users.append({ 'name': user[0], 'username': user[1], 'enrollment_date': str(user[2]), 'completion_date': str(user[3]) if user[3] != None else None, 'status': user[4], 'university': user[5] }) close_db() return users
def find_by_username(self, username): db = get_db() user = format_cypher_list( db.run('''MATCH (u:User) WHERE u.username = $username RETURN u''', username=username)) close_db() return user
def enrolled_in(self, username, course_id): """Determines if a user is enrolled in a course already""" db = get_db() query = '''MATCH (u:User)-[r:ENROLLED]->(c:Course) WHERE c.course_id = $course_id AND u.username = $username RETURN r''' result = db.run(query, course_id=course_id, username=username).single() return True if result != None and len(result) > 0 else False
def check_password(self, username, password): db = get_db() query = db.run( f'MATCH (u:User) WHERE u.username="******" AND u.password="******" RETURN COUNT(u)' ).records() for item in query: if item[0] == 1: result = True else: result = False close_db() return result
def create(self): db = get_db() user = { 'age': self.age, 'id': self.id, 'name': self.name, 'password': self.password, 'university': self.university, 'username': self.username } user = db.run('CREATE (a:Author $user) RETURN a', user=user) close_db() return user.value()[0]
def enroll_in_course(self, username, course_id): db = get_db() enrollment = { 'enrollment_date': date.today(), 'status': 'IN_PROGRESS', } query = '''MATCH (c:Course), (u:User) WHERE c.course_id = $course_id AND u.username = $username CREATE(u)-[r:ENROLLED $enrollment] -> (c) RETURN type(r)''' result = db.run(query, course_id=course_id, username=username, enrollment=enrollment) return True
def find_all(self, page=0, page_size=20): db = get_db() courses = db.run(f"MATCH (u:User)-[e:ENROLLED]->(c:Course)\ RETURN u.name, collect(DISTINCT c.title) as Courses, collect(DISTINCT c.course_id) as CourseIds, u.username SKIP {page} LIMIT {page_size}" ).records() result_array = [] for item in courses: result_array += [{ "user": item[0], "courses_taken": item[1], "course_id": item[2], "username": item[3] }] close_db() return result_array
def get_weekSections(self, course_id): db = get_db() query = db.run(''' MATCH (w:weekSection) WHERE w.course_id= $course_id RETURN DISTINCT w.week_num, w.week_name, w.week_description ORDER BY w.week_num ''', course_id=course_id).records() result_array = [] for item in query: result_array += [{ "week_num": item[0], "week_title": item[1], "week_description": item[2] }] close_db() return result_array
def find(self, query='', page=0, page_size=20): db = get_db() # Problem: search using author, results in author column only output 1 author (keyword) # and not multiple, if there are any. courses = db.run( f'MATCH (u:User)-[e:ENROLLED]->(c:Course) WHERE (u.name =~ ".*(?i){query}.*")\ RETURN u.name, collect(DISTINCT c.title) as Courses, collect(DISTINCT c.course_id) as CourseIds, u.username SKIP {page} LIMIT {page_size}' ).records() result_array = [] for item in courses: result_array += [{ "user": item[0], "courses_taken": item[1], # '; '.join(item[1]), "course_id": item[2], "username": item[3] }] print(result_array) close_db() return result_array
def get_prerequisites(self, course_id): db = get_db() query = '''MATCH(c: Course)-[r:REQUIRES*1..]->(c2: Course)<-[:PROVIDED_BY]->(p: Provider) WHERE c.course_id= $course_id RETURN c2.course_id, c2.description, c2.title, c2.photo_link, c2.direct_link, p.provider''' results = db.run(query, course_id=course_id).records() courses = [] for item in results: courses += [{ "course_id": item[0], "description": item[1], "title": item[2], "photo_link": item[3], "direct_link": item[4], "provider": item[5] }] return courses