Compare commits

...

116 commits

Author SHA1 Message Date
Roberto Pinna
64a184298d #192 Fixed report fields for download 2018-05-26 18:42:16 +08:00
Mark Nelson
ce99b222a7 Fixed PHPDoc 2018-05-26 18:42:12 +08:00
Mark Nelson
026f8fbb84 Added helper function to issue certificates 2018-05-26 18:42:06 +08:00
Mark Nelson
98548a07d2 #148 Increase size limit on fonts 2018-05-26 18:42:00 +08:00
Mark Nelson
6d441b37b4 Bump version 2018-05-17 15:08:39 +08:00
Mark Nelson
21c371ca67 Updated CHANGES.md 2018-05-17 15:08:19 +08:00
Eugene Venter
df7ada60a1 #173 Fix race condition on certificate issues in scheduled task 2018-05-15 12:18:13 +08:00
Mark Nelson
a1613478e2 #169 Ensure we backup the 'verifyany' setting 2018-05-15 12:18:09 +08:00
Mark Nelson
e9f0410b3e #166 Fixed encoding content links 2018-05-15 12:18:02 +08:00
Mark Nelson
87b8a8ba92 Version bump 2018-01-23 08:14:02 +08:00
Mark Nelson
b53d4f5fab Updated travis 2018-01-23 08:14:02 +08:00
Mark Nelson
3d488bcbc4 Updated CHANGES.md 2018-01-23 08:14:02 +08:00
Mark Nelson
4a4fa18c28 #158 Fixed fatal error in PHP < 5.5 2018-01-23 08:14:02 +08:00
Mark Nelson
8fe6c32d9a Bumped version 2017-12-11 16:57:34 +08:00
Mark Nelson
e7497a50e0 Fixed failing Behat tests 2017-12-11 16:36:39 +08:00
Mark Nelson
f3238fcd32 Coding style fixes
Bumping version and tagging so I can
easily upload changes to plugins DB.
2017-12-04 21:05:43 +08:00
Mark Nelson
ae0e587eb4 Updated CHANGES.md 2017-12-03 14:51:19 +08:00
Mark Nelson
e4b5ce7293 #142 Add setting to allow site-wide verification of certificates 2017-12-03 14:49:23 +08:00
Mark Nelson
bd9a59631f #98 Add ability to display the date and grade for all grade items 2017-12-03 14:48:44 +08:00
Mark Nelson
572025716b #126 Use the course full name in emails 2017-12-03 14:48:40 +08:00
Mark Nelson
b189236b6e #140 Add text describing the my certificates list 2017-12-03 14:48:35 +08:00
Mark Nelson
bd160b420a Fixed copyright year 2017-12-03 14:48:30 +08:00
Mark Nelson
ff39fd2cee #98 Added PHPUnit tests for element helper 2017-12-03 14:47:49 +08:00
Mark Nelson
4f0a94af99 #98 Moved helper functions used by multiple elements
Also did the following -

1. Changed get_grade_items() so it takes a course parameter.
2. Renamed get_grade() and get_mod_grade() and changed functionality
   slightly to use newly introduced class.
3. Do not return a date graded value if there is no grade.
4. Added course grade date as an option to the date element.
5. Fix inconsistency between number of decimals showing for a grade
   between the drag and drop interface and the PDF.
2017-12-03 14:47:45 +08:00
Mark Nelson
df6385fe94 Removed improper use of self 2017-12-03 14:47:39 +08:00
Mark Nelson
873ffdf932 Removed unnecessary line break 2017-12-03 14:47:30 +08:00
Mark Nelson
6dcce0a17a #135 Added missing string when performing course reset 2017-12-03 14:47:25 +08:00
Mark Nelson
4797dd287e Fixed Moodle PHPdoc check complaint 2017-12-03 14:47:20 +08:00
Mark Nelson
d0b8df1816 Bumped version 2017-12-03 14:47:00 +08:00
Mark Nelson
01ed749ac1 Updated CHANGES.md
Updating details in advance before version bump.

Also minor fixes to previously listed changes.
2017-12-03 14:46:26 +08:00
Mark Nelson
459244b417 #134 Renamed duplicate() function and modified behaviour 2017-12-03 14:45:51 +08:00
Mark Nelson
9507a87d46 #134 Add ability to duplicate templates 2017-12-03 14:45:45 +08:00
Mark Nelson
6128884b5a #134 Created class for the manage templates table 2017-12-03 14:45:39 +08:00
Mark Nelson
42812529d4 #134 Do not load templates in site templates 2017-12-03 14:45:34 +08:00
Mark Nelson
a9fdf770d0 #119 Add ability to delete issued certificates 2017-12-03 14:45:23 +08:00
Mark Nelson
6e55519b81 Added missing confirm_sesskey() checks 2017-12-03 14:45:18 +08:00
Mark Nelson
d9eb78eee1 #46 Added digital signature element 2017-12-03 14:45:11 +08:00
Mark Nelson
c26dce0e79 Fixed exception being thrown when deleted image is selected 2017-12-03 14:43:59 +08:00
Mark Nelson
33d2f65e56 Add ability to set the width and height even if no image is selected 2017-12-03 14:43:55 +08:00
Mark Nelson
f75a943352 Fixed incorrect setting of 'fileid' form element 2017-12-03 14:43:49 +08:00
Mark Nelson
7b04c7a67f Added missing element 'pageid' value 2017-12-03 14:43:44 +08:00
Mark Nelson
13f892a229 Removed usage of magic getter and abuse of $this->element 2017-12-03 14:43:40 +08:00
Mark Nelson
430ae35d71 Created factory class 2017-12-03 14:43:35 +08:00
Mark Nelson
03ae37705a Moved helper function 2017-12-03 14:43:28 +08:00
Mark Nelson
0b807a2697 Extend Behat test coverage for loading templates 2017-12-03 14:43:24 +08:00
Mark Nelson
cce88f8122 Bumped version 2017-12-03 14:41:46 +08:00
Mark Nelson
9baeb5477d Updated CHANGES.md
Updating details in advance before version bump.
2017-12-03 14:41:01 +08:00
Mark Nelson
dc56808e32 #129 Added Behat test coverage 2017-12-03 14:39:24 +08:00
Mark Nelson
852408ee17 Fixed minor typo in template comment 2017-12-03 14:39:17 +08:00
Mark Nelson
c89c2d447a Lang changes
It's not always necessary to specify it is a 'custom' certificate.

Also some other minor improvements.
2017-12-03 14:39:04 +08:00
Mark Nelson
072d75798f Fixed codechecker line length complaint 2017-12-03 14:38:59 +08:00
Mark Nelson
8c84adeb8d #118 Fixed issue with activity dates not displaying 2017-12-03 14:38:54 +08:00
Mark Nelson
0f206bccbb Added CHANGES.md 2017-12-03 14:38:47 +08:00
Mark Nelson
a9e3714139 #35 Do not right align 'Reposition elements' text 2017-12-03 14:37:11 +08:00
Mark Nelson
f382805d89 Redirect after page deletion to avoid page error when refreshing 2017-12-03 14:37:05 +08:00
Mark Nelson
0f2b2f2fca #35 Make some of the buttons secondary when editing a certificate
MDL-59740 added the ability to determine if a submit button should
be primary, or not.
2017-12-03 14:36:57 +08:00
Mark Nelson
528daad08c #35 Reworded 'addcertpage' string and changed button to a link
Also added an icon next to the link and moved the location via CSS.
2017-12-03 14:36:53 +08:00
Mark Nelson
5d1dd37972 #35 Reworded 'deletecertpage' string and added icon 2017-12-03 14:36:47 +08:00
Mark Nelson
69405ced78 #35 Moved 'Add element' submit button 2017-12-03 14:36:40 +08:00
Mark Nelson
937cf3ad81 #123 Change reference to old column name 2017-12-03 14:35:45 +08:00
Mark Nelson
254fdd4017 #123 Renamed column to avoid breaking Oracle
The word 'size' is a reserved word in Oracle.
2017-12-03 14:35:07 +08:00
Mark Nelson
3b61d1ffa8 #117 Do not process image if 'No image' selected 2017-12-03 14:33:28 +08:00
Mark Nelson
b18f4347cb #116 Re-added previous date formats that no longer exist 2017-12-03 14:32:01 +08:00
Mark Nelson
1015ffa46d #85 Added new capability for viewing the course report 2017-12-03 14:29:32 +08:00
Mark Nelson
a17bb41307 #30 Add more date formats 2017-12-03 14:04:41 +08:00
Mark Nelson
84610ea418 #30 Add the course start and end dates as options 2017-12-03 14:04:35 +08:00
Mark Nelson
8499527489 Bumped version 2017-06-01 10:11:54 +08:00
Mark Nelson
1daf83fb2c #115 Prevent PDF being polluted with HTML at the EOF 2017-05-31 16:49:44 +08:00
Mark Nelson
1b47ae9eea Removed unnecessary semicolon 2017-05-31 16:49:40 +08:00
Mark Nelson
b067d398dd #82 Do not assume $COURSE represents the course for the certificate 2017-05-31 16:49:36 +08:00
Mark Nelson
90109a881e #78 Add setting to allow anyone to verify certificates 2017-05-31 16:49:28 +08:00
Mark Nelson
015519cff1 Verification link should only verify for specified certificate
Also populated navigation bar.
2017-05-31 16:48:19 +08:00
Mark Nelson
4a44f42b50 Update travis 2017-05-21 12:20:31 +08:00
Mark Nelson
2ef17b9766 #111 Fixed task creating duplicate rows in issue table 2017-05-21 12:20:31 +08:00
Mark Nelson
44fdab188e Bumped version 2017-05-04 21:29:53 +08:00
Mark Nelson
c90e0f5a7c #108 Changed usage of expected 'pathnamehash' to use new information 2017-05-04 20:38:52 +08:00
Mark Nelson
48e139c6d3 #108 Upgrade existing image elements 2017-05-04 20:37:45 +08:00
Mark Nelson
8f7ca7528b #106 Minor tweaks to README.md 2017-04-28 10:49:58 +08:00
Mark Nelson
9b96b2c9b3 #102 Handle images during backup/restore 2017-04-27 22:21:21 +08:00
Mark Nelson
074ae948c6 #106 Updated README.md 2017-04-27 22:21:14 +08:00
David Balch
d2fc041038 #100 Add SVG support for image and bgimage elements. 2017-04-27 22:21:06 +08:00
Michael Meneses
408da87cb3 #103 Added composer.json 2017-04-27 22:20:59 +08:00
Mark Nelson
2a6c547d77 #48 Make required time check more robust 2017-04-27 22:20:39 +08:00
Mark Nelson
26cb3850f7 Fixed local_codechecker complaint 2017-04-26 22:09:54 +08:00
Mark Nelson
6eb8e5b27c #48 Added task and settings responsible for emailing certificates 2017-04-26 21:44:04 +08:00
ovidiumorariu
fa1bfc0436 #95 Fixed font style generation 2017-03-16 19:02:43 +08:00
Andrew Nicols
086cb16b3c #89 Copy files to temp directory before accessing 2017-03-08 14:38:02 +08:00
Mark Nelson
7b58fb9ae6 #83 Removed unnecessary pop-up 2017-03-04 18:00:44 +08:00
Mark Nelson
69e872209f #79 Replaced usages of hard-coded strings 2017-02-25 13:50:49 +08:00
Mark Nelson
7a2e080ab8 #88 Make plugins DB codechecker happy 2017-02-25 13:00:55 +08:00
Mark Nelson
a0bb27773f #80 Fixed user picture not displaying on PDF 2017-02-16 18:54:25 +08:00
Mark Nelson
041d49998e #76 Added standard course viewed events 2017-02-16 18:54:21 +08:00
Mark Nelson
325c5330d6 Fixed JSHint warning 2017-02-02 14:47:46 +08:00
Mark Nelson
89dda02900 #51 Added ability to verify certificates 2017-02-02 12:35:29 +08:00
Mark Nelson
f09f552385 Fixed the context level for 'mod/customcert:manage' in db/access.php 2017-02-02 12:28:43 +08:00
Mark Nelson
e021ad2083 Fixed reference to a string present in the mod_certificate activity 2017-02-02 12:28:37 +08:00
Mark Nelson
c9b3b00059 Fixed call to undefined function in customcert_user_complete() 2017-02-02 12:28:32 +08:00
Mark Nelson
9a8bf74714 #71 Added validation for element and template name length 2017-02-02 12:28:25 +08:00
Mark Nelson
055a82e69b #71 Increase customcert_templates 'name' column length 2017-02-02 12:28:12 +08:00
Mark Nelson
bda1c48d51 #65 Added user picture element
Thanks goes to Jean-Michel Vedrine.
2017-02-02 12:25:41 +08:00
Mark Nelson
548f265a22 #74 Ensure image element does not ignore 'showposxy' setting 2017-02-02 12:20:09 +08:00
Mark Nelson
2ea16f9e5e #40 Change download certificate string 2017-02-02 12:20:02 +08:00
Mark Nelson
fba140fff2 #63 Update XY positions in dialogue when moving elements 2017-02-02 12:19:40 +08:00
Mark Nelson
62f73e3db8 #64 Fixed incorrect element position after changing reference point 2017-02-02 12:17:44 +08:00
Mark Nelson
2c3f6d1419 Added missing 'customcert:viewallcertificates' string 2017-01-28 19:50:20 +11:00
Mark Nelson
3f632d3f5e #34 Prevent invalid course module ID error 2017-01-28 19:45:28 +11:00
Mark Nelson
6230759d56 #44 Added page to view all certificates for a user 2016-12-30 14:00:06 +08:00
Mark Nelson
527d453950 #41 Add setting to display the position X and Y elements 2016-12-30 13:59:14 +08:00
Mark Nelson
c1c2f9957e Removed erroneous passing of $course to footer() 2016-12-29 17:22:34 +08:00
Mark Nelson
c5eb4c2912 Addressed lint warnings 2016-12-29 17:22:19 +08:00
Mark Nelson
60d5c8110a Bumped version 2016-12-13 10:54:50 +08:00
Mark Nelson
6903f6db0b #57 Set the activity as viewed when downloaded 2016-11-29 15:49:10 +08:00
Mark Nelson
0777b07502 #49 Allow templates to have the same name 2016-11-29 15:49:03 +08:00
Mark Nelson
0757213295 #55 Fixed references to capabilities from mod_certificate 2016-11-29 13:31:47 +08:00
Mark Nelson
5dfbad3b69 Update maturity 2016-08-26 19:29:47 +08:00
Mark Nelson
c280931773 #15 Fixed namespace for search base class
The namespace was changed in 3.2, so broke when backported.
2016-08-26 19:00:15 +08:00
108 changed files with 6532 additions and 1079 deletions

39
.travis.yml Normal file
View file

@ -0,0 +1,39 @@
language: php
sudo: false
cache:
directories:
- $HOME/.composer/cache
php:
- 5.4
- 7.0
env:
global:
- MOODLE_BRANCH=MOODLE_31_STABLE
matrix:
- DB=pgsql
- DB=mysqli
before_install:
- phpenv config-rm xdebug.ini
- cd ../..
- composer selfupdate
- composer create-project -n --no-dev --prefer-dist moodlerooms/moodle-plugin-ci ci ^1
- export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
install:
- moodle-plugin-ci install
script:
- moodle-plugin-ci phplint
- moodle-plugin-ci codechecker
- moodle-plugin-ci csslint
- moodle-plugin-ci shifter
- moodle-plugin-ci jshint
- moodle-plugin-ci validate
- moodle-plugin-ci phpunit
- moodle-plugin-ci behat

71
CHANGES.md Normal file
View file

