Пример #1
0
 public function submit()
 {
     // 1. Prepare from input
     $input = collect($this->input->post('course'));
     // 2. Create Course
     $course = new Model\Kelas\Course();
     $course->name = $input->get('name');
     $course->description = $input->get('description', '', FALSE);
     $course->days = $input->get('days');
     $course->status = 'draft';
     $course->category()->associate($input->get('category_id'));
     $course->instructor()->associate(sentinel()->getUser());
     $course->save();
     // 3. Create Folder Content and Upload Images
     $kelas_content = PATH_KELASONLINE_CONTENT . '/' . $course->id;
     if (!is_dir($kelas_content)) {
         mkdir($kelas_content, 0775);
     }
     $imageManager = new ImageManager();
     // 3.a. Featured image
     $featured = $this->input->post('featured[src]');
     if (!empty($featured)) {
         $image = $imageManager->make($featured);
         if ($image->mime == 'image/jpeg') {
             $extension = '.jpg';
         } elseif ($image->mime == 'image/png') {
             $extension = '.png';
         } elseif ($image->mime == 'image/gif') {
             $extension = '.gif';
         } else {
             $extension = '';
         }
         $prefix = 'featured_';
         $filename = $prefix . $course->id . $extension;
         $kelas_content = PATH_KELASONLINE_CONTENT . '/' . $course->id;
         $image->save($kelas_content . '/' . $filename);
         $course->featured = $filename;
         $course->save();
     }
     // 3.b. Thumbnail image
     $thumbnail = $this->input->post('thumbnail[src]');
     if (!empty($thumbnail)) {
         $image = $imageManager->make($thumbnail);
         if ($image->mime == 'image/jpeg') {
             $extension = '.jpg';
         } elseif ($image->mime == 'image/png') {
             $extension = '.png';
         } elseif ($image->mime == 'image/gif') {
             $extension = '.gif';
         } else {
             $extension = '';
         }
         $prefix = 'thumbnail_';
         $filename = $prefix . $course->id . $extension;
         $image->save($kelas_content . '/' . $filename);
         $course->thumbnail = $filename;
         $course->save();
     }
     // 4. Chapters
     $chapters = collect($input->get('chapters'));
     $chapters->each(function ($chapter) use($course) {
         $chapter = collect($chapter);
         $modelChapter = new Model\Kelas\Chapter();
         $modelChapter->name = $chapter->get('name');
         $modelChapter->content = $chapter->get('content');
         $modelChapter->order = $chapter->get('order');
         $modelChapter->course()->associate($course);
         $modelChapter->save();
         // 5. Quiz Chapter
         $quiz = collect($chapter->get('quiz'));
         $modelQuiz = new Model\Kelas\Quiz();
         $modelQuiz->name = $quiz->get('name');
         $modelQuiz->time = $quiz->get('time');
         $modelQuiz->chapter()->associate($modelChapter);
         $modelQuiz->save();
         $questions = collect($quiz->get('questions'));
         $questions->each(function ($question) use($modelQuiz, $modelChapter, $course) {
             $question = collect($question);
             $modelQuestion = new Model\Kelas\QuizQuestion();
             $modelQuestion->question = $question->get('question');
             $modelQuestion->option_a = $question->get('option_a');
             $modelQuestion->option_b = $question->get('option_b');
             $modelQuestion->option_c = $question->get('option_c');
             $modelQuestion->option_d = $question->get('option_d');
             $modelQuestion->correct = $question->get('correct');
             $modelQuestion->quiz()->associate($modelQuiz);
             $modelQuestion->save();
         });
         // 6. Attachment
         $attachments = collect($chapter->get('attachments'));
         $attachment_path = PATH_KELASONLINE_CONTENT . '/' . $course->id . '/chapter_' . $modelChapter->id;
         if (!is_dir($attachment_path)) {
             mkdir($attachment_path, 0775);
         }
         $attachments->each(function ($attachment) use($modelChapter, $course) {
             $attachment = collect($attachment);
             $modelAttachment = new Model\Kelas\Attachment();
             $modelAttachment->filename = $attachment->get('filename');
             $modelAttachment->filetype = $attachment->get('filetype');
             $modelAttachment->filesize = $attachment->get('filesize');
             $modelAttachment->chapter()->associate($modelChapter);
             $modelAttachment->save();
             // 6.a. Uploading attachment
             $file_encoded = $attachment->get('dataurl');
             $file_encoded = explode(',', $file_encoded);
             $file_base64 = $file_encoded[1];
             $fp = fopen($modelAttachment->filepath, 'w+');
             fwrite($fp, base64_decode($file_base64));
             fclose($fp);
         });
     });
     // 7. Exam
     $exam = collect($input->get('exam'));
     $modelExam = new Model\Kelas\Exam();
     $modelExam->name = $exam->get('name');
     $modelExam->time = $exam->get('time');
     $modelExam->course()->associate($course);
     $modelExam->save();
     $questions = collect($exam->get('questions'));
     $questions->each(function ($question) use($modelExam, $course) {
         $question = collect($question);
         $modelQuestion = new Model\Kelas\ExamQuestion();
         $modelQuestion->question = $question->get('question');
         $modelQuestion->option_a = $question->get('option_a');
         $modelQuestion->option_b = $question->get('option_b');
         $modelQuestion->option_c = $question->get('option_c');
         $modelQuestion->option_d = $question->get('option_d');
         $modelQuestion->correct = $question->get('correct');
         $modelQuestion->exam()->associate($modelExam);
         $modelQuestion->save();
     });
     set_message_success('Kelas Anda berhasil ditambahkan. Namun kelas Anda masih berada pada tahap moderator.');
     $redirect = 'dashboard/course/edit/' . $course->id;
     if ($this->input->is_ajax_request()) {
         $this->output->set_content_type('application/json')->set_output(json_encode(['redirect' => site_url($redirect)]));
     } else {
         redirect($redirect);
     }
 }