Commit 862c3e31 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Copy views from templates

parent 6bd72796
......@@ -131,6 +131,15 @@ class PluginBlocktypeBlog extends PluginBlocktype {
//public static function artefactchooser_get_sort_order() {
// return 'parent, ctime DESC';
//}
public static function copy_allowed($newowner=null) {
return false;
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
}
}
?>
......@@ -129,6 +129,15 @@ class PluginBlocktypeBlogpost extends PluginBlocktype {
public static function artefactchooser_get_sort_order() {
return 'parent, ctime DESC';
}
public static function copy_allowed($newowner=null) {
return false;
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
}
}
?>
......@@ -107,6 +107,15 @@ class PluginBlocktypeRecentposts extends PluginBlocktype {
public static function artefactchooser_get_sort_order() {
return 'title';
}
public static function copy_allowed($newowner=null) {
return false;
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
}
}
?>
......@@ -889,6 +889,25 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
public static function get_quota_usage($artefact) {
return get_field('artefact_file_files', 'size', 'artefact', $artefact);
}
public function copy_extra($new) {
$oldid = $this->get('id');
$dataroot = get_config('dataroot');
$oldfile = $dataroot . self::get_file_directory($oldid) . '/' . $oldid;
$newid = $new->get('id');
$newdir = $dataroot . self::get_file_directory($newid);
check_dir_exists($newdir);
if (!copy($oldfile, $newdir . '/' . $newid)) {
throw new SystemException('failed copying file artefact');
}
global $USER;
if ($new->get('owner') && $new->get('owner') == $USER->get('id')) {
$USER->quota_add($new->get('size'));
$USER->commit();
}
return;
}
}
class ArtefactTypeFolder extends ArtefactTypeFileBase {
......
......@@ -139,6 +139,15 @@ class PluginBlocktypeContactinfo extends PluginBlocktype {
public static function artefactchooser_get_sort_order() {
return '';
}
public static function copy_allowed($newowner=null) {
return $newowner['type'] == 'user';
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
}
}
?>
......@@ -200,6 +200,15 @@ class PluginBlocktypeProfileinfo extends PluginBlocktype {
public static function artefactchooser_get_sort_order() {
return '';
}
public static function copy_allowed($newowner=null) {
return $newowner['type'] == 'user';
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
}
}
?>
......@@ -147,7 +147,7 @@ abstract class ArtefactType {
if (in_array($field, array('atime', 'ctime', 'mtime'))) {
$value = strtotime($value);
}
if ($field == 'tags' && !is_array($field)) {
if ($field == 'tags' && !is_array($value)) {
$value = preg_split("/\s*,\s*/", trim($value));
}
$this->{$field} = $value;
......@@ -636,6 +636,50 @@ abstract class ArtefactType {
}
}
public function copy_data() {
$ignore = array(
'dirty' => 1,
'parentdirty' => 1,
'deleted' => 1,
'id' => 1,
'ctime' => 1,
'mtime' => 1,
'atime' => 1,
'locked' => 1,
'rolepermissions' => 1,
'viewsinstances' => 1,
'viewsmetadata' => 1,
'childreninstances' => 1,
'childrenmetadata' => 1,
'parentinstance' => 1,
'parentmetadata' => 1
);
$data = array();
foreach (get_object_vars($this) as $k => $v) {
if (!isset($ignore[$k])) {
$data[$k] = $v;
}
}
return $data;
}
public function copy_extra($new) {
return;
}
public function copy_for_new_owner($user, $group, $institution) {
$data = $this->copy_data();
$data['owner'] = $user;
$data['group'] = $group;
$data['institution'] = $institution;
$data['parent'] = null;
$classname = generate_artefact_class_name($data['artefacttype']);
safe_require('artefact', get_field('artefact_installed_type', 'plugin', 'name', $data['artefacttype']));
$copy = new $classname(0, $data);
$copy->commit();
$this->copy_extra($copy);
return $copy->get('id');
}
}
/**
......@@ -841,4 +885,18 @@ function artefact_watchlist_notification($artefactid) {
}
}
function artefact_get_descendants($new) {
$seen = array();
while ($n = array_shift($new)) {
$seen[] = $n;
$children = get_column_sql('SELECT id FROM {artefact} WHERE parent = ? AND id NOT IN (' . implode(',', $seen) . ')', array($n));
if ($children) {
foreach ($children as $c) {
$new[] = $c;
}
}
}
return $seen;
}
?>
......@@ -128,6 +128,15 @@ class PluginBlocktypeEntireresume extends PluginBlocktype {
}
}
}
public static function copy_allowed($newowner=null) {
return $newowner['type'] == 'user';
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
}
}
?>
......@@ -123,6 +123,15 @@ class PluginBlocktypeResumefield extends PluginBlocktype {
public static function artefactchooser_get_sort_order() {
return '';
}
public static function copy_allowed($newowner=null) {
return $newowner['type'] == 'user';
}
public static function copy_artefacts_allowed($newowner=null) {
return false;
}
}
?>
......@@ -615,14 +615,14 @@ class User {
return true;
}
$institution = $v->get('institution');
if ($insitution && $institution != 'mahara' && $this->can_edit_institution($institution)) {
if ($institution && $institution != 'mahara' && $this->can_edit_institution($institution)) {
return true;
}
$group = $v->get('group');
if ($group) {
$editroles = $v->get('editingroles');
$this->reset_grouproles();
return in_array($this->grouproles[$group], $editroles);
return isset($this->grouproles[$group]) && in_array($this->grouproles[$group], $editroles);
}
return false;
}
......
......@@ -155,6 +155,15 @@ abstract class PluginBlocktype extends Plugin {
}
return $blocktypes;
}
public static function copy_allowed($newowner=null) {
return true;
}
public static function copy_artefacts_allowed($newowner=null) {
return true;
}
}
abstract class SystemBlockType extends PluginBlockType {
......@@ -660,6 +669,15 @@ class BlockInstance {
}
return $a;
}
public function copy_allowed($newowner=null) {
return call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'copy_allowed', $newowner);
}
public function copy_artefacts_allowed($newowner=null) {
return call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'copy_artefacts_allowed', $newowner);
}
}
......
......@@ -1686,6 +1686,90 @@ class View {
}
}
public function ownership() {
if ($this->group) {
return array('type' => 'group', 'id' => $this->group);
}
if ($this->owner) {
return array('type' => 'user', 'id' => $this->owner);
}
if ($this->institution) {
return array('type' => 'institution', 'id' => $this->institution);
}
return null;
}
public function copy_contents($template) {
$this->set('numcolumns', $template->get('numcolumns'));
$this->set('layout', $template->get('layout'));
$blocks = get_records_array('block_instance', 'view', $template->get('id'));
if ($blocks) {
$newowner = $this->ownership();
$oldowner = $template->ownership();
$sameowner = $newowner['type'] == $oldowner['type'] && $newowner['id'] == $oldowner['id'];
$artefactcopies = array(); // Correspondence between original artefact ids and id of the copy
foreach ($blocks as $b) {
safe_require('blocktype', $b->blocktype);
$oldblock = new BlockInstance($b->id, $b);
if ($sameowner || $oldblock->copy_allowed($newowner)) {
$newblock = new BlockInstance(0, array(
'blocktype' => $oldblock->get('blocktype'),
'title' => $oldblock->get('title'),
'view' => $this->get('id'),
'column' => $oldblock->get('column'),
'order' => $oldblock->get('order'),
));
$configdata = $oldblock->get('configdata');
$artefactids = PluginBlockType::get_artefacts($oldblock);
if (!$sameowner && !empty($artefactids)) {
if ($oldblock->copy_artefacts_allowed($newowner)) {
// Copy artefacts & put the new artefact
// ids into the new block
// Get all children of the artefacts
$descendants = artefact_get_descendants($artefactids);
foreach ($descendants as $aid) {
if (!isset($artefactcopies[$aid])) {
// Copy the artefact
$a = artefact_instance_from_id($aid);
// Save the id of the original artefact's parent
$artefactcopies[$aid] = (object) array('oldid' => $aid, 'oldparent' => $a->get('parent'));
$artefactcopies[$aid]->newid = $a->copy_for_new_owner($this->owner, $this->group, $this->institution);
}
}
if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = $artefactcopies[$configdata['artefactid']]->newid;
}
else {
foreach ($configdata['artefactids'] as &$oldid) {
$oldid = $artefactcopies[$oldid]->newid;
}
}
}
else if (isset($configdata['artefactid'])) {
$configdata['artefactid'] = null;
}
else if (isset($configdata['artefactids'])) {
$configdata['artefactids'] = array();
}
}
$newblock->set('configdata', $configdata);
$newblock->commit();
}
}
// Go back and fix up the parents of the new artefacts so
// they also point to new artefacts
foreach ($artefactcopies as $c) {
if (isset($artefactcopies[$c->oldparent])) {
$a = artefact_instance_from_id($c->newid);
$a->set('parent', $artefactcopies[$c->oldparent]->newid);
$a->commit();
}
}
}
}
}
......
......@@ -61,7 +61,7 @@ $smarty->assign('form', $form);
$smarty->display('view/delete.tpl');
function deleteview_submit(Pieform $form, $values) {
global $SESSION, $viewid, $groupid, $insitution;
global $SESSION, $viewid, $groupid, $institution;
$view = new View($viewid, null);
$view->delete();
handle_event('deleteview', $viewid);
......
......@@ -53,7 +53,7 @@ if (empty($id)) {
$ownerid = ($group || $institution) ? null : $USER->get('id');
$nontemplates = View::view_search($ownerid, $group, $institution, false, null, 0, false);
$templateoptions = array(0 => 'None');
$templateoptions = array(0 => get_string('none'));
foreach ($templates->data as $t) {
$templateoptions[$t->id] = $t->title;
}
......@@ -169,7 +169,7 @@ else {
}
if ($new && count($templateoptions)) {
$editview['elements']['template'] = array(
$editview['elements']['usetemplate'] = array(
'type' => 'select',
'title' => get_string('Template','view'),
'description' => get_string('createfromtemplatedescription','view'),
......@@ -208,6 +208,7 @@ function editview_submit(Pieform $form, $values) {
if (empty($editing)) {
$view->set('numcolumns', 3); // default
$view->set('template', 0);
if ($group) {
$view->set('group', $group);
}
......@@ -223,15 +224,25 @@ function editview_submit(Pieform $form, $values) {
}
$view->commit();
if (empty($editing) && $group) {
// By default, group views should be visible to the group
$view->set_access(array(array(
'type' => 'group',
'id' => $group,
'startdate' => null,
'stopdate' => null,
'role' => null
)));
if (!$editing) {
if ($values['usetemplate']) {
$template = new View($values['usetemplate']);
if (!$template->get('deleted') && ($template->get('template') && can_view_view($values['usetemplate'])) || $USER->can_edit_view($template)) {
$view->set('dirty', true);
$view->copy_contents($template);
}
}
if ($group) {
// By default, group views should be visible to the group
$view->set_access(array(array(
'type' => 'group',
'id' => $group,
'startdate' => null,
'stopdate' => null,
'role' => null
)));
}
$view->commit();
}
......
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