-
Notifications
You must be signed in to change notification settings - Fork 181
Feature/keeplocalcopy #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HobieCat
wants to merge
23
commits into
mdjnelson:MOODLE_404_STABLE
Choose a base branch
from
HobieCat:feature/keeplocalcopy
base: MOODLE_404_STABLE
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a1dc566
Updated subplugins.json file to match latest format (#688)
Thaveesha222 2cdc66c
Updated GHA for Moodle 5.0
mdjnelson b41d838
Updated CHANGES.md
mdjnelson 13565ec
Fix code style complaints
mdjnelson 1479075
Fixed null array offset warning in QR code element on PHP 8.4 in GHA
mdjnelson 8484023
Bumped version
mdjnelson c7f98e3
Fix notifications when the instance of the activity is on site home #693
andrewhancox 3fe710b
Added customisable filename options for certificates (#684)
Raza403 00c6da5
Fixed version bump issue (#684)
mdjnelson 5f982ff
Added newly introduced fields to install.xml (#684)
mdjnelson cf542a6
Make language string consistent with others
mdjnelson 733428c
Removed unused language strings (#684)
mdjnelson 349a4b4
Removed unnecessary code for filling in form inputs (#684)
mdjnelson 06af2ac
Fixed issue with language strings not matching logic for placeholders…
mdjnelson 8305d26
Added missing fields to backups (#705)
mdjnelson 1472921
Added fields to backup file (#684)
mdjnelson ac2fdda
Removed hack for Oracle databases as they are no longer supported (#701)
mdjnelson 1d74bcb
Add setting to add a button to return to course (#655)
gbarat87 df719c4
Make the setting description more descriptive (#655)
mdjnelson 9b462e5
Removed incorrect passing of boolean value no longer used (#655)
mdjnelson 14e91a8
Do not refer to the course page as a menu (#655)
mdjnelson 0637ca1
Implement new feature keeplocalcopy:
HobieCat ab5e388
rename methods, add return types and remove downloadcerts custom feature
HobieCat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Class represents a local file of an issued certificate. | ||
* | ||
* @package mod_customcert | ||
* @copyright 2023 Giorgio Consorti <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace mod_customcert; | ||
|
||
use file_exception; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
/** | ||
* Class represents a local file of an issued certificate. | ||
* | ||
* @package mod_customcert | ||
* @copyright 2023 Giorgio Consorti <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class localfile { | ||
|
||
/** | ||
* @var \mod_customcert\template the template representing the content of the file. | ||
*/ | ||
protected $template; | ||
|
||
/** | ||
* The component name for the file storage. | ||
*/ | ||
private const component = 'mod_customcert'; | ||
|
||
/** | ||
* The filearea name for the file storage. | ||
*/ | ||
private const filearea = 'customcert_issues'; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param \mod_customcert\template $template | ||
*/ | ||
public function __construct(\mod_customcert\template $template) { | ||
$this->template = $template; | ||
} | ||
|
||
/** | ||
* Save the PDF to the file storage. | ||
* | ||
* @param string $pdfcontent string content of the pdf | ||
* @param int|null $userid the id of the user whose certificate we want to save | ||
* | ||
* @return \stored_file|bool the stored_file object on success, false on error | ||
*/ | ||
public function save_pdf(string $pdfcontent, ?int $userid = null): \stored_file|bool { | ||
global $CFG, $USER; | ||
require_once($CFG->libdir . '/filelib.php'); | ||
|
||
if (empty($userid)) { | ||
$userid = $USER->id; | ||
} | ||
|
||
try { | ||
$file = $this->get_pdf($userid); | ||
if (!$file) { | ||
// Create file containing the pdf | ||
$fs = get_file_storage(); | ||
$file = $fs->create_file_from_string($this->buildFileInfo($userid), $pdfcontent); | ||
} | ||
return $file; | ||
} catch (file_exception $e) { | ||
// maybe log the exception | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Get the PDF from the file storage. | ||
* | ||
* @param int|null $userid the id of the user whose certificate we want to get | ||
* | ||
* @return \stored_file|bool the stored_file object on success, false on error | ||
*/ | ||
public function get_pdf(?int $userid = null): \stored_file|bool { | ||
global $CFG, $USER; | ||
require_once($CFG->libdir . '/filelib.php'); | ||
|
||
if (empty($userid)) { | ||
$userid = $USER->id; | ||
} | ||
|
||
$fileinfo = $this->buildFileInfo($userid); | ||
$fs = get_file_storage(); | ||
return $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], | ||
$fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']); | ||
} | ||
|
||
/** | ||
* Delete the PDF from the file storage. | ||
* | ||
* @param int|null $userid the id of the user whose certificate we want to get | ||
* | ||
* @return bool true on success | ||
*/ | ||
public function delete_pdf(?int $userid = null): bool { | ||
global $USER; | ||
|
||
if (empty($userid)) { | ||
$userid = $USER->id; | ||
} | ||
|
||
try { | ||
$file = $this->get_pdf($userid); | ||
if ($file) { | ||
return $file->delete(); | ||
} | ||
return false; | ||
} catch (file_exception $e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Send the PDF to the browser or return it as a string. | ||
* | ||
* @param int $userid the id of the user whose certificate we want to view | ||
* @param string $deliveryoption the delivery option of the customcert | ||
* @param bool $return Do we want to return the contents of the PDF? | ||
* | ||
* @return string|null Can return the PDF in string format if specified. | ||
*/ | ||
public function send_pdf(?int $userid = NULL, string $deliveryoption = certificate::DELIVERY_OPTION_DOWNLOAD, bool $return = false): string|null { | ||
global $USER; | ||
|
||
if (empty($userid)) { | ||
$userid = $USER->id; | ||
} | ||
|
||
$file = $this->get_pdf($userid); | ||
if ($file) { | ||
if ($return) { | ||
return $file->get_content(); | ||
} else { | ||
// send the file to the browser | ||
send_stored_file( | ||
$file, | ||
0, | ||
0, | ||
$deliveryoption == certificate::DELIVERY_OPTION_DOWNLOAD, | ||
['filename' => $file->get_filename()] | ||
); | ||
die(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Check if a pdf exists in the file storage area. | ||
* | ||
* @param \stdClass $cm the course module | ||
* @param int|null $userid the id of the user whose PDF we want to check | ||
* @param int|null $templateid the template id of the customcert we want to check | ||
* | ||
* @return \stored_file|false the stored_file object on success, false on error | ||
*/ | ||
public static function does_pdf_exist($cm, ?int $userid = null, ?int $templateid = null): \stored_file|bool { | ||
|
||
$fileinfo = self::build_file_info($cm, $userid, $templateid); | ||
$fs = get_file_storage(); | ||
return $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], | ||
$fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']); | ||
} | ||
|
||
/** | ||
* Build the fileinfo array needed by the file storage. | ||
* | ||
* @param int|null $userid the id of the user whose fileinfo array we want to generate | ||
* | ||
* @return array the fileinfo array | ||
*/ | ||
protected function buildFileInfo(?int $userid = null): array { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No space needed here. |
||
return self::build_file_info($this->template->get_cm(), $userid, $this->template->get_id()); | ||
} | ||
|
||
/** | ||
* Build the fileinfo array needed by the file storage, static version. | ||
* | ||
* @param \stdClass $cm the course module | ||
* @param int|null $userid the id of the user whose fileinfo array we want to generate | ||
* @param int|null $templateid the template id of the customcert of the array we want to generate | ||
* | ||
* @return array the fileinfo array | ||
*/ | ||
private static function build_file_info ($cm, ?int $userid = null, ?int $templateid = null): array { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No space needed here. |
||
/** @var \moodle_database $DB */ | ||
global $DB, $USER; | ||
|
||
if (empty($userid)) { | ||
$userid = $USER->id; | ||
} | ||
|
||
if (empty($templateid)) { | ||
$customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST); | ||
$templateid = $customcert->templateid; | ||
} | ||
|
||
$course = $DB->get_record('course', ['id' => $cm->course]); | ||
$context = $DB->get_record('context', ['contextlevel' => '50', 'instanceid' => $course->id]); | ||
$user_info = $DB->get_record('user', ['id' => $userid]); | ||
|
||
return [ | ||
'contextid' => $context->id, | ||
'component' => self::component, | ||
'filearea' => self::filearea, | ||
'itemid' => $templateid, | ||
'userid' => $USER->id, | ||
'author' => fullname($USER), | ||
'filepath' => '/' . $course->id . '/', | ||
'filename' => self::buildFileName($user_info->username, $templateid, $course->shortname), | ||
]; | ||
} | ||
|
||
/** | ||
* Build the PDF filename. | ||
* | ||
* @param string $username | ||
* @param string $templateid | ||
* @param string $courseShortname | ||
* @return string the PDF file name | ||
*/ | ||
public static function buildFileName($username, $templateid, $courseShortname): string { | ||
return $username . '_cert-' . $templateid . '_course-' . $courseShortname . '.pdf'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the type here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't get this, how do you need the code to look like?