-
Notifications
You must be signed in to change notification settings - Fork 159
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
base: MOODLE_311_STABLE
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,5 +178,46 @@ function xmldb_customcert_upgrade($oldversion) { | |
upgrade_mod_savepoint(true, 2020110901, 'customcert'); | ||
} | ||
|
||
if ($oldversion < 2021101100) { | ||
$transaction = $DB->start_delegated_transaction(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need to put this into a transaction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should be on a separate line. |
||
} | ||
|
||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -305,11 +305,12 @@ public static function get_date_formats() { | |
$date = 1530849658; | ||
|
||
$suffix = self::get_ordinal_number_suffix(userdate($date, '%d')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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', | ||
|
@@ -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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change I did to change all old items in DB which are saved in format '1', '2'... to new format