|
| 1 | +""" |
| 2 | +Instructor tasks related to components and xblocks in the course. |
| 3 | +""" |
| 4 | + |
| 5 | +from datetime import datetime |
| 6 | +from time import time |
| 7 | +from pytz import UTC |
| 8 | +from lms.djangoapps.instructor_analytics.csvs import format_dictlist |
| 9 | +from openedx.core.djangoapps.content.course_overviews.models import CourseOverview |
| 10 | +from xmodule.modulestore.django import modulestore |
| 11 | + |
| 12 | +from .runner import TaskProgress |
| 13 | +from .utils import upload_csv_to_report_store |
| 14 | + |
| 15 | + |
| 16 | +def upload_xblock_list_csv( |
| 17 | + _xblock_instance_args, _entry_id, course_id, task_input, action_name |
| 18 | +): |
| 19 | + """ |
| 20 | + generate a csv containing all xblocks in all courses |
| 21 | + """ |
| 22 | + start_time = time() |
| 23 | + start_date = datetime.now(UTC) |
| 24 | + |
| 25 | + overviews = CourseOverview.objects.filter(id=course_id).order_by("id") |
| 26 | + # NOTE: if we want to report on all courses at once, use this line below instead of the one above |
| 27 | + # overviews = CourseOverview.objects.all().order_by('id') |
| 28 | + |
| 29 | + total_courses = overviews.count() |
| 30 | + task_progress = TaskProgress(action_name, total_courses, start_time) |
| 31 | + |
| 32 | + current_step = {"step": "Calculating XBlock Info"} |
| 33 | + task_progress.update_task_state(extra_meta=current_step) |
| 34 | + |
| 35 | + data = [] |
| 36 | + succeeded_count = 0 |
| 37 | + for overview in overviews: |
| 38 | + try: |
| 39 | + course = modulestore().get_course(overview.id) |
| 40 | + data.extend( |
| 41 | + { |
| 42 | + "Course ID": course.id, |
| 43 | + "Course Name": course.display_name, |
| 44 | + "Section Name": section.display_name, |
| 45 | + "Subsection Name": subsection.display_name, |
| 46 | + "Unit Name": unit.display_name, |
| 47 | + "Component Name": component.display_name, |
| 48 | + "Xblock Type": component.location.block_type, |
| 49 | + } |
| 50 | + for section in course.get_children() |
| 51 | + for subsection in section.get_children() |
| 52 | + for unit in subsection.get_children() |
| 53 | + for component in unit.get_children() |
| 54 | + ) |
| 55 | + succeeded_count += 1 |
| 56 | + except: # pylint: disable=bare-except |
| 57 | + print(f"FAILED GETTING COURSE {overview.id} FROM MODULESTORE") |
| 58 | + |
| 59 | + header, rows = format_dictlist( |
| 60 | + data, |
| 61 | + [ |
| 62 | + "Course ID", |
| 63 | + "Course Name", |
| 64 | + "Section Name", |
| 65 | + "Subsection Name", |
| 66 | + "Unit Name", |
| 67 | + "Component Name", |
| 68 | + "Xblock Type", |
| 69 | + ], |
| 70 | + ) |
| 71 | + |
| 72 | + task_progress.attempted = total_courses |
| 73 | + task_progress.succeeded = succeeded_count |
| 74 | + task_progress.skipped = task_progress.failed = total_courses - succeeded_count |
| 75 | + |
| 76 | + rows.insert(0, header) |
| 77 | + |
| 78 | + current_step = {"step": "Uploading CSV"} |
| 79 | + task_progress.update_task_state(extra_meta=current_step) |
| 80 | + |
| 81 | + # Perform the upload |
| 82 | + upload_parent_dir = task_input.get("upload_parent_dir", "") |
| 83 | + upload_filename = task_input.get("filename", "xblocks_list") |
| 84 | + upload_csv_to_report_store( |
| 85 | + rows, upload_filename, course_id, start_date, parent_dir=upload_parent_dir |
| 86 | + ) |
| 87 | + |
| 88 | + return task_progress.update_task_state(extra_meta=current_step) |
0 commit comments