Merge branch 'MDL-61839-master' of git://github.com/mihailges/moodle
[moodle.git] / lib / licenselib.php
blobe49343754b5449456fea7cb15837deeab4bc389d
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
19 /**
20 * A namespace contains license specific functions
22 * @since Moodle 2.0
23 * @package core
24 * @subpackage lib
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 {
32 /**
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]
40 * }
42 static public function add($license) {
43 global $DB;
44 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
45 // record exists
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);
52 } else {
53 $DB->insert_record('license', $license);
55 return true;
58 /**
59 * Get license records
60 * @param mixed $param
61 * @return array
63 static public function get_licenses($param = null) {
64 global $DB;
65 if (empty($param) || !is_array($param)) {
66 $param = array();
68 // get licenses by conditions
69 if ($records = $DB->get_records('license', $param)) {
70 return $records;
71 } else {
72 return array();
76 /**
77 * Get license record by shortname
78 * @param mixed $param the shortname of license, or an array
79 * @return object
81 static public function get_license_by_shortname($name) {
82 global $DB;
83 if ($record = $DB->get_record('license', array('shortname'=>$name))) {
84 return $record;
85 } else {
86 return null;
90 /**
91 * Enable a license
92 * @param string $license the shortname of license
93 * @return boolean
95 static public function enable($license) {
96 global $DB;
97 if ($license = self::get_license_by_shortname($license)) {
98 $license->enabled = 1;
99 $DB->update_record('license', $license);
101 self::set_active_licenses();
102 return true;
106 * Disable a license
107 * @param string $license the shortname of license
108 * @return boolean
110 static public function disable($license) {
111 global $DB, $CFG;
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();
121 return true;
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));
130 $result = array();
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;
151 self::add($license);
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;
159 self::add($license);
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;
167 self::add($license);
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;
175 self::add($license);
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;
183 self::add($license);
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;
191 self::add($license);
193 $license->shortname = 'cc-nc';
194 $license->fullname = 'Creative Commons - No Commercial';
195 $license->source = 'http://creativecommons.org/licenses/by-nc/3.0/';
196 $license->enabled = 1;
197 $license->version = '2013051500';
198 $active_licenses[] = $license->shortname;
199 self::add($license);
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;
207 self::add($license);
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;
215 self::add($license);
217 set_config('licenses', implode(',', $active_licenses));