Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom setting additional date to customize the date display #463

Open
wants to merge 1 commit into
base: MOODLE_311_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,46 @@ function xmldb_customcert_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2020110901, 'customcert');
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change I did to change all old items in DB which are saved in format '1', '2'... to new format

if ($oldversion < 2021101100) {
$transaction = $DB->start_delegated_transaction();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to put this into a transaction.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this improves performance as well as makes it more predictable

$records = $DB->get_records('customcert_elements', ['element' => 'date']);
$total = count($records);
$done = 0;
$pbar = new progress_bar('mod_customcert_change_formatdate', 500, true);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a progress bar as this is a quick change. I think all the code related to this progress bar can be removed. Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a server had a very large number of custom certificates, it's possible to take a long time so I think it makes sense to add a progress bar here. What do you think?

foreach ($records as $record) {
$dateinfo = json_decode($record->data);
$done += 1;
$pbar->update($done, $total, "Changing date format data - $done/$total.");
if (empty($dateinfo)) {
continue;
}
$dateformat = $dateinfo->dateformat;
$update = false;
if ($dateformat == 1) {
$dateformat = '=%B %d, %Y';
$update = true;
} else if ($dateformat == 2) {
$dateformat = '=%B %d#, %Y';
$update = true;
} else if ($dateformat == 3) {
$dateformat = '=%d %B %Y';
$update = true;
} else if ($dateformat == 4) {
$dateformat = '=%B %Y';
$update = true;
}

if ($update) {
$updateelement = new stdClass();
$updateelement->id = $record->id;
$updateelement->data = json_encode([
'dateitem' => $dateinfo->dateitem,
'dateformat' => $dateformat
]);
$DB->update_record('customcert_elements', $updateelement);
}}
$transaction->allow_commit();upgrade_mod_savepoint(true, 2021101100, 'customcert');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be on a separate line.

}

return true;
}
35 changes: 11 additions & 24 deletions element/date/classes/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,12 @@ public static function get_date_formats() {
$date = 1530849658;

$suffix = self::get_ordinal_number_suffix(userdate($date, '%d'));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are the new way I change the format date. User will add new settings like "=%d# %B, %Y", which = is always the first character and '#' to present for 1st, 2nd, 3rd as handling by this function get_ordinal_number_suffix, and should add an intro for this symbol in the description too.


$dateformats = [
1 => userdate($date, '%B %d, %Y'),
2 => userdate($date, '%B %d' . $suffix . ', %Y')
];
$dateformats = [];
$setting = get_config('customcert', 'managedateformat');
$strdateformats = preg_split("~(\r|\n)+~", $setting, -1, PREG_SPLIT_NO_EMPTY);
foreach ($strdateformats as $strdateformat) {
$dateformats['=' . $strdateformat] = userdate($date, str_replace("#", $suffix, $strdateformat));
}

$strdateformats = [
'strftimedate',
Expand Down Expand Up @@ -350,25 +351,11 @@ public static function get_date_formats() {
* @return string
*/
protected function get_date_format_string($date, $dateformat) {
// 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'));
}
// Logic to check custom format date.
if (substr($dateformat, 0, 1) === '=') {
// Now we use custom format that stored in DB.
$suffix = self::get_ordinal_number_suffix(userdate($date, '%d'));
$certificatedate = userdate($date, str_replace("#", $suffix, substr($dateformat, 1)));
}

// Ok, so we must have been passed the actual format in the lang file.
Expand Down
2 changes: 1 addition & 1 deletion element/date/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');

$plugin->version = 2021051700; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2021101100; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2021051700; // Requires this Moodle version (3.11).
$plugin->component = 'customcertelement_date';
5 changes: 5 additions & 0 deletions lang/en/customcert.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
$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['managedateformat'] = 'Manage date format';
$string['managedateformat_desc'] = 'Custom date formats are available for selection when adding a certificate, listed one per line.
Example: %d %m %y. (<a href="https://www.php.net/manual/en/datetime.format.php">See PHP documentation.</a>)
In addition, the special symbol # can be used to indicate \'st\', \'nd\', \'rd\' or \'th\', for 1st 2nd 3rd 4th of the month.
Changing this list does not affect existing certificates, only the options available when creating or updating a 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 Custom certificate activities in courses.';
$string['modify'] = 'Modify';
Expand Down
4 changes: 4 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
get_string('uploadimage', 'customcert'), get_string('uploadimagedesc', 'customcert'),
get_string('uploadimage', 'customcert'), new moodle_url('/mod/customcert/upload_image.php'), ''));

$settings->add(new admin_setting_configtextarea('customcert/managedateformat',
get_string('managedateformat', 'customcert'),
get_string('managedateformat_desc', 'customcert'), "%B %d, %Y\n" . "%B %d#, %Y", PARAM_RAW));

$settings->add(new admin_setting_heading('defaults',
get_string('modeditdefaults', 'admin'), get_string('condifmodeditdefaults', 'admin')));

Expand Down
14 changes: 12 additions & 2 deletions tests/behat/managing_elements.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ Feature: Being able to manage elements in a certificate template
| 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 "admin"
And I navigate to "Plugins > Activity modules > Custom certificate" in site administration
And I set the field "Manage date format" to multiline:
"""
%B %d, %Y
%B %d#, %Y
%d# %B, %Y
"""
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"
Expand Down Expand Up @@ -121,7 +131,7 @@ Feature: Being able to manage elements in a certificate template
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 |
| Date format | =%d# %B, %Y |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
Expand All @@ -132,7 +142,7 @@ Feature: Being able to manage elements in a certificate template
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 |
| Date format | =%d# %B, %Y |
| Font | Helvetica |
| Size | 20 |
| Colour | #045ECD |
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');

$plugin->version = 2021051701; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2021101100; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2021051700; // Requires this Moodle version (3.11).
$plugin->cron = 0; // Period for cron to check this module (secs).
$plugin->component = 'mod_customcert';
Expand Down