Class: MoodleRb::Users

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Utility
Defined in:
lib/moodle_rb/users.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#api_array, #check_for_errors, #key_value_query_format, #query_hash

Constructor Details

#initialize(token, url, query_options) ⇒ Users

Returns a new instance of Users.



8
9
10
11
12
# File 'lib/moodle_rb/users.rb', line 8

def initialize(token, url, query_options)
  @token = token
  @query_options = query_options
  self.class.base_uri url
end

Instance Attribute Details

#query_optionsObject (readonly)

Returns the value of attribute query_options.



6
7
8
# File 'lib/moodle_rb/users.rb', line 6

def query_options
  @query_options
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/moodle_rb/users.rb', line 6

def token
  @token
end

Instance Method Details

#create(params) ⇒ Object

required params: username password firstname lastname email optional params: idnumber

An arbitrary ID code number perhaps from the institution


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/moodle_rb/users.rb', line 19

def create(params)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_user_create_users', token),
      :body => {
        :users => {
          '0' => params
        }
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response.first
end

#destroy(id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/moodle_rb/users.rb', line 55

def destroy(id)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_user_delete_users', token),
      :body => {
        :userids => {
          '0' => id
        }
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response.nil?
end

#enrolled_courses(user_id) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/moodle_rb/users.rb', line 71

def enrolled_courses(user_id)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_enrol_get_users_courses', token),
      :body => {
        :userid => user_id
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response
end

#search(params = {}) ⇒ Object

input keys must be in the list of supported user columns to search id, lastname, firstname, idnumber, username, email



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/moodle_rb/users.rb', line 87

def search(params = {})
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_user_get_users', token),
      :body => {
        :criteria => key_value_query_format(params)
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response['users']
end

#search_identity(search_query) ⇒ Object

Return list of users identities matching the given criteria in their name or other identity fields. Performs a partial match on id, idnumber, fullname, and email



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/moodle_rb/users.rb', line 103

def search_identity(search_query)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_user_search_identity', token),
      :body => {
        :query => search_query
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response['list']
end

#show(id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/moodle_rb/users.rb', line 35

def show(id)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_user_get_users', token),
      :body => {
        :criteria => {
          '0' => {
            :key => 'id',
            :value => id
          }
        }
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response['users'] &&
    response.parsed_response['users'].first
end

#update(params) ⇒ Object

params must include the id of the user it may include any other standard user attributes: username, password, firstname, lastname, email …



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/moodle_rb/users.rb', line 120

def update(params)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_user_update_users', token),
      :body => {
        :users => {
          '0' => params
        }
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.response.code == '200'
end