MDL-53700 competency: Migrating backup logic to core

This commit is contained in:
Frederic Massart 2016-04-06 13:25:38 +08:00
parent 6be1373ee0
commit 8417c3a620
8 changed files with 239 additions and 279 deletions

View File

@ -1,120 +0,0 @@
<?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);
$coursecompetencysettings = new backup_nested_element('course_competency_settings',
array('id'), array('pushratingstouserplans'));
$pluginwrapper->add_child($coursecompetencysettings);
$sql = 'SELECT s.pushratingstouserplans
FROM {' . \core_competency\course_competency_settings::TABLE . '} s
WHERE s.courseid = :courseid';
$coursecompetencysettings->set_source_sql($sql, array('courseid' => backup::VAR_COURSEID));
$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 {' . \core_competency\course_competency::TABLE . '} cc
JOIN {' . \core_competency\competency::TABLE . '} c ON c.id = cc.competencyid
JOIN {' . \core_competency\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;
}
/**
* Define the module plugin structure.
*
* @return backup_plugin_element
*/
protected function define_module_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_module_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, cmc.ruleoutcome, cmc.sortorder, f.idnumber AS frameworkidnumber
FROM {' . \core_competency\course_module_competency::TABLE . '} cmc
JOIN {' . \core_competency\competency::TABLE . '} c ON c.id = cmc.competencyid
JOIN {' . \core_competency\competency_framework::TABLE . '} f ON f.id = c.competencyframeworkid
WHERE cmc.cmid = :coursemoduleid
ORDER BY cmc.sortorder';
$competency->set_source_sql($sql, array('coursemoduleid' => backup::VAR_MODID));
return $plugin;
}
/**
* Returns a condition for whether we include this plugin in the backup or not.
*
* @return array
*/
protected function get_include_condition() {
$result = '';
if (\core_competency\course_competency::record_exists_select('courseid = ?', array($this->task->get_courseid()))) {
$result = 'include';
};
return array('sqlparam' => $result);
}
}

View File

@ -1,159 +0,0 @@
<?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')),
new restore_path_element('course_competency_settings', $this->get_pathfor('/course_competency_settings'))
);
return $paths;
}
/**
* Return the paths.
*
* @return restore_path_element[]
*/
protected function define_module_plugin_structure() {
$paths = array(
new restore_path_element('course_module_competency', $this->get_pathfor('/course_module_competencies/competency'))
);
return $paths;
}
/**
* Process a course competency settings.
*
* @param array $data The data.
*/
public function process_course_competency_settings($data) {
global $DB;
$data = (object) $data;
$courseid = $this->task->get_courseid();
$exists = \core_competency\course_competency_settings::get_record(array('courseid' => $courseid));
// Now update or insert.
if ($exists) {
$settings = $exists;
$settings->set_pushratingstouserplans($data->pushratingstouserplans);
return $settings->update();
} else {
$data = (object) array('courseid' => $courseid, 'pushratingstouserplans' => $data->pushratingstouserplans);
$settings = new \core_competency\course_competency_settings(0, $data);
return !empty($settings->create());
}
}
/**
* Process a course competency.
*
* @param array $data The data.
*/
public function process_course_competency($data) {
$data = (object) $data;
// Mapping the competency by ID numbers.
$framework = \core_competency\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
if (!$framework) {
return;
}
$competency = \core_competency\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()
);
$query = 'competencyid = :competencyid AND courseid = :courseid';
$existing = \core_competency\course_competency::record_exists_select($query, $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 \core_competency\course_competency(0, $record);
$coursecompetency->create();
}
}
/**
* Process a course module competency.
*
* @param array $data The data.
*/
public function process_course_module_competency($data) {
$data = (object) $data;
// Mapping the competency by ID numbers.
$framework = \core_competency\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
if (!$framework) {
return;
}
$competency = \core_competency\competency::get_record(array('idnumber' => $data->idnumber,
'competencyframeworkid' => $framework->get_id()));
if (!$competency) {
return;
}
$params = array(
'competencyid' => $competency->get_id(),
'cmid' => $this->task->get_moduleid()
);
$query = 'competencyid = :competencyid AND cmid = :cmid';
$existing = \core_competency\course_module_competency::record_exists_select($query, $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;
$coursemodulecompetency = new \core_competency\course_module_competency(0, $record);
$coursemodulecompetency->create();
}
}
}

