]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/scripts/public_html/common.php
fix
[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 sum_time($t1, $t2) {
29   $matches1 = array();
30   $matches2 = array();
31   $rex = "/^(\d+)m(\d{2})\.(\d{2})s$/";
32   $m1 = preg_match($rex,$t1,$matches1);
33   $m2 = preg_match($rex,$t2,$matches2);
34   if ($m1 != 0 && $m2 != 0) {
35     $t1_minutes = $matches1[1];
36     $t2_minutes = $matches2[1];
37     $t1_secs = $matches1[2];
38     $t2_secs = $matches2[2];
39     $t1_cents = $matches1[3];
40     $t2_cents = $matches2[3];
41     $time1 = ((int) $t1_cents) + ((int) $t1_secs) * 100 + ((int)$t1_minutes) * 6000 ;
42     $time2 = ((int) $t2_cents) + ((int) $t2_secs) * 100 + ((int)$t2_minutes) * 6000 ;
43     $sum = $time1 + $time2;
44     $min = $sum / 6000;
45     $sec = ($sum % 6000) / 100;
46     $cent = ($sum % 6000) % 100;
47     return sprintf("%dm%02d.%02ds",$min,$sec,$cent);
48   } else {
49     return $t1;
50   }
51 }
52
53 function group_array_by_mark($a) {
54   $rc = array();
55   foreach ($a as $x) {
56     if ($rc[$x['mark']] == NULL) {
57       $rc[$x['mark']] = $x;
58     } else {
59       foreach ($rc[$x['mark']] as $k => $v) {
60         $rc[$x['mark']][$k] = sum_time($v, $x[$k]);
61       }
62     }
63   }
64   return array_values($rc);
65 }
66   
67 function array_to_combo($l,$a) {
68   echo "<select name=\"$l\">";
69   echo "<option value=\"--\">--</option>";
70   foreach ($a as $k => $v) {
71     foreach( array_keys($v) as $k1 => $i) {
72       echo "<option value=\"{$v[$i]}\">{$v[$i]}</option>";
73     }
74   }
75   echo "</select>";
76 }
77
78 ?>