license.php 13.5 KB
Newer Older
1 2 3 4 5 6
<?php
/**
 *
 * @package    mahara
 * @subpackage core
 * @author     Catalyst IT Ltd
7 8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9 10 11 12 13
 * @copyright  (C) 2012 Catalyst IT Australia Pty Ltd http://catalyst-au.net
 */

defined('INTERNAL') || die();

14 15
define('LICENSE_INSTITUTION_DEFAULT', '(institution default)');

16 17 18
/**
 * Given an artefact object, return the first of the two pieform elements.
 *
19 20
 * @param object $artefact The artefact
 * @param boolean $always_allow_none True to indicate that it should definitely include "allow none"; otherwise the function decides
21 22 23 24 25
 * @return array  A pieform element (license field).
 */
function license_form_el_basic($artefact, $always_allow_none=false) {
    if (!get_config('licensemetadata')) {
        return array(
26
            'type' => 'hidden',
27 28 29 30 31
            'ignore' => true,
        );
    }
    global $USER;
    $licenses = get_records_assoc('artefact_license', null, null, 'displayname');
32 33 34 35
    if ($licenses) {
        foreach ($licenses as $l) {
            $options[$l->name] = $l->displayname;
        }
36 37
    }

38 39 40
    // Determine whether to include the "none selected" option in the list of licenses
    $include_noneselected = false;
    // If it was passed in as a param, then we will include "none selected"
41
    if ($always_allow_none) {
42
        $include_noneselected = true;
43 44 45 46 47
    }

    $institution = $USER->get('institutions');
    if ($institution) {
        $institution = array_shift($institution);
48
        // If the user's institution is not set to "license mandatory", then we will include "none selected"
49
        if (empty($institution->licensemandatory)) {
50
            $include_noneselected = true;
51 52 53
        }
    }
    else {
54 55 56 57
        // If the user has no institution, then we will include "none selected"
        $include_noneselected = true;
    }
    if ($include_noneselected) {
58
        $options[''] = get_string('licensenone1');
59
    }
60

61 62 63
    if (empty($artefact)) {
        // Find the correct default license.
        $license = $USER->get_account_preference('licensedefault');
64 65
        // If the user is set to "institution default"
        if ($license == LICENSE_INSTITUTION_DEFAULT) {
66 67 68 69 70 71 72
            if ($institution and isset($institution->licensedefault)) {
                $license = $institution->licensedefault;
            }
            else {
                $license = '';
            }
        }
73 74

        if (!isset($options[$license]) && !get_config('licenseallowcustom')) {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            // Note: this won't happen normally, but it can happen for instance
            // if the site admin removes a license which is the default for the
            // user's institution.
            $license = array_keys($licenses);
            $license = array_shift($license);
        }
    }
    else {
        $license = $artefact->get('license');
        if (empty($license)) {
            $license = '';
        }
        if (!isset($options[$license])) {
            $options[$license] = $license;
        }
    }

    $res = array(
        'defaultvalue' => $license,
        'type'         => 'select',
        'options'      => $options,
        'title'        => get_string('license'),
        'description'  => get_string('licensedesc'),
        'help'         => true,
    );
    if (get_config('licenseallowcustom')) {
        $res['allowother'] = true;
102
        $res['options']['other'] = get_string('licenseother');
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    }
    if (!isset($options[''])) {
        $res['rules'] = array('required' => true);
    }
    return $res;
}


/**
 * Given an artefact object, return the second of the two pieform elements.
 *
 * @param object  The artefact
 * @param string  A prefix for each of the element names; default ''.
 * @return array  A pieform element (fieldset with licensor and licensorurl fields).
 */
function license_form_el_advanced($artefact, $prefix = '') {
    if (!get_config('licensemetadata')) {
        return array(
121
            'type' => 'hidden',
122 123 124 125 126 127 128
            'ignore' => true,
        );
    }
    if (!empty($artefact)) {
        $licensor = $artefact->get('licensor');
        $licensorurl = $artefact->get('licensorurl');
    }
129 130 131 132
    else {
        $licensor = '';
        $licensorurl = '';
    }
133 134 135 136
    return array(
        'type'        => 'fieldset',
        'collapsible' => true,
        'collapsed'   => true,
137
        'class'       => 'last with-formgroup',
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        'legend'      => get_string('licensingadvanced'),
        'elements'    => array(
            $prefix . 'licensor' => array(
                'defaultvalue' => $licensor,
                'type'         => 'text',
                'title'        => get_string('licensor'),
                'description'  => get_string('licensordesc'),
                'help'         => true,
            ),
            $prefix . 'licensorurl' => array(
                'defaultvalue' => $licensorurl,
                'type'         => 'text',
                'title'        => get_string('licensorurl'),
                'description'  => get_string('licensorurldesc'),
                'help'         => true,
            ),
        )
    );

}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/**
 * Given the old license value and the values from the form license and
 * license_other elements, calculate the correct new license value. This is
 * mostly used by the file artefact, because it doesn't use pieforms;
 * everything else uses the similar logic in pieform_element_select_get_value.
 *
 * @param string   Old license value.
 * @param string   Value of the license form element.
 * @param string   Value of the license_other form element.
 * @param string   Optional out argument giving the reason for returning the old license.
 * @return string  New license value.
 */
