4

I am trying to develop a Moodle Android app. I am using MoodleREST source code for my reference. But rest code to upload assignment is not provided by this library. I want to be able to upload assignment from mobile client with a webservice call. Uploading assignment using a webview is possible but in that case user need to login again to access upload assignment page.

I have found something similar here https://moodle.org/mod/forum/discuss.php?d=207875.

I am new to moodle and still learning it, so my question can be a little naive so please bear with it :)

Harshawardhan
  • 1,521
  • 1
  • 24
  • 29

3 Answers3

1

It is kind of possible to upload a submission with a file to the assignment using Moodle webservices.

First upload a file to draft using core_files_upload

http://my-moodle-url/moodle/webservice/rest/server.php?wstoken=token_value_xyz&moodlewsrestformat=json&wsfunction=core_files_upload&component=user&filearea=draft&itemid=0&filepath=/&filename=test2.txt&filecontent=TWFuIGlzIGRpc3Rpbmd1aXNoZWQ=&contextlevel=user&instanceid=8

where:
itemid=0 - moodle will generate and return an itemid or you set itemid
filecontent - base64 encoded file contents
instanceid - userId whose is webservices token

Sample response:

{
    "contextid": 26,
    "component": "user",
    "filearea": "draft",
    "itemid": 293005570,
    "filepath": "/",
    "filename": "test3.txt",
    "url": "http://my-moodle-url/moodle/draftfile.php/26/user/draft/293005570/test3.txt"
}

You can search for an assignment id for the next call with mod_assign_get_assignments

Then use itemid received, here "293005570", in mod_assign_save_submission

http://my-moodle-url/moodle/webservice/rest/server.php?wstoken=token_value_xyz&moodlewsrestformat=json&wsfunction=mod_assign_save_submission&assignmentid=5&plugindata[onlinetext_editor][text] =some_text_here&plugindata[onlinetext_editor][format] =1&plugindata[onlinetext_editor][itemid]=521767865&plugindata[files_filemanager]=521767865

This will add an assignment submission with this file.

The problem I could core_files_upload and mod_assign_save_submission only using a webservices token for a particular user, i.e. each user needs a webservices token which might be not practical. With a webservices user token I get on the first call:

{
    "exception": "moodle_exception",
    "errorcode": "nofile",
    "message": "File not specified"
}

Tested with Postman. This might be related: https://tracker.moodle.org/browse/MDL-61276

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
  • The issue with this is it removes any existing files that have already been uploaded. Is there any way to fix this? ([Full question](https://stackoverflow.com/questions/70936430/how-to-get-the-file-manager-item-id-of-an-assignment-in-moodle-using-the-web-ser)) – Sujit Feb 01 '22 at 07:11
0

Doesnt look like there is existing solution for this in moodle web services. Moodle actually encodes files in base64 which creates burden on mobile devices. Mobile devices dont have that much memory to encode big files. Closet solution published by Moodle HQ (and otherwise) is this : https://github.com/moodlehq/sample-ws-clients/blob/master/PHP-HTTP-filehandling/client.php which saves file as private file and not as assignment. You may have to modify substantially the plugin.

iankit
  • 8,806
  • 10
  • 50
  • 56
0

To upload files I'm using this API with a POST method

https://{YOUR_URL}/webservice/upload.php?moodlewsrestformat=json&wstoken={WSTOKEN}

And you must pass the following parameters as FormData

  • file => File // your file
  • token => Int // same user's wstoken
  • filearea => String // draft, private... etc
  • itemid => Int // set to 0 to create a new file