Commit 87a2c5f2 authored by Richard Mansfield's avatar Richard Mansfield

Group views together when they have the same access records & configuration

Pull access records in a consistent order
Save hash of the config/access set along with views for easy retrieval
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent d9d0abb1
......@@ -615,6 +615,7 @@
<FIELD NAME="visits" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" />
<FIELD NAME="allowcomments" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" />
<FIELD NAME="approvecomments" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" />
<FIELD NAME="accessconf" TYPE="char" LENGTH="40" NOTNULL="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
......
......@@ -2228,5 +2228,12 @@ function xmldb_core_upgrade($oldversion=0) {
set_field('institution', 'priority', 0, 'name', 'mahara');
}
if ($oldversion < 2010122201) {
$table = new XMLDBTable('view');
$field = new XMLDBField('accessconf');
$field->setAttributes(XMLDB_TYPE_CHAR, 40, XMLDB_UNSIGNED, null);
add_field($table, $field);
}
return $status;
}
......@@ -28,7 +28,7 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2010122200;
$config->version = 2010122201;
$config->release = '1.4.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
......@@ -68,6 +68,7 @@ class View {
private $allowcomments;
private $approvecomments;
private $collection;
private $accessconf;
/**
* Valid view layouts. These are read at install time and inserted into
......@@ -561,54 +562,117 @@ class View {
}
public function get_access($timeformat=null) {
if ($data = $this->get_access_records()) {
return self::process_access_records($data, $timeformat);
}
return array();
}
public function get_access_records() {
$data = get_records_sql_array("
SELECT va.*, g.grouptype
SELECT accesstype, va.group, role, usr, token, startdate, stopdate, allowcomments, approvecomments
FROM {view_access} va
LEFT OUTER JOIN {group} g ON (va.group = g.id AND g.deleted = 0)
WHERE va.view = ? AND va.visible = 1",
WHERE view = ? AND visible = 1
ORDER BY
accesstype IS NULL, accesstype DESC,
va.group, role IS NOT NULL, role,
usr,
token,
startdate IS NOT NULL, startdate, stopdate IS NOT NULL, stopdate,
allowcomments, approvecomments",
array($this->id)
);
return $data ? $data : array();
}
if ($data) {
foreach ($data as &$item) {
$item = (array)$item;
if ($item['usr']) {
$item['type'] = 'user';
$item['id'] = $item['usr'];
}
else if ($item['group']) {
$item['type'] = 'group';
$item['id'] = $item['group'];
}
else if ($item['token']) {
$item['type'] = 'token';
$item['id'] = $item['token'];
}
else {
$item['type'] = $item['accesstype'];
$item['id'] = null;
}
public static function process_access_records($data=array(), $timeformat=null) {
$rolegroups = array();
foreach ($data as &$item) {
if ($item->role && !isset($roledata[$item->group])) {
$rolegroups[$item->group] = 1;
}
}
if ($rolegroups) {
$grouptypes = get_records_sql_assoc('
SELECT id, grouptype
FROM {group}
WHERE id IN (' . join(',', array_map('intval', array_keys($rolegroups))) . ')
AND deleted = 0',
array()
);
}
if ($item['role']) {
$item['roledisplay'] = get_string($item['role'], 'grouptype.'.$item['grouptype']);
foreach ($data as &$item) {
$item = (array)$item;
if ($item['usr']) {
$item['type'] = 'user';
$item['id'] = $item['usr'];
}
else if ($item['group']) {
$item['type'] = 'group';
$item['id'] = $item['group'];
}
else if ($item['token']) {
$item['type'] = 'token';
$item['id'] = $item['token'];
}
else {
$item['type'] = $item['accesstype'];
$item['id'] = null;
}
if ($item['role']) {
$item['roledisplay'] = get_string($item['role'], 'grouptype.'.$grouptypes[$item['group']]->grouptype);
}
if ($timeformat) {
if ($item['startdate']) {
$item['startdate'] = strftime($timeformat, strtotime($item['startdate']));
}
if ($timeformat) {
if ($item['startdate']) {
$item['startdate'] = strftime($timeformat, strtotime($item['startdate']));
}
if ($item['stopdate']) {
$item['stopdate'] = strftime($timeformat, strtotime($item['stopdate']));
}
if ($item['stopdate']) {
$item['stopdate'] = strftime($timeformat, strtotime($item['stopdate']));
}
}
}
else {
$data = array();
}
return $data;
}
public static function update_view_access($config, $viewids) {
db_begin();
// Use set_access() on the first view to get a hopefully consistent
// representation of the access list (in the same order as the list
// returned by get_access)
$firstview = new View($viewids[0]);
$fullaccesslist = $firstview->set_access($config['accesslist']);
$firstview->copy_access($viewids);
// Hash the config object so later on we can easily find
// all the views with the same config/access rights
$config['accesslist'] = $fullaccesslist;
$accessconf = substr(md5(serialize($config)), 0, 10);
foreach ($viewids as $viewid) {
$v = new View((int) $viewid);
$v->set('startdate', $config['startdate']);
$v->set('stopdate', $config['stopdate']);
$v->set('template', $config['template']);
$v->set('allowcomments', $config['allowcomments']);
$v->set('approvecomments', $config['approvecomments']);
if (isset($config['copynewuser'])) {
$v->set('copynewuser', $config['copynewuser']);
}
if (isset($config['copynewgroups'])) {
$v->set('copynewgroups', $config['copynewgroups']);
}
$v->set('accessconf', $accessconf);
$v->commit();
}
db_commit();
}
public function is_public() {
$accessrecords = self::user_access_records($this->id, 0);
foreach($accessrecords as &$a) {
......@@ -656,7 +720,17 @@ class View {
unset($item['approvecomments']);
}
$accessrecord = new StdClass;
$accessrecord = (object)array(
'accesstype' => null,
'group' => null,
'role' => null,
'usr' => null,
'token' => null,
'startdate' => null,
'stopdate' => null,
'allowcomments' => 0,
'approvecomments' => 1,
);
switch ($item['type']) {
case 'user':
......@@ -685,7 +759,6 @@ class View {
$accessrecord->accesstype = $item['type'];
}
$accessrecord->view = $this->get('id');
if (isset($item['allowcomments'])) {
$accessrecord->allowcomments = (int) !empty($item['allowcomments']);
if ($accessrecord->allowcomments) {
......@@ -700,7 +773,9 @@ class View {
}
if (array_search($accessrecord, $accessdata_added) === false) {
$accessrecord->view = $this->get('id');
insert_record('view_access', $accessrecord);
unset($accessrecord->view);
$accessdata_added[] = $accessrecord;
}
}
......@@ -714,6 +789,7 @@ class View {
handle_event('saveview', $this->get('id'));
db_commit();
return $accessdata_added;
}
/**
......@@ -3217,13 +3293,14 @@ class View {
* @param integer $owner
* @param integer $group
* @param string $institution
* @param string $matchconfig record all matches with given config hash (see set_access)
*
* @return array, array
*/
function get_views_and_collections($owner=null, $group=null, $institution=null) {
function get_views_and_collections($owner=null, $group=null, $institution=null, $matchconfig=null) {
$ownersql = self::owner_sql((object) array('owner' => $owner, 'group' => $group, 'institution' => $institution));
$records = get_records_sql_array("
SELECT v.id AS vid, v.title AS vname, c.id AS cid, c.name AS cname
SELECT v.id AS vid, v.title AS vname, v.accessconf, c.id AS cid, c.name AS cname
FROM {view} v
LEFT JOIN {collection_view} cv ON v.id = cv.view
LEFT JOIN {collection} c ON cv.collection = c.id
......@@ -3244,11 +3321,17 @@ class View {
if ($r->cid) {
if (!isset($collections[$r->cid])) {
$collections[$r->cid] = array('id' => $r->cid, 'name' => $r->cname, 'views' => array());
if ($matchconfig && $matchconfig == $r->accessconf) {
$collections[$r->cid]['match'] = true;
}
}
$collections[$r->cid]['views'][$r->vid] = $v;
}
else {
$views[$r->vid] = $v;
if ($matchconfig && $matchconfig == $r->accessconf) {
$views[$r->vid]['match'] = true;
}
}
}
......
......@@ -90,7 +90,9 @@ $form = array(
// institution views requires doing some tricky stuff with the 'copy for new users/groups'
// options, and there's not much room for the 'Share' tab in the admin area anyway
if (!$institution) {
list($collections, $views) = View::get_views_and_collections($view->get('owner'), $group, $institution);
list($collections, $views) = View::get_views_and_collections(
$view->get('owner'), $group, $institution, $view->get('accessconf')
);
}
if (!empty($collections)) {
......@@ -98,7 +100,7 @@ if (!empty($collections)) {
$c = array(
'title' => $c['name'],
'value' => $c['id'],
'defaultvalue' => !empty($collection) && $collection->get('id') == $c['id'],
'defaultvalue' => !empty($collection) && ($collection->get('id') == $c['id'] || !empty($c['match'])),
'views' => $c['views'], // Keep these hanging around to check in submit function
);
}
......@@ -114,7 +116,7 @@ if (!empty($views)) {
$v = array(
'title' => $v['name'],
'value' => $v['id'],
'defaultvalue' => $viewid == $v['id'],
'defaultvalue' => $viewid == $v['id'] || !empty($v['match']),
);
}
$form['elements']['views'] = array(
......@@ -129,7 +131,7 @@ $allowcomments = $view->get('allowcomments');
$form['elements']['accesslist'] = array(
'type' => 'viewacl',
'allowcomments' => $allowcomments,
'defaultvalue' => isset($view) ? $view->get_access(get_string('strftimedatetimeshort')) : null
'defaultvalue' => $view->get_access(get_string('strftimedatetimeshort')),
);
......@@ -443,9 +445,30 @@ function editaccess_submit(Pieform $form, $values) {
}
}
$viewconfig = array(
'startdate' => $values['startdate'],
'stopdate' => $values['stopdate'],
'template' => (int) $values['template'],
'allowcomments' => (int) $values['allowcomments'],
'approvecomments' => (int) ($values['allowcomments'] && $values['approvecomments']),
'accesslist' => $values['accesslist'],
);
$toupdate = array();
if ($institution) {
if (isset($values['copynewuser'])) {
$viewconfig['copynewuser'] = (int) ($values['template'] && $values['copynewuser']);
}
if ($institution == 'mahara') {
$createfor = array();
foreach (group_get_grouptypes() as $grouptype) {
if ($values['template'] && $values['copyfornewgroups_'.$grouptype]) {
$createfor[] = $grouptype;
}
}
$viewconfig['copynewgroups'] = $createfor;
}
$toupdate[] = $view->get('id');
}
else {
......@@ -464,40 +487,7 @@ function editaccess_submit(Pieform $form, $values) {
}
}
db_begin();
foreach ($toupdate as $viewid) {
$v = new View($viewid);
$v->set('startdate', $values['startdate']);
$v->set('stopdate', $values['stopdate']);
$istemplate = (int) $values['template'];
$v->set('template', $istemplate);
if (isset($values['copynewuser'])) {
$v->set('copynewuser', (int) ($istemplate && $values['copynewuser']));
}
if ($institution == 'mahara') {
$createfor = array();
foreach (group_get_grouptypes() as $grouptype) {
if ($istemplate && $values['copyfornewgroups_'.$grouptype]) {
$createfor[] = $grouptype;
}
}
$v->set('copynewgroups', $createfor);
}
$v->set('allowcomments', (int) $values['allowcomments']);
if ($values['allowcomments']) {
$v->set('approvecomments', (int) $values['approvecomments']);
}
$v->commit();
}
$first = new View($toupdate[0]);
$first->set_access($values['accesslist']);
$first->copy_access($toupdate);
db_commit();
View::update_view_access($viewconfig, $toupdate);
$SESSION->add_ok_msg(get_string('updatedaccessfornumviews', 'view', count($toupdate)));
......
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