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

Date type custom fields returned as string in API core_course_get_courses

XMLWordPrintable

    • MOODLE_310_STABLE, MOODLE_37_STABLE, MOODLE_38_STABLE
    • MOODLE_310_STABLE
    • Hide
      1. Login as admin
      2. Navigate to Courses > Course custom fields in site administration
      3. Press Add a new category
      4. Press Add a new custom field and select Date and time
      5. Choose a Name and Short name and press Save changes
      6. Navigate to Courses > Add a new course in site administration
      7. Use Course full name: My course 101 & Course shortname: mycourse101
      8. Expand Other fields
      9. Enable the custom field you created in step 4. and choose a date
      10. Press Save and display
      11. Enable web services
      12. Execute the core_course_search_courses external method, e.g.:

        curl -s 'http://localhost:8000/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_course_search_courses&criterianame=search&criteriavalue=mycourse101&wstoken=fd5dddfbb27a6435dfe1aa4908a2275f' | python -m json.tool
        

      13. Execute the core_course_get_courses_by_field external method, e.g.:

        curl -s 'http://localhost:8000/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_course_get_courses_by_field&field=shortname&value=mycourse101&wstoken=fd5dddfbb27a6435dfe1aa4908a2275f' | python -m json.tool
        

      14. Execute the core_course_get_courses external method, e.g.:

        curl -s 'http://localhost:8000/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_course_get_courses&options[ids][]=2&wstoken=fd5dddfbb27a6435dfe1aa4908a2275f' | python -m json.tool
        

      15. For each external method call, confirm the returned data contains custom fields data containing both value and valueraw properties:

        "customfields": [
          {
            "name": "My date field",
            "shortname": "mydatefield",
            "type": "date",
            "value": "1 December 2020",
            "valueraw": "1606780800"
          }
        ],
        

      Show
      Login as admin Navigate to Courses > Course custom fields in site administration Press Add a new category Press Add a new custom field and select Date and time Choose a Name and Short name and press Save changes Navigate to Courses > Add a new course in site administration Use Course full name: My course 101 & Course shortname: mycourse101 Expand Other fields Enable the custom field you created in step 4. and choose a date Press Save and display Enable web services Execute the core_course_search_courses external method, e.g.: curl -s 'http://localhost:8000/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_course_search_courses&criterianame=search&criteriavalue=mycourse101&wstoken=fd5dddfbb27a6435dfe1aa4908a2275f' | python -m json.tool Execute the core_course_get_courses_by_field external method, e.g.: curl -s 'http://localhost:8000/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_course_get_courses_by_field&field=shortname&value=mycourse101&wstoken=fd5dddfbb27a6435dfe1aa4908a2275f' | python -m json.tool Execute the core_course_get_courses external method, e.g.: curl -s 'http://localhost:8000/webservice/rest/server.php?moodlewsrestformat=json' --data 'wsfunction=core_course_get_courses&options[ids][]=2&wstoken=fd5dddfbb27a6435dfe1aa4908a2275f' | python -m json.tool For each external method call, confirm the returned data contains custom fields data containing both value and valueraw properties: "customfields": [ { "name": "My date field", "shortname": "mydatefield", "type": "date", "value": "1 December 2020", "valueraw": "1606780800" } ],

      In Moodle 3.7, course custom fields were introduced and there is something I have wondered about. Why have you formatted dates as human readable strings it makes no sence. A webservice API is an integration tool and now you are dictating how other systems should display / use the date.

      We have a problem using the dates, since we would like to see if the date has passed or not and now we have to parse the date first. This could be an issue, since the date also takes into account the users preferred language and parses from that setting. This means it is quite messy determining the correct format, parse it and then comparing it to the actual date.

      Why is not just an unix timestamp as the rest of moodle dates?

       

      I would almost categorize this as a bug, since it is almost useless if you use it with an external system.

            pholden Paul Holden
            mcardle Ulrik McArdle
            Neill Magill Neill Magill
            Victor Déniz Falcón Victor Déniz Falcón
            Janelle Barcega Janelle Barcega
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 4 hours, 20 minutes
                4h 20m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.