moodle2cc 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,8 +5,7 @@ module Moodle2CC::Canvas
5
5
  def create_module_meta_item_elements(item_node)
6
6
  item_node.identifierref @identifier
7
7
 
8
- uri = URI.parse(@url)
9
- if uri.scheme
8
+ if @external_link
10
9
  item_node.url @url
11
10
  item_node.content_type 'ExternalUrl'
12
11
  else
@@ -106,24 +106,25 @@ module Moodle2CC::CC
106
106
  end
107
107
 
108
108
  def create_resources(resources_node)
109
+ resources = []
109
110
  @moodle_backup.course.mods.each_with_index do |mod, index|
110
- resource = @resource_factory.get_resource_from_mod(mod, index)
111
- if resource
112
- resource.create_resource_node(resources_node)
113
- resource.create_files(@export_dir)
114
- end
111
+ resources << @resource_factory.get_resource_from_mod(mod, index)
115
112
  end
116
-
117
113
  @moodle_backup.files.each do |file|
118
- href = File.join(WEB_RESOURCES_FOLDER, file)
119
- resources_node.resource(
120
- :type => WEBCONTENT,
121
- :identifier => create_key(href, 'resource_'),
122
- :href => href
123
- ) do |resource_node|
124
- resource_node.file(:href => href)
114
+ unless resources.find { |r| r.respond_to?(:url) && r.url == file }
115
+ mod = Moodle2CC::Moodle::Mod.new
116
+ mod.mod_type = 'resource'
117
+ mod.type = 'file'
118
+ mod.reference = file
119
+ mod.course = @moodle_backup.course
120
+ resources << @resource_factory.get_resource_from_mod(mod)
125
121
  end
126
122
  end
123
+
124
+ resources.compact.each do |resource|
125
+ resource.create_resource_node(resources_node)
126
+ resource.create_files(@export_dir)
127
+ end
127
128
  end
128
129
 
129
130
  def create_web_resources
@@ -3,27 +3,44 @@ module Moodle2CC::CC
3
3
  include CCHelper
4
4
  include Resource
5
5
 
6
- attr_accessor :url
6
+ attr_accessor :url, :external_link, :href
7
7
 
8
8
  def initialize(mod)
9
9
  super
10
10
  @url = mod.reference.to_s.strip
11
+ @external_link = self.class.external_link?(mod)
12
+ @href = @external_link ? "#{@identifier}.xml" : File.join(WEB_RESOURCES_FOLDER, @url)
13
+ @identifier = create_key(@href, 'resource_') unless @external_link
11
14
  end
12
15
 
13
16
  def self.create_resource_key(mod)
14
- if !URI.parse(mod.reference).scheme
17
+ unless external_link?(mod)
15
18
  create_key(File.join(WEB_RESOURCES_FOLDER, mod.reference), 'resource_')
16
19
  else
17
20
  super
18
21
  end
19
22
  end
20
23
 
24
+ def self.external_link?(mod)
25
+ !!URI.parse(mod.reference.strip).scheme
26
+ end
27
+
21
28
  def create_resource_node(resources_node)
22
- resources_node.resource(
23
- :type => WEB_LINK,
24
- :identifier => identifier
25
- ) do |resource_node|
26
- resource_node.file(:href => "#{identifier}.xml")
29
+ if @external_link
30
+ resources_node.resource(
31
+ :type => WEB_LINK,
32
+ :identifier => identifier
33
+ ) do |resource_node|
34
+ resource_node.file(:href => href)
35
+ end
36
+ else
37
+ resources_node.resource(
38
+ :type => WEBCONTENT,
39
+ :href => href,
40
+ :identifier => identifier
41
+ ) do |resource_node|
42
+ resource_node.file(:href => href)
43
+ end
27
44
  end
28
45
  end
29
46
 
@@ -32,6 +49,7 @@ module Moodle2CC::CC
32
49
  end
33
50
 
34
51
  def create_xml(export_dir)
52
+ return unless @external_link
35
53
  path = File.join(export_dir, "#{identifier}.xml")
36
54
  FileUtils.mkdir_p(File.dirname(path))
37
55
  File.open(path, 'w') do |file|
@@ -9,9 +9,10 @@ class Moodle2CC::ResourceFactory
9
9
  when 'assignment', 'workshop'
10
10
  @namespace.const_get(:Assignment).new(mod, position)
11
11
  when 'resource'
12
- if mod.type == 'file'
12
+ case mod.type
13
+ when 'file'
13
14
  @namespace.const_get(:WebLink).new(mod)
14
- elsif mod.type == 'html'
15
+ when 'html', 'text'
15
16
  @namespace.const_get(:WebContent).new(mod)
16
17
  end
17
18
  when 'forum'
@@ -1,3 +1,3 @@
1
1
  module Moodle2CC
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -156,12 +156,14 @@ class TestUnitCCConverter < MiniTest::Unit::TestCase
156
156
  assert_equal 'webcontent', resource.attributes['type'].value
157
157
  assert_equal 'web_resources/folder/test.txt', resource.attributes['href'].value
158
158
  assert resource.xpath('xmlns:file[@href="web_resources/folder/test.txt"]').first
159
+ assert_equal 1, xml.search('file[href="web_resources/folder/test.txt"]').count
159
160
 
160
161
  resource = xml.xpath('//xmlns:manifest/xmlns:resources/xmlns:resource[@identifier="ib98bb8ec201a97840ae4ed4bb40207c0"]').first
161
162
  assert resource, 'resources does not exist for "web_resources/test.txt" file'
162
163
  assert_equal 'webcontent', resource.attributes['type'].value
163
164
  assert_equal 'web_resources/test.txt', resource.attributes['href'].value
164
165
  assert resource.xpath('xmlns:file[@href="web_resources/test.txt"]').first
