Class: MoodleRb::Enrolments

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

Constant Summary collapse

STUDENT_ROLE_ID =
5
TEACHER_ROLE_ID =
3

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) ⇒ Enrolments

Returns a new instance of Enrolments.



10
11
12
13
14
# File 'lib/moodle_rb/enrolments.rb', line 10

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/enrolments.rb', line 6

def query_options
  @query_options
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#create(params) ⇒ Object

required params: user_id course_id optional params: role_id - defaults to student role id



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moodle_rb/enrolments.rb', line 20

def create(params)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('enrol_manual_enrol_users', token),
      :body => {
        :enrolments => {
          '0' => {
            :userid => params[:user_id],
            :courseid => params[:course_id],
            :roleid => params[:role_id] || STUDENT_ROLE_ID,
            :timestart => params[:time_start],
            :timeend => params[:time_end]
          }.select {|k, v| v }
        }
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.code == 200 && response.parsed_response.nil?
end

#destroy(params) ⇒ Object

required params: user_id course_id optional params: role_id - defaults to student role id



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/moodle_rb/enrolments.rb', line 46

def destroy(params)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('enrol_manual_unenrol_users', token),
      :body => {
        :enrolments => {
          '0' => {
            :userid => params[:user_id],
            :courseid => params[:course_id],
            :roleid => params[:role_id] || STUDENT_ROLE_ID
          }
        }
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.code == 200 && response.parsed_response.nil?
end