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 |
* unilabel module.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_unilabel
|
|
|
21 |
* @author Andreas Grabs <info@grabs-edv.de>
|
|
|
22 |
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace mod_unilabel;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Create a instance of a content type.
|
|
|
30 |
* @package mod_unilabel
|
|
|
31 |
* @author Andreas Grabs <info@grabs-edv.de>
|
|
|
32 |
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class factory {
|
|
|
36 |
/**
|
|
|
37 |
* Get a list of names from all content type plugins.
|
|
|
38 |
*
|
|
|
39 |
* @return array
|
|
|
40 |
*/
|
|
|
41 |
public static function get_plugin_list() {
|
|
|
42 |
$plugins = self::get_plugins();
|
|
|
43 |
|
|
|
44 |
$return = [];
|
|
|
45 |
foreach ($plugins as $name => $plugin) {
|
|
|
46 |
$return[$name] = $plugin->get_name();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
return $return;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Get all content type plugins.
|
|
|
54 |
*
|
|
|
55 |
* @return array
|
|
|
56 |
*/
|
|
|
57 |
public static function get_plugins() {
|
|
|
58 |
$plugins = \core_component::get_plugin_list('unilabeltype');
|
|
|
59 |
|
|
|
60 |
$return = [];
|
|
|
61 |
foreach ($plugins as $name => $notused) {
|
|
|
62 |
$plugin = self::get_plugin($name);
|
|
|
63 |
if (!$plugin->is_active()) {
|
|
|
64 |
continue;
|
|
|
65 |
}
|
|
|
66 |
$return[$name] = $plugin;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return $return;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Get a content type plugin by a given name.
|
|
|
74 |
*
|
|
|
75 |
* @param string $name
|
|
|
76 |
* @return content_type
|
|
|
77 |
*/
|
|
|
78 |
public static function get_plugin($name): content_type {
|
|
|
79 |
$classname = '\\unilabeltype_' . $name . '\\content_type';
|
|
|
80 |
if (!class_exists($classname)) {
|
|
|
81 |
$classname = '\mod_unilabel\unknown_type';
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
return new $classname();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Delete the content of the current content type.
|
|
|
89 |
*
|
|
|
90 |
* @param int $unilabelid
|
|
|
91 |
* @return void
|
|
|
92 |
*/
|
|
|
93 |
public static function delete_plugin_content($unilabelid) {
|
|
|
94 |
$plugins = self::get_plugins();
|
|
|
95 |
foreach ($plugins as $plugin) {
|
|
|
96 |
$plugin->delete_content($unilabelid);
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Save the content for the current content type plugin.
|
|
|
102 |
*
|
|
|
103 |
* @param \stdClass $formdata
|
|
|
104 |
* @param \stdClass $unilabel
|
|
|
105 |
* @return bool
|
|
|
106 |
*/
|
|
|
107 |
public static function save_plugin_content($formdata, $unilabel) {
|
|
|
108 |
$unilabeltype = self::get_plugin($unilabel->unilabeltype);
|
|
|
109 |
|
|
|
110 |
if ($unilabeltype->save_content($formdata, $unilabel)) {
|
|
|
111 |
return true;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
return false;
|
|
|
115 |
}
|
|
|
116 |
}
|