3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * A namespace contains license specific functions
24 * @subpackage moodlecore
25 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 class license_manager {
31 * Adding a new license type
32 * @param object $license {
33 * shortname => string a shortname of license, will be refered by files table[required]
34 * fullname => string the fullname of the license [required]
35 * source => string the homepage of the license type[required]
36 * enabled => int is it enabled?
37 * version => int a version number used by moodle [required]
40 static public function add($license) {
42 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
44 if ($record->version < $license->version) {
45 // update license record
46 $license->enabled = $record->enabled;
47 $license->id = $record->id;
48 $DB->update_record('license', $license);
51 $DB->insert_record('license', $license);
61 static public function get_licenses($param = null) {
63 if (empty($param) || !is_array($param)) {
66 // get licenses by conditions
67 if ($records = $DB->get_records('license', $param)) {
75 * Get license record by shortname
76 * @param mixed $param the shortname of license, or an array
79 static public function get_license_by_shortname($name) {
81 if ($record = $DB->get_record('license', array('shortname'=>$name))) {
90 * @param string $license the shortname of license
93 static public function enable($license) {
95 if ($license = self::get_license_by_shortname($license)) {
96 $license->enabled = 1;
97 $DB->update_record('license', $license);
99 self::set_active_licenses();
105 * @param string $license the shortname of license
108 static public function disable($license) {
110 if ($license = self::get_license_by_shortname($license)) {
111 $license->enabled = 0;
112 $DB->update_record('license', $license);
114 self::set_active_licenses();
119 * Store active licenses in global $CFG
121 static private function set_active_licenses() {
122 // set to global $CFG
123 $licenses = self::get_licenses(array('enabled'=>1));
125 foreach ($licenses as $l) {
126 $result[] = $l->shortname;
128 set_config('licenses', implode(',', $result));
132 * Install moodle build-in licenses
134 static public function install_licenses() {
135 $active_licenses = array();
137 $license = new stdclass;
139 $license->shortname = 'unknown';
140 $license->fullname = 'Unknown license';
141 $license->source = '';
142 $license->enabled = 1;
143 $license->version = '2010033100';
144 $active_licenses[] = $license->shortname;
147 $license->shortname = 'allrightsreserved';
148 $license->fullname = 'All rights reserved';
149 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
150 $license->enabled = 1;
151 $license->version = '2010033100';
152 $active_licenses[] = $license->shortname;
155 $license->shortname = 'public';
156 $license->fullname = 'Public Domain';
157 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
158 $license->enabled = 1;
159 $license->version = '2010033100';
160 $active_licenses[] = $license->shortname;
163 $license->shortname = 'cc';
164 $license->fullname = 'Creative Commons';
165 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
166 $license->enabled = 1;
167 $license->version = '2010033100';
168 $active_licenses[] = $license->shortname;
171 $license->shortname = 'cc-nd';
172 $license->fullname = 'Creative Commons - NoDerivs';
173 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
174 $license->enabled = 1;
175 $license->version = '2010033100';
176 $active_licenses[] = $license->shortname;
179 $license->shortname = 'cc-nc-nd';
180 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
181 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
182 $license->enabled = 1;
183 $license->version = '2010033100';
184 $active_licenses[] = $license->shortname;
187 $license->shortname = 'cc-nc';
188 $license->fullname = 'Creative Commons - No Commercial';
189 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
190 $license->enabled = 1;
191 $license->version = '2010033100';
192 $active_licenses[] = $license->shortname;
195 $license->shortname = 'cc-nc-sa';
196 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
197 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
198 $license->enabled = 1;
199 $license->version = '2010033100';
200 $active_licenses[] = $license->shortname;
203 $license->shortname = 'cc-sa';
204 $license->fullname = 'Creative Commons - ShareAlike';
205 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
206 $license->enabled = 1;
207 $license->version = '2010033100';
208 $active_licenses[] = $license->shortname;
211 set_config('licenses', implode(',', $active_licenses));