Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lang/en/certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
$string['gradepercent'] = 'Percentage Grade';
$string['gradepoints'] = 'Points Grade';
$string['imagetype'] = 'Image Type';
$string['protected'] = 'Limit to one course';
$string['courseshortname'] = 'Course shortname';
$string['novalidcourseshortname'] = 'Course shortname is not valid. There is not a course with this shortname. Please check.';
$string['missingcourseshortname'] = 'Please specify the course shortname.';
$string['incompletemessage'] = 'In order to download your certificate, you must first complete all required '.'activities. Please return to the course to complete your coursework.';
$string['intro'] = 'Introduction';
$string['issueoptions'] = 'Issue Options';
Expand Down Expand Up @@ -201,4 +205,4 @@
$string['viewcertificateviews'] = 'View {$a} issued certificates';
$string['viewed'] = 'You received this certificate on:';
$string['viewtranscript'] = 'View Certificates';
$string['watermark'] = 'Watermark';
$string['watermark'] = 'Watermark';
37 changes: 37 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,11 +1043,14 @@ function certificate_get_images($type) {
$uploadpath = "$CFG->dataroot/mod/certificate/pix/watermarks";
break;
}

// If valid path
if (!empty($path)) {
$options = array();
$options += certificate_scan_image_dir($path);
$options += certificate_scan_image_dir($uploadpath);
// add course protected files
$options += certificate_scan_image_protected_dir($uploadpath);

// Sort images
ksort($options);
Expand Down Expand Up @@ -1513,3 +1516,37 @@ function certificate_scan_image_dir($path) {
}
return $options;
}

/**
* Scans the protected directory "$path/course/$COURSE->shortname" for valid images.
* The images in protected directory are only visible from the course with the same shortname.
*
* @param string the path
* @return array
*/
function certificate_scan_image_protected_dir($path) {
global $COURSE;

$protecteddir = "/course/$COURSE->shortname/";
$protectedpath = $path.$protecteddir;
// Array to store the images
$options = array();

// Start to scan directory
if (is_dir($protectedpath)) {
if ($handle = opendir($protectedpath)) {
while (false !== ($file = readdir($handle))) {
if (strpos($file, '.png', 1) || strpos($file, '.jpg', 1) ) {
$i = strpos($file, '.');
if ($i > 1) {
// Set the name
$options[$protecteddir.$file] = substr($file, 0, $i);
}
}
}
closedir($handle);
}
}

return $options;
}
8 changes: 6 additions & 2 deletions upload_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@
if ($upload_form->is_cancelled()) {
redirect(new moodle_url('/admin/settings.php?section=modsettingcertificate'));
} else if ($data = $upload_form->get_data()) {
// Ensure the directory for storing is created
$uploaddir = "mod/certificate/pix/$data->imagetype";
$filename = $upload_form->get_new_filename('certificateimage');
$uploaddir = "mod/certificate/pix/$data->imagetype";
// put the file inside a protected directory if needed
if(isset($data->protected)) {
$uploaddir .= "/course/$data->courseshortname/";
}
// Ensure the directory for storing is created
make_upload_directory($uploaddir);
$destination = $CFG->dataroot . '/' . $uploaddir . '/' . $filename;
if (!$upload_form->save_file('certificateimage', $destination, true)) {
Expand Down
20 changes: 18 additions & 2 deletions upload_image_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function definition() {
);

$mform->addElement('select', 'imagetype', get_string('imagetype', 'certificate'), $imagetypes);
$mform->addElement('checkbox', 'protected', get_string('protected', 'certificate'));
$mform->addElement('text', 'courseshortname', get_string('courseshortname', 'certificate'));

$mform->addElement('filepicker', 'certificateimage', '');
$mform->addRule('certificateimage', null, 'required', null, 'client');
Expand Down Expand Up @@ -79,6 +81,20 @@ function validation($data, $files) {
$errors['certificateimage'] = get_string('nofileselected', 'certificate');
}

return $errors;
// check course shortname data
if(isset($data['protected'])) {
// check if shortname is set and not empty
if(!isset($data['courseshortname']) or empty($data['courseshortname'])) {
$errors['courseshortname'] = get_string('missingcourseshortname', 'certificate');
} else { // check if shortname is a valid one
global $DB;
$course = $DB->get_record('course', array('shortname'=>$data['courseshortname']));
if($course == false){
$errors['courseshortname'] = get_string('novalidcourseshortname', 'certificate');
}
}
}

return $errors;
}
}
}