Skip to content

Commit 033b42d

Browse files
committed
Moved duplicated date related code to element_helper class (#499)
1 parent 227c55c commit 033b42d

File tree

3 files changed

+123
-218
lines changed

3 files changed

+123
-218
lines changed

classes/element_helper.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,4 +686,112 @@ public static function get_grade_item_info($gradeitemid, $gradeformat, $userid)
686686
$grade->get_dategraded()
687687
);
688688
}
689+
690+
public static function get_date_formats(): array {
691+
// Hard-code date so users can see the difference between short dates with and without the leading zero.
692+
// Eg. 06/07/18 vs 6/07/18.
693+
$date = 1530849658;
694+
695+
$suffix = self::get_ordinal_number_suffix((int)userdate($date, '%d'));
696+
697+
$dateformats = [
698+
1 => userdate($date, '%B %d, %Y'),
699+
2 => userdate($date, '%B %d' . $suffix . ', %Y'),
700+
];
701+
702+
$strdateformats = [
703+
'strftimedate',
704+
'strftimedatefullshort',
705+
'strftimedatefullshortwleadingzero',
706+
'strftimedateshort',
707+
'strftimedatetime',
708+
'strftimedatetimeshort',
709+
'strftimedatetimeshortwleadingzero',
710+
'strftimedaydate',
711+
'strftimedaydatetime',
712+
'strftimedayshort',
713+
'strftimedaytime',
714+
'strftimemonthyear',
715+
'strftimerecent',
716+
'strftimerecentfull',
717+
'strftimetime',
718+
];
719+
720+
foreach ($strdateformats as $strdateformat) {
721+
if ($strdateformat == 'strftimedatefullshortwleadingzero') {
722+
$dateformats[$strdateformat] = userdate($date, get_string('strftimedatefullshort', 'langconfig'), 99, false);
723+
} else if ($strdateformat == 'strftimedatetimeshortwleadingzero') {
724+
$dateformats[$strdateformat] = userdate($date, get_string('strftimedatetimeshort', 'langconfig'), 99, false);
725+
} else {
726+
$dateformats[$strdateformat] = userdate($date, get_string($strdateformat, 'langconfig'));
727+
}
728+
}
729+
730+
return $dateformats;
731+
}
732+
733+
/**
734+
* Returns the date in a readable format.
735+
*
736+
* @param int $date
737+
* @param string $dateformat
738+
* @return string
739+
*/
740+
public static function get_date_format_string(int $date, string $dateformat): string {
741+
// Keeping for backwards compatibility.
742+
if (is_number($dateformat)) {
743+
switch ($dateformat) {
744+
case 1:
745+
$certificatedate = userdate($date, '%B %d, %Y');
746+
break;
747+
case 2:
748+
$suffix = self::get_ordinal_number_suffix((int)userdate($date, '%d'));
749+
$certificatedate = userdate($date, '%B %d' . $suffix . ', %Y');
750+
break;
751+
case 3:
752+
$certificatedate = userdate($date, '%d %B %Y');
753+
break;
754+
case 4:
755+
$certificatedate = userdate($date, '%B %Y');
756+
break;
757+
default:
758+
$certificatedate = userdate($date, get_string('strftimedate', 'langconfig'));
759+
}
760+
}
761+
762+
// Ok, so we must have been passed the actual format in the lang file.
763+
if (!isset($certificatedate)) {
764+
if ($dateformat == 'strftimedatefullshortwleadingzero') {
765+
$certificatedate = userdate($date, get_string('strftimedatefullshort', 'langconfig'), 99, false);
766+
} else if ($dateformat == 'strftimedatetimeshortwleadingzero') {
767+
$certificatedate = userdate($date, get_string('strftimedatetimeshort', 'langconfig'), 99, false);
768+
} else {
769+
$certificatedate = userdate($date, get_string($dateformat, 'langconfig'));
770+
}
771+
}
772+
773+
return $certificatedate;
774+
}
775+
776+
/**
777+
* Helper function to return the suffix of the day of
778+
* the month, eg 'st' if it is the 1st of the month.
779+
*
780+
* @param int $day the day of the month
781+
* @return string the suffix.
782+
*/
783+
private static function get_ordinal_number_suffix(int $day): string {
784+
if (!in_array(($day % 100), [11, 12, 13])) {
785+
switch ($day % 10) {
786+
// Handle 1st, 2nd, 3rd.
787+
case 1:
788+
return get_string('numbersuffix_st_as_in_first', 'customcertelement_date');
789+
case 2:
790+
return get_string('numbersuffix_nd_as_in_second', 'customcertelement_date');
791+
case 3:
792+
return get_string('numbersuffix_rd_as_in_third', 'customcertelement_date');
793+
}
794+
}
795+
return 'th';
796+
}
689797
}

element/date/classes/element.php

Lines changed: 10 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace customcertelement_date;
2626

27+
use mod_customcert\element_helper;
28+
2729
defined('MOODLE_INTERNAL') || die();
2830

