Skip to content

Commit

Permalink
Merge pull request #33 from jobready/RTO-27047
Browse files Browse the repository at this point in the history
RTO-27047: [LMS course copy] Update Moodle gem to allow course copy
  • Loading branch information
liammcgrath20 authored Feb 13, 2023
2 parents eaa3745 + d85d7aa commit dfe7149
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ Get course module
moodle.courses.module(course_module_id)
```

Import course data from a course into another course
```
moodle.courses.core_course_import_course(
:from_course_id => from_course_id,
:to_course_id => to_course_id
)
```

### Categories

Create a category
Expand Down
18 changes: 18 additions & 0 deletions lib/moodle_rb/courses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,23 @@ def module(course_module_id)
check_for_errors(response)
response.parsed_response['cm']
end

# 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
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
end
end
2 changes: 1 addition & 1 deletion lib/moodle_rb/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module MoodleRb
VERSION = '2.1.0' unless defined?(self::VERSION)
VERSION = '2.1.5' unless defined?(self::VERSION)

def self.version
VERSION
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
http_interactions:
- request:
method: post
uri: http://localhost:8888/moodle28/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_course_import_course&wstoken=
body:
encoding: UTF-8
string: importfrom=&importto=
headers:
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept:
- "*/*"
User-Agent:
- Ruby
response:
status:
code: 200
message: OK
headers:
Accept-Ranges:
- none
Access-Control-Allow-Origin:
- "*"
Age:
- '0'
Cache-Control:
- private, must-revalidate, pre-check=0, post-check=0, max-age=0
Content-Type:
- application/json
Date:
- Mon, 13 Feb 2023 02:46:46 GMT
Expires:
- Thu, 01 Jan 1970 00:00:00 GMT
Pragma:
- no-cache
Server:
- Moodle
X-Cache:
- MISS
X-Clacks-Overhead:
- GNU Terry Pratchett
X-Powered-By:
- Moodle Ninjas
Content-Length:
- '103'
Connection:
- keep-alive
body:
encoding: UTF-8
string: '{"exception":"moodle_exception","errorcode":"invalidtoken","message":"Invalid
token - token not found"}'
http_version:
recorded_at: Mon, 13 Feb 2023 02:46:46 GMT
recorded_with: VCR 2.9.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
http_interactions:
- request:
method: post
uri: http://localhost:8888/moodle28/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_course_import_course&wstoken=60fc9c9415259404795094957e4ab32f
body:
encoding: UTF-8
string: importfrom=467&importto=468
headers:
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept:
- "*/*"
User-Agent:
- Ruby
response:
status:
code: 200
message: OK
headers:
Accept-Ranges:
- none
Access-Control-Allow-Origin:
- "*"
Age:
- '0'
Cache-Control:
- private, must-revalidate, pre-check=0, post-check=0, max-age=0
Content-Type:
- application/json
Date:
- Mon, 13 Feb 2023 02:46:43 GMT
Expires:
- Thu, 01 Jan 1970 00:00:00 GMT
Pragma:
- no-cache
Server:
- Moodle
X-Cache:
- MISS
X-Clacks-Overhead:
- GNU Terry Pratchett
X-Powered-By:
- Moodle Ninjas
Content-Length:
- '4'
Connection:
- keep-alive
body:
encoding: UTF-8
string: 'null'
http_version:
recorded_at: Mon, 13 Feb 2023 02:46:44 GMT
recorded_with: VCR 2.9.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
http_interactions:
- request:
method: post
uri: http://localhost:8888/moodle28/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_course_import_course&wstoken=60fc9c9415259404795094957e4ab32f
body:
encoding: UTF-8
string: importfrom=467&importto=-1
headers:
Accept-Encoding:
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
Accept:
- "*/*"
User-Agent:
- Ruby
response:
status:
code: 200
message: OK
headers:
Accept-Ranges:
- none
Access-Control-Allow-Origin:
- "*"
Age:
- '0'
Cache-Control:
- private, must-revalidate, pre-check=0, post-check=0, max-age=0
Content-Type:
- application/json
Date:
- Mon, 13 Feb 2023 02:46:45 GMT
Expires:
- Thu, 01 Jan 1970 00:00:00 GMT
Pragma:
- no-cache
Server:
- Moodle
X-Cache:
- MISS
X-Clacks-Overhead:
- GNU Terry Pratchett
X-Powered-By:
- Moodle Ninjas
Content-Length:
- '117'
Connection:
- keep-alive
body:
encoding: UTF-8
string: '{"exception":"moodle_exception","errorcode":"invalidcourseid","message":"You
are trying to use an invalid course ID"}'
http_version:
recorded_at: Mon, 13 Feb 2023 02:46:45 GMT
recorded_with: VCR 2.9.3
44 changes: 44 additions & 0 deletions spec/lib/moodle_rb/courses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,48 @@
end
end
end

describe '#core_course_import_course', :vcr => {
:match_requests_on => [:path], :record => :once
} do
context 'when using valid token' do
let(:params) do
{
:from_course_id => 467,
:to_course_id => 468
}
end
let(:result) { course_moodle_rb.core_course_import_course(params) }

specify do
expect(result).to eq true
end

context 'when using invalid course id' do
let(:params) do
{
:from_course_id => 467,
:to_course_id => -1
}
end

specify do
expect{ course_moodle_rb.core_course_import_course(params) }.to raise_error(
MoodleRb::MoodleError,
'You are trying to use an invalid course ID'
)
end
end
end

context 'when using invalid token' do
let(:token) { '' }
specify do
expect{ course_moodle_rb.core_course_import_course({}) }.to raise_error(
MoodleRb::MoodleError,
'Invalid token - token not found'
)
end
end
end
end

0 comments on commit dfe7149

Please sign in to comment.