Module: Moodle2CC::Moodle2Converter::ConverterHelper
- Included in:
- AssessmentConverter, AssignmentConverter, BookConverter, CourseConverter, DiscussionConverter, FeedbackConverter, FileConverter, FolderConverter, GlossaryConverter, HtmlConverter, LabelConverter, Migrator, PageConverter, QuestionBankConverter, QuestionConverters::QuestionConverter, QuestionnaireConverter, SectionConverter, WikiConverter
- Defined in:
- lib/moodle2cc/moodle2converter/converter_helper.rb
Constant Summary collapse
- INTRO_SUFFIX =
'_book_intro'
- CHAPTER_SUFFIX =
'_chapter'
- FOLDER_SUFFIX =
'_folder'
- PAGE_SUFFIX =
'_page'
- ASSESSMENT_SUFFIX =
'_assessment'
- CHOICE_ASSESSMENT_SUFFIX =
'_choice_assessment'
- FEEDBACK_ASSESSMENT_SUFFIX =
'_feedback_assessment'
- QUESTIONNAIRE_ASSESSMENT_SUFFIX =
'_questionnaire_assessment'
- ASSIGNMENT_SUFFIX =
'_assignment'
- COURSE_SUFFIX =
'_course'
- DISCUSSION_SUFFIX =
'_discussion'
- FILE_SUFFIX =
'_file'
- QUESTION_BANK_SUFFIX =
'_question_bank'
- MODULE_SUFFIX =
'_module'
- GLOSSARY_SUFFIX =
'_glossary'
- SUMMARY_PAGE_SUFFIX =
'_summary_page'
- EXTERNAL_URL_SUFFIX =
'_external_url'
- LTI_SUFFIX =
'_lti'
- ACTIVITY_LOOKUP =
{ Moodle2::Models::Page => {suffix: PAGE_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_WIKI_PAGE}, Moodle2::Models::Wiki => {suffix: PAGE_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_WIKI_PAGE}, Moodle2::Models::Assignment => {suffix: ASSIGNMENT_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_ASSIGNMENT}, Moodle2::Models::Folder => {suffix: FOLDER_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_WIKI_PAGE}, Moodle2::Models::Forum => {suffix: DISCUSSION_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_DISCUSSION_TOPIC}, Moodle2::Models::BookChapter => {suffix: CHAPTER_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_WIKI_PAGE}, Moodle2::Models::Quizzes::Quiz => {suffix: ASSESSMENT_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_QUIZ}, Moodle2::Models::Choice => {suffix: CHOICE_ASSESSMENT_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_QUIZ}, Moodle2::Models::Feedback => {suffix: FEEDBACK_ASSESSMENT_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_QUIZ}, Moodle2::Models::Questionnaire => {suffix: QUESTIONNAIRE_ASSESSMENT_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_QUIZ}, Moodle2::Models::Section => {suffix: SUMMARY_PAGE_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_WIKI_PAGE}, Moodle2::Models::Label => {suffix: nil, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_CONTEXT_MODULE_SUB_HEADER}, Moodle2::Models::Glossary => {suffix: GLOSSARY_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_WIKI_PAGE}, Moodle2::Models::ExternalUrl => {suffix: EXTERNAL_URL_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_EXTERNAL_URL}, Moodle2::Models::Resource => {suffix: nil, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_ATTACHMENT}, Moodle2::Models::Lti => {suffix: LTI_SUFFIX, content_type: CanvasCC::Models::ModuleItem::CONTENT_TYPE_CONTEXT_EXTERNAL_TOOL} }
- MAX_TITLE_LENGTH =
250
Instance Method Summary collapse
- #activity_content_type(activity) ⇒ Object
- #generate_unique_identifier ⇒ Object
- #generate_unique_identifier_for(id, suffix = nil) ⇒ Object
- #generate_unique_identifier_for_activity(activity) ⇒ Object
- #generate_unique_resource_path(base_path, readable_name, file_extension = 'html') ⇒ Object
- #get_unique_identifier_for_activity(activity) ⇒ Object
- #truncate_text(text, max_length = nil, ellipsis = '...') ⇒ Object
- #workflow_state(moodle_visibility) ⇒ Object
Instance Method Details
#activity_content_type(activity) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/moodle2cc/moodle2converter/converter_helper.rb', line 90 def activity_content_type(activity) if lookup = ACTIVITY_LOOKUP[activity.class] lookup[:content_type] else raise "Unknown activity type: #{activity.class}" end end |
#generate_unique_identifier ⇒ Object
49 50 51 |
# File 'lib/moodle2cc/moodle2converter/converter_helper.rb', line 49 def generate_unique_identifier "m2#{SecureRandom.uuid.gsub('-', '')}" end |
#generate_unique_identifier_for(id, suffix = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/moodle2cc/moodle2converter/converter_helper.rb', line 73 def generate_unique_identifier_for(id, suffix = nil) unique_id = "m2#{Digest::MD5.hexdigest(id.to_s)}#{suffix}" id_set = Moodle2Converter::Migrator.unique_id_set if id_set.include?(unique_id) # i was under the impression that moodle ids would be unique themselves # but i have been apparently misinformed original_id = unique_id index = 0 while id_set.include?(unique_id) index += 1 unique_id = "#{original_id}#{index}" end end id_set << unique_id unique_id end |
#generate_unique_identifier_for_activity(activity) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/moodle2cc/moodle2converter/converter_helper.rb', line 63 def generate_unique_identifier_for_activity(activity) if lookup = ACTIVITY_LOOKUP[activity.class] unique_id = generate_unique_identifier_for(activity.id, lookup[:suffix]) Moodle2Converter::Migrator.activity_id_map[activity.hash] = unique_id unique_id else raise "Unknown activity type: #{activity.class}" end end |
#generate_unique_resource_path(base_path, readable_name, file_extension = 'html') ⇒ Object
43 44 45 46 47 |
# File 'lib/moodle2cc/moodle2converter/converter_helper.rb', line 43 def generate_unique_resource_path(base_path, readable_name, file_extension = 'html') file_name_suffix = readable_name ? Moodle2CC::CanvasCC::Models::Page.convert_name_to_url(readable_name) : '' ext = file_extension ? ".#{file_extension}" : '' File.join(base_path, generate_unique_identifier(), "#{file_name_suffix}#{ext}") end |
#get_unique_identifier_for_activity(activity) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/moodle2cc/moodle2converter/converter_helper.rb', line 53 def get_unique_identifier_for_activity(activity) # use when we want to retrieve an existing id, not generate a new one id = Moodle2Converter::Migrator.activity_id_map[activity.hash] unless id puts "could not find matching id for #{activity.inspect}" id = generate_unique_identifier_for_activity(activity) end id end |
#truncate_text(text, max_length = nil, ellipsis = '...') ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/moodle2cc/moodle2converter/converter_helper.rb', line 102 def truncate_text(text, max_length = nil, ellipsis = '...') max_length ||= MAX_TITLE_LENGTH return text if !text || text.length <= max_length actual_length = max_length - ellipsis.length # First truncate the text down to the bytes max, then lop off any invalid # unicode characters at the end. truncated = text[0,actual_length][/.{0,#{actual_length}}/mu] truncated + ellipsis end |
#workflow_state(moodle_visibility) ⇒ Object
98 99 100 |
# File 'lib/moodle2cc/moodle2converter/converter_helper.rb', line 98 def workflow_state(moodle_visibility) moodle_visibility ? CanvasCC::Models::WorkflowState::ACTIVE : CanvasCC::Models::WorkflowState::UNPUBLISHED end |