2931
/**
@@ -103,7 +105,8 @@ public function render_form_elements($mform) {
103105
$mform->addElement('select', 'dateitem', get_string('dateitem', 'customcertelement_date'), $dateoptions);
104106
$mform->addHelpButton('dateitem', 'dateitem', 'customcertelement_date');
105107

106-
$mform->addElement('select', 'dateformat', get_string('dateformat', 'customcertelement_date'), self::get_date_formats());
108+
$mform->addElement('select', 'dateformat', get_string('dateformat', 'customcertelement_date'),
109+
element_helper::get_date_formats());
107110
$mform->addHelpButton('dateformat', 'dateformat', 'customcertelement_date');
108111

109112
parent::render_form_elements($mform);
@@ -230,7 +233,7 @@ public function render($pdf, $preview, $user) {
230233

231234
// Ensure that a date has been set.
232235
if (!empty($date)) {
233-
\mod_customcert\element_helper::render_content($pdf, $this, $this->get_date_format_string($date, $dateformat));
236+
\mod_customcert\element_helper::render_content($pdf, $this, element_helper::get_date_format_string($date, $dateformat));
234237
}
235238
}
236239

@@ -252,7 +255,8 @@ public function render_html() {
252255
$dateinfo = json_decode($this->get_data());
253256
$dateformat = $dateinfo->dateformat;
254257

255-
return \mod_customcert\element_helper::render_html_content($this, $this->get_date_format_string(time(), $dateformat));
258+
return \mod_customcert\element_helper::render_html_content($this,
259+
element_helper::get_date_format_string(time(), $dateformat));
256260
}
257261

258262
/**
@@ -312,110 +316,8 @@ public function after_restore($restore) {
312316
* @return array the list of date formats
313317
*/
314318
public static function get_date_formats() {
315-
// Hard-code date so users can see the difference between short dates with and without the leading zero.
316-
// Eg. 06/07/18 vs 6/07/18.
317-
$date = 1530849658;
318-
319-
$suffix = self::get_ordinal_number_suffix(userdate($date, '%d'));
320-
321-
$dateformats = [
322-
1 => userdate($date, '%B %d, %Y'),
323-
2 => userdate($date, '%B %d' . $suffix . ', %Y'),
324-
];
325-
326-
$strdateformats = [
327-
'strftimedate',
328-
'strftimedatefullshort',
329-
'strftimedatefullshortwleadingzero',
330-
'strftimedateshort',
331-
'strftimedatetime',
332-
'strftimedatetimeshort',
333-
'strftimedatetimeshortwleadingzero',
334-
'strftimedaydate',
335-
'strftimedaydatetime',
336-
'strftimedayshort',
337-
'strftimedaytime',
338-
'strftimemonthyear',
339-
'strftimerecent',
340-
'strftimerecentfull',
341-
'strftimetime',
342-
];
343-
344-
foreach ($strdateformats as $strdateformat) {
345-
if ($strdateformat == 'strftimedatefullshortwleadingzero') {
346-
$dateformats[$strdateformat] = userdate($date, get_string('strftimedatefullshort', 'langconfig'), 99, false);
347-
} else if ($strdateformat == 'strftimedatetimeshortwleadingzero') {
348-
$dateformats[$strdateformat] = userdate($date, get_string('strftimedatetimeshort', 'langconfig'), 99, false);
349-
} else {
350-
$dateformats[$strdateformat] = userdate($date, get_string($strdateformat, 'langconfig'));
351-
}
352-
}
353-
354-
return $dateformats;
355-
}
356-
357-
/**
358-
* Returns the date in a readable format.
359-
*
360-
* @param int $date
361-
* @param string $dateformat
362-
* @return string
363-
*/
364-
protected function get_date_format_string($date, $dateformat) {
365-
// Keeping for backwards compatibility.
366-
if (is_number($dateformat)) {
367-
switch ($dateformat) {
368-
case 1:
369-
$certificatedate = userdate($date, '%B %d, %Y');
370-
break;
371-
case 2:
372-
$suffix = self::get_ordinal_number_suffix(userdate($date, '%d'));
373-
$certificatedate = userdate($date, '%B %d' . $suffix . ', %Y');
374-
break;
375-
case 3:
376-
$certificatedate = userdate($date, '%d %B %Y');
377-
break;
378-
case 4:
379-
$certificatedate = userdate($date, '%B %Y');
380-
break;
381-
default:
382-
$certificatedate = userdate($date, get_string('strftimedate', 'langconfig'));
383-
}
384-
}
385-
386-
// Ok, so we must have been passed the actual format in the lang file.
387-
if (!isset($certificatedate)) {
388-
if ($dateformat == 'strftimedatefullshortwleadingzero') {
389-
$certificatedate = userdate($date, get_string('strftimedatefullshort', 'langconfig'), 99, false);
390-
} else if ($dateformat == 'strftimedatetimeshortwleadingzero') {
391-
$certificatedate = userdate($date, get_string('strftimedatetimeshort', 'langconfig'), 99, false);
392-
} else {
393-
$certificatedate = userdate($date, get_string($dateformat, 'langconfig'));
394-
}
395-
}
396-
397-
return $certificatedate;
398-
}
399-
400-
/**
401-
* Helper function to return the suffix of the day of
402-
* the month, eg 'st' if it is the 1st of the month.
403-
*
404-
* @param int $day the day of the month
405-
* @return string the suffix.
406-
*/
407-
protected static function get_ordinal_number_suffix($day) {
408-
if (!in_array(($day % 100), [11, 12, 13])) {
409-
switch ($day % 10) {
410-
// Handle 1st, 2nd, 3rd.
411-
case 1:
412-
return get_string('numbersuffix_st_as_in_first', 'customcertelement_date');
413-
case 2:
414-
return get_string('numbersuffix_nd_as_in_second', 'customcertelement_date');
415-
case 3:
416-
return get_string('numbersuffix_rd_as_in_third', 'customcertelement_date');
417-
}
418-
}
419-
return 'th';
319+
debugging("The method customcertelement_date::get_date_formats is deprecated, " .
320+
"please use element_helper::get_date_formats() instead", DEBUG_DEVELOPER);
321+
return element_helper::get_date_formats();
420322
}
421323
}

0 commit comments

Comments
 (0)