MDL-21146 fixed install regression - copypasted the setting of active license setting...
[moodle.git] / lib / licenselib.php
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.
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/>.
19 /**
20  * A namespace contains license specific functions
21  *
22  * @since 2.0
23  * @package moodlecore
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
27  */
29 class license_manager {
30     /**
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]
38      * }
39      */
40     static public function add($license) {
41         global $DB;
42         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
43             // record exists
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);
49             }
50         } else {
51             $DB->insert_record('license', $license);
52         }
53         return true;
54     }
56     /**
57      * Get license records
58      * @param mixed $param
59      * @return array
60      */
61     static public function get_licenses($param = null) {
62         global $DB;
63         if (empty($param) || !is_array($param)) {
64             $param = array();
65         }
66         // get licenses by conditions
67         if ($records = $DB->get_records('license', $param)) {
68             return $records;
69         } else {
70             return array();
71         }
72     }
74     /**
75      * Get license record by shortname
76      * @param mixed $param the shortname of license, or an array
77      * @return object
78      */
79     static public function get_license_by_shortname($name) {
80         global $DB;
81         if ($record = $DB->get_record('license', array('shortname'=>$name))) {
82             return $record;
83         } else {
84             return null;
85         }
86     }
88     /**
89      * Enable a license
90      * @param string $license the shortname of license
91      * @return boolean
92      */
93     static public function enable($license) {
94         global $DB;
95         if ($license = self::get_license_by_shortname($license)) {
96             $license->enabled = 1;
97             $DB->update_record('license', $license);
98         }
99         self::set_active_licenses();
100         return true;
101     }
103     /**
104      * Disable a license
105      * @param string $license the shortname of license
106      * @return boolean
107      */
108     static public function disable($license) {
109         global $DB;
110         if ($license = self::get_license_by_shortname($license)) {
111             $license->enabled = 0;
112             $DB->update_record('license', $license);
113         }
114         self::set_active_licenses();
115         return true;
116     }
118     /**
119      * Store active licenses in global $CFG
120      */
121     static private function set_active_licenses() {
122         // set to global $CFG
123         $licenses = self::get_licenses(array('enabled'=>1));
124         $result = array();
125         foreach ($licenses as $l) {
126             $result[] = $l->shortname;
127         }
128         set_config('licenses', implode(',', $result));
129     }
131     /**
132      * Install moodle build-in licenses
133      */
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;
145         self::add($license);
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;
153         self::add($license);
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;
161         self::add($license);
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;
169         self::add($license);
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;
177         self::add($license);
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;
185         self::add($license);
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;
193         self::add($license);
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;
201         self::add($license);
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;
209         self::add($license);
211         set_config('licenses', implode(',', $active_licenses));
212     }