scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 3 | |
scmmishra | 9c642ac | 2018-11-01 17:17:49 +0530 | [diff] [blame] | 4 | # Functions to get homepage details |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 5 | @frappe.whitelist(allow_guest=True) |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 6 | def get_portal_details(): |
| 7 | settings = frappe.get_doc("Education Settings") |
| 8 | title = settings.portal_title |
| 9 | description = settings.description |
| 10 | return dict(title=title, description=description) |
| 11 | |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 12 | @frappe.whitelist(allow_guest=True) |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 13 | def get_featured_programs(): |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 14 | featured_program_names = frappe.get_all("Program", filters={"is_published": True, "is_featured": True}) |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 15 | featured_list = [program["name"] for program in featured_program_names] |
| 16 | if featured_list: |
| 17 | return featured_list |
| 18 | else: |
| 19 | return None |
| 20 | |
scmmishra | 9c642ac | 2018-11-01 17:17:49 +0530 | [diff] [blame] | 21 | # Functions to get program & course details |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 22 | @frappe.whitelist(allow_guest=True) |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 23 | def get_program_details(program_name): |
| 24 | try: |
| 25 | program = frappe.get_doc('Program', program_name) |
| 26 | return program |
| 27 | except: |
| 28 | return None |
scmmishra | 9c642ac | 2018-11-01 17:17:49 +0530 | [diff] [blame] | 29 | |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 30 | @frappe.whitelist(allow_guest=True) |
scmmishra | 9c642ac | 2018-11-01 17:17:49 +0530 | [diff] [blame] | 31 | def get_courses(program_name): |
| 32 | program = frappe.get_doc('Program', program_name) |
| 33 | courses = program.get_course_list() |
| 34 | return courses |
| 35 | |
| 36 | @frappe.whitelist() |
| 37 | def get_starting_content(course_name): |
| 38 | course = frappe.get_doc('Course', course_name) |
| 39 | content = course.course_content[0].content |
| 40 | content_type = course.course_content[0].content_type |
| 41 | return dict(content=content, content_type=content_type) |
| 42 | |
| 43 | |
| 44 | # Functions to get content details |
| 45 | @frappe.whitelist() |
| 46 | def get_content(content_name, content_type): |
| 47 | try: |
| 48 | content = frappe.get_doc(content_type, content_name) |
| 49 | return content |
| 50 | except: |
| 51 | frappe.throw("{0} with name {1} does not exist".format(content_type, content_name)) |
| 52 | return None |
| 53 | |
| 54 | @frappe.whitelist() |
| 55 | def get_next_content(content, content_type, course): |
| 56 | course_doc = frappe.get_doc("Course", course) |
| 57 | content_list = [{'content_type':item.content_type, 'content':item.content} for item in course_doc.get_all_children()] |
| 58 | current_index = content_list.index({'content': content, 'content_type': content_type}) |
| 59 | try: |
| 60 | return content_list[current_index + 1] |
| 61 | except IndexError: |
scmmishra | 3741844 | 2018-11-01 19:50:47 +0530 | [diff] [blame] | 62 | return None |
| 63 | |
| 64 | def get_quiz_with_answers(quiz_name): |
| 65 | try: |
| 66 | quiz = frappe.get_doc("Quiz", quiz_name).get_questions() |
| 67 | quiz_output = [{'name':question.name, 'question':question.question, 'options':[{'name': option.name, 'option':option.option, 'is_correct':option.is_correct} for option in question.options]} for question in quiz] |
| 68 | return quiz_output |
| 69 | except: |
| 70 | frappe.throw("Quiz {0} does not exist".format(quiz_name)) |
| 71 | return None |
| 72 | |
| 73 | @frappe.whitelist() |
| 74 | def get_quiz_without_answers(quiz_name): |
| 75 | try: |
| 76 | quiz = frappe.get_doc("Quiz", quiz_name).get_questions() |
| 77 | quiz_output = [{'name':question.name, 'question':question.question, 'options':[{'name': option.name, 'option':option.option} for option in question.options]} for question in quiz] |
| 78 | return quiz_output |
| 79 | except: |
| 80 | frappe.throw("Quiz {0} does not exist".format(quiz_name)) |
scmmishra | f7029f0 | 2018-11-02 13:15:49 +0530 | [diff] [blame] | 81 | return None |
| 82 | |
| 83 | @frappe.whitelist() |
scmmishra | ef9a75e | 2018-11-02 16:35:46 +0530 | [diff] [blame] | 84 | def evaluate_quiz(quiz_response, quiz_name): |
| 85 | """LMS Function: Evaluates a simple multiple choice quiz. |
scmmishra | f7029f0 | 2018-11-02 13:15:49 +0530 | [diff] [blame] | 86 | |
| 87 | |
| 88 | :param quiz_response: contains user selected choices for a quiz in the form of a string formatted as a dictionary. The function uses `json.loads()` to convert it to a python dictionary. |
scmmishra | f7029f0 | 2018-11-02 13:15:49 +0530 | [diff] [blame] | 89 | """ |
| 90 | import json |
scmmishra | f7029f0 | 2018-11-02 13:15:49 +0530 | [diff] [blame] | 91 | quiz_response = json.loads(quiz_response) |
scmmishra | ef9a75e | 2018-11-02 16:35:46 +0530 | [diff] [blame] | 92 | quiz = frappe.get_doc("Quiz", quiz_name) |
| 93 | answers, score = quiz.evaluate(quiz_response, quiz_name) |
| 94 | return(score) |
scmmishra | f7029f0 | 2018-11-02 13:15:49 +0530 | [diff] [blame] | 95 | # quiz_name = kwargs.get('quiz') |
| 96 | # course_name = kwargs.get('course') |
| 97 | # enrollment = get_course_enrollment(course_name, frappe.session.user) |
| 98 | # try: |
| 99 | # quiz = frappe.get_doc("Quiz", quiz_name) |
| 100 | # answers, score = quiz.evaluate(quiz_response, enrollment, quiz_name) |
| 101 | # add_quiz_activity(enrollment, quiz_name, score, answers, quiz_response) |
| 102 | # return score |
| 103 | # except frappe.DoesNotExistError: |
| 104 | # frappe.throw("Quiz {0} does not exist".format(quiz_name)) |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 105 | # return None |
| 106 | |
| 107 | @frappe.whitelist() |
scmmishra | 73a31ec | 2018-11-03 19:31:53 +0530 | [diff] [blame] | 108 | def get_completed_courses(email=frappe.session.user): |
scmmishra | 76222f7 | 2018-11-05 11:02:14 +0530 | [diff] [blame^] | 109 | print("Get completed course ", email) |
scmmishra | 73a31ec | 2018-11-03 19:31:53 +0530 | [diff] [blame] | 110 | try: |
scmmishra | 73a31ec | 2018-11-03 19:31:53 +0530 | [diff] [blame] | 111 | student = frappe.get_doc("Student", get_student_id(email)) |
| 112 | return student.get_completed_courses() |
| 113 | except: |
| 114 | return None |
scmmishra | 8b8db54 | 2018-11-03 18:03:24 +0530 | [diff] [blame] | 115 | |
| 116 | @frappe.whitelist() |
| 117 | def get_continue_data(program_name): |
| 118 | program = frappe.get_doc("Program", program_name) |
| 119 | courses = program.get_all_children() |
| 120 | continue_data = get_starting_content(courses[0].course) |
| 121 | continue_data['course'] = courses[0].course |
| 122 | return continue_data |
| 123 | |
| 124 | def create_student(student_name=frappe.session.user): |
| 125 | student = frappe.get_doc({ |
| 126 | "doctype": "Student", |
| 127 | "first_name": student_name, |
| 128 | "student_email_id": student_name, |
| 129 | }) |
| 130 | student.save() |
| 131 | frappe.db.commit() |
| 132 | return student_name |
| 133 | |
| 134 | @frappe.whitelist() |
scmmishra | 73a31ec | 2018-11-03 19:31:53 +0530 | [diff] [blame] | 135 | def enroll_all_courses_in_program(program_enrollment, student): |
| 136 | course_list = [course.name for course in get_courses(program_enrollment.program)] |
| 137 | for course_name in course_list: |
| 138 | student.enroll_in_course(course_name=course_name, program_enrollment=program_enrollment.name) |
| 139 | |
| 140 | @frappe.whitelist() |
| 141 | def enroll_in_program(program_name, student_email_id): |
scmmishra | 8b8db54 | 2018-11-03 18:03:24 +0530 | [diff] [blame] | 142 | if(not get_student_id(student_email_id)): |
| 143 | create_student(student_email_id) |
| 144 | student = frappe.get_doc("Student", get_student_id(student_email_id)) |
scmmishra | 73a31ec | 2018-11-03 19:31:53 +0530 | [diff] [blame] | 145 | program_enrollment = student.enroll_in_program(program_name) |
| 146 | enroll_all_courses_in_program(program_enrollment, student) |
scmmishra | 8b8db54 | 2018-11-03 18:03:24 +0530 | [diff] [blame] | 147 | |
scmmishra | 73a31ec | 2018-11-03 19:31:53 +0530 | [diff] [blame] | 148 | @frappe.whitelist() |
scmmishra | 8b8db54 | 2018-11-03 18:03:24 +0530 | [diff] [blame] | 149 | def get_student_id(email=None): |
| 150 | """Returns student user name, example EDU-STU-2018-00001 (Based on the naming series). |
| 151 | |
| 152 | :param user: a user email address |
| 153 | """ |
| 154 | try: |
| 155 | return frappe.get_all('Student', filters={'student_email_id': email}, fields=['name'])[0].name |
| 156 | except IndexError: |
scmmishra | 73a31ec | 2018-11-03 19:31:53 +0530 | [diff] [blame] | 157 | return None |
| 158 | |
| 159 | @frappe.whitelist() |
| 160 | def get_program_enrollments(email=frappe.session.user): |
| 161 | try: |
| 162 | student = frappe.get_doc("Student", get_student_id(email)) |
| 163 | return student.get_program_enrollments() |
| 164 | except: |
| 165 | return None |
| 166 | |
| 167 | @frappe.whitelist() |
| 168 | def get_course_enrollments(email=frappe.session.user): |
| 169 | try: |
| 170 | student = frappe.get_doc("Student", get_student_id(email)) |
| 171 | return student.get_course_enrollments() |
| 172 | except: |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 173 | return None |
| 174 | |
| 175 | @frappe.whitelist() |
| 176 | def add_activity(enrollment, content_type, content): |
scmmishra | 76222f7 | 2018-11-05 11:02:14 +0530 | [diff] [blame^] | 177 | if(check_activity_exists(enrollment, content_type, content)): |
| 178 | pass |
| 179 | else: |
| 180 | activity = frappe.get_doc({ |
| 181 | "doctype": "Course Activity", |
| 182 | "enrollment": enrollment, |
| 183 | "content_type": content_type, |
| 184 | "content": content, |
| 185 | "activity_date": frappe.utils.datetime.datetime.now() |
| 186 | }) |
| 187 | activity.save() |
| 188 | frappe.db.commit() |
| 189 | |
| 190 | def check_activity_exists(enrollment, content_type, content): |
| 191 | activity = frappe.get_all("Course Activity", filters={'enrollment': enrollment, 'content_type': content_type, 'content': content}) |
| 192 | if activity: |
| 193 | return True |
| 194 | else: |
| 195 | return False |
| 196 | |
| 197 | @frappe.whitelist() |
| 198 | def add_quiz_activity(enrollment, quiz, score, answers): |
| 199 | pass |