MDL-53700 competency: Migrating settings to core
[moodle.git] / admin / tool / lp / backup / moodle2 / restore_tool_lp_plugin.class.php
CommitLineData
bb50f08d
FM
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/>.
16
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 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once($CFG->dirroot . '/backup/moodle2/restore_tool_plugin.class.php');
28
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 */
36class restore_tool_lp_plugin extends restore_tool_plugin {
37
38 /**
39 * Return the paths.
40 *
41 * @return restore_path_element[]
42 */
43 protected function define_course_plugin_structure() {
44 $paths = array(
c2f55d29
DW
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'))
bb50f08d
FM
47 );
48 return $paths;
49 }
50
db650737
DW
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 }
62
c2f55d29
DW
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;
70
71 $data = (object) $data;
72 $courseid = $this->task->get_courseid();
67bc0eaf 73 $exists = \core_competency\course_competency_settings::get_record(array('courseid' => $courseid));
c2f55d29
DW
74
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);
67bc0eaf 82 $settings = new \core_competency\course_competency_settings(0, $data);
c2f55d29
DW
83 return !empty($settings->create());
84 }
85 }
86
bb50f08d
FM
87 /**
88 * Process a course competency.
89 *
90 * @param array $data The data.
91 */
f446b2e1 92 public function process_course_competency($data) {
f446b2e1
DW
93 $data = (object) $data;
94
95 // Mapping the competency by ID numbers.
67bc0eaf 96 $framework = \core_competency\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
f446b2e1
DW
97 if (!$framework) {
98 return;
99 }
67bc0eaf 100 $competency = \core_competency\competency::get_record(array('idnumber' => $data->idnumber,
f446b2e1
DW
101 'competencyframeworkid' => $framework->get_id()));
102 if (!$competency) {
103 return;
104 }
105
106 $params = array(
107 'competencyid' => $competency->get_id(),
108 'courseid' => $this->task->get_courseid()
109 );
110 $query = 'competencyid = :competencyid AND courseid = :courseid';
67bc0eaf 111 $existing = \core_competency\course_competency::record_exists_select($query, $params);
f446b2e1
DW
112
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;
67bc0eaf 117 $coursecompetency = new \core_competency\course_competency(0, $record);
f446b2e1
DW
118 $coursecompetency->create();
119 }
120
121 }
122
123 /**
124 * Process a course module competency.
125 *
126 * @param array $data The data.
127 */
db650737 128 public function process_course_module_competency($data) {
bb50f08d
FM
129 $data = (object) $data;
130
131 // Mapping the competency by ID numbers.
67bc0eaf 132 $framework = \core_competency\competency_framework::get_record(array('idnumber' => $data->frameworkidnumber));
bb50f08d
FM
133 if (!$framework) {
134 return;
135 }
67bc0eaf 136 $competency = \core_competency\competency::get_record(array('idnumber' => $data->idnumber,
bb50f08d
FM
137 'competencyframeworkid' => $framework->get_id()));
138 if (!$competency) {
139 return;
140 }
141
142 $params = array(
143 'competencyid' => $competency->get_id(),
db650737 144 'cmid' => $this->task->get_moduleid()
bb50f08d 145 );
f446b2e1 146 $query = 'competencyid = :competencyid AND cmid = :cmid';
67bc0eaf 147 $existing = \core_competency\course_module_competency::record_exists_select($query, $params);
bb50f08d
FM
148
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;
67bc0eaf 153 $coursemodulecompetency = new \core_competency\course_module_competency(0, $record);
db650737 154 $coursemodulecompetency->create();
bb50f08d
FM
155 }
156
157 }
158
159}