MDL-61864 tool_policy: function can_delete_version
review TODOs in code, make sure they are either removed or tracker issues are created
This commit is contained in:
@@ -534,6 +534,17 @@ class api {
|
||||
return static::form_policydoc_update_new($formdata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the current version be deleted
|
||||
*
|
||||
* @param stdClass $version object describing version, contains fields policyid, id, status, archived, audience, ...
|
||||
*/
|
||||
public static function can_delete_version($version) {
|
||||
// TODO MDL-61900 allow to delete not only draft versions.
|
||||
return has_capability('tool/policy:managedocs', context_system::instance()) &&
|
||||
$version->status == policy_version::STATUS_DRAFT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the given version (if it is a draft). Also delete policy if this is the only version.
|
||||
*
|
||||
@@ -543,16 +554,14 @@ class api {
|
||||
global $DB;
|
||||
|
||||
$version = static::get_policy_version($versionid);
|
||||
$policy = static::list_policies([$version->policyid])[0];
|
||||
if ($version->archived || $policy->currentversionid == $version->id) {
|
||||
// Can not delete archived or current version.
|
||||
// TODO we could delete guest only versions potentially or versions without acceptances.
|
||||
if (!self::can_delete_version($version)) {
|
||||
// Current version can not be deleted.
|
||||
return;
|
||||
}
|
||||
|
||||
$DB->delete_records('tool_policy_versions', ['id' => $versionid]);
|
||||
|
||||
if (!$policy->archivedversions && !$policy->currentversion && count($policy->draftversions) == 1) {
|
||||
if (!$DB->record_exists('tool_policy_versions', ['policyid' => $version->policyid])) {
|
||||
// This is a single version in a policy. Delete the policy.
|
||||
$DB->delete_records('tool_policy', ['id' => $version->policyid]);
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ class page_managedocs_list implements renderable, templatable {
|
||||
|
||||
foreach ($policy->archivedversions as $i => $version) {
|
||||
$data->versions[] = $this->export_version_for_template($output, $policy, $version,
|
||||
policy_version::STATUS_ARCHIVED, false, false, false);
|
||||
false, false, false);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
@@ -107,27 +107,27 @@ class page_managedocs_list implements renderable, templatable {
|
||||
// There is no current and no draft versions, display the first archived version.
|
||||
$firstpolicy = array_shift($policy->archivedversions);
|
||||
$data->versions[] = $this->export_version_for_template($output, $policy, $firstpolicy,
|
||||
policy_version::STATUS_ARCHIVED, false, $i > 0, $i < count($policies) - 1);
|
||||
false, $i > 0, $i < count($policies) - 1);
|
||||
}
|
||||
|
||||
if (!empty($policy->currentversion)) {
|
||||
|
||||
// Current version of the policy.
|
||||
$data->versions[] = $this->export_version_for_template($output, $policy, $policy->currentversion,
|
||||
policy_version::STATUS_ACTIVE, false, $i > 0, $i < count($policies) - 1);
|
||||
false, $i > 0, $i < count($policies) - 1);
|
||||
|
||||
} else if ($policy->draftversions) {
|
||||
|
||||
// There is no current version, display the first draft version as the current.
|
||||
$firstpolicy = array_shift($policy->draftversions);
|
||||
$data->versions[] = $this->export_version_for_template($output, $policy, $firstpolicy,
|
||||
policy_version::STATUS_DRAFT, false, $i > 0, $i < count($policies) - 1);
|
||||
false, $i > 0, $i < count($policies) - 1);
|
||||
}
|
||||
|
||||
foreach ($policy->draftversions as $draft) {
|
||||
// Show all [other] draft policies indented.
|
||||
$data->versions[] = $this->export_version_for_template($output, $policy, $draft,
|
||||
policy_version::STATUS_DRAFT, true, false, false);
|
||||
true, false, false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -141,14 +141,14 @@ class page_managedocs_list implements renderable, templatable {
|
||||
* @param \renderer_base $output
|
||||
* @param \stdClass $policy
|
||||
* @param \stdClass $version
|
||||
* @param int $status
|
||||
* @param bool $isindented display indented (normally drafts of the current version)
|
||||
* @param bool $moveup can move up
|
||||
* @param bool $movedown can move down
|
||||
* @return \stdClass
|
||||
*/
|
||||
protected function export_version_for_template($output, $policy, $version, $status, $isindented, $moveup, $movedown) {
|
||||
protected function export_version_for_template($output, $policy, $version, $isindented, $moveup, $movedown) {
|
||||
|
||||
$status = $version->status;
|
||||
$version->statustext = get_string('status' . $status, 'tool_policy');
|
||||
|
||||
if ($status == policy_version::STATUS_ACTIVE) {
|
||||
@@ -226,8 +226,7 @@ class page_managedocs_list implements renderable, templatable {
|
||||
['data-action' => 'makecurrent']
|
||||
));
|
||||
}
|
||||
if ($status == policy_version::STATUS_DRAFT) {
|
||||
// TODO can we also delete non-draft version that is guest only or has no acceptances?
|
||||
if (api::can_delete_version($version)) {
|
||||
$actionmenu->add(new action_menu_link(
|
||||
new moodle_url($editbaseurl, ['delete' => $version->id]),
|
||||
null,
|
||||
|
||||
@@ -99,8 +99,7 @@ class page_viewdoc implements renderable, templatable {
|
||||
}
|
||||
|
||||
if (empty($this->policy)) {
|
||||
// TODO Make this nicer error message.
|
||||
throw new \moodle_exception('err_no_active_version', 'tool_policy');
|
||||
throw new \moodle_exception('errorpolicyversionnotfound', 'tool_policy');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,6 @@ class user_agreement implements \templatable, \renderable {
|
||||
$linkparams['returnurl'] = $this->pageurl->out_as_local_url(false);
|
||||
$link = new \moodle_url('/admin/tool/policy/accept.php', $linkparams);
|
||||
$data['acceptlink'] = $link->out(false);
|
||||
$data['acceptmodaldata'] = $link->get_query_string(false); // TODO not needed?
|
||||
}
|
||||
$data['singleversion'] = count($this->versions) == 1;
|
||||
if ($data['singleversion']) {
|
||||
|
||||
Reference in New Issue
Block a user