Commit 0aa6064c authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Reorder create view wizard

parent 5e3163b0
......@@ -27,8 +27,8 @@
defined('INTERNAL') || die();
$string['createview'] = 'Create View';
$string['createviewstepone'] = 'Create View Step One: Details';
$string['createviewsteptwo'] = 'Create View Step Two: Layout';
$string['createviewstepone'] = 'Create View Step One: Layout';
$string['createviewsteptwo'] = 'Create View Step Two: Details';
$string['createviewstepthree'] = 'Create View Step Three: Access';
$string['editviewdetails'] = 'Edit Details for View "%s"';
$string['editblocksforview'] = 'Edit View "%s"';
......@@ -243,5 +243,9 @@ $string['filescopiedfromviewtemplate'] = 'Files copied from %s';
$string['viewfilesdirname'] = 'viewfiles';
$string['viewfilesdirdesc'] = 'Files from copied views';
$string['thisviewmaybecopied'] = 'Copying is allowed';
$string['copythisview'] = 'Copy this View';
$string['createemptyview'] = 'Create Empty View';
$string['copyaview'] = 'Copy a View';
$string['Untitled'] = 'Untitled';
?>
......@@ -1452,6 +1452,21 @@ class View {
}
public static function owner_sql($userid=null, $groupid=null, $institution=null) {
if ($institution) {
return 'institution = ' . db_quote($institution);
}
if ($groupid) {
return '"group" = ' . (int)$groupid;
}
if ($userid) {
return 'owner = ' . (int)$userid;
}
return '';
}
/**
* 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
......@@ -1484,22 +1499,12 @@ class View {
$where = '
WHERE TRUE';
$ph = array();
if ($ownerid) {
$where .= '
AND v.owner = ?';
$ph[] = $ownerid;
}
else if ($groupid) {
$where .= '
AND v.group = ?';
$ph[] = $groupid;
}
else if ($institution) {
$where .= '
AND v.institution = ?';
$ph[] = $institution;
if ($ownerid || $groupid || $institution) {
$where .= ' AND v.' . self::owner_sql($ownerid, $groupid, $institution);
}
$ph = array();
if ($template) {
$where .= '
AND v.template = 1';
......@@ -1827,6 +1832,143 @@ class View {
return $numcopied;
}
public static function new_title($user, $group, $institution) {
$title = get_string('Untitled', 'view');
$taken = get_column_sql('
SELECT title
FROM {view}
WHERE ' . self::owner_sql($user, $group, $institution) . "
AND title LIKE ? || '%'", array($title));
$ext = ''; $i = 0;
if ($taken) {
while (in_array($title . $ext, $taken)) {
$ext = ' (' . ++$i . ')';
}
}
return $title . $ext;
}
}
function create_view_form($group=null, $institution=null, $templatechooser=null) {
global $USER;
$form = array(
'name' => 'createview',
'method' => 'post',
'plugintype' => 'core',
'pluginname' => 'view',
'elements' => array(
'new' => array(
'type' => 'hidden',
'value' => true,
),
)
);
if ($group) {
$form['elements']['group'] = array(
'type' => 'hidden',
'value' => $group,
);
}
else if ($institution) {
$form['elements']['institution'] = array(
'type' => 'hidden',
'value' => $institution,
);
}
if ($templatechooser) {
// Get templates visible to the user and non-templates owned by
// the owner of the view being created
$templates = View::view_search(null, null, null, true, null, 0, false);
$ownerid = ($group || $institution) ? null : $USER->get('id');
$nontemplates = View::view_search($ownerid, $group, $institution, false, null, 0, false);
$templateoptions = array(0 => get_string('none'));
foreach ($templates->data as $t) {
$templateoptions[$t->id] = $t->title;
}
foreach ($nontemplates->data as $t) {
$templateoptions[$t->id] = $t->title;
}
$form['elements']['usetemplate'] = array(
'type' => 'select',
'title' => get_string('Template','view'),
'description' => get_string('createfromtemplatedescription','view'),
'options' => $templateoptions,
);
$form['elements']['submit'] = array(
'type' => 'submitcancel',
'value' => array(get_string('createview', 'view'), get_string('cancel')),
);
}
else {
$form['renderer'] = 'oneline';
$form['elements']['submit'] = array(
'type' => 'submit',
'value' => get_string('createview', 'view'),
);
}
return $form;
}
function createview_submit(Pieform $form, $values) {
global $USER, $SESSION;
$group = isset($values['group']) ? (int) $values['group'] : null;
$institution = isset($values['institution']) ? (int) $values['institution'] : null;
$templateid = isset($values['usetemplate']) ? (int) $values['usetemplate'] : null;
$owner = ($group || $institution) ? null : $USER->get('id');
if ($group && !group_user_can_edit_views($group)
|| $institution && !$USER->can_edit_institution($institution)) {
throw new AccessDeniedException();
}
// Create a new view
$data = (object) array(
'numcolumns' => 3,
'template' => 0,
'group' => $group,
'institution' => $institution,
'owner' => $owner,
'title' => View::new_title($owner, $group, $institution),
);
$view = new View(0, $data);
$view->commit(); // copy_contents call below needs a view id
if ($templateid) {
$template = new View($templateid);
if (!$template->get('deleted') && ($template->get('template') && can_view_view($templateid)) || $USER->can_edit_view($template)) {
$view->set('dirty', true);
$copystatus = $view->copy_contents($template);
$SESSION->add_ok_msg(get_string('copiedblocksandartefactsfromtemplate', 'view', $copystatus['blocks'], $copystatus['artefacts'], $template->get('title')));
}
}
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();
redirect(get_config('wwwroot') . 'view/blocks.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']);
}
if (isset($values['institution'])) {
redirect(get_config('wwwroot') . 'view/institutionviews.php?institution=' . $values['institution']);
}
redirect(get_config('wwwroot') . 'view/');
}
......
......@@ -55,12 +55,7 @@
<form action="{$WWWROOT}view/edit.php" method="GET">
<input type="hidden" name="id" value="{$view}">
<input type="hidden" name="new" value="1">
<input type="submit" class="submit" value="{str tag='back'}">
</form>
<form action="{$WWWROOT}view/access.php" method="GET">
<input type="hidden" name="id" value="{$view}">
<input type="hidden" name="new" value="1">
<input class="submit" type="submit" value="{str tag='next'}">
<input type="submit" class="submit" value="{str tag='next'}">
</form>
{else}
<form action="{$WWWROOT}view/{if $groupid}groupviews.php{elseif $institution}institutionviews.php{/if}" method="GET">
......
{include file="header.tpl"}
{include file="columnfullstart.tpl"}
<h2>{$heading}</h2>
{$choosetemplate}
{include file="columnfullend.tpl"}
{include file="footer.tpl"}
......@@ -3,7 +3,8 @@
{include file="columnleftstart.tpl"}
<span class="addicon fr">
<a href="{$WWWROOT}view/edit.php{if $groupid}?group={$groupid}{elseif $institution}?institution={$institution}{/if}">{str tag="createview" section="view"}</a>
{$createviewform}
<a href="{$WWWROOT}view/choosetemplate.php{if $groupid}?group={$groupid}{elseif $institution}?institution={$institution}{/if}">{str tag="copyaview" section="view"}</a>
</span>
<h2>{$heading}</h2>
......
......@@ -2,11 +2,6 @@
{include file="sidebar.tpl"}
{include file="columnleftstart.tpl"}
{if ($caneditgroupview)}
<span class="addicon fr">
<a href="{$WWWROOT}view/edit.php?group={$groupid}">{str tag="createview" section="view"}</a>
</span>
{/if}
<h2>{$heading}</h2>
{include file="group/tabstart.tpl" current="views"}
......@@ -16,7 +11,7 @@
<tr>
<td class="r{cycle values=0,1}">
<h5><a href="{$WWWROOT}view/view.php?id={$view.id}">{$view.title|escape}</a> {str tag=by section=view}
<a href="{$WWWROOT}{if $view.group}group{else}user{/if}/view.php?id={if $view.group}{$view.group}{else}{$view.owner}{/if}">{if $view.sharedby}{$view.sharedby}{else}{$groupname}{/if}{if $view.template} <span class="template">({str tag=Template section=view})</span>{/if}</a>
<a href="{$WWWROOT}{if $view.group}group{else}user{/if}/view.php?id={if $view.group}{$view.group}{else}{$view.owner}{/if}">{if $view.sharedby}{$view.sharedby}{else}{$groupname}{/if}</a>
</h5>
<span>
{if $view.description}
......@@ -27,6 +22,9 @@
<strong>{str tag="artefacts" section="view"}:</strong>
{foreach from=$view.artefacts item=artefact name=artefacts}<a href="{$WWWROOT}view/artefact.php?artefact={$artefact.id}&amp;view={$view.id}" class="link-artefacts">{$artefact.title|escape}</a>{if !$smarty.foreach.artefacts.last}, {/if}{/foreach}
{/if}
{if $view.template}
<a href="">{str tag=copythisview section=view}</a>
{/if}
</span>
</td>
</tr>
......
......@@ -28,32 +28,27 @@ define('INTERNAL', 1);
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'view');
define('SECTION_PAGE', 'blocks');
require(dirname(dirname(__FILE__)) . '/init.php');
require('view.php');
require_once(get_config('docroot') . 'lib/group.php');
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
$view = new View(param_integer('id'));
$group = $view->get('group');
$institution = $view->get('institution');
View::set_nav($group, $institution);
$id = param_integer('id');
$new = param_boolean('new', false);
$view = new View($id);
if (!$USER->can_edit_view($view)) {
throw new AccessDeniedException();
}
// If the view has been submitted to a group, disallow editing
$submittedto = $view->get('submittedto');
if ($submittedto) {
throw new AccessDeniedException(get_string('canteditsubmitted', 'view', get_field('group', 'name', 'id', $submittedto)));
}
if (!$USER->can_edit_view($view)) {
throw new AccessDeniedException();
}
$new = param_boolean('new');
if ($new) {
define('TITLE', get_string('createviewsteptwo', 'view'));
}
else {
define('TITLE', get_string('editblocksforview', 'view', $view->get('title')));
}
$group = $view->get('group');
$institution = $view->get('institution');
// check if cancel was selected
if ($new && isset($_POST['cancel'])) {
......@@ -61,12 +56,21 @@ if ($new && isset($_POST['cancel'])) {
if ($group) {
redirect(get_config('wwwroot') . '/view/groupviews.php?group='.$group);
}
if ($group) {
if ($institution) {
redirect(get_config('wwwroot') . '/view/institutionviews.php?institution='.$institution);
}
redirect(get_config('wwwroot') . '/view/');
}
View::set_nav($group, $institution);
if ($new) {
define('TITLE', get_string('createviewstepone', 'view'));
}
else {
define('TITLE', get_string('editblocksforview', 'view', $view->get('title')));
}
$category = param_alpha('c', '');
// Make the default category the first tab if none is set
if ($category === '') {
......
<?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');
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
$group = param_integer('group', null);
$institution = param_alphanum('institution', null);
if ($group && !group_user_can_edit_views($group)
|| $institution && !$USER->can_edit_institution($institution)) {
throw new AccessDeniedException();
}
define('TITLE', get_string('copyaview', 'view'));
$choosetemplate = pieform(create_view_form($group, $institution, true));
$smarty = smarty();
$smarty->assign('heading', TITLE);
$smarty->assign('choosetemplate', $choosetemplate);
$smarty->display('view/choosetemplate.tpl');
?>
\ No newline at end of file
......@@ -34,53 +34,26 @@ require(dirname(dirname(__FILE__)) . '/init.php');
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
$id = param_integer('id', 0); // if 0, we're creating a new view
$new = param_boolean('new');
$view = new View(param_integer('id'));
if (empty($id)) {
$new = true;
$group = param_integer('group', null);
$institution = param_alphanum('institution', null);
if ($group && !group_user_can_edit_views($group)
|| $institution && !$USER->can_edit_institution($institution)) {
if (!$USER->can_edit_view($view)) {
throw new AccessDeniedException();
}
// Get templates visible to the user and non-templates owned by
// the owner of the view being created
$templates = View::view_search(null, null, null, true, null, 0, false);
$ownerid = ($group || $institution) ? null : $USER->get('id');
$nontemplates = View::view_search($ownerid, $group, $institution, false, null, 0, false);
$templateoptions = array(0 => get_string('none'));
foreach ($templates->data as $t) {
$templateoptions[$t->id] = $t->title;
}
foreach ($nontemplates->data as $t) {
$templateoptions[$t->id] = $t->title;
}
}
else {
$view = new View($id);
if (!$USER->can_edit_view($view)) {
throw new AccessDeniedException();
}
// If the view has been submitted to a group, disallow editing
$submittedto = $view->get('submittedto');
if ($submittedto) {
// If the view has been submitted to a group, disallow editing
$submittedto = $view->get('submittedto');
if ($submittedto) {
throw new AccessDeniedException(get_string('canteditsubmitted', 'view', get_field('group', 'name', 'id', $submittedto)));
}
$group = $view->get('group');
$institution = $view->get('institution');
}
$group = $view->get('group');
$institution = $view->get('institution');
View::set_nav($group, $institution);
if ($new || empty($id)) {
define('TITLE', get_string('createviewstepone', 'view'));
$new = param_boolean('new', 0);
if ($new) {
define('TITLE', get_string('createviewsteptwo', 'view'));
}
else {
define('TITLE', get_string('editviewdetails', 'view', $view->get('title')));
......@@ -116,7 +89,7 @@ $editview = array(
'elements' => array(
'id' => array(
'type' => 'hidden',
'value' => $id,
'value' => $view->get('id'),
),
'new' => array(
'type' => 'hidden',
......@@ -125,7 +98,7 @@ $editview = array(
'title' => array(
'type' => 'text',
'title' => get_string('title','view'),
'defaultvalue' => isset($view) ? $view->get('title') : null,
'defaultvalue' => $view->get('title'),
'rules' => array( 'required' => true ),
),
'description' => array(
......@@ -133,61 +106,52 @@ $editview = array(
'title' => get_string('description','view'),
'rows' => 10,
'cols' => 70,
'defaultvalue' => isset($view) ? $view->get('description') : null,
'defaultvalue' => $view->get('description'),
),
'tags' => array(
'type' => 'tags',
'title' => get_string('tags'),
'description' => get_string('tagsdescprofile'),
'defaultvalue' => isset($view) ? $view->get('tags') : null,
'defaultvalue' => $view->get('tags'),
'help' => true,
),
),
);
if ($group) {
$editview['elements']['group'] = array(
'type' => 'hidden',
'value' => $group
);
}
else if ($institution) {
$editview['elements']['institution'] = array(
'type' => 'hidden',
'value' => $institution
);
}
else {
if (!($group || $institution)) {
$default = $view->get('ownerformat');
if (!$default) {
$default = FORMAT_NAME_DISPLAYNAME;
}
$editview['elements']['ownerformat'] = array(
'type' => 'select',
'title' => get_string('ownerformat','view'),
'description' => get_string('ownerformatdescription','view'),
'options' => $ownerformatoptions,
'defaultvalue' => isset($view) ? $view->get('ownerformat') : FORMAT_NAME_DISPLAYNAME,
'defaultvalue' => $default,
'rules' => array('required' => true),
);
}
if ($new && count($templateoptions)) {
$editview['elements']['usetemplate'] = array(
'type' => 'select',
'title' => get_string('Template','view'),
'description' => get_string('createfromtemplatedescription','view'),
'options' => $templateoptions,
if ($new) {
$editview['elements']['submit'] = array(
'type' => 'cancelbackcreate',
'value' => array(get_string('cancel'), get_string('back','view'), get_string('next')),
'confirm' => array(get_string('confirmcancelcreatingview', 'view'), null, null),
);
}
else {
$editview['elements']['submit'] = array(
'type' => 'submit',
'value' => get_string('done'),
);
}
$editview['elements']['submit'] = array(
'type' => 'submitcancel',
'value' => array(empty($new) ? get_string('save') : get_string('next'), get_string('cancel')),
'confirm' => $new && isset($view) ? array(null, get_string('confirmcancelcreatingview', 'view')) : null,
);
$editview = pieform($editview);
function editview_cancel_submit() {
global $view, $new, $group, $institution;
if (isset($view) && $new) {
if ($new) {
$view->delete();
}
if ($group) {
......@@ -201,62 +165,31 @@ function editview_cancel_submit() {
function editview_submit(Pieform $form, $values) {
global $USER, $SESSION, $group, $institution;
$editing = !empty($values['id']);
$view = new View($values['id'], $values);
global $view, $SESSION;
if (empty($editing)) {
$view->set('numcolumns', 3); // default
$view->set('template', 0);
if ($group) {
$view->set('group', $group);
}
else if ($institution) {
$view->set('institution', $institution);
}
else {
$view->set('owner', $USER->get('id'));
}
}
else {
$view->set('dirty', true);
if (param_boolean('back')) {
redirect('/view/blocks.php?id=' . $view->get('id') . '&new=' . $new);
}
$view->commit();
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);
$copystatus = $view->copy_contents($template);
$SESSION->add_ok_msg(get_string('copiedblocksandartefactsfromtemplate', 'view', $copystatus['blocks'], $copystatus['artefacts'], $template->get('title')));