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
|
$(document).ready(function() {
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
var simplemde = new SimpleMDE({
element: document.getElementById("id_description"),
forceSync: true,
hideIcons: ["preview", "side-by-side", "fullscreen"]
});
simplemde.codemirror.on("change", function() {
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
$("#description_body").html(simplemde.markdown(simplemde.value()));
});
const player = new Plyr('#player');
var timer = $("#vtimer");
var totalSeconds;
player.on('timeupdate', event => {
totalSeconds = parseInt(player.currentTime)
hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;
minutes = Math.floor(totalSeconds / 60);
seconds = totalSeconds % 60;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
timer.val(hours + ":" + minutes + ":" + seconds);
});
$("#vtimer").on("change keyup paste", function() {
player.pause();
var time = $(this).val().split(":");
var hh = parseInt(time[0]);
var mm = parseInt(time[1]);
var ss = parseInt(time[2]);
player.currentTime = hh * 3600 + mm * 60 + ss;
});
$('#content-type').on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
if (valueSelected == "" || valueSelected == "1") {
$("#id_type").hide();
$("#id_type").attr("required", false);
} else {
if(valueSelected == "4") {
$('#id_type option[value="mcq"]').prop("selected", true);
$('#id_type').attr("disabled", true);
} else {
$('#id_type').attr("disabled", false);
}
$("#id_type").show();
$("#id_type").attr("required", true);
}
});
// Marker Form
$("#marker-form").submit(function(e) {
e.preventDefault();
lock_screen();
var csrf = document.getElementById("marker-form").elements[0].value;
ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf);
});
$('#id_video_file').on('change',function() {
//get the file name
var files = [];
for (var i = 0; i < $(this)[0].files.length; i++) {
files.push($(this)[0].files[i].name);
}
$(this).next('.custom-file-label').html(files.join(', '));
});
$('#id_Lesson_files').on('change',function() {
//get the file name
var files = [];
for (var i = 0; i < $(this)[0].files.length; i++) {
files.push($(this)[0].files[i].name);
}
$(this).next('.custom-file-label').html(files.join(', '));
});
});
function add_topic() {
if (!$("#id_timer").val()) {
$("#id_timer").val($("#vtimer").val());
}
document.getElementById("id_timer").focus();
$("#topic-form").submit(function(e) {
e.preventDefault();
lock_screen();
var csrf = document.getElementById("topic-form").elements[0].value;
ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf);
});
}
function add_question() {
if (!$("#id_timer").val()) {
$("#id_timer").val($("#vtimer").val());
}
document.getElementById("id_timer").focus();
$("#question-form").submit(function(e) {
e.preventDefault();
lock_screen();
var csrf = document.getElementById("question-form").elements[0].value;
ajax_call($(this).attr("action"), $(this).attr("method"), $(this).serialize(), csrf);
});
}
function lock_screen() {
document.getElementById("loader").style.display = "block";
}
function unlock_screen() {
document.getElementById("loader").style.display = "none";
}
function show_error(error) {
var err_msg = "\n";
Object.keys(err).forEach(function(key) {
var value = err[key];
err_msg = err_msg + key + " : " + value[0].message + "\n";
});
show_message(err_msg, "error");
}
function show_message(message, msg_type) {
toastr.options = {
"positionClass": "toast-top-center",
"timeOut": "1500",
"showDuration": "300",
}
switch(msg_type) {
case "info": {
toastr.info(message);
break;
}
case "error": {
toastr.error(message);
break;
}
case "warning": {
toastr.warning(message);
break;
}
case "success": {
toastr.success(message);
break;
}
default: {
toastr.info(message);
break;
}
}
}
function show_toc(toc) {
$("#lesson-content").empty();
$("#toc").html(toc);
}
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function ajax_call(url, method, data, csrf) {
$.ajax({
url: url,
timeout: 15000,
method: method,
data: data,
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrf);
}
},
success: function(msg) {
unlock_screen();
if (msg.status) $("#lesson-content").html(msg.data);
if (parseInt(msg.content_type) === 1) {
add_topic();
}
else {
add_question();
}
if (msg.toc) show_toc(msg.toc);
if (msg.message) {
if (msg.success) {
show_message(msg.message, "success");
}
else {
show_message(msg.message, "warning");
}
}
},
error: function(xhr, data) {
unlock_screen();
switch(xhr.status) {
case 400: {
err = JSON.parse(xhr.responseJSON.message);
show_error(err);
break;
}
case 500: {
show_message('500 status code! server error', "error");
break;
}
case 404: {
show_message('404 status code! server error', "error");
break;
}
default: {
show_message('Unable to perform action. Please try again', "error");
break;
}
}
}
});
}
|