Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Framework mapper.
19
 *
20
 * @package    tool_lpmigrate
21
 * @copyright  2016 Frédéric Massart - FMCorz.net
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace tool_lpmigrate;
26
defined('MOODLE_INTERNAL') || die();
27
 
28
use core_competency\api;
29
 
30
/**
31
 * Framework mapper class.
32
 *
33
 * @package    tool_lpmigrate
34
 * @copyright  2016 Frédéric Massart - FMCorz.net
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class framework_mapper {
38
 
39
    /** @var int The ID of the framework we're migrating from. */
40
    protected $from;
41
    /** @var int The ID of the framework we're migrating to. */
42
    protected $to;
43
    /** @var array The collection of objects at origin. */
44
    protected $collectionfrom;
45
    /** @var array The collection of objects at destination. */
46
    protected $collectionto;
47
    /** @var array Mappings. */
48
    protected $mappings = array();
49
 
50
    /**
51
     * Constructor.
52
     * @param int $from Framework ID from.
53
     * @param int $to Framework ID to.
54
     */
55
    public function __construct($from, $to) {
56
        $this->from = $from;
57
        $this->to = $to;
58
    }
59
 
60
    /**
61
     * Add a mapping.
62
     * @param int $idfrom From ID.
63
     * @param int $idto To ID.
64
     */
65
    public function add_mapping($idfrom, $idto) {
66
        $this->mappings[$idfrom] = $idto;
67
    }
68
 
69
    /**
70
     * Auto map the frameworks.
71
     * @return void
72
     */
73
    public function automap() {
74
        $map = array();
75
 
76
        // Shallow copy.
77
        $collectionfrom = $this->get_collection_from();
78
        $collectionto = $this->get_collection_to();
79
 
80
        // Find mappings.
81
        foreach ($collectionfrom as $keyfrom => $compfrom) {
82
            foreach ($collectionto as $keyto => $compto) {
83
                if ($compfrom->get('idnumber') == $compto->get('idnumber')) {
84
                    $map[$compfrom->get('id')] = $compto->get('id');
85
                    unset($collectionfrom[$keyfrom]);
86
                    unset($collectionto[$keyto]);
87
                    break;
88
                }
89
            }
90
        }
91
 
92
        $this->mappings = $map;
93
    }
94
 
95
    /**
96
     * Get all IDs at origin.
97
     * @return array
98
     */
99
    public function get_all_from() {
100
        return array_keys($this->get_collection_from());
101
    }
102
 
103
    /**
104
     * Get all IDs at destination.
105
     * @return array
106
     */
107
    public function get_all_to() {
108
        return array_keys($this->get_collection_to());
109
    }
110
 
111
    /**
112
     * Get the collection at origin.
113
     * @return array
114
     */
115
    protected function get_collection_from() {
116
        if ($this->collectionfrom === null) {
117
            $this->collectionfrom = api::search_competencies('', $this->from);
118
        }
119
        return $this->collectionfrom;
120
    }
121
 
122
    /**
123
     * Get the collection at destination.
124
     * @return array
125
     */
126
    protected function get_collection_to() {
127
        if ($this->collectionto === null) {
128
            $this->collectionto = api::search_competencies('', $this->to);
129
        }
130
        return $this->collectionto;
131
    }
132
 
133
    /**
134
     * Get the defined mappings.
135
     * @return array
136
     */
137
    public function get_mappings() {
138
        return $this->mappings;
139
    }
140
 
141
    /**
142
     * Get the IDs of the objects at origin which do not have a mapping at destination.
143
     * @return array
144
     */
145
    public function get_unmapped_from() {
146
        return array_keys(array_diff_key($this->get_collection_from(), $this->mappings));
147
    }
148
 
149
    /**
150
     * Get the origin objects with missing mappings.
151
     * @return array
152
     */
153
    public function get_unmapped_objects_from() {
154
        return array_diff_key($this->get_collection_from(), $this->mappings);
155
    }
156
 
157
    /**
158
     * Get the IDs of the objects at destination which do not have a mapping at origin.
159
     * @return array
160
     */
161
    public function get_unmapped_to() {
162
        return array_keys(array_diff_key($this->get_collection_to(), array_flip($this->mappings)));
163
    }
164
 
165
    /**
166
     * Get the destination objects with missing mappings.
167
     * @return array
168
     */
169
    public function get_unmapped_objects_to() {
170
        return array_diff_key($this->get_collection_to(), array_flip($this->mappings));
171
    }
172
 
173
    /**
174
     * Whether some mappings were set.
175
     * @return bool
176
     */
177
    public function has_mappings() {
178
        return !empty($this->mappings);
179
    }
180
 
181
    /**
182
     * Reset the mappings.
183
     * @return void
184
     */
185
    public function reset_mappings() {
186
        $this->mappings = array();
187
    }
188
 
189
}