moodle 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -1
- data/.travis.yml +8 -4
- data/Gemfile +3 -9
- data/LICENSE.md +19 -19
- data/README.md +204 -188
- data/Rakefile +7 -7
- data/lib/moodle.rb +39 -39
- data/lib/moodle/client.rb +70 -68
- data/lib/moodle/helper.rb +11 -11
- data/lib/moodle/protocols/rest.rb +11 -12
- data/lib/moodle/services/cohort.rb +22 -0
- data/lib/moodle/services/course.rb +21 -21
- data/lib/moodle/services/user.rb +39 -39
- data/lib/moodle/services/webservice.rb +9 -9
- data/moodle.gemspec +21 -13
- data/test/fixtures/config.yml +1 -1
- data/test/test_client.rb +33 -33
- data/test/test_helper.rb +9 -9
- data/test/test_moodle.rb +28 -28
- metadata +107 -8
- checksums.yaml +0 -7
data/Rakefile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'rake/testtask'
|
2
|
-
|
3
|
-
Rake::TestTask.new do |t|
|
4
|
-
t.libs << 'test'
|
5
|
-
end
|
6
|
-
|
7
|
-
desc "Running tests"
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
Rake::TestTask.new do |t|
|
4
|
+
t.libs << 'test'
|
5
|
+
end
|
6
|
+
|
7
|
+
desc "Running tests"
|
8
8
|
task :default => :test
|
data/lib/moodle.rb
CHANGED
@@ -1,39 +1,39 @@
|
|
1
|
-
$:.unshift File.dirname(__FILE__)
|
2
|
-
|
3
|
-
require 'moodle/client'
|
4
|
-
require 'yaml'
|
5
|
-
|
6
|
-
module Moodle
|
7
|
-
@@config = {
|
8
|
-
:username => nil,
|
9
|
-
:password => nil,
|
10
|
-
:token => nil,
|
11
|
-
:protocol => nil,
|
12
|
-
:domain => nil,
|
13
|
-
:service => nil,
|
14
|
-
:format => 'json'
|
15
|
-
}
|
16
|
-
|
17
|
-
@valid_config_keys = @@config.keys
|
18
|
-
|
19
|
-
# Configuration is for the instance only
|
20
|
-
def self.new(options={})
|
21
|
-
Moodle::Client.new(options)
|
22
|
-
end
|
23
|
-
|
24
|
-
# Configure at global level trough hash
|
25
|
-
def self.configure(options={})
|
26
|
-
options.each {|k,v| @@config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
|
27
|
-
end
|
28
|
-
|
29
|
-
# Configure at global level through yaml file
|
30
|
-
def self.configure_with(path_to_yaml_file)
|
31
|
-
config = YAML::load(IO.read(path_to_yaml_file))
|
32
|
-
configure(config)
|
33
|
-
end
|
34
|
-
|
35
|
-
# Obtain the global configuration
|
36
|
-
def self.config
|
37
|
-
@@config
|
38
|
-
end
|
39
|
-
end
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
require 'moodle/client'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
module Moodle
|
7
|
+
@@config = {
|
8
|
+
:username => nil,
|
9
|
+
:password => nil,
|
10
|
+
:token => nil,
|
11
|
+
:protocol => nil,
|
12
|
+
:domain => nil,
|
13
|
+
:service => nil,
|
14
|
+
:format => 'json'
|
15
|
+
}
|
16
|
+
|
17
|
+
@valid_config_keys = @@config.keys
|
18
|
+
|
19
|
+
# Configuration is for the instance only
|
20
|
+
def self.new(options={})
|
21
|
+
Moodle::Client.new(options)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Configure at global level trough hash
|
25
|
+
def self.configure(options={})
|
26
|
+
options.each {|k,v| @@config[k.to_sym] = v if @valid_config_keys.include? k.to_sym}
|
27
|
+
end
|
28
|
+
|
29
|
+
# Configure at global level through yaml file
|
30
|
+
def self.configure_with(path_to_yaml_file)
|
31
|
+
config = YAML::load(IO.read(path_to_yaml_file))
|
32
|
+
configure(config)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Obtain the global configuration
|
36
|
+
def self.config
|
37
|
+
@@config
|
38
|
+
end
|
39
|
+
end
|
data/lib/moodle/client.rb
CHANGED
@@ -1,69 +1,71 @@
|
|
1
|
-
require 'moodle/protocols/rest'
|
2
|
-
require 'moodle/services/user'
|
3
|
-
require 'moodle/services/course'
|
4
|
-
require 'moodle/services/
|
5
|
-
require '
|
6
|
-
require '
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
include Moodle::Service::
|
12
|
-
include Moodle::Service::
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
@client = Moodle::Protocol::Rest.new
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
:
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
:
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
1
|
+
require 'moodle/protocols/rest'
|
2
|
+
require 'moodle/services/user'
|
3
|
+
require 'moodle/services/course'
|
4
|
+
require 'moodle/services/cohort'
|
5
|
+
require 'moodle/services/webservice'
|
6
|
+
require 'hashie'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
module Moodle
|
10
|
+
class Client
|
11
|
+
include Moodle::Service::User
|
12
|
+
include Moodle::Service::Course
|
13
|
+
include Moodle::Service::Cohort
|
14
|
+
include Moodle::Service::Webservice
|
15
|
+
|
16
|
+
attr_reader :username, :password, :domain, :protocol, :service, :format, :token
|
17
|
+
|
18
|
+
def initialize(options={})
|
19
|
+
@username = options[:username] || Moodle.config[:username]
|
20
|
+
@password = options[:password] || Moodle.config[:password]
|
21
|
+
@domain = options[:domain] || Moodle.config[:domain]
|
22
|
+
@protocol = options[:protocol] || Moodle.config[:protocol]
|
23
|
+
@service = options[:service] || Moodle.config[:service]
|
24
|
+
@format = options[:format] || Moodle.config[:format]
|
25
|
+
@token = options[:token] || Moodle.config[:token]
|
26
|
+
|
27
|
+
# If no token is provided generate one
|
28
|
+
if @token.nil?
|
29
|
+
@token = self.obtain_token
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Retuns a Moodle::Protocol client instance
|
34
|
+
def client
|
35
|
+
if @client.nil?
|
36
|
+
# Instantiate the client protocol
|
37
|
+
case @protocol
|
38
|
+
when 'rest'
|
39
|
+
@client = Moodle::Protocol::Rest.new
|
40
|
+
else
|
41
|
+
@client = Moodle::Protocol::Rest.new
|
42
|
+
end
|
43
|
+
end
|
44
|
+
@client
|
45
|
+
end
|
46
|
+
|
47
|
+
# Obtains a token from the username and password
|
48
|
+
def obtain_token
|
49
|
+
response = client.request(@domain + '/login/token.php', {
|
50
|
+
:username => @username,
|
51
|
+
:password => @password,
|
52
|
+
:service => @service
|
53
|
+
})
|
54
|
+
|
55
|
+
parsed = JSON.parse(response)
|
56
|
+
parsed['token']
|
57
|
+
end
|
58
|
+
|
59
|
+
# Make a request using the desired protocol and format
|
60
|
+
def request(params={})
|
61
|
+
params.merge!(
|
62
|
+
:wstoken => @token,
|
63
|
+
:moodlewsrestformat => @format,
|
64
|
+
:wsfunction => caller[0][/`.*'/][1..-2]
|
65
|
+
)
|
66
|
+
response = client.request(@domain + '/webservice/' + @protocol + '/server.php', params)
|
67
|
+
|
68
|
+
JSON.parse(response)
|
69
|
+
end
|
70
|
+
end
|
69
71
|
end
|
data/lib/moodle/helper.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require 'hashie'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
module Moodle
|
5
|
-
class Helper
|
6
|
-
|
7
|
-
def to_object(json)
|
8
|
-
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
1
|
+
require 'hashie'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Moodle
|
5
|
+
class Helper
|
6
|
+
|
7
|
+
def to_object(json)
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -1,13 +1,12 @@
|
|
1
|
-
require 'rest-client'
|
2
|
-
|
3
|
-
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module Moodle
|
4
|
+
module Protocol
|
5
|
+
class Rest
|
6
|
+
|
7
|
+
def request(url, params={})
|
8
|
+
RestClient.get(url, :params => params)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
13
12
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Moodle
|
2
|
+
module Service
|
3
|
+
module Cohort
|
4
|
+
# Returns cohort details
|
5
|
+
def core_cohort_get_cohorts(ids)
|
6
|
+
params = {}
|
7
|
+
|
8
|
+
counter = 0
|
9
|
+
ids.each do |id|
|
10
|
+
params['cohortids[' + counter.to_s + ']'] = id
|
11
|
+
counter = counter + 1
|
12
|
+
end
|
13
|
+
|
14
|
+
response = request(params)
|
15
|
+
|
16
|
+
if response.any?
|
17
|
+
cohorts = response.map { |cohort| Hashie::Mash.new(cohort) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,22 +1,22 @@
|
|
1
|
-
module Moodle
|
2
|
-
module Service
|
3
|
-
module Course
|
4
|
-
# Return course details
|
5
|
-
def core_course_get_courses(options)
|
6
|
-
params = {}
|
7
|
-
|
8
|
-
counter = 0
|
9
|
-
options.each do |id|
|
10
|
-
params['options[ids][' + counter.to_s + ']'] = id
|
11
|
-
counter = counter + 1
|
12
|
-
end
|
13
|
-
|
14
|
-
response = request(params)
|
15
|
-
|
16
|
-
if response.any?
|
17
|
-
courses = response.map { |course| Hashie::Mash.new(course) }
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
1
|
+
module Moodle
|
2
|
+
module Service
|
3
|
+
module Course
|
4
|
+
# Return course details
|
5
|
+
def core_course_get_courses(options)
|
6
|
+
params = {}
|
7
|
+
|
8
|
+
counter = 0
|
9
|
+
options.each do |id|
|
10
|
+
params['options[ids][' + counter.to_s + ']'] = id
|
11
|
+
counter = counter + 1
|
12
|
+
end
|
13
|
+
|
14
|
+
response = request(params)
|
15
|
+
|
16
|
+
if response.any?
|
17
|
+
courses = response.map { |course| Hashie::Mash.new(course) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
22
|
end
|
data/lib/moodle/services/user.rb
CHANGED
@@ -1,40 +1,40 @@
|
|
1
|
-
module Moodle
|
2
|
-
module Service
|
3
|
-
module User
|
4
|
-
# Get users by field
|
5
|
-
def core_user_get_users_by_field(field, values)
|
6
|
-
params = {
|
7
|
-
:field => field
|
8
|
-
}
|
9
|
-
|
10
|
-
# Add all the userids as array params
|
11
|
-
counter = 0
|
12
|
-
values.each do |id|
|
13
|
-
params['values[' + counter.to_s + ']'] = id
|
14
|
-
counter = counter + 1
|
15
|
-
end
|
16
|
-
|
17
|
-
response = request(params)
|
18
|
-
Hashie::Mash.new *response
|
19
|
-
end
|
20
|
-
|
21
|
-
# Search for users matching the criteria
|
22
|
-
def core_user_get_users(criteria)
|
23
|
-
params = {}
|
24
|
-
|
25
|
-
counter = 0
|
26
|
-
criteria.each do |key,value|
|
27
|
-
params['criteria[' + counter.to_s + '][key]'] = key.to_s
|
28
|
-
params['criteria[' + counter.to_s + '][value]'] = value
|
29
|
-
counter = counter + 1
|
30
|
-
end
|
31
|
-
|
32
|
-
response = request(params)
|
33
|
-
|
34
|
-
if response['users']
|
35
|
-
users = response['users'].map { |user| Hashie::Mash.new(user) }
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
1
|
+
module Moodle
|
2
|
+
module Service
|
3
|
+
module User
|
4
|
+
# Get users by field
|
5
|
+
def core_user_get_users_by_field(field, values)
|
6
|
+
params = {
|
7
|
+
:field => field
|
8
|
+
}
|
9
|
+
|
10
|
+
# Add all the userids as array params
|
11
|
+
counter = 0
|
12
|
+
values.each do |id|
|
13
|
+
params['values[' + counter.to_s + ']'] = id
|
14
|
+
counter = counter + 1
|
15
|
+
end
|
16
|
+
|
17
|
+
response = request(params)
|
18
|
+
Hashie::Mash.new *response
|
19
|
+
end
|
20
|
+
|
21
|
+
# Search for users matching the criteria
|
22
|
+
def core_user_get_users(criteria)
|
23
|
+
params = {}
|
24
|
+
|
25
|
+
counter = 0
|
26
|
+
criteria.each do |key,value|
|
27
|
+
params['criteria[' + counter.to_s + '][key]'] = key.to_s
|
28
|
+
params['criteria[' + counter.to_s + '][value]'] = value
|
29
|
+
counter = counter + 1
|
30
|
+
end
|
31
|
+
|
32
|
+
response = request(params)
|
33
|
+
|
34
|
+
if response['users']
|
35
|
+
users = response['users'].map { |user| Hashie::Mash.new(user) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
40
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
module Moodle
|
2
|
-
module Service
|
3
|
-
module Webservice
|
4
|
-
# Return some site info / user info / list web service functions
|
5
|
-
def core_webservice_get_site_info
|
6
|
-
Hashie::Mash.new request
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
module Moodle
|
2
|
+
module Service
|
3
|
+
module Webservice
|
4
|
+
# Return some site info / user info / list web service functions
|
5
|
+
def core_webservice_get_site_info
|
6
|
+
Hashie::Mash.new request
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
10
|
end
|