Commit 057c1403 authored by Frederic Massart's avatar Frederic Massart
Browse files

MDL-51024 tool_lpmigrate: Tool to migrate between frameworks

parent e43f6aa7
<?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;
}
}
<?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();
}
}
This diff is collapsed.
<?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/>.
/**
* Migrate frameworks results.
*
* @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\output;
defined('MOODLE_INTERNAL') || die();
use context;
use context_course;
use context_module;
use moodle_url;
use renderable;
use templatable;
use stdClass;
use tool_lp\competency;
use tool_lp\competency_framework;
use tool_lpmigrate\framework_processor;
/**
* Migrate frameworks results 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_results implements renderable, templatable {
/** @var context The current page context. */
protected $pagecontext;
protected $processor;
/**
* Construct.
*
* @param context $pagecontext The current page context.
* @param framework_processor $processor The processor.
* @param competency_framework $frameworkfrom Framework from.
* @param competency_framework $frameworkto Framework to.
* @param array $unmappedfrom Competencies from unmapped.
* @param array $unmappedto Competencies to unmapped.
*/
public function __construct(context $pagecontext, framework_processor $processor, competency_framework $frameworkfrom,
competency_framework $frameworkto, array $unmappedfrom = array(), array $unmappedto = array()) {
if (!$processor->has_run()) {
throw new \coding_exception('The processor has not run.');
}
$this->pagecontext = $pagecontext;
$this->processor = $processor;
$this->unmappedfrom = $unmappedfrom;
$this->unmappedto = $unmappedto;
$this->frameworkfrom = $frameworkfrom;
$this->frameworkto = $frameworkto;
}
/**
* Export the data.
*
* @param renderer_base $output
* @return stdClass
*/
public function export_for_template(\renderer_base $output) {
global $DB;
$data = new stdClass();
$missingmappings = $this->processor->get_missing_mappings();
$data->pagecontextid = $this->pagecontext->id;
$data->expectedccmigrations = $this->processor->get_expected_course_competency_migrations();
$data->expectedmcmigrations = $this->processor->get_expected_module_competency_migrations();
$data->ccmigrationscount = $this->processor->get_course_competency_migrations();
$data->mcmigrationscount = $this->processor->get_module_competency_migrations();
$data->ccremovalscount = $this->processor->get_course_competency_removals();
$data->mcremovalscount = $this->processor->get_module_competency_removals();
$data->unmappedfrom = array();
$data->unmappedto = array();
$exporter = new \tool_lp\external\competency_framework_exporter($this->frameworkfrom);
$data->frameworkfrom = $exporter->export($output);
$exporter = new \tool_lp\external\competency_framework_exporter($this->frameworkto);
$data->frameworkto = $exporter->export($output);
$fromcontext = $this->frameworkfrom->get_context();
$tocontext = $this->frameworkto->get_context();
$compcontext = null;
foreach ($this->unmappedfrom as $comp) {
$exporter = new \tool_lp\external\competency_exporter($comp, array('context' => $fromcontext));
$data->unmappedfrom[] = $exporter->export($output);
}
foreach ($this->unmappedto as $comp) {
$exporter = new \tool_lp\external\competency_exporter($comp, array('context' => $tocontext));
$data->unmappedto[] = $exporter->export($output);
}
$data->coursesfound = $this->processor->get_courses_found_count();
$data->cmsfound = $this->processor->get_cms_found_count();
$data->mappingsmissingcount = count($missingmappings);
$data->hasunmappedto = count($data->unmappedto) > 0;
$data->hasunmappedfrom = count($data->unmappedfrom) > 0;
$warnings = $this->processor->get_warnings();
$data->warnings = array();
$data->warningcount = count($warnings);
$errors = $this->processor->get_errors();
$data->errors = array();
$data->errorcount = count($errors);
foreach ($warnings as $warning) {
$cmcontext = !empty($warning['cmid']) ? context_module::instance($warning['cmid']) : null;
$coursecontext = context_course::instance($warning['courseid']);
$warning['cm'] = $cmcontext ? $cmcontext->get_context_name() : null;
$warning['course'] = $coursecontext->get_context_name();
$warning['competency'] = $DB->get_field(competency::TABLE, 'idnumber', array('id' => $warning['competencyid']));
$data->warnings[] = $warning;
}
foreach ($errors as $error) {
$cmcontext = !empty($error['cmid']) ? context_module::instance($error['cmid']) : null;
$coursecontext = context_course::instance($error['courseid']);
$error['cm'] = $cmcontext ? $cmcontext->get_context_name() : null;
$error['course'] = $coursecontext->get_context_name();
$error['competency'] = $DB->get_field(competency::TABLE, 'idnumber', array('id' => $error['competencyid']));
$data->errors[] = $error;
}
$data->pluginbaseurl = (new moodle_url('/admin/tool/lpmigrate'))->out(false);
$data->cbebaseurl = (new moodle_url('/admin/tool/lp'))->out(false);
return $data;
}
}
<?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/>.
/**
* Renderers.
*
* @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\output;
defined('MOODLE_INTERNAL') || die();
use plugin_renderer_base;
use renderable;
/**
* Renderer 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 renderer extends plugin_renderer_base {
/**
* Defer to template.
*
* @param renderable $page
* @return string
*/
public function render_migrate_framework_results(migrate_framework_results $page) {
$data = $page->export_for_template($this);
return parent::render_from_template('tool_lpmigrate/migrate_frameworks_results', $data);
}
}
<?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/>.
/**
* Capabilities.
*
* @package tool_lpmigrate
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
'tool/lpmigrate:frameworksmigrate' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/site:config'
),
);
<?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/>.
/**
* Page to migrate frameworks.
*
* @package tool_lpmigrate
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../../config.php');
$context = context_system::instance();
require_login(null, false);
require_capability('tool/lpmigrate:frameworksmigrate', $context);
$url = new moodle_url('/admin/tool/lpmigrate/frameworks.php');
$title = get_string('migrateframeworks', 'tool_lpmigrate');
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');
$PAGE->set_url($url);
$PAGE->set_title($title);
$PAGE->set_heading(get_string('pluginname', 'tool_lpmigrate'));
$output = $PAGE->get_renderer('tool_lpmigrate');
echo $output->header();
echo $output->heading($title);
$form = new \tool_lpmigrate\form\migrate_framework($context);
if ($form->is_cancelled()) {
redirect($url);
} else if ($data = $form->get_data()) {
// Map competencies from both framework
$mapper = new \tool_lpmigrate\framework_mapper($data->from, $data->to);
$mapper->automap();
$progress = new \core\progress\display();
$progress->set_display_names(true);
$processor = new \tool_lpmigrate\framework_processor($mapper, $progress);
if (!empty($data->allowedcourses)) {
$processor->set_allowedcourses($data->allowedcourses);
}
if (!empty($data->disallowedcourses)) {
$processor->set_disallowedcourses($data->disallowedcourses);
}
$processor->set_course_start_date_from($data->coursestartdate);
$processor->proceed();
$unmappedfrom = $mapper->get_unmapped_objects_from();
$unmappedto = $mapper->get_unmapped_objects_to();
$renderable = new \tool_lpmigrate\output\migrate_framework_results($context, $processor,
\tool_lp\api::read_framework($data->from), \tool_lp\api::read_framework($data->to), $unmappedfrom, $unmappedto);
echo $output->render($renderable);