X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fmatita%2Fscripts%2Fpublic_html%2Fcommon.php;h=e1bef4dd71599bd3385a8ab28749f239f062d358;hb=4167cea65ca58897d1a3dbb81ff95de5074700cc;hp=09de758b281cad9f24450066c0f94d9911a6df33;hpb=663f8e1c82196b9e3f817bf059edcfcfe22993c3;p=helm.git diff --git a/helm/matita/scripts/public_html/common.php b/helm/matita/scripts/public_html/common.php index 09de758b2..e1bef4dd7 100644 --- a/helm/matita/scripts/public_html/common.php +++ b/helm/matita/scripts/public_html/common.php @@ -3,6 +3,11 @@ function query($q) { $db = mysql_pconnect("localhost","helm"); mysql_select_db("matita"); + if (preg_match("/TIME_TO_SEC/",$q)) { + $group_by = true; + $q = preg_replace("/group by mark/","",$q); + $q = preg_replace("/SEC_TO_TIME\(SUM\(TIME_TO_SEC\(([^)]+)\)\)\)/","$1",$q); + } $rc = mysql_query($q,$db); if(!$rc) { die("Query failed: " . mysql_error()); @@ -13,8 +18,61 @@ function query($q) { } mysql_free_result($rc); mysql_close($db); + if ($group_by){ + return group_array_by_mark($result); + } else { + return $result; + } +} + +function time_2_cents($t) { + $matches = array(); + $rex = "/^(\d+)m(\d\d?)\.(\d{2})s$/"; + $m = preg_match($rex,$t,$matches); + if ( $m == 0 ) exit(1); + $t_minutes = $matches[1]; + $t_secs = $matches[2]; + $t_cents = $matches[3]; + return ((int) $t_cents) + ((int) $t_secs) * 100 + ((int)$t_minutes) * 6000 ; +} - return $result; +function sum_time($t1, $t2) { + $matches1 = array(); + $matches2 = array(); + $rex = "/^(\d+)m(\d\d?)\.(\d{2})s$/"; + $m1 = preg_match($rex,$t1,$matches1); + $m2 = preg_match($rex,$t2,$matches2); + if ($m1 != 0 && $m2 != 0) { + $t1_minutes = $matches1[1]; + $t2_minutes = $matches2[1]; + $t1_secs = $matches1[2]; + $t2_secs = $matches2[2]; + $t1_cents = $matches1[3]; + $t2_cents = $matches2[3]; + $time1 = ((int) $t1_cents) + ((int) $t1_secs) * 100 + ((int)$t1_minutes) * 6000 ; + $time2 = ((int) $t2_cents) + ((int) $t2_secs) * 100 + ((int)$t2_minutes) * 6000 ; + $sum = $time1 + $time2; + $min = $sum / 6000; + $sec = ($sum % 6000) / 100; + $cent = ($sum % 6000) % 100; + return sprintf("%dm%02d.%02ds",$min,$sec,$cent); + } else { + return $t1; + } +} + +function group_array_by_mark($a) { + $rc = array(); + foreach ($a as $x) { + if ($rc[$x['mark']] == NULL) { + $rc[$x['mark']] = $x; + } else { + foreach ($rc[$x['mark']] as $k => $v) { + $rc[$x['mark']][$k] = sum_time($v, $x[$k]); + } + } + } + return array_values($rc); } function array_to_combo($l,$a) {