示例#1
0
 /**
  * Allows the upload of a file.
  */
 public function actionUpload($code = '')
 {
     $model = new UploadForm();
     if (!Yii::app()->user->isGuest) {
         $model->byteacher = true;
     }
     $model->code = $code;
     $model->setUrlExample();
     if (isset($_POST['UploadForm'])) {
         $model->uploadedfile = CUploadedFile::getInstance($model, 'uploadedfile');
         $model->attributes = $_POST['UploadForm'];
         if ($model->validate()) {
             if ($file = $model->saveData(Yii::app()->basePath . DIRECTORY_SEPARATOR . Helpers::getYiiParam('uploadDirectory'))) {
                 if (!$model->byteacher) {
                     if ($file->exercise->assignment->notification) {
                         MailTemplate::model()->mailFromTemplate('new_work_notification', Helpers::getYiiParam('adminEmail'), array('student' => $model->exercise->student, 'file' => $file, 'url' => $this->createAbsoluteSslUrl('file/view', array('id' => $file->id, 'hash' => $file->md5))));
                     }
                     if ($model->exercise->student->email) {
                         MailTemplate::model()->mailFromTemplate('new_work_acknowledgement', array($model->exercise->student->email => $model->exercise->student), array('student' => $model->exercise->student, 'file' => $file, 'url' => $this->createAbsoluteSslUrl('file/view', array('id' => $file->id, 'hash' => $file->md5))));
                         Yii::app()->getUser()->setFlash('success', 'Work correctly uploaded / saved. An email has been sent to your address.');
                     }
                 } else {
                     Yii::app()->getUser()->setFlash('success', 'Work correctly uploaded / saved.');
                 }
                 $this->redirect(array('file/view', 'id' => $file->id, 'hash' => $file->md5, 'status' => 1));
             } else {
                 Yii::app()->getUser()->setFlash('error', 'The work could not be saved.');
             }
         }
     }
     $this->render('upload', array('model' => $model));
 }