Skip to content

Commit

Permalink
Add Bash client
Browse files Browse the repository at this point in the history
  • Loading branch information
knuesel authored and lucaboesch committed Apr 17, 2020
1 parent 65a25f0 commit f5eb1d0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ The local_ws_fileassistant Moodle web service helps accomplishing the task to pu
Moodle course.

Since this functionality is missing but would be greatly appreciated by lecturers who want to create their own scripts to push their
lecture materials to their courses using the command line, here's a way to make this possible. Note this does not provide the
CLI commands, it does, however, allow files to be pushed into a section of a course.
lecture materials to their courses using the command line, here's a way to make this possible.

See below for an example cURL command. More featurful clients are available in the `clients/` subdirectory.

![Animation of the process](docs/local_ws_fileassistant.gif)

Expand Down
21 changes: 21 additions & 0 deletions clients/Bash/README.md
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"
```
10 changes: 10 additions & 0 deletions clients/Bash/moodle.config
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
54 changes: 54 additions & 0 deletions clients/Bash/upload2section.sh
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}&sectionnumber=${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

0 comments on commit f5eb1d0

Please sign in to comment.