You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
443 lines
15 KiB
443 lines
15 KiB
# coding=utf-8
|
|
from django.db.models import Q
|
|
from courses.models import CourseTheme, Exam, Achievements, Lesson, Homework, SkillJ
|
|
from journals.models import LessonJ, HomeworkJ, ExamJ, AchievementJ, TeacherJ, DiplomaJ
|
|
|
|
|
|
def get_empty_face(course):
|
|
return TeacherJ.objects.filter(course__id=course).first().get_empty_face()
|
|
|
|
|
|
def set_read_flag(student, course):
|
|
pass
|
|
|
|
|
|
def get_next_button(student, start_place_type, start_place_id):
|
|
pass
|
|
|
|
|
|
def get_read_flag(student, course):
|
|
pass
|
|
|
|
|
|
def check_read_flag(student, course):
|
|
pass
|
|
|
|
|
|
def check_bought(token, user):
|
|
# Проверка доступа материала по подписке
|
|
pass
|
|
|
|
|
|
def make_journal(journal):
|
|
course = journal.course
|
|
student = journal.student
|
|
|
|
|
|
def get_user_courses(user):
|
|
# Получить рабочие курсы студента
|
|
return TeacherJ.objects.filter(student=user)
|
|
|
|
|
|
def get_user_diploms(user):
|
|
# Получить дипломы пользователя
|
|
return DiplomaJ.objects.filter(student=user).exclude(Q(in_image=None)|Q(key=None)).only('material', 'date')
|
|
|
|
|
|
def get_user_achievements(user):
|
|
# Получить все ачивки пользователя
|
|
themes = {}
|
|
for i in AchievementJ.objects.filter(student=user):
|
|
themes[i.group] = []
|
|
|
|
for i in AchievementJ.objects.filter(student=user):
|
|
themes[i.group].append(i)
|
|
|
|
return themes
|
|
|
|
|
|
def get_user_achievements_count(user):
|
|
# Получение количества ачивок пользователя
|
|
return AchievementJ.objects.filter(student=user).count()
|
|
|
|
|
|
def get_user_skills(user):
|
|
# Получить скилы пользователя
|
|
lessons = [i.material for i in LessonJ.objects.filter(student=user, success=True)]
|
|
skills = SkillJ.objects.filter(lesson__in=lessons).only('size', 'skill')
|
|
result = {}
|
|
for i in skills:
|
|
if i.skill.title in result:
|
|
result[i.skill.title]['size'] += i.size
|
|
else:
|
|
result[i.skill.title] = {
|
|
'color': i.skill.color,
|
|
'size': i.size,
|
|
'icon': i.skill.mini_icon
|
|
}
|
|
return result
|
|
|
|
|
|
def get_user_completed_courses_count(user):
|
|
return ExamJ.objects.filter(student=user, success=True).count()
|
|
|
|
|
|
def get_user_skills_size_count(user):
|
|
# Посчитать размер навыка
|
|
result = 0
|
|
for key, value in get_user_skills(user).items():
|
|
result += value['size']
|
|
return result
|
|
|
|
|
|
def set_achievement(user, title, text, group, achievement_name):
|
|
# Просвоить ачивку
|
|
achievement = Achievements.objects.get(tech_name=achievement_name)
|
|
AchievementJ.objects.create(student=user, title=title, text=text, group=group, achievement=achievement)
|
|
|
|
### Методы над курсом
|
|
def get_students_on_homework_length(course):
|
|
l = 0
|
|
for i in CourseTheme.objects.filter(course=course):
|
|
l += i.get_active_homework_students_length()
|
|
return l
|
|
|
|
|
|
def get_active_exam_students(course):
|
|
result = [i.student for i in ExamJ.objects.filter(exam__course=course, status='T').exclude(success=True)]
|
|
return result
|
|
|
|
|
|
def get_active_exam_students_length(course):
|
|
result = ExamJ.objects.filter(exam__course=course, status='T').count()
|
|
return result
|
|
|
|
|
|
def exam_status_html(course, student):
|
|
status = course.get_exam_status(student)
|
|
data = u'<span class="label font-normal text-white bg-{0}" style="margin-left: 10px;">{1}</span>'
|
|
if status == 'B':
|
|
return data.format(u'primary', u'Пока недоступно')
|
|
elif status == 'P':
|
|
return data.format(u'warning', u'Достуно для выполнения')
|
|
elif status == 'T':
|
|
return data.format(u'info', u'Проверяется преподавателем')
|
|
elif status == 'E':
|
|
return data.format(u'danger', u'Требуется доработка')
|
|
elif status == 'F':
|
|
return data.format(u'success', u'Сдано')
|
|
|
|
|
|
def get_exam_status(course, student):
|
|
try:
|
|
exam = Exam.objects.get(course=course)
|
|
except Exam.DoesNotExist:
|
|
return 'B'
|
|
else:
|
|
return exam.get_status(student)
|
|
|
|
|
|
def check_course_finish(course, student):
|
|
if LessonJ.objects.filter(lesson__course=course, student=student, status='F').count() == Lesson.objects.filter(
|
|
course=course).count() \
|
|
and HomeworkJ.objects.filter(homework__course=course, student=student,
|
|
status='F').count() == Homework.objects.filter(course=course).count() \
|
|
and ExamJ.objects.filter(exam__course=course, student=student, status='F').count() == Exam.objects.filter(
|
|
course=course).count():
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
### Методы над темой
|
|
def get_theme_no_saw_lessons_length(__theme):
|
|
result = 0
|
|
for n in __theme.get_lessons():
|
|
result += n.get_no_saw_lessons_length()
|
|
|
|
return result
|
|
|
|
|
|
def get_theme_description(theme, student):
|
|
result = ''
|
|
if theme.description:
|
|
result += '<p style="padding:10px;">%s</p>' % theme.description
|
|
|
|
if theme.lessons_length() > 1:
|
|
result += u'<ul style="padding: 0 0 0 15px;">'
|
|
for i in Lesson.objects.filter(course=theme.course, theme=theme).order_by('sort'):
|
|
result += i.get_description(student)
|
|
result += u'</ul>'
|
|
return result
|
|
|
|
if not theme.description and theme.lessons_length() == 1:
|
|
return Lesson.objects.get(theme=theme).description
|
|
|
|
|
|
def get_active_homework_students(theme):
|
|
result = []
|
|
if theme.homework_exist():
|
|
for i in HomeworkJ.objects.filter(homework=theme.get_homework(), status='T'):
|
|
result.append(i.student)
|
|
return result
|
|
|
|
|
|
def get_active_homework_students_length(theme):
|
|
if theme.homework_exist():
|
|
return HomeworkJ.objects.filter(homework=self.get_homework(), status='T').count()
|
|
else:
|
|
return 0
|
|
|
|
|
|
def get_actual_theme_lesson_id(theme, student):
|
|
# Получение читаемого урока
|
|
if student:
|
|
try:
|
|
if student.is_authenticated() and student.is_admin:
|
|
return Lesson.objects.get(theme=theme, sort='1').id
|
|
except:
|
|
pass
|
|
|
|
status = theme.get_status(student)
|
|
if status == 'R':
|
|
if LessonJ.objects.filter(status='R', lesson__theme=theme, student=student).exists():
|
|
return LessonJ.objects.filter(status='R', lesson__theme=theme, student=student).first().lesson.id
|
|
else:
|
|
if theme.homework_status(student) in ['R', 'E', 'T', 'A']:
|
|
return Lesson.objects.filter(theme=theme).order_by('sort').last().id
|
|
else:
|
|
return Lesson.objects.filter(theme=theme, free=True).order_by('sort').first().id
|
|
|
|
elif status == 'F':
|
|
return Lesson.objects.filter(theme=theme).order_by('sort').first().id
|
|
|
|
else:
|
|
if Lesson.objects.filter(theme=theme, free=True).exists():
|
|
return Lesson.objects.filter(theme=theme, free=True).order_by('sort').first().id
|
|
else:
|
|
return False
|
|
|
|
|
|
def get_one_lesson_id(theme):
|
|
if theme.lessons_length() == 1:
|
|
return Lesson.objects.filter(course=theme.course, theme=theme).first().id
|
|
else:
|
|
return '0'
|
|
|
|
|
|
def homework_status_html(theme, student):
|
|
status = theme.homework_status(student)
|
|
data = u'<span class="label font-normal text-white bg-{0}" style="margin-left: 10px;">{1}</span>'
|
|
if status == 'B':
|
|
return data.format(u'primary', u'Пока недоступно')
|
|
elif status == 'A':
|
|
return data.format(u'warning', u'Достуно для выполнения')
|
|
elif status == 'T':
|
|
return data.format(u'info', u'Проверяется преподавателем')
|
|
elif status == 'E':
|
|
return data.format(u'danger', u'Требуется доработка')
|
|
elif status == 'F':
|
|
return data.format(u'success', u'Сдано')
|
|
|
|
|
|
def homework_status(theme, student):
|
|
if Homework.objects.filter(theme=theme).exists():
|
|
try:
|
|
homework = HomeworkJ.objects.get(student=student, homework=Homework.objects.get(theme=theme))
|
|
except HomeworkJ.DoesNotExist:
|
|
return 'B'
|
|
else:
|
|
return homework.status
|
|
else:
|
|
return 'B'
|
|
|
|
|
|
def check_theme_finish(theme, student):
|
|
# Проверка, прошел ли пользователь тему по урокам
|
|
if LessonJ.objects.filter(student=student, status='F', lesson__theme=theme).count() == theme.lessons_length():
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
def check_study_access(theme, student):
|
|
if LessonJ.objects.filter(Q(student=student, status='R', lesson__theme=theme) | Q(student=student, status='F',
|
|
lesson__theme=theme)).exists():
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
def get_status(theme, student):
|
|
# 1) Читаемая тема
|
|
# 2) ДЗ на прохождении
|
|
# 3) Укомплектовано
|
|
if student:
|
|
if LessonJ.objects.filter(student=student, lesson__in=Lesson.objects.filter(theme=theme),
|
|
status='R').exists():
|
|
return 'R'
|
|
|
|
elif Lesson.objects.filter(free=True, theme=theme).exists() \
|
|
and not LessonJ.objects.filter(student=student, lesson__in=Lesson.objects.filter(theme=theme,
|
|
free=True)):
|
|
return 'R'
|
|
|
|
elif theme.homework_status(student) != 'B':
|
|
h_status = theme.homework_status(student)
|
|
if h_status == 'F':
|
|
return 'F'
|
|
|
|
else:
|
|
return 'R'
|
|
|
|
elif LessonJ.objects.filter(student=student, lesson__theme=theme,
|
|
status='F').count() == Lesson.objects.filter(theme=theme).count():
|
|
return 'F'
|
|
|
|
else:
|
|
return 'N'
|
|
else:
|
|
if theme.sort == 1 and Lesson.objects.filter(free=True, theme=theme).exists():
|
|
return 'R'
|
|
else:
|
|
return 'N'
|
|
|
|
|
|
def get_theme_color(theme, student):
|
|
# Получить тип
|
|
# Если тип премиум или пополнение
|
|
status = theme.get_status(student)
|
|
if status == 'F':
|
|
return 'b-success'
|
|
elif status == 'N':
|
|
return 'b-default'
|
|
else:
|
|
return 'b-info'
|
|
|
|
|
|
def get_theme_skills(__theme):
|
|
result = []
|
|
for i in SkillJ.objects.filter(lesson__in=__theme.get_lessons()):
|
|
if i.skill not in result:
|
|
result.append(i.skill)
|
|
return result
|
|
|
|
|
|
def have_skills(self):
|
|
return SkillJ.objects.filter(lesson__in=self.get_lessons()).exists()
|
|
|
|
|
|
### Методы над уроком
|
|
def get_lesson_description(lesson, student):
|
|
# 1 - id
|
|
# 2 - course_id
|
|
# 3 - title
|
|
# 4 - status
|
|
# 5 - free
|
|
button = u'<li style="margin-bottom:5px;">' \
|
|
u'<a href="/courses/lesson/{0}" by_type="lesson" self_id="{0}" course_id="{1}" ' \
|
|
u'style="color:#333; border-bottom:1px dotted #333; color: #333;">{2}</a> ' \
|
|
u'{3} ' \
|
|
u'<span class="label label-success" style="margin-left: 10px;">{4}</span>' \
|
|
u'</li>'
|
|
# 0 - title
|
|
# 1 - status
|
|
# 2 - free
|
|
button2 = u'<li style="margin-bottom:5px;">' \
|
|
u'{0} ' \
|
|
u'{1} ' \
|
|
u'<span class="label label-success" style="margin-left: 10px;">{2}</span>' \
|
|
u'</li>'
|
|
|
|
if student and student.is_authenticated() and student.is_admin:
|
|
return button.format(lesson.id, lesson.course.id,
|
|
u'<b>ID: %s</b> %s' % (lesson.id, lesson.title) if student.is_admin else lesson.title,
|
|
'<i class="glyphicon glyphicon-time"></i>', '')
|
|
|
|
elif lesson.get_status(student) != 'N':
|
|
return button.format(lesson.id, lesson.course.id, lesson.title, lesson.get_status_html(student), '')
|
|
|
|
else:
|
|
if lesson.sort == 1 and lesson.free:
|
|
return button.format(lesson.id, lesson.course.id, lesson.title, '<i class="glyphicon glyphicon-time"></i>',
|
|
'')
|
|
else:
|
|
return button2.format(lesson.title, lesson.get_status_html(student), '')
|
|
|
|
# if lesson.get_status(student) != 'N':
|
|
# return button.format(lesson.id, lesson.course.id, lesson.title, lesson.get_status_html(student),
|
|
# u'Бесплатный урок' if lesson.free else '')
|
|
|
|
# else:
|
|
# if lesson.sort == 1 and lesson.free:
|
|
# return button.format(lesson.id, lesson.course.id, lesson.title,
|
|
# '<i class="glyphicon glyphicon-time"></i>',
|
|
# u'Бесплатный урок' if lesson.free else '')
|
|
# else:
|
|
# return button2.format(lesson.title, lesson.get_status_html(student),
|
|
# u'Бесплатный урок' if lesson.free else '')
|
|
|
|
|
|
def get_no_saw_lessons_length(lesson):
|
|
return lesson.comments.filter(saw=False).count()
|
|
|
|
|
|
def get_status_html(lesson, student):
|
|
status = lesson.get_status(student)
|
|
if status == 'N':
|
|
return u'<i class="glyphicon glyphicon-remove"></i>'
|
|
elif status == 'F':
|
|
return u'<i class="glyphicon glyphicon-ok"></i>'
|
|
elif status == 'R':
|
|
return u'<i class="glyphicon glyphicon-time"></i>'
|
|
|
|
|
|
def get_lesson_skills(lesson):
|
|
return SkillJ.objects.filter(lesson=lesson)
|
|
|
|
|
|
def get_comments_length(lesson):
|
|
return lesson.comments.filter(public=True).count()
|
|
|
|
|
|
def get_lesson_status(lesson, student):
|
|
try:
|
|
return LessonJ.objects.get(student=student, lesson=lesson).status
|
|
except LessonJ.DoesNotExist:
|
|
return 'N'
|
|
|
|
|
|
def get_status_name(lesson, student):
|
|
try:
|
|
return LessonJ.objects.get(student=student, lesson=lesson).get_status_display()
|
|
except LessonJ.DoesNotExist:
|
|
return u'Не доступно'
|
|
|
|
|
|
### Методы над ДЗ
|
|
def get_homework_status(__homework, student):
|
|
try:
|
|
homework = HomeworkJ.objects.get(student=student, homework=__homework)
|
|
except HomeworkJ.DoesNotExist:
|
|
return 'B'
|
|
else:
|
|
return homework.status
|
|
|
|
|
|
### Методы экзамена
|
|
|
|
def get_exam_color(exam, student):
|
|
# Получить тип
|
|
# Если тип премиум или пополнение
|
|
status = exam.get_status(student)
|
|
if status == 'P':
|
|
return 'b-info'
|
|
elif status == 'T':
|
|
return 'b-primary'
|
|
elif status == 'F':
|
|
return 'b-success'
|
|
elif status == 'E':
|
|
return 'b-danger'
|
|
else:
|
|
return 'b-default'
|
|
|
|
|