166
+ assert_equal 1, xml.search('file[href="web_resources/test.txt"]').count
165
167
  end
166
168
 
167
169
  def test_it_deletes_all_files_except_imscc
@@ -43,14 +43,42 @@ class TestUnitCCWebLink < MiniTest::Unit::TestCase
43
43
  assert_equal "http://en.wikipedia.org/wiki/Einstein", web_link.url
44
44
  end
45
45
 
46
- def test_it_has_an_identifier
46
+ def test_it_converts_external_link
47
+ @mod.reference = "http://en.wikipedia.org/wiki/Einstein"
48
+ web_link = Moodle2CC::CC::WebLink.new @mod
49
+ assert_equal true, web_link.external_link
50
+
51
+ @mod.reference = "files/myfile.txt"
52
+ web_link = Moodle2CC::CC::WebLink.new @mod
53
+ assert_equal false, web_link.external_link
54
+ end
55
+
56
+ def test_if_converts_href
57
+ @mod.reference = "http://en.wikipedia.org/wiki/Einstein"
58
+ web_link = Moodle2CC::CC::WebLink.new @mod
59
+ assert_equal "#{web_link.identifier}.xml", web_link.href
60
+
61
+ @mod.reference = "files/myfile.txt"
62
+ web_link = Moodle2CC::CC::WebLink.new @mod
63
+ assert_equal 'web_resources/files/myfile.txt', web_link.href
64
+ end
65
+
66
+ def test_it_has_an_identifier_for_external_link
47
67
  @mod.id = 123
48
68
 
49
69
  web_link = Moodle2CC::CC::WebLink.new @mod
50
70
  assert_equal 'iecc4b622fbc1adf8b8a2085e0974ac7d', web_link.identifier
51
71
  end
52
72
 
53
- def test_it_creates_resource_in_imsmanifest
73
+ def test_it_has_an_identifier_for_local_file
74
+ @mod.reference = "files/myfile.txt"
75
+
76
+ web_link = Moodle2CC::CC::WebLink.new @mod
77
+ assert_equal 'i5cfabae035bff8857137d83c2a067809', web_link.identifier
78
+ end
79
+
80
+ def test_it_creates_resource_in_imsmanifest_for_external_link
81
+ @mod.reference = "http://en.wikipedia.org/wiki/Einstein"
54
82
  web_link = Moodle2CC::CC::WebLink.new @mod
55
83
  node = Builder::XmlMarkup.new
56
84
  xml = Nokogiri::XML(web_link.create_resource_node(node))
@@ -64,6 +92,22 @@ class TestUnitCCWebLink < MiniTest::Unit::TestCase
64
92
  assert file
65
93
  end
66
94
 
95
+ def test_it_creates_resource_in_imsmanifest_for_local_file
96
+ @mod.reference = "files/myfile.txt"
97
+ web_link = Moodle2CC::CC::WebLink.new @mod
98
+ node = Builder::XmlMarkup.new
99
+ xml = Nokogiri::XML(web_link.create_resource_node(node))
100
+
101
+ resource = xml.xpath('resource').first
102
+ assert resource
103
+ assert_equal 'webcontent', resource.attributes['type'].value
104
+ assert_equal 'web_resources/files/myfile.txt', resource.attributes['href'].value
105
+ assert_equal 'i5cfabae035bff8857137d83c2a067809', resource.attributes['identifier'].value
106
+
107
+ file = resource.xpath('file[@href="web_resources/files/myfile.txt"]').first
108
+ assert file
109
+ end
110
+
67
111
  def test_it_creates_xml
68
112
  @mod.id = 123
69
113
  @mod.name = "About Your Instructor"
@@ -83,4 +127,13 @@ class TestUnitCCWebLink < MiniTest::Unit::TestCase
83
127
  assert_equal "About Your Instructor", xml.xpath('xmlns:webLink/xmlns:title').text
84
128
  assert xml.xpath('xmlns:webLink/xmlns:url[@href="http://en.wikipedia.org/wiki/Einstein"]').first
85
129
  end
130
+
131
+ def test_it_does_not_create_xml_for_local_files
132
+ @mod.reference = "files/myfile.txt"
133
+
134
+ tmp_dir = File.expand_path('../../../tmp', __FILE__)
135
+ web_link = Moodle2CC::CC::WebLink.new @mod
136
+ web_link.create_xml(tmp_dir)
137
+ refute File.exists?(File.join(tmp_dir, "#{web_link.identifier}.xml")), 'xml file was created for local file'
138
+ end
86
139
  end
@@ -42,7 +42,17 @@ class TestUnitCCResource < MiniTest::Unit::TestCase
42
42
  assert_kind_of Moodle2CC::Canvas::DiscussionTopic, resource
43
43
  end
44
44
 
45
- def test_it_can_get_web_content_resource
45
+ def test_it_can_get_web_content_resource_from_text_resource
46
+ mod = @backup.course.mods.find { |m| m.mod_type == "resource" }
47
+ mod.type = 'text'
48
+ resource = @cc_factory.get_resource_from_mod(mod)
49
+ assert_kind_of Moodle2CC::CC::WebContent, resource
50
+
51
+ resource = @canvas_factory.get_resource_from_mod(mod)
52
+ assert_kind_of Moodle2CC::Canvas::WebContent, resource
53
+ end
54
+
55
+ def test_it_can_get_web_content_resource_from_html_resource
46
56
  mod = @backup.course.mods.find { |m| m.mod_type == "resource" }
47
57
  mod.type = 'html'
48
58
  resource = @cc_factory.get_resource_from_mod(mod)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moodle2cc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christopher Durtschi
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2012-08-03 00:00:00 Z
20
+ date: 2012-08-06 00:00:00 Z
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
23
  name: rubyzip