Commit 2106da31 authored by Richard Mansfield's avatar Richard Mansfield

Choose from group artefacts when editing group views

parent 69be97b1
......@@ -82,6 +82,7 @@ class User {
'accountprefs' => array(),
'activityprefs' => array(),
'institutions' => array(),
'grouproles' => array(),
'theme' => null,
'admininstitutions' => array(),
'staffinstitutions' => array(),
......@@ -120,6 +121,7 @@ class User {
$this->populate($user);
$this->reset_institutions();
$this->reset_grouproles();
return $this;
}
......@@ -508,6 +510,17 @@ class User {
$this->staffinstitutions = $staffinstitutions;
}
public function reset_grouproles() {
$memberships = get_records_array('group_member', 'member', $this->get('id'));
$roles = array();
if ($memberships) {
foreach ($memberships as $m) {
$roles[$m->group] = $m->role;
}
}
$this->set('grouproles', $roles);
}
public function can_view_artefact($a) {
if ($this->get('admin')
|| $this->get('id') == $a->get('owner')
......@@ -542,6 +555,22 @@ class User {
return false;
}
public function can_edit_view($v) {
if ($this->get('admin')) {
return true;
}
$owner = $v->get('owner');
if ($owner == $this->get('id')) {
return true;
}
$group = $v->get('group');
if ($group) {
$editroles = $v->get('editingroles');
return in_array($this->grouproles[$group], $editroles);
}
return false;
}
}
......@@ -662,6 +691,7 @@ class LiveUser extends User {
$this->activityprefs = load_activity_preferences($user->id);
$this->accountprefs = load_account_preferences($user->id);
$this->reset_institutions();
$this->reset_grouproles();
$this->commit();
}
......
......@@ -466,6 +466,7 @@ class BlockInstance {
'validatecallback' => array(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_validate'),
'successcallback' => array($this, 'instance_config_store'),
'elements' => $elements,
'viewgroup' => $this->get_view()->get('group'),
);
if (param_variable('action_acsearch_id_' . $this->get('id'), false)) {
......
......@@ -306,6 +306,7 @@
<FIELDS>
<FIELD NAME="grouptype" TYPE="char" LENGTH="20" NOTNULL="true" />
<FIELD NAME="role" TYPE="text" NOTNULL="true" />
<FIELD NAME="edit_views" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="grouptype,role" />
......
......@@ -1121,6 +1121,11 @@ function xmldb_core_upgrade($oldversion=0) {
execute_sql('ALTER TABLE {grouptype} DROP COLUMN usercancreate');
}
if ($oldversion < 2008062305) {
execute_sql('ALTER TABLE {grouptype_roles} ADD COLUMN edit_views SMALLINT NOT NULL DEFAULT 1');
execute_sql("UPDATE {grouptype_roles} SET edit_views = 0 WHERE grouptype = 'course' AND role = 'member'");
}
return $status;
}
......
......@@ -38,9 +38,8 @@ function pieform_element_artefactchooser(Pieform $form, $element) {
global $USER, $pagination_js;
$value = $form->get_value($element);
$element['offset'] = param_integer('offset', 0);
list($html, $pagination, $count) = View::build_artefactchooser_data($element);
list($html, $pagination, $count) = View::build_artefactchooser_data($element, $form->get_property('viewgroup'));
$smarty = smarty_core();
$smarty->assign('datatable', $element['name'] . '_data');
......
......@@ -542,6 +542,7 @@ abstract class GroupType {
insert_record('grouptype_roles', (object) array(
'grouptype' => $type,
'role' => $r,
'edit_views' => $this->role_can_edit_views($r),
));
}
}
......@@ -561,10 +562,16 @@ abstract class GroupType {
*/
public static abstract function get_roles();
public static abstract function get_view_editing_roles();
public static function takes_view_submissions() {
return false;
}
public static function role_can_edit_views($role) {
return in_array($this->get_view_editing_roles(), $role);
}
}
/**
......
......@@ -44,6 +44,10 @@ class GroupTypeCourse extends GroupType {
return array('member', 'tutor', 'admin');
}
public static function get_view_editing_roles() {
return array('tutor', 'admin');
}
public static function takes_view_submissions() {
return true;
}
......
......@@ -38,6 +38,10 @@ class GroupTypeStandard extends GroupType {
return array('member', 'admin');
}
public static function get_view_editing_roles() {
return array('member', 'admin');
}
}
?>
......@@ -27,7 +27,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2008062304;
$config->version = 2008062305;
$config->release = '1.1.0alpha';
$config->minupgradefrom = 2007080700;
$config->minupgraderelease = '0.8.0 (release tag 0.8.0_RELEASE)';
......
......@@ -33,6 +33,7 @@ class View {
private $id;
private $owner;
private $ownerformat;
private $group;
private $ctime;
private $mtime;
private $atime;
......@@ -51,6 +52,7 @@ class View {
private $columns;
private $dirtycolumns; // for when we change stuff
private $tags;
private $editingroles;
public function __construct($id=0, $data=null) {
if (!empty($id)) {
......@@ -83,6 +85,11 @@ class View {
$this->atime = time();
$this->columns = array();
$this->dirtycolumns = array();
if ($this->group) {
$group = get_record('group', 'id', $this->group);
require_once(get_config('docroot') . 'lib/grouptype/' . $group->grouptype . '.php');
$this->editingroles = call_static_method('GroupType' . $group->grouptype, 'get_view_editing_roles');
}
}
public function get($field) {
......@@ -474,7 +481,7 @@ class View {
// Security
// TODO this might need to be moved below the requestdata check below, to prevent non owners of the view being
// rejected
if ($USER->get('id') != $this->get('owner')) {
if (!$USER->can_edit_view($this)) {
throw new AccessDeniedException(get_string('canteditdontown', 'view'));
}
......@@ -1135,9 +1142,8 @@ class View {
* - Pagination HTML and Javascript
* - The total number of artefacts found
*/
public static function build_artefactchooser_data($data) {
public static function build_artefactchooser_data($data, $group=null) {
global $USER;
$search = '';
if (!empty($data['search']) && param_boolean('s')) {
$search = param_variable('search', '');
......@@ -1147,7 +1153,7 @@ class View {
// $search = '';
//}
}
//log_debug($data);
$artefacttypes = $data['artefacttypes'];
$offset = $data['offset'];
$limit = $data['limit'];
......@@ -1162,7 +1168,11 @@ class View {
safe_require('blocktype', $data['blocktype']);
$blocktypeclass = generate_class_name('blocktype', $data['blocktype']);
$select = 'owner = ' . $USER->get('id');
if ($group) {
$select = '"group" = ' . $group;
} else {
$select = 'owner = ' . $USER->get('id');
}
if (!empty($artefacttypes)) {
$select .= ' AND artefacttype IN(' . implode(',', array_map('db_quote', $artefacttypes)) . ')';
}
......@@ -1239,6 +1249,7 @@ class View {
'extradata' => array(
'value' => $value,
'blocktype' => $data['blocktype'],
'group' => $group,
),
));
......@@ -1303,7 +1314,7 @@ class View {
$data[$i]['id'] = $viewdata[$i]->id;
$data[$i]['title'] = $viewdata[$i]->title;
$data[$i]['description'] = $viewdata[$i]->description;
if ($viewdata[$i]->name) {
if (!empty($viewdata[$i]->name)) {
$data[$i]['submittedto'] = array('name' => $viewdata[$i]->name, 'id' => $viewdata[$i]->groupid);
}
$data[$i]['artefacts'] = array();
......
......@@ -2,7 +2,9 @@
{include file="sidebar.tpl"}
{include file="columnleftstart.tpl"}
<span class="addicon fr"><a href="{$WWWROOT}view/edit.php">{str tag="createview" section="view"}</a></span>
<span class="addicon fr">
<a href="{$WWWROOT}view/edit.php{if $group}?group={$group}{/if}">{str tag="createview" section="view"}</a>
</span>
<h2>{$heading}</h2>
{if $views}
......
......@@ -37,7 +37,7 @@ $data = pieform_element_artefactchooser_set_attributes(
call_static_method(generate_class_name('blocktype', $extradata->blocktype), 'artefactchooser_element', $extradata->value)
);
$data['offset'] = param_integer('offset', 0);
list($html, $pagination, $count, $offset) = View::build_artefactchooser_data($data);
list($html, $pagination, $count, $offset) = View::build_artefactchooser_data($data, $extradata->group);
json_reply(false, array(
'message' => null,
......
......@@ -33,16 +33,18 @@ define('SECTION_PAGE', 'edit');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once(get_config('docroot') . 'lib/view.php');
require_once(get_config('docroot') . 'lib/group.php');
$id = param_integer('id', 0); // if 0, we're creating a new view
$new = param_boolean('new');
if (empty($id)) {
$new = true;
$group = param_integer('group', 0);
}
else {
$view = new View($id);
if ($view->get('owner') != $USER->get('id')) {
if (!$USER->can_edit_view($view)) {
throw new AccessDeniedException(get_string('canteditdontown', 'view'));
}
......@@ -51,6 +53,15 @@ else {
if ($submittedto) {
throw new AccessDeniedException(get_string('canteditsubmitted', 'view', get_field('group', 'name', 'id', $submittedto)));
}
$group = $view->get('group');
if (empty($group)) {
$group = 0;
}
}
if ($group && !group_user_access($group)) {
throw new AccessDeniedException();
}
if ($new || empty($id)) {
......@@ -96,6 +107,10 @@ $editview = pieform(array(
'type' => 'hidden',
'value' => $new,
),
'group' => array(
'type' => 'hidden',
'value' => $group,
),
'title' => array(
'type' => 'text',
'title' => get_string('title','view'),
......@@ -133,10 +148,13 @@ $editview = pieform(array(
));
function editview_cancel_submit() {
global $view, $new;
global $view, $new, $group;
if (isset($view) && $new) {
$view->delete();
}
if ($group) {
redirect('/view/groupviews.php?group='.$group);
}
redirect('/view');
}
......@@ -146,11 +164,20 @@ function editview_submit(Pieform $form, $values) {
$editing = !empty($values['id']);
$view = new View($values['id'], $values);
$group = (int) $values['group'];
if ($group && !group_user_access($group)) {
$SESSION->add_error_msg(get_string('notamember', 'group'));
redirect('/view/groupviews.php?group='.$group);
}
if (empty($editing)) {
$view->set('numcolumns', 3); // default
$view->set('owner', $USER->get('id'));
if ($group) {
$view->set('group', $group);
}
else {
$view->set('owner', $USER->get('id'));
}
}
else {
$view->set('dirty', true);
......@@ -162,8 +189,13 @@ function editview_submit(Pieform $form, $values) {
$redirecturl = '/view/blocks.php?id=' . $view->get('id') . '&new=1';
}
else {
$redirecturl = '/view/index.php';
$SESSION->add_ok_msg(get_string('viewsavedsuccessfully', 'view'));
if ($group) {
$redirecturl = '/view/groupviews.php?group='.$group;
}
else {
$redirecturl = '/view/index.php';
}
}
redirect($redirecturl);
......
......@@ -56,6 +56,7 @@ $pagination = build_pagination(array(
));
$smarty = smarty();
$smarty->assign('group', $group);
$smarty->assign('views', $data->data);
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('heading', get_string('groupviews'));
......
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