Class: MoodleRb::Courses

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Utility
Defined in:
lib/moodle_rb/courses.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) ⇒ Courses

Returns a new instance of Courses.



8
9
10
11
12
# File 'lib/moodle_rb/courses.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/courses.rb', line 6

def query_options
  @query_options
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#contents(course_id) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/moodle_rb/courses.rb', line 136

def contents(course_id)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_course_get_contents', token),
      :body => {
        :courseid => course_id
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response
end

#core_course_import_course(params) ⇒ Object

required params: from_course_id: the id of the course we are importing from to_course_id: the id of the course we are importing to



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/moodle_rb/courses.rb', line 167

def core_course_import_course(params)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_course_import_course', token),
      :body => {
        :importfrom => params[:from_course_id],
        :importto => params[:to_course_id]
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.code == 200 && response.parsed_response.nil?
end

#create(params) ⇒ Object

required params: full_name short_name

must be unique

parent_category

the parent category id inside which the new category will be created

optional params: idnumber

the new course external reference. must be unique


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/moodle_rb/courses.rb', line 49

def create(params)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_course_create_courses', token),
      :body => {
        :courses => {
          '0' => {
            :fullname => params[:full_name],
            :shortname => params[:short_name],
            :categoryid => params[:parent_category],
            :idnumber => params[:idnumber],
            :startdate => params[:start_date],
            :enddate => params[:end_date]
          }.select {|k, v| v }
        }
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response.first
end

#destroy(id) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/moodle_rb/courses.rb', line 90

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

#enrolled_users(course_id) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/moodle_rb/courses.rb', line 106

def enrolled_users(course_id)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_enrol_get_enrolled_users', token),
      :body => {
        :courseid => course_id
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response
end

#grade_items(course_id, user_id = 0, group_id = 0) ⇒ Object



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

def grade_items(course_id, user_id=0, group_id=0)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('gradereport_user_get_grade_items', token),
      :body => {
        :courseid => course_id,
        :userid => user_id,
        :groupid => group_id
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response['usergrades']
end

#indexObject



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

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

#module(course_module_id) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/moodle_rb/courses.rb', line 150

def module(course_module_id)
  response = self.class.post(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_course_get_course_module', token),
      :body => {
        :cmid => course_module_id
      }
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response['cm']
end

#search(criteria_value, criteria_name = 'search') ⇒ Object

Search courses by (name, module, block, tag)



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/moodle_rb/courses.rb', line 26

def search(criteria_value, criteria_name = 'search')
  response = self.class.get(
    '/webservice/rest/server.php',
    {
      :query => query_hash('core_course_search_courses', token).merge({
        :criterianame => criteria_name,
        :criteriavalue => criteria_value
      })
    }.merge(query_options)
  )
  check_for_errors(response)
  response.parsed_response
end

#show(id) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/moodle_rb/courses.rb', line 72

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