Commit 66f4e94e authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Fix feedback attachment uploads

parent 4615ff7f
......@@ -741,7 +741,7 @@ abstract class ArtefactTypeFileBase extends ArtefactType {
* @param integer $group
* @param string $institution
*/
public static function get_new_file_title($desired, $parent, $owner, $group, $institution) {
public static function get_new_file_title($desired, $parent, $owner=null, $group=null, $institution=null) {
$bits = split('\.', $desired);
if (count($bits) > 1 && preg_match('/[^0-9]/', end($bits))) {
$start = join('.', array_slice($bits, 0, count($bits)-1));
......@@ -841,9 +841,6 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
* Returns a boolean indicating success or failure.
*/
public static function save_file($pathname, $data, User &$user=null, $outsidedataroot=false) {
// This is only used when blog posts are saved: Files which
// have been uploaded to the post are moved to a permanent
// location in the files area using this function.
$dataroot = get_config('dataroot');
if (!$outsidedataroot) {
$pathname = $dataroot . $pathname;
......@@ -893,9 +890,18 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
throw new UploadException($error);
}
$size = $um->file['size'];
global $USER;
if (!isset($data->institution) && !isset($data->group) && !$USER->quota_allowed($size)) {
throw new QuotaExceededException(get_string('uploadexceedsquota', 'artefact.file'));
if (!empty($data->owner)) {
global $USER;
if ($data->owner == $USER->get('id')) {
$owner = $USER;
}
else {
$owner = new User;
$owner->find_by_id($data->owner);
}
if (!$owner->quota_allowed($size)) {
throw new QuotaExceededException(get_string('uploadexceedsquota', 'artefact.file'));
}
}
$data->size = $size;
$data->filetype = $um->file['type'];
......@@ -909,11 +915,9 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
$f->delete();
throw new UploadException($error);
}
else {
if (!isset($data->institution) && !isset($data->group)) {
$USER->quota_add($size);
$USER->commit();
}
else if ($owner) {
$owner->quota_add($size);
$owner->commit();
}
return $id;
}
......
......@@ -2500,6 +2500,7 @@ function add_feedback_form_validate(Pieform $form, $values) {
function add_feedback_form_submit(Pieform $form, $values) {
global $view, $artefact, $USER;
$data = new StdClass;
$data->view = $view->get('id');
if ($artefact) {
......@@ -2517,10 +2518,62 @@ function add_feedback_form_submit(Pieform $form, $values) {
$data->authorname = $values['authorname'];
}
$data->ctime = db_format_timestamp(time());
db_begin();
if (is_array($values['attachment'])) {
require_once(get_config('libroot') . 'group.php');
require_once(get_config('libroot') . 'uploadmanager.php');
safe_require('artefact', 'file');
$groupid = $view->get('submittedgroup');
if (group_user_can_assess_submitted_views($groupid, $USER->get('id'))) {
$um = new upload_manager('attachment');
if ($error = $um->preprocess_file()) {
throw new UploadException($error);
}
$owner = $view->get('owner');
$ownerlang = get_user_language($owner);
$folderid = ArtefactTypeFolder::get_folder_id(
get_string_from_language($ownerlang, 'feedbackattachdirname', 'view'),
get_string_from_language($ownerlang, 'feedbackattachdirdesc', 'view'),
null, true, $owner
);
$attachment = (object) array(
'owner' => $owner,
'parent' => $folderid,
'title' => ArtefactTypeFileBase::get_new_file_title($values['attachment']['name'], $folderid, $owner),
'size' => $values['attachment']['size'],
'filetype' => $values['attachment']['type'],
'oldextensin' => $um->original_filename_extension(),
'description' => get_string_from_language(
$ownerlang,
'feedbackonviewbytutorofgroup',
'view',
$view->get('title'),
display_name($USER),
get_field('group', 'name', 'id', $groupid)
),
);
try {
$data->attachment = ArtefactTypeFile::save_uploaded_file('attachment', $attachment);
}
catch (QuotaExceededException $e) {}
}
}
insert_record($table, $data, 'id', true);
require_once('activity.php');
unset($data->id);
activity_occurred('feedback', $data);
db_commit();
if ($artefact) {
$goto = get_config('wwwroot') . 'view/artefact.php?artefact=' . $artefact->get('id') . '&view='.$view->get('id');
}
......
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
//safe_require('artefact', 'file');
$viewid = param_integer('view');
$message = param_variable('message', '');
$public = (int) param_boolean('public', false);
$filename = param_variable('filename');
// Upload the file into the view owner's my files area. This file
// will add to the view owner's quota, but saving the file will not
// fail if the quota is exceeded.
// Group name, view title, feedback number?
$viewdata = get_record_sql('
SELECT
v.title, v.owner, g.name
FROM {view} v
INNER JOIN {group} g ON v.submittedgroup = g.id
WHERE v.id = ' . $viewid, '');
$page = '/view/view.php?id=' . $viewid;
require_once('uploadmanager.php');
$um = new upload_manager('attachment');
if ($error = $um->preprocess_file()) {
// Should do something more sensible here, like display the error
// next to the feedback submit button.
log_info($error, false, false);
redirect($page);
}
$size = $um->file['size'];
$ownerlang = get_user_language($viewdata->owner);
safe_require('artefact', 'file');
$folderid = ArtefactTypeFolder::get_folder_id(get_string_from_language($ownerlang, 'feedbackattachdirname', 'view'),
get_string_from_language($ownerlang, 'feedbackattachdirdesc', 'view'),
null, true, $viewdata->owner);
// Create a new file object
$data = (object) array('owner' => $viewdata->owner,
'parent' => $folderid,
'size' => $size,
'filetype' => $um->file['type'],
'title' => $filename,
'description' => get_string_from_language($ownerlang, 'feedbackonviewbytutorofgroup', 'view',
$viewdata->title, display_name($USER), $viewdata->name));
if (preg_match("/\.([^\.]+)$/", $um->file['name'], $saved)) {
$data->oldextension = $saved[1];
}
$f = ArtefactTypeFile::new_file($um->file['tmp_name'], $data);
$f->commit();
$fileid = $f->get('id');
if ($error = $um->save_file(ArtefactTypeFile::get_file_directory($fileid) , $fileid)) {
$f->delete();
log_info($error, false, false);
redirect($page);
}
else {
$usr = new User();
$usr->find_by_id($viewdata->owner)
->quota_add($size)
->commit();
}
global $USER;
$tutor = $USER->get('id');
$data = (object) array('view' => $viewid,
'author' => $tutor,
'message' => $message,
'attachment' => $fileid,
'ctime' => db_format_timestamp(time()),
'public' => $public);
if (!insert_record('view_feedback', $data)) {
$f->delete();
}
require_once('activity.php');
activity_occurred('feedback', $data);
redirect('/view/view.php?id=' . $viewid);
?>
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