Commit db8d9cc7 authored by Jiri Baum's avatar Jiri Baum
Browse files

License metadata - file. (Bug #1095499)



Adds license metadata to the file artefact type, which doesn't use pieforms.

Change-Id: I0d486c8859d2dc0136f7c485b23df393ddb8b568
Signed-off-by: default avatarJiri Baum <jiri@catalyst-au.net>
parent 0428c0e9
......@@ -35,6 +35,7 @@ defined('INTERNAL') || die();
* @return string The HTML for the element
*/
function pieform_element_filebrowser(Pieform $form, $element) {
require_once('license.php');
global $USER, $_PIEFORM_FILEBROWSERS;
$smarty = smarty_core();
......@@ -112,6 +113,11 @@ function pieform_element_filebrowser(Pieform $form, $element) {
$config['simpleupload'] = 1;
}
$licensing = '<table class="fileuploadlicense"><tbody>'
. license_form_files($prefix)
. '</tbody></table>';
$smarty->assign('licenseform', $licensing);
if ($config['resizeonuploaduseroption'] == 1) {
$smarty->assign('resizeonuploadenable', get_config_plugin('artefact', 'file', 'resizeonuploadenable'));
$smarty->assign('resizeonuploadmaxwidth', get_config_plugin('artefact', 'file', 'resizeonuploadmaxwidth'));
......@@ -217,6 +223,7 @@ function pieform_element_filebrowser_build_path($form, $element, $folder, $owner
function pieform_element_filebrowser_build_filelist($form, $element, $folder, $highlight=null, $user=null, $group=null, $institution=null) {
require_once('license.php');
if (!$form->submitted_by_js()) {
// We're going to rebuild the page from scratch anyway.
return;
......@@ -432,6 +439,7 @@ function pieform_element_filebrowser_get_value(Pieform $form, $element) {
function pieform_element_filebrowser_doupdate(Pieform $form, $element) {
require_once('license.php');
$result = null;
$prefix = $form->get_name() . '_' . $element['name'];
......@@ -475,6 +483,15 @@ function pieform_element_filebrowser_doupdate(Pieform $form, $element) {
'folder' => $element['folder'],
'allowcomments' => param_boolean($prefix . '_edit_allowcomments'),
);
if (get_config('licensemetadata')) {
$data = array_merge($data, array(
'license' => license_coalesce(null,
param_variable($prefix . '_edit_license'),
param_variable($prefix . '_edit_license_other', null)),
'licensor' => param_variable($prefix . '_edit_licensor'),
'licensorurl' => param_variable($prefix . '_edit_licensorurl'),
));
}
if ($form->get_property('group')) {
$data['permissions'] = array('admin' => (object) array('view' => true, 'edit' => true, 'republish' => true));
foreach ($_POST as $k => $v) {
......@@ -560,13 +577,23 @@ function pieform_element_filebrowser_doupdate(Pieform $form, $element) {
'browse' => 1,
);
}
$result = pieform_element_filebrowser_upload($form, $element, array(
$data = array(
'userfile' => $_FILES['userfile'],
'uploadnumber' => param_integer($prefix . '_uploadnumber'),
'uploadfolder' => $element['folder'] ? $element['folder'] : null,
'uploadfoldername' => param_variable($prefix . '_foldername'),
'resizeonuploaduserenable' => $resizeimage,
));
);
if (get_config('licensemetadata')) {
$data = array_merge($data, array(
'license' => license_coalesce(null,
param_variable($prefix . '_edit_license'),
param_variable($prefix . '_edit_license_other', null)),
'licensor' => param_variable($prefix . '_edit_licensor'),
'licensorurl' => param_variable($prefix . '_edit_licensorurl'),
));
}
$result = pieform_element_filebrowser_upload($form, $element, $data);
// If it's a non-js upload, automatically select the newly uploaded file.
$result['browse'] = 1;
if (!$form->submitted_by_js() && !$result['error'] && !empty($element['config']['select'])) {
......@@ -596,14 +623,24 @@ function pieform_element_filebrowser_doupdate(Pieform $form, $element) {
'message' => get_string('nametoolong', 'artefact.file'),
);
}
$result['multiuploads'][$i] = pieform_element_filebrowser_upload($form, $element, array(
$data = array(
'userfile' => $_FILES['userfile'],
'userfileindex' => $i,
'uploadnumber' => param_integer($prefix . '_uploadnumber') - ($size - $i - 1),
'uploadfolder' => $element['folder'] ? $element['folder'] : null,
'uploadfoldername' => param_variable($prefix . '_foldername'),
'resizeonuploaduserenable' => $resizeimage,
));
);
if (get_config('licensemetadata')) {
$data = array_merge($data, array(
'license' => license_coalesce(null,
param_variable($prefix . '_license'),
param_variable($prefix . '_license_other', null)),
'licensor' => param_variable($prefix . '_licensor'),
'licensorurl' => param_variable($prefix . '_licensorurl'),
));
}
$result['multiuploads'][$i] = pieform_element_filebrowser_upload($form, $element, $data);
// TODO, what to do here...
// If it's a non-js upload, automatically select the newly uploaded file.
$result['multiuploads'][$i]['browse'] = 1;
......@@ -708,6 +745,11 @@ function pieform_element_filebrowser_upload(Pieform $form, $element, $data) {
$parentfolder = $data['uploadfolder'] ? (int) $data['uploadfolder'] : null;
$institution = $form->get_property('institution');
$group = $form->get_property('group');
if (get_config('licensemetadata')) {
$license = $data['license'];
$licensor = $data['licensor'];
$licensorurl = $data['licensorurl'];
}
$uploadnumber = (int) $data['uploadnumber'];
$editable = (int) $element['config']['edit'];
$selectable = (int) $element['config']['select'];
......@@ -727,6 +769,11 @@ function pieform_element_filebrowser_upload(Pieform $form, $element, $data) {
$data = new StdClass;
$data->parent = $parentfolder;
$data->owner = null;
if (get_config('licensemetadata')) {
$data->license = $license;
$data->licensor = $licensor;
$data->licensorurl= $licensorurl;
}
if ($parentfolder) {
$parentartefact = artefact_instance_from_id($parentfolder);
......@@ -1025,6 +1072,17 @@ function pieform_element_filebrowser_update(Pieform $form, $element, $data) {
$artefact->set('tags', $newtags);
}
if (get_config('licensemetadata')) {
foreach (array('license', 'licensor', 'licensorurl') as $licensef) {
if ($data[$licensef] !== null) {
$data[$licensef] = trim($data[$licensef]);
if ($artefact->get($licensef) !== $data[$licensef]) {
$artefact->set($licensef, $data[$licensef]);
}
}
}
}
if ($form->get_property('group') && $data['permissions']) {
$artefact->set('rolepermissions', $data['permissions']);
}
......
......@@ -257,6 +257,25 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
}
$(self.id + '_edit_title').value = self.filedata[id].title;
$(self.id + '_edit_description').value = self.filedata[id].description == null ? '' : self.filedata[id].description;
if ($(self.id + '_edit_license')) {
if (self.filedata[id].license == null) {
$(self.id + '_edit_license').value = ''
}
else {
$(self.id + '_edit_license').value = self.filedata[id].license;
if ($(self.id + '_edit_license').value != self.filedata[id].license) {
// Doesn't exist in the select box, add it!
var new_option = jQuery('<option/>');
new_option.attr('value', self.filedata[id].license);
new_option.text(self.filedata[id].license);
jQuery($(self.id + '_edit_license')).append(new_option);
$(self.id + '_edit_license').value = self.filedata[id].license;
}
}
$(self.id + '_edit_licensor').value = self.filedata[id].licensor == null ? '' : self.filedata[id].licensor;
$(self.id + '_edit_licensorurl').value = self.filedata[id].licensorurl == null ? '' : self.filedata[id].licensorurl;
pieform_select_other($(self.id + '_edit_license'));
}
$(self.id + '_edit_allowcomments').checked = self.filedata[id].allowcomments;
$(self.id + '_edit_tags').value = self.filedata[id].tags.join(', ');
replaceChildNodes($(self.id + '_edit_messages'));
......@@ -602,6 +621,15 @@ function FileBrowser(idprefix, folderid, config, globalconfig) {
if (self.filedata[id].tags) {
self.selecteddata[id].tags = self.filedata[id].tags;
}
if (self.filedata[id].license) {
self.selecteddata[id].license = self.filedata[id].license;
}
if (self.filedata[id].licensor) {
self.selecteddata[id].licensor = self.filedata[id].licensor;
}
if (self.filedata[id].licensorurl) {
self.selecteddata[id].licensorurl = self.filedata[id].licensorurl;
}
// Check if the file to add was already in the selected list
var existed = false;
for (i = 0; i < rows.length; i++) {
......
......@@ -155,6 +155,7 @@ $string['savechanges'] = 'Save changes';
$string['selectafile'] = 'Select a file';
$string['selectingfailed'] = 'Selecting failed: file or folder does not exist any more.';
$string['Size'] = 'Size';
$string['License'] = 'License';
$string['spaceused'] = 'Space used';
$string['timeouterror'] = 'File upload failed: try uploading the file again.';
$string['title'] = 'Name';
......
......@@ -374,7 +374,7 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
global $USER;
$select = '
SELECT
a.id, a.artefacttype, a.mtime, f.size, a.title, a.description, a.locked, a.allowcomments, u.profileicon AS defaultprofileicon,
a.id, a.artefacttype, a.mtime, f.size, a.title, a.description, a.license, a.licensor, a.licensorurl, a.locked, a.allowcomments, u.profileicon AS defaultprofileicon,
COUNT(DISTINCT c.id) AS childcount, COUNT (DISTINCT aa.artefact) AS attachcount, COUNT(DISTINCT va.view) AS viewcount,
COUNT(DISTINCT api.id) AS profileiconcount';
$from = '
......@@ -402,7 +402,7 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
$groupby = '
GROUP BY
a.id, a.artefacttype, a.mtime, f.size, a.title, a.description, a.locked, a.allowcomments,
a.id, a.artefacttype, a.mtime, f.size, a.title, a.description, a.license, a.licensor, a.licensorurl, a.locked, a.allowcomments,
u.profileicon';
$phvals = array();
......@@ -1054,6 +1054,7 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
}
public function render_self($options) {
require_once('license.php');
$options['id'] = $this->get('id');
$downloadpath = get_config('wwwroot') . 'artefact/file/download.php?file=' . $this->get('id');
......@@ -1073,6 +1074,12 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$smarty->assign('created', strftime(get_string('strftimedaydatetime'), $this->get('ctime')));
$smarty->assign('modified', strftime(get_string('strftimedaydatetime'), $this->get('mtime')));
$smarty->assign('size', $this->describe_size() . ' (' . $this->get('size') . ' ' . get_string('bytes', 'artefact.file') . ')');
if (get_config('licensemetadata')) {
$smarty->assign('license', render_license($this));
}
else {
$smarty->assign('license', false);
}
foreach (array('title', 'description', 'artefacttype', 'owner', 'tags') as $field) {
$smarty->assign($field, $this->get($field));
......
......@@ -125,6 +125,17 @@ try {
}
catch (ParameterException $e) { }
if (get_config('licensemetadata')) {
// Set licensing information
try {
$license = license_coalesce(null,
param_variable('license'), param_variable('license_other', null));
$licensor = param_variable('licensor');
$licensorurl = param_variable('licensorurl');
}
catch (ParameterException $e) { }
}
try {
$newid = ArtefactTypeFile::save_uploaded_file('userfile', $data);
}
......
......@@ -10,5 +10,8 @@
<tr><th>{str tag=Created section=artefact.file}:</th><td>{$created}</td></tr>
<tr><th>{str tag=lastmodified section=artefact.file}:</th><td>{$modified}</td></tr>
<tr><th>{str tag=Size section=artefact.file}:</th><td>{$size}</td></tr>
{if $license!==false}
<tr><th>{str tag=License section=artefact.file}:</th><td>{$license|safe}</td></tr>
{/if}
<tr><th>{str tag=Download section=artefact.file}:</th><td><a href="{$downloadpath}">{str tag=Download section=artefact.file}</a></td></tr>
</table>
......@@ -58,6 +58,7 @@
</td>
</tr>
{/if}
{license_form_files($prefix, 'edit')}
<tr>
<th><label>{str tag=allowcomments section=artefact.comment}</label></th>
<td><input type="checkbox" name="{$prefix}_edit_allowcomments" id="{$prefix}_edit_allowcomments" {if $fileinfo->allowcomments}checked {/if}/></td>
......
......@@ -50,6 +50,7 @@
{$agreementtext|clean_html|safe}
</div>
{/if}
{$licenseform|safe}
<div class="uploadform">
<label>{if $config.simpleupload}{str tag='uploadfile' section='artefact.file'}{else}{str tag='File' section='artefact.file'}{/if}</label>
<span id="{$prefix}_userfile_container"><input type="file" class="file" id="{$prefix}_userfile" name="userfile[]" multiple size="20" /></span>
......
......@@ -126,6 +126,10 @@ function license_form_el_advanced($artefact, $prefix = '') {
$licensor = $artefact->get('licensor');
$licensorurl = $artefact->get('licensorurl');
}
else {
$licensor = '';
$licensorurl = '';
}
return array(
'type' => 'fieldset',
'collapsible' => true,
......@@ -151,6 +155,97 @@ function license_form_el_advanced($artefact, $prefix = '') {
}
/**
* 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 '';
}
require_once('pieforms/pieform.php');
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 . '_'),
),
);
$pie = new Pieform($form);
$pie->build();
$rendered = $pie->get_property('elements');
if (empty($form['elements'][$prefix . '_license']['rules']['required'])) {
$rowattr = '';
}
else {
$rowattr = 'class="required"';
}
$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']);
$html .= '<tr ' . $rowattr . '><th>' . $e['labelhtml'] . '</th>' .
'<td>' . $e['html'] . $helphtml . '</td></tr>';
$rowattr = '';
}
$html = str_replace(
'id="' . $prefix . '_' . $prefix . '_',
'id="' . $prefix . '_',
$html);
return $html;
}
/**
* Given an artefact object, render the license information.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment