diff --git a/db/upgrade.php b/db/upgrade.php index fb22193a..93a438d5 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -178,5 +178,46 @@ function xmldb_customcert_upgrade($oldversion) { upgrade_mod_savepoint(true, 2020110901, 'customcert'); } + if ($oldversion < 2021101100) { + $transaction = $DB->start_delegated_transaction(); + $records = $DB->get_records('customcert_elements', ['element' => 'date']); + $total = count($records); + $done = 0; + $pbar = new progress_bar('mod_customcert_change_formatdate', 500, true); + 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'); + } + return true; } diff --git a/element/date/classes/element.php b/element/date/classes/element.php index 5c0fb459..ae40710b 100644 --- a/element/date/classes/element.php +++ b/element/date/classes/element.php @@ -305,11 +305,12 @@ public static function get_date_formats() { $date = 1530849658; $suffix = self::get_ordinal_number_suffix(userdate($date, '%d')); - - $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', @@ -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. diff --git a/element/date/version.php b/element/date/version.php index c0b7a86e..5bf785f8 100644 --- a/element/date/version.php +++ b/element/date/version.php @@ -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'; diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 7ed7b9a2..969f1019 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -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. (See PHP documentation.) +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'; diff --git a/settings.php b/settings.php index 6a648edc..cfe8115e 100644 --- a/settings.php +++ b/settings.php @@ -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'))); diff --git a/tests/behat/managing_elements.feature b/tests/behat/managing_elements.feature index f734eb3a..57dc4446 100644 --- a/tests/behat/managing_elements.feature +++ b/tests/behat/managing_elements.feature @@ -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" @@ -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 | @@ -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 | diff --git a/version.php b/version.php index 246a161e..8573d456 100644 --- a/version.php +++ b/version.php @@ -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';