From d1d4813ff2c6318e21d7e28802d83377c54cf024 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Wed, 31 Mar 2010 03:08:05 +0000 Subject: [PATCH] "MDL-21146, improve file license code" --- admin/settings/frontpage.php | 6 +- lang/en_utf8/license.php | 10 ++ lib/adminlib.php | 9 +- lib/db/upgrade.php | 174 ++++++++++++++++++++++++++++++++++- lib/licenselib.php | 110 +++++++++++++--------- repository/lib.php | 9 +- version.php | 2 +- 7 files changed, 261 insertions(+), 59 deletions(-) create mode 100644 lang/en_utf8/license.php diff --git a/admin/settings/frontpage.php b/admin/settings/frontpage.php index 7118f76b6c85f..ed980d6f239d7 100644 --- a/admin/settings/frontpage.php +++ b/admin/settings/frontpage.php @@ -35,9 +35,9 @@ $temp->add(new admin_setting_configselect('maxcategorydepth', get_string('configsitemaxcategorydepth','admin'), get_string('configsitemaxcategorydepthhelp','admin'), 0, $options)); require_once($CFG->libdir . '/licenselib.php'); $licenses = array(); - $array = license_manager::get(); - foreach ($array as $key=>$value) { - $licenses[$value->shortname] = $value->fullname; + $array = explode(',', $CFG->licenses); + foreach ($array as $value) { + $licenses[$value] = get_string($value, 'license'); } $temp->add(new admin_setting_configselect('sitedefaultlicense', get_string('configsitedefaultlicense','admin'), get_string('configsitedefaultlicensehelp','admin'), 'allrightsreserved', $licenses)); diff --git a/lang/en_utf8/license.php b/lang/en_utf8/license.php new file mode 100644 index 0000000000000..5ab02d4412afe --- /dev/null +++ b/lang/en_utf8/license.php @@ -0,0 +1,10 @@ +libdir . '/licenselib.php'); + $url = "licenses.php?sesskey=" . sesskey(); // display strings $txt = get_strings(array('administration', 'settings', 'name', 'enable', 'disable', 'none')); - $licenses = license_manager::get(); + $licenses = license_manager::get_licenses(); $return = $OUTPUT->heading(get_string('managelicenses', 'admin'), 3, 'main', true); + $return .= $OUTPUT->box_start('generalbox editorsui'); $table = new html_table(); @@ -5074,9 +5076,8 @@ public function output_html($data, $query='') { $table->width = '100%'; $table->data = array(); - $url = "licenses.php?sesskey=" . sesskey(); - foreach ($licenses as $key => $value) { - $displayname = html_writer::link($value->source, $value->fullname, array('target'=>'_blank')); + foreach ($licenses as $value) { + $displayname = html_writer::link($value->source, get_string($value->shortname, 'license'), array('target'=>'_blank')); if ($value->enabled == 1) { $hideshow = html_writer::link($url.'&action=disable&license='.$value->shortname, diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index b5aaa7370a338..0eb1f39068bee 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -3115,7 +3115,7 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint($result, 2010032400); } - if ($result && $oldversion < 2010032405) { + if ($result && $oldversion < 2010033101) { /// Define field source to be added to files $table = new xmldb_table('files'); @@ -3159,13 +3159,179 @@ function xmldb_main_upgrade($oldversion) { if (!$dbman->table_exists($table)) { $dbman->create_table($table); } - require_once($CFG->libdir . '/licenselib.php'); - license_manager::install_licenses(); + $active_licenses = array(); + + $license = new stdclass; + + // add unknown license + $license->shortname = 'unknown'; + $license->fullname = 'Unknown license'; + $license->source = ''; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->enabled = $record->enabled; + $license->id = $record->id; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + // add all rights reserved license + $license->shortname = 'allrightsreserved'; + $license->fullname = 'All rights reserved'; + $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved'; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->id = $record->id; + $license->enabled = $record->enabled; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + // add public domain license + $license->shortname = 'public'; + $license->fullname = 'Public Domain'; + $license->source = 'http://creativecommons.org/licenses/publicdomain/'; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->enabled = $record->enabled; + $license->id = $record->id; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + // add creative commons license + $license->shortname = 'cc'; + $license->fullname = 'Creative Commons'; + $license->source = 'http://creativecommons.org/licenses/by/3.0/'; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->enabled = $record->enabled; + $license->id = $record->id; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + // add creative commons no derivs license + $license->shortname = 'cc-nd'; + $license->fullname = 'Creative Commons - NoDerivs'; + $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/'; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->enabled = $record->enabled; + $license->id = $record->id; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + // add creative commons no commercial no derivs license + $license->shortname = 'cc-nc-nd'; + $license->fullname = 'Creative Commons - No Commercial NoDerivs'; + $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/'; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->enabled = $record->enabled; + $license->id = $record->id; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + // add creative commons no commercial + $license->shortname = 'cc-nc-nd'; + $license->shortname = 'cc-nc'; + $license->fullname = 'Creative Commons - No Commercial'; + $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/'; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->enabled = $record->enabled; + $license->id = $record->id; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + // add creative commons no commercial sharealike + $license->shortname = 'cc-nc-sa'; + $license->fullname = 'Creative Commons - No Commercial ShareAlike'; + $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/'; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->enabled = $record->enabled; + $license->id = $record->id; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + // add creative commons sharealike + $license->shortname = 'cc-sa'; + $license->fullname = 'Creative Commons - ShareAlike'; + $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/'; + $license->enabled = 1; + $license->version = '2010033100'; + $active_licenses[] = $license->shortname; + if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) { + if ($record->version < $license->version) { + // update license record + $license->enabled = $record->enabled; + $license->id = $record->id; + $DB->update_record('license', $license); + } + } else { + $DB->insert_record('license', $license); + } + + set_config('licenses', implode(',', $active_licenses)); /// set site default license set_config('sitedefaultlicense', 'allrightsreserved'); /// Main savepoint reached - upgrade_main_savepoint($result, 2010032405); + upgrade_main_savepoint($result, 2010033101); } return $result; diff --git a/lib/licenselib.php b/lib/licenselib.php index 0d2f8646fbad5..c383c1f38eaf9 100644 --- a/lib/licenselib.php +++ b/lib/licenselib.php @@ -43,45 +43,45 @@ static public function add($license) { // record exists if ($record->version < $license->version) { // update license record + $license->enabled = $record->enabled; $license->id = $record->id; $DB->update_record('license', $license); - } else { - debugging('won\'t update'); } } else { - if ($license_id = $DB->insert_record('license', $license)) { - return $license_id; - } else { - return false; - } + $DB->insert_record('license', $license); } + return true; } /** - * Get license record by shortname - * @param mixed $param the shortname of license, or an array - * @return object + * Get license records + * @param mixed $param + * @return array */ - static public function get($param = null) { + static public function get_licenses($param = null) { global $DB; - if (empty($param)) { + if (empty($param) || !is_array($param)) { $param = array(); } + // get licenses by conditions + if ($records = $DB->get_records('license', $param)) { + return $records; + } else { + return array(); + } + } - // get license by shortname - if (is_string($param)) { - if ($record = $DB->get_record('license', array('shortname'=>$name))) { - return $record; - } else { - return null; - } - } else if (is_array($param)) { - // get licenses by conditions - if ($records = $DB->get_records('license', $param)) { - return $records; - } else { - return null; - } + /** + * Get license record by shortname + * @param mixed $param the shortname of license, or an array + * @return object + */ + static public function get_license_by_shortname($name) { + global $DB; + if ($record = $DB->get_record('license', array('shortname'=>$name))) { + return $record; + } else { + return null; } } @@ -92,13 +92,12 @@ static public function get($param = null) { */ static public function enable($license) { global $DB; - if ($record = $DB->get_record('license', array('shortname'=>$license))) { - $record->enabled = 1; - $DB->update_record('license', $record); - return true; - } else { - return false; + if ($license = self::get_license_by_shortname($license)) { + $license->enabled = 1; + $DB->update_record('license', $license); } + self::set_active_licenses(); + return true; } /** @@ -108,13 +107,25 @@ static public function enable($license) { */ static public function disable($license) { global $DB; - if ($record = $DB->get_record('license', array('shortname'=>$license))) { - $record->enabled = 0; - $DB->update_record('license', $record); - return true; - } else { - return false; + if ($license = self::get_license_by_shortname($license)) { + $license->enabled = 0; + $DB->update_record('license', $license); } + self::set_active_licenses(); + return true; + } + + /** + * Store active licenses in global $CFG + */ + static private function set_active_licenses() { + // set to global $CFG + $licenses = self::get_licenses(array('enabled'=>1)); + $result = array(); + foreach ($licenses as $l) { + $result[] = $l->shortname; + } + set_config('licenses', implode(',', $result)); } /** @@ -123,60 +134,67 @@ static public function disable($license) { static public function install_licenses() { $license = new stdclass; + $license->shortname = 'unknown'; + $license->fullname = 'Unknown license'; + $license->source = ''; + $license->enabled = 1; + $license->version = '2010033100'; + self::add($license); + $license->shortname = 'allrightsreserved'; $license->fullname = 'All rights reserved'; $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved'; $license->enabled = 1; - $license->version = '2010032500'; + $license->version = '2010033100'; self::add($license); $license->shortname = 'public'; $license->fullname = 'Public Domain'; $license->source = 'http://creativecommons.org/licenses/publicdomain/'; $license->enabled = 1; - $license->version = '2010032500'; + $license->version = '2010033100'; self::add($license); $license->shortname = 'cc'; $license->fullname = 'Creative Commons'; $license->source = 'http://creativecommons.org/licenses/by/3.0/'; $license->enabled = 1; - $license->version = '2010032500'; + $license->version = '2010033100'; self::add($license); $license->shortname = 'cc-nd'; $license->fullname = 'Creative Commons - NoDerivs'; $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/'; $license->enabled = 1; - $license->version = '2010032500'; + $license->version = '2010033100'; self::add($license); $license->shortname = 'cc-nc-nd'; $license->fullname = 'Creative Commons - No Commercial NoDerivs'; $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/'; $license->enabled = 1; - $license->version = '2010032500'; + $license->version = '2010033100'; self::add($license); $license->shortname = 'cc-nc'; $license->fullname = 'Creative Commons - No Commercial'; $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/'; $license->enabled = 1; - $license->version = '2010032500'; + $license->version = '2010033100'; self::add($license); $license->shortname = 'cc-nc-sa'; $license->fullname = 'Creative Commons - No Commercial ShareAlike'; $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/'; $license->enabled = 1; - $license->version = '2010032500'; + $license->version = '2010033100'; self::add($license); $license->shortname = 'cc-sa'; $license->fullname = 'Creative Commons - ShareAlike'; $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/'; $license->enabled = 1; - $license->version = '2010032500'; + $license->version = '2010033100'; self::add($license); } } diff --git a/repository/lib.php b/repository/lib.php index b58144b7e0428..9fca7167061c4 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -1766,8 +1766,15 @@ function initialise_filepicker($args) { global $CFG, $USER, $PAGE, $OUTPUT; $return = new stdclass; require_once($CFG->libdir . '/licenselib.php'); + $array = explode(',', $CFG->licenses); + $licenses = array(); + foreach ($array as $license) { + $l = new stdclass; + $l->shortname = $license; + $l->fullname = get_string($license, 'license'); + $licenses[] = $l; + } - $licenses = license_manager::get(array('enabled'=>1)); $return->licenses = $licenses; $return->author = fullname($USER); diff --git a/version.php b/version.php index 1b6c380c71298..3792e43786b7f 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2010032405; // YYYYMMDD = date of the last version bump + $version = 2010033101; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 dev (Build: 20100331)'; // Human-friendly version name