Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-67043

Web service to enable H5P offline access in the Moodle app

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 3.8
    • Fix Version/s: 3.8
    • Component/s: H5P
    • Testing Instructions:
      Hide

      Notice: You need to be able to use CURL in a console and run SQL in a DB.

      Setup

      1. Login as admin.
      2. Create a course "Course 1".
      3. Create a teacher t1 user and enrolled in the Course 1.
      4. Create a student s1 user and enrolled in the Course 1.
      5. Ensure webservices is enabled (Site Administration > Advanced features), select "Enable Webservices" (enablewebservices).
      6. Setup a webservices in Moodle (Site Administration ► Plugins ► Webservices > External Service).
      7. Turn on the Rest protocol (Site Administration ► Plugins ► Webservices ► Manage protocols).
      8. Enable "Mobile services": Site administration ► Mobile app ► Mobile settings
      9. Click on Site administration ► Plugins ► Web services ► Manage tokens
      10. Create two tokens, one for the teacher t1 and other for the student s1  to service (Mobile Service).

      Testing Scenario 1: Teacher Call WS to get a valid export H5P and is not deployed in the site.

      1. Login as admin.
      2. Go to the "Course 1".
      3. Add a new activity page,
      4. Edit the page content of the new activity page and upload an H5P file attached in the issue arithmetic-quiz.h5p using the "Manage files" button in the Atto editor.
      5. Now click the "Link" button, click "Browse repositories", select "Embedded files" and select the h5p file you just uploaded.
      6. Save the changes.
      7. Copy the URL of the h5p file.
      8. Next, you can do a CURL REST in the console, call simulating a WS client:
        • You need to replace the wstoken for the token created for the teacher in the setup and the URL of your moodle instance and the URL parameter for the URL copied in step 7.

          curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=YOURURLH5PFILE&frame=0&export=0&copyright=0&embed=0&wstoken=WSTOKENTEACHER' | python -m "json.tool"

        • Execute this SQL in the Database:

          SELECT id, filename, filepath, filesize, mimetype, timemodified FROM mdl_files WHERE component LIKE 'core_h5p' AND filearea LIKE 'export' AND filename LIKE 'arithmetic-quiz%' ORDER BY timemodified DESC LIMIT 1;

      1. Expected result: Check that the responses' values from the CURL call has the same values like the results from the SQL. The filename, filepath, filesize, mimetype and timemodified should be part of the files record response from CURL call.
      2. Confirm that: the fileurl value in the response from the CURL call you should get the file using a new tab on the browser.

      Testing Scenario 2: Student Call WS to get a valid export H5P and is already deployed in the site.

      1. Login as admin.
      2. Go to the "Course 1".
      3. Add a new activity page,
      4. Edit the page content of the new activity page and upload an H5P file attached in the issue find-the-words-7-7.h5p using the "Manage files" button in the Atto editor.
      5. Now click the "Link" button, click "Browse repositories", select "Embedded files" and select the h5p file you just uploaded.
      6. Save the changes.
      7. Copy the URL of the h5p file.
      8. Paste the next URL and change YOURURLH5PFILE for the url copied in step 7.
        http://YOURMOODLESITE/h5p/embed.php?url=YOURURLH5PFILE
      1. You have to see the H5P file. So, the H5P file has been deployed.
      2. Next, you can do a CURL REST in the console, call simulating a WS client:
        • You need to replace the wstoken for the token created for the student in the setup and the URL of your moodle instance and the URL parameter for the URL copied in step 7.

          curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=YOURMOODLEURLH5P&embed=0&wstoken=STUDENTWSTOKEN' | python -m "json.tool"

        • Execute this SQL in the Database:

          SELECT id, filename, filepath, filesize, mimetype, timemodified FROM mdl_files WHERE component LIKE 'core_h5p' AND filearea LIKE 'export' AND filename LIKE 'find-the-words%'

      1. Expected result: Check that the responses' values from the CURL call has the same values like the results from the SQL. The filename, filepath, filesize, mimetype and timemodified should be part of the files record response from CURL call
      2. Confirm that :the fileurl value in the response from the CURL call you should get the file using a new tab on the browser.

      Testing Scenario 3: Call WS with an empty url.

      1. Do a CURL REST in the console, call simulating a WS client:
        • You need to replace the wstoken for the token created for the teacher in the setup and the URL of your moodle instance and the URL parameter will be empty.

          curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=&frame=0&export=0&copyright=0&embed=0&wstoken=WSTOKENTEACHER' | python -m "json.tool"

      1. Expected result: You have to get a response like thiis:

        {
            "files": [],
            "warnings": [
                {
                    "item": "",
                    "message": "Invalid H5P content URL.",
                    "warningcode": "0"
                }
            ]
        }

      Testing Scenario 4: Call WS with an h5p file unknown.

      1. Do a CURL REST in the console, call simulating a WS client:
        • You need to replace the wstoken for the token created for the teacher in the setup and the URL of your moodle instance. For the URL parameter you could use the step 7 on the testing scenario 1 and change the name of the file for this, h5pfile.h5p.

          curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=YOURURLH5PFILE&frame=0&export=0&copyright=0&embed=0&wstoken=WSTOKENTEACHER' | python -m "json.tool"

      1. Expected result: You have to get a response like this:

        {
            "files": [],
            "warnings": [
                {
                    "item": "http://localhost/m/h5pmobile_master/pluginfile.php/43/mod_page/content/2/h5pfile.h5p",
                    "message": "H5P file not found",
                    "warningcode": null
                }
            ]
        }

        The item value has to be the same like your YOURURLH5PFILE.

      Testing Scenario 5: Call WS with an invalid h5p.

      1. Login as admin.
      2. Go to the "Course 1".
      3. Add a new activity page,
      4. Edit the page content of the new activity page and upload an H5P file attached in the issue invalid.h5p using the "Manage files" button in the Atto editor.
      5. Now click the "Link" button, click "Browse repositories", select "Embedded files" and select the h5p file you just uploaded.
      6. Save the changes.
      7. Copy the URL of the h5p file.
      8. Next, you can do a CURL REST in the console, call simulating a WS client:
        • You need to replace the wstoken for the token created for the teacher in the setup and the URL of your moodle instance and the URL parameter for the URL copied in step 7.

          curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=YOURMOODLEURLH5P&embed=0&wstoken=STUDENTWSTOKEN' | python -m "json.tool"

      9. Expected result: You have to get a response like this:

        {
            "files": [],
            "warnings": [
                {
                    "item": "http://localhost/m/stable_master/pluginfile.php/649/mod_page/content/1/invalid.h5p",
                    "message": "File \"content/phet.html\" not allowed. Only files with the following extensions are allowed: json png jpg jpeg gif bmp tif tiff svg eot ttf woff woff2 otf webm mp4 ogg mp3 m4a wav txt pdf rtf doc docx xls xlsx ppt pptx odt ods odp xml csv diff patch swf md textile vtt webvtt.",
                    "warningcode": "notinwhitelist"
                }
            ]
        }

      Show
      Notice: You need to be able to use CURL in a console and run SQL in a DB. Setup Login as admin. Create a course "Course 1". Create a teacher t1 user and enrolled in the Course 1. Create a student s1 user and enrolled in the Course 1. Ensure webservices is enabled (Site Administration > Advanced features), select "Enable Webservices" ( enablewebservices ). Setup a webservices in Moodle (Site Administration ► Plugins ► Webservices > External Service). Turn on the Rest protocol (Site Administration ► Plugins ► Webservices ► Manage protocols). Enable "Mobile services": Site administration ► Mobile app ► Mobile settings Click on Site administration ► Plugins ► Web services ► Manage tokens Create two tokens, one for the teacher t1 and other for the student s1  to service (Mobile Service). Testing Scenario 1: Teacher Call WS to get a valid export H5P and is not deployed in the site. Login as admin. Go to the "Course 1". Add a new activity page, Edit the page content of the new activity page and upload an H5P file attached in the issue arithmetic-quiz.h5p using the "Manage files" button in the Atto editor. Now click the "Link" button, click "Browse repositories", select "Embedded files" and select the h5p file you just uploaded. Save the changes. Copy the URL of the h5p file. Next, you can do a CURL REST in the console, call simulating a WS client: You need to replace the wstoken for the token created for the teacher in the setup and the URL of your moodle instance and the URL parameter for the URL copied in step 7. curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=YOURURLH5PFILE&frame=0&export=0&copyright=0&embed=0&wstoken=WSTOKENTEACHER' | python -m "json.tool" Execute this SQL in the Database: SELECT id, filename, filepath, filesize, mimetype, timemodified FROM mdl_files WHERE component LIKE 'core_h5p' AND filearea LIKE 'export' AND filename LIKE 'arithmetic-quiz%' ORDER BY timemodified DESC LIMIT 1; Expected result: Check that the responses' values from the CURL call has the same values like the results from the SQL. The  filename, filepath, filesize, mimetype and timemodified  should be part of the files record response from CURL call. Confirm that : the fileurl value in the response from the CURL call you should get the file using a new tab on the browser. Testing Scenario 2: Student Call WS to get a valid export H5P and is already deployed in the site. Login as admin. Go to the "Course 1". Add a new activity page, Edit the page content of the new activity page and upload an H5P file attached in the issue find-the-words-7-7.h5p using the "Manage files" button in the Atto editor. Now click the "Link" button, click "Browse repositories", select "Embedded files" and select the h5p file you just uploaded. Save the changes. Copy the URL of the h5p file. Paste the next URL and change YOURURLH5PFILE for the url copied in step 7. http://YOURMOODLESITE/h5p/embed.php?url=YOURURLH5PFILE You have to see the H5P file. So, the H5P file has been deployed. Next, you can do a CURL REST in the console, call simulating a WS client: You need to replace the wstoken for the token created for the student in the setup and the URL of your moodle instance and the URL parameter for the URL copied in step 7. curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=YOURMOODLEURLH5P&embed=0&wstoken=STUDENTWSTOKEN' | python -m "json.tool" Execute this SQL in the Database: SELECT id, filename, filepath, filesize, mimetype, timemodified FROM mdl_files WHERE component LIKE 'core_h5p' AND filearea LIKE 'export' AND filename LIKE 'find-the-words%' Expected result: Check that the responses' values from the CURL call has the same values like the results from the SQL. The  filename, filepath, filesize, mimetype and timemodified  should be part of the files record response from CURL call Confirm that  :the fileurl value in the response from the CURL call you should get the file using a new tab on the browser. Testing Scenario 3: Call WS with an empty url. Do a CURL REST in the console, call simulating a WS client: You need to replace the wstoken for the token created for the teacher in the setup and the URL of your moodle instance and the URL parameter will be empty. curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=&frame=0&export=0&copyright=0&embed=0&wstoken=WSTOKENTEACHER' | python -m "json.tool" Expected result: You have to get a response like thiis: { "files": [], "warnings": [ { "item": "", "message": "Invalid H5P content URL.", "warningcode": "0" } ] } Testing Scenario 4: Call WS with an h5p file unknown. Do a CURL REST in the console, call simulating a WS client: You need to replace the wstoken for the token created for the teacher in the setup and the URL of your moodle instance. For the URL parameter you could use the step 7 on the testing scenario 1 and change the name of the file for this, h5pfile.h5p. curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=YOURURLH5PFILE&frame=0&export=0&copyright=0&embed=0&wstoken=WSTOKENTEACHER' | python -m "json.tool" Expected result: You have to get a response like this: { "files": [], "warnings": [ { "item": "http://localhost/m/h5pmobile_master/pluginfile.php/43/mod_page/content/2/h5pfile.h5p", "message": "H5P file not found", "warningcode": null } ] } The item value has to be the same like your YOURURLH5PFILE. Testing Scenario 5: Call WS with an invalid h5p. Login as admin. Go to the "Course 1". Add a new activity page, Edit the page content of the new activity page and upload an H5P file attached in the issue invalid.h5p using the "Manage files" button in the Atto editor. Now click the "Link" button, click "Browse repositories", select "Embedded files" and select the h5p file you just uploaded. Save the changes. Copy the URL of the h5p file. Next, you can do a CURL REST in the console, call simulating a WS client: You need to replace the wstoken for the token created for the teacher in the setup and the URL of your moodle instance and the URL parameter for the URL copied in step 7. curl 'http://YOURMOODLESITE/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_h5p_get_trusted_h5p_file&url=YOURMOODLEURLH5P&embed=0&wstoken=STUDENTWSTOKEN' | python -m "json.tool" Expected result:  You have to get a response like this: { "files": [], "warnings": [ { "item": "http://localhost/m/stable_master/pluginfile.php/649/mod_page/content/1/invalid.h5p", "message": "File \"content/phet.html\" not allowed. Only files with the following extensions are allowed: json png jpg jpeg gif bmp tif tiff svg eot ttf woff woff2 otf webm mp4 ogg mp3 m4a wav txt pdf rtf doc docx xls xlsx ppt pptx odt ods odp xml csv diff patch swf md textile vtt webvtt.", "warningcode": "notinwhitelist" } ] }
    • Affected Branches:
      MOODLE_38_STABLE
    • Fixed Branches:
      MOODLE_38_STABLE
    • Pull Master Branch:
      MDL-67043-master
    • Sprint:
      H5P Sprint Relase 3.8

      Description

      We are going to need a WebService that does the following:

      • It will receive the URL of an H5P package.
      • The WS should check if that package can be played (check the trusted capability and so). It's the same check that the embed.php will do in Moodle, that way the app will know if the package can be downloaded for offline usage or not. If it cannot be played, the WS should return a warning or a flag that indicates this case. We could use a moodle exception too if you think it's better.
      • If the package can be played and hasn't been deployed before, the WS should deploy it now.
      • Once deployed, the WS should build a new H5P file with the trusted content_type libraries (downloaded from h5p hub). The app cannot use the original h5p file because it contains content_type libraries that aren't trusted.
      • The WS should return the new h5p file's URL, size and timemodified/hash. You can check the class external_files to see the data that's usually returned for files in WebServices. The app needs to be able to know if the h5p file needs to be re-downloaded or not, so we need a hash or a timemodified that tells it. A file needs to be re-downloaded if the user uploaded a new h5p file or if any of the content_type libraries has been updated.

        Attachments

        1. Screenshot_2.png
          Screenshot_2.png
          442 kB
        2. Screenshot_1.png
          Screenshot_1.png
          353 kB
        3. invalid.h5p
          426 kB
        4. find-the-words-7-7.h5p
          617 kB
        5. arithmetic-quiz.h5p
          731 kB

          Issue Links

            Activity

              People

              Assignee:
              cescobedo Carlos Escobedo
              Reporter:
              dpalou Dani Palou
              Peer reviewer:
              Mihail Geshoski
              Integrator:
              Andrew Nicols
              Tester:
              Janelle Barcega
              Participants:
              Component watchers:
              Amaia Anabitarte, Carlos Escobedo, Ferran Recio, Sara Arjona (@sarjona), Víctor Déniz Falcón
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Fix Release Date:
                18/Nov/19

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 1 day, 4 hours, 50 minutes
                  1d 4h 50m