View File

@ -189,6 +189,9 @@ abstract class backup_activity_task extends backup_task {
// Generate the grade history file. The setting 'grade_histories' is handled in the step.
$this->add_step(new backup_activity_grade_history_structure_step('activity_grade_history', 'grade_history.xml'));
// Generate the competency file.
$this->add_step(new backup_activity_competencies_structure_step('activity_competencies', 'competencies.xml'));
// Annotate the scales used in already annotated outcomes
$this->add_step(new backup_annotate_scales_from_outcomes('annotate_scales'));

View File

@ -127,6 +127,9 @@ class backup_course_task extends backup_task {
$this->add_step(new backup_course_logstores_structure_step('course_logstores', 'logstores.xml'));
}
// Generate the course competencies.
$this->add_step(new backup_course_competencies_structure_step('course_competencies', 'competencies.xml'));
// Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
$this->add_step(new backup_inforef_structure_step('course', 'inforef.xml'));

View File

@ -1535,6 +1535,68 @@ class backup_course_logstores_structure_step extends backup_structure_step {
class backup_activity_logstores_structure_step extends backup_course_logstores_structure_step {
}
/**
* Course competencies backup structure step.
*/
class backup_course_competencies_structure_step extends backup_structure_step {
protected function define_structure() {
$wrapper = new backup_nested_element('course_competencies');
$settings = new backup_nested_element('settings', array('id'), array('pushratingstouserplans'));
$wrapper->add_child($settings);
$sql = 'SELECT s.pushratingstouserplans
FROM {' . \core_competency\course_competency_settings::TABLE . '} s
WHERE s.courseid = :courseid';
$settings->set_source_sql($sql, array('courseid' => backup::VAR_COURSEID));
$competencies = new backup_nested_element('competencies');
$wrapper->add_child($competencies);
$competency = new backup_nested_element('competency', null, array('idnumber', 'ruleoutcome',
'sortorder', 'frameworkidnumber'));
$competencies->add_child($competency);
$sql = 'SELECT c.idnumber, cc.ruleoutcome, cc.sortorder, f.idnumber AS frameworkidnumber
FROM {' . \core_competency\course_competency::TABLE . '} cc
JOIN {' . \core_competency\competency::TABLE . '} c ON c.id = cc.competencyid
JOIN {' . \core_competency\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 $wrapper;
}
}
/**
* Activity competencies backup structure step.
*/
class backup_activity_competencies_structure_step extends backup_structure_step {
protected function define_structure() {
$wrapper = new backup_nested_element('course_module_competencies');
$competencies = new backup_nested_element('competencies');
$wrapper->add_child($competencies);
$competency = new backup_nested_element('competency', null, array('idnumber', 'ruleoutcome',
'sortorder', 'frameworkidnumber'));
$competencies->add_child($competency);
$sql = 'SELECT c.idnumber, cmc.ruleoutcome, cmc.sortorder, f.idnumber AS frameworkidnumber
FROM {' . \core_competency\course_module_competency::TABLE . '} cmc
JOIN {' . \core_competency\competency::TABLE . '} c ON c.id = cmc.competencyid
JOIN {' . \core_competency\competency_framework::TABLE . '} f ON f.id = c.competencyframeworkid
WHERE cmc.cmid = :coursemoduleid
ORDER BY cmc.sortorder';
$competency->set_source_sql($sql, array('coursemoduleid' => backup::VAR_MODID));
return $wrapper;
}
}
/**
* structure in charge of constructing the inforef.xml file for all the items we want
* to have referenced there (users, roles, files...)

View File

@ -179,6 +179,9 @@ abstract class restore_activity_task extends restore_task {
$this->add_step(new restore_activity_logstores_structure_step('activity_logstores', 'logstores.xml'));
}
// Activity competencies.
$this->add_step(new restore_activity_competencies_structure_step('activity_competencies', 'competencies.xml'));
// At the end, mark it as built
$this->built = true;
}

View File

@ -112,6 +112,9 @@ class restore_course_task extends restore_task {
$this->add_step(new restore_calendarevents_structure_step('course_calendar', 'calendar.xml'));
}
// Course competencies.
$this->add_step(new restore_course_competencies_structure_step('course_competencies', 'competencies.xml'));
// At the end, mark it as built
$this->built = true;
}

View File

@ -3055,6 +3055,171 @@ class restore_course_logstores_structure_step extends restore_structure_step {
class restore_activity_logstores_structure_step extends restore_course_logstores_structure_step {
}
/**
* Restore course competencies structure step.
*/
class restore_course_competencies_structure_step extends restore_structure_step {
/**
* Returns the structure.
*
* @return array
*/
protected function define_structure() {
$paths = array(
new restore_path_element('course_competency', '/course_competencies/competencies/competency'),
new restore_path_element('course_competency_settings', '/course_competencies/settings')
);
return $paths;
}
/**
* Process a course competency settings.
*
* @param array $data The data.
*/
public function process_course_competency_settings($data) {
global $DB;
$data = (object) $data;
$courseid = $this->task->get_courseid();
$exists = \core_competency\course_competency_settings::get_record(array('courseid' => $courseid));
// Now update or insert.
if ($exists) {
$settings = $exists;
$settings->set_pushratingstouserplans($data->pushratingstouserplans);
return $settings->update();
} else {
$data = (object) array('courseid' => $courseid, 'pushratingstouserplans' => $data->pushratingstouserplans);
$settings = new \core_competency\course_competency_settings(0, $data);
$result = $settings->create();
return !empty($result);
}
}
/**
* Process a course competency.
*
* @param array $data The data.
*/
public function process_course_competency($data) {
$data = (object) $data;
// Mapping the competency by ID numbers.
$framework = \core_competency\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
if (!$framework) {
return;
}
$competency = \core_competency\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()
);
$query = 'competencyid = :competencyid AND courseid = :courseid';
$existing = \core_competency\course_competency::record_exists_select($query, $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 \core_competency\course_competency(0, $record);
$coursecompetency->create();
}
}
/**
* Execute conditions.
*
* @return bool
*/
protected function execute_condition() {
// Do not execute if the competencies XML file is not found.
$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
if (!file_exists($fullpath)) {
return false;
}
return true;
}
}
/**
* Restore activity competencies structure step.
*/
class restore_activity_competencies_structure_step extends restore_structure_step {
/**
* Defines the structure.
*
* @return array
*/
protected function define_structure() {
$paths = array(
new restore_path_element('course_module_competency', '/course_module_competencies/competencies/competency')
);
return $paths;
}
/**
* Process a course module competency.
*
* @param array $data The data.
*/
public function process_course_module_competency($data) {
$data = (object) $data;
// Mapping the competency by ID numbers.
$framework = \core_competency\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
if (!$framework) {
return;
}
$competency = \core_competency\competency::get_record(array('idnumber' => $data->idnumber,
'competencyframeworkid' => $framework->get_id()));
if (!$competency) {
return;
}
$params = array(
'competencyid' => $competency->get_id(),
'cmid' => $this->task->get_moduleid()
);
$query = 'competencyid = :competencyid AND cmid = :cmid';
$existing = \core_competency\course_module_competency::record_exists_select($query, $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;
$coursemodulecompetency = new \core_competency\course_module_competency(0, $record);
$coursemodulecompetency->create();
}
}
/**
* Execute conditions.
*
* @return bool
*/
protected function execute_condition() {
// Do not execute if the competencies XML file is not found.
$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
if (!file_exists($fullpath)) {
return false;
}
return true;
}
}
/**
* Defines the restore step for advanced grading methods attached to the activity module