From 3f3ee7114e21d47e6c9b8b60cce3cecd5707ab44 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Tue, 1 Nov 2011 14:15:55 +0100 Subject: [PATCH] MDL-29794 Do not offer re-sharing of unmodified forms The patch introduces a new field 'timecopied' that holds the timestamp of when the given form was cloned last time. Using this field, we can decide whether the form can be actually re-shared or not. Note that the logic just hides the icon. It is meant as a usability feature, not any real protection from sharing. --- grade/grading/form/rubric/lib.php | 2 +- grade/grading/manage.php | 43 +++++++++++++++++++++++++++++-- grade/grading/templates.php | 6 ++++- lang/en/grading.php | 3 ++- lib/db/install.xml | 7 ++--- lib/db/upgrade.php | 10 +++++++ version.php | 2 +- 7 files changed, 64 insertions(+), 9 deletions(-) diff --git a/grade/grading/form/rubric/lib.php b/grade/grading/form/rubric/lib.php index 085ee0afb8d..8cdc9b80e61 100644 --- a/grade/grading/form/rubric/lib.php +++ b/grade/grading/form/rubric/lib.php @@ -198,7 +198,7 @@ class gradingform_rubric_controller extends gradingform_controller { if ($this->definition === false) { $this->definition = new stdClass(); foreach (array('id', 'name', 'description', 'descriptionformat', 'status', 'copiedfromid', - 'timecreated', 'usercreated', 'timemodified', 'usermodified', 'options') as $fieldname) { + 'timecreated', 'usercreated', 'timemodified', 'usermodified', 'timecopied', 'options') as $fieldname) { $this->definition->$fieldname = $record->$fieldname; } $this->definition->rubric_criteria = array(); diff --git a/grade/grading/manage.php b/grade/grading/manage.php index 9bdabdd19cc..1b93e1ec538 100644 --- a/grade/grading/manage.php +++ b/grade/grading/manage.php @@ -117,6 +117,7 @@ if (!empty($shareform)) { $targetcontroller = $targetarea->get_controller($method); $targetcontroller->update_definition($controller->get_definition_copy($targetcontroller), gradingform_controller::DEFINITION_STATUS_READY); + $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id)); redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactionsharedone', 'core_grading')))); } } @@ -161,13 +162,51 @@ if (!empty($method)) { echo $output->container_start('actions'); if ($controller->is_form_defined()) { $definition = $controller->get_definition(); + // icon to edit the form definition echo $output->management_action_icon($controller->get_editor_url($returnurl), get_string('manageactionedit', 'core_grading'), 'b/document-edit'); + // icon to delete the current form definition echo $output->management_action_icon(new moodle_url($PAGE->url, array('deleteform' => $definition->id)), get_string('manageactiondelete', 'core_grading'), 'b/edit-delete'); + // icon to save the form as a new template if (has_capability('moodle/grade:sharegradingforms', get_system_context())) { - echo $output->management_action_icon(new moodle_url($PAGE->url, array('shareform' => $definition->id)), - get_string('manageactionshare', 'core_grading'), 'b/bookmark-new'); + if (empty($definition->copiedfromid)) { + $hasoriginal = false; + } else { + $hasoriginal = $DB->record_exists('grading_definitions', array('id' => $definition->copiedfromid)); + } + if (!$hasoriginal) { + // was created from scratch or is orphaned + if (empty($definition->timecopied)) { + // was never shared before + $allowshare = true; + } else if ($definition->timemodified > $definition->timecopied) { + // was modified since last time shared + $allowshare = true; + } else { + // was not modified since last time shared + $allowshare = false; + } + } else { + // was created from a template and the template still exists + if ($definition->timecreated == $definition->timemodified) { + // was not modified since created + $allowshare = false; + } else if (empty($definition->timecopied)) { + // was modified but was not re-shared yet + $allowshare = true; + } else if ($definition->timemodified > $definition->timecopied) { + // was modified since last time re-shared + $allowshare = true; + } else { + // was not modified since last time re-shared + $allowshare = false; + } + } + if ($allowshare) { + echo $output->management_action_icon(new moodle_url($PAGE->url, array('shareform' => $definition->id)), + get_string('manageactionshare', 'core_grading'), 'b/bookmark-new'); + } } } else { echo $output->management_action_icon($controller->get_editor_url($returnurl), diff --git a/grade/grading/templates.php b/grade/grading/templates.php index d7e89ca8799..397407f96a1 100644 --- a/grade/grading/templates.php +++ b/grade/grading/templates.php @@ -90,6 +90,7 @@ if ($pick) { require_sesskey(); $targetcontroller->update_definition($sourcecontroller->get_definition_copy($targetcontroller), gradingform_controller::DEFINITION_STATUS_READY); + $DB->set_field('grading_definitions', 'timecopied', time(), array('id' => $definition->id)); redirect(new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid))); } } @@ -209,7 +210,10 @@ foreach ($rs as $template) { $rs->close(); if (!$found) { - echo $output->heading(get_string('nothingtodisplay')); + echo $output->heading(get_string('nosharedformfound', 'core_grading')); + echo $output->single_button( + new moodle_url('/grade/grading/manage.php', array('areaid' => $targetid)), + get_string('back'), 'get'); } echo $output->footer(); diff --git a/lang/en/grading.php b/lang/en/grading.php index e8f9f244c07..b7f06da0bee 100644 --- a/lang/en/grading.php +++ b/lang/en/grading.php @@ -53,9 +53,10 @@ $string['manageactiondeletedone'] = 'The form was successfully deleted'; $string['manageactionedit'] = 'Edit the current form definition'; $string['manageactionnew'] = 'Define new grading form from scratch'; $string['manageactionshare'] = 'Publish the form as a new template'; -$string['manageactionshareconfirm'] = 'You are going to save a copy of the grading form \'{$a}\' as a new public template. Other users at your site will be able to create new grading forms in their activities from that template. Note that users are able to reuse their own grading forms in other activities even if the forms were not saved as template.'; +$string['manageactionshareconfirm'] = 'You are going to save a copy of the grading form \'{$a}\' as a new public template. Other users at your site will be able to create new grading forms in their activities from that template.'; $string['manageactionsharedone'] = 'The form was successfully saved as a template'; $string['noitemid'] = 'Grading not possible. The graded item does not exist.'; +$string['nosharedformfound'] = 'No template found'; $string['templatedelete'] = 'Delete'; $string['templatedeleteconfirm'] = 'You are going to delete the shared template \'{$a}\'. Deleting a template does not affect existing forms that were created from it.'; $string['templateedit'] = 'Edit'; diff --git a/lib/db/install.xml b/lib/db/install.xml index 967061d317d..c4e17269729 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2813,8 +2813,9 @@ - - + + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 602cbc902c1..cd78543e80b 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -6885,6 +6885,16 @@ FROM upgrade_main_savepoint(true, 2011102700.03); } + // TODO squash this before merging into the master - MDL-29798 + if ($oldversion < 2011102700.05) { + $table = new xmldb_table('grading_definitions'); + $field = new xmldb_field('timecopied', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0', 'usermodified'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + upgrade_main_savepoint(true, 2011102700.05); + } + return true; } diff --git a/version.php b/version.php index 93d0a884ca5..cb6a6a96df8 100644 --- a/version.php +++ b/version.php @@ -31,7 +31,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2011102700.04; // YYYYMMDD = weekly release date of this DEV branch +$version = 2011102700.05; // YYYYMMDD = weekly release date of this DEV branch // RR = release increments - 00 in DEV branches // .XX = incremental changes