Merge branch 'MDL-28250-23' of git://github.com/danpoltawski/moodle into MOODLE_23_STABLE
[moodle.git] / lib / licenselib.php
CommitLineData
1dce6261
DC
1<?php
2
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.
14//
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/>.
17
18
19/**
20 * A namespace contains license specific functions
21 *
78bfb562
PS
22 * @since 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
1dce6261
DC
27 */
28
78bfb562
PS
29defined('MOODLE_INTERNAL') || die();
30
1dce6261
DC
31class 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 * }
41 */
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
d1d4813f 48 $license->enabled = $record->enabled;
1dce6261
DC
49 $license->id = $record->id;
50 $DB->update_record('license', $license);
1dce6261
DC
51 }
52 } else {
d1d4813f 53 $DB->insert_record('license', $license);
1dce6261 54 }
d1d4813f 55 return true;
1dce6261
DC
56 }
57
58 /**
d1d4813f
DC
59 * Get license records
60 * @param mixed $param
61 * @return array
1dce6261 62 */
d1d4813f 63 static public function get_licenses($param = null) {
1dce6261 64 global $DB;
d1d4813f 65 if (empty($param) || !is_array($param)) {
1dce6261
DC
66 $param = array();
67 }
d1d4813f
DC
68 // get licenses by conditions
69 if ($records = $DB->get_records('license', $param)) {
70 return $records;
71 } else {
72 return array();
73 }
74 }
1dce6261 75
d1d4813f
DC
76 /**
77 * Get license record by shortname
78 * @param mixed $param the shortname of license, or an array
79 * @return object
80 */
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;
1dce6261
DC
87 }
88 }
89
90 /**
91 * Enable a license
92 * @param string $license the shortname of license
93 * @return boolean
94 */
95 static public function enable($license) {
96 global $DB;
d1d4813f
DC
97 if ($license = self::get_license_by_shortname($license)) {
98 $license->enabled = 1;
99 $DB->update_record('license', $license);
1dce6261 100 }
d1d4813f
DC
101 self::set_active_licenses();
102 return true;
1dce6261
DC
103 }
104
105 /**
106 * Disable a license
107 * @param string $license the shortname of license
108 * @return boolean
109 */
110 static public function disable($license) {
ba74f517
DC
111 global $DB, $CFG;
112 // Site default license cannot be disabled!
113 if ($license == $CFG->sitedefaultlicense) {
114 print_error('error');
115 }
d1d4813f
DC
116 if ($license = self::get_license_by_shortname($license)) {
117 $license->enabled = 0;
118 $DB->update_record('license', $license);
1dce6261 119 }
d1d4813f
DC
120 self::set_active_licenses();
121 return true;
122 }
123
124 /**
125 * Store active licenses in global $CFG
126 */
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;
133 }
134 set_config('licenses', implode(',', $result));
1dce6261
DC
135 }
136
137 /**
138 * Install moodle build-in licenses
139 */
140 static public function install_licenses() {
f56af888
PS
141 $active_licenses = array();
142
6bdfef5d 143 $license = new stdClass();
1dce6261 144
d1d4813f
DC
145 $license->shortname = 'unknown';
146 $license->fullname = 'Unknown license';
147 $license->source = '';
148 $license->enabled = 1;
149 $license->version = '2010033100';
f56af888 150 $active_licenses[] = $license->shortname;
d1d4813f
DC
151 self::add($license);
152
1dce6261
DC
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;
d1d4813f 157 $license->version = '2010033100';
f56af888 158 $active_licenses[] = $license->shortname;
1dce6261
DC
159 self::add($license);
160
161 $license->shortname = 'public';
162 $license->fullname = 'Public Domain';
163 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
164 $license->enabled = 1;
d1d4813f 165 $license->version = '2010033100';
f56af888 166 $active_licenses[] = $license->shortname;
1dce6261
DC
167 self::add($license);
168
169 $license->shortname = 'cc';
170 $license->fullname = 'Creative Commons';
171 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
172 $license->enabled = 1;
d1d4813f 173 $license->version = '2010033100';
f56af888 174 $active_licenses[] = $license->shortname;
1dce6261
DC
175 self::add($license);
176
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;
d1d4813f 181 $license->version = '2010033100';
f56af888 182 $active_licenses[] = $license->shortname;
1dce6261
DC
183 self::add($license);
184
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;
d1d4813f 189 $license->version = '2010033100';
f56af888 190 $active_licenses[] = $license->shortname;
1dce6261
DC
191 self::add($license);
192
193 $license->shortname = 'cc-nc';
194 $license->fullname = 'Creative Commons - No Commercial';
81fe100c 195 $license->source = 'http://creativecommons.org/licenses/by-nc/3.0/';
1dce6261 196 $license->enabled = 1;
81fe100c 197 $license->version = '2013051500';
f56af888 198 $active_licenses[] = $license->shortname;
1dce6261
DC
199 self::add($license);
200
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;
d1d4813f 205 $license->version = '2010033100';
f56af888 206 $active_licenses[] = $license->shortname;
1dce6261
DC
207 self::add($license);
208
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;
d1d4813f 213 $license->version = '2010033100';
f56af888 214 $active_licenses[] = $license->shortname;
1dce6261 215 self::add($license);
f56af888
PS
216
217 set_config('licenses', implode(',', $active_licenses));
1dce6261
DC
218 }
219}