Commit 2ad167e5 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic
Browse files

Bug 1784781: adding peer & manager roles for user access to a view

- creating the role table
- selecting the role when editing view access
- fix: for already saved user and group access rules, show all roles
in the dropdown to make it possible to change the value

to do: display content depending on role

behatnotneeded : will be added later

Change-Id: Ie83e0ca84704182b40bcc2fa4a678da099a1793d
parent d99d0c75
......@@ -134,6 +134,10 @@ $string['friends'] = 'Friends';
$string['groups'] = 'Groups';
$string['users'] = 'Users';
$string['everyoneingroup'] = 'Everyone in group';
$string['nospecialrole'] = 'No special role';
$string['peer'] = 'Peer';
$string['manager'] = 'Manager';
$string['peermanager'] = 'Peer and manager';
// secret url
$string['token'] = 'Secret URL';
......
......@@ -1417,5 +1417,14 @@
<KEY NAME="editedbyfk" TYPE="foreign" FIELDS="editedby" REFTABLE="usr" REFFIELDS="id" />
</KEYS>
</TABLE>
<TABLE NAME="usr_roles">
<FIELDS>
<FIELD NAME="role" TYPE="char" LENGTH="255" NOTNULL="true" />
<FIELD NAME="see_block_content" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="role" />
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
......@@ -5941,5 +5941,23 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2018080200) {
log_debug('Adding peer, manager and peer&manager roles');
$table = new XMLDBTable('usr_roles');
$table->addFieldInfo('role', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL);
$table->addFieldInfo('see_block_content', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('role'));
create_table($table);
$roles = array('peer' => 0, 'manager' => 1, 'peermanager' => 1);
foreach ($roles as $role => $state) {
$obj = new StdClass;
$obj->role = $role;
$obj->see_block_content = $state;
insert_record('usr_roles', $obj);
}
}
return $status;
}
\ No newline at end of file
}
......@@ -80,6 +80,9 @@ function pieform_element_viewacl(Pieform $form, $element) {
$item[$datetype] = Pieform::hsc(strftime($datetimeformat, $item[$datetype]));
}
}
if ($item['type'] == 'group') {
$item['grouptype'] = get_field('group', 'grouptype', 'id', $item['id']);
}
// only show access that is still current. Expired access will be deleted if the form is saved
if ($form->is_submitted() || !$rawstopdate || (time() <= $rawstopdate)) {
......@@ -203,6 +206,9 @@ function pieform_element_viewacl(Pieform $form, $element) {
}
}
$userroles = View::get_user_access_roles();
$grouproles = get_group_access_roles();
$smarty->assign('datepickeroptions', $datepickeroptionstr);
$smarty->assign('datepickertooltips', json_encode($tooltips));
$smarty->assign('viewtype', $element['viewtype']);
......@@ -217,6 +223,8 @@ function pieform_element_viewacl(Pieform $form, $element) {
$smarty->assign('allgroups', json_encode($allgroups));
$smarty->assign('mygroups', json_encode($mygroups));
$smarty->assign('faves', json_encode($faves));
$smarty->assign('userroles', json_encode($userroles));
$smarty->assign('grouproles', json_encode($grouproles));
return $smarty->fetch('form/viewacl.tpl');
}
......
......@@ -3143,3 +3143,12 @@ function group_sort_categories() {
}
}
}
function get_group_access_roles() {
$roles = get_records_array('grouptype_roles');
$data = array();
foreach ($roles as $r) {
$data[$r->grouptype][] = array('name' => $r->role, 'display' => get_string($r->role, 'grouptype.' . $r->grouptype));
}
return $data;
}
......@@ -859,6 +859,17 @@ function core_install_lastcoredata_defaults() {
// if we're upgrading this happens somewhere else. This is because of dependency issues around
// the order of installation stuff.
install_blocktype_extras();
// Setting user roles for content block access
$table = new XMLDBTable('usr_roles');
$roles = array('peer' => 0, 'manager' => 1, 'peermanager' => 1);
foreach ($roles as $role => $state) {
$obj = new StdClass;
$obj->role = $role;
$obj->see_block_content = $state;
insert_record('usr_roles', $obj);
}
}
function core_install_firstcoredata_defaults() {
......
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2018070500;
$config->version = 2018080200;
$config->series = '18.10';
$config->release = '18.10dev';
$config->minupgradefrom = 2015030409;
......
......@@ -1037,10 +1037,11 @@ class View {
public function process_access_records($data=array(), $timeformat=null) {
$rolegroups = array();
foreach ($data as &$item) {
if ($item->role && !isset($roledata[$item->group])) {
if (isset($item->group) && $item->role && !isset($roledata[$item->group])) {
$rolegroups[$item->group] = 1;
}
}
if ($rolegroups) {
$grouptypes = get_records_sql_assoc('
SELECT id, grouptype
......@@ -1082,9 +1083,12 @@ class View {
$item['locked'] = true;
}
if ($item['role']) {
if ($item['role'] && isset($item['group'])) {
$item['roledisplay'] = get_string($item['role'], 'grouptype.'.$grouptypes[$item['group']]->grouptype);
}
else {
$item['roledisplay'] = get_string($item['role'], 'view');
}
if ($timeformat) {
if ($item['startdate']) {
$item['startdate'] = strftime($timeformat, strtotime($item['startdate']));
......@@ -1446,6 +1450,15 @@ class View {
switch ($item['type']) {
case 'user':
$accessrecord->usr = $item['id'];
if (isset($item['role']) && strlen($item['role'])) {
$roleinfo = get_column('usr_roles', 'role');
foreach ($roleinfo as $key => $role) {
if ($role == $item['role']) {
$accessrecord->role = $item['role'];
}
}
}
break;
case 'group':
$accessrecord->group = $item['id'];
......@@ -1699,6 +1712,15 @@ class View {
return true;
}
public static function get_user_access_roles() {
$roles = get_records_array('usr_roles');
$data = array();
foreach ($roles as $r) {
$data[] = array('name' => $r->role, 'display' => get_string($r->role, 'view'));
}
return $data;
}
public function get_autocreate_grouptypes() {
if (!isset($this->copynewgroups)) {
$this->copynewgroups = get_column('view_autocreate_grouptype', 'grouptype', 'view', $this->id);
......@@ -6683,6 +6705,9 @@ class View {
if ($access->usr) {
$access->accesstype = 'user';
$access->id = $access->usr;
if ($access->role) {
$access->roledisplay = get_string($access->role, 'view');
}
}
else if ($access->group) {
$access->accesstype = 'group';
......
<input type="hidden" name="accesslist" value="">
<div class="panel panel-secondary view-container" id="editaccesswrap" data-viewtype="{{$viewtype}}">
<div class="panel panel-secondary view-container" id="editaccesswrap"
data-viewtype="{{$viewtype}}"
data-user-roles='{{$userroles}}'
data-group-roles='{{$grouproles}}' >
{{if $viewtype == "profile" }}
<h2 class="panel-heading">{{str tag=profile section=view}}</h2>
{{/if}}
......@@ -82,7 +85,12 @@
</div>
<span class="picker input-short{% if (!o.presets.role) { %} hidden{% } %}">
<select data-roles="grouproles" name="accesslist[{%=o.id%}][role]" class="form-control input-small select">
{% if (o.presets.role) { %}<option value="{%=o.presets.role%}" selected>{%=o.presets.roledisplay%}</option>{% } %}
{% if (o.presets.role) { %}
<option value="" >{%=o.defaultText%}</option>
{% for (var i=0; i<o.roles.length; i++) { %}
<option value="{%=o.roles[i].name%}" {% if (o.presets.role == o.roles[i].name) { %} selected {% } %}>{%=o.roles[i].display%}</option>
{% } %}
{% } %}
</select>
</span>
</div>
......@@ -302,7 +310,10 @@ jQuery(function($) {
var data,
lastrow,
id,
viewtype = $('[data-viewtype]').attr('data-viewtype');
viewtype = $('[data-viewtype]').attr('data-viewtype'),
roles,
defaultText,
grouproles;
if($('#accesslistitems tr').length > 0){
lastrow = $('#accesslistitems tr:last-child');
......@@ -312,11 +323,30 @@ jQuery(function($) {
id = 0;
}
if (!presets.empty) {
if (presets.type == 'user') {
roles = JSON.parse($('[data-user-roles]').attr('data-user-roles'));
defaultText = {{jstr tag=nospecialrole section=view}};
}
else {
// group
grouproles = JSON.parse($('[data-group-roles]').attr('data-group-roles'));
defaultText = {{jstr tag=everyoneingroup section=view}};
if (presets.grouptype == 'course') {
roles = grouproles.course;
}
else {
roles = grouproles.standard;
}
}
}
data = {
id: id,
shareoptions: shareoptions,
presets: presets,
viewtype: viewtype
viewtype: viewtype,
roles: roles,
defaultText: defaultText
};
$('#accesslistitems').append(tmpl("row-template", data));
......@@ -435,18 +465,26 @@ jQuery(function($) {
function showRoleSelect(e, self) {
var roles = JSON.parse($(self).attr('data-roles')),
grouptype = $(self).parent().find('[data-grouptype]').attr('data-grouptype'),
data,
defaultText = {{jstr tag=everyoneingroup section=view}},
id = $(self).closest('tr').attr('data-id'),
select = $(self).closest('.dropdown-group').find('[data-roles="grouproles"]');
data = {
id: id,
defaultText: defaultText,
roles: roles[grouptype]
};
if ($(self).attr('data-type') == 'group') {
var grouptype = $(self).parent().find('[data-grouptype]').attr('data-grouptype'),
defaultText = {{jstr tag=everyoneingroup section=view}};
data = {
id: id,
defaultText: defaultText,
roles: roles[grouptype]
};
}
else {
var defaultText = {{jstr tag=nospecialrole section=view}};
data = {
id: id,
defaultText: defaultText,
roles: roles
};
}
select.html(tmpl("roles-template", data));
select.prop('disabled', false).parent().removeClass('hidden');
}
......
......@@ -16,7 +16,7 @@
{elseif $accessgroup.accesstype == 'institution'}
<a href="{$WWWROOT}account/institutions.php">{$accessgroup.id|institution_display_name}</a>
{elseif $accessgroup.accesstype == 'user'}
<a href="{profile_url($accessgroup.id)}">{$accessgroup.id|display_name}</a>
<a href="{profile_url($accessgroup.id)}">{$accessgroup.id|display_name}</a>{if $accessgroup.role} ({$accessgroup.roledisplay}){/if}
{/if}
{if $accessgroup.startdate}
{if $accessgroup.stopdate}
......
......@@ -32,6 +32,11 @@ switch ($type) {
break;
case 'user':
$data = search_user($query, $limit, $offset, array('exclude' => $USER->get('id')));
$roles = get_records_array('usr_roles');
$data['roles'] = array();
foreach ($roles as $r) {
$data['roles'][] = array('name' => $r->role, 'display' => get_string($r->role, 'view'));
}
break;
case 'group':
require_once('group.php');
......
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