/** * Tests functions related to note extension. */ public function testExtension() { $note1 = $this->notes('note1'); $note2 = $this->notes('note2'); $note3 = $this->notes('note3'); $allowedTypes = Note::getAllowedTypes(); $this->assertEquals(3, count($allowedTypes)); $this->assertTrue(in_array(array('extension' => 'pdf', 'name' => 'PDF'), $allowedTypes)); $this->assertTrue(in_array(array('extension' => 'jpg', 'name' => 'Gambar'), $allowedTypes)); $this->assertTrue(in_array(array('extension' => 'htm', 'name' => 'Teks'), $allowedTypes)); $this->assertEquals('pdf', $note1->getExtension()); $this->assertEquals('jpg', $note2->getExtension()); $this->assertEquals('htm', $note3->getExtension()); $this->assertFileExists($note1->getTypeIcon()); $this->assertFileExists($note2->getTypeIcon()); $this->assertFileExists($note3->getTypeIcon()); $typeNames = Note::getTypeNames(); $this->assertEquals(3, count($typeNames)); $this->assertTrue(in_array('PDF', $typeNames)); $this->assertTrue(in_array('Gambar', $typeNames)); $this->assertTrue(in_array('Teks', $typeNames)); for ($i = 0; $i < count($allowedTypes); $i++) { $type = $allowedTypes[$i]; $this->assertEquals($i, Note::getTypeFromExtension($type['extension'])); } $this->assertEquals(-1, Note::getTypeFromExtension('mp3')); }
/** * Uploads a note. */ public function actionUpload() { $model = new Note(); if (isset($_POST['Note'])) { $model->attributes = $_POST['Note']; if ($model->validate()) { // sets extension $extension = 'htm'; if (empty($model->raw_file_text)) { $noteFile = CUploadedFile::getInstance($model, 'file'); $extension = $noteFile->extensionName; } $model->type = Note::getTypeFromExtension($extension); $model->save(false); // saves file $filePath = Yii::app()->params['notesDir']; if (empty($model->raw_file_text)) { $noteFile = CUploadedFile::getInstance($model, 'file'); $noteFile->saveAs($filePath . $model->id . '.' . $noteFile->extensionName); } else { touch($filePath . $model->id . '.htm'); file_put_contents($filePath . $model->id . '.htm', $model->raw_file_text); } $event = new UploadEvent($this); $event->student = $model->student; $this->onNewUpload($event); $message['text'] = 'Berkas berhasil diunggah.'; $message['type'] = 'general'; $message['default_text'] = 'Saya baru saja mengunggah ' . $model->title . ' pada BerKuliah!'; $message['name'] = $model->title; $message['link'] = array('note/view', 'id' => $model->id); $message['picture'] = $model->getTypeIcon(); $message['caption'] = $model->course->name; $message['description'] = $model->description; Yii::app()->user->addShareMessage($message); $this->redirect(array('home/index')); } else { $model->faculty_id = null; Yii::app()->user->setNotification('danger', 'Terdapat kesalahan pengisian.'); } } $this->render('upload', array('model' => $model)); }