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

Include institution and department update in webservice

    XMLWordPrintable

    Details

    • Testing Instructions:
      Hide
      Setup
      1. Log in as an admin.
      2. Go to "Site administration / Advanced features" and tick "Enable web services":
      3. Go to "Site administration / Plugins / Web services / External services" and add a new "External service".
      4. Set a name for the external service, let's name this as "MoodleWeb"
      5. Tick "Enabled"
      6. Save the changes.
      7. Click on the external service's "Functions" link.
      8. Add the following functions:
        • core_user_create_users
        • core_user_update_users
      9. Go to "Site administration / Plugins / Web services / Manage tokens" and create a token for the admin user for the "MoodleWeb" service. Take note of this token and let's call this as "YOUR_WS_TOKEN".
      User creation
      1. Open a terminal and enter the following cURL command (replace the capitalised values with the appropriate values:

        curl 'YOUR_MOODLE_URL/webservice/rest/server.php?moodlewsrestformat=json' \
        --data 'wsfunction=core_user_create_users&wstoken=YOUR_WS_TOKEN&users[0][createpassword]=1&users[0][username]=USERNAME&users[0][firstname]=FIRSTNAME&users[0][lastname]=LASTNAME&users[0][email]=EMAIL&users[0][maildisplay]=1&users[0][interests]=COMMA_SEPARATED_INTERESTS&users[0][url]=YOUR_WEBPAGE_URL&users[0][icq]=ICQ_NUMBER&users[0][skype]=skype&users[0][aim]=AIM_NUMBER&users[0][yahoo]=YAHOO_EMAIL&users[0][msn]=MSN_EMAIL&users[0][institution]=INSTITUTION_NAME&users[0][department]=DEPARTMENT&users[0][phone1]=PHONE_NUMBER&users[0][phone2]=MOBILE_NUMBER&users[0][address]=POSTAL_ADDRESS' | python -m "json.tool"

        • Note: The parameters after the "email" parameter are optional. For example, if you don't want to include the address, you can simply remove the "&users[0][address]=POSTAL_ADDRESS" part of the command.
      2. Confirm that the command is successfully executed. You should see it return a JSON response that contains the user's ID and username. Something like

        [
        {
        "id": 23,
        "username": "USERNAME"
        }
        ]

      1. Take note of this user's ID.
      2. On your web browser, go to "Site administration / Users / Accounts / Browse list of users
      3. Click on the link of the user that you created via the "core_user_create_users" web service function.
      4. On the user's profile page, click "Edit profile"
      5. Confirm that the user data that you entered for the user in the cURL command are being shown in their correct fields.
      User update
      1. Back on the terminal, enter the following cURL command, replace the capitalised parameters with the appropriate values:

        curl 'YOUR_MOODLE_URL/webservice/rest/server.php?moodlewsrestformat=json' \
        --data 'wsfunction=core_user_update_users&wstoken=YOUR_WS_TOKEN&users[0][id]=USERID&users[0][maildisplay]=2&users[0][interests]=NEW_COMMA_SEPARATED_INTERESTS&users[0][url]=NEW_YOUR_WEBPAGE_URL&users[0][icq]=NEW_ICQ_NUMBER&users[0][skype]=NEW_skype&users[0][aim]=NEW_AIM_NUMBER&users[0][yahoo]=NEW_YAHOO_EMAIL&users[0][msn]=NEW_MSN_EMAIL&users[0][institution]=NEW_INSTITUTION_NAME&users[0][department]=NEW_DEPARTMENT&users[0][phone1]=NEW_PHONE_NUMBER&users[0][phone2]=NEW_MOBILE_NUMBER&users[0][address]=NEW_POSTAL_ADDRESS' | python -m "json.tool"

        • Note: The parameters after the "id" parameter are optional. For example, if you don't want to include the address, you can simply remove the "&users[0][address]=POSTAL_ADDRESS" part of the command.
      2. Confirm that you don't encounter any error and you only see a 'null' response. (It's weird, but it's already been this way for ages, might be worth raising a separate issue in the future.)
      3. Back on your web browser, refresh the user's "Edit profile" page.
      4. Confirm that the updated user data that you entered for the user in the cURL command are being shown in their correct fields.
      Show
      Setup Log in as an admin. Go to " Site administration / Advanced features " and tick " Enable web services ": Go to " Site administration / Plugins / Web services / External services " and add a new " External service ". Set a name for the external service, let's name this as " MoodleWeb " Tick " Enabled " Save the changes. Click on the external service's " Functions " link. Add the following functions: core_user_create_users core_user_update_users Go to " Site administration / Plugins / Web services / Manage tokens " and create a token for the admin user for the " MoodleWeb " service. Take note of this token and let's call this as " YOUR_WS_TOKEN ". User creation Open a terminal and enter the following cURL command (replace the capitalised values with the appropriate values: curl ' YOUR_MOODLE_URL /webservice/rest/server.php?moodlewsrestformat=json' \ --data 'wsfunction=core_user_create_users&wstoken= YOUR_WS_TOKEN &users [0] [createpassword] =1&users [0] [username] = USERNAME &users [0] [firstname] = FIRSTNAME &users [0] [lastname] = LASTNAME &users [0] [email] = EMAIL &users [0] [maildisplay] =1&users [0] [interests] = COMMA_SEPARATED_INTERESTS &users [0] [url] = YOUR_WEBPAGE_URL &users [0] [icq] = ICQ_NUMBER &users [0] [skype] = skype &users [0] [aim] = AIM_NUMBER &users [0] [yahoo] = YAHOO_EMAIL &users [0] [msn] = MSN_EMAIL &users [0] [institution] = INSTITUTION_NAME &users [0] [department] = DEPARTMENT &users [0] [phone1] = PHONE_NUMBER &users [0] [phone2] = MOBILE_NUMBER &users [0] [address] = POSTAL_ADDRESS ' | python -m "json.tool" Note: The parameters after the " email " parameter are optional. For example, if you don't want to include the address, you can simply remove the " &users [0] [address] = POSTAL_ADDRESS " part of the command. Confirm that the command is successfully executed. You should see it return a JSON response that contains the user's ID and username. Something like [ { "id": 23, "username": " USERNAME " } ] Take note of this user's ID. On your web browser, go to " Site administration / Users / Accounts / Browse list of users Click on the link of the user that you created via the " core_user_create_users " web service function. On the user's profile page, click " Edit profile " Confirm that the user data that you entered for the user in the cURL command are being shown in their correct fields. User update Back on the terminal, enter the following cURL command, replace the capitalised parameters with the appropriate values: curl ' YOUR_MOODLE_URL /webservice/rest/server.php?moodlewsrestformat=json' \ --data 'wsfunction=core_user_update_users&wstoken= YOUR_WS_TOKEN &users [0] [id] = USERID &users [0] [maildisplay] =2&users [0] [interests] = NEW_COMMA_SEPARATED_INTERESTS &users [0] [url] = NEW_YOUR_WEBPAGE_URL &users [0] [icq] = NEW_ICQ_NUMBER &users [0] [skype] = NEW_skype &users [0] [aim] = NEW_AIM_NUMBER &users [0] [yahoo] = NEW_YAHOO_EMAIL &users [0] [msn] = NEW_MSN_EMAIL &users [0] [institution] = NEW_INSTITUTION_NAME &users [0] [department] = NEW_DEPARTMENT &users [0] [phone1] = NEW_PHONE_NUMBER &users [0] [phone2] = NEW_MOBILE_NUMBER &users [0] [address] = NEW_POSTAL_ADDRESS ' | python -m "json.tool" Note: The parameters after the " id " parameter are optional. For example, if you don't want to include the address, you can simply remove the " &users [0] [address] = POSTAL_ADDRESS " part of the command. Confirm that you don't encounter any error and you only see a ' null ' response. (It's weird, but it's already been this way for ages, might be worth raising a separate issue in the future.) Back on your web browser, refresh the user's " Edit profile " page. Confirm that the updated user data that you entered for the user in the cURL command are being shown in their correct fields.
    • Difficulty:
      Easy
    • Affected Branches:
      MOODLE_36_STABLE
    • Fixed Branches:
      MOODLE_36_STABLE
    • Pull Master Branch:
      MDL-62657-master

      Description

      The webservice core_user_create_users doesn't allow to set the department and institution of a user.

      The department and institution are very important field as they can be used in grader reports, user search and other Moodle screens. See: /admin/settings.php?section=userpolicies under Show user identity.

      I think this is an easy fix, the department and institution profile fields seem to be regular profile fields. Are these just forgotten in the core_user_create_users webservice?

      [edit]

      Nicholas Stefanski also reports missing fields phone1 and maildisplay

       

        Attachments

          Activity

            People

            • Votes:
              4 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Fix Release Date:
                3/Dec/18

                Time Tracking

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 35 minutes
                35m