LMS: Course Activity

Co-authored-by: Chinmay Pai <chinmaydpai@gmail.com>
diff --git a/erpnext/education/doctype/course_activity/course_activity.json b/erpnext/education/doctype/course_activity/course_activity.json
index 2708974..76c82b5 100644
--- a/erpnext/education/doctype/course_activity/course_activity.json
+++ b/erpnext/education/doctype/course_activity/course_activity.json
@@ -45,7 +45,7 @@
    "search_index": 0, 
    "set_only_once": 1, 
    "translatable": 0, 
-   "unique": 1
+   "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
@@ -122,8 +122,41 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "content_type", 
+   "fieldtype": "Select", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Content Type", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "\nArticle\nVideo", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "content", 
-   "fieldtype": "Link", 
+   "fieldtype": "Dynamic Link", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "ignore_xss_filter": 0, 
@@ -134,7 +167,7 @@
    "label": "Content", 
    "length": 0, 
    "no_copy": 0, 
-   "options": "Content", 
+   "options": "content_type", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 0, 
@@ -191,7 +224,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-10-15 17:49:09.157437", 
+ "modified": "2018-10-23 11:53:35.054871", 
  "modified_by": "Administrator", 
  "module": "Education", 
  "name": "Course Activity", 
diff --git a/erpnext/education/doctype/quiz_activity/quiz_activity.json b/erpnext/education/doctype/quiz_activity/quiz_activity.json
index 29086f1..b618cf5 100644
--- a/erpnext/education/doctype/quiz_activity/quiz_activity.json
+++ b/erpnext/education/doctype/quiz_activity/quiz_activity.json
@@ -43,7 +43,7 @@
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
-   "set_only_once": 0, 
+   "set_only_once": 1, 
    "translatable": 0, 
    "unique": 0
   }, 
@@ -62,7 +62,7 @@
    "ignore_xss_filter": 0, 
    "in_filter": 0, 
    "in_global_search": 0, 
-   "in_list_view": 0, 
+   "in_list_view": 1, 
    "in_standard_filter": 0, 
    "label": "Student", 
    "length": 0, 
@@ -72,7 +72,7 @@
    "precision": "", 
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
-   "read_only": 0, 
+   "read_only": 1, 
    "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
@@ -137,7 +137,7 @@
    "precision": "", 
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
-   "read_only": 0, 
+   "read_only": 1, 
    "remember_last_selected_value": 0, 
    "report_hide": 0, 
    "reqd": 0, 
@@ -191,7 +191,7 @@
    "ignore_xss_filter": 0, 
    "in_filter": 0, 
    "in_global_search": 0, 
-   "in_list_view": 0, 
+   "in_list_view": 1, 
    "in_standard_filter": 0, 
    "label": "Quiz", 
    "length": 0, 
@@ -304,7 +304,7 @@
    "report_hide": 0, 
    "reqd": 0, 
    "search_index": 0, 
-   "set_only_once": 0, 
+   "set_only_once": 1, 
    "translatable": 0, 
    "unique": 0
   }, 
@@ -322,7 +322,7 @@
    "ignore_xss_filter": 0, 
    "in_filter": 0, 
    "in_global_search": 0, 
-   "in_list_view": 0, 
+   "in_list_view": 1, 
    "in_standard_filter": 0, 
    "label": "Score", 
    "length": 0, 
@@ -351,7 +351,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-10-19 16:27:05.321554", 
+ "modified": "2018-10-23 11:06:31.779929", 
  "modified_by": "Administrator", 
  "module": "Education", 
  "name": "Quiz Activity", 
diff --git a/erpnext/education/utils.py b/erpnext/education/utils.py
index 3962758..c6ca6ef 100644
--- a/erpnext/education/utils.py
+++ b/erpnext/education/utils.py
@@ -98,55 +98,28 @@
 	frappe.db.commit()
 
 @frappe.whitelist()
-def add_activity(content_type, **kwargs):
-	activity_does_not_exists, activity = check_entry_exists(kwargs.get('program'))
-	if activity_does_not_exists:
-		current_activity = frappe.get_doc({
-			"doctype": "Course Activity",
-			"student_id": get_student_id(frappe.session.user),
-			"program_name": kwargs.get('program'),
-			"lms_activity": [{
-				"course_name": kwargs.get('course'),
-				"content_name": kwargs.get('content'),
-				"status": "Completed"
-				}]
-			})
-		if content_type == "Quiz":
-			activity = current_activity.lms_activity[-1]
-			activity.quiz_score = kwargs.get('score')
-			activity.selected_options = ", ".join(kwargs.get('selected_options'))
-			activity.result = ", ".join([str(item) for item in kwargs.get('result')]),
-			activity.status = "Passed"
-		current_activity.save()
-		frappe.db.commit()
+def add_activity(content_type, content, course, program):
+	enrollment = get_course_enrollment(course, frappe.session.user)
+	if check_activity_exists(enrollment['name'], content_type, content):
+		pass
 	else:
-		if content_type in ("Article", "Video"):
-			lms_activity_list = [[data.course_name, data.content_name] for data in activity.lms_activity]
-			if not [kwargs.get('course'), kwargs.get('content')] in lms_activity_list:
-				activity.append("lms_activity", {
-					"course_name": kwargs.get('course'),
-					"content_name": kwargs.get('content'),
-					"status": "Completed"
-				})
-		else:
-			activity.append("lms_activity", {
-				"course_name": kwargs.get('course'),
-				"content_name": kwargs.get('content'),
-				"status": "Passed",
-				"quiz_score": kwargs.get('score'),
-				"selected_options": ", ".join(kwargs.get('selected_options')),
-				"result": ", ".join([str(item) for item in kwargs.get('result')])
+		course_actvity = frappe.get_doc({
+			"doctype": "Course Activity",
+			"enrollment": enrollment['name'],
+			"content_type": content_type,
+			"content": content,
+			"date": frappe.utils.datetime.datetime.now()
 			})
-		activity.save()
+		course_actvity.save()
 		frappe.db.commit()
 
-def check_entry_exists(program):
-	try:
-		activity_name = frappe.get_all("Course Activity", filters={"student_id": get_student_id(frappe.session.user), "program_name": program})[0]
-	except IndexError:
-		return True, None
+def check_activity_exists(enrollment, content_type, content):
+	activity = frappe.get_list("Course Activity", filters={"enrollment": enrollment, "content_type": content_type, "content": content})
+	if activity:
+		return True
 	else:
-		return None, frappe.get_doc("Course Activity", activity_name)
+		return False
+
 
 def get_program():
 	program_list = frappe.get_list("Program", filters={"is_published": is_published})
@@ -182,6 +155,7 @@
 	try:
 		return frappe.get_list("Course Enrollment", filters={'course':course, 'student':student_id})[0]
 	except IndexError:
+		frappe.throw("The user is not enrolled for the course {course}".format(course=course))
 		return None
 
 def get_student_id(email=None):
@@ -190,7 +164,6 @@
 	:param user: a user email address
 	"""
 	try:
-		print("email is",email)
 		return frappe.get_all('Student', filters={'student_email_id': email}, fields=['name'])[0].name
 	except IndexError:
 		frappe.throw("Student with email {0} does not exist".format(email))