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
25 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') ||
die();
31 class license_manager
{
33 * Adding a new license type
34 * @param object $license {
35 * shortname => string a shortname of license, will be refered by files table[required]
36 * fullname => string the fullname of the license [required]
37 * source => string the homepage of the license type[required]
38 * enabled => int is it enabled?
39 * version => int a version number used by moodle [required]
42 static public function add($license) {
44 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname
))) {
46 if ($record->version
< $license->version
) {
47 // update license record
48 $license->enabled
= $record->enabled
;
49 $license->id
= $record->id
;
50 $DB->update_record('license', $license);
53 $DB->insert_record('license', $license);
63 static public function get_licenses($param = null) {
65 if (empty($param) ||
!is_array($param)) {
68 // get licenses by conditions
69 if ($records = $DB->get_records('license', $param)) {
77 * Get license record by shortname
78 * @param mixed $param the shortname of license, or an array
81 static public function get_license_by_shortname($name) {
83 if ($record = $DB->get_record('license', array('shortname'=>$name))) {
92 * @param string $license the shortname of license
95 static public function enable($license) {
97 if ($license = self
::get_license_by_shortname($license)) {
98 $license->enabled
= 1;
99 $DB->update_record('license', $license);
101 self
::set_active_licenses();
107 * @param string $license the shortname of license
110 static public function disable($license) {
112 // Site default license cannot be disabled!
113 if ($license == $CFG->sitedefaultlicense
) {
114 print_error('error');
116 if ($license = self
::get_license_by_shortname($license)) {
117 $license->enabled
= 0;
118 $DB->update_record('license', $license);
120 self
::set_active_licenses();
125 * Store active licenses in global $CFG
127 static private function set_active_licenses() {
128 // set to global $CFG
129 $licenses = self
::get_licenses(array('enabled'=>1));
131 foreach ($licenses as $l) {
132 $result[] = $l->shortname
;
134 set_config('licenses', implode(',', $result));
138 * Install moodle build-in licenses
140 static public function install_licenses() {
141 $active_licenses = array();
143 $license = new stdClass();
145 $license->shortname
= 'unknown';
146 $license->fullname
= 'Unknown license';
147 $license->source
= '';
148 $license->enabled
= 1;
149 $license->version
= '2010033100';
150 $active_licenses[] = $license->shortname
;
153 $license->shortname
= 'allrightsreserved';
154 $license->fullname
= 'All rights reserved';
155 $license->source
= 'http://en.wikipedia.org/wiki/All_rights_reserved';
156 $license->enabled
= 1;
157 $license->version
= '2010033100';
158 $active_licenses[] = $license->shortname
;
161 $license->shortname
= 'public';
162 $license->fullname
= 'Public Domain';
163 $license->source
= 'http://creativecommons.org/licenses/publicdomain/';
164 $license->enabled
= 1;
165 $license->version
= '2010033100';
166 $active_licenses[] = $license->shortname
;
169 $license->shortname
= 'cc';
170 $license->fullname
= 'Creative Commons';
171 $license->source
= 'http://creativecommons.org/licenses/by/3.0/';
172 $license->enabled
= 1;
173 $license->version
= '2010033100';
174 $active_licenses[] = $license->shortname
;
177 $license->shortname
= 'cc-nd';
178 $license->fullname
= 'Creative Commons - NoDerivs';
179 $license->source
= 'http://creativecommons.org/licenses/by-nd/3.0/';
180 $license->enabled
= 1;
181 $license->version
= '2010033100';
182 $active_licenses[] = $license->shortname
;
185 $license->shortname
= 'cc-nc-nd';
186 $license->fullname
= 'Creative Commons - No Commercial NoDerivs';
187 $license->source
= 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
188 $license->enabled
= 1;
189 $license->version
= '2010033100';
190 $active_licenses[] = $license->shortname
;
193 $license->shortname
= 'cc-nc';
194 $license->fullname
= 'Creative Commons - No Commercial';
195 $license->source
= 'http://creativecommons.org/licenses/by-nd/3.0/';
196 $license->enabled
= 1;
197 $license->version
= '2010033100';
198 $active_licenses[] = $license->shortname
;
201 $license->shortname
= 'cc-nc-sa';
202 $license->fullname
= 'Creative Commons - No Commercial ShareAlike';
203 $license->source
= 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
204 $license->enabled
= 1;
205 $license->version
= '2010033100';
206 $active_licenses[] = $license->shortname
;
209 $license->shortname
= 'cc-sa';
210 $license->fullname
= 'Creative Commons - ShareAlike';
211 $license->source
= 'http://creativecommons.org/licenses/by-sa/3.0/';
212 $license->enabled
= 1;
213 $license->version
= '2010033100';
214 $active_licenses[] = $license->shortname
;
217 set_config('licenses', implode(',', $active_licenses));