From fc289cab3b6fc98d4d24f09f9d9b8bab8ccfa8cf Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Wed, 21 Apr 2010 22:42:30 +0000 Subject: [PATCH] SCORM MDL-18835 display time periods in "human-readable" format. - mainly for SCORM 2004 objects. - thanks to Vlas Voloshin for patch - merged from HEAD --- mod/scorm/locallib.php | 41 +++++++++++++++++++++++++++++++++++++++++ mod/scorm/report.php | 16 ++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/mod/scorm/locallib.php b/mod/scorm/locallib.php index 20448cb21df..825c2836764 100755 --- a/mod/scorm/locallib.php +++ b/mod/scorm/locallib.php @@ -1334,4 +1334,45 @@ function scorm_delete_attempt($userid, $scormid, $attemptid) { delete_records('scorm_scoes_track', 'userid', $userid, 'scormid', $scormid, 'attempt', $attemptid); return true; } + +/** + * Converts SCORM date/time notation to human-readable format + * The function works with both SCORM 1.2 and SCORM 2004 time formats + * @param $datetime string SCORM date/time + * @return string human-readable date/time + */ +function scorm_format_date_time($datetime) { + // fetch date/time strings + $stryears = get_string('numyears'); + $strmonths = get_string('nummonths'); + $strdays = get_string('numdays'); + $strhours = get_string('numhours'); + $strminutes = get_string('numminutes'); + $strseconds = get_string('numseconds'); + + if ($datetime[0] == 'P') { + // if timestamp starts with 'P' - it's a SCORM 2004 format + // this regexp discards empty sections, takes Month/Minute ambiguity into consideration, + // and outputs filled sections, discarding leading zeroes and any format literals + // also saves the only zero before seconds decimals (if there are any) and discards decimals if they are zero + $pattern = array( '#([A-Z])0+Y#', '#([A-Z])0+M#', '#([A-Z])0+D#', '#P(|\d+Y)0*(\d+)M#', '#0*(\d+)Y#', '#0*(\d+)D#', '#P#', + '#([A-Z])0+H#', '#([A-Z])[0.]+S#', '#\.0+S#', '#T(|\d+H)0*(\d+)M#', '#0*(\d+)H#', '#0+\.(\d+)S#', '#0*([\d.]+)S#', '#T#' ); + $replace = array( '$1', '$1', '$1', '$1$2'.$strmonths.' ', '$1'.$stryears.' ', '$1'.$strdays.' ', '', + '$1', '$1', 'S', '$1$2'.$strminutes.' ', '$1'.$strhours.' ', '0.$1'.$strseconds, '$1'.$strseconds, ''); + } else { + // else we have SCORM 1.2 format there + // first convert the timestamp to some SCORM 2004-like format for conveniency + $datetime = preg_replace('#^(\d+):(\d+):([\d.]+)$#', 'T$1H$2M$3S', $datetime); + // then convert in the same way as SCORM 2004 + $pattern = array( '#T0+H#', '#([A-Z])0+M#', '#([A-Z])[0.]+S#', '#\.0+S#', '#0*(\d+)H#', '#0*(\d+)M#', '#0+\.(\d+)S#', '#0*([\d.]+)S#', '#T#' ); + $replace = array( 'T', '$1', '$1', 'S', '$1'.$strhours.' ', '$1'.$strminutes.' ', '0.$1'.$strseconds, '$1'.$strseconds, '' ); + //$pattern = '##'; + //$replace = ''; + } + + $result = preg_replace($pattern, $replace, $datetime); + + return $result; +} + ?> \ No newline at end of file diff --git a/mod/scorm/report.php b/mod/scorm/report.php index 7d9d8c8cde7..40ec8ac3409 100755 --- a/mod/scorm/report.php +++ b/mod/scorm/report.php @@ -261,7 +261,7 @@ $row[] = ''.$strstatus.' '.format_string($sco->title); $row[] = get_string($trackdata->status,'scorm'); - $row[] = $trackdata->total_time; + $row[] = scorm_format_date_time($trackdata->total_time); $row[] = $score; $row[] = $detailslink; } else { @@ -300,7 +300,7 @@ } $strstatus = get_string($trackdata->status,'scorm'); echo ''.$strstatus.' '.$trackdata->total_time.'
'.$scoreview.'
'; + $strstatus.'" /> '.scorm_format_date_time($trackdata->total_time).'
'.$scoreview.'
'; echo ''."\n"; echo '

'.get_string('details','scorm').'

'; @@ -333,7 +333,11 @@ $printedelements[]=$element; $row = array(); $row[] = get_string($key,'scorm'); - $row[] = s($trackdata->$element); + if ($key == 'time') { + $row[] = s(scorm_format_date_time($trackdata->$element)); + } else { + $row[] = s($trackdata->$element); + } $table->data[] = $row; } } @@ -442,7 +446,11 @@ $existelements = true; $row = array(); $row[] = get_string($element,'scorm') != '[['.$element.']]' ? get_string($element,'scorm') : $element; - $row[] = s($value); + if (strpos($element, '_time') === false) { + $row[] = s($value); + } else { + $row[] = s(scorm_format_date_time($value)); + } $table->data[] = $row; } }