예제 #1
0
        private async Task TryInitialExportCourse(Course course, CourseInitialExportOptions exportOptions, CourseExportResults results)
        {
            var stepikCourse = await client.GetCourse(exportOptions.StepikCourseId).ConfigureAwait(false);

            results.StepikCourseTitle = stepikCourse.Title;
            await ClearCourse(stepikCourse).ConfigureAwait(false);

            var unitIndex = 0;

            foreach (var unit in course.Units)
            {
                results.Info($"Converting ulearn unit «{unit.Title}» into stepik section");
                var section = ConvertUlearnUnitIntoStepikSection(unit);
                section.CourseId = exportOptions.StepikCourseId;
                section.Position = ++unitIndex;
                section          = await client.UploadSection(section).ConfigureAwait(false);

                if (!section.Id.HasValue)
                {
                    throw new StepikApiException($"Didn't receive `section`'s ID from stepik: {section.JsonSerialize()}");
                }

                var currentStepikLessonId = -1;
                var blockIndex            = 0;
                var lessonIndex           = 0;
                var isFirstSlideInUnit    = true;
                foreach (var slide in unit.Slides)
                {
                    results.Info($"Converting ulearn slide «{slide.Title}» with id {slide.Id} into stepik steps");
                    var needToStartNewLesson = isFirstSlideInUnit || exportOptions.SlideIdsWhereNewLessonsStart.Contains(slide.Id);
                    isFirstSlideInUnit = false;
                    if (needToStartNewLesson)
                    {
                        results.Info("Starting new stepik lesson for this slide");
                        var lesson = await client.UploadLesson(new StepikApiLesson
                        {
                            Title = slide.Title
                        }).ConfigureAwait(false);

                        if (!lesson.Id.HasValue)
                        {
                            throw new StepikApiException($"Didn't receive `lesson`'s ID from stepik: {lesson.JsonSerialize()}");
                        }

                        currentStepikLessonId = lesson.Id.Value;

                        await client.UploadUnit(new StepikApiUnit
                        {
                            Position  = ++lessonIndex,
                            SectionId = section.Id.Value,
                            LessonId  = currentStepikLessonId,
                        }).ConfigureAwait(false);

                        blockIndex = 0;
                    }

                    blockIndex += await InsertSlideAsStepsInLesson(course, slide, currentStepikLessonId, blockIndex, exportOptions, results).ConfigureAwait(false);
                }
            }
        }
예제 #2
0
        private async Task TryUpdateCourse(Course course, CourseUpdateOptions updateOptions, CourseExportResults results)
        {
            results.Info($"Downloading stepik course #{updateOptions.StepikCourseId}");
            var stepikCourse = await client.GetCourse(updateOptions.StepikCourseId).ConfigureAwait(false);

            results.StepikCourseTitle = stepikCourse.Title;

            results.Info($"Downloading stepik sections for it ({string.Join(", ", stepikCourse.SectionsIds)})");
            var stepikSections = new Dictionary <int, StepikApiSection>();

            // TODO (andgein): download multiple sections in one request
            foreach (var sectionId in stepikCourse.SectionsIds)
            {
                stepikSections[sectionId] = await client.GetSection(sectionId).ConfigureAwait(false);
            }

            var stepikUnitsIds = stepikSections.SelectMany(kvp => kvp.Value.UnitsIds).ToList();

            results.Info($"Downloading stepik units ({string.Join(", ", stepikUnitsIds)}) and lessons for them");
            var stepikUnits = new Dictionary <int, StepikApiUnit>();

            foreach (var unitId in stepikUnitsIds)
            {
                stepikUnits[unitId] = await client.GetUnit(unitId).ConfigureAwait(false);
            }

            var stepikLessonsIds = stepikUnits.Select(kvp => kvp.Value.LessonId);
            var stepikLessons    = new Dictionary <int, StepikApiLesson>();

            foreach (var lessonId in stepikLessonsIds)
            {
                stepikLessons[lessonId] = await client.GetLesson(lessonId).ConfigureAwait(false);
            }

            var sectionIndex = stepikCourse.SectionsIds.Count;

            foreach (var slideUpdateOptions in updateOptions.SlidesUpdateOptions)
            {
                var slideId = slideUpdateOptions.SlideId;
                var slide   = course.FindSlideById(slideId);
                if (slide == null)
                {
                    results.Error($"Unable to find slide {slideId}, continue without it");
                    continue;
                }
                results.Info($"Updating slide «{slide.Title}» with id {slide.Id}");
                var             stepId = slideUpdateOptions.InsertAfterStep;
                StepikApiLesson lesson;
                int             position;

                if (stepId != -1 && stepikLessons.Values.Any(l => l.StepsIds.Contains(stepId)))
                {
                    var lessonId = stepikLessons.FirstOrDefault(kvp => kvp.Value.StepsIds.Contains(stepId)).Key;
                    // Re-download lesson because it can be changed for a while
                    lesson = await client.GetLesson(lessonId).ConfigureAwait(false);

                    position = lesson.StepsIds.FindIndex(stepId);

                    results.Info($"Removing old steps created for this slide: {string.Join(", ", slideUpdateOptions.RemoveStepsIds)}");
                    await RemoveStepsFromLesson(lesson, slideUpdateOptions.RemoveStepsIds, results).ConfigureAwait(false);
                }
                else
                {
                    results.Info("Creating new stepik lesson for this slide");
                    lesson = await CreateLessonAndSectionForSlide(slide, stepikCourse, ++sectionIndex).ConfigureAwait(false);

                    position = 0;
                }

                results.Info($"Inserting steps for slide «{slide.Title}» into lesson {lesson.Id} on position {position}");
                await InsertSlideAsStepsInLesson(course, slide, lesson.Id.Value, position + 1, updateOptions, results);
            }
        }