简体   繁体   English

使用 REST 网络服务功能在 Moodle 上上传作业

[英]Upload assignment on Moodle using REST webservice function

I am trying to develop a Moodle Android app.我正在尝试开发 Moodle Android 应用程序。 I am using MoodleREST source code for my reference.我使用 MoodleREST 源代码作为参考。 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.可以使用 webview 上传作业,但在这种情况下,用户需要再次登录才能访问上传作业页面。

I have found something similar here https://moodle.org/mod/forum/discuss.php?d=207875 .我在这里发现了类似的东西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 :)我是moodle的新手并且仍在学习它,所以我的问题可能有点幼稚,所以请耐心等待:)

It is kind of possible to upload a submission with a file to the assignment using Moodle webservices.可以使用 Moodle 网络服务将带有文件的提交上传到作业。

First upload a file to draft using core_files_upload首先使用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 itemid=0 -moodle 将生成并返回一个itemid或者您设置 itemid
filecontent - base64 encoded file contents filecontent - base64 编码的文件内容
instanceid - userId whose is webservices token instanceid - 其为 webservices 令牌的用户 ID

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您可以使用mod_assign_get_assignments搜索下一次调用的分配 ID

Then use itemid received, here "293005570", in mod_assign_save_submission然后在mod_assign_save_submission使用接收到的 itemid,这里是“293005570”

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, ie each user needs a webservices token which might be not practical.问题我只能使用特定用户的 webservices 令牌core_files_uploadmod_assign_save_submission ,即每个用户都需要一个 webservices 令牌,这可能不切实际。 With a webservices user token I get on the first call:使用 webservices 用户令牌,我接到了第一个电话:

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

Tested with Postman.用邮递员测试。 This might be related: https://tracker.moodle.org/browse/MDL-61276这可能是相关的: https : //tracker.moodle.org/browse/MDL-61276

Doesnt look like there is existing solution for this in moodle web services.看起来在moodle Web 服务中没有针对此的现有解决方案。 Moodle actually encodes files in base64 which creates burden on mobile devices. Moodle 实际上以 base64 编码文件,这会给移动设备带来负担。 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. Moodle HQ(和其他)发布的壁橱解决方案是这样的: https : //github.com/moodlehq/sample-ws-clients/blob/master/PHP-HTTP-filehandling/client.php将文件保存为私有文件而不是作为任务。 You may have to modify substantially the plugin.您可能需要对插件进行大量修改。

To upload files I'm using this API with a POST method要上传文件,我使用此 API 和 POST 方法

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

And you must pass the following parameters as FormData并且您必须将以下参数作为 FormData 传递

  • file => File // your file file => File // 你的文件
  • token => Int // same user's wstoken token => Int // 同一个用户的 wstoken
  • filearea => String // draft, private... etc filearea => String // 草稿、私有...等
  • itemid => Int // set to 0 to create a new file itemid => Int // 设置为 0 以创建一个新文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM