Commit 86d8cc21 authored by Son Nguyen's avatar Son Nguyen

Enable collections in groups and institutions (Bug #886080)

1. For group collections
Only roles that can edit group pages can do these:
- Listing group collections
- Creating a new group collection
- Deleting and editing an selected group collection.
- Copying a collection
2. For institution collections
Only institution admins can:
- Listing insitution collections
- Creating a new institution collection
- Deleting and editing an selected institution collection.
- Copying a collection

Change-Id: I799bcfaf0f751d1a8c1fa9b5e5df4832832d8ea1
Signed-off-by: default avatarSon Nguyen <son.nguyen@catalyst.net.nz>
parent e5bdacad
......@@ -72,7 +72,7 @@ class PluginArtefactFile extends PluginArtefact {
'path' => 'groups/files',
'url' => 'artefact/file/groupfiles.php?group='.$groupid,
'title' => get_string('Files', 'artefact.file'),
'weight' => 70,
'weight' => 80,
),
);
}
......
......@@ -1033,9 +1033,22 @@ class User {
*/
public function can_edit_collection($c) {
$owner = $c->get('owner');
if ($owner == $this->get('id')) {
if ($owner > 0 && $owner == $this->get('id')) {
return true;
}
$institution = $c->get('institution');
if ($institution && $this->can_edit_institution($institution)) {
return true;
}
$group = $c->get('group');
if ($group) {
$this->reset_grouproles();
if (!isset($this->grouproles[$group])) {
return false;
}
require_once('group.php');
return group_role_can_edit_views($group, $this->grouproles[$group]);
}
return false;
}
......
......@@ -26,7 +26,6 @@
*/
define('INTERNAL', 1);
define('MENUITEM', 'myportfolio/collection');
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'collection');
......@@ -35,14 +34,39 @@ define('SECTION_PAGE', 'delete');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
require_once('collection.php');
define('TITLE', get_string('deletecollection', 'collection'));
$id = param_integer('id');
$collection = new Collection($id);
if (!$USER->can_edit_collection($collection)) {
$SESSION->add_error_msg(get_string('cantdeletecollection', 'collection'));
redirect('/collection/');
throw new AccessDeniedException(get_string('cantdeletecollection', 'collection'));
}
$groupid = $collection->get('group');
$institutionname = $collection->get('institution');
$urlparams = array();
if (!empty($groupid)) {
define('MENUITEM', 'groups/collections');
define('GROUP', $groupid);
$urlparams['group'] = $groupid;
}
else if (!empty($institutionname)) {
if ($institutionname == 'mahara') {
define('ADMIN', 1);
define('MENUITEM', 'configsite/collections');
}
else {
define('INSTITUTIONALADMIN', 1);
define('MENUITEM', 'manageinstitutions/institutioncollections');
}
$urlparams['institution'] = $institutionname;
}
else {
define('MENUITEM', 'myportfolio/collection');
}
define('TITLE', get_string('deletespecifiedcollection', 'collection', $collection->get('name')));
$baseurl = get_config('wwwroot') . 'collection/index.php';
if ($urlparams) {
$baseurl .= '?' . http_build_query($urlparams);
}
if ($collection->is_submitted()) {
......@@ -57,20 +81,20 @@ $form = pieform(array(
'submit' => array(
'type' => 'submitcancel',
'value' => array(get_string('yes'), get_string('no')),
'goto' => get_config('wwwroot') . 'collection/',
'goto' => $baseurl,
),
),
));
$smarty = smarty();
$smarty->assign('subheading', TITLE);
$smarty->assign('PAGEHEADING', TITLE);
$smarty->assign('message', get_string('collectionconfirmdelete', 'collection'));
$smarty->assign('form', $form);
$smarty->display('collection/delete.tpl');
function deletecollection_submit(Pieform $form, $values) {
global $SESSION, $collection;
global $SESSION, $collection, $baseurl;
$collection->delete();
$SESSION->add_ok_msg(get_string('collectiondeleted', 'collection'));
redirect('/collection/');
redirect($baseurl);
}
......@@ -26,7 +26,6 @@
*/
define('INTERNAL', 1);
define('MENUITEM', 'myportfolio/collection');
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'collection');
......@@ -37,33 +36,86 @@ require_once('pieforms/pieform.php');
require_once('collection.php');
$new = param_boolean('new', 0);
$id = !$new ? param_integer('id') : 0;
$copy = param_boolean('copy', 0);
$collection = new Collection($id);
if (!$USER->can_edit_collection($collection)) {
$SESSION->add_error_msg(get_string('canteditdontown', 'collection'));
redirect('/collection/');
if ($new) { // if creating a new collection
$owner = null;
$groupid = param_integer('group', 0);
$institutionname = param_alphanum('institution', false);
if (empty($groupid) && empty($institutionname)) {
$owner = $USER->get('id');
}
$collection = new Collection(null, array('owner' => $owner, 'group' => $groupid, 'institution' => $institutionname));
define('SUBTITLE', get_string('edittitleanddesc', 'collection'));
}
else { // if editing an existing or copied collection
$id = param_integer('id');
$collection = new Collection($id);
$owner = $collection->get('owner');
$groupid = $collection->get('group');
$institutionname = $collection->get('institution');
define('SUBTITLE', $collection->get('name').': '.get_string('edittitleanddesc', 'collection'));
}
if ($collection->is_submitted()) {
$submitinfo = $collection->submitted_to();
throw new AccessDeniedException(get_string('canteditsubmitted', 'collection', $submitinfo->name));
}
// if not a new collection
if (!$new) {
define('TITLE', $collection->get('name').': '.get_string('edittitleanddesc', 'collection'));
$urlparams = array();
if (!empty($groupid)) {
define('MENUITEM', 'groups/collections');
define('GROUP', $groupid);
$group = group_current_group();
define('TITLE', $group->name . ' - ' . get_string('editcollection', 'collection'));
$urlparams['group'] = $groupid;
}
else if (!empty($institutionname)) {
if ($institutionname == 'mahara') {
define('ADMIN', 1);
define('MENUITEM', 'configsite/collections');
}
else {
define('INSTITUTIONALADMIN', 1);
define('MENUITEM', 'manageinstitutions/institutioncollections');
}
define('TITLE', get_string('editcollection', 'collection'));
$urlparams['institution'] = $institutionname;
}
else {
define('TITLE', get_string('edittitleanddesc', 'collection'));
define('MENUITEM', 'myportfolio/collection');
define('TITLE', get_string('editcollection', 'collection'));
}
if (!$USER->can_edit_collection($collection)) {
throw new AccessDeniedException(get_string('canteditcollection', 'collection'));
}
$baseurl = get_config('wwwroot') . 'collection/index.php';
if ($urlparams) {
$baseurl .= '?' . http_build_query($urlparams);
}
$elements = $collection->get_collectionform_elements();
$submitstr = $new ? array('cancel' => get_string('cancel'), 'submit' => get_string('next') . ': ' . get_string('editviews', 'collection'))
: array(get_string('save'), get_string('cancel'));
$confirm = $new ? array('cancel' => get_string('confirmcancelcreatingcollection','collection')) : null;
if ($copy) {
$type = 'submit';
$submitstr = get_string('next') . ': ' . get_string('editviews', 'collection');
$confirm = null;
}
else {
$type = 'submitcancel';
if ($new) {
$submitstr = array('cancel' => get_string('cancel'), 'submit' => get_string('next') . ': ' . get_string('editviews', 'collection'));
$confirm = array('cancel' => get_string('confirmcancelcreatingcollection','collection'));
}
else {
$submitstr = array(get_string('save'), get_string('cancel'));
$confirm = null;
}
}
$elements['submit'] = array(
'type' => 'submitcancel',
'type' => $type,
'value' => $submitstr,
'confirm' => $confirm,
);
......@@ -77,24 +129,28 @@ $form = pieform(array(
));
$smarty = smarty();
$smarty->assign('PAGEHEADING', TITLE);
if (!empty($groupid)) {
$smarty->assign('PAGESUBHEADING', SUBTITLE);
$smarty->assign('PAGEHELPNAME', '0');
$smarty->assign('SUBPAGEHELPNAME', '1');
}
else {
$smarty->assign('PAGEHEADING', SUBTITLE);
}
$smarty->assign_by_ref('form', $form);
$smarty->display('collection/edit.tpl');
function submit(Pieform $form, $values) {
global $SESSION, $new;
global $SESSION, $new, $copy, $urlparams;
$values['navigation'] = (int) $values['navigation'];
$collection = Collection::save($values);
if (!$new) {
$SESSION->add_ok_msg(get_string('collectionsaved', 'collection'));
}
$collection->post_edit_redirect($new);
$collection->post_edit_redirect($new, $copy, $urlparams);
}
function edit_cancel_submit() {
global $collection, $new;
if ($new && $collection) {
$collection->delete();
}
redirect('/collection/');
global $baseurl;
redirect($baseurl);
}
......@@ -26,8 +26,6 @@
*/
define('INTERNAL', 1);
define('MENUITEM', 'myportfolio/collection');
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'collection');
define('SECTION_PAGE', 'index');
......@@ -35,13 +33,75 @@ define('SECTION_PAGE', 'index');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
require_once('collection.php');
define('TITLE', get_string('Collections', 'collection'));
// offset and limit for pagination
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 5);
$data = Collection::get_mycollections_data($offset, $limit);
$owner = null;
$groupid = param_integer('group', 0);
$institutionname = param_alphanum('institution', false);
$urlparams = array();
if (!empty($groupid)) {
define('MENUITEM', 'groups/collections');
define('GROUP', $groupid);
$group = group_current_group();
// Check if user can edit group collections <-> user can edit group views
$role = group_user_access($group->id);
$canedit = $role && group_role_can_edit_views($group, $role);
if (!$canedit) {
throw new GroupAccessDeniedException(get_string('cantlistgroupcollections', 'collection'));
}
define('SUBTITLE', get_string('groupcollections', 'collection'));
define('TITLE', $group->name);
$urlparams['group'] = $groupid;
}
else if (!empty($institutionname)) {
if ($institutionname == 'mahara') {
define('ADMIN', 1);
define('MENUITEM', 'configsite/collections');
define('TITLE', get_string('sitecollections', 'collection'));
// Check if user is a site admin
$canedit = $USER->get('admin');
if (!$canedit) {
throw new AccessDeniedException(get_string('cantlistinstitutioncollections', 'collection'));
}
}
else {
define('INSTITUTIONALADMIN', 1);
define('MENUITEM', 'manageinstitutions/institutioncollections');
define('TITLE', get_string('institutioncollections', 'collection'));
// Check if user is a institution admin
$canedit = $USER->get('admin') || $USER->is_institutional_admin();
if (!$canedit) {
throw new AccessDeniedException(get_string('cantlistinstitutioncollections', 'collection'));
}
require_once('institution.php');
// Get list of availlable institutions
$s = institution_selector_for_page($institutionname, get_config('wwwroot') . 'collection/index.php');
$institutionname = $s['institution'];
if ($institutionname === false) {
$smarty = smarty();
$smarty->display('admin/users/noinstitutions.tpl');
exit;
}
}
define('SUBTITLE', '');
$urlparams['institution'] = $institutionname;
}
else {
define('MENUITEM', 'myportfolio/collection');
$owner = $USER->get('id');
define('SUBTITLE', '');
define('TITLE', get_string('Collections', 'collection'));
}
$baseurl = get_config('wwwroot') . 'collection/index.php';
if ($urlparams) {
$baseurl .= '?' . http_build_query($urlparams);
}
$data = Collection::get_mycollections_data($offset, $limit, $owner, $groupid, $institutionname);
foreach ($data->data as $value) {
$collection = new Collection($value->id);
$views = $collection->get('views');
......@@ -53,7 +113,7 @@ foreach ($data->data as $value) {
$pagination = build_pagination(array(
'id' => 'collectionslist_pagination',
'class' => 'center',
'url' => get_config('wwwroot') . 'collection/index.php',
'url' => $baseurl,
'count' => $data->count,
'limit' => $data->limit,
'offset' => $data->offset,
......@@ -67,9 +127,20 @@ $pagination = build_pagination(array(
));
$smarty = smarty(array('paginator'));
$urlparamsstr = '';
if ($urlparams) {
$urlparamsstr = '&' . http_build_query($urlparams);
}
$smarty->assign('addonelink', get_config('wwwroot') . 'collection/edit.php?new=1' . $urlparamsstr);
if (!empty($institutionname) && ($institutionname != 'mahara')) {
$smarty->assign('institution', $institutionname);
$smarty->assign('institutionselector', $s['institutionselector']);
$smarty->assign('INLINEJAVASCRIPT', $s['institutionselectorjs']);
}
$smarty->assign('urlparamsstr', $urlparamsstr);
$smarty->assign('collections', $data->data);
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('strnocollectionsaddone',
get_string('nocollectionsaddone','collection','<a href="' . get_config('wwwroot') . 'collection/edit.php?new=1">', '</a>'));
$smarty->assign('PAGEHEADING', TITLE);
$smarty->assign('PAGESUBHEADING', SUBTITLE);
$smarty->display('collection/index.tpl');
......@@ -26,7 +26,6 @@
*/
define('INTERNAL', 1);
define('MENUITEM', 'myportfolio/collection');
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'collection');
......@@ -44,16 +43,45 @@ $direction = param_variable('direction','');
$collection = new Collection($id);
if (!$USER->can_edit_collection($collection)) {
$SESSION->add_error_msg(get_string('canteditdontown', 'collection'));
redirect('/collection/');
throw new AccessDeniedException(get_string('canteditcollection', 'collection'));
}
$owner = $collection->get('owner');
$groupid = $collection->get('group');
$institutionname = $collection->get('institution');
$urlparams = array();
if (!empty($groupid)) {
define('MENUITEM', 'groups/collections');
define('GROUP', $groupid);
$group = group_current_group();
define('TITLE', $group->name . ' - ' . get_string('editcollection', 'collection'));
$urlparams['group'] = $groupid;
}
else if (!empty($institutionname)) {
if ($institutionname == 'mahara') {
define('ADMIN', 1);
define('MENUITEM', 'configsite/collections');
}
else {
define('INSTITUTIONALADMIN', 1);
define('MENUITEM', 'manageinstitutions/institutioncollections');
}
define('TITLE', get_string('editcollection', 'collection'));
$urlparams['institution'] = $institutionname;
}
else {
define('MENUITEM', 'myportfolio/collection');
define('TITLE', get_string('editcollection', 'collection'));
}
define('SUBTITLE', $collection->get('name'). ': ' . get_string('editviews', 'collection'));
$baseurl = get_config('wwwroot') . 'collection/index.php';
if ($urlparams) {
$baseurl .= '?' . http_build_query($urlparams);
}
if ($collection->is_submitted()) {
$submitinfo = $collection->submitted_to();
throw new AccessDeniedException(get_string('canteditsubmitted', 'collection', $submitinfo->name));
}
define('TITLE', $collection->get('name') . ': ' . get_string('editviews', 'collection'));
if ($view AND !empty($direction)) {
$collection->set_viewdisplayorder($view,$direction);
redirect('/collection/views.php?id='.$id);
......@@ -83,7 +111,7 @@ if ($views) {
$elements = array();
$viewsform = null;
if ($available = Collection::available_views()) {
if ($available = Collection::available_views($owner, $groupid, $institutionname)) {
foreach ($available as $a) {
$elements['view_'.$a->id] = array(
'type' => 'checkbox',
......@@ -108,7 +136,15 @@ if ($available = Collection::available_views()) {
$smarty = smarty();
$smarty->assign('PAGEHEADING', TITLE);
if (!empty($groupid)) {
$smarty->assign('PAGESUBHEADING', SUBTITLE);
$smarty->assign('PAGEHELPNAME', '0');
$smarty->assign('SUBPAGEHELPNAME', '1');
}
else {
$smarty->assign('PAGEHEADING', SUBTITLE);
}
$smarty->assign('baseurl', $baseurl);
$smarty->assign('displayurl',get_config('wwwroot').'collection/views.php?id='.$id);
$smarty->assign('removeurl',get_config('wwwroot').'collection/deleteview.php?id='.$id);
$smarty->assign_by_ref('views', $views);
......
......@@ -40,6 +40,12 @@ $string['add'] = 'Add';
$string['addviews'] = 'Add pages';
$string['addviewstocollection'] = 'Add pages to collection';
$string['back'] = 'Back';
$string['cantlistgroupcollections'] = 'You are not allowed to list group collections.';
$string['cantlistinstitutioncollections'] = 'You are not allowed to list institution collections.';
$string['canteditgroupcollections'] = 'You are not allowed to edit group collections.';
$string['canteditinstitutioncollections'] = 'You are not allowed to edit institution collections.';
$string['canteditcollection'] = 'You are not allowed to edit this collection.';
$string['cantcreatecollection'] = 'You are not allowed to create this collection.';
$string['cantdeletecollection'] = 'You cannot delete this collection.';
$string['canteditdontown'] = 'You cannot edit this collection because you do not own it.';
$string['canteditsubmitted'] = 'You can\'t edit this collection because it has been submitted for assessment to %s. You will have to wait until a tutor releases it.';
......@@ -47,6 +53,9 @@ $string['collection'] = 'collection';
$string['Collection'] = 'Collection';
$string['collections'] = 'Collections';
$string['Collections'] = 'Collections';
$string['groupcollections'] = 'Group collections';
$string['institutioncollections'] = 'Institution collections';
$string['sitecollections'] = 'Site collections';
$string['collectionaccess'] = 'Collection access';
$string['collectionaccesseditedsuccessfully'] = 'Collection access saved successfully';
$string['collectioneditaccess'] = 'You are editing access for %d pages in this collection';
......@@ -76,6 +85,7 @@ $string['emptycollection'] = 'Empty collection';
$string['manageviews'] = 'Manage pages';
$string['name'] = 'Collection name';
$string['newcollection'] = 'New collection';
$string['nocollections'] = 'No collections yet.';
$string['nocollectionsaddone'] = 'No collections yet. %sAdd one%s!';
$string['nooverride'] = 'No override';
$string['noviewsavailable'] = 'No pages are available to add.';
......
......@@ -277,6 +277,7 @@ $string['currentfriends'] = 'Current friends';
$string['pendingfriends'] = 'Pending friends';
$string['backtofriendslist'] = 'Back to friends list';
$string['findnewfriends'] = 'Find new friends';
$string['Collections'] = 'Collections';
$string['Views'] = 'Pages';
$string['Files'] = 'Files';
$string['noviewstosee'] = 'None that you can see :(';
......
......@@ -33,6 +33,8 @@ class Collection {
private $name;
private $description;
private $owner;
private $group;
private $institution;
private $mtime;
private $ctime;
private $navigation;
......@@ -42,8 +44,6 @@ class Collection {
private $views;
public function __construct($id=0, $data=null) {
global $USER;
$userid = $USER->get('id');
if (!empty($id)) {
$tempdata = get_record('collection','id',$id);
......@@ -61,7 +61,6 @@ class Collection {
else {
$this->ctime = time();
$this->mtime = time();
$this->owner = $userid;
}
if (empty($data)) {
......@@ -236,6 +235,18 @@ class Collection {
$data->name = self::new_name($desiredname, (object)$collectiondata);
$data->description = $colltemplate->get('description');
$data->navigation = $colltemplate->get('navigation');
if (!empty($collectiondata['group'])) {
$data->group = $collectiondata['group'];
}
else if (!empty($collectiondata['institution'])) {
$data->institution = $collectiondata['institution'];
}
else if (!empty($collectiondata['owner'])) {
$data->owner = $collectiondata['owner'];
}
else {
$data->owner = $userid;
}
$collection = self::save($data);
......@@ -244,7 +255,13 @@ class Collection {
$views = $colltemplate->get('views');
$copyviews = array();
foreach ($views['views'] as $v) {
$values = array('new' => true, 'usetemplate' => $v->view);
$values = array(
'new' => true,
'owner' => isset($data->owner) ? $data->owner : null,
'group' => isset($data->group) ? $data->group : null,
'institution' => isset($data->institution) ? $data->institution : null,
'usetemplate' => $v->view
);
list($view, $template, $copystatus) = View::create_from_template($values, $v->view, $userid, $checkaccess);
if (isset($copystatus['quotaexceeded'])) {
$SESSION->clear('messages');
......@@ -289,32 +306,51 @@ class Collection {
}
/**
* Returns a list of the current users collections
* Returns a list of the current user, group, or institution collections
*
* @param offset current page to display
* @param limit how many collections to display per page
* @param groupid current group ID
* @param institutionname current institution name
* @return array (count: integer, data: array, offset: integer, limit: integer)
*/
public static function get_mycollections_data($offset=0, $limit=10) {
global $USER;
($data = get_records_sql_assoc("
SELECT c.id, c.description, c.name
public static function get_mycollections_data($offset=0, $limit=10, $owner=null, $groupid=null, $institutionname=null) {
if (!empty($groupid)) {
$wherestm = '"group" = ?';
$values = array($groupid);
$count = count_records('collection', 'group', $groupid);
}
else if (!empty($institutionname)) {
$wherestm = 'institution = ?';
$values = array($institutionname);
$count = count_records('collection', 'institution', $institutionname);
}
else if (!empty($owner)) {
$wherestm = 'owner = ?';
$values = array($owner);
$count = count_records('collection', 'owner', $owner);
}
else {
$count = 0;
}
$data = array();
if ($count > 0) {
$data = get_records_sql_assoc("
SELECT c.id, c.description, c.name
FROM {collection} c
WHERE c.owner = ?
ORDER BY c.na