@ -0,0 +1,71 @@
# Changelog
All notable changes to this project will be documented in this file.
Note - All hash comments refer to the issue number. Eg. #169 refers to https://github.com/markn86/moodle-mod_customcert/issues/169.
## [3.1.21] - 2018-05-17
### Fixed
- Race condition on certificate issues in scheduled task (#173).
- Ensure we backup the 'verifyany' setting (#169).
- Fixed encoding content links used by restore (#166).
## [3.1.20] - 2018-01-23
### Fixed
- The fatal error ```Can't use method return value in write context``` would occur in PHP < 5.5 . Note - this fix does not need
to be applied to newer branches as the Moodle versions require PHP > 5.5.
## [3.1.17] - 2017-11-13
### Added
- Added capability ```mod/customcert:verifyallcertificates``` that provides a user with the ability to verify any certificate
on the site by simply visiting the ```mod/customcert/verify_certificate.php``` page, rather than having to go to the
verification link for each certificate.
- Added site setting ```customcert/verifyallcertificates``` which when enabled allows any person (including users not logged in)
to be able to verify any certificate on the site, rather than having to go to the verification link for each certificate.
However, this only applies to certificates where ```Allow anyone to verify a certificate``` has been set to ```Yes``` in the
certificate settings.
- You can now display the grade and date of all grade items, not just the course and course activities.
- Text has been added above the ```My certificates``` list to explain that it contains certificates that have been issued to
avoid confusion as to why certificates may not be appearing.
### Changed
- The course full name is now used in emails.
### Fixed
- Added missing string used in course reset.
## [3.1.16] - 2017-09-04
### Added
- New digital signature element (uses existing functionality in the TCPDF library).
- Ability to duplicate site templates via the manage templates page.
- Ability to delete issued certificates for individual users on the course report page.
### Changed
- Removed usage of magic getter and abuse of ```$this->element```. The variable ```$this->element``` will still be
accessible by any third-party element plugins, though this is discouraged and the appropriate ```get_xxx()```
method should be used instead. Using ```$this->element``` in ```definition_after_data()``` will no longer work.
Please explicitly set the value of any custom fields you have in the form.
### Fixed
- Added missing ```confirm_sesskey()``` checks.
- Minor bug fixes.
## [3.1.15] - 2017-08-11
### Added
- Added much needed Behat test coverage.
### Changed
- Minor language string changes.
- Made changes to the UI when editing a certificate.
- Moved the 'Add element' submit button below the list of elements.
- Added icon next to the 'Delete page' link.
- Changed the 'Add page' button to a link, added an icon and moved it's location to the right.
- Do not make all submit buttons primary. MDL-59740 needs to be applied to your Moodle install in order to notice the change.
### Fixed
- Issue where the date an activity was graded was not displaying at all.
## [3.1.14] - 2017-08-05
### Changed
- Renamed the column 'size' in the table 'customcert_elements' to 'fontsize' due to 'size' being a reserved word in Oracle.

View file

@ -1,49 +1,39 @@
QUICK INSTALL
=============
# The custom certificate activity
There are two installation methods that are available. Follow one of these, then log into your Moodle site as an administrator and visit the notifications page to complete the install.
This activity allows the dynamic generation of PDF certificates with complete customisation via the web browser.
==================== MOST RECOMMENDED METHOD - Git ====================
## Installation
If you do not have git installed, please see the below link. Please note, it is not necessary to set up the SSH Keys. This is only needed if you are going to create a repository of your own on github.com.
There are two installation methods that are available.
Information on installing git - http://help.github.com/set-up-git-redirect/
Follow one of these, then log into your Moodle site as an administrator and visit the notifications page to complete the install.
Once you have git installed, simply visit the Moodle mod directory and clone git://github.com/markn86/moodle-mod_customcert.git, remember to rename the folder to customcert if you do not specify this in the clone command
### Git
Eg. Linux command line would be as follow -
This requires Git being installed. If you do not have Git installed, please visit the [Git website](https://git-scm.com/downloads "Git website").
git clone git://github.com/markn86/moodle-mod_customcert.git customcert
Once you have Git installed, simply visit your Moodle mod directory and clone the repository using the following command.
Use git pull to update this repository periodically to ensure you have the latest version.
```
git clone https://github.com/markn86/moodle-mod_customcert.git customcert
```
==================== Download the customcert module. ====================
Then checkout the branch corresponding to the version of Moodle you are using with the following command. Make sure to replace MOODLE_32_STABLE with the version of Moodle you are using.
Visit https://github.com/markn86/moodle-mod_customcert and download the zip, uncompress this zip and extract the folder. The folder will have a name similar to markn86-moodle-mod_customcert-c9fbadb, you MUST rename this to customcert. Place this folder in your mod folder in your Moodle directory.
```
git checkout MOODLE_32_STABLE
```
nb. The reason this is not the recommended method is due to the fact you have to over-write the contents of this folder to apply any future updates to the customcert module. In the above method there is a simple command to update the files.
Use `git pull` to update this repository periodically to ensure you have the most recent updates.
### Download the zip
Customisation
=============
Visit the [Moodle plugins website](https://moodle.org/plugins/mod_customcert "Moodle plugins website") and download the zip corresponding to the version of Moodle you are using. Extract the zip and place the 'customcert' folder in the mod folder in your Moodle directory.
========================= Add Fonts to TCPDF. =========================
## More information
For customization, you have a couple of options in the Custom Certificate module. One of the most common customizations is, adding fonts to the Certificate creator.
Visit the [wiki page](https://docs.moodle.org/en/Custom_certificate_module "Wiki page") for more details. Also, it is a wiki, so please feel free to edit it. :)
Custom Certificate uses a pdf creator called TCPDF, which embeds fonts into the PDF file, so that you can view fonts, even though they are not installed on the target machine. This requires you to tell TCPDF which fonts to take.
## License
Importing Fonts into TCPDF is quite simple:
1. Convert the font you want embedded into the tcpdf format (for example using [http://fonts.snm-portal.com][1] or [http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf][2])
2. Put the converted files into the folder: `/path/to/moodle/lib/tcpdf/fonts`
3. Use the font in your certificate builder
4. Enjoy
> Please note that inserting a file into the tcpdf folder is considered a "core hack" and might not be available on some commercially hosted systems. Please check your providers agreement on the topic to make sure this is something you're allowed to do.
[1]: http://fonts.snm-portal.com "http://fonts.snm-portal.com"
[2]: http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf "http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf"
Licensed under the [GNU GPL License](http://www.gnu.org/copyleft/gpl.html).

View file

@ -22,12 +22,12 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
if (!defined('AJAX_SCRIPT')) {
define('AJAX_SCRIPT', true);
}
require_once(__DIR__ . '/../../config.php');
$tid = required_param('tid', PARAM_INT);
$values = required_param('values', PARAM_RAW);
$values = json_decode($values);

View file

@ -1 +1 @@
define(["jquery","core/yui","core/fragment","mod_customcert/dialogue","core/notification","core/str","core/templates","core/ajax"],function(a,b,c,d,e,f,g,h){var i=function(b){this._node=a(b),this._setEvents()};return i.prototype.CUSTOMCERT_REF_POINT_TOPLEFT=0,i.prototype.CUSTOMCERT_REF_POINT_TOPCENTER=1,i.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT=2,i.prototype._setEvents=function(){this._node.on("click",".element",this._editElement.bind(this))},i.prototype._editElement=function(a){var g=a.currentTarget.id.substr(8),h=this._node.attr("data-contextid"),i={elementid:g};c.loadFragment("mod_customcert","editelement",h,i).done(function(a,c){f.get_string("editelement","mod_customcert").done(function(e){b.use("moodle-core-formchangechecker",function(){new d(e,"<div id='elementcontent'></div>",this._editElementDialogueConfig.bind(this,g,a,c),(void 0),(!0))}.bind(this))}.bind(this))}.bind(this)).fail(e.exception)},i.prototype._editElementDialogueConfig=function(b,c,d,e){g.replaceNode("#elementcontent",c,d);var f=a(e.getContent());f.on("click","#id_submitbutton",function(c){M.core_formchangechecker.reset_form_dirty_state(),this._saveElement(b).always(function(){this._getElementHTML(b).always(function(c){var d=this._node.find("#element-"+b),f=a("#id_refpoint").val(),g="";f==this.CUSTOMCERT_REF_POINT_TOPLEFT?g="refpoint-left":f==this.CUSTOMCERT_REF_POINT_TOPCENTER?g="refpoint-center":f==this.CUSTOMCERT_REF_POINT_TOPRIGHT&&(g="refpoint-right"),d.empty().append(c),d.removeClass(),d.addClass("element "+g),e.close()}.bind(this))}.bind(this)),c.preventDefault()}.bind(this)),f.on("click","#id_cancel",function(a){e.close(),a.preventDefault()}.bind(this))},i.prototype._getElementHTML=function(a){var b=this._node.attr("data-templateid"),c=h.call([{methodname:"mod_customcert_get_element_html",args:{templateid:b,elementid:a}}]);return c[0]},i.prototype._saveElement=function(b){var c=this._node.attr("data-templateid"),d=a("#editelementform").serializeArray(),e=h.call([{methodname:"mod_customcert_save_element",args:{templateid:c,elementid:b,values:d}}]);return e[0]},{init:function(a){new i(a)}}});
define(["jquery","core/yui","core/fragment","mod_customcert/dialogue","core/notification","core/str","core/templates","core/ajax"],function(a,b,c,d,e,f,g,h){var i=function(b){this._node=a(b),this._setEvents()};return i.prototype.CUSTOMCERT_REF_POINT_TOPLEFT=0,i.prototype.CUSTOMCERT_REF_POINT_TOPCENTER=1,i.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT=2,i.prototype.PIXELSINMM=3.779527559055,i.prototype._setEvents=function(){this._node.on("click",".element",this._editElement.bind(this))},i.prototype._editElement=function(a){var g=a.currentTarget.id.substr(8),h=this._node.attr("data-contextid"),i={elementid:g};c.loadFragment("mod_customcert","editelement",h,i).done(function(a,c){f.get_string("editelement","mod_customcert").done(function(e){b.use("moodle-core-formchangechecker",function(){new d(e,"<div id='elementcontent'></div>",this._editElementDialogueConfig.bind(this,g,a,c),void 0,!0)}.bind(this))}.bind(this))}.bind(this)).fail(e.exception)},i.prototype._editElementDialogueConfig=function(b,c,d,e){g.replaceNode("#elementcontent",c,d),this._setPositionInForm(b);var f=a(e.getContent());f.on("click","#id_submitbutton",function(c){M.core_formchangechecker.reset_form_dirty_state(),this._saveElement(b).then(function(){this._getElementHTML(b).done(function(c){var d=this._node.find("#element-"+b),f=parseInt(a("#id_refpoint").val()),g="";f==this.CUSTOMCERT_REF_POINT_TOPLEFT?g="refpoint-left":f==this.CUSTOMCERT_REF_POINT_TOPCENTER?g="refpoint-center":f==this.CUSTOMCERT_REF_POINT_TOPRIGHT&&(g="refpoint-right"),d.empty().append(c),d.removeClass(),d.addClass("element "+g),d.attr("data-refpoint",f);var h=a("#editelementform #id_posx").val(),i=a("#editelementform #id_posy").val();this._setPosition(b,f,h,i),e.close()}.bind(this))}.bind(this)),c.preventDefault()}.bind(this)),f.on("click","#id_cancel",function(a){e.close(),a.preventDefault()})},i.prototype._setPosition=function(a,c,d,e){var f=b.one("#element-"+a);d=b.one("#pdf").getX()+d*this.PIXELSINMM,e=b.one("#pdf").getY()+e*this.PIXELSINMM;var g=parseFloat(f.getComputedStyle("width")),h=f.width*this.PIXELSINMM;switch(h&&g>h&&(g=h),c){case this.CUSTOMCERT_REF_POINT_TOPCENTER:d-=g/2;break;case this.CUSTOMCERT_REF_POINT_TOPRIGHT:d=d-g+2}f.setX(d),f.setY(e)},i.prototype._setPositionInForm=function(c){var d=a("#editelementform #id_posx"),e=a("#editelementform #id_posy");if(d.length&&e.length){var f=b.one("#element-"+c),g=f.getX()-b.one("#pdf").getX(),h=f.getY()-b.one("#pdf").getY(),i=parseInt(f.getData("refpoint")),j=parseFloat(f.getComputedStyle("width"));switch(i){case this.CUSTOMCERT_REF_POINT_TOPCENTER:g+=j/2;break;case this.CUSTOMCERT_REF_POINT_TOPRIGHT:g+=j}g=Math.round(parseFloat(g/this.PIXELSINMM)),h=Math.round(parseFloat(h/this.PIXELSINMM)),d.val(g),e.val(h)}},i.prototype._getElementHTML=function(a){var b=this._node.attr("data-templateid"),c=h.call([{methodname:"mod_customcert_get_element_html",args:{templateid:b,elementid:a}}]);return c[0]},i.prototype._saveElement=function(b){var c=this._node.attr("data-templateid"),d=a("#editelementform").serializeArray(),e=h.call([{methodname:"mod_customcert_save_element",args:{templateid:c,elementid:b,values:d}}]);return e[0]},{init:function(a){new i(a)}}});

View file

@ -42,7 +42,7 @@ define(['core/yui'], function(Y) {
wide = false;
}
Y.use('moodle-core-notification', 'timers', function () {
Y.use('moodle-core-notification', 'timers', function() {
var width = '480px';
if (wide) {
width = '800px';
@ -91,10 +91,12 @@ define(['core/yui'], function(Y) {
/**
* Get content.
*
* @returns {HTMLElement}
*/
dialogue.prototype.getContent = function() {
return this.yuiDialogue.bodyNode.getDOMNode();
};
return /** @alias module:mod_customcert/dialogue */ dialogue;
return dialogue;
});

View file

@ -23,126 +23,191 @@
*/
define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/notification',
'core/str', 'core/templates', 'core/ajax'],
function($, Y, fragment, Dialogue, notification, str, template, ajax) {
function($, Y, fragment, Dialogue, notification, str, template, ajax) {
/**
* RearrangeArea class.
*
* @param {String} selector The rearrange PDF selector
*/
var RearrangeArea = function(selector) {
this._node = $(selector);
this._setEvents();
};
/**
* RearrangeArea class.
*
* @param {String} selector The rearrange PDF selector
*/
var RearrangeArea = function(selector) {
this._node = $(selector);
this._setEvents();
};
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT = 0;
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER = 1;
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT = 2;
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPLEFT = 0;
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPCENTER = 1;
RearrangeArea.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT = 2;
RearrangeArea.prototype.PIXELSINMM = 3.779527559055;
RearrangeArea.prototype._setEvents = function() {
this._node.on('click', '.element', this._editElement.bind(this));
};
RearrangeArea.prototype._setEvents = function() {
this._node.on('click', '.element', this._editElement.bind(this));
};
RearrangeArea.prototype._editElement = function(event) {
var elementid = event.currentTarget.id.substr(8);
var contextid = this._node.attr('data-contextid');
var params = {
'elementid' : elementid
};
RearrangeArea.prototype._editElement = function(event) {
var elementid = event.currentTarget.id.substr(8);
var contextid = this._node.attr('data-contextid');
var params = {
'elementid': elementid
};
fragment.loadFragment('mod_customcert', 'editelement', contextid, params).done(function(html, js) {
str.get_string('editelement', 'mod_customcert').done(function(title) {
Y.use('moodle-core-formchangechecker', function () {
new Dialogue(
title,
'<div id=\'elementcontent\'></div>',
this._editElementDialogueConfig.bind(this, elementid, html, js),
undefined,
true
);
fragment.loadFragment('mod_customcert', 'editelement', contextid, params).done(function(html, js) {
str.get_string('editelement', 'mod_customcert').done(function(title) {
Y.use('moodle-core-formchangechecker', function() {
new Dialogue(
title,
'<div id=\'elementcontent\'></div>',
this._editElementDialogueConfig.bind(this, elementid, html, js),
undefined,
true
);
}.bind(this));
}.bind(this));
}.bind(this)).fail(notification.exception);
};
RearrangeArea.prototype._editElementDialogueConfig = function(elementid, html, js, popup) {
// Place the content in the dialogue.
template.replaceNode('#elementcontent', html, js);
// We may have dragged the element changing it's position.
// Ensure the form has the current up-to-date location.
this._setPositionInForm(elementid);
// Add events for when we save, close and cancel the page.
var body = $(popup.getContent());
body.on('click', '#id_submitbutton', function(e) {
// Do not want to ask the user if they wish to stay on page after saving.
M.core_formchangechecker.reset_form_dirty_state();
// Save the data.
this._saveElement(elementid).then(function() {
// Update the DOM to reflect the adjusted value.
this._getElementHTML(elementid).done(function(html) {
var elementNode = this._node.find('#element-' + elementid);
var refpoint = parseInt($('#id_refpoint').val());
var refpointClass = '';
if (refpoint == this.CUSTOMCERT_REF_POINT_TOPLEFT) {
refpointClass = 'refpoint-left';
} else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPCENTER) {
refpointClass = 'refpoint-center';
} else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPRIGHT) {
refpointClass = 'refpoint-right';
}
elementNode.empty().append(html);
// Update the ref point.
elementNode.removeClass();
elementNode.addClass('element ' + refpointClass);
elementNode.attr('data-refpoint', refpoint);
// Move the element.
var posx = $('#editelementform #id_posx').val();
var posy = $('#editelementform #id_posy').val();
this._setPosition(elementid, refpoint, posx, posy);
// All done.
popup.close();
}.bind(this));
}.bind(this));
e.preventDefault();
}.bind(this));
}.bind(this));
}.bind(this)).fail(notification.exception);
};
RearrangeArea.prototype._editElementDialogueConfig = function(elementid, html, js, popup) {
// Place the content in the dialogue.
template.replaceNode('#elementcontent', html, js);
// Add events for when we save, close and cancel the page.
var body = $(popup.getContent());
body.on('click', '#id_submitbutton', function(e) {
// Do not want to ask the user if they wish to stay on page after saving.
M.core_formchangechecker.reset_form_dirty_state();
// Save the data.
this._saveElement(elementid).always(function() {
// Update the DOM to reflect the adjusted value.
this._getElementHTML(elementid).always(function(html) {
var elementNode = this._node.find('#element-' + elementid);
var refpoint = $('#id_refpoint').val();
var refpointClass = '';
if (refpoint == this.CUSTOMCERT_REF_POINT_TOPLEFT) {
refpointClass = 'refpoint-left';
} else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPCENTER) {
refpointClass = 'refpoint-center';
} else if (refpoint == this.CUSTOMCERT_REF_POINT_TOPRIGHT) {
refpointClass = 'refpoint-right';
}
elementNode.empty().append(html);
// Update the ref point.
elementNode.removeClass();
elementNode.addClass('element ' + refpointClass);
body.on('click', '#id_cancel', function(e) {
popup.close();
}.bind(this));
}.bind(this));
e.preventDefault();
}.bind(this));
e.preventDefault();
});
};
body.on('click', '#id_cancel', function(e) {
popup.close();
e.preventDefault();
}.bind(this));
};
RearrangeArea.prototype._setPosition = function(elementid, refpoint, posx, posy) {
var element = Y.one('#element-' + elementid);
RearrangeArea.prototype._getElementHTML = function(elementid) {
// Get the variables we need.
var templateid = this._node.attr('data-templateid');
posx = Y.one('#pdf').getX() + posx * this.PIXELSINMM;
posy = Y.one('#pdf').getY() + posy * this.PIXELSINMM;
var nodewidth = parseFloat(element.getComputedStyle('width'));
var maxwidth = element.width * this.PIXELSINMM;
// Call the web service to get the updated element.
var promises = ajax.call([{
methodname: 'mod_customcert_get_element_html',
args: {
templateid : templateid,
elementid : elementid
}
}]);
if (maxwidth && (nodewidth > maxwidth)) {
nodewidth = maxwidth;
}
// Return the promise.
return promises[0];
};
switch (refpoint) {
case this.CUSTOMCERT_REF_POINT_TOPCENTER:
posx -= nodewidth / 2;
break;
case this.CUSTOMCERT_REF_POINT_TOPRIGHT:
posx = posx - nodewidth + 2;
break;
}
RearrangeArea.prototype._saveElement = function(elementid) {
// Get the variables we need.
var templateid = this._node.attr('data-templateid');
var inputs = $('#editelementform').serializeArray();
element.setX(posx);
element.setY(posy);
};
// Call the web service to save the element.
var promises = ajax.call([{
methodname: 'mod_customcert_save_element',
args: {
templateid : templateid,
elementid : elementid,
values : inputs
}
}]);
RearrangeArea.prototype._setPositionInForm = function(elementid) {
var posxelement = $('#editelementform #id_posx');
var posyelement = $('#editelementform #id_posy');
// Return the promise.
return promises[0];
};
if (posxelement.length && posyelement.length) {
var element = Y.one('#element-' + elementid);
var posx = element.getX() - Y.one('#pdf').getX();
var posy = element.getY() - Y.one('#pdf').getY();
var refpoint = parseInt(element.getData('refpoint'));
var nodewidth = parseFloat(element.getComputedStyle('width'));
return {
init : function(selector) {
new RearrangeArea(selector);
switch (refpoint) {
case this.CUSTOMCERT_REF_POINT_TOPCENTER:
posx += nodewidth / 2;
break;
case this.CUSTOMCERT_REF_POINT_TOPRIGHT:
posx += nodewidth;
break;
}
posx = Math.round(parseFloat(posx / this.PIXELSINMM));
posy = Math.round(parseFloat(posy / this.PIXELSINMM));
posxelement.val(posx);
posyelement.val(posy);
}
};
RearrangeArea.prototype._getElementHTML = function(elementid) {
// Get the variables we need.
var templateid = this._node.attr('data-templateid');
// Call the web service to get the updated element.
var promises = ajax.call([{
methodname: 'mod_customcert_get_element_html',
args: {
templateid: templateid,
elementid: elementid
}
}]);
// Return the promise.
return promises[0];
};
RearrangeArea.prototype._saveElement = function(elementid) {
// Get the variables we need.
var templateid = this._node.attr('data-templateid');
var inputs = $('#editelementform').serializeArray();
// Call the web service to save the element.
var promises = ajax.call([{
methodname: 'mod_customcert_save_element',
args: {
templateid: templateid,
elementid: elementid,
values: inputs
}
}]);
// Return the promise.
return promises[0];
};
return {
init: function(selector) {
new RearrangeArea(selector);
}
};
}
};
});
);

View file

@ -15,8 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the backup tasks that provides all the settings and steps to perform
* one complete backup of the activity.
* This file contains the backup tasks that provides all the settings and steps to perform a backup of the activity.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
@ -29,6 +28,10 @@ require_once($CFG->dirroot . '/mod/customcert/backup/moodle2/backup_customcert_s
/**
* Handles creating tasks to peform in order to create the backup.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_customcert_activity_task extends backup_activity_task {
@ -49,6 +52,9 @@ class backup_customcert_activity_task extends backup_activity_task {
/**
* Code the transformations to perform in the activity in order to get transportable (encoded) links.
*
* @param string $content
* @return mixed|string
*/
static public function encode_content_links($content) {
global $CFG;
@ -56,12 +62,12 @@ class backup_customcert_activity_task extends backup_activity_task {
$base = preg_quote($CFG->wwwroot, "/");
// Link to the list of customcerts.
$search ="/(".$base."\/mod\/customcert\/index.php\?id\=)([0-9]+)/";
$content = preg_replace($search, '$@customcertINDEX*$2@$', $content);
$search = "/(".$base."\/mod\/customcert\/index.php\?id\=)([0-9]+)/";
$content = preg_replace($search, '$@CUSTOMCERTINDEX*$2@$', $content);
// Link to customcert view by moduleid.
$search ="/(".$base."\/mod\/customcert\/view.php\?id\=)([0-9]+)/";
$content = preg_replace($search, '$@customcertVIEWBYID*$2@$', $content);
$search = "/(".$base."\/mod\/customcert\/view.php\?id\=)([0-9]+)/";
$content = preg_replace($search, '$@CUSTOMCERTVIEWBYID*$2@$', $content);
return $content;
}

View file

@ -26,6 +26,10 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
/**
* Define the complete customcert structure for backup, with file and id annotations.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_customcert_activity_structure_step extends backup_activity_structure_step {
@ -38,8 +42,8 @@ class backup_customcert_activity_structure_step extends backup_activity_structur
// The instance.
$customcert = new backup_nested_element('customcert', array('id'), array(
'templateid', 'name', 'intro', 'introformat', 'requiredtime', 'protection',
'timecreated', 'timemodified'));
'templateid', 'name', 'intro', 'introformat', 'requiredtime', 'verifyany', 'emailstudents',
'emailteachers', 'emailothers', 'protection', 'timecreated', 'timemodified'));
// The template.
$template = new backup_nested_element('template', array('id'), array(
@ -53,14 +57,14 @@ class backup_customcert_activity_structure_step extends backup_activity_structur
// The elements.
$element = new backup_nested_element('element', array('id'), array(
'pageid', 'name', 'element', 'data', 'font', 'size',
'pageid', 'name', 'element', 'data', 'font', 'fontsize',
'colour', 'posx', 'posy', 'width', 'refpoint', 'sequence',
'timecreated', 'timemodified'));
// The issues.
$issues = new backup_nested_element('issues');
$issue = new backup_nested_element('issue', array('id'), array(
'customcertid', 'userid', 'timecreated', 'code'));
'customcertid', 'userid', 'timecreated', 'emailed', 'code'));
// Build the tree.
$customcert->add_child($issues);
@ -90,6 +94,10 @@ class backup_customcert_activity_structure_step extends backup_activity_structur
// Annotate the user id's where required.
$issue->annotate_ids('user', 'userid');
// Define file annotations.
$customcert->annotate_files('mod_customcert', 'intro', null);
$customcert->annotate_files('mod_customcert', 'image', null, context_course::instance($this->get_courseid())->id);
// Return the root element (customcert), wrapped into standard activity structure.
return $this->prepare_activity_structure($customcert);
}

View file

@ -27,8 +27,11 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->dirroot . '/mod/customcert/backup/moodle2/restore_customcert_stepslib.php');
/**
* The class definition for assigning restore tasks that provides all the settings and steps
* to perform one complete restore of the activity.
* The class definition for assigning tasks that provide the settings and steps to perform a restore of the activity.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_customcert_activity_task extends restore_activity_task {
@ -109,7 +112,7 @@ class restore_customcert_activity_task extends restore_activity_task {
// Go through the elements for the certificate.
foreach ($elements as $e) {
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($e)) {
if ($e = \mod_customcert\element_factory::get_element_instance($e)) {
$e->after_restore($this);
}
}

View file

@ -26,6 +26,10 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
/**
* Define the complete customcert structure for restore, with file and id annotations.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_customcert_activity_structure_step extends restore_activity_structure_step {
@ -159,4 +163,17 @@ class restore_customcert_activity_structure_step extends restore_activity_struct
$newitemid = $DB->insert_record('customcert_issues', $data);
$this->set_mapping('customcert_issue', $oldid, $newitemid);
}
/**
* Called immediately after all the other restore functions.
*/
protected function after_execute() {
parent::after_execute();
// Add the files.
$this->add_related_files('mod_customcert', 'intro', null);
// Note - we can't use get_old_contextid() as it refers to the module context.
$this->add_related_files('mod_customcert', 'image', null, $this->get_task()->get_info()->original_course_contextid);
}
}

View file

@ -14,8 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mod_customcert;
/**
* Creates an upload form on the settings page.
*
@ -23,10 +21,19 @@ namespace mod_customcert;
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/adminlib.php');
/**
* Class extends admin setting class to allow/process an uploaded file
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_setting_link extends \admin_setting_configtext {
@ -40,6 +47,18 @@ class admin_setting_link extends \admin_setting_configtext {
*/
protected $linkname;
/**
* The admin_setting_link constructor.
*
* @param string $name
* @param string $visiblename
* @param string $description
* @param string $linkname
* @param mixed|string $link
* @param int|null $defaultsetting
* @param string $paramtype
* @param null $size
*/
public function __construct($name, $visiblename, $description, $linkname, $link, $defaultsetting,
$paramtype = PARAM_RAW, $size=null) {
$this->link = $link;

View file

@ -29,7 +29,11 @@ defined('MOODLE_INTERNAL') || die();
/**
* Class certificate.
*
* Represents a customcert activity instance.
* Helper functionality for certificates.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class certificate {
@ -82,13 +86,14 @@ class certificate {
*
* @param int $draftitemid the draft area containing the files
* @param int $contextid the context we are storing this image in
* @param string $filearea indentifies the file area.
*/
public static function upload_imagefiles($draftitemid, $contextid) {
public static function upload_files($draftitemid, $contextid, $filearea = 'image') {
global $CFG;
// Save the file if it exists that is currently in the draft area.
require_once($CFG->dirroot . '/lib/filelib.php');
file_save_draft_area_files($draftitemid, $contextid, 'mod_customcert', 'image', 0);
file_save_draft_area_files($draftitemid, $contextid, 'mod_customcert', $filearea, 0);
}
/**
@ -150,7 +155,7 @@ class certificate {
// Array to store the sizes.
$sizes = array();
for ($i = 1; $i <= 60; $i++) {
for ($i = 1; $i <= 200; $i++) {
$sizes[$i] = $i;
}
@ -161,14 +166,22 @@ class certificate {
* Get the time the user has spent in the course.
*
* @param int $courseid
* @param int $userid
* @return int the total time spent in seconds
*/
public static function get_course_time($courseid) {
public static function get_course_time($courseid, $userid = 0) {
global $CFG, $DB, $USER;
if (empty($userid)) {
$userid = $USER->id;
}
$logmanager = get_log_manager();
$readers = $logmanager->get_readers();
$enabledreaders = get_config('tool_log', 'enabled_stores');
if (empty($enabledreaders)) {
return 0;
}
$enabledreaders = explode(',', $enabledreaders);
// Go through all the readers until we find one that we can use.
@ -197,12 +210,12 @@ class certificate {
WHERE userid = :userid
AND $coursefield = :courseid
ORDER BY $timefield ASC";
$params = array('userid' => $USER->id, 'courseid' => $courseid);
$params = array('userid' => $userid, 'courseid' => $courseid);
$totaltime = 0;
if ($logs = $DB->get_recordset_sql($sql, $params)) {
foreach ($logs as $log) {
if (!isset($login)) {
// For the first time $login is not set so the first log is also the first login
// For the first time $login is not set so the first log is also the first login.
$login = $log->$timefield;
$lasthit = $log->$timefield;
$totaltime = 0;
@ -215,7 +228,7 @@ class certificate {
} else {
$totaltime += $delay;
}
// Now the actual log became the previous log for the next cycle
// Now the actual log became the previous log for the next cycle.
$lasthit = $log->$timefield;
}
@ -234,7 +247,7 @@ class certificate {
* @param int $limitfrom
* @param int $limitnum
* @param string $sort
* @return \stdClass the users
* @return array the users
*/
public static function get_issues($customcertid, $groupmode, $cm, $limitfrom, $limitnum, $sort = '') {
global $DB;
@ -252,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
@ -317,7 +330,7 @@ class certificate {
$conditionsparams = array();
// Get all users that can manage this certificate to exclude them from the report.
$certmanagers = array_keys(get_users_by_capability($context, 'mod/certificate:manage', 'u.id'));
$certmanagers = array_keys(get_users_by_capability($context, 'mod/customcert:manage', 'u.id'));
$certmanagers = array_merge($certmanagers, array_keys(get_admins()));
list($sql, $params) = $DB->get_in_or_equal($certmanagers, SQL_PARAMS_NAMED, 'cert');
$conditionssql .= "AND NOT u.id $sql \n";
@ -364,6 +377,71 @@ class certificate {
return array($conditionssql, $conditionsparams);
}
/**
* Get number of certificates for a user.
*
* @param int $userid
* @return int
*/
public static function get_number_of_certificates_for_user($userid) {
global $DB;
$sql = "SELECT COUNT(*)
FROM {customcert} c
INNER JOIN {customcert_issues} ci
ON c.id = ci.customcertid
WHERE ci.userid = :userid";
return $DB->count_records_sql($sql, array('userid' => $userid));
}
/**
* Gets the certificates for the user.
*
* @param int $userid
* @param int $limitfrom
* @param int $limitnum
* @param string $sort
* @return array
*/
public static function get_certificates_for_user($userid, $limitfrom, $limitnum, $sort = '') {
global $DB;
if (empty($sort)) {
$sort = 'ci.timecreated DESC';
}
$sql = "SELECT c.id, c.name, co.fullname as coursename, ci.code, ci.timecreated
FROM {customcert} c
INNER JOIN {customcert_issues} ci
ON c.id = ci.customcertid
INNER JOIN {course} co
ON c.course = co.id
WHERE ci.userid = :userid
ORDER BY $sort";
return $DB->get_records_sql($sql, array('userid' => $userid), $limitfrom, $limitnum);
}
/**
* Issues a certificate to a user.
*
* @param int $certificateid The ID of the certificate
* @param int $userid The ID of the user to issue the certificate to
* @return int The ID of the issue
*/
public static function issue_certificate($certificateid, $userid) {
global $DB;
$issue = new \stdClass();
$issue->userid = $userid;
$issue->customcertid = $certificateid;
$issue->code = self::generate_code();
$issue->emailed = 0;
$issue->timecreated = time();
// Insert the record into the database.
return $DB->insert_record('customcert_issues', $issue);
}
/**
* Generates a 10-digit code of random letters and numbers.
*

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the form for handling editing a customcert element.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
@ -48,17 +56,14 @@ class edit_element_form extends \moodleform {
$element = $this->_customdata['element'];
// Do not display the name if we are on the rearrange page.
if (!isset($this->_customdata['rearrange'])) {
// Add the field for the name of the element, this is required for all elements.
$mform->addElement('text', 'name', get_string('elementname', 'customcert'));
$mform->setType('name', PARAM_TEXT);
$mform->setDefault('name', get_string('pluginname', 'customcertelement_' . $element->element));
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->addHelpButton('name', 'elementname', 'customcert');
}
// Add the field for the name of the element, this is required for all elements.
$mform->addElement('text', 'name', get_string('elementname', 'customcert'), 'maxlength="255"');
$mform->setType('name', PARAM_TEXT);
$mform->setDefault('name', get_string('pluginname', 'customcertelement_' . $element->element));
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->addHelpButton('name', 'elementname', 'customcert');
$this->element = \mod_customcert\element::instance($element);
$this->element = \mod_customcert\element_factory::get_element_instance($element);
$this->element->render_form_elements($mform);
$this->add_action_buttons(true);
@ -79,6 +84,14 @@ class edit_element_form extends \moodleform {
* @return array the errors that were found
*/
public function validation($data, $files) {
return $this->element->validate_form_elements($data, $files);
$errors = array();
if (\core_text::strlen($data['name']) > 255) {
$errors['name'] = get_string('nametoolong', 'customcert');
}
$errors += $this->element->validate_form_elements($data, $files);
return $errors;
}
}

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the form for handling the layout of the customcert instance.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
@ -34,12 +42,12 @@ require_once($CFG->dirroot . '/mod/customcert/includes/colourpicker.php');
class edit_form extends \moodleform {
/**
* The id of the template being used.
* @var int The id of the template being used.
*/
protected $tid = null;
/**
* The total number of pages for this cert.
* @var int The total number of pages for this cert.
*/
protected $numpages = 1;
@ -47,11 +55,11 @@ class edit_form extends \moodleform {
* Form definition.
*/
public function definition() {
global $DB;
global $DB, $OUTPUT;
$mform =& $this->_form;
$mform->addElement('text', 'name', get_string('name', 'customcert'));
$mform->addElement('text', 'name', get_string('name', 'customcert'), 'maxlength="255"');
$mform->setType('name', PARAM_TEXT);
$mform->addRule('name', null, 'required');
@ -72,16 +80,23 @@ class edit_form extends \moodleform {
$this->add_customcert_page_elements($page);
}
$mform->closeHeaderBefore('addcertpage');
$mform->addElement('submit', 'addcertpage', get_string('addcertpage', 'customcert'));
$mform->closeHeaderBefore('submitbtn');
// Link to add another page.
$addpagelink = new \moodle_url('/mod/customcert/edit.php',
array(
'tid' => $this->tid,
'aid' => 1,
'action' => 'addpage',
'sesskey' => sesskey()
)
);
$icon = $OUTPUT->pix_icon('t/switch_plus', get_string('addcertpage', 'customcert'));
$addpagehtml = \html_writer::link($addpagelink, $icon . get_string('addcertpage', 'customcert'));
$mform->addElement('html', \html_writer::tag('div', $addpagehtml, array('class' => 'addpage')));
// Add the submit buttons.
$group = array();
$group[] = $mform->createElement('submit', 'submitbtn', get_string('savechanges'));
$group[] = $mform->createElement('submit', 'previewbtn', get_string('savechangespreview', 'customcert'));
$group[] = $mform->createElement('submit', 'previewbtn', get_string('savechangespreview', 'customcert'), array(), false);
$mform->addElement('group', 'submitbtngroup', '', $group, '', false);
$mform->addElement('hidden', 'tid');
@ -123,20 +138,15 @@ class edit_form extends \moodleform {
/**
* Some basic validation.
*
* @param $data
* @param $files
* @param array $data
* @param array $files
* @return array the errors that were found
*/
public function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
// Check that the template name does not already exist for another template.
if ($template = $DB->get_record('customcert_templates', array('name' => $data['name']))) {
if (empty($data['tid']) || $template->id != $data['tid']) {
$errors['name'] = get_string('customcertnameexists', 'customcert');
}
if (\core_text::strlen($data['name']) > 255) {
$errors['name'] = get_string('nametoolong', 'customcert');
}
// Go through the data and check any width, height or margin values.
@ -190,9 +200,9 @@ class edit_form extends \moodleform {
}
$editlink = '/mod/customcert/edit.php';
$editlinkparams = array('tid' => $this->tid);
$editlinkparams = array('tid' => $this->tid, 'sesskey' => sesskey());
$editelementlink = '/mod/customcert/edit_element.php';
$editelementlinkparams = array('tid' => $this->tid);
$editelementlinkparams = array('tid' => $this->tid, 'sesskey' => sesskey());
// Place the ordering arrows.
// Only display the move up arrow if it is not the first.
@ -228,17 +238,13 @@ class edit_form extends \moodleform {
$mform->setDefault('pagerightmargin_' . $page->id, 0);
$mform->addHelpButton('pagerightmargin_' . $page->id, 'rightmargin', 'customcert');
$group = array();
$group[] = $mform->createElement('select', 'element_' . $page->id, '', element::get_available_types());
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'));
$mform->addElement('group', 'elementgroup', '', $group, '', false);
// Check if there are elements to add.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) {
// Get the total number of elements.
$numelements = count($elements);
// Create a table to display these elements.
$table = new \html_table();
$table->attributes = array('class' => 'generaltable elementstable');
$table->head = array(get_string('name', 'customcert'), get_string('type', 'customcert'), '');
$table->align = array('left', 'left', 'left');
// Loop through and add the elements to the table.
@ -252,11 +258,13 @@ class edit_form extends \moodleform {
// Link to edit this element.
$link = new \moodle_url($editelementlink, $editelementlinkparams + array('id' => $element->id,
'action' => 'edit'));
$icons = $OUTPUT->action_icon($link, new \pix_icon('t/edit', get_string('edit')));
$icons = $OUTPUT->action_icon($link, new \pix_icon('t/edit', get_string('edit')), null,
array('class' => 'action-icon edit-icon'));
// Link to delete the element.
$link = new \moodle_url($editlink, $editlinkparams + array('action' => 'deleteelement',
'aid' => $element->id));
$icons .= $OUTPUT->action_icon($link, new \pix_icon('t/delete', get_string('delete')));
$icons .= $OUTPUT->action_icon($link, new \pix_icon('t/delete', get_string('delete')), null,
array('class' => 'action-icon delete-icon'));
// Now display any moving arrows if they are needed.
if ($numelements > 1) {
// Only display the move up arrow if it is not the first.
@ -282,16 +290,23 @@ class edit_form extends \moodleform {
get_string('rearrangeelements', 'customcert'));
// Add the table to the form.
$mform->addElement('static', 'elements_' . $page->id, get_string('elements', 'customcert'), \html_writer::table($table)
. \html_writer::tag( 'div', $link, array('style' => 'text-align:right')));
. \html_writer::tag( 'div', $link));
$mform->addHelpButton('elements_' . $page->id, 'elements', 'customcert');
}
$group = array();
$group[] = $mform->createElement('select', 'element_' . $page->id, '', element_helper::get_available_element_types());
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'),
array(), false);
$mform->addElement('group', 'elementgroup', '', $group, '', false);
// Add option to delete this page if there is more than one page.
if ($this->numpages > 1) {
// Link to delete the element.
// Link to delete the page.
$deletelink = new \moodle_url($editlink, $editlinkparams + array('action' => 'deletepage', 'aid' => $page->id));
$deletelink = \html_writer::tag('a', get_string('deletecertpage', 'customcert'), array('href' => $deletelink->out(false), 'class' => 'deletebutton'));
$mform->addElement('html', \html_writer::tag('div', $deletelink, array('class' => 'deletebutton')));
$icon = $OUTPUT->pix_icon('t/delete', get_string('deletecertpage', 'customcert'));
$deletepagehtml = \html_writer::link($deletelink, $icon . get_string('deletecertpage', 'customcert'));
$mform->addElement('html', \html_writer::tag('div', $deletepagehtml, array('class' => 'deletebutton')));
}
}
}

View file

@ -30,13 +30,77 @@ defined('MOODLE_INTERNAL') || die();
* Class element
*
* All customercert element plugins are based on this class.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class element {
/**
* @var \stdClass $element The data for the element we are adding.
* @var \stdClass $element The data for the element we are adding - do not use, kept for legacy reasons.
*/
public $element;
protected $element;
/**
* @var int The id.
*/
protected $id;
/**
* @var int The page id.
*/
protected $pageid;
/**
* @var string The name.
*/
protected $name;
/**
* @var mixed The data.
*/
protected $data;
/**
* @var string The font name.
*/
protected $font;
/**
* @var int The font size.
*/
protected $fontsize;
/**
* @var string The font colour.
*/
protected $colour;
/**
* @var int The position x.
*/
protected $posx;
/**
* @var int The position y.
*/
protected $posy;
/**
* @var int The width.
*/
protected $width;
/**
* @var int The refpoint.
*/
protected $refpoint;
/**
* @var bool $showposxy Show position XY form elements?
*/
protected $showposxy;
/**
* Constructor.
@ -44,7 +108,122 @@ abstract class element {
* @param \stdClass $element the element data
*/
public function __construct($element) {
$showposxy = get_config('customcert', 'showposxy');
// Keeping this for legacy reasons so we do not break third-party elements.
$this->element = clone($element);
$this->id = $element->id;
$this->pageid = $element->pageid;
$this->name = $element->name;
$this->data = $element->data;
$this->font = $element->font;
$this->fontsize = $element->fontsize;
$this->colour = $element->colour;
$this->posx = $element->posx;
$this->posy = $element->posy;
$this->width = $element->width;
$this->refpoint = $element->refpoint;
$this->showposxy = isset($showposxy) && $showposxy;
}
/**
* Returns the id.
*
* @return int
*/
public function get_id() {
return $this->id;
}
/**
* Returns the page id.
*
* @return int
*/
public function get_pageid() {
return $this->pageid;
}
/**
* Returns the name.
*
* @return int
*/
public function get_name() {
return $this->name;
}
/**
* Returns the data.
*
* @return mixed
*/
public function get_data() {
return $this->data;
}
/**
* Returns the font name.
*
* @return string
*/
public function get_font() {
return $this->font;
}
/**
* Returns the font size.
*
* @return int
*/
public function get_fontsize() {
return $this->fontsize;
}
/**
* Returns the font colour.
*
* @return string
*/
public function get_colour() {
return $this->colour;
}
/**
* Returns the position x.
*
* @return int
*/
public function get_posx() {
return $this->posx;
}
/**
* Returns the position y.
*
* @return int
*/
public function get_posy() {
return $this->posy;
}
/**
* Returns the width.
*
* @return int
*/
public function get_width() {
return $this->width;
}
/**
* Returns the refpoint.
*
* @return int
*/
public function get_refpoint() {
return $this->refpoint;
}
/**
@ -57,7 +236,10 @@ abstract class element {
// Render the common elements.
element_helper::render_form_element_font($mform);
element_helper::render_form_element_colour($mform);
element_helper::render_form_element_position($mform);
if ($this->showposxy) {
element_helper::render_form_element_position($mform);
}
element_helper::render_form_element_width($mform);
}
/**
@ -69,8 +251,18 @@ abstract class element {
public function definition_after_data($mform) {
// Loop through the properties of the element and set the values
// of the corresponding form element, if it exists.
foreach ($this->element as $property => $value) {
if ($mform->elementExists($property)) {
$properties = [
'name' => $this->name,
'font' => $this->font,
'fontsize' => $this->fontsize,
'colour' => $this->colour,
'posx' => $this->posx,
'posy' => $this->posy,
'width' => $this->width,
'refpoint' => $this->refpoint
];
foreach ($properties as $property => $value) {
if (!is_null($value) && $mform->elementExists($property)) {
$element = $mform->getElement($property);
$element->setValue($value);
}
@ -91,7 +283,10 @@ abstract class element {
// Common validation methods.
$errors += element_helper::validate_form_element_colour($data);
$errors += element_helper::validate_form_element_position($data);
if ($this->showposxy) {
$errors += element_helper::validate_form_element_position($data);
}
$errors += element_helper::validate_form_element_width($data);
return $errors;
}
@ -111,15 +306,19 @@ abstract class element {
$element->name = $data->name;
$element->data = $this->save_unique_data($data);
$element->font = (isset($data->font)) ? $data->font : null;
$element->size = (isset($data->size)) ? $data->size : null;
$element->fontsize = (isset($data->fontsize)) ? $data->fontsize : null;
$element->colour = (isset($data->colour)) ? $data->colour : null;
if ($this->showposxy) {
$element->posx = (isset($data->posx)) ? $data->posx : null;
$element->posy = (isset($data->posy)) ? $data->posy : null;
}
$element->width = (isset($data->width)) ? $data->width : null;
$element->refpoint = (isset($data->refpoint)) ? $data->refpoint : null;
$element->timemodified = time();
// Check if we are updating, or inserting a new element.
if (!empty($this->element->id)) { // Must be updating a record in the database.
$element->id = $this->element->id;
if (!empty($this->id)) { // Must be updating a record in the database.
$element->id = $this->id;
return $DB->update_record('customcert_elements', $element);
} else { // Must be adding a new one.
$element->element = $data->element;
@ -176,7 +375,6 @@ abstract class element {
*/
public abstract function render_html();
/**
* Handles deleting any data this element may have introduced.
* Can be overridden if more functionality is needed.
@ -186,7 +384,7 @@ abstract class element {
public function delete() {
global $DB;
return $DB->delete_records('customcert_elements', array('id' => $this->element->id));
return $DB->delete_records('customcert_elements', array('id' => $this->id));
}
/**
@ -198,7 +396,9 @@ abstract class element {
*
* @param \restore_customcert_activity_task $restore
*/
public function after_restore($restore) { }
public function after_restore($restore) {
}
/**
* Magic getter for read only access.
@ -206,66 +406,9 @@ abstract class element {
* @param string $name
*/
public function __get($name) {
debugging('Please call the appropriate get_* function instead of relying on magic getters', DEBUG_DEVELOPER);
if (property_exists($this->element, $name)) {
return $this->element->$name;
}
}
/**
* Returns an instance of the element class.
*
* @param \stdClass $element the element
* @return \mod_customcert\element|bool returns the instance of the element class, or false if element
* class does not exists.
*/
public static function instance($element) {
// Get the class name.
$classname = '\\customcertelement_' . $element->element . '\\element';
// Ensure the necessary class exists.
if (class_exists($classname)) {
return new $classname($element);
}
return false;
}
/**
* Return the list of possible elements to add.
*
* @return array the list of element types that can be used.
*/
public static function get_available_types() {
global $CFG;
// Array to store the element types.
$options = array();
// Check that the directory exists.
$elementdir = "$CFG->dirroot/mod/customcert/element";
if (file_exists($elementdir)) {
// Get directory contents.
$elementfolders = new \DirectoryIterator($elementdir);
// Loop through the elements folder.
foreach ($elementfolders as $elementfolder) {
// If it is not a directory or it is '.' or '..', skip it.
if (!$elementfolder->isDir() || $elementfolder->isDot()) {
continue;
}
// Check that the standard class exists, if not we do
// not want to display it as an option as it will not work.
$foldername = $elementfolder->getFilename();
// Get the class name.
$classname = '\\customcertelement_' . $foldername . '\\element';
// Ensure the necessary class exists.
if (class_exists($classname)) {
$component = "customcertelement_{$foldername}";
$options[$foldername] = get_string('pluginname', $component);
}
}
}
\core_collator::asort($options);
return $options;
}
}

View file

@ -0,0 +1,70 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* Contains the factory class responsible for creating custom certificate instances.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die();
/**
* The factory class responsible for creating custom certificate instances.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class element_factory {
/**
* Returns an instance of the element class.
*
* @param \stdClass $element the element
* @return \mod_customcert\element|bool returns the instance of the element class, or false if element
* class does not exists.
*/
public static function get_element_instance($element) {
// Get the class name.
$classname = '\\customcertelement_' . $element->element . '\\element';
$data = new \stdClass();
$data->id = isset($element->id) ? $element->id : null;
$data->pageid = isset($element->pageid) ? $element->pageid : null;
$data->name = isset($element->name) ? $element->name : get_string('pluginname', 'customcertelement_' . $element->element);
$data->element = $element->element;
$data->data = isset($element->data) ? $element->data : null;
$data->font = isset($element->font) ? $element->font : null;
$data->fontsize = isset($element->fontsize) ? $element->fontsize : null;
$data->colour = isset($element->colour) ? $element->colour : null;
$data->posx = isset($element->posx) ? $element->posx : null;
$data->posy = isset($element->posy) ? $element->posy : null;
$data->width = isset($element->width) ? $element->width : null;
$data->refpoint = isset($element->refpoint) ? $element->refpoint : null;
// Ensure the necessary class exists.
if (class_exists($classname)) {
return new $classname($data);
}
return false;
}
}

View file

@ -15,21 +15,29 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Provides useful functions related to elements.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
* Provides useful functions related to elements.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/grade/constants.php');
require_once($CFG->dirroot . '/grade/lib.php');
require_once($CFG->dirroot . '/grade/querylib.php');
/**
* Class helper.
*
* Provides useful functions related to elements.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class element_helper {
@ -56,15 +64,15 @@ class element_helper {
* @param string $content the content to render
*/
public static function render_content($pdf, $element, $content) {
list($font, $attr) = \mod_customcert\element_helper::get_font($element);
$pdf->setFont($font, $attr, $element->size);
$fontcolour = \TCPDF_COLORS::convertHTMLColorToDec($element->colour, $fontcolour);
list($font, $attr) = self::get_font($element);
$pdf->setFont($font, $attr, $element->get_fontsize());
$fontcolour = \TCPDF_COLORS::convertHTMLColorToDec($element->get_colour(), $fontcolour);
$pdf->SetTextColor($fontcolour['R'], $fontcolour['G'], $fontcolour['B']);
$x = $element->posx;
$y = $element->posy;
$w = $element->width;
$refpoint = $element->refpoint;
$x = $element->get_posx();
$y = $element->get_posy();
$w = $element->get_width();
$refpoint = $element->get_refpoint();
$actualwidth = $pdf->GetStringWidth($content);
if ($w and $w < $actualwidth) {
@ -73,19 +81,19 @@ class element_helper {
switch ($refpoint) {
case self::CUSTOMCERT_REF_POINT_TOPRIGHT:
$x = $element->posx - $actualwidth;
$x = $element->get_posx() - $actualwidth;
if ($x < 0) {
$x = 0;
$w = $element->posx;
$w = $element->get_posx();
} else {
$w = $actualwidth;
}
break;
case self::CUSTOMCERT_REF_POINT_TOPCENTER:
$x = $element->posx - $actualwidth / 2;
$x = $element->get_posx() - $actualwidth / 2;
if ($x < 0) {
$x = 0;
$w = $element->posx * 2;
$w = $element->get_posx() * 2;
} else {
$w = $actualwidth;
}
@ -107,18 +115,18 @@ class element_helper {
* @return string the html
*/
public static function render_html_content($element, $content) {
list($font, $attr) = \mod_customcert\element_helper::get_font($element);
list($font, $attr) = self::get_font($element);
$fontstyle = 'font-family: ' . $font;
if (strpos($attr, 'B') !== false) {
$fontstyle .= ': font-weight: bold';
$fontstyle .= '; font-weight: bold';
}
if (strpos($attr, 'I') !== false) {
$fontstyle .= ': font-style: italic';
$fontstyle .= '; font-style: italic';
}
$style = $fontstyle . '; color: ' . $element->colour . '; font-size: ' . $element->size . 'pt;';
if ($element->width) {
$style .= ' width: ' . $element->width . 'mm';
$style = $fontstyle . '; color: ' . $element->get_colour() . '; font-size: ' . $element->get_fontsize() . 'pt;';
if ($element->get_width()) {
$style .= ' width: ' . $element->get_width() . 'mm';
}
return \html_writer::div($content, '', array('style' => $style));
}
@ -133,10 +141,11 @@ class element_helper {
$mform->setType('font', PARAM_TEXT);
$mform->setDefault('font', 'times');
$mform->addHelpButton('font', 'font', 'customcert');
$mform->addElement('select', 'size', get_string('fontsize', 'customcert'), \mod_customcert\certificate::get_font_sizes());
$mform->setType('size', PARAM_INT);
$mform->setDefault('size', 12);
$mform->addHelpButton('size', 'fontsize', 'customcert');
$mform->addElement('select', 'fontsize', get_string('fontsize', 'customcert'),
\mod_customcert\certificate::get_font_sizes());
$mform->setType('fontsize', PARAM_INT);
$mform->setDefault('fontsize', 12);
$mform->addHelpButton('fontsize', 'fontsize', 'customcert');
}
/**
@ -157,6 +166,22 @@ class element_helper {
* @param \mod_customcert\edit_element_form $mform the edit_form instance.
*/
public static function render_form_element_position($mform) {
$mform->addElement('text', 'posx', get_string('posx', 'customcert'), array('size' => 10));
$mform->setType('posx', PARAM_INT);
$mform->setDefault('posx', 0);
$mform->addHelpButton('posx', 'posx', 'customcert');
$mform->addElement('text', 'posy', get_string('posy', 'customcert'), array('size' => 10));
$mform->setType('posy', PARAM_INT);
$mform->setDefault('posy', 0);
$mform->addHelpButton('posy', 'posy', 'customcert');
}
/**
* Helper function to render the width element.
*
* @param \mod_customcert\edit_element_form $mform the edit_form instance.
*/
public static function render_form_element_width($mform) {
$mform->addElement('text', 'width', get_string('elementwidth', 'customcert'), array('size' => 10));
$mform->setType('width', PARAM_INT);
$mform->setDefault('width', 0);
@ -180,7 +205,7 @@ class element_helper {
public static function validate_form_element_colour($data) {
$errors = array();
// Validate the colour.
if (!\mod_customcert\element_helper::validate_colour($data['colour'])) {
if (!self::validate_colour($data['colour'])) {
$errors['colour'] = get_string('invalidcolour', 'customcert');
}
return $errors;
@ -194,10 +219,33 @@ class element_helper {
*/
public static function validate_form_element_position($data) {
$errors = array();
// Check if posx is not set, or not numeric or less than 0.
if ((!isset($data['posx'])) || (!is_numeric($data['posx'])) || ($data['posx'] < 0)) {
$errors['posx'] = get_string('invalidposition', 'customcert', 'X');
}
// Check if posy is not set, or not numeric or less than 0.
if ((!isset($data['posy'])) || (!is_numeric($data['posy'])) || ($data['posy'] < 0)) {
$errors['posy'] = get_string('invalidposition', 'customcert', 'Y');
}
return $errors;
}
/**
* Helper function to perform validation on the width element.
*
* @param array $data the submitted data
* @return array the validation errors
*/
public static function validate_form_element_width($data) {
$errors = array();
// Check if width is less than 0.
if (isset($data['width']) && $data['width'] < 0) {
$errors['width'] = get_string('invalidelementwidth', 'customcert');
}
return $errors;
}
@ -209,7 +257,7 @@ class element_helper {
*/
public static function get_font($element) {
// Variable for the font.
$font = $element->font;
$font = $element->get_font();
// Get the last two characters of the font name.
$fontlength = strlen($font);
$lastchar = $font[$fontlength - 1];
@ -309,4 +357,255 @@ class element_helper {
return $sequence;
}
/**
* Helper function that returns the course id for this element.
*
* @param int $elementid The element id
* @return int The course id
*/
public static function get_courseid($elementid) {
global $DB, $SITE;
$sql = "SELECT course
FROM {customcert} c
INNER JOIN {customcert_pages} cp
ON c.templateid = cp.templateid
INNER JOIN {customcert_elements} ce
ON cp.id = ce.pageid
WHERE ce.id = :elementid";
// Check if there is a course associated with this element.
if ($course = $DB->get_record_sql($sql, array('elementid' => $elementid))) {
return $course->course;
} else { // Must be in a site template.
return $SITE->id;
}
}
/**
* Return the list of possible elements to add.
*
* @return array the list of element types that can be used.
*/
public static function get_available_element_types() {
global $CFG;
// Array to store the element types.
$options = array();
// Check that the directory exists.
$elementdir = "$CFG->dirroot/mod/customcert/element";
if (file_exists($elementdir)) {
// Get directory contents.
$elementfolders = new \DirectoryIterator($elementdir);
// Loop through the elements folder.
foreach ($elementfolders as $elementfolder) {
// If it is not a directory or it is '.' or '..', skip it.
if (!$elementfolder->isDir() || $elementfolder->isDot()) {
continue;
}
// Check that the standard class exists, if not we do
// not want to display it as an option as it will not work.
$foldername = $elementfolder->getFilename();
// Get the class name.
$classname = '\\customcertelement_' . $foldername . '\\element';
// Ensure the necessary class exists.
if (class_exists($classname)) {
$component = "customcertelement_{$foldername}";
$options[$foldername] = get_string('pluginname', $component);
}
}
}
\core_collator::asort($options);
return $options;
}
/**
* Helper function to return all the grades items for a given course.
*
* @param \stdClass $course The course we want to return the grade items for
* @return array the array of gradeable items in the course
*/
public static function get_grade_items($course) {
global $DB;
// Array to store the grade items.
$modules = array();
// Collect course modules data.
$modinfo = get_fast_modinfo($course);
$mods = $modinfo->get_cms();
$sections = $modinfo->get_section_info_all();
// Create the section label depending on course format.
$sectionlabel = get_string('section');
if ($course->format == 'topics') {
$sectionlabel = get_string('topic');
} else if ($course->format == 'weeks') {
$sectionlabel = get_string('week');
}
// Loop through each course section.
for ($i = 0; $i <= count($sections) - 1; $i++) {
// Confirm the index exists, should always be true.
if (isset($sections[$i])) {
// Get the individual section.
$section = $sections[$i];
// Get the mods for this section.
$sectionmods = explode(",", $section->sequence);
// Loop through the section mods.
foreach ($sectionmods as $sectionmod) {
// Should never happen unless DB is borked.
if (empty($mods[$sectionmod])) {
continue;
}
$mod = $mods[$sectionmod];
$instance = $DB->get_record($mod->modname, array('id' => $mod->instance));
// Get the grade items for this activity.
if ($gradeitems = grade_get_grade_items_for_activity($mod)) {
$moditem = grade_get_grades($course->id, 'mod', $mod->modname, $mod->instance);
$gradeitem = reset($moditem->items);
if (isset($gradeitem->grademax)) {
$modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name;
}
}
}
}
}
if ($gradeitems = \grade_item::fetch_all(['courseid' => $course->id])) {
$arrgradeitems = [];
foreach ($gradeitems as $gi) {
// Skip the course and mod items since we already have them.
if ($gi->itemtype == 'mod' || $gi->itemtype == 'course') {
continue;
}
$arrgradeitems['gradeitem:' . $gi->id] = get_string('gradeitem', 'grades') . ' : ' . $gi->get_name(true);
}
// Alphabetise this.
asort($arrgradeitems);
// Merge results.
$modules = $modules + $arrgradeitems;
}
return $modules;
}
/**
* Helper function to return the grade information for a course for a specified user.
*
* @param int $courseid
* @param int $gradeformat
* @param int $userid
* @return grade_information|bool the grade information, or false if there is none.
*/
public static function get_course_grade_info($courseid, $gradeformat, $userid) {
$courseitem = \grade_item::fetch_course_item($courseid);
if (!$courseitem) {
return false;
}
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid));
return new grade_information(
$courseitem->get_name(),
$grade->finalgrade,
grade_format_gradevalue($grade->finalgrade, $courseitem, true, $gradeformat, $decimals),
$grade->get_dategraded()
);
}
/**
* Helper function to return the grade information for a module for a specified user.
*
* @param int $cmid
* @param int $gradeformat
* @param int $userid
* @return grade_information|bool the grade information, or false if there is none.
*/
public static function get_mod_grade_info($cmid, $gradeformat, $userid) {
global $DB;
if (!$cm = $DB->get_record('course_modules', array('id' => $cmid))) {
return false;
}
if (!$module = $DB->get_record('modules', array('id' => $cm->module))) {
return false;
}
$gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid);
if (empty($gradeitem)) {
return false;
}
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$item = new \grade_item();
$item->gradetype = GRADE_TYPE_VALUE;
$item->courseid = $cm->course;
$itemproperties = reset($gradeitem->items);
foreach ($itemproperties as $key => $value) {
$item->$key = $value;
}
$objgrade = $item->grades[$userid];
$dategraded = null;
if (!empty($objgrade->dategraded)) {
$dategraded = $objgrade->dategraded;
}
return new grade_information(
$item->name,
$objgrade->grade,
grade_format_gradevalue($objgrade->grade, $item, true, $gradeformat, $decimals),
$dategraded
);
}
/**
* Helper function to return the grade information for a grade item for a specified user.
*
* @param int $gradeitemid
* @param int $gradeformat
* @param int $userid
* @return grade_information|bool the grade information, or false if there is none.
*/
public static function get_grade_item_info($gradeitemid, $gradeformat, $userid) {
if (!$gradeitem = \grade_item::fetch(['id' => $gradeitemid])) {
return false;
}
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$grade = new \grade_grade(array('itemid' => $gradeitem->id, 'userid' => $userid));
return new grade_information(
$gradeitem->get_name(),
$grade->finalgrade,
grade_format_gradevalue($grade->finalgrade, $gradeitem, true, $gradeformat, $decimals),
$grade->get_dategraded()
);
}
}

View file

@ -0,0 +1,37 @@
<?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/>.
/**
* Contains the course module instance list viewed event class.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\event;
defined('MOODLE_INTERNAL') || die();
/**
* The course module instance list viewed event class.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed {
}

View file

@ -0,0 +1,45 @@
<?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/>.
/**
* Contains the course module viewed event class.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\event;
defined('MOODLE_INTERNAL') || die();
/**
* The course module viewed event class.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_viewed extends \core\event\course_module_viewed {
/**
* Initialises the event.
*/
protected function init() {
$this->data['objecttable'] = 'customcert';
parent::init();
}
}

View file

@ -27,11 +27,11 @@ defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/externallib.php");
/**
* This is the external API for this tool.
*
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
* This is the external API for this tool.
*
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external extends \external_api {
/**
@ -99,7 +99,7 @@ class external extends \external_api {
}
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($element)) {
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
return $e->save_form_elements($data);
}
@ -159,7 +159,7 @@ class external extends \external_api {
}
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($element)) {
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
return $e->render_html();
}

View file

@ -0,0 +1,108 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* Contains the class that provides a grade object to be used by elements for display purposes.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die();
/**
* The class that provides a grade object to be used by elements for display purposes.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grade_information {
/**
* @var string The grade name.
*/
protected $name;
/**
* @var float The raw grade.
*/
protected $grade;
/**
* @var string The grade to display
*/
protected $displaygrade;
/**
* @var int The date it was graded.
*/
protected $dategraded;
/**
* The constructor.
*
* @param string $name
* @param float $grade
* @param string $displaygrade
* @param int $dategraded
*/
public function __construct($name, $grade, $displaygrade, $dategraded) {
$this->name = $name;
$this->grade = $grade;
$this->displaygrade = $displaygrade;
$this->dategraded = $dategraded;
}
/**
* Returns the name.
*
* @return string
*/
public function get_name() {
return $this->name;
}
/**
* Returns the raw grade.
*
* @return float
*/
public function get_grade() {
return $this->grade;
}
/**
* Returns the display grade.
*
* @return string
*/
public function get_displaygrade() {
return $this->displaygrade;
}
/**
* Returns the date it was graded.
*
* @return int
*/
public function get_dategraded() {
return $this->dategraded;
}
}

View file

@ -14,13 +14,20 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the form for loading customcert templates.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->libdir . '/formslib.php');
/**
* The form for loading customcert templates.
*

View file

@ -0,0 +1,142 @@
<?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/>.
/**
* The table that displays the templates in a given context.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->libdir . '/tablelib.php');
/**
* Class for the table that displays the templates in a given context.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manage_templates_table extends \table_sql {
/**
* @var \context $context
*/
protected $context;
/**
* Sets up the table.
*
* @param \context $context
*/
public function __construct($context) {
parent::__construct('mod_customcert_manage_templates_table');
$columns = [
'name',
'actions'
];
$headers = [
get_string('name'),
''
];
$this->define_columns($columns);
$this->define_headers($headers);
$this->collapsible(false);
$this->sortable(true);
$this->context = $context;
}
/**
* Generate the name column.
*
* @param \stdClass $template
* @return string
*/
public function col_name($template) {
return $template->name;
}
/**
* Generate the actions column.
*
* @param \stdClass $template
* @return string
*/
public function col_actions($template) {
global $OUTPUT;
// Link to edit the template.
$editlink = new \moodle_url('/mod/customcert/edit.php', array('tid' => $template->id));
$editicon = $OUTPUT->action_icon($editlink, new \pix_icon('t/edit', get_string('edit')));
// Link to duplicate the template.
$duplicatelink = new \moodle_url('/mod/customcert/manage_templates.php',
array(
'tid' => $template->id,
'action' => 'duplicate',
'sesskey' => sesskey()
)
);
$duplicateicon = $OUTPUT->action_icon($duplicatelink, new \pix_icon('t/copy', get_string('duplicate')), null,
array('class' => 'action-icon duplicate-icon'));
// Link to delete the template.
$deletelink = new \moodle_url('/mod/customcert/manage_templates.php',
array(
'tid' => $template->id,
'action' => 'delete',
'sesskey' => sesskey()
)
);
$deleteicon = $OUTPUT->action_icon($deletelink, new \pix_icon('t/delete', get_string('delete')), null,
array('class' => 'action-icon delete-icon'));
return $editicon . $duplicateicon . $deleteicon;
}
/**
* Query the reader.
*
* @param int $pagesize size of page for paginated displayed table.
* @param bool $useinitialsbar do you want to use the initials bar.
*/
public function query_db($pagesize, $useinitialsbar = true) {
global $DB;
$total = $DB->count_records('customcert_templates', array('contextid' => $this->context->id));
$this->pagesize($pagesize, $total);
$this->rawdata = $DB->get_records('customcert_templates', array('contextid' => $this->context->id),
$this->get_sql_sort(), '*', $this->get_page_start(), $this->get_page_size());
// Set initial bars.
if ($useinitialsbar) {
$this->initialbars($total > $pagesize);
}
}
}

View file

@ -0,0 +1,178 @@
<?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/>.
/**
* The report that displays the certificates the user has throughout the site.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die;
global $CFG;
require_once($CFG->libdir . '/tablelib.php');
/**
* Class for the report that displays the certificates the user has throughout the site.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class my_certificates_table extends \table_sql {
/**
* @var int $userid The user id
*/
protected $userid;
/**
* Sets up the table.
*
* @param int $userid
* @param string|null $download The file type, null if we are not downloading
*/
public function __construct($userid, $download = null) {
parent::__construct('mod_customcert_report_table');
$columns = array(
'name',
'coursename',
'timecreated',
'code'
);
$headers = array(
get_string('name'),
get_string('course'),
get_string('receiveddate', 'customcert'),
get_string('code', 'customcert')
);
// Check if we were passed a filename, which means we want to download it.
if ($download) {
$this->is_downloading($download, 'customcert-report');
}
if (!$this->is_downloading()) {
$columns[] = 'download';
$headers[] = get_string('file');
}
$this->define_columns($columns);
$this->define_headers($headers);
$this->collapsible(false);
$this->sortable(true);
$this->no_sorting('code');
$this->no_sorting('download');
$this->is_downloadable(true);
$this->userid = $userid;
}
/**
* Generate the name column.
*
* @param \stdClass $certificate
* @return string
*/
public function col_name($certificate) {
return $certificate->name;
}
/**
* Generate the course name column.
*
* @param \stdClass $certificate
* @return string
*/
public function col_coursename($certificate) {
return $certificate->coursename;
}
/**
* Generate the certificate time created column.
*
* @param \stdClass $certificate
* @return string
*/
public function col_timecreated($certificate) {
return userdate($certificate->timecreated);
}
/**
* Generate the code column.
*
* @param \stdClass $certificate
* @return string
*/
public function col_code($certificate) {
return $certificate->code;
}
/**
* Generate the download column.
*
* @param \stdClass $certificate
* @return string
*/
public function col_download($certificate) {
global $OUTPUT;
$icon = new \pix_icon('i/import', get_string('download'));
$link = new \moodle_url('/mod/customcert/my_certificates.php',
array('userid' => $this->userid,
'certificateid' => $certificate->id,
'downloadcert' => '1'));
return $OUTPUT->action_link($link, '', null, null, $icon);
}
/**
* Query the reader.
*
* @param int $pagesize size of page for paginated displayed table.
* @param bool $useinitialsbar do you want to use the initials bar.
*/
public function query_db($pagesize, $useinitialsbar = true) {
$total = certificate::get_number_of_certificates_for_user($this->userid);
$this->pagesize($pagesize, $total);
$this->rawdata = certificate::get_certificates_for_user($this->userid, $this->get_page_start(),
$this->get_page_size(), $this->get_sql_sort());
// Set initial bars.
if ($useinitialsbar) {
$this->initialbars($total > $pagesize);
}
}
/**
* Download the data.
*/
public function download() {
\core\session\manager::write_close();
$total = certificate::get_number_of_certificates_for_user($this->userid);
$this->out($total, false);
exit;
}
}

View file

@ -0,0 +1,46 @@
<?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/>.
/**
* Email certificate as html renderer.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\output\email;
defined('MOODLE_INTERNAL') || die();
/**
* Email certificate as html renderer.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends \mod_customcert\output\renderer {
/**
* The template name for this renderer.
*
* @return string
*/
public function get_template_name() {
return 'email_certificate_html';
}
}

View file

@ -0,0 +1,46 @@
<?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/>.
/**
* Email certificate as text renderer.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\output\email;
defined('MOODLE_INTERNAL') || die();
/**
* Email certificate as text renderer.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer_textemail extends \mod_customcert\output\renderer {
/**
* The template name for this renderer.
*
* @return string
*/
public function get_template_name() {
return 'email_certificate_text';
}
}

View file

@ -0,0 +1,116 @@
<?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/>.
/**
* Email certificate renderable.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\output;
defined('MOODLE_INTERNAL') || die();
/**
* Email certificate renderable.
*
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class email_certificate implements \renderable, \templatable {
/**
* @var bool Are we emailing the student?
*/
public $isstudent;
/**
* @var string The name of the user who owns the certificate.
*/
public $userfullname;
/**
* @var string The course short name.
*/
public $courseshortname;
/**
* @var string The course full name.
*/
public $coursefullname;
/**
* @var int The certificate name.
*/
public $certificatename;
/**
* @var int The course module id.
*/
public $cmid;
/**
* Constructor.
*
* @param bool $isstudent Are we emailing the student?
* @param string $userfullname The name of the user who owns the certificate.
* @param string $courseshortname The short name of the course.
* @param string $coursefullname The full name of the course.
* @param string $certificatename The name of the certificate.
* @param string $cmid The course module id.
*/
public function __construct($isstudent, $userfullname, $courseshortname, $coursefullname, $certificatename, $cmid) {
$this->isstudent = $isstudent;
$this->userfullname = $userfullname;
$this->courseshortname = $courseshortname;
$this->coursefullname = $coursefullname;
$this->certificatename = $certificatename;
$this->cmid = $cmid;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \renderer_base $renderer The render to be used for formatting the email
* @return \stdClass The data ready for use in a mustache template
*/
public function export_for_template(\renderer_base $renderer) {
$data = new \stdClass();
// Used for the body text.
$info = new \stdClass();
$info->userfullname = $this->userfullname;
$info->certificatename = $this->certificatename;
$info->courseshortname = $this->courseshortname;
$info->coursefullname = $this->coursefullname;
if ($this->isstudent) {
$data->emailgreeting = get_string('emailstudentgreeting', 'customcert', $this->userfullname);
$data->emailbody = get_string('emailstudentbody', 'customcert', $info);
$data->emailcertificatelink = new \moodle_url('/mod/customcert/view.php', array('id' => $this->cmid));
$data->emailcertificatetext = get_string('emailstudentcertificatelinktext', 'customcert');
} else {
$data->emailgreeting = get_string('emailnonstudentgreeting', 'customcert');
$data->emailbody = get_string('emailnonstudentbody', 'customcert', $info);
$data->emailcertificatelink = new \moodle_url('/mod/customcert/report.php', array('id' => $this->cmid));
$data->emailcertificatetext = get_string('emailnonstudentcertificatelinktext', 'customcert');
}
return $data;
}
}

View file

@ -0,0 +1,63 @@
<?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/>.
/**
* Contains renderer class.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\output;
defined('MOODLE_INTERNAL') || die();
use plugin_renderer_base;
/**
* Renderer class.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {
/**
* Renders the verify certificate results.
*
* Defer to template.
*
* @param \mod_customcert\output\verify_certificate_results $page
* @return string html for the page
*/
public function render_verify_certificate_results(verify_certificate_results $page) {
$data = $page->export_for_template($this);
return parent::render_from_template('mod_customcert/verify_certificate_results', $data);
}
/**
* Formats the email used to send the certificate by the email_certificate_task.
*
* @param email_certificate $certificate The certificate to email
* @return string
*/
public function render_email_certificate(email_certificate $certificate) {
$data = $certificate->export_for_template($this);
return $this->render_from_template('mod_customcert/' . $this->get_template_name(), $data);
}
}

View file

@ -0,0 +1,96 @@
<?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/>.
/**
* Contains class used to prepare a verification result for display.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\output;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* Class to prepare a verification result for display.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class verify_certificate_result implements templatable, renderable {
/**
* @var string The URL to the user's profile.
*/
public $userprofileurl;
/**
* @var string The user's fullname.
*/
public $userfullname;
/**
* @var string The URL to the course page.
*/
public $courseurl;
/**
* @var string The course's fullname.
*/
public $coursefullname;
/**
* @var string The certificate's name.
*/
public $certificatename;
/**
* Constructor.
*
* @param \stdClass $result
*/
public function __construct($result) {
$this->userprofileurl = new \moodle_url('/user/view.php', array('id' => $result->userid,
'course' => $result->courseid));
$this->userfullname = fullname($result);
$this->courseurl = new \moodle_url('/course/view.php', array('id' => $result->courseid));
$this->coursefullname = $result->coursefullname;
$this->certificatename = $result->certificatename;
}
/**
* Function to export the renderer data in a format that is suitable for a mustache template.
*
* @param \renderer_base $output Used to do a final render of any components that need to be rendered for export.
* @return \stdClass|array
*/
public function export_for_template(\renderer_base $output) {
$result = new \stdClass();
$result->userprofileurl = $this->userprofileurl;
$result->userfullname = $this->userfullname;
$result->coursefullname = $this->coursefullname;
$result->courseurl = $this->courseurl;
$result->certificatename = $this->certificatename;
return $result;
}
}

View file

@ -0,0 +1,89 @@
<?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/>.
/**
* Contains class used to prepare verification results for display.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\output;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* Class to prepare verification results for display.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class verify_certificate_results implements templatable, renderable {
/**
* @var bool Was the code successfully verified?
*/
public $success;
/**
* @var string The message to display.
*/
public $message;
/**
* @var array The certificates issued with the matching code.
*/
public $issues;
/**
* Constructor.
*
* @param \stdClass $result
*/
public function __construct($result) {
$this->success = $result->success;
if ($this->success) {
$this->message = get_string('verified', 'customcert');
} else {
$this->message = get_string('notverified', 'customcert');
}
$this->issues = $result->issues;
}
/**
* Function to export the renderer data in a format that is suitable for a mustache template.
*
* @param \renderer_base $output Used to do a final render of any components that need to be rendered for export.
* @return \stdClass|array
*/
public function export_for_template(\renderer_base $output) {
$result = new \stdClass();
$result->success = $this->success;
$result->message = $this->message;
$result->issues = array();
foreach ($this->issues as $issue) {
$resultissue = new verify_certificate_result($issue);
$result->issues[] = $resultissue->export_for_template($output);
}
return $result;
}
}

View file

@ -30,6 +30,10 @@ defined('MOODLE_INTERNAL') || die();
* Class helper.
*
* Provides useful functions.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class page_helper {

View file

@ -27,6 +27,13 @@ use core\plugininfo\base;
defined('MOODLE_INTERNAL') || die();
/**
* Subplugin info class.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class customcertelement extends base {
/**

View file

@ -86,6 +86,11 @@ class report_table extends \table_sql {
$headers[] = get_string('file');
}
if (!$this->is_downloading() && has_capability('mod/customcert:manage', \context_module::instance($cm->id))) {
$columns[] = 'actions';
$headers[] = '';
}
$this->define_columns($columns);
$this->define_headers($headers);
$this->collapsible(false);
@ -108,7 +113,11 @@ class report_table extends \table_sql {
public function col_fullname($user) {
global $OUTPUT;
return $OUTPUT->user_picture($user) . ' ' . fullname($user);
if (!$this->is_downloading()) {
return $OUTPUT->user_picture($user) . ' ' . fullname($user);
} else {
return fullname($user);
}
}
/**
@ -149,6 +158,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.
*

View file

@ -33,6 +33,6 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity extends \core_search\base_activity {
class activity extends \core_search\area\base_activity {
}

View file

@ -0,0 +1,230 @@
<?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/>.
/**
* A scheduled task for emailing certificates.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert\task;
defined('MOODLE_INTERNAL') || die();
/**
* A scheduled task for emailing certificates.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class email_certificate_task extends \core\task\scheduled_task {
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('taskemailcertificate', 'customcert');
}
/**
* Execute.
*/
public function execute() {
global $DB, $PAGE;
// Get all the certificates that have requested someone get emailed.
$sql = "SELECT c.*, ct.id as templateid, ct.name as templatename, ct.contextid, co.id as courseid,
co.fullname as coursefullname, co.shortname as courseshortname
FROM {customcert} c
JOIN {customcert_templates} ct
ON c.templateid = ct.id
JOIN {course} co
ON c.course = co.id
WHERE (c.emailstudents = :emailstudents
OR c.emailteachers = :emailteachers
OR c.emailothers != '')";
if ($customcerts = $DB->get_records_sql($sql, array('emailstudents' => 1, 'emailteachers' => 1))) {
// The renderers used for sending emails.
$htmlrenderer = $PAGE->get_renderer('mod_customcert', 'email', 'htmlemail');
$textrenderer = $PAGE->get_renderer('mod_customcert', 'email', 'textemail');
foreach ($customcerts as $customcert) {
// Get the context.
$context = \context::instance_by_id($customcert->contextid);
// Get the person we are going to send this email on behalf of.
// Look through the teachers.
if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'u.id ASC',
'', '', '', '', false, true)) {
$teachers = sort_by_roleassignment_authority($teachers, $context);
$userfrom = reset($teachers);
} else { // Ok, no teachers, use administrator name.
$userfrom = get_admin();
}
$courseshortname = format_string($customcert->courseshortname, true, array('context' => $context));
$coursefullname = format_string($customcert->coursefullname, true, array('context' => $context));
$certificatename = format_string($customcert->name, true, array('context' => $context));
// Used to create the email subject.
$info = new \stdClass;
$info->coursename = $courseshortname; // Added for BC, so users who have edited the string don't lose this value.
$info->courseshortname = $courseshortname;
$info->coursefullname = $coursefullname;
$info->certificatename = $certificatename;
// Get a list of all the issues.
$userfields = get_all_user_name_fields(true, 'u');
$sql = "SELECT u.id, u.username, $userfields, u.email, ci.id as issueid, ci.emailed
FROM {customcert_issues} ci
JOIN {user} u
ON ci.userid = u.id
WHERE ci.customcertid = :customcertid";
$issuedusers = $DB->get_records_sql($sql, array('customcertid' => $customcert->id));
// Now, get a list of users who can access the certificate but have not yet.
$enrolledusers = get_enrolled_users(\context_course::instance($customcert->courseid), 'mod/customcert:view');
foreach ($enrolledusers as $enroluser) {
// Check if the user has already been issued.
if (in_array($enroluser->id, array_keys((array) $issuedusers))) {
continue;
}
// Now check if the certificate is not visible to the current user.
$cm = get_fast_modinfo($customcert->courseid, $enroluser->id)->instances['customcert'][$customcert->id];
if (!$cm->uservisible) {
continue;
}
// Don't want to email those with the capability to manage the certificate.
if (has_capability('mod/customcert:manage', $context, $enroluser->id)) {
continue;
}
// Check that they have passed the required time.
if (!empty($customcert->requiredtime)) {
if (\mod_customcert\certificate::get_course_time($customcert->courseid,
$enroluser->id) < ($customcert->requiredtime * 60)) {
continue;
}
}
// Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
$issueid = $DB->get_field('customcert_issues', 'id',
array('userid' => $enroluser->id, 'customcertid' => $customcert->id));
if (empty($issueid)) {
// Ok, issue them the certificate.
$issueid = \mod_customcert\certificate::issue_certificate($customcert->id, $enroluser->id);
}
// Add them to the array so we email them.
$enroluser->issueid = $issueid;
$enroluser->emailed = 0;
$issuedusers[] = $enroluser;
}
// Remove all the users who have already been emailed.
foreach ($issuedusers as $key => $issueduser) {
if ($issueduser->emailed) {
unset($issuedusers[$key]);
}
}
// Now, email the people we need to.
if ($issuedusers) {
foreach ($issuedusers as $user) {
$userfullname = fullname($user);
// Create a directory to store the PDF we will be sending.
$tempdir = make_temp_directory('certificate/attachment');
if (!$tempdir) {
return false;
}
// Now, get the PDF.
$template = new \stdClass();
$template->id = $customcert->templateid;
$template->name = $customcert->templatename;
$template->contextid = $customcert->contextid;
$template = new \mod_customcert\template($template);
$filecontents = $template->generate_pdf(false, $user->id, true);
// Set the name of the file we are going to send.
$filename = $courseshortname . '_' . $certificatename;
$filename = \core_text::entities_to_utf8($filename);
$filename = strip_tags($filename);
$filename = rtrim($filename, '.');
$filename = str_replace('&', '_', $filename) . '.pdf';
// Create the file we will be sending.
$tempfile = $tempdir . '/' . md5(microtime() . $user->id) . '.pdf';
file_put_contents($tempfile, $filecontents);
if ($customcert->emailstudents) {
$renderable = new \mod_customcert\output\email_certificate(true, $userfullname, $courseshortname,
$coursefullname, $certificatename, $customcert->contextid);
$subject = get_string('emailstudentsubject', 'customcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
email_to_user($user, fullname($userfrom), $subject, $message, $messagehtml, $tempfile, $filename);
}
if ($customcert->emailteachers) {
$renderable = new \mod_customcert\output\email_certificate(false, $userfullname, $courseshortname,
$coursefullname, $certificatename, $customcert->contextid);
$subject = get_string('emailnonstudentsubject', 'customcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
foreach ($teachers as $teacher) {
email_to_user($teacher, fullname($userfrom), $subject, $message, $messagehtml, $tempfile,
$filename);
}
}
if (!empty($customcert->emailothers)) {
$others = explode(',', $customcert->emailothers);
foreach ($others as $email) {
$email = trim($email);
if (validate_email($email)) {
$renderable = new \mod_customcert\output\email_certificate(false, $userfullname,
$courseshortname, $coursefullname, $certificatename, $customcert->contextid);
$subject = get_string('emailnonstudentsubject', 'customcert', $info);
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
$emailuser = new \stdClass();
$emailuser->id = -1;
$emailuser->email = $email;
email_to_user($emailuser, fullname($userfrom), $subject, $message, $messagehtml, $tempfile,
$filename);
}
}
}
// Set the field so that it is emailed.
$DB->set_field('customcert_issues', 'emailed', 1, array('id' => $user->issueid));
}
}
}
}
}
}

View file

@ -17,7 +17,8 @@
/**
* Class represents a customcert template.
*
* @copyright 2015 Mark Nelson <markn@moodle.com>
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -28,6 +29,7 @@ defined('MOODLE_INTERNAL') || die();
/**
* Class represents a customcert template.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -70,7 +72,7 @@ class template {
$savedata = new \stdClass();
$savedata->id = $this->id;
$savedata->name = $data->name;
$savedata->timemodified= time();
$savedata->timemodified = time();
$DB->update_record('customcert_templates', $savedata);
}
@ -157,7 +159,7 @@ class template {
if ($elements = $DB->get_records_sql($sql, array('templateid' => $this->id))) {
foreach ($elements as $element) {
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($element)) {
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
$e->delete();
} else {
// The plugin files are missing, so just remove the entry from the DB.
@ -197,7 +199,7 @@ class template {
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) {
foreach ($elements as $element) {
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($element)) {
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
$e->delete();
} else {
// The plugin files are missing, so just remove the entry from the DB.
@ -227,7 +229,7 @@ class template {
$element = $DB->get_record('customcert_elements', array('id' => $elementid), '*', MUST_EXIST);
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($element)) {
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
$e->delete();
} else {
// The plugin files are missing, so just remove the entry from the DB.
@ -248,8 +250,10 @@ class template {
*
* @param bool $preview true if it is a preview, false otherwise
* @param int $userid the id of the user whose certificate we want to view
* @param bool $return Do we want to return the contents of the PDF?
* @return string|void Can return the PDF in string format if specified.
*/
public function generate_pdf($preview = false, $userid = null) {
public function generate_pdf($preview = false, $userid = null, $return = false) {
global $CFG, $DB, $USER;
if (empty($userid)) {
@ -295,16 +299,59 @@ class template {
// Loop through and display.
foreach ($elements as $element) {
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($element)) {
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
$e->render($pdf, $preview, $user);
}
}
}
}
if ($return) {
return $pdf->Output('', 'S');
}
$pdf->Output($filename, 'D');
}
}
/**
* Handles copying this template into another.
*
* @param int $copytotemplateid The template id to copy to
*/
public function copy_to_template($copytotemplateid) {
global $DB;
// Get the pages for the template, there should always be at least one page for each template.
if ($templatepages = $DB->get_records('customcert_pages', array('templateid' => $this->id))) {
// Loop through the pages.
foreach ($templatepages as $templatepage) {
$page = clone($templatepage);
$page->templateid = $copytotemplateid;
$page->timecreated = time();
$page->timemodified = $page->timecreated;
// Insert into the database.
$page->id = $DB->insert_record('customcert_pages', $page);
// Now go through the elements we want to load.
if ($templateelements = $DB->get_records('customcert_elements', array('pageid' => $templatepage->id))) {
foreach ($templateelements as $templateelement) {
$element = clone($templateelement);
$element->pageid = $page->id;
$element->timecreated = time();
$element->timemodified = $element->timecreated;
// Ok, now we want to insert this into the database.
$element->id = $DB->insert_record('customcert_elements', $element);
// Load any other information the element may need to for the template.
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
if (!$e->copy_element($templateelement)) {
// Failed to copy - delete the element.
$e->delete();
}
}
}
}
}
}
}
/**
* Handles moving an item on a template.
*

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the class that handles uploading files.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
@ -21,7 +29,7 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->libdir.'/formslib.php');
/**
* Handles uploading files
* Handles uploading files.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
@ -43,7 +51,8 @@ class upload_image_form extends \moodleform {
'maxbytes' => $CFG->maxbytes,
'subdirs' => 1,
'accepted_types' => 'image');
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions);
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '',
$this->filemanageroptions);
$this->add_action_buttons();
}
@ -56,7 +65,8 @@ class upload_image_form extends \moodleform {
// Editing existing instance - copy existing files into draft area.
$draftitemid = file_get_submitted_draft_itemid('customcertimage');
file_prepare_draft_area($draftitemid, \context_system::instance()->id, 'mod_customcert', 'image', 0, $this->filemanageroptions);
file_prepare_draft_area($draftitemid, \context_system::instance()->id, 'mod_customcert', 'image', 0,
$this->filemanageroptions);
$element = $mform->getElement('customcertimage');
$element->setValue($draftitemid);
}

View file

@ -0,0 +1,68 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* This files contains the form for verifying a certificate.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_customcert;
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->libdir . '/formslib.php');
/**
* The form for verifying a certificate.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class verify_certificate_form extends \moodleform {
/**
* Form definition.
*/
public function definition() {
$mform =& $this->_form;
$mform->addElement('text', 'code', get_string('code', 'customcert'));
$mform->setType('code', PARAM_ALPHANUM);
$mform->addElement('submit', 'verify', get_string('verify', 'customcert'));
}
/**
* Validation.
*
* @param array $data
* @param array $files
* @return array the errors that were found
*/
public function validation($data, $files) {
$errors = array();
if ($data['code'] === '') {
$errors['code'] = get_string('invalidcode', 'customcert');
}
return $errors;
}
}

10
composer.json Normal file
View file

@ -0,0 +1,10 @@
{
"name": "markn86/moodle-mod_customcert",
"type": "moodle-mod",
"require": {
"composer/installers": "~1.0"
},
"extra": {
"installer-name": "customcert"
}
}

View file

@ -52,11 +52,48 @@ $capabilities = array(
'mod/customcert:manage' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE, CONTEXT_SYSTEM,
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
'mod/customcert:viewreport' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
'mod/customcert:viewallcertificates' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
)
),
'mod/customcert:verifycertificate' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
'mod/customcert:verifyallcertificates' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
)
),
);

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/customcert/db" VERSION="20150311" COMMENT="XMLDB file for Moodle mod/customcert"
<XMLDB PATH="mod/customcert/db" VERSION="20170530" COMMENT="XMLDB file for Moodle mod/customcert"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
@ -13,6 +13,10 @@
<FIELD NAME="intro" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="requiredtime" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="verifyany" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="emailstudents" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="emailteachers" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="emailothers" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="protection" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
@ -25,7 +29,7 @@
<TABLE NAME="customcert_templates" COMMENT="Stores each customcert template">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="name" TYPE="char" LENGTH="40" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="References contextid."/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
@ -41,6 +45,7 @@
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="customcertid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="code" TYPE="char" LENGTH="40" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="emailed" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
@ -73,7 +78,7 @@
<FIELD NAME="element" TYPE="text" LENGTH="big" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="data" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="size" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="fontsize" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="colour" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="posx" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="posy" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>

View file

@ -22,6 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$functions = array(
'mod_customcert_save_element' => array(
'classname' => 'mod_customcert\external',

38
db/tasks.php Normal file
View file

@ -0,0 +1,38 @@
<?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/>.
/**
* Definition of customcert scheduled tasks.
*
* @package mod_customcert
* @category task
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$tasks = array(
array(
'classname' => 'mod_customcert\task\email_certificate_task',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*'
)
);

View file

@ -31,5 +31,109 @@ defined('MOODLE_INTERNAL') || die;
* @return bool always true
*/
function xmldb_customcert_upgrade($oldversion) {
global $DB;
$dbman = $DB->get_manager();
if ($oldversion < 2016052306) {
$table = new xmldb_table('customcert_templates');
$field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id');
$dbman->change_field_precision($table, $field);
// Savepoint reached.
upgrade_mod_savepoint(true, 2016052306, 'customcert');
}
if ($oldversion < 2016052308) {
$table = new xmldb_table('customcert');
$field = new xmldb_field('emailstudents', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'requiredtime');
// Conditionally launch add field.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('emailteachers', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'emailstudents');
// Conditionally launch add field.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('emailothers', XMLDB_TYPE_TEXT, null, null, null, null, null, 'emailteachers');
// Conditionally launch add field.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$table = new xmldb_table('customcert_issues');
$field = new xmldb_field('emailed', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'code');
// Conditionally launch add field.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Savepoint reached.
upgrade_mod_savepoint(true, 2016052308, 'customcert');
}
if ($oldversion < 2016052310) {
// Remove any duplicate rows from customcert issue table.
// This SQL fetches the id of those records which have duplicate customcert issues.
// This doesn't return the first issue.
$fromclause = "FROM (
SELECT min(id) AS minid, userid, customcertid
FROM {customcert_issues}
GROUP BY userid, customcertid
) minid
JOIN {customcert_issues} ci
ON ci.userid = minid.userid
AND ci.customcertid = minid.customcertid
AND ci.id > minid.minid";
// Get the records themselves.
$getduplicatessql = "SELECT ci.id $fromclause ORDER BY minid";
if ($records = $DB->get_records_sql($getduplicatessql)) {
// Delete them.
$ids = implode(',', array_keys($records));
$DB->delete_records_select('customcert_issues', "id IN ($ids)");
}
// Savepoint reached.
upgrade_mod_savepoint(true, 2016052310, 'customcert');
}
if ($oldversion < 2016052311) {
// Add column for new 'verifycertificateanyone' setting.
$table = new xmldb_table('customcert');
$field = new xmldb_field('verifyany', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0',
'requiredtime');
// Conditionally launch add field.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Savepoint reached.
upgrade_mod_savepoint(true, 2016052311, 'customcert');
}
if ($oldversion < 2016052314) {
$table = new xmldb_table('customcert_elements');
$field = new xmldb_field('size');
// Rename column as it is a reserved word in Oracle.
if ($dbman->field_exists($table, $field)) {
$field->set_attributes(XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'font');
$dbman->rename_field($table, $field, 'fontsize');
}
// Savepoint reached.
upgrade_mod_savepoint(true, 2016052314, 'customcert');
}
return true;
}

123
edit.php
View file

@ -66,58 +66,74 @@ if ($context->contextlevel == CONTEXT_SYSTEM) {
$PAGE->navbar->add(get_string('editcustomcert', 'customcert'));
}
// Flag to determine if we are deleting anything.
$deleting = false;
if ($tid) {
switch ($action) {
case 'pmoveup' :
$template->move_item('page', $actionid, 'up');
break;
case 'pmovedown' :
$template->move_item('page', $actionid, 'down');
break;
case 'emoveup' :
$template->move_item('element', $actionid, 'up');
break;
case 'emovedown' :
$template->move_item('element', $actionid, 'down');
break;
case 'deletepage' :
if (!empty($confirm)) { // Check they have confirmed the deletion.
$template->delete_page($actionid);
} else {
// Set deletion flag to true.
$deleting = true;
// Create the message.
$message = get_string('deletepageconfirm', 'customcert');
// Create the link options.
$nourl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
$yesurl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid,
'action' => 'deletepage',
'aid' => $actionid,
'confirm' => 1,
'sesskey' => sesskey()));
}
break;
case 'deleteelement' :
if (!empty($confirm)) { // Check they have confirmed the deletion.
$template->delete_element($actionid);
} else {
// Set deletion flag to true.
$deleting = true;
// Create the message.
$message = get_string('deleteelementconfirm', 'customcert');
// Create the link options.
$nourl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
$yesurl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid,
'action' => 'deleteelement',
'aid' => $actionid,
'confirm' => 1,
'sesskey' => sesskey()));
}
break;
if ($action && confirm_sesskey()) {
switch ($action) {
case 'pmoveup' :
$template->move_item('page', $actionid, 'up');
break;
case 'pmovedown' :
$template->move_item('page', $actionid, 'down');
break;
case 'emoveup' :
$template->move_item('element', $actionid, 'up');
break;
case 'emovedown' :
$template->move_item('element', $actionid, 'down');
break;
case 'addpage' :
$template->add_page();
$url = new \moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
redirect($url);
break;
case 'deletepage' :
if (!empty($confirm)) { // Check they have confirmed the deletion.
$template->delete_page($actionid);
$url = new \moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
redirect($url);
} else {
// Set deletion flag to true.
$deleting = true;
// Create the message.
$message = get_string('deletepageconfirm', 'customcert');
// Create the link options.
$nourl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
$yesurl = new moodle_url('/mod/customcert/edit.php',
array(
'tid' => $tid,
'action' => 'deletepage',
'aid' => $actionid,
'confirm' => 1,
'sesskey' => sesskey()
)
);
}
break;
case 'deleteelement' :
if (!empty($confirm)) { // Check they have confirmed the deletion.
$template->delete_element($actionid);
} else {
// Set deletion flag to true.
$deleting = true;
// Create the message.
$message = get_string('deleteelementconfirm', 'customcert');
// Create the link options.
$nourl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
$yesurl = new moodle_url('/mod/customcert/edit.php',
array(
'tid' => $tid,
'action' => 'deleteelement',
'aid' => $actionid,
'confirm' => 1,
'sesskey' => sesskey()
)
);
}
break;
}
}
}
@ -182,11 +198,6 @@ if ($data = $mform->get_data()) {
// Save any page data.
$template->save_page($data);
// Check if we are adding a page.
if (!empty($data->addcertpage)) {
$template->add_page();
}
// Loop through the data.
foreach ($data as $key => $value) {
// Check if they chose to add an element to a page.
@ -210,6 +221,7 @@ if ($data = $mform->get_data()) {
// Check if we want to preview this custom certificate.
if (!empty($data->previewbtn)) {
$template->generate_pdf(true);
exit();
}
// Redirect to the editing page to show form with recent updates.
@ -220,9 +232,10 @@ if ($data = $mform->get_data()) {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('editcustomcert', 'customcert'));
$mform->display();
if ($tid) {
if ($tid && $context->contextlevel == CONTEXT_MODULE) {
$loadtemplateurl = new moodle_url('/mod/customcert/load_template.php', array('tid' => $tid));
$loadtemplateform = new \mod_customcert\load_template_form($loadtemplateurl, array('context' => $context));
$loadtemplateform = new \mod_customcert\load_template_form($loadtemplateurl, array('context' => $context), 'post',
'', array('id' => 'loadtemplateform'));
$loadtemplateform->display();
}
echo $OUTPUT->footer();

View file

@ -86,7 +86,7 @@ if ($data = $mform->get_data()) {
// Set the element variable.
$data->element = $element->element;
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($data)) {
if ($e = \mod_customcert\element_factory::get_element_instance($data)) {
$e->save_form_elements($data);
}

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element background image's core interaction API.
*
* @package customcertelement_bgimage
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_bgimage;
defined('MOODLE_INTERNAL') || die();
@ -33,8 +41,9 @@ class element extends \customcertelement_image\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function render_form_elements($mform) {
$mform->addElement('select', 'image', get_string('image', 'customcertelement_image'), self::get_images());
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions);
$mform->addElement('select', 'fileid', get_string('image', 'customcertelement_image'), self::get_images());
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '',
$this->filemanageroptions);
}
/**
@ -49,24 +58,6 @@ class element extends \customcertelement_image\element {
return array();
}
/**
* This will handle how form data will be saved into the data column in the
* customcert_elements table.
*
* @param \stdClass $data the form data
* @return string the json encoded array
*/
public function save_unique_data($data) {
// Array of data we will be storing in the database.
$arrtostore = array(
'pathnamehash' => $data->image,
'width' => 0,
'height' => 0
);
return json_encode($arrtostore);
}
/**
* Handles rendering the element on the pdf.
*
@ -75,25 +66,30 @@ class element extends \customcertelement_image\element {
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
global $CFG;
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
$data = $this->get_data();
if (empty($data)) {
return;
}
$imageinfo = json_decode($this->element->data);
$imageinfo = json_decode($data);
// Get the image.
$fs = get_file_storage();
if ($file = $fs->get_file_by_hash($imageinfo->pathnamehash)) {
$contenthash = $file->get_contenthash();
$l1 = $contenthash[0] . $contenthash[1];
$l2 = $contenthash[2] . $contenthash[3];
$location = $CFG->dataroot . '/filedir' . '/' . $l1 . '/' . $l2 . '/' . $contenthash;
// If there is no file, we have nothing to display.
if (empty($imageinfo->filename)) {
return;
}
if ($file = $this->get_file()) {
$location = make_request_directory() . '/target';
$file->copy_content_to($location);
// Set the image to the size of the PDF page.
$pdf->Image($location, 0, 0, $pdf->getPageWidth(), $pdf->getPageHeight());
$mimetype = $file->get_mimetype();
if ($mimetype == 'image/svg+xml') {
$pdf->ImageSVG($location, 0, 0, $pdf->getPageWidth(), $pdf->getPageHeight());
} else {
$pdf->Image($location, 0, 0, $pdf->getPageWidth(), $pdf->getPageHeight());
}
}
}
@ -109,19 +105,23 @@ class element extends \customcertelement_image\element {
global $DB;
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
$data = $this->get_data();
if (empty($data)) {
return '';
}
$imageinfo = json_decode($this->element->data);
$imageinfo = json_decode($data);
// Get the image.
$fs = get_file_storage();
if ($file = $fs->get_file_by_hash($imageinfo->pathnamehash)) {
// If there is no file, we have nothing to display.
if (empty($imageinfo->filename)) {
return '';
}
if ($file = $this->get_file()) {
$url = \moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_customcert', 'image', $file->get_itemid(),
$file->get_filepath(), $file->get_filename());
// Get the page we are rendering this on.
$page = $DB->get_record('customcert_pages', array('id' => $this->element->pageid), '*', MUST_EXIST);
$page = $DB->get_record('customcert_pages', array('id' => $this->get_pageid()), '*', MUST_EXIST);
// Set the image to the size of the page.
$style = 'width: ' . $page->width . 'mm; height: ' . $page->height . 'mm';

View file

@ -0,0 +1,66 @@
<?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/>.
/**
* Customcert background image element upgrade code.
*
* @package customcertelement_bgimage
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Customcert background image element upgrade code.
*
* @param int $oldversion the version we are upgrading from
* @return bool always true
*/
function xmldb_customcertelement_bgimage_upgrade($oldversion) {
global $DB;
if ($oldversion < 2016052303) {
// Go through each 'image' element and update the file stored information.
if ($images = $DB->get_records_select('customcert_elements', $DB->sql_compare_text('element') . ' = \'bgimage\'')) {
// Create a file storage instance we are going to use to create pathname hashes.
$fs = get_file_storage();
// Go through and update the details.
foreach ($images as $image) {
// Get the current data we have stored for this element.
$elementinfo = json_decode($image->data);
if ($file = $fs->get_file_by_hash($elementinfo->pathnamehash)) {
$arrtostore = array(
'contextid' => $file->get_contextid(),
'filearea' => $file->get_filearea(),
'itemid' => $file->get_itemid(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
'width' => (int) $elementinfo->width,
'height' => (int) $elementinfo->height
);
$arrtostore = json_encode($arrtostore);
$DB->set_field('customcert_elements', 'data', $arrtostore, array('id' => $image->id));
}
}
}
// Savepoint reached.
upgrade_plugin_savepoint(true, 2016052303, 'customcertelement', 'bgimage');
}
return true;
}

View file

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2016052302; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2016052303; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2016052300; // Requires this Moodle version (3.1).
$plugin->component = 'customcertelement_bgimage';

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element border's core interaction API.
*
* @package customcertelement_border
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_border;
defined('MOODLE_INTERNAL') || die();
@ -50,8 +58,8 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
$colour = \TCPDF_COLORS::convertHTMLColorToDec($this->element->colour, $colour);
$pdf->SetLineStyle(array('width' => $this->element->data, 'color' => $colour));
$colour = \TCPDF_COLORS::convertHTMLColorToDec($this->get_colour(), $colour);
$pdf->SetLineStyle(array('width' => $this->get_data(), 'color' => $colour));
$pdf->Line(0, 0, $pdf->getPageWidth(), 0);
$pdf->Line($pdf->getPageWidth(), 0, $pdf->getPageWidth(), $pdf->getPageHeight());
$pdf->Line(0, $pdf->getPageHeight(), $pdf->getPageWidth(), $pdf->getPageHeight());
@ -98,8 +106,10 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function definition_after_data($mform) {
if (!empty($this->element->data)) {
$this->element->width = $this->element->data;
$data = $this->get_data();
if (!empty($data)) {
$element = $mform->getElement('width');
$element->setValue($data);
}
parent::definition_after_data($mform);
}

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element categoryname's core interaction API.
*
* @package customcertelement_categoryname
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_categoryname;
defined('MOODLE_INTERNAL') || die();
@ -35,7 +43,7 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
\mod_customcert\element_helper::render_content($pdf, $this, self::get_category_name());
\mod_customcert\element_helper::render_content($pdf, $this, self::get_category_name($this->get_id()));
}
/**
@ -47,22 +55,28 @@ class element extends \mod_customcert\element {
* @return string the html
*/
public function render_html() {
return \mod_customcert\element_helper::render_html_content($this, self::get_category_name());
global $COURSE;
return \mod_customcert\element_helper::render_html_content($this, $COURSE->fullname);
}
/**
* Helper function that returns the category name.
*
* @param int $elementid
* @return string
*/
protected static function get_category_name() {
global $DB, $COURSE;
protected static function get_category_name($elementid) {
global $DB, $SITE;
$courseid = \mod_customcert\element_helper::get_courseid($elementid);
$course = get_course($courseid);
// Check that there is a course category available.
if (!empty($COURSE->category)) {
return $DB->get_field('course_categories', 'name', array('id' => $COURSE->category), MUST_EXIST);
if (!empty($course->category)) {
return $DB->get_field('course_categories', 'name', array('id' => $course->category), MUST_EXIST);
} else { // Must be in a site template.
return $COURSE->fullname;
return $SITE->fullname;
}
}
}

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element code's core interaction API.
*
* @package customcertelement_code
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_code;
defined('MOODLE_INTERNAL') || die();
@ -41,11 +49,12 @@ class element extends \mod_customcert\element {
$code = \mod_customcert\certificate::generate_code();
} else {
// Get the page.
$page = $DB->get_record('customcert_pages', array('id' => $this->element->pageid), '*', MUST_EXIST);
$page = $DB->get_record('customcert_pages', array('id' => $this->get_pageid()), '*', MUST_EXIST);
// Get the customcert this page belongs to.
$customcert = $DB->get_record('customcert', array('templateid' => $page->templateid), '*', MUST_EXIST);
// Now we can get the issue for this user.
$issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $customcert->id), '*', MUST_EXIST);
$issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $customcert->id),
'*', MUST_EXIST);
$code = $issue->code;
}

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element coursename's core interaction API.
*
* @package customcertelement_coursename
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_coursename;
defined('MOODLE_INTERNAL') || die();
@ -35,9 +43,10 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
global $COURSE;
$courseid = \mod_customcert\element_helper::get_courseid($this->get_id());
$course = get_course($courseid);
\mod_customcert\element_helper::render_content($pdf, $this, $COURSE->fullname);
\mod_customcert\element_helper::render_content($pdf, $this, $course->fullname);
}
/**

View file

@ -14,10 +14,23 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element date's core interaction API.
*
* @package customcertelement_date
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_date;
defined('MOODLE_INTERNAL') || die();
/**
* Date - Course grade date
*/
define('CUSTOMCERT_DATE_COURSE_GRADE', '0');
/**
* Date - Issue
*/
@ -28,6 +41,16 @@ define('CUSTOMCERT_DATE_ISSUE', '-1');
*/
define('CUSTOMCERT_DATE_COMPLETION', '-2');
/**
* Date - Course start
*/
define('CUSTOMCERT_DATE_COURSE_START', '-3');
/**
* Date - Course end
*/
define('CUSTOMCERT_DATE_COURSE_END', '-4');
require_once($CFG->dirroot . '/lib/grade/constants.php');
/**
@ -45,11 +68,16 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function render_form_elements($mform) {
global $COURSE;
// Get the possible date options.
$dateoptions = array();
$dateoptions[CUSTOMCERT_DATE_ISSUE] = get_string('issueddate', 'customcertelement_date');
$dateoptions[CUSTOMCERT_DATE_COMPLETION] = get_string('completiondate', 'customcertelement_date');
$dateoptions = $dateoptions + \customcertelement_grade\element::get_grade_items();;
$dateoptions[CUSTOMCERT_DATE_COURSE_START] = get_string('coursestartdate', 'customcertelement_date');
$dateoptions[CUSTOMCERT_DATE_COURSE_END] = get_string('courseenddate', 'customcertelement_date');
$dateoptions[CUSTOMCERT_DATE_COURSE_GRADE] = get_string('coursegradedate', 'customcertelement_date');
$dateoptions = $dateoptions + \mod_customcert\element_helper::get_grade_items($COURSE);
$mform->addElement('select', 'dateitem', get_string('dateitem', 'customcertelement_date'), $dateoptions);
$mform->addHelpButton('dateitem', 'dateitem', 'customcertelement_date');
@ -86,15 +114,18 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
global $COURSE, $DB;
global $DB;
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
$data = $this->get_data();
if (empty($data)) {
return;
}
$courseid = \mod_customcert\element_helper::get_courseid($this->id);
// Decode the information stored in the database.
$dateinfo = json_decode($this->element->data);
$dateinfo = json_decode($data);
$dateitem = $dateinfo->dateitem;
$dateformat = $dateinfo->dateformat;
@ -103,33 +134,55 @@ class element extends \mod_customcert\element {
$date = time();
} else {
// Get the page.
$page = $DB->get_record('customcert_pages', array('id' => $this->element->pageid), '*', MUST_EXIST);
$page = $DB->get_record('customcert_pages', array('id' => $this->get_pageid()), '*', MUST_EXIST);
// Get the customcert this page belongs to.
$customcert = $DB->get_record('customcert', array('templateid' => $page->templateid), '*', MUST_EXIST);
// Now we can get the issue for this user.
$issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $customcert->id), '*', MUST_EXIST);
$issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $customcert->id),
'*', MUST_EXIST);
if ($dateitem == CUSTOMCERT_DATE_ISSUE) {
$date = $issue->timecreated;
} else if ($dateitem == CUSTOMCERT_DATE_COMPLETION) {
// Get the enrolment end date.
// Get the last completion date.
$sql = "SELECT MAX(c.timecompleted) as timecompleted
FROM {course_completions} c
WHERE c.userid = :userid
AND c.course = :courseid";
if ($timecompleted = $DB->get_record_sql($sql, array('userid' => $issue->userid, 'courseid' => $COURSE->id))) {
if ($timecompleted = $DB->get_record_sql($sql, array('userid' => $issue->userid, 'courseid' => $courseid))) {
if (!empty($timecompleted->timecompleted)) {
$date = $timecompleted->timecompleted;
}
}
} else if ($dateitem == CUSTOMCERT_DATE_COURSE_START) {
$date = $DB->get_field('course', 'startdate', array('id' => $courseid));
} else if ($dateitem == CUSTOMCERT_DATE_COURSE_END) {
$date = $DB->get_field('course', 'enddate', array('id' => $courseid));
} else {
$gradeitem = new \stdClass();
$gradeitem->gradeitem = $dateitem;
$gradeitem->gradeformat = GRADE_DISPLAY_TYPE_PERCENTAGE;
if ($modinfo = \customcertelement_grade\element::get_grade($gradeitem, $issue->userid)) {
if (!empty($modinfo->dategraded)) {
$date = $modinfo->dategraded;
}
if ($dateitem == CUSTOMCERT_DATE_COURSE_GRADE) {
$grade = \mod_customcert\element_helper::get_course_grade_info(
$courseid,
GRADE_DISPLAY_TYPE_DEFAULT,
$user->id
);
} else if (strpos($dateitem, 'gradeitem:') === 0) {
$gradeitemid = substr($dateitem, 10);
$grade = \mod_customcert\element_helper::get_grade_item_info(
$gradeitemid,
$dateitem,
$user->id
);
} else {
$grade = \mod_customcert\element_helper::get_mod_grade_info(
$dateitem,
GRADE_DISPLAY_TYPE_DEFAULT,
$user->id
);
}
$dategraded = $grade->get_dategraded();
if ($grade && !empty($dategraded)) {
$date = $dategraded;
}
}
}
@ -150,12 +203,13 @@ class element extends \mod_customcert\element {
*/
public function render_html() {
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
return;
$data = $this->get_data();
if (empty($data)) {
return '';
}
// Decode the information stored in the database.
$dateinfo = json_decode($this->element->data);
$dateinfo = json_decode($data);
$dateformat = $dateinfo->dateformat;
return \mod_customcert\element_helper::render_html_content($this, $this->get_date_format_string(time(), $dateformat));
@ -168,10 +222,15 @@ class element extends \mod_customcert\element {
*/
public function definition_after_data($mform) {
// Set the item and format for this element.
if (!empty($this->element->data)) {
$dateinfo = json_decode($this->element->data);
$this->element->dateitem = $dateinfo->dateitem;
$this->element->dateformat = $dateinfo->dateformat;
$data = $this->get_data();
if (!empty($data)) {
$dateinfo = json_decode($data);
$element = $mform->getElement('dateitem');
$element->setValue($dateinfo->dateitem);
$element = $mform->getElement('dateformat');
$element->setValue($dateinfo->dateformat);
}
parent::definition_after_data($mform);
@ -188,10 +247,10 @@ class element extends \mod_customcert\element {
public function after_restore($restore) {
global $DB;
$dateinfo = json_decode($this->element->data);
$dateinfo = json_decode($this->get_data());
if ($newitem = \restore_dbops::get_backup_ids_record($restore->get_restoreid(), 'course_module', $dateinfo->dateitem)) {
$dateinfo->dateitem = $newitem->newitemid;
$DB->set_field('customcert_elements', 'data', self::save_unique_data($dateinfo), array('id' => $this->element->id));
$DB->set_field('customcert_elements', 'data', $this->save_unique_data($dateinfo), array('id' => $this->get_id()));
}
}
@ -201,12 +260,27 @@ class element extends \mod_customcert\element {
* @return array the list of date formats
*/
public static function get_date_formats() {
$dateformats = array();
$dateformats[1] = 'January 1, 2000';
$dateformats[2] = 'January 1st, 2000';
$dateformats[3] = '1 January 2000';
$dateformats[4] = 'January 2000';
$dateformats[5] = get_string('userdateformat', 'customcertelement_date');
$date = time();
$suffix = self::get_ordinal_number_suffix(userdate($date, '%d'));
$dateformats = array(
1 => userdate($date, '%B %d, %Y'),
2 => userdate($date, '%B %d' . $suffix . ', %Y'),
'strftimedate' => userdate($date, get_string('strftimedate', 'langconfig')),
'strftimedatefullshort' => userdate($date, get_string('strftimedatefullshort', 'langconfig')),
'strftimedateshort' => userdate($date, get_string('strftimedateshort', 'langconfig')),
'strftimedatetime' => userdate($date, get_string('strftimedatetime', 'langconfig')),
'strftimedatetimeshort' => userdate($date, get_string('strftimedatetimeshort', 'langconfig')),
'strftimedaydate' => userdate($date, get_string('strftimedaydate', 'langconfig')),
'strftimedaydatetime' => userdate($date, get_string('strftimedaydatetime', 'langconfig')),
'strftimedayshort' => userdate($date, get_string('strftimedayshort', 'langconfig')),
'strftimedaytime' => userdate($date, get_string('strftimedaytime', 'langconfig')),
'strftimemonthyear' => userdate($date, get_string('strftimemonthyear', 'langconfig')),
'strftimerecent' => userdate($date, get_string('strftimerecent', 'langconfig')),
'strftimerecentfull' => userdate($date, get_string('strftimerecentfull', 'langconfig')),
'strftimetime' => userdate($date, get_string('strftimetime', 'langconfig'))
);
return $dateformats;
}
@ -219,22 +293,30 @@ class element extends \mod_customcert\element {
* @return string
*/
protected function get_date_format_string($date, $dateformat) {
switch ($dateformat) {
case 1:
$certificatedate = userdate($date, '%B %d, %Y');
break;
case 2:
$suffix = $this->get_ordinal_number_suffix(userdate($date, '%d'));
$certificatedate = userdate($date, '%B %d' . $suffix . ', %Y');
break;
case 3:
$certificatedate = userdate($date, '%d %B %Y');
break;
case 4:
$certificatedate = userdate($date, '%B %Y');
break;
default:
$certificatedate = userdate($date, get_string('strftimedate', 'langconfig'));
// Keeping for backwards compatibility.
if (is_number($dateformat)) {
switch ($dateformat) {
case 1:
$certificatedate = userdate($date, '%B %d, %Y');
break;
case 2:
$suffix = self::get_ordinal_number_suffix(userdate($date, '%d'));
$certificatedate = userdate($date, '%B %d' . $suffix . ', %Y');
break;
case 3:
$certificatedate = userdate($date, '%d %B %Y');
break;
case 4:
$certificatedate = userdate($date, '%B %Y');
break;
default:
$certificatedate = userdate($date, get_string('strftimedate', 'langconfig'));
}
}
// Ok, so we must have been passed the actual format in the lang file.
if (!isset($certificatedate)) {
$certificatedate = userdate($date, get_string($dateformat, 'langconfig'));
}
return $certificatedate;
@ -247,7 +329,7 @@ class element extends \mod_customcert\element {
* @param int $day the day of the month
* @return string the suffix.
*/
protected function get_ordinal_number_suffix($day) {
protected static function get_ordinal_number_suffix($day) {
if (!in_array(($day % 100), array(11, 12, 13))) {
switch ($day % 10) {
// Handle 1st, 2nd, 3rd.

View file

@ -23,6 +23,9 @@
*/
$string['completiondate'] = 'Completion date';
$string['courseenddate'] = 'Course end date';
$string['coursegradedate'] = 'Course grade date';
$string['coursestartdate'] = 'Course start date';
$string['dateformat'] = 'Date format';
$string['dateformat_help'] = 'This is the format of the date that will be displayed';
$string['dateitem'] = 'Date item';

View file

@ -0,0 +1,340 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* This file contains the customcert element digitial signature's core interaction API.
*
* @package customcertelement_digitalsignature
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_digitalsignature;
defined('MOODLE_INTERNAL') || die();
/**
* The customcert element digital signature's core interaction API.
*
* @package customcertelement_digitalsignature
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class element extends \customcertelement_image\element {
/**
* @var array The file manager options for the certificate.
*/
protected $signaturefilemanageroptions = array();
/**
* Constructor.
*
* @param \stdClass $element the element data
*/
public function __construct($element) {
global $COURSE;
$this->signaturefilemanageroptions = [
'maxbytes' => $COURSE->maxbytes,
'subdirs' => 1,
'accepted_types' => ['.crt']
];
parent::__construct($element);
}
/**
* This function renders the form elements when adding a customcert element.
*
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function render_form_elements($mform) {
$mform->addElement('select', 'fileid', get_string('image', 'customcertelement_image'), self::get_images());
$mform->addElement('select', 'signaturefileid', get_string('digitalsignature', 'customcertelement_digitalsignature'),
self::get_signatures());
$mform->addElement('text', 'signaturename', get_string('signaturename', 'customcertelement_digitalsignature'));
$mform->setType('signaturename', PARAM_TEXT);
$mform->setDefault('signaturename', '');
$mform->addElement('passwordunmask', 'signaturepassword',
get_string('signaturepassword', 'customcertelement_digitalsignature'));
$mform->setType('signaturepassword', PARAM_TEXT);
$mform->setDefault('signaturepassword', '');
$mform->addElement('text', 'signaturelocation', get_string('signaturelocation', 'customcertelement_digitalsignature'));
$mform->setType('signaturelocation', PARAM_TEXT);
$mform->setDefault('signaturelocation', '');
$mform->addElement('text', 'signaturereason', get_string('signaturereason', 'customcertelement_digitalsignature'));
$mform->setType('signaturereason', PARAM_TEXT);
$mform->setDefault('signaturereason', '');
$mform->addElement('text', 'signaturecontactinfo',
get_string('signaturecontactinfo', 'customcertelement_digitalsignature'));
$mform->setType('signaturecontactinfo', PARAM_TEXT);
$mform->setDefault('signaturecontactinfo', '');
$mform->addElement('text', 'width', get_string('width', 'customcertelement_image'), array('size' => 10));
$mform->setType('width', PARAM_INT);
$mform->setDefault('width', 0);
$mform->addHelpButton('width', 'width', 'customcertelement_image');
$mform->addElement('text', 'height', get_string('height', 'customcertelement_image'), array('size' => 10));
$mform->setType('height', PARAM_INT);
$mform->setDefault('height', 0);
$mform->addHelpButton('height', 'height', 'customcertelement_image');
if (get_config('customcert', 'showposxy')) {
\mod_customcert\element_helper::render_form_element_position($mform);
}
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '',
$this->filemanageroptions);
$mform->addElement('filemanager', 'digitalsignature',
get_string('uploaddigitalsignature', 'customcertelement_digitalsignature'), '',
$this->signaturefilemanageroptions);
}
/**
* Handles saving the form elements created by this element.
* Can be overridden if more functionality is needed.
*
* @param \stdClass $data the form data
* @return bool true of success, false otherwise.
*/
public function save_form_elements($data) {
global $COURSE, $SITE;
// Set the context.
if ($COURSE->id == $SITE->id) {
$context = \context_system::instance();
} else {
$context = \context_course::instance($COURSE->id);
}
// Handle file uploads.
\mod_customcert\certificate::upload_files($data->customcertimage, $context->id);
// Handle file certificate uploads.
\mod_customcert\certificate::upload_files($data->digitalsignature, $context->id, 'signature');
return parent::save_form_elements($data);
}
/**
* This will handle how form data will be saved into the data column in the
* customcert_elements table.
*
* @param \stdClass $data the form data
* @return string the json encoded array
*/
public function save_unique_data($data) {
$arrtostore = [
'signaturename' => $data->signaturename,
'signaturepassword' => $data->signaturepassword,
'signaturelocation' => $data->signaturelocation,
'signaturereason' => $data->signaturereason,
'signaturecontactinfo' => $data->signaturecontactinfo,
'width' => !empty($data->width) ? (int) $data->width : 0,
'height' => !empty($data->height) ? (int) $data->height : 0
];
// Array of data we will be storing in the database.
$fs = get_file_storage();
if (!empty($data->fileid)) {
if ($file = $fs->get_file_by_id($data->fileid)) {
$arrtostore += [
'contextid' => $file->get_contextid(),
'filearea' => $file->get_filearea(),
'itemid' => $file->get_itemid(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
];
}
}
if (!empty($data->signaturefileid)) {
if ($signaturefile = $fs->get_file_by_id($data->signaturefileid)) {
$arrtostore += [
'signaturecontextid' => $signaturefile->get_contextid(),
'signaturefilearea' => $signaturefile->get_filearea(),
'signatureitemid' => $signaturefile->get_itemid(),
'signaturefilepath' => $signaturefile->get_filepath(),
'signaturefilename' => $signaturefile->get_filename()
];
}
}
return json_encode($arrtostore);
}
/**
* Handles rendering the element on the pdf.
*
* @param \pdf $pdf the pdf object
* @param bool $preview true if it is a preview, false otherwise
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
// If there is no element data, we have nothing to display.
$data = $this->get_data();
if (empty($data)) {
return;
}
$imageinfo = json_decode($data);
// If there is no file, we have nothing to display.
if (empty($imageinfo->filename)) {
return;
}
// If there is no signature file, we have nothing to display.
if (empty($imageinfo->signaturefilename)) {
return;
}
if ($file = $this->get_file()) {
$location = make_request_directory() . '/target';
$file->copy_content_to($location);
$mimetype = $file->get_mimetype();
if ($mimetype == 'image/svg+xml') {
$pdf->ImageSVG($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
} else {
$pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
}
}
if ($signaturefile = $this->get_signature_file()) {
$location = make_request_directory() . '/target';
$signaturefile->copy_content_to($location);
$info = [
'Name' => $imageinfo->signaturename,
'Location' => $imageinfo->signaturelocation,
'Reason' => $imageinfo->signaturereason,
'ContactInfo' => $imageinfo->signaturecontactinfo
];
$pdf->setSignature('file://' . $location, '', $imageinfo->signaturepassword, '', 2, $info);
$pdf->setSignatureAppearance($this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
}
}
/**
* Sets the data on the form when editing an element.
*
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function definition_after_data($mform) {
global $COURSE, $SITE;
// Set the context.
if ($COURSE->id == $SITE->id) {
$context = \context_system::instance();
} else {
$context = \context_course::instance($COURSE->id);
}
$data = $this->get_data();
if (!empty($data)) {
$imageinfo = json_decode($data);
$element = $mform->getElement('signaturename');
$element->setValue($imageinfo->signaturename);
$element = $mform->getElement('signaturepassword');
$element->setValue($imageinfo->signaturepassword);
$element = $mform->getElement('signaturelocation');
$element->setValue($imageinfo->signaturelocation);
$element = $mform->getElement('signaturereason');
$element->setValue($imageinfo->signaturereason);
$element = $mform->getElement('signaturecontactinfo');
$element->setValue($imageinfo->signaturecontactinfo);
if (!empty($imageinfo->signaturefilename)) {
if ($signaturefile = $this->get_signature_file()) {
$element = $mform->getElement('signaturefileid');
$element->setValue($signaturefile->get_id());
}
}
}
// Editing existing instance - copy existing files into draft area.
$draftitemid = file_get_submitted_draft_itemid('digitalsignature');
file_prepare_draft_area($draftitemid, $context->id, 'mod_customcert', 'signature', 0,
$this->signaturefilemanageroptions);
$element = $mform->getElement('digitalsignature');
$element->setValue($draftitemid);
parent::definition_after_data($mform);
}
/**
* Return the list of possible images to use.
*
* @return array the list of images that can be used
*/
public static function get_signatures() {
global $COURSE;
// Create file storage object.
$fs = get_file_storage();
// The array used to store the digital signatures.
$arrfiles = array();
// Loop through the files uploaded in the system context.
if ($files = $fs->get_area_files(\context_system::instance()->id, 'mod_customcert', 'signature', false,
'filename', false)) {
foreach ($files as $hash => $file) {
$arrfiles[$file->get_id()] = $file->get_filename();
}
}
// Loop through the files uploaded in the course context.
if ($files = $fs->get_area_files(\context_course::instance($COURSE->id)->id, 'mod_customcert', 'signature', false,
'filename', false)) {
foreach ($files as $hash => $file) {
$arrfiles[$file->get_id()] = $file->get_filename();
}
}
\core_collator::asort($arrfiles);
$arrfiles = array('0' => get_string('nosignature', 'customcertelement_digitalsignature')) + $arrfiles;
return $arrfiles;
}
/**
* Fetch stored file.
*
* @return \stored_file|bool stored_file instance if exists, false if not
*/
public function get_signature_file() {
$imageinfo = json_decode($this->get_data());
$fs = get_file_storage();
return $fs->get_file($imageinfo->signaturecontextid, 'mod_customcert', $imageinfo->signaturefilearea,
$imageinfo->signatureitemid, $imageinfo->signaturefilepath, $imageinfo->signaturefilename);
}
}

View file

@ -0,0 +1,33 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* Strings for component 'customcertelement_digitalsignature', language 'en'.
*
* @package customcertelement_digitalsignature
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['digitalsignature'] = 'Digital signature';
$string['nosignature'] = 'No signature';
$string['pluginname'] = 'Digital signature';
$string['signaturename'] = 'Signature name';
$string['signaturepassword'] = 'Signature password';
$string['signaturelocation'] = 'Signature location';
$string['signaturereason'] = 'Signature reason';
$string['signaturecontactinfo'] = 'Signature contact info';
$string['uploaddigitalsignature'] = 'Upload digital signature';

View file

@ -0,0 +1,29 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* This file contains the version information for the digital signature plugin.
*
* @package customcertelement_digitalsignature
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2016052300; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2016052300; // Requires this Moodle version (3.1).
$plugin->component = 'customcertelement_digitalsignature';

View file

@ -14,14 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element grade's core interaction API.
*
* @package customcertelement_grade
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_grade;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/grade/constants.php');
require_once($CFG->dirroot . '/grade/lib.php');
require_once($CFG->dirroot . '/grade/querylib.php');
/**
* Grade - Course
*/
@ -42,18 +46,20 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function render_form_elements($mform) {
global $COURSE;
// Get the grade items we can display.
$gradeitems = array();
$gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade');
$gradeitems = $gradeitems + self::get_grade_items();
$gradeitems = $gradeitems + \mod_customcert\element_helper::get_grade_items($COURSE);
// The grade items.
$mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
$mform->setType('gradeitem', PARAM_INT);
$mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_grade');
// The grade format.
$mform->addElement('select', 'gradeformat', get_string('gradeformat', 'customcertelement_grade'), self::get_grade_format_options());
$mform->addElement('select', 'gradeformat', get_string('gradeformat', 'customcertelement_grade'),
self::get_grade_format_options());
$mform->setType('gradeformat', PARAM_INT);
$mform->addHelpButton('gradeformat', 'gradeformat', 'customcertelement_grade');
@ -86,23 +92,54 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
global $COURSE;
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
$data = $this->get_data();
if (empty($data)) {
return;
}
$courseid = \mod_customcert\element_helper::get_courseid($this->id);
// Decode the information stored in the database.
$gradeinfo = json_decode($this->element->data);
$gradeinfo = json_decode($data);
$gradeitem = $gradeinfo->gradeitem;
$gradeformat = $gradeinfo->gradeformat;
// If we are previewing this certificate then just show a demonstration grade.
if ($preview) {
$courseitem = \grade_item::fetch_course_item($COURSE->id);
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, 2);
// Define how many decimals to display.
$decimals = 2;
if ($gradeinfo->gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$courseitem = \grade_item::fetch_course_item($courseid);
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, $decimals);
} else {
// Get the grade for the grade item.
$grade = self::get_grade($gradeinfo, $user->id);
if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
$grade = \mod_customcert\element_helper::get_course_grade_info(
$courseid,
$gradeformat,
$user->id
);
} else if (strpos($gradeitem, 'gradeitem:') === 0) {
$gradeitemid = substr($gradeitem, 10);
$grade = \mod_customcert\element_helper::get_grade_item_info(
$gradeitemid,
$gradeformat,
$user->id
);
} else {
$grade = \mod_customcert\element_helper::get_mod_grade_info(
$gradeitem,
$gradeformat,
$user->id
);
}
if ($grade) {
$grade = $grade->get_displaygrade();
}
}
\mod_customcert\element_helper::render_content($pdf, $this, $grade);
@ -120,19 +157,22 @@ class element extends \mod_customcert\element {
global $COURSE;
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
return;
$data = $this->get_data();
if (empty($data)) {
return '';
}
// Decode the information stored in the database.
$gradeinfo = json_decode($this->element->data);
$gradeinfo = json_decode($data);
$courseitem = \grade_item::fetch_course_item($COURSE->id);
// Define how many decimals to display.
$decimals = 2;
if ($gradeinfo->gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, $decimals);
return \mod_customcert\element_helper::render_html_content($this, $grade);
@ -145,10 +185,15 @@ class element extends \mod_customcert\element {
*/
public function definition_after_data($mform) {
// Set the item and format for this element.
if (!empty($this->element->data)) {
$gradeinfo = json_decode($this->element->data);
$this->element->gradeitem = $gradeinfo->gradeitem;
$this->element->gradeformat = $gradeinfo->gradeformat;
$data = $this->get_data();
if (!empty($data)) {
$gradeinfo = json_decode($data);
$element = $mform->getElement('gradeitem');
$element->setValue($gradeinfo->gradeitem);
$element = $mform->getElement('gradeformat');
$element->setValue($gradeinfo->gradeformat);
}
parent::definition_after_data($mform);
@ -165,73 +210,13 @@ class element extends \mod_customcert\element {
public function after_restore($restore) {
global $DB;
$gradeinfo = json_decode($this->element->data);
$gradeinfo = json_decode($this->get_data());
if ($newitem = \restore_dbops::get_backup_ids_record($restore->get_restoreid(), 'course_module', $gradeinfo->gradeitem)) {
$gradeinfo->gradeitem = $newitem->newitemid;
$DB->set_field('customcert_elements', 'data', self::save_unique_data($gradeinfo), array('id' => $this->element->id));
$DB->set_field('customcert_elements', 'data', $this->save_unique_data($gradeinfo), array('id' => $this->get_id()));
}
}
/**
* Helper function to return all the grades items for this course.
*
* @return array the array of gradeable items in the course
*/
public static function get_grade_items() {
global $COURSE, $DB;
// Array to store the grade items.
$modules = array();
// Collect course modules data.
$modinfo = get_fast_modinfo($COURSE);
$mods = $modinfo->get_cms();
$sections = $modinfo->get_section_info_all();
// Create the section label depending on course format.
switch ($COURSE->format) {
case 'topics':
$sectionlabel = get_string('topic');
break;
case 'weeks':
$sectionlabel = get_string('week');
break;
default:
$sectionlabel = get_string('section');
break;
}
// Loop through each course section.
for ($i = 0; $i <= count($sections) - 1; $i++) {
// Confirm the index exists, should always be true.
if (isset($sections[$i])) {
// Get the individual section.
$section = $sections[$i];
// Get the mods for this section.
$sectionmods = explode(",", $section->sequence);
// Loop through the section mods.
foreach ($sectionmods as $sectionmod) {
// Should never happen unless DB is borked.
if (empty($mods[$sectionmod])) {
continue;
}
$mod = $mods[$sectionmod];
$instance = $DB->get_record($mod->modname, array('id' => $mod->instance));
// Get the grade items for this activity.
if ($gradeitems = grade_get_grade_items_for_activity($mod)) {
$moditem = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance);
$gradeitem = reset($moditem->items);
if (isset($gradeitem->grademax)) {
$modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name;
}
}
}
}
}
return $modules;
}
/**
* Helper function to return all the possible grade formats.
*
@ -245,84 +230,4 @@ class element extends \mod_customcert\element {
return $gradeformat;
}
/**
* Helper function to return the grade to display.
*
* @param \stdClass $gradeinfo
* @param int $userid
* @return string the grade result
*/
public static function get_grade($gradeinfo, $userid) {
global $COURSE;
// Get the grade information.
$gradeitem = $gradeinfo->gradeitem;
$gradeformat = $gradeinfo->gradeformat;
// Check if we are displaying the course grade.
if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
if ($courseitem = \grade_item::fetch_course_item($COURSE->id)) {
// Set the grade type we want.
$courseitem->gradetype = GRADE_TYPE_VALUE;
$grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid));
$coursegrade = grade_format_gradevalue($grade->finalgrade, $courseitem, true, $gradeformat, 2);
return $coursegrade;
}
} else { // Get the module grade.
if ($modinfo = self::get_mod_grade($gradeitem, $gradeformat, $userid)) {
return $modinfo->gradetodisplay;
}
}
// Only gets here if no grade was retrieved from the DB.
return '';
}
/**
* Helper function to return the grade the user achieved for a specified module.
*
* @param int $moduleid
* @param int $gradeformat
* @param int $userid
* @return \stdClass the grade information
*/
public static function get_mod_grade($moduleid, $gradeformat, $userid) {
global $DB;
$cm = $DB->get_record('course_modules', array('id' => $moduleid), '*', MUST_EXIST);
$module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST);
$gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid);
if (!empty($gradeitem)) {
$item = new \grade_item();
$item->gradetype = GRADE_TYPE_VALUE;
$item->courseid = $cm->course;
$itemproperties = reset($gradeitem->items);
foreach ($itemproperties as $key => $value) {
$item->$key = $value;
}
// Grade for the user.
$grade = $item->grades[$userid]->grade;
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
// Create the object we will be returning.
$modinfo = new \stdClass;
$modinfo->name = $DB->get_field($module->name, 'name', array('id' => $cm->instance));
$modinfo->gradetodisplay = grade_format_gradevalue($grade, $item, true, $gradeformat, $decimals);
if ($grade) {
$modinfo->dategraded = $item->grades[$userid]->dategraded;
} else {
$modinfo->dategraded = time();
}
return $modinfo;
}
return false;
}
}

View file

@ -28,8 +28,8 @@ $string['gradeitem_help'] = 'The grade item you wish to display the grade of.';
$string['gradeformat'] = 'Grade format';
$string['gradeformat_help'] = 'The format you wish to use when displaying the grade.';
$string['gradeitem'] = 'Grade item';
$string['gradepercent'] = 'Percentage Grade';
$string['gradepoints'] = 'Points Grade';
$string['gradeletter'] = 'Letter Grade';
$string['gradepercent'] = 'Percentage';
$string['gradepoints'] = 'Points';
$string['gradeletter'] = 'Letter';
$string['pluginname'] = 'Grade';
$string['previewgrade'] = 'Preview grade';

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element gradeitemname's core interaction API.
*
* @package customcertelement_gradeitemname
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_gradeitemname;
defined('MOODLE_INTERNAL') || die();
@ -33,8 +41,10 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function render_form_elements($mform) {
global $COURSE;
$mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_gradeitemname'),
\customcertelement_grade\element::get_grade_items());
\mod_customcert\element_helper::get_grade_items($COURSE));
$mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_gradeitemname');
parent::render_form_elements($mform);
@ -66,9 +76,10 @@ class element extends \mod_customcert\element {
global $DB;
// Check that the grade item is not empty.
if (!empty($this->element->data)) {
$data = $this->get_data();
if (!empty($data)) {
// Get the course module information.
$cm = $DB->get_record('course_modules', array('id' => $this->element->data), '*', MUST_EXIST);
$cm = $DB->get_record('course_modules', array('id' => $data), '*', MUST_EXIST);
$module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST);
// Get the name of the item.
@ -90,9 +101,10 @@ class element extends \mod_customcert\element {
global $DB;
// Check that the grade item is not empty.
if (!empty($this->element->data)) {
$data = $this->get_data();
if (!empty($data)) {
// Get the course module information.
$cm = $DB->get_record('course_modules', array('id' => $this->element->data), '*', MUST_EXIST);
$cm = $DB->get_record('course_modules', array('id' => $data), '*', MUST_EXIST);
$module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST);
// Get the name of the item.
@ -110,8 +122,10 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function definition_after_data($mform) {
if (!empty($this->element->data)) {
$this->element->gradeitem = $this->element->data;
$data = $this->get_data();
if (!empty($data)) {
$element = $mform->getElement('gradeitem');
$element->setValue($data);
}
parent::definition_after_data($mform);
}

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element image's core interaction API.
*
* @package customcertelement_image
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_image;
defined('MOODLE_INTERNAL') || die();
@ -27,6 +35,9 @@ defined('MOODLE_INTERNAL') || die();
*/
class element extends \mod_customcert\element {
/**
* @var array The file manager options.
*/
protected $filemanageroptions = array();
/**
@ -52,7 +63,7 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function render_form_elements($mform) {
$mform->addElement('select', 'image', get_string('image', 'customcertelement_image'), self::get_images());
$mform->addElement('select', 'fileid', get_string('image', 'customcertelement_image'), self::get_images());
$mform->addElement('text', 'width', get_string('width', 'customcertelement_image'), array('size' => 10));
$mform->setType('width', PARAM_INT);
@ -64,7 +75,12 @@ class element extends \mod_customcert\element {
$mform->setDefault('height', 0);
$mform->addHelpButton('height', 'height', 'customcertelement_image');
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions);
if (get_config('customcert', 'showposxy')) {
\mod_customcert\element_helper::render_form_element_position($mform);
}
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '',
$this->filemanageroptions);
}
/**
@ -89,7 +105,9 @@ class element extends \mod_customcert\element {
}
// Validate the position.
$errors += \mod_customcert\element_helper::validate_form_element_position($data);
if (get_config('customcert', 'showposxy')) {
$errors += \mod_customcert\element_helper::validate_form_element_position($data);
}
return $errors;
}
@ -112,7 +130,7 @@ class element extends \mod_customcert\element {
}
// Handle file uploads.
\mod_customcert\certificate::upload_imagefiles($data->customcertimage, $context->id);
\mod_customcert\certificate::upload_files($data->customcertimage, $context->id);
return parent::save_form_elements($data);
}
@ -125,12 +143,24 @@ class element extends \mod_customcert\element {
* @return string the json encoded array
*/
public function save_unique_data($data) {
// Array of data we will be storing in the database.
$arrtostore = array(
'pathnamehash' => $data->image,
'width' => (int) $data->width,
'height' => (int) $data->height
);
$arrtostore = [
'width' => !empty($data->width) ? (int) $data->width : 0,
'height' => !empty($data->height) ? (int) $data->height : 0
];
if (!empty($data->fileid)) {
// Array of data we will be storing in the database.
$fs = get_file_storage();
if ($file = $fs->get_file_by_id($data->fileid)) {
$arrtostore += [
'contextid' => $file->get_contextid(),
'filearea' => $file->get_filearea(),
'itemid' => $file->get_itemid(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
];
}
}
return json_encode($arrtostore);
}
@ -143,23 +173,29 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
global $CFG;
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
$data = $this->get_data();
if (empty($data)) {
return;
}
$imageinfo = json_decode($this->element->data);
$imageinfo = json_decode($data);
// Get the image.
$fs = get_file_storage();
if ($file = $fs->get_file_by_hash($imageinfo->pathnamehash)) {
$contenthash = $file->get_contenthash();
$l1 = $contenthash[0] . $contenthash[1];
$l2 = $contenthash[2] . $contenthash[3];
$location = $CFG->dataroot . '/filedir' . '/' . $l1 . '/' . $l2 . '/' . $contenthash;
$pdf->Image($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height);
// If there is no file, we have nothing to display.
if (empty($imageinfo->filename)) {
return;
}
if ($file = $this->get_file()) {
$location = make_request_directory() . '/target';
$file->copy_content_to($location);
$mimetype = $file->get_mimetype();
if ($mimetype == 'image/svg+xml') {
$pdf->ImageSVG($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
} else {
$pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
}
}
}
@ -173,15 +209,22 @@ class element extends \mod_customcert\element {
*/
public function render_html() {
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
$data = $this->get_data();
if (empty($data)) {
return '';
}
$imageinfo = json_decode($this->element->data);
$imageinfo = json_decode($data);
// If there is no file, we have nothing to display.
if (empty($imageinfo->filename)) {
return '';
}
// Get the image.
$fs = get_file_storage();
if ($file = $fs->get_file_by_hash($imageinfo->pathnamehash)) {
if ($file = $fs->get_file($imageinfo->contextid, 'mod_customcert', $imageinfo->filearea, $imageinfo->itemid,
$imageinfo->filepath, $imageinfo->filename)) {
$url = \moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_customcert', 'image', $file->get_itemid(),
$file->get_filepath(), $file->get_filename());
$fileimageinfo = $file->get_imageinfo();
@ -217,11 +260,25 @@ class element extends \mod_customcert\element {
global $COURSE, $SITE;
// Set the image, width and height for this element.
if (!empty($this->element->data)) {
$imageinfo = json_decode($this->element->data);
$this->element->image = $imageinfo->pathnamehash;
$this->element->width = $imageinfo->width;
$this->element->height = $imageinfo->height;
$data = $this->get_data();
if (!empty($data)) {
$imageinfo = json_decode($data);
if (!empty($imageinfo->filename)) {
if ($file = $this->get_file()) {
$element = $mform->getElement('fileid');
$element->setValue($file->get_id());
}
}
if (isset($imageinfo->width) && $mform->elementExists('width')) {
$element = $mform->getElement('width');
$element->setValue($imageinfo->width);
}
if (isset($imageinfo->height) && $mform->elementExists('height')) {
$element = $mform->getElement('height');
$element->setValue($imageinfo->height);
}
}
// Set the context.
@ -231,7 +288,6 @@ class element extends \mod_customcert\element {
$context = \context_course::instance($COURSE->id);
}
// Editing existing instance - copy existing files into draft area.
$draftitemid = file_get_submitted_draft_itemid('customcertimage');
file_prepare_draft_area($draftitemid, $context->id, 'mod_customcert', 'image', 0, $this->filemanageroptions);
@ -241,6 +297,43 @@ class element extends \mod_customcert\element {
parent::definition_after_data($mform);
}
/**
* This function is responsible for handling the restoration process of the element.
*
* We will want to update the file's pathname hash.
*
* @param \restore_customcert_activity_task $restore
*/
public function after_restore($restore) {
global $DB;
// Get the current data we have stored for this element.
$elementinfo = json_decode($this->get_data());
// Update the context.
$elementinfo->contextid = \context_course::instance($restore->get_courseid())->id;
// Encode again before saving.
$elementinfo = json_encode($elementinfo);
// Perform the update.
$DB->set_field('customcert_elements', 'data', $elementinfo, array('id' => $this->get_id()));
}
/**
* Fetch stored file.
*
* @return \stored_file|bool stored_file instance if exists, false if not
*/
public function get_file() {
$imageinfo = json_decode($this->get_data());
$fs = get_file_storage();
return $fs->get_file($imageinfo->contextid, 'mod_customcert', $imageinfo->filearea, $imageinfo->itemid,
$imageinfo->filepath, $imageinfo->filename);
}
/**
* Return the list of possible images to use.
*
@ -257,18 +350,19 @@ class element extends \mod_customcert\element {
// Loop through the files uploaded in the system context.
if ($files = $fs->get_area_files(\context_system::instance()->id, 'mod_customcert', 'image', false, 'filename', false)) {
foreach ($files as $hash => $file) {
$arrfiles[$hash] = $file->get_filename();
$arrfiles[$file->get_id()] = $file->get_filename();
}
}
// Loop through the files uploaded in the course context.
if ($files = $fs->get_area_files(\context_course::instance($COURSE->id)->id, 'mod_customcert', 'image', false, 'filename', false)) {
if ($files = $fs->get_area_files(\context_course::instance($COURSE->id)->id, 'mod_customcert', 'image', false,
'filename', false)) {
foreach ($files as $hash => $file) {
$arrfiles[$hash] = $file->get_filename();
$arrfiles[$file->get_id()] = $file->get_filename();
}
}
\core_collator::asort($arrfiles);
$arrfiles = array_merge(array('0' => get_string('noimage', 'customcert')), $arrfiles);
$arrfiles = array('0' => get_string('noimage', 'customcert')) + $arrfiles;
return $arrfiles;
}

View file

@ -0,0 +1,66 @@
<?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/>.
/**
* Customcert image element upgrade code.
*
* @package customcertelement_image
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Customcert image element upgrade code.
*
* @param int $oldversion the version we are upgrading from
* @return bool always true
*/
function xmldb_customcertelement_image_upgrade($oldversion) {
global $DB;
if ($oldversion < 2016052303) {
// Go through each 'image' element and update the file stored information.
if ($images = $DB->get_records_select('customcert_elements', $DB->sql_compare_text('element') . ' = \'image\'')) {
// Create a file storage instance we are going to use to create pathname hashes.
$fs = get_file_storage();
// Go through and update the details.
foreach ($images as $image) {
// Get the current data we have stored for this element.
$elementinfo = json_decode($image->data);
if ($file = $fs->get_file_by_hash($elementinfo->pathnamehash)) {
$arrtostore = array(
'contextid' => $file->get_contextid(),
'filearea' => $file->get_filearea(),
'itemid' => $file->get_itemid(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
'width' => (int) $elementinfo->width,
'height' => (int) $elementinfo->height
);
$arrtostore = json_encode($arrtostore);
$DB->set_field('customcert_elements', 'data', $arrtostore, array('id' => $image->id));
}
}
}
// Savepoint reached.
upgrade_plugin_savepoint(true, 2016052303, 'customcertelement', 'image');
}
return true;
}

View file

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2016052302; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2016052303; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2016052300; // Requires this Moodle version (3.1).
$plugin->component = 'customcertelement_image';

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element studentname's core interaction API.
*
* @package customcertelement_studentname
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_studentname;
defined('MOODLE_INTERNAL') || die();

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element teachername's core interaction API.
*
* @package customcertelement_teachername
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_teachername;
defined('MOODLE_INTERNAL') || die();
@ -63,7 +71,7 @@ class element extends \mod_customcert\element {
public function render($pdf, $preview, $user) {
global $DB;
$teacher = $DB->get_record('user', array('id' => $this->element->data));
$teacher = $DB->get_record('user', array('id' => $this->get_data()));
$teachername = fullname($teacher);
\mod_customcert\element_helper::render_content($pdf, $this, $teachername);
@ -80,7 +88,7 @@ class element extends \mod_customcert\element {
public function render_html() {
global $DB;
$teacher = $DB->get_record('user', array('id' => $this->element->data));
$teacher = $DB->get_record('user', array('id' => $this->get_data()));
$teachername = fullname($teacher);
return \mod_customcert\element_helper::render_html_content($this, $teachername);
@ -113,8 +121,10 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function definition_after_data($mform) {
if (!empty($this->element->data)) {
$this->element->teacher = $this->element->data;
$data = $this->get_data();
if (!empty($data)) {
$element = $mform->getElement('teacher');
$element->setValue($data);
}
parent::definition_after_data($mform);
}

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element text's core interaction API.
*
* @package customcertelement_text
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_text;
defined('MOODLE_INTERNAL') || die();
@ -59,7 +67,7 @@ class element extends \mod_customcert\element {
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
\mod_customcert\element_helper::render_content($pdf, $this, $this->element->data);
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_data());
}
/**
@ -71,7 +79,7 @@ class element extends \mod_customcert\element {
* @return string the html
*/
public function render_html() {
return \mod_customcert\element_helper::render_html_content($this, $this->element->data);
return \mod_customcert\element_helper::render_html_content($this, $this->get_data());
}
/**
@ -80,8 +88,10 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function definition_after_data($mform) {
if (!empty($this->element->data)) {
$this->element->text = $this->element->data;
$data = $this->get_data();
if (!empty($data)) {
$element = $mform->getElement('text');
$element->setValue($data);
}
parent::definition_after_data($mform);
}

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the customcert element userfield's core interaction API.
*
* @package customcertelement_userfield
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_userfield;
defined('MOODLE_INTERNAL') || die();
@ -93,7 +101,7 @@ class element extends \mod_customcert\element {
global $CFG, $DB;
// The user field to display.
$field = $this->element->data;
$field = $this->get_data();
// The value to display on the PDF.
$value = '';
if (is_number($field)) { // Must be a custom user profile field.
@ -124,7 +132,7 @@ class element extends \mod_customcert\element {
global $CFG, $DB, $USER;
// The user field to display.
$field = $this->element->data;
$field = $this->get_data();
// The value to display - we always want to show a value here so it can be repositioned.
$value = $field;
if (is_number($field)) { // Must be a custom user profile field.
@ -156,8 +164,10 @@ class element extends \mod_customcert\element {
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function definition_after_data($mform) {
if (!empty($this->element->data)) {
$this->element->userfield = $this->element->data;
$data = $this->get_data();
if (!empty($data)) {
$element = $mform->getElement('userfield');
$element->setValue($data);
}
parent::definition_after_data($mform);
}

View file

@ -0,0 +1,212 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* This file contains the customcert element userpicture's core interaction API.
*
* @package customcertelement_userpicture
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace customcertelement_userpicture;
defined('MOODLE_INTERNAL') || die();
/**
* The customcert element userpicture's core interaction API.
*
* @package customcertelement_userpicture
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class element extends \mod_customcert\element {
/**
* This function renders the form elements when adding a customcert element.
*
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function render_form_elements($mform) {
$mform->addElement('text', 'width', get_string('width', 'customcertelement_userpicture'), array('size' => 10));
$mform->setType('width', PARAM_INT);
$mform->setDefault('width', 0);
$mform->addHelpButton('width', 'width', 'customcertelement_userpicture');
$mform->addElement('text', 'height', get_string('height', 'customcertelement_userpicture'), array('size' => 10));
$mform->setType('height', PARAM_INT);
$mform->setDefault('height', 0);
$mform->addHelpButton('height', 'height', 'customcertelement_userpicture');
if (get_config('customcert', 'showposxy')) {
\mod_customcert\element_helper::render_form_element_position($mform);
}
}
/**
* Performs validation on the element values.
*
* @param array $data the submitted data
* @param array $files the submitted files
* @return array the validation errors
*/
public function validate_form_elements($data, $files) {
// Array to return the errors.
$errors = array();
// Check if width is not set, or not numeric or less than 0.
if ((!isset($data['width'])) || (!is_numeric($data['width'])) || ($data['width'] < 0)) {
$errors['width'] = get_string('invalidwidth', 'customcertelement_userpicture');
}
// Check if height is not set, or not numeric or less than 0.
if ((!isset($data['height'])) || (!is_numeric($data['height'])) || ($data['height'] < 0)) {
$errors['height'] = get_string('invalidheight', 'customcertelement_userpicture');
}
// Validate the position.
if (get_config('customcert', 'showposxy')) {
$errors += \mod_customcert\element_helper::validate_form_element_position($data);
}
return $errors;
}
/**
* This will handle how form data will be saved into the data column in the
* customcert_elements table.
*
* @param \stdClass $data the form data
* @return string the json encoded array
*/
public function save_unique_data($data) {
// Array of data we will be storing in the database.
$arrtostore = array(
'width' => (int) $data->width,
'height' => (int) $data->height
);
return json_encode($arrtostore);
}
/**
* Handles rendering the element on the pdf.
*
* @param \pdf $pdf the pdf object
* @param bool $preview true if it is a preview, false otherwise
* @param \stdClass $user the user we are rendering this for
*/
public function render($pdf, $preview, $user) {
global $CFG;
// If there is no element data, we have nothing to display.
$data = $this->get_data();
if (empty($data)) {
return;
}
$imageinfo = json_decode($data);
$context = \context_user::instance($user->id);
// Get files in the user icon area.
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'user', 'icon', 0);
// Get the file we want to display.
$file = null;
foreach ($files as $filefound) {
if (!$filefound->is_directory()) {
$file = $filefound;
break;
}
}
// Show image if we found one.
if ($file) {
$location = make_request_directory() . '/target';
$file->copy_content_to($location);
$pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
} else if ($preview) { // Can't find an image, but we are in preview mode then display default pic.
$location = $CFG->dirroot . '/pix/u/f1.png';
$pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
}
}
/**
* Render the element in html.
*
* This function is used to render the element when we are using the
* drag and drop interface to position it.
*
* @return string the html
*/
public function render_html() {
global $PAGE, $USER;
// If there is no element data, we have nothing to display.
$data = $this->get_data();
if (empty($data)) {
return '';
}
$imageinfo = json_decode($data);
// Get the image.
$userpicture = new \user_picture($USER);
$userpicture->size = 1;
$url = $userpicture->get_url($PAGE)->out(false);
// The size of the images to use in the CSS style.
$style = '';
if ($imageinfo->width === 0 && $imageinfo->height === 0) {
// Put this in so code checker doesn't complain.
$style .= '';
} else if ($imageinfo->width === 0) { // Then the height must be set.
$style .= 'width: ' . $imageinfo->height . 'mm; ';
$style .= 'height: ' . $imageinfo->height . 'mm';
} else if ($imageinfo->height === 0) { // Then the width must be set.
$style .= 'width: ' . $imageinfo->width . 'mm; ';
$style .= 'height: ' . $imageinfo->width . 'mm';
} else { // Must both be set.
$style .= 'width: ' . $imageinfo->width . 'mm; ';
$style .= 'height: ' . $imageinfo->height . 'mm';
}
return \html_writer::tag('img', '', array('src' => $url, 'style' => $style));
}
/**
* Sets the data on the form when editing an element.
*
* @param \mod_customcert\edit_element_form $mform the edit_form instance
*/
public function definition_after_data($mform) {
// Set the image, width and height for this element.
$data = $this->get_data();
if (!empty($data)) {
$imageinfo = json_decode($data);
$element = $mform->getElement('width');
$element->setValue($imageinfo->width);
$element = $mform->getElement('height');
$element->setValue($imageinfo->height);
}
parent::definition_after_data($mform);
}
}

View file

@ -0,0 +1,31 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* Strings for component 'customcertelement_userpicture', language 'en'.
*
* @package customcertelement_userpicture
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['height'] = 'Height';
$string['height_help'] = 'Height of the image in mm. If equal to zero, it is automatically calculated.';
$string['invalidheight'] = 'The height has to be a valid number greater than or equal to 0.';
$string['invalidwidth'] = 'The width has to be a valid number greater than or equal to 0.';
$string['pluginname'] = 'User picture';
$string['width'] = 'Width';
$string['width_help'] = 'Width of the image in mm. If equal to zero, it is automatically calculated.';

View file

@ -0,0 +1,29 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* This file contains the version information for the userpicture plugin.
*
* @package customcertelement_userpicture
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2016052300; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2016052300; // Requires this Moodle version (3.1).
$plugin->component = 'customcertelement_userpicture';

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the form element for handling the colour picker.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once("HTML/QuickForm/text.php");
@ -25,41 +33,19 @@ require_once("HTML/QuickForm/text.php");
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_customcert_colourpicker extends HTML_QuickForm_text {
class moodlequickform_customcert_colourpicker extends HTML_QuickForm_text {
/**
* The string for the help icon, if empty then no help icon will be displayed.
* @var string The string for the help icon, if empty then no help icon will be displayed.
*/
public $_helpbutton = '';
/**
* Constructor for the colour picker.
*
* @param string $elementName
* @param string $elementLabel
* @param array $attributes
*/
function __construct($elementName = null, $elementLabel = null, $attributes = null) {
parent::__construct($elementName, $elementLabel, $attributes);
}
/**
* Old school constructor name to support earlier versions of Moodle.
*
* @param string $elementName
* @param string $elementLabel
* @param array $attributes
*/
function MoodleQuickForm_customcert_colourpicker($elementName = null, $elementLabel = null, $attributes = null) {
self::__construct($elementName, $elementLabel, $attributes);
}
/**
* Returns the html string to display this element.
*
* @return string
*/
public function toHtml() {
public function tohtml() {
global $PAGE, $OUTPUT;
$PAGE->requires->js_init_call('M.util.init_colour_picker', array($this->getAttribute('id'), null));
@ -79,7 +65,7 @@ class MoodleQuickForm_customcert_colourpicker extends HTML_QuickForm_text {
*
* @return string html for help button
*/
public function getHelpButton(){
public function gethelpbutton() {
return $this->_helpbutton;
}
}

View file

@ -40,6 +40,13 @@ $pageurl = new moodle_url('/mod/customcert/index.php', array('id' => $course->id
$PAGE->set_pagelayout('incourse');
$PAGE->navbar->add(get_string('modulenameplural', 'customcert'));
// Add the page view to the Moodle log.
$event = \mod_customcert\event\course_module_instance_list_viewed::create(array(
'context' => context_course::instance($course->id)
));
$event->add_record_snapshot('course', $course);
$event->trigger();
// Get the customcerts, if there are none display a notice.
if (!$customcerts = get_all_instances_in_course('customcert', $course)) {
echo $OUTPUT->header();
@ -65,8 +72,8 @@ foreach ($customcerts as $customcert) {
$link = html_writer::tag('a', $customcert->name, array('href' => new moodle_url('/mod/customcert/view.php',
array('id' => $customcert->coursemodule))));
} else {
$link = html_writer::tag('a', $customcert->name, array('class' => 'dimmed', 'href' =>
new moodle_url('/mod/customcert/view.php', array('id' => $customcert->coursemodule))));
$link = html_writer::tag('a', $customcert->name, array('class' => 'dimmed',
'href' => new moodle_url('/mod/customcert/view.php', array('id' => $customcert->coursemodule))));
}
// If we are at a different section then print a horizontal rule.
if ($customcert->section !== $currentsection) {

View file

@ -22,48 +22,72 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['addcertpage'] = 'Add another certificate page';
$string['addcertpage'] = 'Add page';
$string['addelement'] = 'Add element';
$string['awardedto'] = 'Awarded to';
$string['cannotverifyallcertificates'] = 'You do not have the permission to verify all certificates on the site.';
$string['certificate'] = 'Certificate';
$string['code'] = 'Code';
$string['copy'] = 'Copy';
$string['coursetimereq'] = 'Required minutes in course';
$string['coursetimereq_help'] = 'Enter here the minimum amount of time, in minutes, that a student must be logged into the course before they will be able to receive
the certificate.';
$string['createtemplate'] = 'Create template';
$string['customcertnameexists'] = 'A certificate already exists with this name';
$string['customcertreport'] = 'Custom certificate report';
$string['customcert:addinstance'] = 'Add a new custom certificate instance';
$string['customcert:manage'] = 'Manage a custom certificate';
$string['customcert:view'] = 'View a custom certificate';
$string['deletecertpage'] = 'Delete certificate page';
$string['customcert:viewreport'] = 'View course report';
$string['customcert:viewallcertificates'] = 'View all certificates';
$string['customcert:verifyallcertificates'] = 'Verify all certificates on the site';
$string['customcert:verifycertificate'] = 'Verify a certificate';
$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['deleteissuedcertificates'] = 'Delete issued certificates';
$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';
$string['editcustomcert'] = 'Edit custom certificate';
$string['duplicate'] = 'Duplicate';
$string['duplicateconfirm'] = 'Duplicate confirmation';
$string['duplicatetemplateconfirm'] = 'Are you sure you want to duplicate this certificate template?';
$string['editcustomcert'] = 'Edit certificate';
$string['editelement'] = 'Edit element';
$string['edittemplate'] = 'Edit template';
$string['elementname'] = 'Element name';
$string['elementname_help'] = 'This will be the name used to identify this element when editing a custom certificate. For example, you may have multiple images on a
page and will want to distinguish between them quickly when editing the certificate. Note: this will not displayed on the PDF.';
$string['elementname_help'] = 'This will be the name used to identify this element when editing a certificate. Note: this will not displayed on the PDF.';
$string['elements'] = 'Elements';
$string['elements_help'] = 'This is the list of elements that will be displayed on the certificate.
Please note: The elements are rendered in this order. The order can be changed by using the arrows next to each element.';
$string['elementwidth'] = 'Width';
$string['elementwidth_help'] = 'Specify the width of the element - \'0\' means that there is no width constraint.';
$string['emailnonstudentbody'] = 'Attached is the certificate \'{$a->certificatename}\' for \'{$a->userfullname}\' for the course \'{$a->coursefullname}\'.';
$string['emailnonstudentcertificatelinktext'] = 'View certificate report';
$string['emailnonstudentgreeting'] = 'Hi';
$string['emailnonstudentsubject'] = '{$a->coursefullname}: {$a->certificatename}';
$string['emailstudentbody'] = 'Attached is your certificate \'{$a->certificatename}\' for the course \'{$a->coursefullname}\'.';
$string['emailstudentcertificatelinktext'] = 'View certificate';
$string['emailstudentgreeting'] = 'Dear {$a}';
$string['emailstudentsubject'] = '{$a->coursefullname}: {$a->certificatename}';
$string['emailstudents'] = 'Email students';
$string['emailstudents_help'] = 'If set this will email the students a copy of the certificate when it becomes available.';
$string['emailteachers'] = 'Email teachers';
$string['emailteachers_help'] = 'If set this will email the teachers a copy of the certificate when it becomes available.';
$string['emailothers'] = 'Email others';
$string['emailothers_help'] = 'If set this will email the email addresses listed here (separated by a comma) with a copy of the certificate when it becomes available.';
$string['font'] = 'Font';
$string['font_help'] = 'The font used when generating this element.';
$string['fontcolour'] = 'Colour';
$string['fontcolour_help'] = 'The colour of the font.';
$string['fontsize'] = 'Size';
$string['fontsize_help'] = 'The size of the font in points.';
$string['getcustomcert'] = 'Get your custom certificate';
$string['getcustomcert'] = 'Download certificate';
$string['height'] = 'Height';
$string['height_help'] = 'This is the height of the certificate PDF in mm. For reference an A4 piece of paper is 297mm high and a letter is 279mm high.';
$string['invalidcode'] = 'Invalid code supplied.';
$string['invalidcolour'] = 'Invalid colour chosen, please enter a valid HTML colour name, or a six-digit, or three-digit hexadecimal colour.';
$string['invalidelementwidth'] = 'Please enter a positive number.';
$string['invalidposition'] = 'Please select a positive number for position {$a}.';
@ -78,29 +102,38 @@ $string['load'] = 'Load';
$string['loadtemplate'] = 'Load template';
$string['loadtemplatemsg'] = 'Are you sure you wish to load this template? This will remove any existing pages and elements for this certificate.';
$string['managetemplates'] = 'Manage templates';
$string['managetemplatesdesc'] = 'This link will take you to a new screen where you will be able to manage templates used by customcert activities in courses.';
$string['managetemplatesdesc'] = 'This link will take you to a new screen where you will be able to manage templates used by Custom certificate activities in courses.';
$string['modify'] = 'Modify';
$string['modulename'] = 'Custom certificate';
$string['modulenameplural'] = 'Custom certificates';
$string['modulename_help'] = 'This module allows for the dynamic generation of PDF certificates.';
$string['modulename_link'] = 'Custom_certificate_module';
$string['mycertificates'] = 'My certificates';
$string['mycertificatesdescription'] = 'These are the certificates you have been issued by either email or downloading manually.';
$string['name'] = 'Name';
$string['nocustomcerts'] = 'There are no custom certificates for this course';
$string['nametoolong'] = 'You have exceeded the maximum length allowed for the name';
$string['nocustomcerts'] = 'There are no certificates for this course';
$string['noimage'] = 'No image';
$string['notemplates'] = 'No templates';
$string['notissued'] = 'Not issued';
$string['notverified'] = 'Not verified';
$string['options'] = 'Options';
$string['page'] = 'Page {$a}';
$string['pluginadministration'] = 'Custom certificate administration';
$string['pluginname'] = 'Custom certificate';
$string['print'] = 'Print';
$string['portrait'] = 'Portrait';
$string['posx'] = 'Position X';
$string['posx_help'] = 'This is the position in mm from the top left corner you wish the element\'s reference point to locate in the x direction.';
$string['posy'] = 'Position Y';
$string['posy_help'] = 'This is the position in mm from the top left corner you wish the element\'s reference point to locate in the y direction.';
$string['print'] = 'Print';
$string['rearrangeelements'] = 'Reposition elements';
$string['rearrangeelementsheading'] = 'Drag and drop elements to change where they are positioned on the certificate.';
$string['receiveddate'] = 'Received date';
$string['refpoint'] = 'Reference point location';
$string['refpoint_help'] = 'The reference point is the location of an element from which its x and y coordinates are determined. It is indicated by the \'+\' that appears in the centre or corners of the element.';
$string['replacetemplate'] = 'Replace';
$string['requiredtimenotmet'] = 'You must spend at least a minimum of {$a->requiredtime} minutes in the course before you can access this certificate.';
$string['rightmargin'] = 'Right margin';
$string['rightmargin_help'] = 'This is the right margin of the certificate PDF in mm.';
$string['save'] = 'Save';
@ -111,7 +144,12 @@ $string['savetemplate'] = 'Save template';
$string['search:activity'] = 'Custom certificate - activity information';
$string['setprotection'] = 'Set protection';
$string['setprotection_help'] = 'Choose the actions you wish to prevent users from performing on this certificate.';
$string['showposxy'] = 'Show position X and Y';
$string['showposxy_desc'] = 'This will show the X and Y position when editing of an element, allowing the user to accurately specify the location.
This isn\'t required if you plan on solely using the drag and drop interface for this purpose.';
$string['summaryofissue'] = 'Summary of issue';
$string['taskemailcertificate'] = 'Handles emailing certificates.';
$string['templatename'] = 'Template name';
$string['templatenameexists'] = 'That template name is currently in use, please choose another.';
$string['topcenter'] = 'Center';
@ -120,7 +158,16 @@ $string['topright'] = 'Top right';
$string['type'] = 'Type';
$string['uploadimage'] = 'Upload image';
$string['uploadimagedesc'] = 'This link will take you to a new screen where you will be able to upload images. Images uploaded using
this method will be available throughout your site to all users who are able to create a custom certificate.';
$string['viewcustomcertissues'] = 'View {$a} issued custom certificates';
this method will be available throughout your site to all users who are able to create a certificate.';
$string['verified'] = 'Verified';
$string['verify'] = 'Verify';
$string['verifyallcertificates'] = 'Allow verification of all certificates';
$string['verifyallcertificates_desc'] = 'When this setting is enabled any person (including users not logged in) can visit the link \'{$a}\' in order to verify any certificate on the site, rather than having to go to the verification link for each certificate.
Note - this only applies to certificates where \'Allow anyone to verify a certificate\' has been set to \'Yes\' in the certificate settings.';
$string['verifycertificate'] = 'Verify certificate';
$string['verifycertificateanyone'] = 'Allow anyone to verify a certificate';
$string['verifycertificateanyone_help'] = 'This setting enables anyone with the certificate verification link (including users not logged in) to verify a certificate.';
$string['viewcustomcertissues'] = 'View {$a} issued certificates';
$string['width'] = 'Width';
$string['width_help'] = 'This is the width of the certificate PDF in mm. For reference an A4 piece of paper is 210mm wide and a letter is 216mm wide.';

43
lib.php
View file

@ -132,7 +132,8 @@ function customcert_reset_userdata($data) {
FROM {customcert} cert
WHERE cert.course = :courseid";
$DB->delete_records_select('customcert_issues', "customcertid IN ($sql)", array('courseid' => $data->courseid));
$status[] = array('component' => $componentstr, 'item' => get_string('customcertremoved', 'customcert'), 'error' => false);
$status[] = array('component' => $componentstr, 'item' => get_string('deleteissuedcertificates', 'customcert'),
'error' => false);
}
// Updating dates - shift may be negative too.
@ -152,7 +153,7 @@ function customcert_reset_userdata($data) {
*/
function customcert_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'customcertheader', get_string('modulenameplural', 'customcert'));
$mform->addElement('advcheckbox', 'reset_customcert', get_string('deletissuedcustomcerts', 'customcert'));
$mform->addElement('advcheckbox', 'reset_customcert', get_string('deleteissuedcertificates', 'customcert'));
}
/**
@ -206,8 +207,6 @@ function customcert_user_complete($course, $user, $mod, $customcert) {
echo $OUTPUT->box_start();
echo get_string('issued', 'customcert') . ": ";
echo userdate($issue->timecreated);
customcert_print_user_files($customcert->id, $user->id);
echo '<br />';
echo $OUTPUT->box_end();
} else {
print_string('notissuedyet', 'customcert');
@ -234,7 +233,7 @@ function customcert_pluginfile($course, $cm, $context, $filearea, $args, $forced
if ($filearea === 'image') {
if ($context->contextlevel == CONTEXT_MODULE) {
require_login($course, false, $cm);
} else if ($context->contextlevel == CONTEXT_SYSTEM && !has_capability('mod/certificate:manage', $context)) {
} else if ($context->contextlevel == CONTEXT_SYSTEM && !has_capability('mod/customcert:manage', $context)) {
return false;
}
@ -251,6 +250,8 @@ function customcert_pluginfile($course, $cm, $context, $filearea, $args, $forced
}
/**
* The features this activity supports.
*
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
* @uses FEATURE_GROUPMEMBERSONLY
@ -316,7 +317,7 @@ function mod_customcert_output_fragment_editelement($args) {
$element = $DB->get_record('customcert_elements', array('id' => $args['elementid']), '*', MUST_EXIST);
$pageurl = new moodle_url('/mod/customcert/rearrange.php', array('pid' => $element->pageid));
$form = new \mod_customcert\edit_element_form($pageurl, array('element' => $element, 'rearrange' => true));
$form = new \mod_customcert\edit_element_form($pageurl, array('element' => $element));
return $form->render();
}
@ -352,15 +353,39 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na
$customcertnode->add_node($node, $beforekey);
}
if (has_capability('mod/customcert:verifycertificate', $PAGE->cm->context)) {
$node = navigation_node::create(get_string('verifycertificate', 'customcert'),
new moodle_url('/mod/customcert/verify_certificate.php', array('contextid' => $PAGE->cm->context->id)),
navigation_node::TYPE_SETTING, null, 'mod_customcert_verify_certificate',
new pix_icon('t/check', ''));
$customcertnode->add_node($node, $beforekey);
}
return $customcertnode->trim_if_empty();
}
/**
* Add nodes to myprofile page.
*
* @param \core_user\output\myprofile\tree $tree Tree object
* @param stdClass $user user object
* @param bool $iscurrentuser
* @param stdClass $course Course object
* @return bool
*/
function mod_customcert_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
$url = new moodle_url('/mod/customcert/my_certificates.php', array('userid' => $user->id));
$node = new core_user\output\myprofile\node('miscellaneous', 'mycustomcerts',
get_string('mycertificates', 'customcert'), null, $url);
$tree->add_node($node);
}
/**
* Handles editing the 'name' of the element in a list.
*
* @param $itemtype
* @param $itemid
* @param $newvalue
* @param string $itemtype
* @param int $itemid
* @param string $newvalue
* @return \core\output\inplace_editable
*/
function mod_customcert_inplace_editable($itemtype, $itemid, $newvalue) {

View file

@ -42,7 +42,7 @@ if ($cm = $template->get_cm()) {
$template->require_manage();
// Check that they have confirmed they wish to load the template.
if ($confirm) {
if ($confirm && confirm_sesskey()) {
// First, remove all the existing elements and pages.
$sql = "SELECT e.*
FROM {customcert_elements} e
@ -52,7 +52,7 @@ if ($confirm) {
if ($elements = $DB->get_records_sql($sql, array('templateid' => $template->get_id()))) {
foreach ($elements as $element) {
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($element)) {
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
$e->delete();
}
}
@ -61,39 +61,8 @@ if ($confirm) {
// Delete the pages.
$DB->delete_records('customcert_pages', array('templateid' => $template->get_id()));
// Store the current time in a variable.
$time = time();
// Now, get the template data we want to load.
if ($templatepages = $DB->get_records('customcert_pages', array('templateid' => $ltid))) {
// Loop through the pages.
foreach ($templatepages as $templatepage) {
$page = clone($templatepage);
$page->templateid = $tid;
$page->timecreated = $time;
$page->timemodified = $time;
// Insert into the database.
$page->id = $DB->insert_record('customcert_pages', $page);
// Now go through the elements we want to load.
if ($templateelements = $DB->get_records('customcert_elements', array('pageid' => $templatepage->id))) {
foreach ($templateelements as $templateelement) {
$element = clone($templateelement);
$element->pageid = $page->id;
$element->timecreated = $time;
$element->timemodified = $time;
// Ok, now we want to insert this into the database.
$element->id = $DB->insert_record('customcert_elements', $element);
// Load any other information the element may need to for the template.
if ($e = \mod_customcert\element::instance($element)) {
if (!$e->copy_element($templateelement)) {
// Failed to copy - delete the element.
$e->delete();
}
}
}
}
}
}
// Copy the items across.
$loadtemplate->copy_to_template($template->get_id());
// Redirect.
$url = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
@ -104,7 +73,8 @@ if ($confirm) {
$nourl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
$yesurl = new moodle_url('/mod/customcert/load_template.php', array('tid' => $tid,
'ltid' => $ltid,
'confirm' => 1));
'confirm' => 1,
'sesskey' => sesskey()));
$pageurl = new moodle_url('/mod/customcert/load_template.php', array('tid' => $tid, 'ltid' => $ltid));
\mod_customcert\page_helper::page_setup($pageurl, $template->get_context(), get_string('loadtemplate', 'customcert'));

View file

@ -27,6 +27,8 @@ require_once('../../config.php');
$contextid = optional_param('contextid', context_system::instance()->id, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$confirm = optional_param('confirm', 0, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 10, PARAM_INT);
if ($action) {
$tid = required_param('tid', PARAM_INT);
@ -51,65 +53,74 @@ $pageurl = new moodle_url('/mod/customcert/manage_templates.php');
// Additional page setup.
$PAGE->navbar->add(get_string('managetemplates', 'customcert'));
// Check if we are deleting a template.
if ($tid) {
if ($action == 'delete') {
if (!$confirm) {
$nourl = new moodle_url('/mod/customcert/manage_templates.php');
$yesurl = new moodle_url('/mod/customcert/manage_templates.php', array('tid' => $tid,
'action' => 'delete',
if ($action && confirm_sesskey()) {
$nourl = new moodle_url('/mod/customcert/manage_templates.php');
$yesurl = new moodle_url('/mod/customcert/manage_templates.php',
array(
'tid' => $tid,
'action' => $action,
'confirm' => 1,
'sesskey' => sesskey()));
'sesskey' => sesskey()
)
);
// Show a confirmation page.
$strheading = get_string('deleteconfirm', 'customcert');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
$message = get_string('deletetemplateconfirm', 'customcert');
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
// Check if we are deleting a template.
if ($action == 'delete') {
if (!$confirm) {
// Show a confirmation page.
$strheading = get_string('deleteconfirm', 'customcert');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
$message = get_string('deletetemplateconfirm', 'customcert');
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
}
// Delete the template.
$template->delete();
// Redirect back to the manage templates page.
redirect(new moodle_url('/mod/customcert/manage_templates.php'));
} else if ($action == 'duplicate') {
if (!$confirm) {
// Show a confirmation page.
$strheading = get_string('duplicateconfirm', 'customcert');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
$message = get_string('duplicatetemplateconfirm', 'customcert');
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
}
// Create another template to copy the data to.
$newtemplate = new \stdClass();
$newtemplate->name = $template->get_name() . ' (' . strtolower(get_string('duplicate', 'customcert')) . ')';
$newtemplate->contextid = $template->get_contextid();
$newtemplate->timecreated = time();
$newtemplate->timemodified = $newtemplate->timecreated;
$newtemplateid = $DB->insert_record('customcert_templates', $newtemplate);
// Copy the data to the new template.
$template->copy_to_template($newtemplateid);
// Redirect back to the manage templates page.
redirect(new moodle_url('/mod/customcert/manage_templates.php'));
}
// Delete the template.
$template->delete();
// Redirect back to the manage templates page.
redirect(new moodle_url('/mod/customcert/manage_templates.php'));
}
}
// Get all the templates that are available.
if ($templates = $DB->get_records('customcert_templates', array('contextid' => $contextid), 'timecreated DESC')) {
// Create a table to display these elements.
$table = new html_table();
$table->head = array(get_string('name', 'customcert'), '');
$table->align = array('left', 'center');
foreach ($templates as $template) {
// Link to edit the element.
$editlink = new \moodle_url('/mod/customcert/edit.php', array('tid' => $template->id));
$editicon = $OUTPUT->action_icon($editlink, new \pix_icon('t/edit', get_string('edit')));
// Link to delete the element.
$deletelink = new \moodle_url('/mod/customcert/manage_templates.php', array('tid' => $template->id,
'action' => 'delete'));
$deleteicon = $OUTPUT->action_icon($deletelink, new \pix_icon('t/delete', get_string('delete')));
$row = new html_table_row();
$row->cells[] = $template->name;
$row->cells[] = $editicon . $deleteicon;
$table->data[] = $row;
}
}
$table = new \mod_customcert\manage_templates_table($context);
$table->define_baseurl($pageurl);
echo $OUTPUT->header();
if (isset($table)) {
echo html_writer::table($table);
} else {
echo html_writer::tag('div', get_string('notemplates', 'customcert'), array('class' => 'alert'));
}
$table->out($perpage, false);
$url = new moodle_url('/mod/customcert/edit.php?contextid=' . $contextid);
echo $OUTPUT->single_button($url, get_string('createtemplate', 'customcert'), 'get');
echo $OUTPUT->footer();

View file

@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the instance add/edit form.
*
* @package mod_customcert
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->dirroot.'/course/moodleform_mod.php');
@ -49,11 +57,28 @@ class mod_customcert_mod_form extends moodleform_mod {
$mform->addElement('header', 'options', get_string('options', 'customcert'));
$mform->addElement('selectyesno', 'emailstudents', get_string('emailstudents', 'customcert'));
$mform->setType('emailstudents', 0);
$mform->addHelpButton('emailstudents', 'emailstudents', 'customcert');
$mform->addElement('selectyesno', 'emailteachers', get_string('emailteachers', 'customcert'));
$mform->setDefault('emailteachers', 0);
$mform->addHelpButton('emailteachers', 'emailteachers', 'customcert');
$mform->addElement('text', 'emailothers', get_string('emailothers', 'customcert'), array('size' => '40'));
$mform->setType('emailothers', PARAM_TEXT);
$mform->addHelpButton('emailothers', 'emailothers', 'customcert');
$mform->addElement('selectyesno', 'verifyany', get_string('verifycertificateanyone', 'customcert'));
$mform->setType('verifyany', 0);
$mform->addHelpButton('verifyany', 'verifycertificateanyone', 'customcert');
$mform->addElement('text', 'requiredtime', get_string('coursetimereq', 'customcert'), array('size' => '3'));
$mform->setType('requiredtime', PARAM_INT);
$mform->addHelpButton('requiredtime', 'coursetimereq', 'customcert');
$mform->addElement('checkbox', 'protection_print', get_string('setprotection', 'customcert'), get_string('print', 'customcert'));
$mform->addElement('checkbox', 'protection_print', get_string('setprotection', 'customcert'),
get_string('print', 'customcert'));
$mform->addElement('checkbox', 'protection_modify', '', get_string('modify', 'customcert'));
$mform->addElement('checkbox', 'protection_copy', '', get_string('copy', 'customcert'));
$mform->addHelpButton('protection_print', 'setprotection', 'customcert');

80
my_certificates.php Normal file
View file

@ -0,0 +1,80 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* Handles viewing the certificates for a certain user.
*
* @package mod_customcert
* @copyright 2016 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
$userid = optional_param('userid', $USER->id, PARAM_INT);
$download = optional_param('download', null, PARAM_ALPHA);
$downloadcert = optional_param('downloadcert', '', PARAM_BOOL);
if ($downloadcert) {
$certificateid = required_param('certificateid', PARAM_INT);
$customcert = $DB->get_record('customcert', array('id' => $certificateid), '*', MUST_EXIST);
}
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', \mod_customcert\certificate::CUSTOMCERT_PER_PAGE, PARAM_INT);
$pageurl = $url = new moodle_url('/mod/customcert/my_certificates.php', array('userid' => $userid,
'page' => $page, 'perpage' => $perpage));
// Requires a login.
require_login();
// Check that we have a valid user.
$user = \core_user::get_user($userid, '*', MUST_EXIST);
// If we are viewing certificates that are not for the currently logged in user then do a capability check.
if (($userid != $USER->id) && !has_capability('mod/customcert:viewallcertificates', context_system::instance())) {
print_error('You are not allowed to view these certificates');
}
// Check if we requested to download a certificate.
if ($downloadcert) {
$template = $DB->get_record('customcert_templates', array('id' => $customcert->templateid), '*', MUST_EXIST);
$template = new \mod_customcert\template($template);
$template->generate_pdf(false, $userid);
exit();
}
$table = new \mod_customcert\my_certificates_table($userid, $download);
$table->define_baseurl($pageurl);
if ($table->is_downloading()) {
$table->download();
exit();
}
$PAGE->set_url($pageurl);
$PAGE->set_context(context_user::instance($userid));
$PAGE->set_title(get_string('mycertificates', 'customcert'));
$PAGE->set_pagelayout('standard');
$PAGE->navigation->extend_for_user($user);
// Additional page setup.
$PAGE->navbar->add(get_string('profile'), new moodle_url('/user/profile.php', array('id' => $userid)));
$PAGE->navbar->add(get_string('mycertificates', 'customcert'));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('mycertificates', 'customcert'));
echo html_writer::div(get_string('mycertificatesdescription', 'customcert'));
$table->out($perpage, false);
echo $OUTPUT->footer();

View file

@ -60,7 +60,7 @@ $PAGE->navbar->add($str, new \action_link($link, $str));
$PAGE->navbar->add(get_string('rearrangeelements', 'customcert'));
// Include the JS we need.
$PAGE->requires->yui_module('moodle-mod_customcert-rearrange', 'M.mod_customcert.rearrange.init',
$PAGE->requires->yui_module('moodle-mod_customcert-rearrange', 'Y.M.mod_customcert.rearrange.init',
array($template->get_id(),
$page,
$elements));
@ -91,7 +91,7 @@ if ($page->leftmargin) {
if ($elements) {
foreach ($elements as $element) {
// Get an instance of the element class.
if ($e = \mod_customcert\element::instance($element)) {
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
switch ($element->refpoint) {
case \mod_customcert\element_helper::CUSTOMCERT_REF_POINT_TOPRIGHT:
$class = 'element refpoint-right';
@ -103,7 +103,8 @@ if ($elements) {
default:
$class = 'element refpoint-left';
}
$html .= html_writer::tag('div', $e->render_html(), array('class' => $class, 'id' => 'element-' . $element->id));
$html .= html_writer::tag('div', $e->render_html(), array('class' => $class,
'data-refpoint' => $element->refpoint, 'id' => 'element-' . $element->id));
}
}
}

View file

@ -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);
}
@ -44,7 +46,41 @@ require_login($course, false, $cm);
// Check capabilities.
$context = context_module::instance($cm->id);
require_capability('mod/customcert:manage', $context);
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) {
@ -80,4 +116,4 @@ groups_print_activity_menu($cm, $url);
$table->out($perpage, false);
echo $OUTPUT->footer($course);
echo $OUTPUT->footer();

View file

@ -24,6 +24,17 @@
defined('MOODLE_INTERNAL') || die;
$url = $CFG->wwwroot . '/mod/customcert/verify_certificate.php';
$settings->add(new admin_setting_configcheckbox('customcert/verifyallcertificates',
get_string('verifyallcertificates', 'customcert'),
get_string('verifyallcertificates_desc', 'customcert', $url),
0));
$settings->add(new admin_setting_configcheckbox('customcert/showposxy',
get_string('showposxy', 'customcert'),
get_string('showposxy_desc', 'customcert'),
0));
$settings->add(new \mod_customcert\admin_setting_link('customcert/managetemplates',
get_string('managetemplates', 'customcert'), get_string('managetemplatesdesc', 'customcert'),
get_string('managetemplates', 'customcert'), new moodle_url('/mod/customcert/manage_templates.php'), ''));

View file

@ -1,5 +1,10 @@
#page-mod-customcert-edit .deletebutton {
text-align:right;
text-align: right;
}
#page-mod-customcert-edit .addpage {
border-top: 1px solid #f4f4f4;
text-align: right;
}
#page-mod-customcert-edit #id_replace {
@ -14,27 +19,27 @@
#page-mod-customcert-rearrange .savepositionsbtn,
#page-mod-customcert-rearrange .applypositionsbtn,
#page-mod-customcert-rearrange .cancelbtn {
float:left
float: left;
}
#page-mod-customcert-rearrange .element {
display:inline-block;
display: inline-block;
position: absolute;
word-wrap: break-word;
}
#page-mod-customcert-rearrange .element:before {
content: "";
display: block;
background-image: url([[pix:mod_customcert|target]]);
background-repeat: no-repeat;
width: 100%;
height: 9px;
content: "";
display: block;
float: left;
height: 9px;
width: 100%;
}
#page-mod-customcert-rearrange .element:hover {
cursor:move;
cursor: move;
}
#page-mod-customcert-rearrange .element.refpoint-left:before {
@ -53,9 +58,9 @@
}
#page-mod-customcert-rearrange #pdf {
clear:both;
border-style:solid;
border-width:1px;
border-style: solid;
border-width: 1px;
clear: both;
}
#page-mod-customcert-rearrange div#leftmargin {

View file

@ -0,0 +1,46 @@
{{!
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/>.
}}
{{!
@template mod_customcert/email_certificate_html
The certificate verification result
Classes required for JS:
* None
Data attibutes required for JS:
* All data attributes are required
Context variables required for this template:
* emailgreeting
* emailbody
* emailcertificatelink
* emailcertificatelinktext
Example context (json):
{
"emailgreeting": "Dear Angus MacGyver",
"emailbody": "Attached is your certificate 'The basics' for the course 'Survival as an '80s action hero'",
"emailcertificatelink": "http://yoursite.com/mod/customcert/view.php?id=4",
"emailcertificatelinktext": "Certificate report"
}
}}
{{{emailgreeting}}}
{{{emailbody}}}
<a href="{{emailcertificatelink}}">{{emailcertificatelinktext}}</a>.

View file

@ -0,0 +1,44 @@
{{!
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/>.
}}
{{!
@template mod_customcert/email_certificate_text
The certificate verification result
Classes required for JS:
* None
Data attibutes required for JS:
* All data attributes are required
Context variables required for this template:
* emailgreeting
* emailbody
* emailcertificatelink
* emailcertificatelinktext
Example context (json):
{
"emailgreeting": "Dear Angus MacGyver",
"emailbody": "Attached is your certificate 'The basics' for the course 'Survival as an '80s action hero'",
"emailcertificatelink": "http://yoursite.com/mod/customcert/view.php?id=4",
"emailcertificatelinktext": "Certificate report"
}
}}
{{{emailgreeting}}}
{{{emailbody}}}

View file

@ -0,0 +1,48 @@
{{!
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/>.
}}
{{!
@template mod_customcert/verify_certificate_result
The certificate verification result
Classes required for JS:
* None
Data attibutes required for JS:
* All data attributes are required
Context variables required for this template:
* userprofileurl The URL to the user's profile
* userfullname The fullname of the user
* courseurl The URL of the course
* coursefullname The full name of the course
* certificatename The name of the certificate
Example context (json):
{
"userprofileurl": "http://www.example.com",
"userfullname": "Mark Smith",
"courseurl": "http://www.example.com",
"coursefullname": "Computing 101",
"certificatename": "Ability to write 'Hello world' in Java."
}
}}
<ul class="list-unstyled unstyled">
<li><strong>{{#str}}fullname{{/str}}:</strong> <a href="{{{userprofileurl}}}">{{userfullname}}</a></li>
<li><strong>{{#str}}course{{/str}}:</strong> <a href="{{{courseurl}}}">{{coursefullname}}</a></li>
<li><strong>{{#str}}certificate, customcert{{/str}}:</strong> {{certificatename}}</li>
</ul>

View file

@ -0,0 +1,43 @@
{{!
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/>.
}}
{{!
@template mod_customcert/verify_certificate_results
The certificate verification results page
Classes required for JS:
* None
Data attibutes required for JS:
* All data attributes are required
Context variables required for this template:
* None
Example context (json):
{
}
}}
{{#success}}
{{#issues}}
{{> mod_customcert/verify_certificate_result }}
{{/issues}}
{{> core/notification_success}}
{{/success}}
{{^success}}
{{> core/notification_error}}
{{/success}}

View file

@ -0,0 +1,150 @@
<?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/>.
/**
* Contains the class responsible for step definitions related to mod_customcert.
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
/**
* The class responsible for step definitions related to mod_customcert.
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_mod_customcert extends behat_base {
/**
* Adds an element to the specified page of a template.
*
* @codingStandardsIgnoreLine
* @Given /^I add the element "(?P<element_name>(?:[^"]|\\")*)" to page "(?P<page_number>\d+)" of the "(?P<template_name>(?:[^"]|\\")*)" certificate template$/
* @param string $elementname
* @param int $pagenum
* @param string $templatename
*/
public function i_add_the_element_to_the_certificate_template_page($elementname, $pagenum, $templatename) {
global $DB;
$template = $DB->get_record('customcert_templates', array('name' => $templatename), '*', MUST_EXIST);
$page = $DB->get_record('customcert_pages', array('templateid' => $template->id, 'sequence' => $pagenum),
'*', MUST_EXIST);
$this->execute('behat_forms::i_set_the_field_to', array($this->escape('element_' . $page->id),
$this->escape($elementname)));
$this->execute('behat_forms::press_button', get_string('addelement', 'customcert'));
}
/**
* Deletes an element from a specified page of a template.
*
* @Given /^I delete page "(?P<page_number>\d+)" of the "(?P<template_name>(?:[^"]|\\")*)" certificate template$/
* @param int $pagenum
* @param string $templatename
*/
public function i_delete_the_certificate_page($pagenum, $templatename) {
global $DB;
$template = $DB->get_record('customcert_templates', array('name' => $templatename), '*', MUST_EXIST);
$page = $DB->get_record('customcert_pages', array('templateid' => $template->id, 'sequence' => $pagenum),
'*', MUST_EXIST);
$this->execute('behat_general::i_click_on_in_the', array('Delete page', 'link',
$this->escape('#id_page_' . $page->id), 'css_element'));
$this->execute('behat_forms::press_button', get_string('continue'));
}
/**
* Verifies the certificate code for a user.
*
* @Given /^I verify the "(?P<certificate_name>(?:[^"]|\\")*)" certificate for the user "(?P<user_name>(?:[^"]|\\")*)"$/
* @param string $certificatename
* @param string $username
*/
public function i_verify_the_custom_certificate_for_user($certificatename, $username) {
global $DB;
$certificate = $DB->get_record('customcert', array('name' => $certificatename), '*', MUST_EXIST);
$user = $DB->get_record('user', array('username' => $username), '*', MUST_EXIST);
$issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $certificate->id),
'*', MUST_EXIST);
$this->execute('behat_forms::i_set_the_field_to', array(get_string('code', 'customcert'), $issue->code));
$this->execute('behat_forms::press_button', get_string('verify', 'customcert'));
$this->execute('behat_general::assert_page_contains_text', get_string('verified', 'customcert'));
$this->execute('behat_general::assert_page_not_contains_text', get_string('notverified', 'customcert'));
}
/**
* Verifies the certificate code for a user.
*
* @Given /^I can not verify the "(?P<certificate_name>(?:[^"]|\\")*)" certificate for the user "(?P<user_name>(?:[^"]|\\")*)"$/
* @param string $certificatename
* @param string $username
*/
public function i_can_not_verify_the_custom_certificate_for_user($certificatename, $username) {
global $DB;
$certificate = $DB->get_record('customcert', array('name' => $certificatename), '*', MUST_EXIST);
$user = $DB->get_record('user', array('username' => $username), '*', MUST_EXIST);
$issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $certificate->id),
'*', MUST_EXIST);
$this->execute('behat_forms::i_set_the_field_to', array(get_string('code', 'customcert'), $issue->code));
$this->execute('behat_forms::press_button', get_string('verify', 'customcert'));
$this->execute('behat_general::assert_page_contains_text', get_string('notverified', 'customcert'));
$this->execute('behat_general::assert_page_not_contains_text', get_string('verified', 'customcert'));
}
/**
* Directs the user to the URL for verifying a certificate.
*
* This has been created as we allow non-users to verify certificates and they can not navigate to
* the page like a conventional user.
*
* @Given /^I visit the verification url for the "(?P<certificate_name>(?:[^"]|\\")*)" certificate$/
* @param string $certificatename
*/
public function i_visit_the_verification_url_for_custom_certificate($certificatename) {
global $DB;
$certificate = $DB->get_record('customcert', array('name' => $certificatename), '*', MUST_EXIST);
$template = $DB->get_record('customcert_templates', array('id' => $certificate->templateid), '*', MUST_EXIST);
$url = new moodle_url('/mod/customcert/verify_certificate.php', array('contextid' => $template->contextid));
$this->getSession()->visit($this->locate_path($url->out_as_local_url()));
}
/**
* Directs the user to the URL for verifying all certificates on the site.
*
* @Given /^I visit the verification url for the site$/
*/
public function i_visit_the_verification_url_for_the_site() {
$url = new moodle_url('/mod/customcert/verify_certificate.php');
$this->getSession()->visit($this->locate_path($url->out_as_local_url()));
}
}

View file

@ -0,0 +1,307 @@
@mod @mod_customcert
Feature: Being able to manage elements in a certificate template
In order to ensure managing elements in a certificate template works as expected
As a teacher
I need to manage elements in a certificate template
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| teacher2 | Teacher | 2 | teacher2@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| teacher2 | C1 | editingteacher |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| assign | Assignment 1 | Assignment 1 intro | C1 | assign1 |
| assign | Assignment 2 | Assignment 2 intro | C1 | assign2 |
| customcert | Custom certificate 1 | Custom certificate 1 intro | C1 | customcert1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Custom certificate 1"
And I navigate to "Edit certificate" in current page administration
Scenario: Add and edit elements in a certificate template
# Background image.
And I add the element "Background image" to page "1" of the "Custom certificate 1" certificate template
And I press "Save changes"
And I should see "Background image" in the "elementstable" "table"
# Border.
And I add the element "Border" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Width | 2 |
| Colour | #045ECD |
And I press "Save changes"
And I should see "Border" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Border" "table_row"
And the following fields match these values:
| Width | 2 |
| Colour | #045ECD |
And I press "Save changes"
# Category name.
And I add the element "Category name" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Category name" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Category name" "table_row"
And the following fields match these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# Code.
And I add the element "Code" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Code" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Code" "table_row"
And the following fields match these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# Course name.
And I add the element "Course name" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Course name" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Course name" "table_row"
And the following fields match these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# Date.
And I add the element "Date" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Date item | Course start date |
| Date format | 2 |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Date" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Date" "table_row"
And the following fields match these values:
| Date item | Course start date |
| Date format | 2 |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# Digital signature.
And I add the element "Digital signature" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Signature name | This is the signature name |
| Signature password | Some awesome password |
| Signature location | Mordor |
| Signature reason | Meh, felt like it. |
| Signature contact info | Sauron |
| Width | 25 |
| Height | 15 |
And I press "Save changes"
And I should see "Digital signature" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Digital signature" "table_row"
And the following fields match these values:
| Signature name | This is the signature name |
| Signature password | Some awesome password |
| Signature location | Mordor |
| Signature reason | Meh, felt like it. |
| Signature contact info | Sauron |
| Width | 25 |
| Height | 15 |
And I press "Save changes"
# Grade.
And I add the element "Grade" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Grade item | Topic 0 : Assignment 1 |
| Grade format | Percentage |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Grade" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Grade" "table_row"
And the following fields match these values:
| Grade item | Topic 0 : Assignment 1 |
| Grade format | Percentage |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# Grade item name.
And I add the element "Grade item name" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Grade item | Topic 0 : Assignment 2 |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Grade item name" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Grade item name" "table_row"
And the following fields match these values:
| Grade item | Topic 0 : Assignment 2 |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# Image.
And I add the element "Image" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Width | 25 |
| Height | 15 |
And I press "Save changes"
And I should see "Image" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Image" "table_row"
And the following fields match these values:
| Width | 25 |
| Height | 15 |
And I press "Save changes"
# Student name.
And I add the element "Student name" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Student name" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Student name" "table_row"
And the following fields match these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# Teacher name.
And I add the element "Teacher name" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Teacher | Teacher 2 |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Teacher name" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Teacher name" "table_row"
And the following fields match these values:
| Teacher | Teacher 2 |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# Text.
And I add the element "Text" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Text | Test this |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "Text" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Text" "table_row"
And the following fields match these values:
| Text | Test this |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# User field.
And I add the element "User field" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| User field | Country |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I should see "User field" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "User field" "table_row"
And the following fields match these values:
| User field | Country |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
# User picture.
And I add the element "User picture" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values:
| Width | 10 |
| Height | 10 |
And I press "Save changes"
And I should see "User picture" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "User picture" "table_row"
And the following fields match these values:
| Width | 10 |
| Height | 10 |
And I press "Save changes"
# Just to test there are no exceptions being thrown.
And I follow "Reposition elements"
And I press "Save and close"
And I press "Save changes and preview"
Scenario: Delete an element from a certificate template
And I add the element "Background image" to page "1" of the "Custom certificate 1" certificate template
And I press "Save changes"
And I should see "Background image" in the "elementstable" "table"
And I add the element "Student name" to page "1" of the "Custom certificate 1" certificate template
And I press "Save changes"
And I should see "Student name" in the "elementstable" "table"
And I click on ".delete-icon" "css_element" in the "Student name" "table_row"
And I press "Cancel"
And I should see "Background image" in the "elementstable" "table"
And I should see "Student name" in the "elementstable" "table"
And I click on ".delete-icon" "css_element" in the "Student name" "table_row"
And I press "Continue"
And I should see "Background image" in the "elementstable" "table"
And I should not see "Student name" in the "elementstable" "table"

View file

@ -0,0 +1,42 @@
@mod @mod_customcert
Feature: Being able to manage pages in a certificate template
In order to ensure managing pages in a certificate template works as expected
As a teacher
I need to manage pages in a certificate template
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| customcert | Custom certificate 1 | Custom certificate 1 intro | C1 | customcert1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Custom certificate 1"
And I navigate to "Edit certificate" in current page administration
Scenario: Adding a page to a certificate template
And I follow "Add page"
And I should see "Page 1"
And I should see "Page 2"
Scenario: Deleting a page from a certificate template
And I add the element "Background image" to page "1" of the "Custom certificate 1" certificate template
And I press "Save changes"
And I add the element "Student name" to page "1" of the "Custom certificate 1" certificate template
And I press "Save changes"
And I follow "Add page"
And I should see "Page 1"
And I should see "Page 2"
And I delete page "2" of the "Custom certificate 1" certificate template
And I should see "Background image" in the "elementstable" "table"
And I should see "Student name" in the "elementstable" "table"
And I should not see "Page 1"
And I should not see "Page 2"

View file

@ -0,0 +1,105 @@
@mod @mod_customcert
Feature: Being able to manage site templates
In order to ensure managing site templates works as expected
As an admin
I need to manage and load site templates
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| customcert | Custom certificate 1 | Custom certificate 1 intro | C1 | customcert1 |
And I log in as "admin"
Scenario: Adding a site template and loading it into a course certificate
And I expand "Site administration" node
And I expand "Plugins" node
And I expand "Activity modules" node
And I follow "Custom certificate"
And I follow "Manage templates"
And I press "Create template"
And I set the field "Name" to "Site template"
And I press "Save changes"
And I add the element "Border" to page "1" of the "Site template" certificate template
And I set the following fields to these values:
| Width | 5 |
| Colour | #045ECD |
And I press "Save changes"
And I add the element "Category name" to page "1" of the "Site template" certificate template
And I set the following fields to these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
And I press "Save changes"
And I am on "Course 1" course homepage
And I follow "Custom certificate 1"
And I navigate to "Edit certificate" in current page administration
And I set the field "ltid" to "Site template"
And I click on "Load" "button" in the "#loadtemplateform" "css_element"
And I should see "Are you sure you wish to load this template"
And I press "Cancel"
And "elementstable" "table" should not exist
And I set the field "ltid" to "Site template"
And I click on "Load" "button" in the "#loadtemplateform" "css_element"
And I should see "Are you sure you wish to load this template"
And I press "Continue"
And I should see "Border" in the "elementstable" "table"
And I should see "Category name" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Border" "table_row"
And the following fields match these values:
| Width | 5 |
| Colour | #045ECD |
And I press "Save changes"
And I click on ".edit-icon" "css_element" in the "Category name" "table_row"
And the following fields match these values:
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
| Width | 20 |
| Reference point location | Top left |
Scenario: Deleting a site template
And I expand "Site administration" node
And I expand "Plugins" node
And I expand "Activity modules" node
And I follow "Custom certificate"
And I follow "Manage templates"
And I press "Create template"
And I set the field "Name" to "Site template"
And I press "Save changes"
And I follow "Manage templates"
And I click on ".delete-icon" "css_element" in the "Site template" "table_row"
And I press "Cancel"
And I should see "Site template"
And I click on ".delete-icon" "css_element" in the "Site template" "table_row"
And I press "Continue"
And I should not see "Site template"
Scenario: Duplicating a site template
And I expand "Site administration" node
And I expand "Plugins" node
And I expand "Activity modules" node
And I follow "Custom certificate"
And I follow "Manage templates"
And I press "Create template"
And I set the field "Name" to "Site template"
And I press "Save changes"
And I follow "Manage templates"
And I click on ".duplicate-icon" "css_element" in the "Site template" "table_row"
And I press "Cancel"
And I should see "Site template"
And I should not see "Site template (duplicate)"
And I click on ".duplicate-icon" "css_element" in the "Site template" "table_row"
And I press "Continue"
And I should see "Site template"
And I should see "Site template (duplicate)"

View file

@ -0,0 +1,39 @@
@mod @mod_customcert
Feature: Being able to view the certificates you have been issued
In order to ensure that a user can view the certificates they have been issued
As a student
I need to view the certificates I have been issued
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
| Course 2 | C2 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| student1 | C2 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| customcert | Custom certificate 1 | Custom certificate 1 intro | C1 | customcert1 |
| customcert | Custom certificate 2 | Custom certificate 2 intro | C2 | customcert2 |
Scenario: View your issued certificates on the my certificates page
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 follow "Profile" in the user menu
And I follow "My certificates"
And I should see "Custom certificate 1"
And I should not see "Custom certificate 2"
And I am on "Course 2" course homepage
And I follow "Custom certificate 2"
And I press "Download certificate"
And I follow "Profile" in the user menu
And I follow "My certificates"
And I should see "Custom certificate 1"
And I should see "Custom certificate 2"

View file

@ -0,0 +1,38 @@
@mod @mod_customcert
Feature: Being able to set the required minutes in a course before viewing the certificate
In order to ensure the required minutes in a course setting works as expected
As a teacher
I need to ensure students can not view a certificate until the required minutes have passed
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber | requiredtime |
| customcert | Custom certificate 1 | Custom certificate 1 intro | C1 | customcert1 | 1 |
Scenario: Check the user can not access the certificate before the required time
And I log in as "student1"
And I am on "Course 1" course homepage
And I follow "Custom certificate 1"
And I should see "You must spend at least a minimum of"
And I should not see "Download certificate"
And I press "Continue"
And I should see "Custom certificate 1"
Scenario: Check the user can access the certificate after the required time
And I log in as "student1"
And I am on "Course 1" course homepage
And I wait "60" seconds
And I am on "Course 1" course homepage
And I follow "Custom certificate 1"
And I should not see "You must spend at least a minimum of"

View file

@ -0,0 +1,53 @@
@mod @mod_customcert
Feature: Being able to set a site setting to determine whether or not to display the position X and Y fields
In order to ensure the show position X and Y fields setting works as expected
As an admin
I need to ensure teachers can see the position X and Y fields depending on the site setting
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| customcert | Custom certificate 1 | Custom certificate 1 intro | C1 | customcert1 |
Scenario: Adding an element with the show position X and Y setting disabled
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Custom certificate 1"
And I navigate to "Edit certificate" in current page administration
And I add the element "Code" to page "1" of the "Custom certificate 1" certificate template
And I should not see "Position X"
And I should not see "Position Y"
Scenario: Adding an element with the show position X and Y setting enabled
And I log in as "admin"
And I expand "Site administration" node
And I expand "Plugins" node
And I expand "Activity modules" node
And I follow "Custom certificate"
And I set the field "Show position X and Y" to "1"
And I press "Save changes"
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 navigate to "Edit certificate" in current page administration
And I add the element "Code" to page "1" of the "Custom certificate 1" certificate template
And I should see "Position X"
And I should see "Position Y"
And I set the following fields to these values:
| Position X | 5 |
| Position Y | 10 |
And I press "Save changes"
And I click on ".edit-icon" "css_element" in the "Code" "table_row"
And the following fields match these values:
| Position X | 5 |
| Position Y | 10 |

View file

@ -0,0 +1,108 @@
@mod @mod_customcert
Feature: Being able to verify that a certificate is valid or not
In order to ensure that a user can verify a certificate is valid
As a teacher and non-user
I need to be able to verify a certificate
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber | verifyany |
| customcert | Custom certificate 1 | Custom certificate 1 intro | C1 | customcert1 | 0 |
| customcert | Custom certificate 2 | Custom certificate 2 intro | C1 | customcert2 | 1 |
Scenario: Verify a certificate as a teacher
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 "teacher1"
And I am on "Course 1" course homepage
And I follow "Custom certificate 1"
And I navigate to "Verify certificate" in current page administration
And I set the field "Code" to "NOTAVALIDCODE"
And I press "Verify"
And I should see "Not verified"
And I verify the "Custom certificate 1" certificate for the user "student1"
Scenario: Attempt to verify a certificate as a non-user
And I visit the verification url for the "Custom certificate 1" certificate
# User should get redirected to log in as we do not allow non-users to verify.
And I should see "Remember username"
Scenario: Verify a certificate as a non-user
And I log in as "student1"
And I am on "Course 1" course homepage
And I follow "Custom certificate 2"
And I press "Download certificate"
And I log out
And I visit the verification url for the "Custom certificate 2" certificate
And I set the field "Code" to "NOTAVALIDCODE"
And I press "Verify"
And I should see "Not verified"
And I verify the "Custom certificate 2" certificate for the user "student1"
Scenario: Attempt to verify a certificate as a non-user using the site-wide URL
And the following config values are set as admin:
| verifyallcertificates | 0 | customcert |
And I visit the verification url for the site
# User should see an error message as we do not allow non-users to verify all certificates on the site.
And I should see "You do not have the permission to verify all certificates on the site"
Scenario: Attempt to verify a certificate as a teacher using the site-wide URL
And the following config values are set as admin:
| verifyallcertificates | 0 | customcert |
And I log in as "teacher1"
And I visit the verification url for the site
# User should see an error message as we do not allow teachers to verify all certificates on the site.
And I should see "You do not have the permission to verify all certificates on the site"
Scenario: Verify a certificate as an admin using the site-wide URL
And the following config values are set as admin:
| verifyallcertificates | 0 | customcert |
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 am on "Course 1" course homepage
And I follow "Custom certificate 2"
And I press "Download certificate"
And I log out
And I log in as "admin"
# The admin (or anyone with the capability 'mod/customcert:verifyallcertificates') can visit the URL regardless of the setting.
And I visit the verification url for the site
And I set the field "Code" to "NOTAVALIDCODE"
And I press "Verify"
And I should see "Not verified"
# The admin (or anyone with the capability 'mod/customcert:verifyallcertificates') can verify any certificate regardless of the 'verifyany' setting.
And I verify the "Custom certificate 1" certificate for the user "student1"
And I verify the "Custom certificate 2" certificate for the user "student1"
Scenario: Verify a certificate as a non-user using the site-wide URL
And the following config values are set as admin:
| verifyallcertificates | 1 | customcert |
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 am on "Course 1" course homepage
And I follow "Custom certificate 2"
And I press "Download certificate"
And I log out
And I visit the verification url for the site
And I set the field "Code" to "NOTAVALIDCODE"
And I press "Verify"
And I should see "Not verified"
And I can not verify the "Custom certificate 1" certificate for the user "student1"
And I verify the "Custom certificate 2" certificate for the user "student1"

View file

@ -0,0 +1,67 @@
@mod @mod_customcert
Feature: Being able to view the certificates that have been issued
In order to ensure that a user can view the certificates that have been issued
As a teacher
I need to view the certificates that have been issued
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
| student2 | Student | 2 | student2@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
| student2 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| customcert | Custom certificate 1 | Custom certificate 1 intro | C1 | customcert1 |
Scenario: View the issued certificates
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"
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"

View file

@ -0,0 +1,315 @@
<?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/>.
/**
* File contains the unit tests for the element helper class.
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* Unit tests for the element helper class.
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_customcert_element_helper_testcase extends advanced_testcase {
/**
* Test set up.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Tests we are returning the correct course id for an element in a course customcert activity.
*/
public function test_get_courseid_element_in_course_certificate() {
global $DB;
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a custom certificate in the course.
$customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id));
// Get the template to add elements to.
$template = $DB->get_record('customcert_templates', array('contextid' => context_module::instance($customcert->cmid)->id));
$template = new \mod_customcert\template($template);
// Add a page to the template.
$pageid = $template->add_page();
// Add an element to this page.
$element = new \stdClass();
$element->name = 'Test element';
$element->element = 'testelement';
$element->pageid = $pageid;
$element->sequence = \mod_customcert\element_helper::get_element_sequence($element->pageid);
$element->timecreated = time();
$element->id = $DB->insert_record('customcert_elements', $element);
// Confirm the correct course id is returned.
$this->assertEquals($course->id, \mod_customcert\element_helper::get_courseid($element->id));
}
/**
* Tests we are returning the correct course id for an element in a site template.
*/
public function test_get_courseid_element_in_site_template() {
global $DB, $SITE;
// Add a template to the site.
$template = \mod_customcert\template::create('Site template', context_system::instance()->id);
// Add a page to the template.
$pageid = $template->add_page();
// Add an element to this page.
$element = new \stdClass();
$element->name = 'Test element';
$element->element = 'testelement';
$element->pageid = $pageid;
$element->sequence = \mod_customcert\element_helper::get_element_sequence($element->pageid);
$element->timecreated = time();
$element->id = $DB->insert_record('customcert_elements', $element);
// Confirm the correct course id is returned.
$this->assertEquals($SITE->id, \mod_customcert\element_helper::get_courseid($element->id));
}
/**
* Test we return the correct grade items in a course.
*/
public function test_get_grade_items() {
global $DB;
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a few gradeable items.
$assign1 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$assign2 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$assign3 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
// Create a manual grade item.
$gi = $this->getDataGenerator()->create_grade_item(['courseid' => $course->id]);
// Create a category grade item.
$gc = $this->getDataGenerator()->create_grade_category(['courseid' => $course->id]);
$gc = $DB->get_record('grade_items', ['itemtype' => 'category', 'iteminstance' => $gc->id]);
// Confirm the function returns the correct number of grade items.
$gradeitems = \mod_customcert\element_helper::get_grade_items($course);
$this->assertCount(5, $gradeitems);
$this->assertArrayHasKey($assign1->cmid, $gradeitems);
$this->assertArrayHasKey($assign2->cmid, $gradeitems);
$this->assertArrayHasKey($assign3->cmid, $gradeitems);
$this->assertArrayHasKey('gradeitem:' . $gi->id, $gradeitems);
$this->assertArrayHasKey('gradeitem:' . $gc->id, $gradeitems);
}
/**
* Test we return the correct grade information for an activity.
*/
public function test_get_mod_grade_info() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create two users.
$student1 = $this->getDataGenerator()->create_user();
$student2 = $this->getDataGenerator()->create_user();
// Enrol them into the course.
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
// Create a gradeable item.
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
// Give a grade to the student.
$gi = grade_item::fetch(
[
'itemtype' => 'mod',
'itemmodule' => 'assign',
'iteminstance' => $assign->id,
'courseid' => $course->id
]
);
$datagrade = 50;
$time = time();
$grade = new grade_grade();
$grade->itemid = $gi->id;
$grade->userid = $student1->id;
$grade->rawgrade = $datagrade;
$grade->finalgrade = $datagrade;
$grade->rawgrademax = 100;
$grade->rawgrademin = 0;
$grade->timecreated = $time;
$grade->timemodified = $time;
$grade->insert();
// Check that the user received the grade.
$grade = \mod_customcert\element_helper::get_mod_grade_info(
$assign->cmid,
GRADE_DISPLAY_TYPE_PERCENTAGE,
$student1->id
);
$this->assertEquals($assign->name, $grade->get_name());
$this->assertEquals('50.00000', $grade->get_grade());
$this->assertEquals('50 %', $grade->get_displaygrade());
$this->assertEquals($time, $grade->get_dategraded());
// Check that the user we did not grade has no grade.
$grade = \mod_customcert\element_helper::get_mod_grade_info(
$assign->cmid,
GRADE_DISPLAY_TYPE_PERCENTAGE,
$student2->id
);
$this->assertEquals($assign->name, $grade->get_name());
$this->assertEquals(null, $grade->get_grade());
$this->assertEquals('-', $grade->get_displaygrade());
$this->assertEquals(null, $grade->get_dategraded());
}
/**
* Test we return the correct grade information for a course.
*/
public function test_get_course_grade_info() {
global $CFG;
// Including to use constant.
require_once($CFG->dirroot . '/mod/customcert/element/grade/classes/element.php');
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create two users.
$student1 = $this->getDataGenerator()->create_user();
$student2 = $this->getDataGenerator()->create_user();
// Enrol them into the course.
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
// Get the course item.
$coursegradeitem = grade_item::fetch_course_item($course->id);
$datagrade = 50;
$time = time();
$grade = new grade_grade();
$grade->itemid = $coursegradeitem->id;
$grade->userid = $student1->id;
$grade->rawgrade = $datagrade;
$grade->finalgrade = $datagrade;
$grade->rawgrademax = 100;
$grade->rawgrademin = 0;
$grade->timecreated = $time;
$grade->timemodified = $time;
$grade->insert();
// Check that the user received the grade.
$grade = \mod_customcert\element_helper::get_course_grade_info(
$course->id,
GRADE_DISPLAY_TYPE_PERCENTAGE,
$student1->id
);
$this->assertEquals(get_string('coursetotal', 'grades'), $grade->get_name());
$this->assertEquals('50.00000', $grade->get_grade());
$this->assertEquals('50 %', $grade->get_displaygrade());
$this->assertEquals($time, $grade->get_dategraded());
// Check that the user we did not grade has no grade.
$grade = \mod_customcert\element_helper::get_course_grade_info(
$course->id,
GRADE_DISPLAY_TYPE_PERCENTAGE,
$student2->id
);
$this->assertEquals(get_string('coursetotal', 'grades'), $grade->get_name());
$this->assertEquals(null, $grade->get_grade());
$this->assertEquals('-', $grade->get_displaygrade());
$this->assertEquals(null, $grade->get_dategraded());
}
/**
* Test we return the correct grade information for a grade item.
*/
public function test_get_grade_item_info() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create two users.
$student1 = $this->getDataGenerator()->create_user();
$student2 = $this->getDataGenerator()->create_user();
// Enrol them into the course.
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
// Create a manual grade item.
$gi = $this->getDataGenerator()->create_grade_item(['itemname' => 'Grade item yo', 'courseid' => $course->id]);
// Give a grade to the student.
$gi = grade_item::fetch(['id' => $gi->id]);
$datagrade = 50;
$time = time();
$grade = new grade_grade();
$grade->itemid = $gi->id;
$grade->userid = $student1->id;
$grade->rawgrade = $datagrade;
$grade->finalgrade = $datagrade;
$grade->rawgrademax = 100;
$grade->rawgrademin = 0;
$grade->timecreated = $time;
$grade->timemodified = $time;
$grade->insert();
// Check that the user received the grade.
$grade = \mod_customcert\element_helper::get_grade_item_info(
$gi->id,
GRADE_DISPLAY_TYPE_PERCENTAGE,
$student1->id
);
$this->assertEquals('Grade item yo', $grade->get_name());
$this->assertEquals('50.00000', $grade->get_grade());
$this->assertEquals('50 %', $grade->get_displaygrade());
$this->assertEquals($time, $grade->get_dategraded());
// Check that the user we did not grade has no grade.
$grade = \mod_customcert\element_helper::get_grade_item_info(
$gi->id,
GRADE_DISPLAY_TYPE_PERCENTAGE,
$student2->id
);
$this->assertEquals('Grade item yo', $grade->get_name());
$this->assertEquals(null, $grade->get_grade());
$this->assertEquals('-', $grade->get_displaygrade());
$this->assertEquals(null, $grade->get_dategraded());
}
}

View file

@ -0,0 +1,272 @@
<?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/>.
/**
* File contains the unit tests for the email certificate task.
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
/**
* Unit tests for the email certificate task.
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_customcert_task_email_certificate_task_testcase extends advanced_testcase {
/**
* Test set up.
*/
public function setUp() {
$this->resetAfterTest();
}
/**
* Tests the email certificate task for students.
*/
public function test_email_certificates_students() {
global $CFG, $DB;
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create some users.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user(array('firstname' => 'Teacher', 'lastname' => 'One'));
// Enrol two of them in the course as students.
$roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
// Enrol one of the users as a teacher.
$this->getDataGenerator()->enrol_user($user3->id, $course->id, $roleids['editingteacher']);
// Create a custom certificate.
$customcert = $this->getDataGenerator()->create_module('customcert', array('course' => $course->id,
'emailstudents' => 1));
// Ok, now issue this to one user.
\mod_customcert\certificate::issue_certificate($customcert->id, $user1->id);
// Confirm there is only one entry in this table.
$this->assertEquals(1, $DB->count_records('customcert_issues'));
// Run the task.
$sink = $this->redirectEmails();
$task = new \mod_customcert\task\email_certificate_task();
$task->execute();
$emails = $sink->get_messages();
// Get the issues from the issues table now.
$issues = $DB->get_records('customcert_issues');
$this->assertCount(2, $issues);
// Confirm that it was marked as emailed and was not issued to the teacher.
foreach ($issues as $issue) {
$this->assertEquals(1, $issue->emailed);
$this->assertNotEquals($user3->id, $issue->userid);
}
// Confirm that we sent out emails to the two users.
$this->assertCount(2, $emails);
$this->assertContains(fullname($user3), $emails[0]->header);
$this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
$this->assertEquals($user1->email, $emails[0]->to);
$this->assertContains(fullname($user3), $emails[1]->header);
$this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
$this->assertEquals($user2->email, $emails[1]->to);
// Now, run the task again and ensure we did not issue any more certificates.
$sink = $this->redirectEmails();
$task = new \mod_customcert\task\email_certificate_task();
$task->execute();
$emails = $sink->get_messages();
$issues = $DB->get_records('customcert_issues');
$this->assertCount(2, $issues);
$this->assertCount(0, $emails);
}
/**
* Tests the email certificate task for teachers.
*/
public function test_email_certificates_teachers() {
global $CFG, $DB;
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create some users.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user(array('firstname' => 'Teacher', 'lastname' => 'One'));
// Enrol two of them in the course as students.
$roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
// Enrol one of the users as a teacher.
$this->getDataGenerator()->enrol_user($user3->id, $course->id, $roleids['editingteacher']);
// Create a custom certificate.
$this->getDataGenerator()->create_module('customcert', array('course' => $course->id,
'emailteachers' => 1));
// Run the task.
$sink = $this->redirectEmails();
$task = new \mod_customcert\task\email_certificate_task();
$task->execute();
$emails = $sink->get_messages();
// Confirm that we only sent out 2 emails, both emails to the teacher for the two students.
$this->assertCount(2, $emails);
$this->assertContains(fullname($user3), utf8_encode($emails[0]->header));
$this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
$this->assertEquals($user3->email, $emails[0]->to);
$this->assertContains(fullname($user3), utf8_encode($emails[1]->header));
$this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
$this->assertEquals($user3->email, $emails[1]->to);
}
/**
* Tests the email certificate task for others.
*/
public function test_email_certificates_others() {
global $CFG;
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create some users.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
// Enrol two of them in the course as students.
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);
// Create a custom certificate.
$this->getDataGenerator()->create_module('customcert', array('course' => $course->id,
'emailothers' => 'testcustomcert@example.com, doo@dah'));
// Run the task.
$sink = $this->redirectEmails();
$task = new \mod_customcert\task\email_certificate_task();
$task->execute();
$emails = $sink->get_messages();
// Confirm that we only sent out 2 emails, both emails to the other address that was valid for the two students.
$this->assertCount(2, $emails);
$this->assertContains(fullname(get_admin()), utf8_encode($emails[0]->header));
$this->assertEquals($CFG->noreplyaddress, $emails[0]->from);
$this->assertEquals('testcustomcert@example.com', $emails[0]->to);
$this->assertContains(fullname(get_admin()), utf8_encode($emails[1]->header));
$this->assertEquals($CFG->noreplyaddress, $emails[1]->from);
$this->assertEquals('testcustomcert@example.com', $emails[1]->to);
}
/**
* Tests the email certificate task when the certificate is not visible.
*/
public function test_email_certificates_students_not_visible() {
global $DB;
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a user.
$user1 = $this->getDataGenerator()->create_user();
// Enrol them in the course.
$roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
// Create a custom certificate.
$this->getDataGenerator()->create_module('customcert', array('course' => $course->id, 'emailstudents' => 1));
// Remove the permission for the user to view the certificate.
assign_capability('mod/customcert:view', CAP_PROHIBIT, $roleids['student'], \context_course::instance($course->id));
// Run the task.
$sink = $this->redirectEmails();
$task = new \mod_customcert\task\email_certificate_task();
$task->execute();
$emails = $sink->get_messages();
// Confirm there are no issues as the user did not have permissions to view it.
$issues = $DB->get_records('customcert_issues');
$this->assertCount(0, $issues);
// Confirm no emails were sent.
$this->assertCount(0, $emails);
}
/**
* Tests the email certificate task when the student has not met the required time for the course.
*/
public function test_email_certificates_students_havent_met_required_time() {
global $DB;
// Set the standard log to on.
set_config('enabled_stores', 'logstore_standard', 'tool_log');
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a user.
$user1 = $this->getDataGenerator()->create_user();
// Enrol them in the course.
$this->getDataGenerator()->enrol_user($user1->id, $course->id);
// Create a custom certificate.
$this->getDataGenerator()->create_module('customcert', array('course' => $course->id, 'emailstudents' => 1,
'requiredtime' => '60'));
// Run the task.
$sink = $this->redirectEmails();
$task = new \mod_customcert\task\email_certificate_task();
$task->execute();
$emails = $sink->get_messages();
// Confirm there are no issues as the user did not meet the required time.
$issues = $DB->get_records('customcert_issues');
$this->assertCount(0, $issues);
// Confirm no emails were sent.
$this->assertCount(0, $emails);
}
}

64
tests/generator/lib.php Normal file
View file

@ -0,0 +1,64 @@
<?php
// This file is part of the customcert module for 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/>.
/**
* Contains the class responsible for data generation during unit tests
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* The class responsible for data generation during unit tests
*
* @package mod_customcert
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_customcert_generator extends testing_module_generator {
/**
* Creates an instance of the custom certificate.
*
* @param array|stdClass|null $record
* @param array|null $options
* @return stdClass
*/
public function create_instance($record = null, array $options = null) {
$record = (object)(array)$record;
$defaultsettings = array(
'requiredtime' => 0,
'emailstudents' => 0,
'emailteachers' => 0,
'emailothers' => '',
'protection' => ''
);
foreach ($defaultsettings as $name => $value) {
if (!isset($record->{$name})) {
$record->{$name} = $value;
}
}
return parent::create_instance($record, (array)$options);
}
}

Some files were not shown because too many files have changed in this diff Show more