uv_parser.inc.php 7.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php
/* Copyright 2011
 * - Antoine Tenart < antoine dot tenart at gmail dot com >
 *
 * Ce fichier fait partie du site de l'Association des Étudiants de
 * l'UTBM, http://ae.utbm.fr.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

/**
    Parse le mail du SME contenant l'affectation aux groupes et UVs.
**/

/* TODO : question des doubles horaires (ET)
          gestion de l'hors edt
          renommer la classe ??
*/

Antoine Tenart's avatar
meh  
Antoine Tenart committed
33
require_once($topdir . 'include/mysql.inc.php');
34 35
require_once($topdir . 'pedagogie/include/pedagogie.inc.php');

36 37 38
class UVParser
{
  // --- Protected vars
Antoine Tenart's avatar
Antoine Tenart committed
39
  protected $is_hedt;
Antoine Tenart's avatar
Antoine Tenart committed
40
  protected $id;
41
  protected $text;
Antoine Tenart's avatar
Antoine Tenart committed
42 43 44
  protected $uv;
  protected $semester;
  protected $type;
Antoine Tenart's avatar
Antoine Tenart committed
45
  protected $group;
46 47 48 49
  protected $begin_hour;
  protected $end_hour;
  protected $day;
  protected $room;
50
  protected $frequency;
Antoine Tenart's avatar
Antoine Tenart committed
51

52
  protected $db;
53 54 55 56 57 58 59 60
  protected $_target = array();
  protected $_results = array();

  // Rules
  protected $_phrase;
  protected $_title;
  protected $_info;
  protected $_schedule;
Antoine Tenart's avatar
foo  
Antoine Tenart committed
61
  protected $_uv = '([A-Z]{2}[0-9]{2})';
62 63 64 65 66 67 68 69 70
  protected $_type = '(?:(C|TD|TP)([0-9]))';
  protected $_day = '(L|MA|ME|J|V|S)';
  protected $_frequency = '(\(1SEMAINE\/2\))';
  protected $_hour = '([0-2]?[0-9]H[0-5][0-9])';
  protected $_room = '(?:en([A-Z][0-9]{1,3}[A-Z]?))';


  // --- public functions
  // constructor
71 72 73 74
  function UVParser(&$db, $semester = SEMESTRE_NOW) {
    $this->db = &$db;
    $this->semester = $semester;

75 76 77
    $this->_schedule = "$this->_hour$this->_hour";

    $this->_title = "$this->_uv$this->_type?";
78
    $this->_info = "(?:$this->_day$this->_schedule$this->_frequency?$this->_room|(HORSEMPLOIduTEMPS))";
79

Antoine Tenart's avatar
Antoine Tenart committed
80
    $this->_phrase = "$this->_title$this->_info";
81 82 83
  }

  // load text & parse it
Antoine Tenart's avatar
\_o<  
Antoine Tenart committed
84
  public function load_by_text($txt, $load_next = false) {
Antoine Tenart's avatar
plop  
Antoine Tenart committed
85
    $txt = preg_replace('/(.+):(.+)et(.+)/', "$1$2\n$1$3", $txt); // life is easy
86
    $txt = str_replace(array(' ', ':', '-'), '', $txt);
Antoine Tenart's avatar
rha  
Antoine Tenart committed
87
    $txt = $this->get_real_uv($txt);
88 89 90
    $this->_target = explode("\n",$txt);

    $this->parse();
Antoine Tenart's avatar
\_o<  
Antoine Tenart committed
91 92 93

    if( $load_next )
      $this->load_next();
94 95 96 97 98 99 100 101 102 103
  }

