summaryrefslogtreecommitdiff
path: root/stats
diff options
context:
space:
mode:
authorprathamesh2020-11-09 15:52:56 +0530
committerprathamesh2020-11-09 15:52:56 +0530
commitb6f78c5db15cb7e34649ca67f3295dcb1a102bbe (patch)
tree68f1b3b669e1ad265e6b79a227716e2bbc14aa22 /stats
parent530fb70da288d53be0c7e293121e807ffa2a73c2 (diff)
downloadonline_test-b6f78c5db15cb7e34649ca67f3295dcb1a102bbe.tar.gz
online_test-b6f78c5db15cb7e34649ca67f3295dcb1a102bbe.tar.bz2
online_test-b6f78c5db15cb7e34649ca67f3295dcb1a102bbe.zip
Modify Lesson Statistics
- gives total spent time - sets initial percentage to less than 25% - gives completion status(True or False)
Diffstat (limited to 'stats')
-rw-r--r--stats/models.py75
-rw-r--r--stats/templates/view_lesson_tracking.html29
-rw-r--r--stats/views.py10
3 files changed, 89 insertions, 25 deletions
diff --git a/stats/models.py b/stats/models.py
index 95def40..0200a80 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -7,6 +7,19 @@ from django.contrib.auth.models import User
from yaksh.models import Course, Lesson
+def str_to_datetime(s):
+ return timezone.datetime.strptime(s, '%H:%M:%S')
+
+
+def str_to_time(s):
+ return timezone.datetime.strptime(s, '%H:%M:%S').time()
+
+
+def time_to_seconds(time):
+ return timezone.timedelta(hours=time.hour, minutes=time.minute,
+ seconds=time.second).total_seconds()
+
+
class TrackLesson(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
@@ -14,16 +27,71 @@ class TrackLesson(models.Model):
current_time = models.CharField(max_length=100, default="00:00:00")
video_duration = models.CharField(max_length=100, default="00:00:00")
creation_time = models.DateTimeField(auto_now_add=True)
+ watched = models.BooleanField(default=False)
class Meta:
unique_together = ('user', 'course', 'lesson')
- def get_last_access_time_and_vists(self):
+ def get_log_counter(self):
+ return self.lessonlog_set.count()
+
+ def get_current_time(self):
+ if self.current_time == '00:00:00':
+ return 'just started'
+ return self.current_time
+
+ def get_video_duration(self):
+ if self.video_duration == '00:00:00':
+ return 'will be available after 25% completion'
+ return self.video_duration
+
+ def set_current_time(self, ct):
+ t = timezone.datetime.strptime(ct, '%H:%M:%S').time()
+ current = timezone.datetime.strptime(self.current_time,
+ '%H:%M:%S').time()
+ if t > current:
+ self.current_time = ct
+
+ def get_percentage_complete(self):
+ if self.current_time == '00:00:00' and self.video_duration == '00:00:00':
+ return 'less than 25%'
+ duration = str_to_time(self.video_duration)
+ watch_time = str_to_time(self.current_time)
+ duration_seconds = time_to_seconds(duration)
+ watched_seconds = time_to_seconds(watch_time)
+ percentage = round((watched_seconds / duration_seconds) * 100)
+ return 'approx {0} %'.format(percentage)
+
+
+ def get_last_access_time(self):
lesson_logs = self.lessonlog_set
- last_access_time = None
+ last_access_time = self.creation_time
if lesson_logs.exists():
last_access_time = lesson_logs.last().last_access_time
- return last_access_time, lesson_logs.count()
+ return last_access_time
+
+ def set_watched(self):
+ if self.current_time != '00:00:00' and self.video_duration != '00:00:00':
+ duration = str_to_time(self.video_duration)
+ watch_time = (str_to_datetime(self.current_time) + timezone.timedelta(
+ seconds=10)).time()
+ self.watched = watch_time >= duration
+
+ def get_watched(self):
+ if not self.watched:
+ self.set_watched()
+ self.save()
+ return self.watched
+
+
+ def time_spent(self):
+ if self.video_duration != '00:00:00':
+ hits = self.get_log_counter()
+ duration = str_to_time(self.video_duration)
+ hit_duration = int((time_to_seconds(duration))/4)
+ total_duration = hits * hit_duration
+ return str(timezone.timedelta(seconds=total_duration))
+ return self.get_current_time()
def __str__(self):
return (f"Track {self.lesson} in {self.course} "
@@ -32,4 +100,5 @@ class TrackLesson(models.Model):
class LessonLog(models.Model):
track = models.ForeignKey(TrackLesson, on_delete=models.CASCADE)
+ current_time = models.CharField(max_length=20, default="00:00:00")
last_access_time = models.DateTimeField(default=timezone.now)
diff --git a/stats/templates/view_lesson_tracking.html b/stats/templates/view_lesson_tracking.html
index 7962410..ef5c9ae 100644
--- a/stats/templates/view_lesson_tracking.html
+++ b/stats/templates/view_lesson_tracking.html
@@ -15,15 +15,6 @@
$(document).ready(function() {
$("#stats-table").tablesorter({});
- $('#stats-table tr').each(function() {
- var td = $(this).find("td");
- var elapsed = td.eq(4).html();
- var duration = td.eq(5).html();
- if (elapsed != undefined || duration != undefined) {
- percent = (get_time_in_seconds(elapsed) / get_time_in_seconds(duration)) * 100;
- td.eq(6).html(Math.round(percent));
- }
- });
});
</script>
{% endblock %}
@@ -49,22 +40,22 @@
<th>Started on&nbsp;<i class="fa fa-sort"></i></th>
<th>Current Duration&nbsp;<i class="fa fa-sort"></i></th>
<th>Video Duration&nbsp;<i class="fa fa-sort"></i></th>
- <th>Percentage watched&nbsp;<i class="fa fa-sort"></i></th>
- <th>Total visits&nbsp;<i class="fa fa-sort"></i></th>
+ <th>Percentage Watched&nbsp;<i class="fa fa-sort"></i></th>
+ <th>Watched Once Completely&nbsp;<i class="fa fa-sort"></i></th>
+ <th>Total Time Spent&nbsp;<i class="fa fa-sort"></i></th>
</tr>
</thead>
{% for track in trackings %}
<tr>
<td>{{ forloop.counter0|add:objects.start_index }}</td>
<td>{{track.user.get_full_name}}</td>
- {% with track.get_last_access_time_and_vists as time_and_visits %}
- <td>{{time_and_visits.0}}</td>
+ <td>{{track.get_last_access_time}}</td>
<td>{{track.creation_time}}</td>
- <td>{{track.current_time}}</td>
- <td>{{track.video_duration}}</td>
- <td></td>
- <td>{{time_and_visits.1}}</td>
- {% endwith %}
+ <td>{{track.get_current_time}}</td>
+ <td>{{track.get_video_duration}}</td>
+ <td>{{track.get_percentage_complete}}</td>
+ <td>{{track.get_watched}}</td>
+ <td>{{track.time_spent}}</td>
</tr>
{% endfor %}
</table>
@@ -72,4 +63,4 @@
<br>
{% include "yaksh/paginator.html" %}
</div>
-{% endblock %} \ No newline at end of file
+{% endblock %}
diff --git a/stats/views.py b/stats/views.py
index f7c028f..53b7cf7 100644
--- a/stats/views.py
+++ b/stats/views.py
@@ -7,7 +7,7 @@ from django.core.paginator import Paginator
from django.http import Http404
# Local Imports
-from stats.models import TrackLesson, LessonLog
+from stats.models import TrackLesson, LessonLog, str_to_time, time_to_seconds
from yaksh.models import Course
from yaksh.decorators import email_verified
@@ -25,12 +25,16 @@ def add_tracker(request, tracker_id):
video_duration = request.POST.get('video_duration')
current_time = request.POST.get('current_video_time')
if current_time:
- track.current_time = current_time
+ track.set_current_time(current_time)
track.video_duration = video_duration
LessonLog.objects.create(
- track_id=track.id, last_access_time=timezone.now()
+ track_id=track.id, current_time=current_time,
+ last_access_time=timezone.now()
)
track.save()
+ if not track.watched:
+ track.set_watched()
+ track.save()
success = True
else:
success = False