|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require 'nokogiri' |
4 | | - |
5 | 3 | class Exercise < ApplicationRecord |
6 | 4 | include Context |
7 | 5 | include Creation |
@@ -474,54 +472,6 @@ def duplicate(attributes = {}) |
474 | 472 | exercise |
475 | 473 | end |
476 | 474 |
|
477 | | - def determine_file_role_from_proforma_file(task_node, file_node) |
478 | | - file_id = file_node.xpath('@id') |
479 | | - file_class = file_node.xpath('@class').first.value |
480 | | - comment = file_node.xpath('@comment').first.value |
481 | | - is_referenced_by_test = task_node.xpath("p:tests/p:test/p:filerefs/p:fileref[@id=#{file_id}]") |
482 | | - is_referenced_by_model_solution = task_node.xpath("p:model-solutions/p:model-solution/p:filerefs/p:fileref[@id=#{file_id}]") |
483 | | - if is_referenced_by_test && (file_class == 'internal') |
484 | | - return 'teacher_defined_test' |
485 | | - elsif is_referenced_by_model_solution && (file_class == 'internal') |
486 | | - return 'reference_implementation' |
487 | | - elsif (file_class == 'template') && (comment == 'main') |
488 | | - return 'main_file' |
489 | | - end |
490 | | - |
491 | | - 'regular_file' |
492 | | - end |
493 | | - |
494 | | - def from_proforma_xml(xml_string) |
495 | | - # how to extract the proforma functionality into a different module in rails? |
496 | | - xml = Nokogiri::XML(xml_string) |
497 | | - xml.collect_namespaces |
498 | | - task_node = xml.xpath('/root/p:task') |
499 | | - description = task_node.xpath('p:description/text()')[0].content |
500 | | - self.attributes = { |
501 | | - title: task_node.xpath('p:meta-data/p:title/text()')[0].content, |
502 | | - description:, |
503 | | - instructions: description, |
504 | | - } |
505 | | - task_node.xpath('p:files/p:file').all? do |file| |
506 | | - file_name_split = file.xpath('@filename').first.value.split('.') |
507 | | - file_class = file.xpath('@class').first.value |
508 | | - role = determine_file_role_from_proforma_file(task_node, file) |
509 | | - feedback_message_nodes = task_node.xpath('p:tests/p:test/p:test-configuration/c:feedback-message/text()') |
510 | | - files.build({ |
511 | | - name: file_name_split.first, |
512 | | - content: file.xpath('text()').first.content, |
513 | | - read_only: false, |
514 | | - hidden: file_class == 'internal', |
515 | | - role:, |
516 | | - feedback_message: role == 'teacher_defined_test' ? feedback_message_nodes.first.content : nil, |
517 | | - file_type: FileType.find_by( |
518 | | - file_extension: ".#{file_name_split.second}" |
519 | | - ), |
520 | | - }) |
521 | | - end |
522 | | - self.execution_environment_id = 1 |
523 | | - end |
524 | | - |
525 | 475 | def generate_token |
526 | 476 | self.token ||= SecureRandom.hex(4) |
527 | 477 | end |
|
0 commit comments