From 671ec8f50e99cdbd58121951206f4e488db3a610 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Wed, 19 Oct 2011 13:25:47 +0200 Subject: [PATCH] Form definitions can be removed now --- grade/grading/form/lib.php | 32 +++++++++++++++++++++++++++++- grade/grading/form/rubric/lib.php | 20 +++++++++++++++++++ grade/grading/manage.php | 25 ++++++++++++++++++++++- grade/grading/yui/manage/manage.js | 4 ++-- lang/en/grading.php | 8 ++++++++ 5 files changed, 85 insertions(+), 4 deletions(-) diff --git a/grade/grading/form/lib.php b/grade/grading/form/lib.php index f0663aa6d91..f8e3b6100da 100644 --- a/grade/grading/form/lib.php +++ b/grade/grading/form/lib.php @@ -368,8 +368,31 @@ abstract class gradingform_controller { return $output->preview_definition_header($this); } - //////////////////////////////////////////////////////////////////////////// + /** + * Deletes the form definition and all the associated data + * + * @see delete_plugin_definition() + * @return void + */ + public function delete_definition() { + global $DB; + if (!$this->is_form_defined()) { + // nothing to do + return; + } + + // firstly, let the plugin delete everything from their own tables + $this->delete_plugin_definition(); + // then, delete all instances left + $DB->delete_records('grading_instances', array('formid' => $this->definition->id)); + // finally, delete the main definition record + $DB->delete_records('grading_definitions', array('id' => $this->definition->id)); + + $this->definition = false; + } + + //////////////////////////////////////////////////////////////////////////// /** * Loads the form definition if it exists @@ -385,6 +408,13 @@ abstract class gradingform_controller { 'method' => $this->get_method_name()), '*', IGNORE_MISSING); } + /** + * Deletes all plugin data associated with the given form definiton + * + * @see delete_definition() + */ + abstract protected function delete_plugin_definition(); + /** * @return string the name of the grading method plugin, eg 'rubric' * @see PARAM_PLUGIN diff --git a/grade/grading/form/rubric/lib.php b/grade/grading/form/rubric/lib.php index 67298ca0f54..763b94620fe 100644 --- a/grade/grading/form/rubric/lib.php +++ b/grade/grading/form/rubric/lib.php @@ -549,4 +549,24 @@ class gradingform_rubric_controller extends gradingform_controller { return $header . $rubric; } + + /** + * Deletes the rubric definition and all the associated information + */ + protected function delete_plugin_definition() { + global $DB; + + // get the list of instances + $instances = array_keys($DB->get_records('grading_instances', array('formid' => $this->definition->id), '', 'id')); + // delete all fillings + $DB->delete_records_list('gradingform_rubric_fillings', 'forminstanceid', $instances); + // delete instances + $DB->delete_records_list('grading_instances', 'id', $instances); + // get the list of criteria records + $criteria = array_keys($DB->get_records('gradingform_rubric_criteria', array('formid' => $this->definition->id), '', 'id')); + // delete levels + $DB->delete_records_list('gradingform_rubric_levels', 'criterionid', $criteria); + // delete critera + $DB->delete_records_list('gradingform_rubric_criteria', 'id', $criteria); + } } diff --git a/grade/grading/manage.php b/grade/grading/manage.php index f761a4fb4ab..7868f6afb59 100644 --- a/grade/grading/manage.php +++ b/grade/grading/manage.php @@ -37,6 +37,8 @@ $returnurl = optional_param('returnurl', null, PARAM_LOCALURL); $setmethod = optional_param('setmethod', null, PARAM_PLUGIN); // publish the given form definition as a new template in the forms bank $shareform = optional_param('shareform', null, PARAM_INT); +// delete the given form definition +$deleteform = optional_param('deleteform', null, PARAM_INT); // consider the required action as confirmed $confirmed = optional_param('confirmed', false, PARAM_BOOL); // a message to display, typically a previous action's result @@ -121,6 +123,27 @@ if (!empty($shareform)) { } } +// delete the form definition +if (!empty($deleteform)) { + $controller = $manager->get_controller($method); + $definition = $controller->get_definition(); + if (!$confirmed) { + // let the user confirm they understand the consequences (also known as WTF-effect) + echo $output->header(); + echo $output->confirm(markdown_to_html(get_string('manageactiondeleteconfirm', 'core_grading', array( + 'formname' => s($definition->name), + 'component' => $manager->get_component_title(), + 'area' => $manager->get_area_title()))), + new moodle_url($PAGE->url, array('deleteform' => $deleteform, 'confirmed' => 1)), $PAGE->url); + echo $output->footer(); + die(); + } else { + require_sesskey(); + $controller->delete_definition(); + redirect(new moodle_url($PAGE->url, array('message' => get_string('manageactiondeletedone', 'core_grading')))); + } +} + echo $output->header(); if (!empty($message)) { @@ -142,7 +165,7 @@ if (!empty($method)) { $definition = $controller->get_definition(); echo $output->management_action_icon($controller->get_editor_url($returnurl), get_string('manageactionedit', 'core_grading'), 'b/document-edit'); - echo $output->management_action_icon($PAGE->url, + echo $output->management_action_icon(new moodle_url($PAGE->url, array('deleteform' => $definition->id)), get_string('manageactiondelete', 'core_grading'), 'b/edit-delete'); if (has_capability('moodle/grade:sharegradingforms', get_system_context())) { echo $output->management_action_icon(new moodle_url($PAGE->url, array('shareform' => $definition->id)), diff --git a/grade/grading/yui/manage/manage.js b/grade/grading/yui/manage/manage.js index 2d1ecfbed4c..935c3834a44 100644 --- a/grade/grading/yui/manage/manage.js +++ b/grade/grading/yui/manage/manage.js @@ -23,8 +23,8 @@ YUI.add('moodle-core_grading-manage', function(Y) { var box = e.currentTarget; var anim = new Y.Anim({ node: box, - from: { opacity: 1 }, - to: { opacity: 0 }, + duration: 1, + to: { opacity: 0, height: 0 }, }); anim.run(); anim.on('end', function() { diff --git a/lang/en/grading.php b/lang/en/grading.php index 4e5479a0969..12257dfeeac 100644 --- a/lang/en/grading.php +++ b/lang/en/grading.php @@ -42,6 +42,14 @@ $string['gradingmethodnone'] = 'Simple direct grading'; $string['gradingmethods'] = 'Grading methods'; $string['manageactionclone'] = 'Create new grading form from a template'; $string['manageactiondelete'] = 'Remove the currently defined form'; +$string['manageactiondeleteconfirm'] = 'You are going to remove the grading form \'{$a->formname}\' and all the associated information from \'{$a->component} ({$a->area})\'. Please make sure you understand the following consequences: + +* There is no way to undo this operation. +* You can switch to another grading method including the \'Simple direct grading\' without removing this form. +* All the information about how the grading forms are filled will be lost. +* The calculated result grades stored in the gradebook will not be affected. However the explanation of how they were calculated will not be available. +* This operation does not affect eventual copies of this form in other activities.'; +$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';