-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65a25f0
commit f5eb1d0
Showing
4 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Bash client | ||
|
||
The Bash script `upload2section.sh` can be used to upload a local file to the | ||
private file area and make it available in a section of a course. | ||
|
||
The script looks for a `moodle.config` file in the current directory. A | ||
template is provided, **make sure to edit it before running the script**. | ||
|
||
## Usage | ||
|
||
To upload the file "test 1.txt" to section 4 of course 22686: | ||
|
||
``` | ||
./upload2section.sh 22686 4 "test 1.txt" | ||
``` | ||
|
||
An additional parameter can be given to specify the display name: | ||
|
||
``` | ||
./upload2section.sh 22686 4 "test 1.txt" "Test file" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Bash variables for Moodle access | ||
|
||
# The hostname of the Moodle server | ||
moodle_host=XXX | ||
|
||
# The mobile web sevice token | ||
moodle_web_service_token=XXX | ||
|
||
# The file resource token | ||
moodle_file_resource_token=XXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Abort on errors, including unset variables | ||
set -eu | ||
|
||
# If wrong number of parameters was given, show usage message and exit | ||
if (($# < 3 || $# > 4)); then | ||
echo "Usage: ${0##*/} <course ID> <section ID> <filename> [<display name>]" >&2 | ||
exit 1 | ||
fi | ||
|
||
# Assign command-line parameters to named variables | ||
course=$1 | ||
section=$2 | ||
filename=$3 | ||
displayname=${4:-$filename} # Use filename if display name not specified | ||
|
||
# Read Moodle configuration, including secret tokens | ||
source moodle.config | ||
|
||
# Upload the file | ||
upload_output=$(curl -sS -F "file_1=@${filename}" "https://${moodle_host}/webservice/upload.php?token=${moodle_web_service_token}") | ||
|
||
# Get uploaded file ID from cURL output | ||
item_pattern='"itemid":([0-9]+)' | ||
if [[ "${upload_output}" =~ $item_pattern ]]; then | ||
uploaded_item=${BASH_REMATCH[1]} | ||
echo "Uploaded \"${filename}\" to item ${uploaded_item}" | ||
else | ||
echo "Unexpected output for upload of file \"${filename}\": ${upload_output}" >&2 | ||
exit 1 | ||
fi | ||
|
||
# Put uploaded file in private file area | ||
private_file_output=$(curl -sS "https://${moodle_host}/webservice/rest/server.php?wstoken=${moodle_web_service_token}&moodlewsrestformat=json&wsfunction=core_user_add_user_private_files&draftid=${uploaded_item}") | ||
if [[ "${private_file_output}" == "null" ]]; then | ||
echo "File placed in private area" | ||
else | ||
echo "Unexpected output when placing file in private area: ${private_file_output}" >&2 | ||
exit 1 | ||
fi | ||
|
||
# Add file in course's section | ||
section_output=$(curl -sS "https://${moodle_host}/webservice/rest/server.php?wstoken=${moodle_file_resource_token}&wsfunction=local_ws_fileassistant_create_file_resource&filename=${filename}&courseid=${course}§ionnumber=${section}&displayname=${displayname}") | ||
|
||
# Get resource ID | ||
resource_pattern='resource id ([0-9]+)\.' | ||
if [[ "${section_output}" =~ $resource_pattern ]]; then | ||
resource=${BASH_REMATCH[1]} | ||
echo "File added to course ${course} section ${section}, resource ${resource} with display name \"${displayname}\"" | ||
else | ||
echo "Unexpected output when adding file to course's section: ${section_output}" >&2 | ||
exit 1 | ||
fi |