function license_coalesce($old_license, $new_license, $new_license_other, &$error = '') {

    global $USER;
    if ($new_license === 'other') {
        $new_license = trim($new_license_other);
    }

    $institution = $USER->get('institutions');
    if ($institution) {
        $institution = array_shift($institution);
        if (!empty($institution->licensemandatory) and trim($new_license)==='') {
            $error = get_string('licensemandatoryerror');
            return $old_license;
        }
    }

    if (get_config('licenseallowcustom') or $new_license === $old_license) {
        return $new_license;
    }

    if (record_exists('artefact_license', 'name', $new_license)) {
        return $new_license;
    }
    else {
        $error = get_string('licensenocustomerror');
        return $old_license;
    }
}

/**
 * Given an artefact object, return the form already rendered.
 *
 * @param object   The artefact
 * @return string  HTML containing <tr> tags at the top level.
 */
function license_form_files($prefix, $prefix2=null) {
    if (!get_config('licensemetadata')) {
        return '';
    }
    if ($prefix2 !== null) {
        $prefix .= '_' . $prefix2;
    }
    $form = array(
        'name' => $prefix,
        'plugintype' => 'artefact',
        'pluginname' => 'file',
        'elements' => array(
            $prefix . '_license' => license_form_el_basic(null),
            'license_advanced' => license_form_el_advanced(null, $prefix . '_'),
        ),
    );
222
    $pie = pieform_instance($form);
223 224 225 226 227 228
    $pie->build();
    $rendered = $pie->get_property('elements');
    if (empty($form['elements'][$prefix . '_license']['rules']['required'])) {
        $rowattr = '';
    }
    else {
229
        $rowattr = 'required';
230 231 232 233 234 235 236 237
    }
    $html = '';
    foreach (array(
        $rendered[$prefix . '_license'],
        $rendered['license_advanced']['elements'][$prefix . '_licensor'],
        $rendered['license_advanced']['elements'][$prefix . '_licensorurl'],
    ) as $e) {
        $helphtml = preg_replace('/files_filebrowser_(edit_)?licens/', 'licens', $e['helphtml']);
238
        $html .= '<div class="form-group'.' '.$rowattr.'">' . $e['labelhtml'] . '' .
Pat Kira's avatar
Pat Kira committed
239
                 '' . $e['html'] . $helphtml . '</div>';
240 241 242 243 244 245
        $rowattr = '';
    }
    $html = str_replace(
        'id="' . $prefix . '_' . $prefix . '_',
        'id="' . $prefix . '_',
        $html);
246 247 248 249
    $html = str_replace(
        'for="' . $prefix . '_' . $prefix . '_',
        'for="' . $prefix . '_',
        $html);
250 251 252
    return $html;
}

253 254 255 256
/**
 * Given an artefact object, render the license information.
 */
