Commit f0ca6b1e authored by Robert Lyon's avatar Robert Lyon Committed by Yuliya Bozhko
Browse files

Allow view to be copied from view/view.php (Bug #745418)



Have added a is_copyable() function check to see if we can
add a 'copy page' button to a view

Currently uses the view_search() function that returns all allowed views
to copy then checks if current view is one of them.

Change-Id: I083d9254ec7cabc861c02ce9cc404b4f6b313e72
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent c69f3fef
......@@ -15,6 +15,7 @@ defined('INTERNAL') || die();
$string['add'] = 'Add';
$string['addone'] = 'Add one';
$string['cancel'] = 'Cancel';
$string['copy'] = 'Copy';
$string['copytoclipboard'] = 'Copy secret URL to the clipboard';
$string['delete'] = 'Delete';
$string['deletespecific'] = 'Delete "%s"';
......
......@@ -367,6 +367,7 @@ $string['filescopiedfromviewtemplate'] = 'Files copied from %s';
$string['viewfilesdirname'] = 'viewfiles';
$string['viewfilesdirdesc'] = 'Files from copied pages';
$string['thisviewmaybecopied'] = 'Copying is allowed';
$string['thisviewmaynotbecopied'] = 'Copying is not allowed';
$string['copythisview'] = 'Copy this page';
$string['copyview'] = 'Copy page';
$string['createemptyview'] = 'Create empty page';
......
......@@ -3839,6 +3839,24 @@ class View {
return array(self::owner_sql($ownerobj), array());
}
/**
* Check if the view is copyable by the user.
*
* @return bool
*/
public function is_copyable() {
global $USER;
$search = new StdClass;
$search->copyableby = (object) array('group' => null, 'institution' => null, 'owner' => $USER->get('id'));
$results = self::view_search('', '', null, $search->copyableby, null, null, true, null, null, false, null, null, $this->id);
// Check that the this view is one the user is allowed to copy
if (!empty($results->count)) {
return true;
}
return false;
}
/**
* Get all views visible to a user. Complicated because a view v
* is visible to a user u at time t if any of the following are
......@@ -3867,10 +3885,11 @@ class View {
* @param bool $collection Use query against collection names and descriptions
* @param array $accesstypes Only return views visible due to the given access types
* @param array $tag Only return views with this tag
* @param integer $viewid Only return a particular view (find by view id)
*
*/
public static function view_search($query=null, $ownerquery=null, $ownedby=null, $copyableby=null, $limit=null, $offset=0,
$extra=true, $sort=null, $types=null, $collection=false, $accesstypes=null, $tag=null) {
$extra=true, $sort=null, $types=null, $collection=false, $accesstypes=null, $tag=null, $viewid=null) {
global $USER;
$admin = $USER->get('admin');
$loggedin = $USER->is_logged_in();
......@@ -4143,7 +4162,11 @@ class View {
}
}
}
// if we need to just check one view
if (!empty($viewid)) {
$where .= " AND v.id = ?";
$whereparams = array_merge($whereparams, array($viewid));
}
$ph = array_merge($fromparams, $whereparams);
$count = count_records_sql('SELECT COUNT(*) ' . $from . $where, $ph);
......@@ -5998,6 +6021,44 @@ function createview_submit(Pieform $form, $values) {
redirect(get_config('wwwroot') . 'view/edit.php?new=1&id=' . $view->get('id'));
}
/**
* Copy a view via a 'copy' url
* Currently for copying a page via a 'copy' button on view/view.php
*
* @param integer $id View id
* @param bool $istemplate (optional) If you want to mark as template
* @param integer $groupid Group id (optional) The group to copy the view to
*/
function copyview($id, $istemplate = false, $groupid = null) {
global $USER, $SESSION;
// check that the user can copy view
$view = new View($id);
if (!$view->is_copyable()) {
throw new AccessDeniedException(get_string('thisviewmaynotbecopied', 'view'));
}
// set up a packet of values to send to the create_from_template function
$values = array('new' => 1,
'owner' => $USER->get('id'),
'template' => (int) $istemplate,
);
if (!empty($groupid) && is_int($groupid)) {
$values['group'] = $groupid;
}
list($view, $template, $copystatus) = View::create_from_template($values, $id);
if (isset($copystatus['quotaexceeded'])) {
$SESSION->add_error_msg(get_string('viewcopywouldexceedquota', 'view'));
redirect(get_config('wwwroot') . 'view/view.php?id=' . $id);
}
$SESSION->add_ok_msg(get_string('copiedblocksandartefactsfromtemplate', 'view',
$copystatus['blocks'],
$copystatus['artefacts'],
$template->get('title'))
);
redirect(get_config('wwwroot') . 'view/edit.php?new=1&id=' . $view->get('id'));
}
function createview_cancel_submit(Pieform $form, $values) {
if (isset($values['group'])) {
redirect(get_config('wwwroot') . 'view/groupviews.php?group=' . $values['group']);
......
......@@ -2,15 +2,18 @@
{if $notrudeform}<div class="message deletemessage">{$notrudeform|safe}</div>{/if}
{if !$microheaders && ($mnethost || $editurl)}
{if !$microheaders && ($mnethost || $editurl || $copyurl)}
<div class="viewrbuttons">
{if $editurl}{strip}
{if $new}
<a class="btn" href="{$editurl}">{str tag=back}</a>
{else}
<a title="{str tag=editthisview section=view}" href="{$editurl}" class="btn editview">{str tag=editthisview section=view}</a>
<a title="{str tag=editthisview section=view}" href="{$editurl}" class="btn editview">{str tag=edit section=mahara}</a>
{/if}
{/strip}{/if}
{if $copyurl}{strip}
<a title="{str tag=copythisview section=view}" href="{$copyurl}" class="btn editview">{str tag=copy section=mahara}</a>
{/strip}{/if}
{if $mnethost}<a href="{$mnethost.url}" class="btn">{str tag=backto arg1=$mnethost.name}</a>{/if}
</div>
{/if}
......
<?php
/**
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @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.
*
*/
define('INTERNAL', 1);
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'view');
define('SECTION_PAGE', 'copy');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
$viewid = param_integer('id');
$groupid = param_integer('group', null);
$view = new View($viewid);
if (!$view->is_copyable()) {
throw new AccessDeniedException(get_string('thisviewmaynotbecopied', 'view'));
}
copyview($view->get('id'), 0, $groupid);
\ No newline at end of file
......@@ -248,6 +248,7 @@ if (get_config_plugin('blocktype', 'gallery', 'useslimbox2')) {
}
$can_edit = $USER->can_edit_view($view) && !$submittedgroup && !$view->is_submitted();
$can_copy = $view->is_copyable($view);
$viewgroupform = false;
if ($owner && $owner == $USER->get('id')) {
......@@ -347,6 +348,7 @@ if ($view->is_anonymous()) {
$titletext = ($collection && $shownav) ? hsc($collection->get('name')) : $view->display_title(true, false, false);
$smarty->assign('visitstring', $view->visit_message());
if (get_config('viewmicroheaders')) {
$microheaderlinks = array();
$smarty->assign('microheaders', true);
$smarty->assign('microheadertitle', $titletext);
......@@ -364,28 +366,36 @@ if (get_config('viewmicroheaders')) {
if ($can_edit) {
if ($new) {
$microheaderlinks = array(
array(
'name' => get_string('back'),
'url' => get_config('wwwroot') . 'view/blocks.php?id=' . $viewid . '&new=1',
'type' => 'reply',
),
$microheaderlinks[] = array(
'name' => get_string('back'),
'url' => get_config('wwwroot') . 'view/blocks.php?id=' . $viewid . '&new=1',
'type' => 'reply',
);
}
else {
$microheaderlinks = array(
array(
'name' => get_string('editthisview', 'view'),
'image' => $THEME->get_url('images/btn_edit.png'),
'url' => get_config('wwwroot') . 'view/blocks.php?id=' . $viewid,
),
$microheaderlinks[] = array(
'name' => get_string('edit', 'mahara'),
'image' => $THEME->get_url('images/btn_edit.png'),
'url' => get_config('wwwroot') . 'view/blocks.php?id=' . $viewid,
);
}
$smarty->assign('microheaderlinks', $microheaderlinks);
}
if ($can_copy) {
$microheaderlinks[] = array(
'name' => get_string('copy', 'mahara'),
'image' => $THEME->get_url('images/btn_edit.png'),
'url' => get_config('wwwroot') . 'view/copy.php?id=' . $viewid,
);
}
$smarty->assign('microheaderlinks', $microheaderlinks);
}
else if ($can_edit) {
$smarty->assign('editurl', get_config('wwwroot') . 'view/blocks.php?id=' . $viewid . ($new ? '&new=1' : ''));
else {
if ($can_edit) {
$smarty->assign('editurl', get_config('wwwroot') . 'view/blocks.php?id=' . $viewid . ($new ? '&new=1' : ''));
}
if ($can_copy) {
$smarty->assign('copyurl', get_config('wwwroot') . 'view/copy.php?id=' . $viewid);
}
}
$title = hsc(TITLE);
......
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