149ebf206c000556c90de6f087de27df14806fbd
[moodle.git] / admin / tool / lp / backup / moodle2 / restore_tool_lp_plugin.class.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Restore file.
19  *
20  * @package    tool_lp
21  * @copyright  2015 Frédéric Massart - FMCorz.net
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot . '/backup/moodle2/restore_tool_plugin.class.php');
29 /**
30  * Restore class.
31  *
32  * @package    tool_lp
33  * @copyright  2015 Frédéric Massart - FMCorz.net
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class restore_tool_lp_plugin extends restore_tool_plugin {
38     /**
39      * Return the paths.
40      *
41      * @return restore_path_element[]
42      */
43     protected function define_course_plugin_structure() {
44         $paths = array(
45             new restore_path_element('course_competency', $this->get_pathfor('/course_competencies/competency')),
46             new restore_path_element('course_competency_settings', $this->get_pathfor('/course_competency_settings'))
47         );
48         return $paths;
49     }
51     /**
52      * Return the paths.
53      *
54      * @return restore_path_element[]
55      */
56     protected function define_module_plugin_structure() {
57         $paths = array(
58             new restore_path_element('course_module_competency', $this->get_pathfor('/course_module_competencies/competency'))
59         );
60         return $paths;
61     }
63     /**
64      * Process a course competency settings.
65      *
66      * @param  array $data The data.
67      */
68     public function process_course_competency_settings($data) {
69         global $DB;
71         $data = (object) $data;
72         $courseid = $this->task->get_courseid();
73         $exists = \core_competency\course_competency_settings::get_record(array('courseid' => $courseid));
75         // Now update or insert.
76         if ($exists) {
77             $settings = $exists;
78             $settings->set_pushratingstouserplans($data->pushratingstouserplans);
79             return $settings->update();
80         } else {
81             $data = (object) array('courseid' => $courseid, 'pushratingstouserplans' => $data->pushratingstouserplans);
82             $settings = new \core_competency\course_competency_settings(0, $data);
83             return !empty($settings->create());
84         }
85     }
87     /**
88      * Process a course competency.
89      *
90      * @param  array $data The data.
91      */
92     public function process_course_competency($data) {
93         $data = (object) $data;
95         // Mapping the competency by ID numbers.
96         $framework = \core_competency\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
97         if (!$framework) {
98             return;
99         }
100         $competency = \core_competency\competency::get_record(array('idnumber' => $data->idnumber,
101             'competencyframeworkid' => $framework->get_id()));
102         if (!$competency) {
103             return;
104         }
106         $params = array(
107             'competencyid' => $competency->get_id(),
108             'courseid' => $this->task->get_courseid()
109         );
110         $query = 'competencyid = :competencyid AND courseid = :courseid';
111         $existing = \core_competency\course_competency::record_exists_select($query, $params);
113         if (!$existing) {
114             // Sortorder is ignored by precaution, anyway we should walk through the records in the right order.
115             $record = (object) $params;
116             $record->ruleoutcome = $data->ruleoutcome;
117             $coursecompetency = new \core_competency\course_competency(0, $record);
118             $coursecompetency->create();
119         }
121     }
123     /**
124      * Process a course module competency.
125      *
126      * @param  array $data The data.
127      */
128     public function process_course_module_competency($data) {
129         $data = (object) $data;
131         // Mapping the competency by ID numbers.
132         $framework = \core_competency\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
133         if (!$framework) {
134             return;
135         }
136         $competency = \core_competency\competency::get_record(array('idnumber' => $data->idnumber,
137             'competencyframeworkid' => $framework->get_id()));
138         if (!$competency) {
139             return;
140         }
142         $params = array(
143             'competencyid' => $competency->get_id(),
144             'cmid' => $this->task->get_moduleid()
145         );
146         $query = 'competencyid = :competencyid AND cmid = :cmid';
147         $existing = \core_competency\course_module_competency::record_exists_select($query, $params);
149         if (!$existing) {
150             // Sortorder is ignored by precaution, anyway we should walk through the records in the right order.
151             $record = (object) $params;
152             $record->ruleoutcome = $data->ruleoutcome;
153             $coursemodulecompetency = new \core_competency\course_module_competency(0, $record);
154             $coursemodulecompetency->create();
155         }
157     }