Пример #1
0
 public function actualitza($courseYear, $date = NULL)
 {
     $this->logger = sfContext::getInstance()->getLogger();
     $this->courseYear = $courseYear;
     $this->sessions = array();
     $timetableDOM = new simple_html_dom();
     //echo $this->courseYear."<br>";
     //echo $timetableDOM->load_file($courseYear->getUrlHorari())."<br>";
     $timetableDOM = file_get_html($courseYear->getUrlHorari());
     if (!($timetableDOM && is_object($timetableDOM) && isset($timetableDOM->nodes))) {
         $this->logger->debug("La pàgina HTML és invàlida: " . $this->courseYear);
         return -1;
     }
     // Date of first week is in the first table, second row, second cell.
     $firstWeek = $timetableDOM->find('table', 0)->find('tr', 1)->find('td', 1)->plaintext;
     $currWeekNum = $this->calculateCurrentWeek($firstWeek, $date);
     $this->logger->info('Current week number is ' . $currWeekNum);
     $currWeekDOM = $timetableDOM->find('table', $currWeekNum);
     if ($date) {
         $currWeekStartDate = $this->weekStartDate(new DateTime($date));
     } else {
         $currWeekStartDate = $this->weekStartDate(new DateTime());
     }
     $this->deleteWeekSessions($currWeekStartDate->format('d.m.Y H:i:s'));
     $this->logger->info("Current week start date is " . $currWeekStartDate->format('d.m.Y H:i:s'));
     //echo "////// /////// /////// Current week start date is ". $currWeekStartDate->format('d.m.Y H:i:s');
     // Create sessions for each period in the timetable and save them to the database.
     $rows = $currWeekDOM->find('tr');
     for ($row = 0; $row < sizeof($rows); $row++) {
         if ($row > 1) {
             $cells = $rows[$row]->find('div');
             $period = new Period($cells[0]->plaintext);
             $period->setCourseYear($this->courseYear);
             $this->logger->info("Start time is " . $period->getStart()->format("H:i:s") . ". End time is " . $period->getEnd()->format("H:i:s"));
             for ($cell = 0; $cell < sizeof($cells); $cell++) {
                 if ($cell > 0) {
                     // Parse the start/end times for this row's period.
                     if (trim($cells[$cell]->plaintext) === '') {
                         $this->logger->debug("Skipping cell [" . $row . "][" . $cell . "], it's empty.");
                         continue;
                     }
                     // Session start date is the week's start date + current day of week. Time is provided in the first cell of each row
                     $sessionStartDateTime = new DateTime($currWeekStartDate->format('d.m.Y'));
                     $sessionStartDateTime->add(new DateInterval('P' . strval($cell - 1) . 'D'));
                     $sessionStartDateTime->setTime($period->getStart()->format('H'), $period->getStart()->format('i'));
                     $period->setStart($sessionStartDateTime);
                     $this->logger->debug('SessionStartDateTime is: ' . $sessionStartDateTime->format('d.m.Y H:i:s'));
                     // Agafem el timestamp de l'hora que acaba la classe i la guardem a la variable period
                     $sessionEndDateTime = new DateTime($currWeekStartDate->format('d.m.Y'));
                     $sessionEndDateTime->add(new DateInterval('P' . strval($cell - 1) . 'D'));
                     $sessionEndDateTime = $sessionEndDateTime->setTime($period->getEnd()->format('H'), $period->getEnd()->format('i'));
                     $period->setEnd($sessionEndDateTime);
                     $this->logger->debug('SessionEndDateTime is: ' . $sessionEndDateTime->format('d.m.Y H:i:s'));
                     //echo "*******LA PRUEBA DEL DELITO: ".$cells[$cell]->plaintext."**********<br>";
                     // Get the plaintext for the period and insert each line into an array.
                     $this->logger->debug("Periodinfoarray is: " . var_export($cells[$cell]->plaintext, true), 'err');
                     $periodInfo = html_entity_decode($cells[$cell]->plaintext, ENT_QUOTES, 'UTF-8');
                     $periodInfo = Encoding::toUTF8($periodInfo);
                     $periodInfoArray = explode("\n", $periodInfo);
                     foreach ($periodInfoArray as $i => $value) {
                         unset($periodInfoArray[$i]);
                         $periodInfoArray[] = trim($value);
                     }
                     // Re-index the array starting from zero.
                     $period->setDetails(array_values($periodInfoArray));
                     $this->logger->debug("Trimmed periodinfoarray is: " . var_export($period->getDetails(), true));
                     //echo "parsejant<br />";
                     $this->parseSession($period);
                 }
             }
             unset($period);
         }
     }
 }