From a9fdf770d0a6aeb9065ae3de3a40911e46bd8d57 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Sat, 2 Sep 2017 14:01:21 +0800 Subject: [PATCH] #119 Add ability to delete issued certificates --- classes/certificate.php | 2 +- classes/report_table.php | 26 ++++++++++++++ lang/en/customcert.php | 1 + report.php | 36 ++++++++++++++++++++ tests/behat/view_issued_certificates.feature | 26 ++++++++++++++ 5 files changed, 90 insertions(+), 1 deletion(-) diff --git a/classes/certificate.php b/classes/certificate.php index 846e19f..ab2609b 100644 --- a/classes/certificate.php +++ b/classes/certificate.php @@ -265,7 +265,7 @@ class certificate { // Return the issues. $ufields = \user_picture::fields('u'); - $sql = "SELECT $ufields, ci.code, ci.timecreated + $sql = "SELECT $ufields, ci.id as issueid, ci.code, ci.timecreated FROM {user} u INNER JOIN {customcert_issues} ci ON u.id = ci.userid diff --git a/classes/report_table.php b/classes/report_table.php index 92b577e..e34be5b 100644 --- a/classes/report_table.php +++ b/classes/report_table.php @@ -86,6 +86,11 @@ class report_table extends \table_sql { $headers[] = get_string('file'); } + if (has_capability('mod/customcert:manage', \context_module::instance($cm->id))) { + $columns[] = 'actions'; + $headers[] = ''; + } + $this->define_columns($columns); $this->define_headers($headers); $this->collapsible(false); @@ -149,6 +154,27 @@ class report_table extends \table_sql { return $OUTPUT->action_link($link, '', null, null, $icon); } + /** + * Generate the actions column. + * + * @param \stdClass $user + * @return string + */ + public function col_actions($user) { + global $OUTPUT; + + $icon = new \pix_icon('i/delete', get_string('delete')); + $link = new \moodle_url('/mod/customcert/report.php', + [ + 'id' => $this->cm->id, + 'deleteissue' => $user->issueid, + 'sesskey' => sesskey() + ] + ); + + return $OUTPUT->action_icon($link, $icon, null, ['class' => 'action-icon delete-icon']); + } + /** * Query the reader. * diff --git a/lang/en/customcert.php b/lang/en/customcert.php index d2a5a40..47f6b44 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -43,6 +43,7 @@ $string['deletecertpage'] = 'Delete page'; $string['deleteconfirm'] = 'Delete confirmation'; $string['deleteelement'] = 'Delete element'; $string['deleteelementconfirm'] = 'Are you sure you want to delete this element?'; +$string['deleteissueconfirm'] = 'Are you sure you want to delete this certificate issue?'; $string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?'; $string['deletetemplateconfirm'] = 'Are you sure you want to delete this certificate template?'; $string['description'] = 'Description'; diff --git a/report.php b/report.php index 9728be6..4f2e94a 100644 --- a/report.php +++ b/report.php @@ -27,6 +27,8 @@ require_once('../../config.php'); $id = required_param('id', PARAM_INT); $download = optional_param('download', null, PARAM_ALPHA); $downloadcert = optional_param('downloadcert', '', PARAM_BOOL); +$deleteissue = optional_param('deleteissue', 0, PARAM_INT); +$confirm = optional_param('confirm', 0, PARAM_BOOL); if ($downloadcert) { $userid = required_param('userid', PARAM_INT); } @@ -46,6 +48,40 @@ require_login($course, false, $cm); $context = context_module::instance($cm->id); require_capability('mod/customcert:viewreport', $context); +if ($deleteissue && confirm_sesskey()) { + require_capability('mod/customcert:manage', $context); + + if (!$confirm) { + $nourl = new moodle_url('/mod/customcert/report.php', ['id' => $id]); + $yesurl = new moodle_url('/mod/customcert/report.php', + [ + 'id' => $id, + 'deleteissue' => $deleteissue, + 'confirm' => 1, + 'sesskey' => sesskey() + ] + ); + + // Show a confirmation page. + $strheading = get_string('deleteconfirm', 'customcert'); + $PAGE->navbar->add($strheading); + $PAGE->set_title($strheading); + $PAGE->set_url($url); + $message = get_string('deleteissueconfirm', 'customcert'); + echo $OUTPUT->header(); + echo $OUTPUT->heading($strheading); + echo $OUTPUT->confirm($message, $yesurl, $nourl); + echo $OUTPUT->footer(); + exit(); + } + + // Delete the issue. + $DB->delete_records('customcert_issues', array('id' => $deleteissue, 'customcertid' => $customcert->id)); + + // Redirect back to the manage templates page. + redirect(new moodle_url('/mod/customcert/report.php', array('id' => $id))); +} + // Check if we requested to download another user's certificate. if ($downloadcert) { $template = $DB->get_record('customcert_templates', array('id' => $customcert->templateid), '*', MUST_EXIST); diff --git a/tests/behat/view_issued_certificates.feature b/tests/behat/view_issued_certificates.feature index af253e7..5e952e7 100644 --- a/tests/behat/view_issued_certificates.feature +++ b/tests/behat/view_issued_certificates.feature @@ -39,3 +39,29 @@ Feature: Being able to view the certificates that have been issued And I follow "View 2 issued certificates" And I should see "Student 1" And I should see "Student 2" + + Scenario: Delete an issued certificate + And I log in as "student1" + And I am on "Course 1" course homepage + And I follow "Custom certificate 1" + And I press "Download certificate" + And I log out + And I log in as "student2" + And I am on "Course 1" course homepage + And I follow "Custom certificate 1" + And I press "Download certificate" + And I log out + And I log in as "teacher1" + And I am on "Course 1" course homepage + And I follow "Custom certificate 1" + And I follow "View 2 issued certificates" + And I should see "Student 1" + And I should see "Student 2" + And I click on ".delete-icon" "css_element" in the "Student 2" "table_row" + And I press "Cancel" + And I should see "Student 1" + And I should see "Student 2" + And I click on ".delete-icon" "css_element" in the "Student 2" "table_row" + And I press "Continue" + And I should see "Student 1" + And I should not see "Student 2"