Skip to content

Commit

Permalink
MDL-51024 tool_lpmigrate: Tool to migrate between frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Apr 18, 2016
1 parent e43f6aa commit 057c140
Show file tree
Hide file tree
Showing 12 changed files with 2,101 additions and 0 deletions.
96 changes: 96 additions & 0 deletions admin/tool/lpmigrate/classes/form/migrate_framework.php
@@ -0,0 +1,96 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Form.
*
* @package tool_lpmigrate
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_lpmigrate\form;
defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/formslib.php');
\MoodleQuickForm::registerElementType('framework_autocomplete',
$CFG->dirroot . '/admin/tool/lp/classes/form/framework_autocomplete.php',
'\\tool_lp\\form\\framework_autocomplete');

/**
* Form class.
*
* @package tool_lpmigrate
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class migrate_framework extends \moodleform {

protected $pagecontext;

public function __construct(\context $context) {
$this->pagecontext = $context;
parent::__construct();
}

protected function definition() {
$mform = $this->_form;

$mform->addElement('header', 'hdrcourses', get_string('frameworks', 'tool_lpmigrate'));

$mform->addElement('framework_autocomplete', 'from', get_string('migratefrom', 'tool_lpmigrate'), array(
'contextid' => $this->pagecontext->id,
'onlyvisible' => '0',
), 1, 2, 3);
$mform->addRule('from', get_string('required'), 'required', null);
$mform->addHelpButton('from', 'migratefrom', 'tool_lpmigrate');

$mform->addElement('framework_autocomplete', 'to', get_string('migrateto', 'tool_lpmigrate'), array(
'contextid' => $this->pagecontext->id,
'onlyvisible' => '1', // We cannot add competencies from hidden frameworks, so it must be visible.
), 1, 2, 3);
$mform->addRule('to', get_string('required'), 'required', null);
$mform->addHelpButton('to', 'migrateto', 'tool_lpmigrate');

$mform->addElement('header', 'hdrcourses', get_string('courses'));
$mform->addElement('course', 'allowedcourses', get_string('limittothese', 'tool_lpmigrate'), array('showhidden' => true, 'multiple' => true));
$mform->addHelpButton('allowedcourses', 'allowedcourses', 'tool_lpmigrate');
$mform->addElement('course', 'disallowedcourses', get_string('excludethese', 'tool_lpmigrate'), array('showhidden' => true, 'multiple' => true));
$mform->addHelpButton('disallowedcourses', 'disallowedcourses', 'tool_lpmigrate');
$mform->addElement('date_time_selector', 'coursestartdate', get_string('startdatefrom', 'tool_lpmigrate'), array('optional' => true));
$mform->addHelpButton('coursestartdate', 'coursestartdate', 'tool_lpmigrate');

$this->add_action_buttons(true, get_string('performmigration', 'tool_lpmigrate'));
}

public function validation($data, $files) {
$errors = array();

if ($data['from'] == $data['to']) {
$errors['to'] = get_string('errorcannotmigratetosameframework', 'tool_lpmigrate');

} else if (!empty($data['from']) && !empty($data['to'])) {
$mapper = new \tool_lpmigrate\framework_mapper($data['from'], $data['to']);
$mapper->automap();
if (!$mapper->has_mappings()) {
$errors['to'] = 'Could not map to any competency in this framework.';
}
}

return $errors;
}

}
189 changes: 189 additions & 0 deletions admin/tool/lpmigrate/classes/framework_mapper.php
@@ -0,0 +1,189 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Framework mapper.
*
* @package tool_lpmigrate
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_lpmigrate;
defined('MOODLE_INTERNAL') || die();

use tool_lp\api;

/**
* Framework mapper class.
*
* @package tool_lpmigrate
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class framework_mapper {

/** @var int The ID of the framework we're migrating from. */
protected $from;
/** @var int The ID of the framework we're migrating to. */
protected $to;
/** @var array The collection of objects at origin. */
protected $collectionfrom;
/** @var array The collection of objects at destination. */
protected $collectionto;
/** @var array Mappings. */
protected $mappings = array();

/**
* Constructor.
* @param int $from Framework ID from.
* @param int $to Framework ID to.
*/
public function __construct($from, $to) {
$this->from = $from;
$this->to = $to;
}

/**
* Add a mapping.
* @param int $idfrom From ID.
* @param int $idto To ID.
*/
public function add_mapping($idfrom, $idto) {
$this->mappings[$idfrom] = $idto;
}

/**
* Auto map the frameworks.
* @return void
*/
public function automap() {
$map = array();

// Shallow copy.
$collectionfrom = $this->get_collection_from();
$collectionto = $this->get_collection_to();

// Find mappings.
foreach ($collectionfrom as $keyfrom => $compfrom) {
foreach ($collectionto as $keyto => $compto) {
if ($compfrom->get_idnumber() == $compto->get_idnumber()) {
$map[$compfrom->get_id()] = $compto->get_id();
unset($collectionfrom[$keyfrom]);
unset($collectionto[$keyto]);
break;
}
}
}

$this->mappings = $map;
}

/**
* Get all IDs at origin.
* @return array
*/
public function get_all_from() {
return array_keys($this->get_collection_from());
}

/**
* Get all IDs at destination.
* @return array
*/
public function get_all_to() {
return array_keys($this->get_collection_to());
}

/**
* Get the collection at origin.
* @return array
*/
protected function get_collection_from() {
if ($this->collectionfrom === null) {
$this->collectionfrom = api::search_competencies('', $this->from);
}
return $this->collectionfrom;
}

/**
* Get the collection at destination.
* @return array
*/
protected function get_collection_to() {
if ($this->collectionto === null) {
$this->collectionto = api::search_competencies('', $this->to);
}
return $this->collectionto;
}

/**
* Get the defined mappings.
* @return array
*/
public function get_mappings() {
return $this->mappings;
}

/**
* Get the IDs of the objects at origin which do not have a mapping at destination.
* @return array
*/
public function get_unmapped_from() {
return array_keys(array_diff_key($this->get_collection_from(), $this->mappings));
}

/**
* Get the origin objects with missing mappings.
* @return array
*/
public function get_unmapped_objects_from() {
return array_diff_key($this->get_collection_from(), $this->mappings);
}

/**
* Get the IDs of the objects at destination which do not have a mapping at origin.
* @return array
*/
public function get_unmapped_to() {
return array_keys(array_diff_key($this->get_collection_to(), array_flip($this->mappings)));
}

/**
* Get the destination objects with missing mappings.
* @return array
*/
public function get_unmapped_objects_to() {
return array_diff_key($this->get_collection_to(), array_flip($this->mappings));
}

/**
* Whether some mappings were set.
* @return bool
*/
public function has_mappings() {
return !empty($this->mappings);
}

/**
* Reset the mappings.
* @return void
*/
public function reset_mappings() {
$this->mappings = array();
}

}

0 comments on commit 057c140

Please sign in to comment.