function render_license($artefact) {
257 258
    global $USER;

259 260 261
    if (!$artefact || !($artefact instanceof ArtefactType)) {
        throw new MaharaException('The object ' . $artefact . 'is NOT an artefact');
    }
262 263 264
    $license = $artefact->get('license');
    $licensor = $artefact->get('licensor');
    $licensorurl = $artefact->get('licensorurl');
265 266 267 268 269 270 271 272 273
    // TODO: Should probably rewrite this URL cleanup code
    if ($licensorurl) {
        if ($licensor == '') {
            $licensor = preg_replace('(^https?://)', '', $license);
        }
        if (strpos($licensorurl, '://') === FALSE) {
            $licensorurl = 'http://' . $licensorurl;
        }
    }
274 275 276 277

    if ($license) {
        $details = get_record('artefact_license', 'name', $license);
    }
278
    else {
279 280 281 282 283
        // No license selected, so it's the default "all rights reserved" to the copyright holder
        if ($licensor) {
            $copyrightholder = $licensor;
        }
        else {
284
            $copyrightholder = hsc($artefact->display_owner());
285 286 287 288 289 290
        }

        if ($licensorurl) {
            $copyrightholder = '<a href="' . hsc($licensorurl) . '" class="licensor">' . hsc($copyrightholder) . "</a>";
        }
        return get_string('licensenonedetailed1', 'mahara', $copyrightholder);
291
    }
292

293 294 295 296 297 298 299
    if (strpos($license, '://') === FALSE) {
        $license = 'http://' . $license;
    }

    if (!empty($details)) {
        $html = '<a href="' . hsc($license) . '" class="license">';
        if ($details->icon) {
300
            $html .= '<img src="' . license_icon_url($details->icon) . '" class="license-icon"' .
301 302 303 304 305 306 307 308 309 310 311 312 313
                'alt="' . get_string('licenseiconalt') . '">';
        }
        $html .= hsc($details->displayname) . '</a>';
        if ($details->shortname != '') {
            $html .= ' (' . $details->shortname . ')';
        }
    }
    else {
        // No details configured, just format it up as a linked URL.
        $html = '<a href="' . hsc($license) . '" class="license">' .
            hsc(preg_replace('(^https?://)', '', $license)) . '</a>';
    }

314
    if ($licensorurl) {
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        $html .= '<br>' . get_string('licensor') . ': '
              . '<a href="' . hsc($licensorurl) . '" class="licensor">'
              . hsc($licensor) . '</a>';
    }
    else if ($licensor != '') {
        $html .= '<br>' . get_string('licensor') . ': '
              . '<span class="licensor">' . hsc($licensor) . '</span>';
    }
    else {
        $html .= '<br><span class="licensor"></span>';
    }


    return $html;
}

/**
 * Return the URL for a given icon.
 */
function license_icon_url($icon) {
    global $THEME;
336
    if (preg_match('/^license:([a-z_-]+)\.png$/', $icon, $m)) {
337
        $icon = $THEME->get_image_url('license/' . $m[1]);
338 339 340
    }
    return $icon;
}
341 342 343 344 345

/**
 * Install the initial set of licenses (if the license table is empty).
 */
function install_licenses_default() {
346
    $exist = record_exists('artefact_license');
347 348 349 350
    if ($exist) {
        return;
    }

351
    $license = new stdClass();
352
    $license->name = 'http://creativecommons.org/licenses/by-sa/4.0/';
353 354 355 356 357
    $license->displayname = get_string('licensedisplaynamebysa', 'install');
    $license->shortname = get_string('licenseshortnamebysa', 'install');
    $license->icon = 'license:by-sa.png';
    insert_record('artefact_license', $license);

358
    $license = new stdClass();
359
    $license->name = 'http://creativecommons.org/licenses/by/4.0/';
360 361 362 363 364
    $license->displayname = get_string('licensedisplaynameby', 'install');
    $license->shortname = get_string('licenseshortnameby', 'install');
    $license->icon = 'license:by.png';
    insert_record('artefact_license', $license);

365
    $license = new stdClass();
366
    $license->name = 'http://creativecommons.org/licenses/by-nd/4.0/';
367 368 369 370 371
    $license->displayname = get_string('licensedisplaynamebynd', 'install');
    $license->shortname = get_string('licenseshortnamebynd', 'install');
    $license->icon = 'license:by-nd.png';
    insert_record('artefact_license', $license);

372
    $license = new stdClass();
373
    $license->name = 'http://creativecommons.org/licenses/by-nc-sa/4.0/';
374 375 376 377 378
    $license->displayname = get_string('licensedisplaynamebyncsa', 'install');
    $license->shortname = get_string('licenseshortnamebyncsa', 'install');
    $license->icon = 'license:by-nc-sa.png';
    insert_record('artefact_license', $license);

379
    $license = new stdClass();
380
    $license->name = 'http://creativecommons.org/licenses/by-nc/4.0/';
381 382 383 384 385
    $license->displayname = get_string('licensedisplaynamebync', 'install');
    $license->shortname = get_string('licenseshortnamebync', 'install');
    $license->icon = 'license:by-nc.png';
    insert_record('artefact_license', $license);

386
    $license = new stdClass();
387
    $license->name = 'http://creativecommons.org/licenses/by-nc-nd/4.0/';
388 389 390 391 392
    $license->displayname = get_string('licensedisplaynamebyncnd', 'install');
    $license->shortname = get_string('licenseshortnamebyncnd', 'install');
    $license->icon = 'license:by-nc-nd.png';
    insert_record('artefact_license', $license);

393
    $license = new stdClass();
394 395 396 397 398 399 400
    $license->name = 'http://www.gnu.org/copyleft/fdl.html';
    $license->displayname = get_string('licensedisplaynamegfdl', 'install');
    $license->shortname = get_string('licenseshortnamegfdl', 'install');
    $license->icon = 'license:gfdl.png';
    insert_record('artefact_license', $license);

}