From f78a6f09033875c706bad77314d65a3938db6d45 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Sun, 1 May 2016 11:23:52 +0800 Subject: [PATCH] Get the framework details from the XML and remove them from the form. --- classes/form/import.php | 48 +++++++++++++++---- classes/framework_importer.php | 87 +++++++++++++++++++++++++++++++++- index.php | 11 ++--- lang/en/tool_lpimportrdf.php | 1 + 4 files changed, 130 insertions(+), 17 deletions(-) diff --git a/classes/form/import.php b/classes/form/import.php index d24bece..dac1866 100644 --- a/classes/form/import.php +++ b/classes/form/import.php @@ -38,23 +38,36 @@ * @copyright 2015 Damyon Wiese * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class import extends \tool_lp\form\competency_framework { +class import extends moodleform { /** * Define the form - called by parent constructor */ - public function definition_after_data() { + public function definition() { + global $PAGE; + $mform = $this->_form; $element = $mform->createElement('filepicker', 'importfile', get_string('importfile', 'tool_lpimportrdf')); - $mform->insertElementBefore($element, 'idnumber'); + $mform->addElement($element); $mform->addRule('importfile', null, 'required'); - } - public function get_submitted_data() { - // The field importfile mucks up the default validation from the parent form. - $data = parent::get_submitted_data(); - unset($data->importfile); - return $data; + $scales = get_scales_menu(); + $scaleid = $mform->addElement('select', 'scaleid', get_string('scale', 'tool_lp'), $scales); + $mform->setType('scaleid', PARAM_INT); + $mform->addHelpButton('scaleid', 'scale', 'tool_lp'); + $mform->addRule('scaleid', null, 'required', null, 'client'); + $mform->addElement('button', 'scaleconfigbutton', get_string('configurescale', 'tool_lp')); + // Add js. + $mform->addElement('hidden', 'scaleconfiguration', '', array('id' => 'tool_lp_scaleconfiguration')); + $mform->setType('scaleconfiguration', PARAM_RAW); + $PAGE->requires->js_call_amd('tool_lp/scaleconfig', 'init', array('#id_scaleid', + '#tool_lp_scaleconfiguration', '#id_scaleconfigbutton')); + + $mform->addElement('selectyesno', 'visible', + get_string('visible', 'tool_lp')); + $mform->setDefault('visible', true); + $mform->addHelpButton('visible', 'visible', 'tool_lp'); + $this->add_action_buttons(false, get_string('import', 'tool_lpimportrdf')); } public function set_import_error($msg) { @@ -63,4 +76,21 @@ public function set_import_error($msg) { $mform->setElementError('importfile', $msg); } + /** + * Extra validation. + * + * @param stdClass $data Data to validate. + * @param array $files Array of files. + * @param array $errors Currently reported errors. + */ + public function validation($data, $files) { + $errors = parent::validation($data, $files); + // Move the error from scaleconfiguration to the form element scale ID. + if (isset($errors['scaleconfiguration']) && !isset($errors['scaleid'])) { + $errors['scaleid'] = $errors['scaleconfiguration']; + unset($errors['scaleconfiguration']); + } + return $errors; + } + } diff --git a/classes/framework_importer.php b/classes/framework_importer.php index 158d4b5..cff8c2b 100644 --- a/classes/framework_importer.php +++ b/classes/framework_importer.php @@ -26,7 +26,9 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); +use context_system; use core_competency\api; +use core_competency\invalid_persistent_exception; use DOMDocument; use stdClass; @@ -45,6 +47,9 @@ class framework_importer { /** @var array $tree The competencies tree */ var $tree = array(); + /** @var stdClass The framework node */ + var $framework = null; + public function fail($msg) { $this->error = $msg; return false; @@ -59,6 +64,42 @@ public function __construct($xml) { $this->fail(get_string('invalidimportfile', 'tool_lpimportrdf')); return; } + + $this->framework = new stdClass(); + + $elements = $doc->getElementsByTagName('StandardDocument'); + foreach ($elements as $element) { + // Get the idnumber. + $attr = $element->attributes->getNamedItem('about'); + if (!$attr) { + $this->fail(get_string('invalidimportfile', 'tool_lpimportrdf')); + return; + } + $parts = explode('/', $attr->nodeValue); + $this->framework->idnumber = array_pop($parts); + $this->framework->shortname = $this->framework->idnumber; + + foreach ($element->childNodes as $child) { + if ($child->localName == 'description') { + $this->framework->description = $child->nodeValue; + } else if ($child->localName == 'title') { + $this->framework->shortname = $child->nodeValue; + } else if ($child->localName == 'subject') { + // Get the resource attribute. + $attr = $child->attributes->getNamedItem('resource'); + if ($attr) { + $parts = explode('/', $attr->nodeValue); + $this->framework->subject = array_pop($parts); + } + } + } + break; + } + + if ($this->framework->subject) { + $this->framework->description .= '
' . get_string('subject', 'tool_lpimportrdf', $this->framework->subject); + } + $elements = $doc->getElementsByTagName('Statement'); $records = array(); foreach ($elements as $element) { @@ -233,11 +274,46 @@ public function set_related_competencies($record) { } } + private function create_framework($scaleid, $scaleconfiguration, $visible) { + $framework = false; + + $record = new stdClass(); + $record->shortname = $this->framework->shortname; + $record->idnumber = $this->framework->idnumber; + $record->description = $this->framework->description; + $record->descriptionformat = FORMAT_HTML; + $record->scaleid = $scaleid; + $record->scaleconfiguration = $scaleconfiguration; + $record->visible = $visible; + $record->contextid = context_system::instance()->id; + + $taxdefaults = array(); + $taxcount = 4; + for ($i = 1; $i <= $taxcount; $i++) { + $taxdefaults[$i] = \core_competency\competency_framework::TAXONOMY_COMPETENCY; + } + $record->taxonomies = $taxdefaults; + + try { + $framework = api::create_framework($record); + } catch (invalid_persistent_exception $ip) { + return $this->fail($ip->getDescription()); + } + + return $framework; + } + /** - * @param \competency\competency_framework + * @param \stdClass containing scaleconfig * @return boolean */ - public function import_to_framework($framework) { + public function import($data) { + + $framework = $this->create_framework($data->scaleid, $data->scaleconfiguration, $data->visible); + if (!$framework) { + return false; + } + foreach ($this->tree as $record) { $this->create_competency(null, $record, $framework); } @@ -246,4 +322,11 @@ public function import_to_framework($framework) { } return true; } + + /** + * @param \competency\competency_framework + * @return boolean + */ + public function import_to_framework($framework) { + } } diff --git a/index.php b/index.php index 0a76138..166f632 100644 --- a/index.php +++ b/index.php @@ -51,14 +51,13 @@ unset($data->importfile); - $framework = \core_competency\api::create_framework($data); - - if ($framework) { - $importer->import_to_framework($framework); + $framework = $importer->import($data); + $error = $importer->get_error(); + if ($error) { + $form->set_import_error($error); + } else { redirect(new moodle_url('continue.php', array('id' => $framework->get_id()))); die(); - } else { - $form->set_import_error(get_string('invalidpersistent', 'core_competency')); } } } diff --git a/lang/en/tool_lpimportrdf.php b/lang/en/tool_lpimportrdf.php index 114a3af..665c10f 100644 --- a/lang/en/tool_lpimportrdf.php +++ b/lang/en/tool_lpimportrdf.php @@ -27,3 +27,4 @@ $string['invalidimportfile'] = 'File format is invalid.'; $string['subject'] = 'Subject: {$a}'; $string['educationlevel'] = 'Education Level: {$a}'; +$string['import'] = 'Import';