public function editChapter($id) { if (!$this->input->post()) { $this->template->build('course/chapter'); } else { // 1. Prepare from input $input = collect($this->input->post('course')); $course = Model\Kelas\Course::withDrafts()->findOrFail($id); $this->repository->set($course); // 2. Remove data $remove = collect($this->input->post('remove')); // 2.a Remove Chapter foreach ($remove->get('chapters', []) as $chapter_id) { $this->repository->deleteChapter($chapter_id); } // 2.b Remove Quiz foreach ($remove->get('chapter_quiz', []) as $question) { list($chapter_id, $question_id) = explode('/', $question); $this->repository->deleteChapterQuiz($question_id); } // 2.b Remove Attachment foreach ($remove->get('chapter_attachment', []) as $attachment) { list($chapter_id, $attachment_id) = explode('/', $attachment); $this->repository->deleteChapterAttachment($attachment_id); } // 4. Chapters $chapters = collect($input->get('chapters')); $chapters->each(function ($chapter) use($course) { $chapter = collect($chapter); if ($chapter->get('id')) { $modelChapter = Model\Kelas\Chapter::findOrFail($chapter->get('id')); } else { $modelChapter = new Model\Kelas\Chapter(); $modelChapter->course()->associate($course); } $modelChapter->name = $chapter->get('name'); $modelChapter->content = $chapter->get('content'); $modelChapter->order = $chapter->get('order'); $modelChapter->save(); // 5. Quiz Chapter $quiz = collect($chapter->get('quiz')); if ($quiz->get('id')) { $modelQuiz = Model\Kelas\Quiz::findOrFail($quiz->get('id')); } else { $modelQuiz = new Model\Kelas\Quiz(); $modelQuiz->chapter()->associate($modelChapter); } $modelQuiz->name = $quiz->get('name'); $modelQuiz->time = $quiz->get('time'); $modelQuiz->save(); $questions = collect($quiz->get('questions')); $questions->each(function ($question) use($modelQuiz, $modelChapter, $course) { $question = collect($question); if ($question->get('id')) { $modelQuestion = Model\Kelas\QuizQuestion::findOrFail($question->get('id')); } else { $modelQuestion = new Model\Kelas\QuizQuestion(); $modelQuestion->quiz()->associate($modelQuiz); } $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->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); } $modelChapter->attachments->each(function ($attachment) use($attachments) { $attachments->each(function ($input) use($attachment) { $input = collect($input); if (!$input->get('id') == $attachment->id) { // } }); }); $attachments->each(function ($attachment) use($modelChapter, $course) { $attachment = collect($attachment); if ($attachment->get('id')) { // Already } else { $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); } }); }); redirect('dashboard/course/edit/' . $course->id . '/chapter', 'refresh'); } }
public function download($filename) { $this->load->helper('download'); $attachment = Model\Kelas\Attachment::findByHashids($filename); $content = file_get_contents($attachment->filepath); force_download($attachment->filename, $content); }