Skip to content

Commit

Permalink
Get the framework details from the XML and remove them from the form.
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed May 1, 2016
1 parent 5ddc46b commit f78a6f0
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 17 deletions.
48 changes: 39 additions & 9 deletions classes/form/import.php
Expand Up @@ -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) {
Expand All @@ -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;
}

}
87 changes: 85 additions & 2 deletions classes/framework_importer.php
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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 .= '<br/>' . get_string('subject', 'tool_lpimportrdf', $this->framework->subject);
}

$elements = $doc->getElementsByTagName('Statement');
$records = array();
foreach ($elements as $element) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -246,4 +322,11 @@ public function import_to_framework($framework) {
}
return true;
}

/**
* @param \competency\competency_framework
* @return boolean
*/
public function import_to_framework($framework) {
}
}
11 changes: 5 additions & 6 deletions index.php
Expand Up @@ -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'));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_lpimportrdf.php
Expand Up @@ -27,3 +27,4 @@
$string['invalidimportfile'] = 'File format is invalid.';
$string['subject'] = 'Subject: {$a}';
$string['educationlevel'] = 'Education Level: {$a}';
$string['import'] = 'Import';

0 comments on commit f78a6f0

Please sign in to comment.