forked from frappe/lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.py
More file actions
243 lines (190 loc) · 6.08 KB
/
Copy pathplugins.py
File metadata and controls
243 lines (190 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""
The plugins module provides various plugins to change the default
behaviour some parts of the lms app.
A site specify what plugins to use using appropriate entries in the frappe
hooks, written in the `hooks.py`.
This module exposes two plugins: ProfileTab and PageExtension.
The ProfileTab is used to specify any additional tabs to be displayed
on the profile page of the user.
The PageExtension is used to load additinal stylesheets and scripts to
be loaded in a webpage.
"""
from urllib.parse import quote
import frappe
from frappe import _
class PageExtension:
"""PageExtension is a plugin to inject custom styles and scripts
into a web page.
The subclasses should overwrite the `render_header()` and
`render_footer()` methods to inject whatever styles/scripts into
the webpage.
"""
def __init__(self):
self.context = frappe._dict()
def set_context(self, context):
self.context = context
def render_header(self):
"""Returns the HTML snippet to be included in the head section
of the web page.
Typically used to include the stylesheets and javascripts to be
included in the <head> of the webpage.
"""
return ""
def render_footer(self):
"""Returns the HTML snippet to be included in the body tag at
the end of web page.
Typically used to include javascripts that need to be executed
after the page is loaded.
"""
return ""
class ProfileTab:
"""Base class for profile tabs.
Every subclass of ProfileTab must implement two methods:
- get_title()
- render()
"""
def __init__(self, user):
self.user = user
def get_title(self):
"""Returns the title of the tab.
Every subclass must implement this.
"""
raise NotImplementedError()
def render(self):
"""Renders the contents of the tab as HTML.
Every subclass must implement this.
"""
raise NotImplementedError()
class LiveCodeExtension(PageExtension):
def render_header(self):
livecode_url = frappe.get_value("LMS Settings", None, "livecode_url")
context = {"livecode_url": livecode_url}
return frappe.render_template("templates/livecode/extension_header.html", context)
def render_footer(self):
livecode_url = frappe.get_value("LMS Settings", None, "livecode_url")
context = {"livecode_url": livecode_url}
return frappe.render_template("templates/livecode/extension_footer.html", context)
def quiz_renderer(quiz_name):
if frappe.session.user == "Guest":
return " <div class='alert alert-info'>" + _(
"Quiz is not available to Guest users. Please login to continue."
)
+"</div>"
quiz = frappe.db.get_value(
"LMS Quiz",
quiz_name,
[
"name",
"title",
"max_attempts",
"show_answers",
"show_submission_history",
"passing_percentage",
],
as_dict=True,
)
from lms.lms.doctype.lms_question.lms_question import (
QUESTION_CORRECTNESS_FIELDS,
QUESTION_EXPLANATION_FIELDS,
QUESTION_OPTION_FIELDS,
QUESTION_POSSIBILITY_FIELDS,
)
quiz.questions = []
fields = [
"name",
"question",
"type",
"multiple",
*QUESTION_OPTION_FIELDS,
*QUESTION_CORRECTNESS_FIELDS,
*QUESTION_EXPLANATION_FIELDS,
*QUESTION_POSSIBILITY_FIELDS,
]
questions = frappe.get_all(
"LMS Quiz Question",
filters={"parent": quiz.name},
fields=["question", "marks"],
order_by="idx",
)
for question in questions:
details = frappe.db.get_value("LMS Question", question.question, fields, as_dict=1)
details["marks"] = question.marks
quiz.questions.append(details)
no_of_attempts = frappe.db.count("LMS Quiz Submission", {"owner": frappe.session.user, "quiz": quiz_name})
if quiz.show_submission_history:
all_submissions = frappe.get_all(
"LMS Quiz Submission",
{
"quiz": quiz.name,
"member": frappe.session.user,
},
["name", "score", "creation"],
order_by="creation desc",
)
return frappe.render_template(
"templates/quiz/quiz.html",
{
"quiz": quiz,
"no_of_attempts": no_of_attempts,
"all_submissions": all_submissions if quiz.show_submission_history else None,
"hide_quiz": False,
},
)
def exercise_renderer(argument):
exercise = frappe.get_doc("LMS Exercise", argument)
context = dict(exercise=exercise)
return frappe.render_template("templates/exercise.html", context)
def youtube_video_renderer(video_id):
return f"""
<iframe width="100%" height="400"
src="https://www.youtube.com/embed/{video_id}"
title="YouTube video player"
frameborder="0"
class="youtube-video
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
"""
def embed_renderer(details):
type = details.split("|||")[0]
src = details.split("|||")[1]
width = "100%"
height = "400"
if type == "pdf":
width = "75%"
height = "600"
return f"""
<iframe width={width} height={height}
src={src}
title="Embedded Content"
frameborder="0"
style="border-radius: var(--border-radius-lg)"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
"""
def video_renderer(src):
return f"<video controls width='100%' controls controlsList='nodownload'><source src={quote(src)} type='video/mp4'></video>"
def audio_renderer(src):
return f"<audio width='100%' controls controlsList='nodownload'><source src={quote(src)} type='audio/mp3'></audio>"
def pdf_renderer(src):
return f"<iframe src='{quote(src)}#toolbar=0' width='100%' height='700px'></iframe>"
def assignment_renderer(detail):
supported_types = {
"Document": ".doc,.docx,.xml,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"PDF": ".pdf",
"Image": ".png, .jpg, .jpeg",
"Video": "video/*",
}
question = detail.split("-")[0]
file_type = detail.split("-")[1]
accept = supported_types[file_type] if file_type else ""
return frappe.render_template(
"templates/assignment.html",
{"question": question, "accept": accept, "file_type": file_type},
)
def show_custom_signup():
settings = frappe.get_single("LMS Settings")
if settings.custom_signup_content or settings.user_category:
return "lms/templates/signup-form.html"
return "frappe/templates/signup.html"