![](/img/trans.png)
[英]Uploading file to Moodle using the REST Service core_files_upload
[英]Upload assignment on Moodle using REST webservice function
我正在嘗試開發 Moodle Android 應用程序。 我使用 MoodleREST 源代碼作為參考。 但是該庫不提供用於上傳作業的其余代碼。 我希望能夠通過網絡服務調用從移動客戶端上傳作業。 可以使用 webview 上傳作業,但在這種情況下,用戶需要再次登錄才能訪問上傳作業頁面。
我在這里發現了類似的東西https://moodle.org/mod/forum/discuss.php?d=207875 。
我是moodle的新手並且仍在學習它,所以我的問題可能有點幼稚,所以請耐心等待:)
可以使用 Moodle 網絡服務將帶有文件的提交上傳到作業。
首先使用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
其中:
itemid=0 -moodle 將生成並返回一個itemid
或者您設置 itemid
filecontent - base64 編碼的文件內容
instanceid - 其為 webservices 令牌的用戶 ID
示例響應:
{
"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"
}
您可以使用mod_assign_get_assignments
搜索下一次調用的分配 ID
然后在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
這將使用此文件添加作業提交。
問題我只能使用特定用戶的 webservices 令牌core_files_upload
和mod_assign_save_submission
,即每個用戶都需要一個 webservices 令牌,這可能不切實際。 使用 webservices 用戶令牌,我接到了第一個電話:
{
"exception": "moodle_exception",
"errorcode": "nofile",
"message": "File not specified"
}
用郵遞員測試。 這可能是相關的: https : //tracker.moodle.org/browse/MDL-61276
看起來在moodle Web 服務中沒有針對此的現有解決方案。 Moodle 實際上以 base64 編碼文件,這會給移動設備帶來負擔。 移動設備沒有那么多內存來編碼大文件。 Moodle HQ(和其他)發布的壁櫥解決方案是這樣的: https : //github.com/moodlehq/sample-ws-clients/blob/master/PHP-HTTP-filehandling/client.php將文件保存為私有文件而不是作為任務。 您可能需要對插件進行大量修改。
要上傳文件,我使用此 API 和 POST 方法
https://{YOUR_URL}/webservice/upload.php?moodlewsrestformat=json&wstoken={WSTOKEN}
並且您必須將以下參數作為 FormData 傳遞
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.