Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Mar 11, 2024
1 parent 2bb5031 commit 07834c2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
53 changes: 47 additions & 6 deletions classes/certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,12 @@ public static function get_course_time(int $courseid, int $userid = 0): int {
/**
* Download all certificate issues.
*
* @param \stdClass $template
* @param template $template
* @param array $issues
* @return void
* @throws \moodle_exception
*/
public static function download_all_for_instance(\stdClass $template, array $issues): void {
$template = new \mod_customcert\template($template);

public static function download_issues_with_same_template(\mod_customcert\template $template, array $issues): void {
$zipdir = make_request_directory();
if (!$zipdir) {
return;
Expand All @@ -286,10 +285,52 @@ public static function download_all_for_instance(\stdClass $template, array $iss
exit();
}

public static function download_all_for_site() {
public static function download_all_for_site(): void {
global $DB;

$issues = $DB->get_records('customcert_issues');
list($namefields, $nameparams) = \core_user\fields::get_sql_fullname();
$sql = "SELECT ci.*, $namefields as fullname, ct.id as templateid, ct.name as templatename, ct.contextid
FROM {customcert_issues} ci
JOIN {user} u
ON ci.userid = u.id
JOIN {customcert} c
ON ci.customcertid = c.id
JOIN {customcert_templates} ct
ON c.templateid = ct.id";
if ($issues = $DB->get_records_sql($sql, $nameparams)) {
print_object($issues);
exit();
$zipdir = make_request_directory();
if (!$zipdir) {
return;
}

$zipfilenameprefix = userdate(time(), self::ZIP_FILE_NAME_DOWNLOAD_ALL_CERTIFICATES_DATE_FORMAT);
$zipfilename = $zipfilenameprefix . "_" . self::ZIP_FILE_NAME_DOWNLOAD_ALL_CERTIFICATES;
$zipfullpath = $zipdir . DIRECTORY_SEPARATOR . $zipfilename;

$ziparchive = new \zip_archive();

foreach ($issues as $issue) {
$template = new \stdClass();
$template->id = $issue->templateid;
$template->name = $issue->templatename;
$template->contextid = $issue->contextid;
$template = new \mod_customcert\template($template);

$userfullname = str_replace(' ', '_', mb_strtolower($issue->fullname, FORMAT_PLAIN));
print_object($userfullname);
print_object($issue);
print_object('here is this one');
exit();
$pdfname = $userfullname . DIRECTORY_SEPARATOR . 'certificate.pdf';
$filecontents = $template->generate_pdf(false, $issue->userid, true);
$ziparchive->add_file_from_string($pdfname, $filecontents);
}

send_file($zipfullpath, $zipfilename);
exit();
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion download_all_certificates.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require('../../config.php');
require_once('../../config.php');
require_once($CFG->libdir . '/filestorage/zip_archive.php');

require_login();

Expand Down
2 changes: 1 addition & 1 deletion lang/en/customcert.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$string['customcertsettings'] = 'Custom certificate settings';
$string['downloadallissuedcertificates'] = 'Download all issued certificates';
$string['downloadallsitecertificates'] = 'Download all site certificates';
$string['downloadallsitecertificatesdesc'] = 'This will download all the certificates on the site and place them in a zip file.';
$string['downloadallsitecertificatesdesc'] = 'This will download all the certificates on the site in a zip file.';
$string['deletecertpage'] = 'Delete page';
$string['deleteconfirm'] = 'Delete confirmation';
$string['deleteelement'] = 'Delete element';
Expand Down
3 changes: 2 additions & 1 deletion view.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@

// Check if we are downloading all certificates.
if ($downloadall && $canmanage && confirm_sesskey()) {
$template = new \mod_customcert\template($template);
$issues = \mod_customcert\certificate::get_issues($customcert->id, $groupmode, $cm, 0, 0);

// The button is not visible if there are no issues, so in this case just redirect back to this page.
if (empty($issues)) {
redirect(new moodle_url('/mod/customcert/view.php', ['id' => $id]));
}

\mod_customcert\certificate::download_all_for_instance($template, $issues);
\mod_customcert\certificate::download_issues_with_same_template($template, $issues);
exit();
}

Expand Down

0 comments on commit 07834c2

Please sign in to comment.