134 lines
		
	
	
	
		
			4.4 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable file
		
	
	
	
	
			
		
		
	
	
			134 lines
		
	
	
	
		
			4.4 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable file
		
	
	
	
	
| <?php
 | |
| 
 | |
| namespace block_timetable\task;
 | |
|  
 | |
| /**
 | |
|  * An example of a scheduled task.
 | |
|  */
 | |
| class import_data extends \core\task\scheduled_task {
 | |
|  
 | |
|   /**
 | |
|   * Return the task's name as shown in admin screens.
 | |
|   *
 | |
|   * @return string
 | |
|   */
 | |
|   public function get_name() {
 | |
|      return get_string('import_data', 'block_timetable');
 | |
|   }
 | |
|  
 | |
|   /**
 | |
|    * Execute the task.
 | |
|    */
 | |
|   public function execute() {
 | |
|      global $DB,$CFG;
 | |
|      $filehash  = get_config('block_timetable', 'filehash');
 | |
|      $lastmtime = get_config('block_timetable', 'lastmtime');
 | |
|      $filenamel = get_config('timetable', 'fnamelesson');
 | |
|      $filenames = get_config('timetable', 'fnamesubst');
 | |
| 
 | |
|      if ($filenames && (file_exists ($filenames))) {
 | |
|         if (($handle = fopen($filenames, 'r')) !== FALSE) {
 | |
| 	   $hash = hash_file('md5', $filenames);
 | |
| 	   if ($hash != $filehash) {
 | |
| 	      $max_id = 0;
 | |
| 	      $max_changetime = 0;
 | |
| 	      if ($result = $DB->get_record_sql("select max(id), max(changetime) from {$CFG->prefix}timetable_substitution")) {
 | |
| 		$max_id = $result->{'max(id)'};
 | |
| 		$max_changetime = $result->{'max(changetime)'};
 | |
| 	      }
 | |
| 	      echo "Max id = $max_id\n";
 | |
| 	      echo "Max changetime = $max_changetime\n";
 | |
| 	      try {
 | |
|  	        $transaction = $DB->start_delegated_transaction();
 | |
|                 while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
 | |
| 	      	   $dataobject = (object)array('id' 		=> $data[0], 
 | |
| 		      			  'date'     		=> $data[1],
 | |
| 		      			  'period'  		=> $data[2],
 | |
| 		      			  'absence' 		=> $data[3],
 | |
| 		      			  'lesson'    		=> $data[4],
 | |
| 		      			  'teachera'		=> $data[5],
 | |
| 		      			  'teacherb'    	=> $data[6],
 | |
| 		      			  'subjecta'   		=> utf8_encode($data[7]),
 | |
| 		      			  'statistica'   	=> $data[8],
 | |
| 		      			  'subjectb'    	=> utf8_encode($data[9]),
 | |
| 		      			  'statisticb'   	=> $data[10],
 | |
| 		      			  'rooma'   		=> $data[11],
 | |
| 		      			  'roomb'   		=> $data[12],
 | |
| 		      			  'statisticflag'   	=> $data[13],
 | |
| 		      			  'classesa'   		=> utf8_encode($data[14]),
 | |
| 		      			  'reason'   		=> utf8_encode($data[15]),
 | |
| 		      			  'text'  	 	=> utf8_encode($data[16]),
 | |
| 		      			  'type'   		=> $data[17],
 | |
| 		      			  'classesb'   		=> utf8_encode($data[18]),
 | |
| 		      			  'substitutiontype'   	=> $data[19],
 | |
| 		      			  'changetime'   	=> $data[20],
 | |
| 					  'unknown' 		=> $data[21]);
 | |
| 		   //echo var_dump($dataobject);
 | |
| 		   foreach (get_object_vars($dataobject) as $key => $value) {
 | |
| 			   if ($value=='') unset($dataobject->{$key});
 | |
| 		   }
 | |
| 		   if ($dataobject->id > $max_id) {
 | |
| 			   $DB->insert_record_raw("timetable_substitution", $dataobject,false, false, true);
 | |
| 			   echo "Object (id={$data[0]}) inserted\n";
 | |
| 		   } elseif ($dataobject->changetime >= $max_changetime) {
 | |
| 	              $DB->update_record("timetable_substitution", $dataobject);
 | |
| 			   echo "Object (id={$data[0]}) updated\n";
 | |
| 		   }
 | |
|                 }
 | |
| 	   	$transaction->allow_commit();
 | |
| 		fclose($handle);
 | |
| 	   	set_config('filehash', $hash, 'block_timetable');
 | |
| 	      } catch(Exception $e) {
 | |
| 	        $transaction->rollback($e);
 | |
| 	      }
 | |
| 	   }
 | |
|         }
 | |
|      }
 | |
| 
 | |
| 
 | |
| 
 | |
|      //$filename = "/srv/stundenplan/lesson.txt"; 
 | |
|      echo "Lastmtime = $lastmtime\n";
 | |
|      if ($filenamel && (file_exists ($filenamel))) {
 | |
| 	$mtime = filemtime ( $filenamel );
 | |
|         echo "mtime = $mtime\n";
 | |
| 	if ($mtime<=$lastmtime) return;	
 | |
| 	echo "after return\n";
 | |
| 	try {
 | |
|  	   $transaction = $DB->start_delegated_transaction();
 | |
| 	   echo "before delete\n";
 | |
| 	   $DB->delete_records_select("timetable_lesson", "id>0");
 | |
| 	   echo "after delete\n";
 | |
| 	   $handle = @fopen($filenamel, "r");	   
 | |
| 	   echo "after handle\n";
 | |
| 	   while (($buffer = fgets($handle, 4096)) !== false) {
 | |
| //	      echo $buffer;
 | |
| 	      $buffer = utf8_encode(rtrim($buffer));
 | |
| 	      $data = explode("\t", $buffer);
 | |
| 	      if (count($data) != 10) throw new Exception('Wrong size of elements');
 | |
| 	      $dataobject = (object)array('teacher' => $data[0], 
 | |
| 		      			  'day'     => $data[1],
 | |
| 		      			  'period'  => $data[2],
 | |
| 		      			  'subject' => $data[3],
 | |
| 		      			  'room'    => $data[4],
 | |
| 		      			  'lessonid'=> $data[5],
 | |
| 		      			  'flag'    => $data[6],
 | |
| 		      			  'class'   => $data[7],
 | |
| 		      			  'week'    => $data[8],
 | |
| 					  'unknown' => $data[9]);
 | |
| 	       
 | |
| 	      $DB->insert_record("timetable_lesson", $dataobject);
 | |
| 	   }
 | |
| 	   $transaction->allow_commit();
 | |
| 	   $lastmtime = $mtime;
 | |
| 	   set_config('lastmtime', $lastmtime, 'block_timetable');
 | |
| 	} catch(Exception $e) {
 | |
| 	   $transaction->rollback($e);
 | |
| 	}
 | |
| 
 | |
|      }
 | |
|    // Apply fungus cream.
 | |
|    // Apply chainsaw.
 | |
|    // Apply olive oil.
 | |
|   }
 | |
| }
 | 
