Skip to contents

Scope

This guide shows how to prepare a simple Moodle-ready CSV grade file from learnrTrackR. The package does not call the Moodle API. The workflow is a manual CSV import into the Moodle gradebook.

MoodleDocs states that grades can be imported as CSV or XML files, or by pasting from a spreadsheet. MoodleDocs also notes that grade import is equivalent to manual grading in the grader report, so imported grades should be tested carefully before use in a real course.

Create a small gradebook

library(learnrTrackR)

db_path <- tempfile(fileext = ".sqlite")
con <- init_tracking_db(db_path, overwrite = TRUE)

register_students(
  con,
  data.frame(
    student_id = c("student_001", "student_002"),
    student_label = c("Student 1", "Student 2"),
    group_id = c("A", "A")
  )
)
#> # A tibble: 2 × 5
#>   student_id  student_label email group_id created_at          
#>   <chr>       <chr>         <chr> <chr>    <chr>               
#> 1 student_001 Student 1     NA    A        2026-06-24T12:25:38Z
#> 2 student_002 Student 2     NA    A        2026-06-24T12:25:38Z

register_questions(
  con,
  tutorial_id = "module_01",
  questions = data.frame(
    question_id = c("q1", "q2"),
    max_score = c(1, 1)
  )
)
#> # A tibble: 2 × 6
#>   question_id tutorial_id question_label question_type max_score created_at     
#>   <chr>       <chr>       <chr>          <chr>             <dbl> <chr>          
#> 1 q1          module_01   q1             NA                    1 2026-06-24T12:…
#> 2 q2          module_01   q2             NA                    1 2026-06-24T12:…

track_attempt(
  con,
  student_id = "student_001",
  tutorial_id = "module_01",
  question_id = "q1",
  submitted_answer = "mean(x)",
  score = 1,
  max_score = 1,
  require_registered_student = TRUE
)

track_attempt(
  con,
  student_id = "student_002",
  tutorial_id = "module_01",
  question_id = "q1",
  submitted_answer = "sd(x)",
  score = 0,
  max_score = 1,
  require_registered_student = TRUE
)

Build the Moodle table

moodle_grades() returns a wide table with one identifier column and one grade column.

moodle_grades(
  con,
  tutorial_id = "module_01",
  grade_item = "Module 01 quiz"
)
#> # A tibble: 2 × 2
#>   useridnumber `Module 01 quiz`
#>   <chr>                   <dbl>
#> 1 student_001                50
#> 2 student_002                 0

By default, the identifier column is useridnumber, a common Moodle mapping target when institutional student identifiers are stored in Moodle. If your course uses a different field, change id_column.

moodle_grades(
  con,
  tutorial_id = "module_01",
  id_column = "email",
  grade_item = "Module 01 quiz"
)
#> # A tibble: 2 × 2
#>   email       `Module 01 quiz`
#>   <chr>                  <dbl>
#> 1 student_001               50
#> 2 student_002                0

This example still uses student_id values in the exported email column, because the example database does not store real email addresses. In a real course, choose an identifier that matches exactly one Moodle user.

Export the CSV file

csv_path <- tempfile(fileext = ".csv")

export_moodle_grades(
  con,
  path = csv_path,
  tutorial_id = "module_01",
  grade_item = "Module 01 quiz",
  grade_value = "percent",
  digits = 2
)

readr::read_csv(csv_path, show_col_types = FALSE)
#> # A tibble: 2 × 2
#>   useridnumber `Module 01 quiz`
#>   <chr>                   <dbl>
#> 1 student_001                50
#> 2 student_002                 0

Use grade_value = "percent" for a 0 to 100 grade, or grade_value = "score" for the raw gradebook score.

If the database contains several registered groups, use group_id to export only one cohort:

moodle_grades(
  con,
  tutorial_id = "module_01",
  grade_item = "Module 01 quiz",
  group_id = "A"
)
#> # A tibble: 2 × 2
#>   useridnumber `Module 01 quiz`
#>   <chr>                   <dbl>
#> 1 student_001                50
#> 2 student_002                 0

Suggested Moodle import workflow

Before importing into a real course:

  1. Create the grade item in Moodle before importing.
  2. Export a small gradebook sample from Moodle.
  3. Confirm the user identifier field used in your course.
  4. Generate a learnrTrackR CSV with the same identifier field.
  5. Import the CSV in Moodle’s gradebook import interface.
  6. Preview the import and map the user field and grade item columns.
  7. Validate the result on a small test group before importing a full cohort.

Do not add new grade columns in spreadsheet software after exporting from Moodle. MoodleDocs warns that Moodle may reject imports containing columns that did not exist in the exported gradebook. If a new grade item is needed, create it in Moodle first.

Privacy check before import

The Moodle grade CSV should contain only the identifier needed for matching and the grade item column. Do not include attempts, feedback, free-text answers, or pseudonymisation keys in a Moodle import file.

References