diff --git a/lms/lms/utils.py b/lms/lms/utils.py index baf6dbee..04939d1b 100644 --- a/lms/lms/utils.py +++ b/lms/lms/utils.py @@ -1,10 +1,13 @@ import re import frappe -from frappe.utils import flt, cint, cstr, getdate, add_months, fmt_money +from frappe.utils import flt, cint, cstr, getdate, add_months, fmt_money, get_datetime, format_date from lms.lms.md import markdown_to_html, find_macros import string from frappe import _ from frappe.desk.doctype.notification_log.notification_log import make_notification_logs +from frappe.desk.doctype.dashboard_chart.dashboard_chart import get_result +from frappe.utils.dateutils import get_period + RE_SLUG_NOTALLOWED = re.compile("[^a-z0-9]+") @@ -546,3 +549,38 @@ def get_filtered_membership(course, memberships): def show_start_learing_cta(course, membership): return not course.disable_self_learning and not membership and not course.upcoming \ and not check_profile_restriction() and not is_instructor(course.name) and course.status == "Approved" + + +@frappe.whitelist(allow_guest=True) +def get_chart_data(chart_name, timespan, timegrain, from_date, to_date): + chart = frappe.get_doc("Dashboard Chart", chart_name) + filters = [([chart.document_type, "docstatus", "<", 2, False])] + doctype = chart.document_type + datefield = chart.based_on + value_field = chart.value_based_on or "1" + from_date = get_datetime(from_date).strftime("%Y-%m-%d") + to_date = get_datetime(to_date) + + filters.append([doctype, datefield, ">=", from_date, False]) + filters.append([doctype, datefield, "<=", to_date, False]) + + data = frappe.db.get_all( + doctype, + fields=[f"{datefield} as _unit", f"SUM({value_field})", "COUNT(*)"], + filters=filters, + group_by="_unit", + order_by="_unit asc", + as_list=True, + ) + + result = get_result(data, timegrain, from_date, to_date, chart.chart_type) + + return { + "labels": [ + format_date(get_period(r[0], timegrain), parse_day_first=True) + if timegrain in ("Daily", "Weekly") + else get_period(r[0], timegrain) + for r in result + ], + "datasets": [{"name": chart.name, "values": [r[1] for r in result]}], + } diff --git a/lms/www/courses/index.html b/lms/www/courses/index.html index e4f7d693..85a55751 100644 --- a/lms/www/courses/index.html +++ b/lms/www/courses/index.html @@ -77,13 +77,11 @@ {% endif %} - {% if frappe.session.user != "Guest" %} - {% endif %}
@@ -121,11 +119,9 @@ {% endif %} - {% if frappe.session.user != "Guest" %}
{% include "lms/templates/stats.html" %}
- {% endif %} diff --git a/lms/www/courses/index.js b/lms/www/courses/index.js index 7fccbce9..415d6a5a 100644 --- a/lms/www/courses/index.js +++ b/lms/www/courses/index.js @@ -1,9 +1,7 @@ frappe.ready(() => { - if (frappe.session.user != "Guest") { - generate_graph("New Signups"); - generate_graph("Course Enrollments"); - } + generate_graph("New Signups"); + generate_graph("Course Enrollments"); }); @@ -11,10 +9,11 @@ const generate_graph = (chart_name) => { let date = frappe.datetime; frappe.call({ - method: "frappe.desk.doctype.dashboard_chart.dashboard_chart.get", + method: "lms.lms.utils.get_chart_data", args: { "chart_name": chart_name, "timespan": "Select Date Range", + "timegrain": "Daily", "from_date": date.add_days(date.get_today(), -30), "to_date": date.get_today() },