From bb50f08db26fa060b3d3e2b0669cce30a214d60c Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 27 Nov 2015 15:14:26 +0800 Subject: [PATCH] MDL-49464 tool_lp: Include course competencies in backups --- .../moodle2/backup_tool_lp_plugin.class.php | 80 +++++++++++++++++ .../moodle2/restore_tool_lp_plugin.class.php | 87 +++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 admin/tool/lp/backup/moodle2/backup_tool_lp_plugin.class.php create mode 100644 admin/tool/lp/backup/moodle2/restore_tool_lp_plugin.class.php diff --git a/admin/tool/lp/backup/moodle2/backup_tool_lp_plugin.class.php b/admin/tool/lp/backup/moodle2/backup_tool_lp_plugin.class.php new file mode 100644 index 00000000000..c3766efc99d --- /dev/null +++ b/admin/tool/lp/backup/moodle2/backup_tool_lp_plugin.class.php @@ -0,0 +1,80 @@ +. + +/** + * 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); + } + +} diff --git a/admin/tool/lp/backup/moodle2/restore_tool_lp_plugin.class.php b/admin/tool/lp/backup/moodle2/restore_tool_lp_plugin.class.php new file mode 100644 index 00000000000..4a7387c2017 --- /dev/null +++ b/admin/tool/lp/backup/moodle2/restore_tool_lp_plugin.class.php @@ -0,0 +1,87 @@ +. + +/** + * 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(); + } + + } + +}