Exemplo n.º 1
0
 /**
  * Perform minimal validation on the settings form
  *
  * @param   array  $data   Data to be validated
  * @param   array  $files  Files to be validated
  *
  * @return  array
  */
 public function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     // If no errors have been detecting on subtitle
     if (!isset($errors['subtitle'])) {
         global $USER;
         $fs = get_file_storage();
         $context = context_user::instance($USER->id);
         $files = $fs->get_area_files($context->id, 'user', 'draft', $data['subtitle'], 'id DESC', false);
         $noerror = false;
         // Loop on all files (normally only one)
         foreach ($files as $file) {
             // Save the file to a temporary path
             try {
                 $filepath = $file->copy_content_to_temp();
             } catch (\Exception $e) {
                 $errors['subtitle'] = get_string('subtitleunabletosave', 'elang');
                 break;
             }
             // Try to detect encoding
             $config = get_config('elang');
             if (isset($config->encodings)) {
                 $encodings = $config->encodings;
             } else {
                 $encodings = null;
             }
             $contents = Elang\transcodeSubtitle(file_get_contents($filepath), 'UTF-8', $encodings);
             if (false === $contents) {
                 // Automatic detection does not succeed
                 $errors['subtitle'] = get_string('subtitleunknownencoding', 'elang');
                 break;
             } else {
                 // Encoding succeeds, put back encoded contents in file
                 file_put_contents($filepath, $contents);
                 // Detect vtt format
                 try {
                     $caption = new \Captioning\Format\WebvttFile($filepath);
                     $caption->setUseIconv(function_exists('mb_convert_encoding'));
                     $noerror = true;
                     break;
                 } catch (\Exception $e) {
                 }
                 // Detect subrip format
                 try {
                     $caption = new \Captioning\Format\SubripFile($filepath);
                     $caption->setUseIconv(function_exists('mb_convert_encoding'));
                     $noerror = true;
                     break;
                 } catch (\Exception $e) {
                 }
             }
         }
         if ($noerror) {
             // $noerror means automatic detection of encoding has been successfull and the file is in vtt or subrip format
             $this->vtt = new \Captioning\Format\WebvttFile();
             // Construct the cues in vtt format
             $cues = $caption->getCues();
             if ($cues) {
                 foreach ($cues as $cue) {
                     $this->vtt->addCue($cue->getText(), \Captioning\Format\WebvttCue::ms2tc($cue->getStartMS()), \Captioning\Format\WebvttCue::ms2tc($cue->getStopMS()));
                 }
             }
         } elseif (!isset($errors['subtitle'])) {
             // File is not in vtt or subrip format
             $errors['subtitle'] = get_string('subtitleinvalidformat', 'elang');
         }
     }
     $jaro = str_replace(",", ".", $data['jaroDistance']);
     if (!is_numeric($jaro) || $jaro <= 0 || $jaro > 1) {
         $errors['jaroDistance'] = get_string('jaroDistance_error', 'elang');
     }
     return $errors;
 }