RPA library for Moodle automation with Robot Framework
Project description
RPA Moodle
Robot Framework library for Moodle LMS automation via Web Services API.
Installation
pip install rpa-moodle
Features
- Course Management: Create, retrieve, and manage Moodle courses
- Category Management: Organize courses into categories
- Quiz Generation: Create quizzes from Excel files or Google Docs (GIFT/XML format)
- User Management: Create users and enroll them in courses
- Excel Integration: Import course and quiz data from Excel
- Google Docs Integration (Optional): Read quiz questions directly from Google Docs
- Robot Framework Support: Full integration with Robot Framework
Quick Start
Python
from RPA.Moodle import MoodleLibrary
# Method 1: Initialize with credentials
moodle = MoodleLibrary(
base_url="https://your-moodle-site.com",
token="your_web_service_token"
)
# Method 2: Initialize without credentials, setup from token file
moodle = MoodleLibrary()
moodle.setup_moodle_connection("moodle_token.json")
# Create a course
course = moodle.create_course(
fullname="Python Programming 101",
shortname="PYTHON101",
categoryid=1,
summary="Learn Python from scratch"
)
# Create quiz from Excel
quiz_file = moodle.create_quiz_from_excel("quiz_data.xlsx", "gift")
Robot Framework
*** Settings ***
# Method 1: Initialize with credentials
Library RPA.Moodle https://your-moodle-site.com your_token
# Method 2: Initialize without credentials
Library RPA.Moodle
*** Test Cases ***
Setup Connection From File
# If initialized without credentials
Set Up Moodle Connection moodle_token.json
Create Course
${course}= Create Course
... fullname=Python 101
... shortname=PY101
... categoryid=1
Log Course ID: ${course['id']}
Create Quiz From Excel
${quiz_file}= Create Quiz From Excel quiz_data.xlsx gift
Log Quiz file created: ${quiz_file}
Token File Format
If using Set Up Moodle Connection, create a JSON file with your Moodle credentials:
moodle_token.json:
{
"base_url": "https://your-moodle-site.com",
"token": "your_moodle_web_service_token"
}
Excel Format
The library expects Excel files with specific sheets:
Sheet: Course
| fullname | shortname | category | summary |
|---|---|---|---|
| Python Programming 101 | PYTHON101 | Programming | Learn Python basics |
Sheet: Quiz
| name | intro | timelimit | grade |
|---|---|---|---|
| Python Quiz | Test your knowledge | 3600 | 100 |
Sheet: Questions
| question_number | question_text | question_type | option_a | option_b | option_c | option_d | correct_answer | points |
|---|---|---|---|---|---|---|---|---|
| 1 | What is Python? | multichoice | A programming language | A snake | A framework | An OS | A | 1 |
| 2 | Python is compiled | truefalse | FALSE | 1 |
Question Types:
multichoice: Multiple choice (A, B, C, D)truefalse: True/False questionsshortanswer: Short answer questions
Moodle Setup
1. Enable Web Services
- Go to Site Administration > Advanced features
- Enable Web services
- Save changes
2. Enable REST Protocol
- Go to Site Administration > Plugins > Web services > Manage protocols
- Enable REST protocol
3. Create External Service
- Go to Site Administration > Plugins > Web services > External services
- Click Add
- Add required functions:
core_course_create_coursescore_course_get_courses_by_fieldcore_course_get_categoriescore_course_create_categoriescore_user_create_usersenrol_manual_enrol_users
4. Generate Token
- Go to Site Administration > Plugins > Web services > Manage tokens
- Click Add
- Select user and service
- Copy the generated token
API Reference
Course Management
Create Course
course = moodle.create_course(fullname, shortname, categoryid, summary="")
Get Course
course = moodle.get_course_by_shortname(shortname)
Ensure Course Exists
course = moodle.ensure_course_exists(fullname, shortname, categoryid)
Category Management
Get Categories
categories = moodle.get_course_categories()
Create Category
category = moodle.create_course_category(name, parent=0, description="")
Ensure Category Exists
category = moodle.ensure_category_exists(name, parent=0)
Quiz Management
Create Quiz from Excel
# GIFT format
quiz_file = moodle.create_quiz_from_excel(excel_path, "gift")
# XML format
quiz_file = moodle.create_quiz_from_excel(excel_path, "xml")
Generate Quiz Files
# GIFT format
moodle.generate_quiz_gift_file(questions, output_path)
# XML format
moodle.generate_quiz_xml_file(questions, output_path, quiz_name)
User Management
Create User
user = moodle.create_user(username, password, firstname, lastname, email)
Enroll User
# roleid: 5 = Student, 3 = Teacher
moodle.enroll_user_in_course(userid, courseid, roleid=5)
Complete Workflows
Create Course from Excel
course = moodle.create_course_from_excel(excel_path)
Create Course and Quiz
result = moodle.create_course_and_quiz_from_excel(excel_path, quiz_format="gift")
# Returns: {'course': {...}, 'quiz_file': '...', 'message': '...'}
Google Docs Integration (Optional)
Installation
To use Google Docs integration, install with Google API support:
pip install rpa-moodle[google]
Or install the dependencies manually:
pip install google-auth google-auth-oauthlib google-api-python-client
Setup Google API Credentials
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable Google Drive API and Google Docs API
- Create OAuth 2.0 credentials
- Download the credentials JSON file
Usage
Python
from RPA.Moodle import MoodleLibrary
# Initialize with Google credentials
moodle = MoodleLibrary(
base_url="https://your-moodle-site.com",
token="your_moodle_token",
google_credentials_path="path/to/google_token.json"
)
# Or setup Google connection later
moodle.setup_google_connection("path/to/google_token.json")
# Create quiz from Google Doc
doc_url = "https://docs.google.com/document/d/YOUR_DOC_ID/edit"
quiz_file = moodle.create_quiz_from_google_doc(doc_url, "quiz_output.txt", "gift")
Robot Framework
*** Settings ***
Library RPA.Moodle https://moodle.site.com moodle_token
*** Test Cases ***
Create Quiz From Google Doc
Setup Google Connection /path/to/google_token.json
${quiz_file}= Create Quiz From Google Doc
... https://docs.google.com/document/d/DOC_ID/edit
... quiz_output.txt
... gift
Log Quiz file: ${quiz_file}
Google Doc Format
Your Google Doc should follow this format:
Question 1: What is Python?
A. A programming language
B. A snake
C. A framework
D. An operating system
Question 2: Python is a compiled language
A. True
B. False
---HẾT---
1. A
2. B
Format Rules:
- Questions start with "Question" or "Câu" followed by number
- Options labeled A, B, C, D
- Delimiter
---HẾT---separates questions from answers - Answer key format:
1. A(question number, dot, answer letter)
API Methods
Setup Google Connection
moodle.setup_google_connection(token_file_path)
Read Quiz from Google Doc
questions = moodle.read_quiz_from_google_doc(doc_id, delimiter="---HẾT---")
Create Quiz from Google Doc
quiz_file = moodle.create_quiz_from_google_doc(doc_id_or_url, output_path, format="gift")
Importing Quizzes to Moodle
After generating GIFT or XML files:
- Go to your course in Moodle
- Click More > Question bank > Import
- Select format (GIFT or Moodle XML)
- Upload the generated file
- Click Import
- Create a Quiz activity and add questions from the Question bank
Requirements
- Python 3.8+
- requests >= 2.31.0
- pandas >= 2.0.0
- openpyxl >= 3.1.0
- robotframework >= 6.0.0
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues and questions, please open an issue on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rpa_moodle-1.0.1.tar.gz.
File metadata
- Download URL: rpa_moodle-1.0.1.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bce8c08a14f3e323186997327e416306128db2140c2e7b55dbdbb26caf58ab2d
|
|
| MD5 |
a465b3a0a3e93b4303fdb61520f401db
|
|
| BLAKE2b-256 |
cfa52e07c4ee5ba059b6053cffcbc7b41bda7763063da698023105b3b2b97630
|
File details
Details for the file rpa_moodle-1.0.1-py3-none-any.whl.
File metadata
- Download URL: rpa_moodle-1.0.1-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6a78e2110f6ad9df4f6f6b4aac29c2b30f6402b793aa064ec6615c2db8cd988
|
|
| MD5 |
2bed969a0b1a2a3f2910b117583a30ff
|
|
| BLAKE2b-256 |
f87927893d29d1fb04b41cce11124cdbb8ef0a148e6f360725ec8513c9c02b6c
|