]> git.mjollnir.org Git - moodle.git/commitdiff
SCORM MDL-18835 display time periods in "human-readable" format. - mainly for SCORM...
authorDan Marsden <Dan@digitalclay.co.nz>
Fri, 18 Dec 2009 10:10:04 +0000 (10:10 +0000)
committerDan Marsden <Dan@digitalclay.co.nz>
Fri, 18 Dec 2009 10:10:04 +0000 (10:10 +0000)
mod/scorm/locallib.php
mod/scorm/report.php
mod/scorm/simpletest/test_formatdatetime.php [new file with mode: 0644]

index d8811c2703edb2df87dca1b741e47cd46aeebc07..e1a75dfd45119fbf7d64a068df5d565ca7f6f3fd 100755 (executable)
@@ -1189,4 +1189,43 @@ function scorm_delete_attempt($userid, $scormid, $attemptid) {
     $DB->delete_records('scorm_scoes_track', array('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;
+}
index bec0139e0b041bc9e95809e20a6faf87fe7b49e3..9c5ca6ee6d3ae988c2318acdf40b16aabd98cfc3 100755 (executable)
                                 $row[] = '<img src="'.$OUTPUT->pix_url('pix/' . $trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.
                                          $strstatus.'" />&nbsp;'.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 {
             }
             $strstatus = get_string($trackdata->status,'scorm');
             echo '<img src="'.$$OUTPUT->pix_url('pix/'.$trackdata->status, 'scorm').'" alt="'.$strstatus.'" title="'.
-            $strstatus.'" />&nbsp;'.$trackdata->total_time.'<br />'.$scoreview.'<br />';
+            $strstatus.'" />&nbsp;'.scorm_format_date_time($trackdata->total_time).'<br />'.$scoreview.'<br />';
             echo '</div>'."\n";
             echo '<hr /><h2>'.get_string('details','scorm').'</h2>';
 
                     $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;
                 }
             }
                         $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;
                     }
                 }
diff --git a/mod/scorm/simpletest/test_formatdatetime.php b/mod/scorm/simpletest/test_formatdatetime.php
new file mode 100644 (file)
index 0000000..f72f793
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+if (!defined('MOODLE_INTERNAL')) {
+    die('Direct access to this script is forbidden.'); //  It must be included from a Moodle page
+}
+
+// Unit tests for scorm_formatdatetime function from locallib.php
+// Please note that they're made to work only with English language strings,
+// so you have to set up English in Moodle settings first.
+
+// Make sure the code being tested is accessible.
+require_once($CFG->dirroot . '/mod/scorm/locallib.php'); // Include the code to test
+class scorm_formatdatetime_test extends UnitTestCase {
+    function test_scorm2004_format() {
+        $stryears = get_string('years');
+        $strmonths = trim(get_string('nummonths'));
+        $strdays = get_string('days');
+        $strhours = get_string('hours');
+        $strminutes = get_string('minutes');
+        $strseconds = get_string('seconds');
+
+        $SUTs = array(1=>'PT001H012M0043.12S', 2=>'PT15.3S', 3=>'P01Y02M5DT0H7M', 4=>'P0Y0M0DT0H1M00.00S',
+                      5=>'P1YT15M00.01S', 6=>'P0Y0M0DT0H0M0.0S', 7=>'P1MT4M0.30S', 8=>'PT', 9=>'P1DT2H3S', 10=>'P4M');
+        $validates = array(1=>"1 $strhours 12 $strminutes 43.12 $strseconds", 2=>"15.3 $strseconds", 3=>"1 $stryears 2 $strmonths 5 $strdays 7 $strminutes ",
+                           4=>"1 $strminutes ", 5=>"1 $stryears 15 $strminutes 0.01 $strseconds", 6=>'', 7=>"1 $strmonths 4 $strminutes 0.30 $strseconds",
+                           8=>'', 9=>"1 $strdays 2 $strhours 3 $strseconds", 10=>"4 $strmonths ");
+        foreach ($SUTs as $key=>$SUT) {
+            $formatted = scorm_format_date_time($SUT);
+            $this->assertEqual($formatted, $validates[$key]);
+        }
+    }
+    function test_scorm12_format() {
+        $stryears = get_string('years');
+        $strmonths = get_string('months');
+        $strdays = get_string('days');
+        $strhours = get_string('hours');
+        $strminutes = get_string('minutes');
+        $strseconds = get_string('seconds');
+
+        $SUTs = array(1=>'00:00:00', 2=>'1:2:3', 3=>'12:34:56.78', 4=>'00:12:00.03', 5=>'01:00:23', 6=>'00:12:34.00',
+                      7=>'00:01:02.03', 8=>'00:00:00.1', 9=>'1:23:00', 10=>'2:00:00');
+        $validates = array(1=>'', 2=>"1 $strhours 2 $strminutes 3 $strseconds", 3=>"12 $strhours 34 $strminutes 56.78 $strseconds",
+                           4=>"12 $strminutes 0.03 $strseconds", 5=>"1 $strhours 23 $strseconds", 6=>"12 $strminutes 34 $strseconds",
+                           7=>"1 $strminutes 2.03 $strseconds", 8=>"0.1 $strseconds", 9=>"1 $strhours 23 $strminutes ", 10=>"2 $strhours ");
+        foreach ($SUTs as $key=>$SUT) {
+            $formatted = scorm_format_date_time($SUT);
+            $this->assertEqual($formatted, $validates[$key]);
+        }
+    }
+    
+    function test_non_datetime() {
+    }
+}