hdalab/filter.php
changeset 118 fdf808d7d374
parent 115 46c0f7a935d1
equal deleted inserted replaced
117:dc6c3ac62efa 118:fdf808d7d374
     2 header('Content-type: application/json');
     2 header('Content-type: application/json');
     3 
     3 
     4 require('connect.inc.php');
     4 require('connect.inc.php');
     5 
     5 
     6 $periode = (isset($_REQUEST['period']) ? pg_escape_string($_REQUEST['period']) : NULL);
     6 $periode = (isset($_REQUEST['period']) ? pg_escape_string($_REQUEST['period']) : NULL);
       
     7 $pays = (isset($_REQUEST['country']) ? pg_escape_string($_REQUEST['country']) : NULL);
     7 $label = (isset($_REQUEST['label']) ? pg_escape_string($_REQUEST['label']) : NULL);
     8 $label = (isset($_REQUEST['label']) ? pg_escape_string($_REQUEST['label']) : NULL);
     8 $contentlist = (isset($_REQUEST['contentlist']) ? pg_escape_string($_REQUEST['contentlist']) : NULL);
     9 $contentlist = (isset($_REQUEST['contentlist']) ? pg_escape_string($_REQUEST['contentlist']) : NULL);
     9 $max_tag_order = (isset($_REQUEST['mto']) ? pg_escape_string($_REQUEST['mto']) : 12);
    10 $max_tag_order = (isset($_REQUEST['mto']) ? pg_escape_string($_REQUEST['mto']) : 12);
    10 $content_count = (isset($_REQUEST['contentcount']) ? pg_escape_string($_REQUEST['contentcount']) : 12);
    11 $content_count = (isset($_REQUEST['contentcount']) ? pg_escape_string($_REQUEST['contentcount']) : 12);
    11 $tag_count = (isset($_REQUEST['tagcount']) ? pg_escape_string($_REQUEST['tagcount']) : 30);
    12 $tag_count = (isset($_REQUEST['tagcount']) ? pg_escape_string($_REQUEST['tagcount']) : 30);
    12 $cont_count = 0;
    13 $cont_count = 0;
    13 
    14 
    14 if ($label or $periode or gettype($contentlist) != "NULL") {
    15 if ($label or $pays or $periode or gettype($contentlist) != "NULL") {
    15     $globalsql = "SELECT id, title, description, url FROM hdabo_datasheet E";
    16     $globalsql = "SELECT id, title, description, url, latitude, longitude FROM hdabo_datasheet E LEFT JOIN hdaviz_datasheet_insee K ON E.id = K.datasheet_id LEFT JOIN hdaviz_insee_coords Q ON K.insee_code = Q.code_insee";
    16     $globalids = array();
    17     $globalids = array();
    17     $globalfilters = array();
    18     $globalfilters = array("validated=TRUE");
    18     if ($periode) {
    19     if ($periode) {
    19         $years = split(',',$periode);
    20         $years = split(',',$periode);
    20         $start_year = $years[0];
    21         $start_year = $years[0];
    21         $end_year = (count($years) > 1 ? $years[1] : $start_year);
    22         $end_year = (count($years) > 1 ? $years[1] : $start_year);
    22         $delta = max( 1, floor( ($end_year - $start_year ) / 2 ) );
    23         $delta = max( 1, floor( ($end_year - $start_year ) / 2 ) );
    29         }
    30         }
    30         pg_free_result($rq);
    31         pg_free_result($rq);
    31         $globalids = $ids;
    32         $globalids = $ids;
    32         $globalfilters[] = "EXISTS (SELECT * FROM hdabo_taggedsheet F WHERE F.tag_id IN (" . join(",", $ids) . ") AND F.order <= $max_tag_order AND F.datasheet_id = E.id)";
    33         $globalfilters[] = "EXISTS (SELECT * FROM hdabo_taggedsheet F WHERE F.tag_id IN (" . join(",", $ids) . ") AND F.order <= $max_tag_order AND F.datasheet_id = E.id)";
    33     }
    34     }
       
    35     if ($pays) {
       
    36         $listepays = split(',',$pays);
       
    37         foreach ($listepays as $unpays) {
       
    38             $rq = pg_query("SELECT id FROM hdaviz_inclusiongeo WHERE isocode='$unpays'");
       
    39             $ids = array();
       
    40             while($ligne = pg_fetch_row($rq)) {
       
    41                 $ids[] = $ligne[0];
       
    42             }
       
    43             pg_free_result($rq);
       
    44             $globalids = array_merge($globalids,$ids);
       
    45             $globalfilters[] = "EXISTS (SELECT * FROM hdabo_taggedsheet G WHERE G.tag_id IN (" . join(",", $ids) . ") AND G.order <= $max_tag_order AND G.datasheet_id = E.id)";
       
    46         }
       
    47     }
    34     if ($label) {
    48     if ($label) {
    35         $lblist = split(',',$label);
    49         $lblist = split(',',$label);
    36         foreach ($lblist as $txtlbl) {
    50         foreach ($lblist as $txtlbl) {
    37             $rq = pg_query("SELECT id FROM hdabo_tag WHERE label ILIKE '$txtlbl'");
    51             $rq = pg_query("SELECT id FROM hdabo_tag WHERE label ILIKE '$txtlbl'");
    38             $ids = array();
    52             $ids = array();
    62         $ligne["score"] = 0;
    76         $ligne["score"] = 0;
    63         $ligne["tags"] = array();
    77         $ligne["tags"] = array();
    64         $contenus[$ligne["id"]] = $ligne;
    78         $contenus[$ligne["id"]] = $ligne;
    65     }
    79     }
    66     pg_free_result($rq);
    80     pg_free_result($rq);
    67     $rq = pg_query("SELECT label, isocode FROM hdaviz_cc");
       
    68     $countries = array();
       
    69     while($ligne = pg_fetch_row($rq)) {
       
    70         $countries[$ligne[0]] = array("isocode" => $ligne[1], "score" => 0);
       
    71     }
       
    72     pg_free_result($rq);
       
    73     $rq = pg_query("SELECT DISTINCT label FROM hdabo_tag WHERE category_id=5");
    81     $rq = pg_query("SELECT DISTINCT label FROM hdabo_tag WHERE category_id=5");
    74     $disciplines = array();
    82     $disciplines = array();
    75     while($ligne = pg_fetch_row($rq)) {
    83     while($ligne = pg_fetch_row($rq)) {
    76         $disciplines[$ligne[0]] = array("label" => $ligne[0], "score" => 0);
    84         $disciplines[$ligne[0]] = array("label" => $ligne[0], "score" => 0);
    77     }
    85     }
    78     pg_free_result($rq);
    86     pg_free_result($rq);
    79     $tags = array();
    87     $tags = array();
    80     $contentids = join(",",array_keys($contenus));
    88     $contentids = join(",",array_keys($contenus));
    81     if ($contentids) {
    89     if ($contentids) {
    82         $rq = pg_query("SELECT L.datasheet_id, L.tag_id, M.label, L.order FROM hdabo_taggedsheet L, hdabo_tag M WHERE L.datasheet_id IN ($contentids) AND L.tag_id = M.id AND L.order <= $max_tag_order ORDER BY L.order ASC");
    90         $rq = pg_query("SELECT L.datasheet_id, L.tag_id, M.label, L.order, M.category_id FROM hdabo_taggedsheet L, hdabo_tag M WHERE L.datasheet_id IN ($contentids) AND L.tag_id = M.id AND L.order <= $max_tag_order ORDER BY L.order ASC");
    83         while($ligne = pg_fetch_row($rq)) {
    91         while($ligne = pg_fetch_row($rq)) {
    84             $match_tag = in_array($ligne[1], $globalids);
    92             $match_tag = in_array($ligne[1], $globalids);
    85             $contenus[$ligne[0]]["tags"][] = array("id" => $ligne[1], "label" => $ligne[2], "order" => $ligne[3], "match" => $match_tag);
    93             $contenus[$ligne[0]]["tags"][] = array("id" => $ligne[1], "label" => $ligne[2], "order" => $ligne[3], "match" => $match_tag);
    86             $tagscore = 2*$max_tag_order-$ligne[3];
    94             $tagscore = 2*$max_tag_order-$ligne[3];
    87             if (!isset($tags[$ligne[1]])) {
    95             if (!in_array($ligne[4], array(1,2,5))) {
    88                 $tags[$ligne[1]] = array("id" => $ligne[1], "label" => $ligne[2], "score" => 0);
    96                 if (!isset($tags[$ligne[1]])) {
    89             }
    97                     $tags[$ligne[1]] = array("id" => $ligne[1], "label" => $ligne[2], "score" => 0);
    90             $tags[$ligne[1]]["score"] += $tagscore;
    98                 }
       
    99                 $tags[$ligne[1]]["score"] += $tagscore;
       
   100                 if ($match_tag) {
       
   101                     $tags[$ligne[1]]["match"] = True;
       
   102                 }
       
   103             }
    91             if ($match_tag) {
   104             if ($match_tag) {
    92                 $contenus[$ligne[0]]["score"] += $tagscore;
   105                 $contenus[$ligne[0]]["score"] += $tagscore;
    93                 $tags[$ligne[1]]["match"] = True;
   106             }
    94             }
   107 /*            if (isset($countries[$ligne[2]])) {
    95             if (isset($countries[$ligne[2]])) {
       
    96                 $countries[$ligne[2]]["score"] += $tagscore;
   108                 $countries[$ligne[2]]["score"] += $tagscore;
    97             }
   109             }
       
   110  */
    98             if (isset($disciplines[$ligne[2]])) {
   111             if (isset($disciplines[$ligne[2]])) {
    99                 $disciplines[$ligne[2]]["score"] += $tagscore;
   112                 $disciplines[$ligne[2]]["score"] += $tagscore;
   100             }
   113             }
   101         }
   114         }
   102         pg_free_result($rq);
   115         pg_free_result($rq);
   110     } else {
   123     } else {
   111         $contenus = array_values($contenus);
   124         $contenus = array_values($contenus);
   112     }
   125     }
   113     usort($tags,"triscore");
   126     usort($tags,"triscore");
   114     usort($disciplines,"triscore");
   127     usort($disciplines,"triscore");
   115     $countries = array_values($countries);
   128 /*    $countries = array_values($countries);
       
   129  */
   116     $tags = array_slice($tags, 0, $tag_count);
   130     $tags = array_slice($tags, 0, $tag_count);
   117     $disciplines = array_slice($disciplines, 0, 10);
   131     $disciplines = array_slice($disciplines, 0, 10);
   118     $years = array();
   132     $years = array();
       
   133     $countries = array();
   119     if ($contentids) {
   134     if ($contentids) {
   120         $rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - V.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet V WHERE U.tag_id = V.tag_id AND V.order <= $max_tag_order AND V.datasheet_id IN ($contentids) GROUP BY U.start_year, U.end_year");
   135         $rq = pg_query("SELECT U.start_year, U.end_year, COUNT(*) score FROM hdaviz_years U, hdabo_taggedsheet V WHERE U.tag_id = V.tag_id AND V.order <= $max_tag_order AND V.datasheet_id IN ($contentids) GROUP BY U.start_year, U.end_year");
   121         while($ligne = pg_fetch_row($rq)) {
   136         while($ligne = pg_fetch_row($rq)) {
   122             foreach(range($ligne[0], $ligne[1]) as $year) {
   137             foreach(range($ligne[0], $ligne[1]) as $year) {
   123                 $years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 );
   138                 $years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 );
   124             }
   139             }
   125         }
   140         }
   126         pg_free_result($rq);
   141         pg_free_result($rq);
       
   142         $rq = pg_query("SELECT U.isocode, COUNT(*) FROM hdaviz_inclusiongeo U, hdabo_taggedsheet V WHERE U.id = V.tag_id AND V.order <= $max_tag_order AND V.datasheet_id IN ($contentids) GROUP BY U.isocode");
       
   143         while($ligne = pg_fetch_row($rq)) {
       
   144             $countries[$ligne[0]] = $ligne[1];
       
   145         }
       
   146         pg_free_result($rq);
   127     }
   147     }
   128 } else {
   148 } else {
   129     $rq = pg_query("SELECT id, title, description, url FROM hdabo_datasheet ORDER BY RANDOM() LIMIT $content_count");
   149     $rq = pg_query("SELECT id, title, description, url, latitude, longitude FROM hdabo_datasheet E LEFT JOIN hdaviz_datasheet_insee K ON E.id = K.datasheet_id LEFT JOIN hdaviz_insee_coords Q ON K.insee_code = Q.code_insee WHERE validated=TRUE ORDER BY RANDOM() LIMIT $content_count");
   130     while($ligne = pg_fetch_assoc($rq)) {
   150     while($ligne = pg_fetch_assoc($rq)) {
   131         $ligne["tags"] = array();
   151         $ligne["tags"] = array();
   132         $contenus[$ligne["id"]] = $ligne;
   152         $contenus[$ligne["id"]] = $ligne;
   133     }
   153     }
   134     $rq = pg_query("SELECT COUNT(*) FROM hdabo_datasheet");
   154     $rq = pg_query("SELECT COUNT(*) FROM hdabo_datasheet WHERE validated=TRUE");
   135     $ligne = pg_fetch_row($rq);
   155     $ligne = pg_fetch_row($rq);
   136     $cont_count = $ligne[0];
   156     $cont_count = $ligne[0];
   137     pg_free_result($rq);
   157     pg_free_result($rq);
   138     $rq = pg_query("SELECT L.datasheet_id, L.tag_id, M.label, L.order FROM hdabo_taggedsheet L, hdabo_tag M WHERE L.datasheet_id IN (".join(",",array_keys($contenus)).") AND L.tag_id = M.id AND L.order <= $max_tag_order ORDER BY L.order ASC");
   158     $rq = pg_query("SELECT L.datasheet_id, L.tag_id, M.label, L.order FROM hdabo_taggedsheet L, hdabo_tag M WHERE L.datasheet_id IN (".join(",",array_keys($contenus)).") AND L.tag_id = M.id AND L.order <= $max_tag_order ORDER BY L.order ASC");
   139     while($ligne = pg_fetch_row($rq)) {
   159     while($ligne = pg_fetch_row($rq)) {
   140         $contenus[$ligne[0]]["tags"][] = array("id" => $ligne[1], "label" => $ligne[2], "order" => $ligne[3]);
   160         $contenus[$ligne[0]]["tags"][] = array("id" => $ligne[1], "label" => $ligne[2], "order" => $ligne[3]);
   141     }
   161     }
   142     pg_free_result($rq);
   162     pg_free_result($rq);
   143     $contenus = array_values($contenus);
   163     $contenus = array_values($contenus);
   144     $rq = pg_query("SELECT hdabo_tag.id, label, SUM(".(2*$max_tag_order)." - hdabo_taggedsheet.order) score FROM hdabo_tag, hdabo_taggedsheet WHERE hdabo_tag.id = hdabo_taggedsheet.tag_id AND hdabo_taggedsheet.order <= $max_tag_order GROUP BY hdabo_tag.id, label ORDER BY score DESC LIMIT $tag_count");
   164     
       
   165     $rq = pg_query("SELECT label, SUM(".(2*$max_tag_order)." - V.order) score FROM hdabo_tag U, hdabo_taggedsheet V WHERE U.id = V.tag_id AND V.order <= $max_tag_order AND category_id NOT IN (1,2,5) AND EXISTS (SELECT * FROM hdabo_datasheet W WHERE V.datasheet_id = W.id AND W.validated=TRUE) GROUP BY label ORDER BY score DESC LIMIT $tag_count");
   145     $tags = pg_fetch_all($rq);
   166     $tags = pg_fetch_all($rq);
   146     pg_free_result($rq);
   167     pg_free_result($rq);
   147     $rq = pg_query("SELECT C.isocode, SUM(".(2*$max_tag_order)." - B.order) score FROM hdabo_tag A, hdabo_taggedsheet B, hdaviz_cc C WHERE A.id = B.tag_id AND B.order <= $max_tag_order AND A.label = C.label GROUP BY C.isocode");
   168     
   148     $countries = pg_fetch_all($rq);
   169     $rq = pg_query("SELECT A.label, COUNT(*) score FROM hdabo_tag A, hdabo_taggedsheet B WHERE A.id = B.tag_id AND B.order <= $max_tag_order AND A.category_id=5 AND EXISTS (SELECT * FROM hdabo_datasheet W WHERE B.datasheet_id = W.id AND W.validated=TRUE) GROUP BY A.label ORDER BY score DESC LIMIT 10");
   149     pg_free_result($rq);
       
   150     $rq = pg_query("SELECT A.label, SUM(".(2*$max_tag_order)." - B.order) score FROM hdabo_tag A, hdabo_taggedsheet B WHERE A.id = B.tag_id AND B.order <= $max_tag_order AND A.category_id=5 GROUP BY A.label ORDER BY score DESC LIMIT 10");
       
   151     $disciplines = pg_fetch_all($rq);
   170     $disciplines = pg_fetch_all($rq);
   152     pg_free_result($rq);
   171     pg_free_result($rq);
   153     $rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - hdabo_taggedsheet.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet WHERE U.tag_id = hdabo_taggedsheet.tag_id AND hdabo_taggedsheet.order <= $max_tag_order GROUP BY U.start_year, U.end_year");
   172     
       
   173     $rq = pg_query("SELECT U.start_year, U.end_year, COUNT(*) FROM hdaviz_years U, hdabo_taggedsheet V WHERE U.tag_id = V.tag_id AND V.order <= $max_tag_order AND EXISTS (SELECT * FROM hdabo_datasheet W WHERE V.datasheet_id = W.id AND W.validated=TRUE) GROUP BY U.start_year, U.end_year");
   154     $years = array();
   174     $years = array();
   155     while($ligne = pg_fetch_row($rq)) {
   175     while($ligne = pg_fetch_row($rq)) {
   156         foreach(range($ligne[0], $ligne[1]) as $year) {
   176         foreach(range($ligne[0], $ligne[1]) as $year) {
   157             $years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 );
   177             $years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 );
   158         }
   178         }
       
   179     }
       
   180     pg_free_result($rq);
       
   181     $countries = array();
       
   182     $rq = pg_query("SELECT U.isocode, COUNT(*) FROM hdaviz_inclusiongeo U, hdabo_taggedsheet V WHERE U.id = V.tag_id AND V.order <= $max_tag_order AND EXISTS (SELECT * FROM hdabo_datasheet W WHERE V.datasheet_id = W.id AND W.validated=TRUE) GROUP BY U.isocode");
       
   183     while($ligne = pg_fetch_row($rq)) {
       
   184         $countries[$ligne[0]] = $ligne[1];
   159     }
   185     }
   160     pg_free_result($rq);
   186     pg_free_result($rq);
   161 }
   187 }
   162 ksort($years);
   188 ksort($years);
   163 $yearchange = array();
   189 $yearchange = array();