public function storechapter() { $chapter = Model\Kelas\Chapter::findOrFail(set_value('chapter_id')); $this->form_validation->set_rules('name', 'Name', 'required'); $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); $this->form_validation->set_rules('content', 'Komentar', 'required'); $this->form_validation->set_rules('chapter_id', '', 'required'); if ($this->form_validation->run() == FALSE) { if ($this->request->isXmlHttpRequest()) { $response = new Response(); $response->setContent(json_encode(['status' => 'error', 'data' => $this->form_validation->error_array()])); $response->headers->set('Content-Type', 'application/json'); } else { keepValidationErrors(); redirect('course/showchapter/' . $chapter->course->slug . '/chapter-' . $chapter->order . '#comment-' . $comment->id, 'refresh'); } } else { $data = array('content' => set_value('content'), 'name' => set_value('name'), 'email' => set_value('email'), 'chapter_id' => set_value('chapter_id'), 'parent' => set_value('parent', 0), 'status' => 'draft'); $comment = Model\Kelas\ChapterComment::create($data); if ($user = auth()->check()) { $comment->user()->associate($user->id); if ($user->inRole(['su', 'adm', 'edt', 'ins', 'mdr'])) { $comment->status = 'publish'; } else { $comment->status = 'draft'; } $comment->save(); } if ($this->input->is_ajax_request()) { $response = new Response(); $response->setContent(json_encode(['status' => 'success', 'data' => $comment])); $response->headers->set('Content-Type', 'application/json'); } else { if ($comment->status == 'publish') { set_message_success('Komentar Anda sudah ditampilkan.'); redirect('course/showchapter/' . $chapter->course->slug . '/chapter-' . $chapter->order . '#comment-' . $comment->id, 'refresh'); } else { set_message_success('Komentar Anda akan tampil setelah dimoderasi.'); redirect('course/showchapter/' . $chapter->course->slug . '/chapter-' . $chapter->order . '#form-comment', 'refresh'); } } } }
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'); } }