Moodle API Client¶
REST API client for Moodle Web Services.
mograder.transport.moodle_api
¶
Moodle REST Web Services API client for mograder.
MoodleAPIError
¶
Bases: Exception
Raised when a Moodle API call returns an error.
Source code in src/mograder/transport/moodle_api.py
MoodleAPIClient
¶
Client for Moodle's REST Web Services API.
Source code in src/mograder/transport/moodle_api.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
get_site_info()
¶
Get site info for the authenticated user.
Returns dict with: userid, username, fullname, sitename.
Source code in src/mograder/transport/moodle_api.py
get_assignments(course_id)
¶
Get assignments for a course.
Returns a flat list of assignment dicts with keys: id, cmid, name, duedate, intro, introattachments.
Source code in src/mograder/transport/moodle_api.py
download_file(file_url, dest)
¶
Download a file from Moodle, appending the token for auth.
Automatically converts pluginfile.php URLs to
webservice/pluginfile.php so token authentication works.
Source code in src/mograder/transport/moodle_api.py
upload_file(filepath, itemid=0)
¶
Upload a file to the user's draft area.
When itemid is 0 (default), a new draft area is created. Pass a non-zero itemid to append to an existing draft area.
Returns the draft area itemid.
Source code in src/mograder/transport/moodle_api.py
upload_files_to_draft(filepaths)
¶
Upload multiple files to a single draft area.
Returns the draft area itemid.
Source code in src/mograder/transport/moodle_api.py
update_introattachments(cmid, draft_itemid)
¶
Attach a draft area to an assignment's introattachments.
Uses core_course_edit_module to update the assignment module.
Raises MoodleAPIError if the API call fails (e.g. insufficient
permissions).
Source code in src/mograder/transport/moodle_api.py
update_intro(cmid, intro_html)
¶
Update the assignment description (intro) field.
Uses core_course_edit_module to replace the intro HTML.
Source code in src/mograder/transport/moodle_api.py
save_submission(assignment_id, item_id)
¶
Save a file submission draft for an assignment.
Source code in src/mograder/transport/moodle_api.py
submit_for_grading(assignment_id)
¶
Finalize a submission so it's visible to graders.
get_submission_status(assignment_id)
¶
Get the current user's submission status, grade, and feedback.
Returns dict with keys: status, graded, grade, feedback.
Source code in src/mograder/transport/moodle_api.py
get_submissions(assignment_id)
¶
Get all submissions for an assignment.
Returns list of dicts with keys: userid, status, files. Each file has: filename, fileurl, filesize.
Source code in src/mograder/transport/moodle_api.py
list_participants(assignment_id)
¶
List participants for an assignment.
Returns list of {id, username, fullname}.
Source code in src/mograder/transport/moodle_api.py
save_grades(assignment_id, grades, workflow_state='')
¶
Save grades for multiple students.
Each grade dict should have: userid, grade, feedback (text). Uses the singular save_grade API for broader token compatibility.
Source code in src/mograder/transport/moodle_api.py
resolve_credentials(cli_url, cli_token, config)
¶
Resolve Moodle URL and token from CLI flags, env vars, or config.
Priority: CLI flag > environment variable > config file.
Source code in src/mograder/transport/moodle_api.py
find_assignment(client, course_id, name)
¶
Find an assignment by name within a course.
Tries exact match first, then case-insensitive substring. Raises click.UsageError if no match or ambiguous.
Source code in src/mograder/transport/moodle_api.py
sync_assignments(client, course_id, *, include_pattern=None)
¶
Fetch visible assignments from Moodle and return config-ready dicts.
Each dict has keys: name, id, cmid, duedate, files.
File URLs use pluginfile.php (browser-accessible); callers using
token auth should go through MoodleAPIClient.download_file which
converts automatically.
Source code in src/mograder/transport/moodle_api.py
request_token(url, username, password, service='moodle_mobile_app')
¶
Exchange username/password for a web service token via /login/token.php.
Returns the token string. Raises MoodleAPIError on failure.
Source code in src/mograder/transport/moodle_api.py
build_sso_login_url(url, passport, service='moodle_mobile_app')
¶
Build the SSO launch URL for browser-based token acquisition.
The user visits this URL, logs in via SSO, and is redirected to
moodlemobile://token=<base64_encoded_data>.
Source code in src/mograder/transport/moodle_api.py
extract_token_from_sso_url(url_string)
¶
Extract the web service token from a moodlemobile://token=... redirect URL.
The token payload is base64-encoded as siteid:::token or
siteid:::token:::privatetoken. Returns the token (second field).
Raises MoodleAPIError if the URL cannot be parsed.
Source code in src/mograder/transport/moodle_api.py
load_cached_token(url)
¶
save_cached_token(url, token, fullname)
¶
Persist a token to ~/.config/mograder/token.json.