feat: add new batch and mentor course mapping

This commit is contained in:
pateljannat
2021-04-21 10:47:10 +05:30
parent a2fe314420
commit b09ec1e29d
8 changed files with 71 additions and 64 deletions

View File

@@ -99,10 +99,9 @@
"label": "Start Time" "label": "Start Time"
}, },
{ {
"description": "Enter Days of the week comma separated.",
"fieldname": "sessions_on", "fieldname": "sessions_on",
"fieldtype": "Data", "fieldtype": "Data",
"label": "Sessions On" "label": "Sessions On Days"
}, },
{ {
"fieldname": "end_time", "fieldname": "end_time",
@@ -112,7 +111,7 @@
], ],
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"links": [], "links": [],
"modified": "2021-04-15 10:52:24.358628", "modified": "2021-04-21 10:21:23.183399",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "LMS", "module": "LMS",
"name": "LMS Batch", "name": "LMS Batch",

View File

@@ -29,7 +29,7 @@ class LMSBatchMembership(Document):
frappe.throw(_("{0} is already a {1} of {2} course through {3} batch").format(member_name, membership.member_type, course, membership.batch)) frappe.throw(_("{0} is already a {1} of {2} course through {3} batch").format(member_name, membership.member_type, course, membership.batch))
@frappe.whitelist() @frappe.whitelist()
def create_member(batch, member=None, member_type="Student", role="Member"): def create_membership(batch, member=None, member_type="Student", role="Member"):
if not member: if not member:
member = frappe.db.get_value("Community Member", {"email": frappe.session.user}, "name") member = frappe.db.get_value("Community Member", {"email": frappe.session.user}, "name")
frappe.get_doc({ frappe.get_doc({

View File

@@ -3,8 +3,16 @@
# For license information, please see license.txt # For license information, please see license.txt
from __future__ import unicode_literals from __future__ import unicode_literals
# import frappe import frappe
from frappe.model.document import Document from frappe.model.document import Document
from frappe import _
class LMSCourseMentorMapping(Document): class LMSCourseMentorMapping(Document):
pass def validate(self):
duplicate_mapping = frappe.get_all("LMS Course Mentor Mapping",
filters = {
"course": self.course,
"mentor": self.mentor
})
if len(duplicate_mapping):
frappe.throw(_("{0} is already a mentor for course {1}").format(self.mentor_name, self.course))

View File

@@ -1,3 +1,17 @@
frappe.ready(function() { frappe.ready(function() {
// bind events here frappe.web_form.after_save = () => {
let data = frappe.web_form.get_values();
frappe.call({
"method": "community.lms.doctype.lms_batch_membership.lms_batch_membership.create_membership",
"args": {
"batch": data.title,
"member_type": "Mentor"
},
"callback": (data) => {
if (data.message == "OK") {
window.location.href = "/courses"
}
}
})
}
}) })

View File

@@ -18,7 +18,7 @@
"is_standard": 1, "is_standard": 1,
"login_required": 1, "login_required": 1,
"max_attachment_size": 0, "max_attachment_size": 0,
"modified": "2021-04-20 11:51:35.815876", "modified": "2021-04-21 10:22:38.461088",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "LMS", "module": "LMS",
"name": "add-a-new-batch", "name": "add-a-new-batch",
@@ -64,7 +64,7 @@
"fieldname": "start_time", "fieldname": "start_time",
"fieldtype": "Data", "fieldtype": "Data",
"hidden": 0, "hidden": 0,
"label": "Start Time", "label": "Start Time (HH:MM:SS)",
"max_length": 0, "max_length": 0,
"max_value": 0, "max_value": 0,
"read_only": 0, "read_only": 0,
@@ -96,11 +96,11 @@
}, },
{ {
"allow_read_on_all_link_options": 0, "allow_read_on_all_link_options": 0,
"description": "Enter Days of the week comma separated.", "description": "",
"fieldname": "sessions_on", "fieldname": "sessions_on",
"fieldtype": "Data", "fieldtype": "Data",
"hidden": 0, "hidden": 0,
"label": "Sessions On", "label": "Sessions On Days",
"max_length": 0, "max_length": 0,
"max_value": 0, "max_value": 0,
"options": "", "options": "",
@@ -113,7 +113,7 @@
"fieldname": "end_time", "fieldname": "end_time",
"fieldtype": "Data", "fieldtype": "Data",
"hidden": 0, "hidden": 0,
"label": "End Time", "label": "End Time (HH:MM:SS)",
"max_length": 0, "max_length": 0,
"max_value": 0, "max_value": 0,
"read_only": 0, "read_only": 0,

View File

@@ -98,12 +98,14 @@
<h2 class="course-title" data-course={{ course.name | urlencode }}>{{ course.title }}</h2> <h2 class="course-title" data-course={{ course.name | urlencode }}>{{ course.title }}</h2>
{{course.short_introduction}} {{course.short_introduction}}
</div> </div>
{% if course.video_link %}
<div> <div>
<iframe width="417" height="274" src="{{course.video_link}}" title="YouTube video player" <iframe width="417" height="274" src="{{course.video_link}}" title="YouTube video player"
frameborder="0" frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe> allowfullscreen></iframe>
</div> </div>
{% endif %}
</div> </div>
{% if is_mentor %} {% if is_mentor %}
<h4 class="margin-bottom">Your Batches</h4> <h4 class="margin-bottom">Your Batches</h4>
@@ -138,18 +140,18 @@
{% endif %} {% endif %}
<hr> <hr>
<div class="text-right mb-5" style="max-height: 10%;"> <div class="text-right mb-5" style="max-height: 10%;">
<button class="btn btn-primary mb-2 mr-2 manage-batch" data-batch="{{ batch.name | urlencode }}">Manage</button> <button class="btn btn-primary btn-sm mb-2 mr-2 manage-batch" data-batch="{{ batch.name | urlencode }}">Manage</button>
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<a class="btn btn-primary add-batch" href="/add-a-new-batch?course={{course.name}}" data-course="{{course.name | urlencode}}">Add a new batch</a> <a class="btn btn-primary add-batch margin-bottom" href="/add-a-new-batch?new=1&course={{course.name}}" data-course="{{course.name | urlencode}}">Add a new batch</a>
</div> </div>
{% else %} {% else %}
<div> <div class="margin-bottom">
<div class="mentor_message"> <div class="mentor_message">
<p> You are a mentor for this course. </p> <p> You are a mentor for this course. </p>
<button class="btn btn-primary"> Create your first batch </button> <a class="btn btn-primary" href="/add-a-new-batch?new=1&course={{course.name}}" > Create your first batch </a>
</div> </div>
</div> </div>
{% endif %} {% endif %}
@@ -182,7 +184,7 @@
{% endif %} {% endif %}
<hr> <hr>
<div class="text-right mb-5" style="max-height: 10%;"> <div class="text-right mb-5" style="max-height: 10%;">
<button class="btn btn-primary mb-2 mr-2 join-batch" data-batch="{{ batch.name | urlencode }}">Join <button class="btn btn-primary btn-sm mb-2 mr-2 join-batch" data-batch="{{ batch.name | urlencode }}">Join
now</button> now</button>
</div> </div>
</div> </div>
@@ -227,6 +229,7 @@
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
{% if not is_mentor %}
<div class="pt-5 mentor-request"> <div class="pt-5 mentor-request">
Interested to mentor this course? <span class="apply-now" data-course={{course.name | urlencode}} Interested to mentor this course? <span class="apply-now" data-course={{course.name | urlencode}}
style="color: #2490EF; cursor: pointer;">Apply now!</span> style="color: #2490EF; cursor: pointer;">Apply now!</span>
@@ -236,6 +239,7 @@
any more interested to mentor this course, you can <span class="cancel-request" data-course={{course.name | any more interested to mentor this course, you can <span class="cancel-request" data-course={{course.name |
urlencode}} style="color: #2490EF; cursor: pointer;">Cancel your Application</span>. urlencode}} style="color: #2490EF; cursor: pointer;">Cancel your Application</span>.
</div> </div>
{% endif %}
{% if course.topics %} {% if course.topics %}
<div class="mt-5"> <div class="mt-5">
<h4 class="margin-bottom">Topics</h4> <h4 class="margin-bottom">Topics</h4>

View File

@@ -92,7 +92,7 @@ frappe.ready(() => {
} }
batch = decodeURIComponent($(e.currentTarget).attr("data-batch")) batch = decodeURIComponent($(e.currentTarget).attr("data-batch"))
frappe.call({ frappe.call({
"method": "community.lms.doctype.lms_batch_membership.lms_batch_membership.create_member", "method": "community.lms.doctype.lms_batch_membership.lms_batch_membership.create_membership",
"args": { "args": {
"batch": batch "batch": batch
}, },

View File

@@ -3,36 +3,27 @@ from frappe.utils import nowdate, getdate
def get_context(context): def get_context(context):
context.no_cache = 1 context.no_cache = 1
try: try:
course_id = frappe.form_dict["course"] course_id = frappe.form_dict["course"]
except KeyError: except KeyError:
frappe.local.flags.redirect_location = "/courses" frappe.local.flags.redirect_location = "/courses"
raise frappe.Redirect raise frappe.Redirect
context.course = get_course(course_id) context.course = get_course(course_id)
context.batches = get_course_batches(context.course.name) context.batches = get_course_batches(context.course.name)
context.upcoming_batches = get_upcoming_batches(context.course.name, context.batches) context.memberships = get_membership(context.batches)
#context.course_enrolled = has_enrolled(course_id) context.upcoming_batches = [] if len(context.memberships) else get_upcoming_batches(context.course.name)
context.discussions, context.memberships = get_discussions(context.batches) context.instructor = get_instructor(context.course.owner)
context.instructor = get_instructor(course_id)
context.mentors = get_mentors(context.course.name) context.mentors = get_mentors(context.course.name)
if context.memberships: context.is_mentor = is_mentor(context.course.name)
context.member_type = context.memberships[0].member_type
if context.member_type != "Student": if context.is_mentor:
context.batches = [membership.batch for membership in context.memberships] context.mentor_batches = get_mentor_batches(context.memberships) # Your Bacthes for mentor
if context.member_type == "Mentor":
context.is_mentor = True
context.mentor_batches = get_mentor_batches(context.memberships)
context.current_batch = context.memberships[0].batch
context.author = context.memberships[0].member
else:
context.membership_type = None
context.batches = []
context.current_batch = None
context.author = None
def get_course(slug): def get_course(slug):
course = frappe.db.get_value("LMS Course", {"slug": slug}, course = frappe.db.get_value("LMS Course", {"slug": slug},
["name", "slug", "title", "description", "short_introduction", "video_link"], as_dict=1) ["name", "slug", "title", "description", "short_introduction", "video_link", "owner"], as_dict=1)
course["topics"] = frappe.db.get_all("LMS Topic", course["topics"] = frappe.db.get_all("LMS Topic",
filters={ filters={
@@ -43,13 +34,10 @@ def get_course(slug):
) )
return course return course
def get_upcoming_batches(course, all_batches): def get_upcoming_batches(course):
memberships = get_membership(all_batches)
if not len(memberships):
batches = frappe.get_all("LMS Batch", {"course": course, "start_date": [">", nowdate()]}, ["start_date", "start_time", "end_time", "sessions_on", "name"]) batches = frappe.get_all("LMS Batch", {"course": course, "start_date": [">", nowdate()]}, ["start_date", "start_time", "end_time", "sessions_on", "name"])
batches = get_batch_mentors(batches) batches = get_batch_mentors(batches)
return batches return batches
return []
def get_batch_mentors(batches): def get_batch_mentors(batches):
for batch in batches: for batch in batches:
@@ -76,19 +64,16 @@ def get_membership(batches):
memberships.append(membership) memberships.append(membership)
return memberships return memberships
def get_instructor(slug): def get_instructor(owner):
owner = frappe.db.get_value("LMS Course", {"slug":slug}, ["owner"])
instructor = frappe.get_doc("Community Member", {"email": owner}) instructor = frappe.get_doc("Community Member", {"email": owner})
instructor.course_count = len(frappe.get_all("LMS Course", {"owner": owner})) instructor.course_count = len(frappe.get_all("LMS Course", {"owner": owner}))
return instructor return instructor
def get_mentors(course): def get_mentors(course):
course_mentors = [] course_mentors = []
mentors = frappe.get_all("LMS Course Mentor Mapping", {"course": course}, ["member"]) mentors = frappe.get_all("LMS Course Mentor Mapping", {"course": course}, ["mentor"])
for mentor in mentors: for mentor in mentors:
member = frappe.get_doc("Community Member", mentor.member) member = frappe.get_doc("Community Member", mentor.mentor)
member_in_list = list(filter(lambda x: x.name == member.name, course_mentors))
if not member_in_list:
member.batch_count = len(frappe.get_all("LMS Batch Membership", {"member": member.name, "member_type": "Mentor"})) member.batch_count = len(frappe.get_all("LMS Batch Membership", {"member": member.name, "member_type": "Mentor"}))
course_mentors.append(member) course_mentors.append(member)
return course_mentors return course_mentors
@@ -112,6 +97,15 @@ def get_mentor_batches(memberships):
mentor_batches = get_batch_mentors(mentor_batches) mentor_batches = get_batch_mentors(mentor_batches)
return mentor_batches return mentor_batches
def is_mentor(course):
try:
member = frappe.db.get_value("Community Member", {"email": frappe.session.user}, ["name"])
except frappe.DoesNotExistError:
return False
mapping = frappe.get_all("LMS Course Mentor Mapping", {"course": course, "mentor": member})
if len(mapping):
return True
@frappe.whitelist() @frappe.whitelist()
def get_messages(batch): def get_messages(batch):
messages = frappe.get_all("LMS Message", {"batch": batch}, ["*"], order_by="creation desc") messages = frappe.get_all("LMS Message", {"batch": batch}, ["*"], order_by="creation desc")
@@ -123,18 +117,6 @@ def get_messages(batch):
message.author_name = "You" message.author_name = "You"
return messages return messages
@frappe.whitelist()
def has_enrolled(course):
return frappe.db.get_value("LMS Course Enrollment", {"course": course, "owner": frappe.session.user})
@frappe.whitelist()
def enroll(course):
return frappe.get_doc({
"doctype": "LMS Course Enrollment",
"course": course,
"user": frappe.session.user
}).save()
@frappe.whitelist() @frappe.whitelist()
def save_message(message, author, batch): def save_message(message, author, batch):
doc = frappe.get_doc({ doc = frappe.get_doc({