EVOLUTION-NINJA
Edit File: Add_Chapters.php
<?php namespace App\Controllers; use CodeIgniter\Controller; class Add_Chapters extends Controller { public function __construct() { $this->db = \Config\Database::connect(); } public function get_headers() { $builder = $this->db->table('header'); $query = $builder->get(); return $this->response->setJSON($query->getResult()); } public function get_courses($header_id) { $builder = $this->db->table('header_value'); $builder->where('header_id', $header_id); $query = $builder->get(); return $this->response->setJSON($query->getResult()); } public function get_syllabus($header_id, $course_id) { $builder = $this->db->table('syllabus'); $builder->where('header_id', $header_id); $builder->where('course_id', $course_id); $query = $builder->get(); return $this->response->setJSON($query->getResult()); } public function add_chapter_form() { $header_id = $this->request->getPost('header_id'); $course_id = $this->request->getPost('course_id'); $syllabus_id = $this->request->getPost('syllabus_id'); $chapter_name = $this->request->getPost('chapter_name'); if (is_null($header_id) || is_null($course_id) || is_null($syllabus_id) || is_null($chapter_name)) { return $this->response->setJSON(['result' => 0, 'message' => 'Invalid input data']); } $data = [ 'header_id' => $header_id, 'course_id' => $course_id, 'syllabus_id' => $syllabus_id, 'chapter_name' => $chapter_name, ]; try { $builder = $this->db->table('chapters'); if ($builder->insert($data)) { return $this->response->setJSON(['result' => 1, 'message' => 'Chapter added successfully']); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Failed to add chapter']); } } catch (\Exception $e) { return $this->response->setJSON(['result' => 0, 'message' => $e->getMessage()]); } } public function get_chapters_list() { $draw = intval($this->request->getGet('draw')); $start = intval($this->request->getGet('start')); $length = intval($this->request->getGet('length')); $search = $this->request->getGet('search')['value']; $order = $this->request->getGet('order'); $columns = $this->request->getGet('columns'); $limit = $length; $offset = $start; $builder = $this->db->table('chapters'); $builder->select('chapters.*, header.header_name, header_value.value_name AS course_name, syllabus.syllabus_name'); $builder->join('header', 'header.id = chapters.header_id'); $builder->join('header_value', 'header_value.id = chapters.course_id'); $builder->join('syllabus', 'syllabus.id = chapters.syllabus_id'); if (!empty($search)) { $builder->groupStart(); $builder->like('chapters.chapter_name', $search) ->orLike('header.header_name', $search) ->orLike('header_value.value_name', $search) ->orLike('syllabus.syllabus_name', $search); $builder->groupEnd(); } if (!empty($order)) { foreach ($order as $ord) { $columnIndex = $ord['column']; $columnName = $columns[$columnIndex]['data']; $dir = $ord['dir']; $builder->orderBy($columnName, $dir); } } $totalRecords = $builder->countAllResults(false); $builder->limit($limit, $offset); $query = $builder->get()->getResult(); $response = [ 'draw' => $draw, 'recordsTotal' => $totalRecords, 'recordsFiltered' => $totalRecords, 'data' => $query, ]; return $this->response->setJSON($response); } public function delete_chapter($chapter_id) { try { $builder = $this->db->table('chapters'); $builder->where('id', $chapter_id); if ($builder->delete()) { return $this->response->setJSON(['result' => 1, 'message' => 'Chapter deleted successfully']); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Failed to delete chapter']); } } catch (\Exception $e) { return $this->response->setJSON(['result' => 0, 'message' => $e->getMessage()]); } } public function get_chapter($chapter_id) { $builder = $this->db->table('chapters'); $builder->select('chapters.*, header.header_name, header_value.value_name AS course_name, syllabus.syllabus_name'); $builder->join('header', 'header.id = chapters.header_id'); $builder->join('header_value', 'header_value.id = chapters.course_id'); $builder->join('syllabus', 'syllabus.id = chapters.syllabus_id'); $builder->where('chapters.id', $chapter_id); $query = $builder->get()->getRow(); return $this->response->setJSON(['result' => 1, 'data' => $query]); } public function get_headers1() { $builder = $this->db->table('header'); $query = $builder->get()->getResult(); return $this->response->setJSON(['result' => 1, 'data' => $query]); } public function get_courses1($header_id) { $builder = $this->db->table('header_value'); $builder->where('header_id', $header_id); $query = $builder->get()->getResult(); return $this->response->setJSON(['result' => 1, 'data' => $query]); } public function get_syllabus1($header_id, $course_id) { $builder = $this->db->table('syllabus'); $builder->where('header_id', $header_id); $builder->where('course_id', $course_id); $query = $builder->get()->getResult(); return $this->response->setJSON(['result' => 1, 'data' => $query]); } public function update_chapter() { $chapter_id = $this->request->getPost('chapter_id'); $header_id = $this->request->getPost('header_id'); $course_id = $this->request->getPost('course_id'); $syllabus_id = $this->request->getPost('syllabus_id'); $chapter_name = $this->request->getPost('chapter_name'); $data = [ 'header_id' => $header_id, 'course_id' => $course_id, 'syllabus_id' => $syllabus_id, 'chapter_name' => $chapter_name, ]; $builder = $this->db->table('chapters'); $builder->where('id', $chapter_id); $update = $builder->update($data); if ($update) { return $this->response->setJSON(['result' => 1, 'message' => 'Chapter updated successfully']); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Failed to update chapter']); } } }