Class: MoodleRb::Categories

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

Constant Summary collapse

ROOT_CATEGORY =
0

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

Returns a new instance of Categories.



9
10
11
12
13
# File 'lib/moodle_rb/categories.rb', line 9

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

def query_options
  @query_options
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#create(params) ⇒ Object

required params: name optional params: parent_category

the parent category id inside which the new category will be created
- set to nil for a root category

idnumber

the new category external reference. must be unique

description

the new category description


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

def create(params)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_course_create_categories', token),
      :body => {
        :categories => {
          '0' => {
            :name => params[:name],
            :parent => (params[:parent_category] || ROOT_CATEGORY),
            :idnumber => params[:idnumber],
            :description => params[:description]
          }
        }
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response.first
end

#destroy(id) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/moodle_rb/categories.rb', line 76

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

#indexObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/moodle_rb/categories.rb', line 15

def index
  response = self.class.get(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_course_get_categories', token)
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response
end

#show(id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/moodle_rb/categories.rb', line 57

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