Skip to content

Commit 25711a6

Browse files
committed
Enhance documentation for course copying functions in copy_course.go
1 parent 58cde34 commit 25711a6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

servers/core/course/copy_course.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
log "github.com/sirupsen/logrus"
1818
)
1919

20+
// copyCourseInternal creates a deep copy of the given course.
21+
// It copies phases, metadata, DTO mappings, graphs, and the application form if present.
22+
// It also creates course-specific Keycloak roles and groups.
23+
// The function runs within a database transaction.
2024
func copyCourseInternal(ctx context.Context, sourceCourseID uuid.UUID, courseVariables courseDTO.CopyCourseRequest, requesterID string) (courseDTO.Course, error) {
2125
sourceCourse, err := CourseServiceSingleton.queries.GetCourse(ctx, sourceCourseID)
2226
if err != nil {
@@ -115,6 +119,8 @@ func copyCourseInternal(ctx context.Context, sourceCourseID uuid.UUID, courseVar
115119
return courseDTO.GetCourseDTOFromDBModel(createdCourse)
116120
}
117121

122+
// copyCoursePhases duplicates all phases from the source course to the target course.
123+
// It returns a mapping of old phase IDs to new phase IDs.
118124
func copyCoursePhases(ctx context.Context, qtx *db.Queries, sourceID, targetID uuid.UUID) (map[uuid.UUID]uuid.UUID, error) {
119125
sequence, err := qtx.GetCoursePhaseSequence(ctx, sourceID)
120126
if err != nil {
@@ -161,6 +167,8 @@ func copyCoursePhases(ctx context.Context, qtx *db.Queries, sourceID, targetID u
161167
return mapping, nil
162168
}
163169

170+
// copyCoursePhaseGraph replicates the course phase dependency graph from the source course
171+
// to the target course using the provided phase ID mapping.
164172
func copyCoursePhaseGraph(ctx context.Context, qtx *db.Queries, sourceID, targetID uuid.UUID, phaseMap map[uuid.UUID]uuid.UUID) error {
165173
graph, err := qtx.GetCoursePhaseGraph(ctx, sourceID)
166174
if err != nil {
@@ -182,6 +190,8 @@ func copyCoursePhaseGraph(ctx context.Context, qtx *db.Queries, sourceID, target
182190
return nil
183191
}
184192

193+
// setInitialPhase sets the initial course phase in the target course by mapping
194+
// the initial phase from the source course via the provided phase ID mapping.
185195
func setInitialPhase(ctx context.Context, qtx *db.Queries, sourceID, targetID uuid.UUID, phaseMap map[uuid.UUID]uuid.UUID) error {
186196
sequence, err := qtx.GetCoursePhaseSequence(ctx, sourceID)
187197
if err != nil {
@@ -201,6 +211,8 @@ func setInitialPhase(ctx context.Context, qtx *db.Queries, sourceID, targetID uu
201211
return nil
202212
}
203213

214+
// copyDTOs collects all participation DTOs (inputs and outputs) used by the source course phases.
215+
// It returns a map from source DTO IDs to themselves, to support mapping-based operations.
204216
func copyDTOs(ctx context.Context, qtx *db.Queries, sourceID uuid.UUID) (map[uuid.UUID]uuid.UUID, error) {
205217
unordered, err := qtx.GetNotOrderedCoursePhases(ctx, sourceID)
206218
if err != nil {
@@ -244,6 +256,8 @@ func copyDTOs(ctx context.Context, qtx *db.Queries, sourceID uuid.UUID) (map[uui
244256
return dtoIDMap, nil
245257
}
246258

259+
// copyMetaGraphs recreates the phase and participation data graphs for the target course
260+
// using the given mappings for phases and DTOs.
247261
func copyMetaGraphs(ctx context.Context, qtx *db.Queries, sourceID, targetID uuid.UUID, phaseMap, dtoMap map[uuid.UUID]uuid.UUID) error {
248262
// Phase Data Graph
249263
phaseGraph, err := qtx.GetPhaseDataGraph(ctx, sourceID)
@@ -295,6 +309,8 @@ func copyMetaGraphs(ctx context.Context, qtx *db.Queries, sourceID, targetID uui
295309
return updateParticipationDataGraphHelper(ctx, qtx, targetID, converted)
296310
}
297311

312+
// copyApplicationForm copies the application form—including all questions—from
313+
// the source course phase to the target course phase.
298314
func copyApplicationForm(ctx context.Context, qtx *db.Queries, sourceCoursePhaseID, targetCoursePhaseID uuid.UUID) error {
299315
applicationForm, err := getApplicationFormHelper(ctx, qtx, sourceCoursePhaseID)
300316
if err != nil {
@@ -352,6 +368,8 @@ func copyApplicationForm(ctx context.Context, qtx *db.Queries, sourceCoursePhase
352368
return nil
353369
}
354370

371+
// updateParticipationDataGraphHelper deletes and recreates all participation data graph connections
372+
// for the given course using the provided metadata graph items.
355373
func updateParticipationDataGraphHelper(ctx context.Context, qtx *db.Queries, courseID uuid.UUID, graphUpdate []courseDTO.MetaDataGraphItem) error {
356374
// delete all previous connections
357375
err := qtx.DeleteParticipationDataGraphConnections(ctx, courseID)
@@ -376,6 +394,8 @@ func updateParticipationDataGraphHelper(ctx context.Context, qtx *db.Queries, co
376394

377395
}
378396

397+
// getApplicationPhaseID returns the ID of the application phase for the given course.
398+
// If no application phase exists, it returns uuid.Nil and pgx.ErrNoRows.
379399
func getApplicationPhaseID(ctx context.Context, qtx *db.Queries, courseID uuid.UUID) (uuid.UUID, error) {
380400
applicationPhaseID, err := qtx.GetApplicationPhaseIDForCourse(ctx, courseID)
381401
if err != nil {
@@ -384,6 +404,8 @@ func getApplicationPhaseID(ctx context.Context, qtx *db.Queries, courseID uuid.U
384404
return applicationPhaseID, nil
385405
}
386406

407+
// updatePhaseDataGraphHelper deletes and recreates all phase data graph connections
408+
// for the given course using the provided metadata graph items.
387409
func updatePhaseDataGraphHelper(ctx context.Context, qtx *db.Queries, courseID uuid.UUID, graphUpdate []courseDTO.MetaDataGraphItem) error {
388410
// delete all previous connections
389411
err := qtx.DeletePhaseDataGraphConnections(ctx, courseID)
@@ -408,6 +430,8 @@ func updatePhaseDataGraphHelper(ctx context.Context, qtx *db.Queries, courseID u
408430

409431
}
410432

433+
// updateApplicationFormHelper applies updates to a course phase's application form.
434+
// It handles creation, deletion, and updating of text and multi-select questions.
411435
func updateApplicationFormHelper(ctx context.Context, qtx *db.Queries, coursePhaseId uuid.UUID, form applicationDTO.UpdateForm) error {
412436
// Check if course phase is application phase
413437
isApplicationPhase, err := qtx.CheckIfCoursePhaseIsApplicationPhase(ctx, coursePhaseId)
@@ -486,6 +510,8 @@ func updateApplicationFormHelper(ctx context.Context, qtx *db.Queries, coursePha
486510
return nil
487511
}
488512

513+
// getApplicationFormHelper retrieves the application form for the given course phase,
514+
// including all associated questions. Returns an error if the phase is not an application phase.
489515
func getApplicationFormHelper(ctx context.Context, qtx *db.Queries, coursePhaseID uuid.UUID) (applicationDTO.Form, error) {
490516
ctxWithTimeout, cancel := db.GetTimeoutContext(ctx)
491517
defer cancel()

0 commit comments

Comments
 (0)