  // load next parsed UV, if any (usefull in a loop)
  public function load_next() {
    $foo = current($this->_results);
    next($this->_results);

    if(!$foo)
      return false;

104 105 106 107 108
    if( !is_numeric($foo[1][3]) )
      $this->uv = get_real_uv($foo[1]);
    else
      $this->uv = $foo[1];

Antoine Tenart's avatar
Antoine Tenart committed
109 110 111 112 113 114 115 116 117 118 119
    $this->text = $foo[0];

    if(isset($foo[9])) {
      $this->hedt = true;

      $this->type = 'THE';
      $this->group = 1;
      $this->day = 0;
      $this->begin_hour = "00H00";
      $this->end_hour = "00H00";
      $this->room = null;
120
      $this->frequency = 1;
Antoine Tenart's avatar
Antoine Tenart committed
121 122
    }
    else {
123
      $days = array('L' => 1, 'MA' => 2, 'ME' => 3, 'J' => 4, 'V' => 5, 'S' => 6);
Antoine Tenart's avatar
Antoine Tenart committed
124 125 126 127
      $this->hedt = false;

      $this->type = $foo[2];
      $this->group = $foo[3];
128
      $this->day = $days[$foo[4]];
Antoine Tenart's avatar
Antoine Tenart committed
129 130
      $this->begin_hour = $foo[5];
      $this->end_hour = $foo[6];
131
      $this->room = $this->get_real_room($foo[8]);
Antoine Tenart's avatar
Antoine Tenart committed
132 133 134 135 136 137

      if($foo[7] == '')
        $this->frequency = 1;
      else
        $this->frequency = 2;
    }
138

Antoine Tenart's avatar
Antoine Tenart committed
139
    $this->id = $this->load_id_uv();
Antoine Tenart's avatar
kwain  
Antoine Tenart committed
140

141 142 143
    return true;
  }

Antoine Tenart's avatar
Antoine Tenart committed
144 145 146 147
  public function get_id_uv() {
    return $this->id;
  }

148
  public function get_id_group() {
Antoine Tenart's avatar
Antoine Tenart committed
149
    $sql = "SELECT `id_groupe` FROM `pedag_groupe`";
Antoine Tenart's avatar
Antoine Tenart committed
150 151

    if( $this->is_hedt() ) {
Antoine Tenart's avatar
Antoine Tenart committed
152
      $sql .= " WHERE `id_uv` = ".$this->id." AND `type` = 'THE' AND `semestre` ='".$this->semester."' LIMIT 1";
Antoine Tenart's avatar
Antoine Tenart committed
153 154
    } else {
      $sql .= " WHERE `id_uv` = ".$this->id." AND `type` = '".$this->type."'";
Antoine Tenart's avatar
Antoine Tenart committed
155
      $sql .= " AND `debut` = '".str_replace('H', ':', $this->begin_hour)."' AND `jour` = ".$this->day." AND `salle` = '".$this->room."'";
156
      $sql .= " AND `semestre` = '".$this->semester."' LIMIT 1";
Antoine Tenart's avatar
Antoine Tenart committed
157
    }
158

Antoine Tenart's avatar
Antoine Tenart committed
159
    $req = new requete($this->db, $sql);
160

Antoine Tenart's avatar
kwain  
Antoine Tenart committed
161 162
    if($req->is_success()) {
      $res = $req->get_row();
163
      return $res['id_groupe'];
Antoine Tenart's avatar
kwain  
Antoine Tenart committed
164
    }
Antoine Tenart's avatar
o<  
Antoine Tenart committed
165

Antoine Tenart's avatar
Antoine Tenart committed
166
    return null;
167 168
  }

Antoine Tenart's avatar
Antoine Tenart committed
169
  public function get_uv() {
Antoine Tenart's avatar
meh  
Antoine Tenart committed
170 171
    if( !empty($this->uv) )
      return $this->uv;
Antoine Tenart's avatar
Antoine Tenart committed
172 173

    return null;
Antoine Tenart's avatar
Antoine Tenart committed
174 175
  }

176 177 178 179
  public function get_text() {
    return $this->text;
  }

180
  public function get_nice_print() {
Antoine Tenart's avatar
rha  
Antoine Tenart committed
181
    $plop = array( 'C' => 'Cours', 'TD' => 'Travaux dirigés', 'TP' => 'Travaux pratiques');
182
    $jours = array( 1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi');
183

Antoine Tenart's avatar
Antoine Tenart committed
184
    $ret = $plop[$this->type] . (preg_match('/^[A|E|U|I|O]$/', $this->uv[0]) ? ' d\'' : ' de ') . $this->uv;
Antoine Tenart's avatar
Antoine Tenart committed
185
    $ret .= ' le ' . $jours[$this->day] .' de ' . $this->begin_hour . ' à ' . $this->end_hour . ' en ' . $this->room . '.';
Antoine Tenart's avatar
Antoine Tenart committed
186 187

    return $ret;
188 189
  }

190
  public function get_info_add_group() {
Antoine Tenart's avatar
\_o<  
Antoine Tenart committed
191
    while(true) {
Antoine Tenart's avatar
\_o<  
Antoine Tenart committed
192
      $sql = "SELECT * FROM pedag_groupe WHERE `type` = '".$this->type."' AND `num_group` = ".$this->group;
Antoine Tenart's avatar
\_o<  
Antoine Tenart committed
193 194 195 196 197 198 199 200 201 202 203 204
      $sql .= " AND `id_uv` = ".$this->id;

      $req = new requete($this->db, $sql);

      if( $req->is_success() )
        if( isset( $req) ) {
          $this->group++;
          continue;
        }
      break;
    }

205 206 207 208
    return array( $this->type,
                  $this->group,
                  $this->frequency,
                  $this->semester,
209
                  $this->day,
210 211 212 213 214 215
                  str_replace('H', ':', $this->begin_hour),
                  str_replace('H', ':', $this->end_hour),
                  $this->room
        );
  }

216
  public function is_weekly() {
217
    return ($this->frequency == 1 ? true : false);
218 219
  }

Antoine Tenart's avatar
Antoine Tenart committed
220 221 222 223
  public function is_hedt() {
    return $this->hedt;
  }

224 225 226 227 228 229 230

  // --- protected functions
  // parse text loaded
  protected function parse() {
    while( $foo = current($this->_target) ) {
      preg_match('/'.$this->_phrase.'/', $foo, $matches);

Antoine Tenart's avatar
plop  
Antoine Tenart committed
231
      if($matches)
232 233 234 235 236 237
        $this->_results[] = $matches;

      next($this->_target);
    }
  }

238 239 240 241 242 243 244 245 246 247 248
  protected function get_real_room($room) {
    $seek = array(); $destroy = array();  // ste blague
    $matches = array(
        '/H11/' => 'H011'
        );

    while(list($s,$d) = each($matches)) {
      $seek[] = $s;
      $destroy[] = $d;
    }

Antoine Tenart's avatar
Antoine Tenart committed
249
    return preg_replace($seek, $destroy, $room);
250 251
  }

252 253
  // to put in bdd
  protected function get_real_uv($uv) {
254
    $seek = array(); $destroy = array(); // st'humour de répétition
255
    $matches = array(
Antoine Tenart's avatar
o<  
Antoine Tenart committed
256 257 258 259
        '/MT1[A-Z]/' => 'MT11',
        '/MT2[A-Z]/' => 'MT12',
        '/PS1[A-Z]/' => 'PS11',
        '/PS2[A-Z]/' => 'PS12',
Antoine Tenart's avatar
Antoine Tenart committed
260
        '/ST1[A-Z]/' => 'ST10',
Antoine Tenart's avatar
hem  
Antoine Tenart committed
261
        '/YO1[A-Z]/' => 'LO11'
262
        );
Antoine Tenart's avatar
Antoine Tenart committed
263 264 265 266 267

    while(list($s, $d) = each($matches)) {
      $seek[] = $s;
      $destroy[] = $d;
    }
268 269 270 271

    return preg_replace($seek, $destroy, $uv);
  }

Antoine Tenart's avatar
Antoine Tenart committed
272
  protected function load_id_uv() {
Antoine Tenart's avatar
kwain  
Antoine Tenart committed
273
    $sql = "SELECT id_uv FROM pedag_uv WHERE code='".$this->uv."' LIMIT 1";
Antoine Tenart's avatar
rha  
Antoine Tenart committed
274
    $req = new requete($this->db, $sql);
Antoine Tenart's avatar
kwain  
Antoine Tenart committed
275 276 277 278 279 280 281 282 283

    if( $req->is_success() ) {
      $res = $req->get_row();
      return $res['id_uv'];
    }
    else
      return null;
  }

284 285
}