Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FDCC tasks #122

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
91 changes: 91 additions & 0 deletions cms/djangoapps/fdcc_utils/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from datetime import date, timedelta
import logging
import os
import six

from celery.task import task
from celery_utils.logged_task import LoggedTask
from django.contrib.auth.models import User
from django.core.management.base import CommandError

from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from xmodule.modulestore.django import modulestore
from contentstore.views.certificates import CertificateManager, _get_course_and_check_access
from contentstore.views.item import _get_xblock, _save_xblock
from fdcc_utils.utils import get_course, get_sections
from lms.djangoapps.certificates.api import set_cert_generation_enabled

log = logging.getLogger(__name__)


@task(base=LoggedTask)
def configure_fdcc_certificate(course_id=None, cert_is_active=None, self_generation_enabled=None):
log.info(u"Configuring Certificate for course %s - cert_is_active: %s - self_generation_enabled: %s",
course_id, cert_is_active, self_generation_enabled)
Dismissed Show dismissed Hide dismissed

if not cert_is_active and self_generation_enabled is True:
raise CommandError(("Enabling certificate self generation without activating the certificate "
"will not make the certificate available to students."))

try:
course_key = CourseKey.from_string(course_id)
except InvalidKeyError:
raise CommandError(u"Invalid course_key: '%s'." % course_id)

store = modulestore()
if not store.get_course(course_key):
raise CommandError(u"Course with %s key not found." % course_id)

# De-/activate Certificate
fdcc_admin = User.objects.get(username=os.getenv('FDCC_ADMIN_USERNAME'))
stefdworschak marked this conversation as resolved.
Show resolved Hide resolved
course = _get_course_and_check_access(course_key, fdcc_admin)
certificates = CertificateManager.get_certificates(course)
if not certificates:
raise CommandError(u"No certificates found for course: '%s' ." % course_id)

for certificate in certificates:
certificate['is_active'] = cert_is_active is True
break

store.update_item(course, fdcc_admin.id)
is_active, certificates = CertificateManager.is_activated(course)
if not is_active and cert_is_active is True:
raise CommandError(u"There was an error activating the certificate for course '%s'." % course_id)

cert_event_type = 'activated' if cert_is_active else 'deactivated'
CertificateManager.track_event(cert_event_type, {'course_id': six.text_type(course.id)})

# Dis-/enable certificate self generation
set_cert_generation_enabled(course_key, self_generation_enabled)

log.info(u"Configured Certificate for course %s - cert_is_active: %s - self_generation_enabled: %s",
course_id, cert_is_active, self_generation_enabled)
Dismissed Show dismissed Hide dismissed


@task(base=LoggedTask)
def schedule_fdcc_module(course_id=None, only_visible=None):
log.info(u"Configuring schedule for course %s (only visible: %s)", course_id, only_visible)
start_date = date.today()
# If not run on a Monday, make start_date to be the Monday of the current week
if date.today().weekday() != 0:
start_date = start_date - timedelta(days=start_date.weekday())

fdcc_admin = User.objects.get(username=os.getenv('FDCC_ADMIN_USERNAME'))
course_xblock = get_course(course_id)
_save_xblock(fdcc_admin, course_xblock, metadata={
'start': start_date.strftime('%Y-%m-%d') + 'T05:00:00Z',
'end': (start_date + timedelta(days=6)).strftime('%Y-%m-%d') + 'T23:00:00Z'
})
sections = get_sections(course_xblock, (only_visible is True))
for i, section_xblock in enumerate(sections):
day_increment = min(i, 4) # Max 4 days which brings it to Friday
start_time = (start_date + timedelta(days=day_increment)).strftime('%Y-%m-%d') + 'T05:00:00Z'
log.info(u"Setting start time for section '%s' to %s", section_xblock.display_name, start_time)
_save_xblock(fdcc_admin, section_xblock, metadata={'start': start_time})
for lesson in section_xblock.children:
lesson_xblock = _get_xblock(lesson, fdcc_admin)
log.info(u"Setting start time for lesson '%s' to %s", lesson_xblock.display_name, start_time)
_save_xblock(fdcc_admin, lesson_xblock, metadata={'start': start_time})

log.info(u"Configured schedule for course %s (only visible: %s)", course_id, only_visible)
25 changes: 25 additions & 0 deletions cms/djangoapps/fdcc_utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from opaque_keys.edx.keys import CourseKey
from xmodule.modulestore.django import modulestore


def get_children(parent):
if not hasattr(parent, 'children'):
return []
else:
return parent.children


def get_course(course_id):
course_key = CourseKey.from_string(course_id)
store = modulestore()
return store.get_course(course_key, depth=2)


def get_sections(course, only_visible=True):
store = modulestore()
children = [store.get_item(child_usage_key) for child_usage_key in get_children(course)]
if only_visible:
return [c for c in children if not c.visible_to_staff_only and not c.hide_from_toc]

return children

1 change: 1 addition & 0 deletions cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@
'openedx.core.djangoapps.content.learning_sequences.apps.LearningSequencesConfig',

'module_importexport',
'cms.djangoapps.fdcc_utils',
]


Expand Down
Loading