Commit 1dcaef85 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1763163: record who has been given what page for one time copy



This changes the parent patch a bit. Instead of recording on the view
table if a page is able to be copied to existing group members we
insetad record in a new existing copy table if a group page/collection
has been copied to a member and if not make the copy.

We've also made the switch a slide switch, eg when switching it to
'Yes' it does the task on save and then switches back to 'No'.

This means at a later time if set to 'Yes' again it will do the copying
again and give any newer members a copy of all pages/collections as
well as give older members copies of any newer pages / collections /
collection pages since the previous switching to 'No'

Note: Currently adding new collection pages to older members will only
add the pages as singular pages to their portfolio.

behatnotneeded

Change-Id: Ia22d8138d80ec5fdb5a401de7289b256f9e5fc69
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 01af3dab
......@@ -1408,9 +1408,11 @@ class User {
/**
* Makes a literal copy of a list of views for this user.
*
* @param array $templateids A list of viewids to copy.
* @param array $templateids A list of viewids to copy.
* @param boolean $checkviewaccess Check that the user can see the view before copying it.
* @param boolean $onlyonce Check that the user already has a copy of the view.
*/
public function copy_views($templateids, $checkviewaccess=true) {
public function copy_views($templateids, $checkviewaccess=true, $onlyonce=false) {
if (!$templateids) {
// Nothing to do
return;
......@@ -1428,23 +1430,31 @@ class User {
db_begin();
$artefactcopies = array();
$copied = array();
foreach ($templateids as $tid) {
View::create_from_template(array(
if ($onlyonce && get_field('existingcopy', 'id', 'view', $tid, 'usr', $this->get('id'))) {
continue;
}
list($view) = View::create_from_template(array(
'owner' => $this->get('id'),
'title' => $views[$tid]->title,
'description' => $views[$tid]->description,
'type' => $views[$tid]->type == 'profile' && $checkviewaccess ? 'portfolio' : $views[$tid]->type,
), $tid, $this->get('id'), $checkviewaccess, false, $artefactcopies);
$copied[$tid] = $view->get('id');
}
db_commit();
return $copied;
}
/**
* Makes a literal copy of a list of collections for this user.
*
* @param array $templateids A list of collectionids to copy.
* @param boolean $checkviewaccess Check that the user can see the view before copying it.
* @param boolean $onlyonce Check that the user already has a copy of the collection and all views within it.
*/
public function copy_collections($templateids, $checkviewaccess=true) {
public function copy_collections($templateids, $checkviewaccess=true, $onlyonce=false) {
if (!$templateids) {
// Nothing to do
return;
......@@ -1462,10 +1472,42 @@ class User {
db_begin();
foreach ($templateids as $tid) {
Collection::create_from_template(array(
'owner' => $this->get('id'),
'title' => $collections[$tid]->name,
), $tid, $this->get('id'), $checkviewaccess);
$anyexistingviews = get_records_sql_array("
SELECT cv.*, (
CASE WHEN EXISTS (
SELECT 1 FROM {existingcopy} ec
WHERE ec.collection = cv.collection
AND ec.view = cv.view
AND ec.usr = ?) THEN 1 ELSE 0 END
) AS hascopy
FROM {collection_view} cv
WHERE cv.collection = ?", array($this->get('id'), $tid));
$sum = 0;
foreach ($anyexistingviews as $item) {
$sum += $item->hascopy;
}
if ($onlyonce && $sum > 0 && $sum === count($anyexistingviews)) {
// We have all views for this collection so skip
continue;
}
else if ($onlyonce && $sum > 0 && $sum < count($anyexistingviews)) {
// We have some but not all views so we need to add missing ones to the collection
foreach ($anyexistingviews as $ev) {
if (!$ev->hascopy) {
$copied = $this->copy_views(array($ev->view), $checkviewaccess, $onlyonce);
// @TODO add copied page to user's collection
// We can't do this yet as we don't know what id of collection that was made or if it still exists
// so we just add the singular page - the user can add it their collection if they wish
}
}
}
else {
// Copy full collection
Collection::create_from_template(array(
'owner' => $this->get('id'),
'title' => $collections[$tid]->name,
), $tid, $this->get('id'), $checkviewaccess);
}
}
db_commit();
}
......@@ -1532,21 +1574,43 @@ class User {
/**
* Makes a literal copy of a list of views and collections for existing group members.
*
* @param array values .
* @param boolean collection
* @param array $templateids Array of either view ids or collection ids
* @param boolean $collection Are the supplied ids collection ids
*/
public function copy_group_views_collections_to_existing_members($views, $collection = false) {
if (empty($views)) {
public function copy_group_views_collections_to_existing_members($templateids, $collection = false) {
if (empty($templateids)) {
return;
}
if ($collection) {
// Copy the collection to the current users portfolio
$this->copy_collections($views, false);
$this->copy_collections($templateids, false, true);
// Need to loop thru collections to find the list of viewids
$results = get_records_select_array('collection_view', 'collection IN (' . implode(', ', db_array_to_ph($templateids)) . ')', $templateids, '', 'collection, view, displayorder');
foreach ($results as $result) {
$where = new StdClass;
$where->view = $result->view;
$where->collection = $result->collection;
$where->usr = $this->id;
$record = clone $where;
$record->ctime = db_format_timestamp(time());
ensure_record_exists('existingcopy', $where, $record);
}
}
else {
// Copy the page to the current users portfolio
$this->copy_views($views, false);
$this->copy_views($templateids, false, true);
// Loop thru viewids to add them to the done table
foreach ($templateids as $id) {
$where = new StdClass;
$where->view = $id;
$where->usr = $this->id;
$record = clone $where;
$record->ctime = db_format_timestamp(time());
ensure_record_exists('existingcopy', $where, $record);
}
}
}
}
......
......@@ -424,7 +424,7 @@ $string['confirmcopydesc'] = 'Please choose which you would like to copy:';
$string['Untitled'] = 'Untitled';
$string['copyforexistingmembersprogress'] = 'Copying portfolios for existing group members';
$string['existinggroupmembercopy'] = 'Copy for existing group members';
$string['existinggroupmembercopydesc1'] = 'Copy the selected pages / collections to the personal portfolio area of all existing group members. People who are added to the group after this setting is enabled will not be affected.';
$string['existinggroupmembercopydesc1'] = 'Copy the selected pages / collections to the personal portfolio area of all existing group members. The slide switch resets after saving. Group members will only get a copy once.';
$string['copyfornewusers'] = 'Copy for new users';
$string['copyfornewusersdescription2'] = 'Whenever a new user is created, automatically make a personal copy of the selected pages / collections in the user\'s account. If you want these users to be able to copy the selected pages / collections later on as well, please allow copying in general.';
$string['copyfornewmembers'] = 'Copy for new institution members';
......
......@@ -154,6 +154,7 @@ class Collection {
delete_records('collection_view','collection',$this->id);
delete_records('collection_tag','collection',$this->id);
delete_records('collection','id',$this->id);
delete_records('existingcopy', 'collection', $this->id);
// Secret url records belong to the collection, so remove them from the view.
// @todo: add user message to whatever calls this.
......@@ -243,6 +244,8 @@ class Collection {
* @param int $userid The user who has issued the command to create the
* collection.
* @param int $checkaccess Whether to check that the user can see the collection before copying it
* @param boolean $titlefromtemplate Title of new collection or view will be exactly copied from the template
*
* @return array A list consisting of the new collection, the template collection and
* information about the copy - i.e. how many blocks and
* artefacts were copied
......
......@@ -806,7 +806,6 @@
<FIELD NAME="locked" TYPE="int" LENGTH="1" DEFAULT="0" NOTNULL="true" />
<FIELD NAME="urlid" TYPE="char" LENGTH="100" NOTNULL="false" />
<FIELD NAME="anonymise" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" />
<FIELD NAME="existinggroupmembercopy" TYPE="int" LENGTH="1" DEFAULT="0" NOTNULL="true" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
......@@ -1143,6 +1142,21 @@
<KEY NAME="collectionfk" TYPE="foreign" FIELDS="collection" REFTABLE="collection" REFFIELDS="id" />
</KEYS>
</TABLE>
<TABLE NAME="existingcopy">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
<FIELD NAME="view" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="collection" TYPE="int" LENGTH="10" NOTNULL="false" />
<FIELD NAME="usr" TYPE="int" LENGTH="10" NOTNULL="true" />
<FIELD NAME="ctime" TYPE="datetime" NOTNULL="true" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="viewfk" TYPE="foreign" FIELDS="view" REFTABLE="view" REFFIELDS="id" />
<KEY NAME="collectionfk" TYPE="foreign" FIELDS="collection" REFTABLE="collection" REFFIELDS="id" />
<KEY NAME="usrfk" TYPE="foreign" FIELDS="usr" REFTABLE="usr" REFFIELDS="id" />
</KEYS>
</TABLE>
<TABLE NAME="iframe_source_icon">
<FIELDS>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" />
......
......@@ -5845,5 +5845,26 @@ function xmldb_core_upgrade($oldversion=0) {
add_field($table, $field);
}
if ($oldversion < 2018050201) {
log_debug('Create new "existingcopy" table to map who already has what. Also drop "existinggroupmembercopy" field');
$table = new XMLDBTable('view');
$field = new XMLDBField('existinggroupmembercopy');
drop_field($table, $field);
$table = new XMLDBTable('existingcopy');
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
$table->addFieldInfo('view', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL);
$table->addFieldInfo('collection', XMLDB_TYPE_INTEGER, 10);
$table->addFieldInfo('usr', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL);
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, null, XMLDB_NOTNULL);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addKeyInfo('viewfk', XMLDB_KEY_FOREIGN, array('view'), 'view', array('id'));
$table->addKeyInfo('collectionfk', XMLDB_KEY_FOREIGN, array('collection'), 'collection', array('id'));
$table->addKeyInfo('usrfk', XMLDB_KEY_FOREIGN, array('usr'), 'usr', array('id'));
create_table($table);
}
return $status;
}
......@@ -1585,6 +1585,7 @@ function delete_user($userid) {
delete_records('usr_login_data', 'usr', $userid);
delete_records('usr_pendingdeletion', 'usr', $userid); // just in case
delete_records('usr_agreement', 'usr', $userid);
delete_records('existingcopy', 'usr', $userid);
if (is_plugin_active('framework', 'module')) {
delete_records('framework_assessment_feedback', 'usr', $userid);
......
......@@ -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 = 2018050200;
$config->version = 2018050201;
$config->series = '18.10';
$config->release = '18.10dev';
$config->minupgradefrom = 2015030409;
......
......@@ -63,7 +63,6 @@ class View {
private $urlid;
private $skin;
private $anonymise = 0;
private $existinggroupmembercopy = 0;
const UNSUBMITTED = 0;
const SUBMITTED = 1;
......@@ -884,6 +883,7 @@ class View {
delete_records('view_autocreate_grouptype', 'view', $this->id);
delete_records('view_tag','view',$this->id);
delete_records('view_visit','view',$this->id);
delete_records('existingcopy', 'view', $this->id);
$eventdata = array('id' => $this->id, 'eventfor' => 'view');
if ($collection = $this->get_collection()) {
$eventdata['collection'] = $collection->get('id');
......@@ -1058,9 +1058,6 @@ class View {
if (isset($config['copynewgroups'])) {
$v->set('copynewgroups', $config['copynewgroups']);
}
if (isset($config['existinggroupmembercopy'])) {
$v->set('existinggroupmembercopy', $config['existinggroupmembercopy']);
}
$v->commit();
}
......
......@@ -203,7 +203,7 @@ if ($group && in_array( $USER->get('id'), $admintutorids, true )) {
'type' => 'switchbox',
'title' => get_string('existinggroupmembercopy', 'view'),
'description' => get_string('existinggroupmembercopydesc1', 'view'),
'defaultvalue' => $view->get('existinggroupmembercopy'),
'defaultvalue' => 0,
)));
}
......@@ -511,7 +511,7 @@ function editaccess_submit(Pieform $form, $values) {
$viewconfig['existinggroupmembercopy'] = !empty($values['existinggroupmembercopy']) ? $values['existinggroupmembercopy'] : 0;
// Add funtionality here which copies the page into existing group members pages.
if ($viewconfig['existinggroupmembercopy'] && !$view->get('existinggroupmembercopy')) {
if ($viewconfig['existinggroupmembercopy']) {
$groupmembers = group_get_member_ids($group, array('member'));
$key = 0;
$total = count($groupmembers);
......
......@@ -135,7 +135,7 @@ if ($group && in_array($USER->get('id'), $admintutorids, true)) {
'type' => 'switchbox',
'title' => get_string('existinggroupmembercopy', 'view'),
'description' => get_string('existinggroupmembercopydesc1', 'view'),
'defaultvalue' => $view->get('existinggroupmembercopy'),
'defaultvalue' => 0,
)));
}
......@@ -431,7 +431,7 @@ function accessurl_submit(Pieform $form, $values) {
// Add functionality here which copies the page into existing
// group members pages.
if ($viewconfig['existinggroupmembercopy'] && !$view->get('existinggroupmembercopy')) {
if ($viewconfig['existinggroupmembercopy']) {
$groupmembers = group_get_member_ids($group, array('member'));
$key = 0;
$total = count($groupmembers);
......
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