MDL-49464 tool_lp: Include course competencies in backups

This commit is contained in:
Frederic Massart
2016-04-18 10:58:45 +08:00
parent d660824b7f
commit bb50f08db2
2 changed files with 167 additions and 0 deletions
@@ -0,0 +1,80 @@
<?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/>.
/**
* Backup file.
*
* @package tool_lp
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/backup/moodle2/backup_tool_plugin.class.php');
/**
* Backup class.
*
* @package tool_lp
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_tool_lp_plugin extends backup_tool_plugin {
/**
* Define the plugin structure.
*
* @return backup_plugin_element
*/
protected function define_course_plugin_structure() {
$plugin = $this->get_plugin_element(null, $this->get_include_condition(), 'include');
$pluginwrapper = new backup_nested_element($this->get_recommended_name());
$plugin->add_child($pluginwrapper);
$coursecompetencies = new backup_nested_element('course_competencies');
$pluginwrapper->add_child($coursecompetencies);
$competency = new backup_nested_element('competency', null, array('idnumber', 'ruleoutcome',
'sortorder', 'frameworkidnumber'));
$coursecompetencies->add_child($competency);
$sql = 'SELECT c.idnumber, cc.ruleoutcome, cc.sortorder, f.idnumber AS frameworkidnumber
FROM {' . \tool_lp\course_competency::TABLE . '} cc
JOIN {' . \tool_lp\competency::TABLE . '} c ON c.id = cc.competencyid
JOIN {' . \tool_lp\competency_framework::TABLE . '} f ON f.id = c.competencyframeworkid
WHERE cc.courseid = :courseid
ORDER BY cc.sortorder';
$competency->set_source_sql($sql, array('courseid' => backup::VAR_COURSEID));
return $plugin;
}
/**
* Returns a condition for whether we include this report in the backup or not.
*
* @return array
*/
protected function get_include_condition() {
$result = '';
if (\tool_lp\course_competency::record_exists_select('courseid = ?', array($this->task->get_courseid()))) {
$result = 'include';
};
return array('sqlparam' => $result);
}
}
@@ -0,0 +1,87 @@
<?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/>.
/**
* Restore file.
*
* @package tool_lp
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/backup/moodle2/restore_tool_plugin.class.php');
/**
* Restore class.
*
* @package tool_lp
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_tool_lp_plugin extends restore_tool_plugin {
/**
* Return the paths.
*
* @return restore_path_element[]
*/
protected function define_course_plugin_structure() {
$paths = array(
new restore_path_element('course_competency', $this->get_pathfor('/course_competencies/competency'))
);
return $paths;
}
/**
* Process a course competency.
*
* @param array $data The data.
*/
public function process_course_competency($data) {
global $DB;
$data = (object) $data;
// Mapping the competency by ID numbers.
$framework = \tool_lp\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
if (!$framework) {
return;
}
$competency = \tool_lp\competency::get_record(array('idnumber' => $data->idnumber,
'competencyframeworkid' => $framework->get_id()));
if (!$competency) {
return;
}
$params = array(
'competencyid' => $competency->get_id(),
'courseid' => $this->task->get_courseid()
);
$existing = \tool_lp\course_competency::record_exists_select('competencyid = :competencyid AND courseid = :courseid', $params);
if (!$existing) {
// Sortorder is ignored by precaution, anyway we should walk through the records in the right order.
$record = (object) $params;
$record->ruleoutcome = $data->ruleoutcome;
$coursecompetency = new \tool_lp\course_competency(0, $record);
$coursecompetency->create();
}
}
}