]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/scripts/public_html/common.php
ocaml 3.09 transition
[helm.git] / helm / matita / scripts / public_html / common.php
1 <?php
2
3 function query($q) {
4   $db = mysql_pconnect("localhost","helm");
5   mysql_select_db("matita");
6   if (preg_match("/TIME_TO_SEC/",$q)) {
7     $group_by = true;
8     $q = preg_replace("/group by mark/","",$q);
9     $q = preg_replace("/SEC_TO_TIME\(SUM\(TIME_TO_SEC\(([^)]+)\)\)\)/","$1",$q);
10   }
11   $rc = mysql_query($q,$db);
12   if(!$rc) {
13     die("Query failed: " . mysql_error());
14   }
15   $result = array();
16   while( $row = mysql_fetch_array($rc, MYSQL_ASSOC)){
17     $result[] = $row;
18   }
19   mysql_free_result($rc);
20   mysql_close($db);
21   if ($group_by){
22     return group_array_by_mark($result);
23   } else {
24     return $result;
25   }
26 }
27
28 function time_2_cents($t) {
29   $matches = array();
30   $rex = "/^(\d+)m(\d\d?)\.(\d{2})s$/";
31   $m = preg_match($rex,$t,$matches);
32   if ( $m == 0 ) exit(1);
33   $t_minutes = $matches[1];
34   $t_secs = $matches[2];
35   $t_cents = $matches[3];
36   return ((int) $t_cents) + ((int) $t_secs) * 100 + ((int)$t_minutes) * 6000 ;
37 }
38
39 function sum_time($t1, $t2) {
40   $matches1 = array();
41   $matches2 = array();
42   $rex = "/^(\d+)m(\d\d?)\.(\d{2})s$/";
43   $m1 = preg_match($rex,$t1,$matches1);
44   $m2 = preg_match($rex,$t2,$matches2);
45   if ($m1 != 0 && $m2 != 0) {
46     $t1_minutes = $matches1[1];
47     $t2_minutes = $matches2[1];
48     $t1_secs = $matches1[2];
49     $t2_secs = $matches2[2];
50     $t1_cents = $matches1[3];
51     $t2_cents = $matches2[3];
52     $time1 = ((int) $t1_cents) + ((int) $t1_secs) * 100 + ((int)$t1_minutes) * 6000 ;
53     $time2 = ((int) $t2_cents) + ((int) $t2_secs) * 100 + ((int)$t2_minutes) * 6000 ;
54     $sum = $time1 + $time2;
55     $min = $sum / 6000;
56     $sec = ($sum % 6000) / 100;
57     $cent = ($sum % 6000) % 100;
58     return sprintf("%dm%02d.%02ds",$min,$sec,$cent);
59   } else {
60     return $t1;
61   }
62 }
63
64 function group_array_by_mark($a) {
65   $rc = array();
66   foreach ($a as $x) {
67     if ($rc[$x['mark']] == NULL) {
68       $rc[$x['mark']] = $x;
69     } else {
70       foreach ($rc[$x['mark']] as $k => $v) {
71         $rc[$x['mark']][$k] = sum_time($v, $x[$k]);
72       }
73     }
74   }
75   return array_values($rc);
76 }
77   
78 function array_to_combo($l,$a) {
79   echo "<select name=\"$l\">";
80   echo "<option value=\"--\">--</option>";
81   foreach ($a as $k => $v) {
82     foreach( array_keys($v) as $k1 => $i) {
83       echo "<option value=\"{$v[$i]}\">{$v[$i]}</option>";
84     }
85   }
86   echo "</select>";
87 }
88
89 ?>