Example #1
0
 public function saveData($path)
 {
     $file = new File();
     $file->exercise_id = $this->exercise->id;
     $file->comment = strip_tags($this->comment);
     $file->content = $this->content;
     $file->url = $this->url;
     $file->uploaded_at = date('Y-m-d H:i:s');
     if ($this->uploadedfile instanceof CUploadedFile) {
         $file->original_name = $this->uploadedfile->name;
         $file->size = $this->uploadedfile->size;
         $file->type = $this->uploadedfile->type;
         $file->md5 = md5_file($this->uploadedfile->tempName);
     } else {
         $file->md5 = md5($this->url . $this->content);
     }
     $transaction = $file->getDbConnection()->beginTransaction();
     try {
         if (!$file->save()) {
             $this->addError('file', 'An error occured during file analysis.');
             return null;
         }
         if ($this->uploadedfile instanceof CUploadedFile) {
             $this->uploadedfile->saveAs($path . DIRECTORY_SEPARATOR . $file->id . '_' . $file->md5);
         }
         $this->exercise->linked_to = null;
         $this->exercise->increaseStatus(Exercise::STATUS_WORK_UPLOADED);
         $transaction->commit();
         return $file;
     } catch (Exception $e) {
         $this->addError('file', 'An error occured during file storing.');
         $transaction->rollback();
         return null;
     }
 }