Skip to content

Commit d4828c0

Browse files
committed
add 'download all certificates' link to nav menus
1 parent c35d84f commit d4828c0

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

lib.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,27 @@ function mod_customcert_output_fragment_editelement($args) {
312312
return $form->render();
313313
}
314314

315+
/**
316+
* This function extends the course navigation block for the site.
317+
*
318+
* @param \navigation_node $parentnode
319+
* @param \stdClass $course
320+
* @param \context_course $context
321+
*/
322+
function customcert_extend_navigation_course(\navigation_node $parentnode, \stdClass $course, \context_course $context) {
323+
global $PAGE;
324+
325+
$addnode = $context->contextlevel === 50;
326+
$addnode = $addnode && !($context->instanceid === SITEID);
327+
$addnode = $addnode && has_capability('mod/customcert:viewallcertificates', $context);
328+
$isCourseNav = is_null($PAGE->cm->instance);
329+
if ($addnode && $isCourseNav) {
330+
if ($node = build_downloadall_node($isCourseNav, $course, $context)) {
331+
$parentnode->add_node($node);
332+
}
333+
}
334+
}
335+
315336
/**
316337
* This function extends the settings navigation block for the site.
317338
*
@@ -343,6 +364,12 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na
343364
$customcertnode->add_node($node, $beforekey);
344365
}
345366

367+
if (has_capability('mod/customcert:viewallcertificates', $PAGE->cm->context)) {
368+
if ($node = build_downloadall_node(false, $PAGE->cm->get_course(), null)) {
369+
$customcertnode->add_node($node, $beforekey);
370+
}
371+
}
372+
346373
if (has_capability('mod/customcert:verifycertificate', $settings->get_page()->cm->context)) {
347374
$node = navigation_node::create(get_string('verifycertificate', 'customcert'),
348375
new moodle_url('/mod/customcert/verify_certificate.php', array('contextid' => $settings->get_page()->cm->context->id)),
@@ -354,6 +381,50 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na
354381
return $customcertnode->trim_if_empty();
355382
}
356383

384+
/**
385+
* Build the navigation node for the 'Download certificates' item.
386+
*
387+
* @param boolean $isCourseNav
388+
* @param \stdClass $course
389+
* @param \course_context $context
390+
* @return \navigation_node|null null if there are no certifcates available for the download
391+
*/
392+
function build_downloadall_node(bool $isCourseNav, stdClass $course, \context_course $context = null) {
393+
394+
global $DB, $PAGE;
395+
396+
if (!$isCourseNav) {
397+
$courseContext = \context_course::instance($course->id);
398+
//Check if there is available certs
399+
$certs = $DB->get_records('customcert', ['id' => $PAGE->cm->instance]);
400+
$users = $DB->get_records('role_assignments', ['contextid' => $courseContext->id]);
401+
$urlparams = ['customcertid' => $PAGE->cm->instance];
402+
} else {
403+
$certs = $DB->get_records('customcert', ['course' => $context->instanceid]);
404+
$users = $DB->get_records('role_assignments', ['contextid' => $context->id]);
405+
$urlparams = ['courseid' => $course->id];
406+
}
407+
$availablecerts = false;
408+
foreach ($certs as $certid => $cert_fields) {
409+
foreach ($users as $userid => $user_fields) {
410+
if (!$DB->get_record('customcert_issues', ['userid' => $user_fields->userid, 'customcertid' => $certid])) {
411+
continue;
412+
}
413+
$availablecerts = true;
414+
break;
415+
}
416+
if ($availablecerts) break;
417+
}
418+
if ($availablecerts) {
419+
$node = \navigation_node::create(get_string('bulkdownloadlink', 'mod_customcert'),
420+
new \moodle_url('/mod/customcert/downloadcerts.php', $urlparams),
421+
\navigation_node::TYPE_SETTING, null, 'mod_customcert_downloadcerts',
422+
new \pix_icon('a/download_all', 'certificates'));
423+
return $node;
424+
}
425+
return null;
426+
}
427+
357428
/**
358429
* Add nodes to myprofile page.
359430
*
@@ -420,7 +491,8 @@ function mod_customcert_inplace_editable($itemtype, $itemid, $newvalue) {
420491
*/
421492
function mod_customcert_get_fontawesome_icon_map() {
422493
return [
423-
'mod_customcert:download' => 'fa-download'
494+
'mod_customcert:deletelocalcopy' => 'fa-minus-square',
495+
'mod_customcert:download' => 'fa-download',
424496
];
425497
}
426498

0 commit comments

Comments
 (0)