EVOLUTION-NINJA
Edit File: add_chapter.php
<?php echo view('includes/home_sidebar');?> <style> #leftDiv { height: 960px !important; } .form-control { appearance: auto; } table thead th { background-color:#00484C; color:#fff; } .btn.submit{ background-color:#0065A3 !important; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <link href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css" rel="stylesheet"> <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script> <main id="rightDiv"> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <section class="fee-structure"> <h3 style="color: rgba(0, 72, 76, 1);">Add Chapter</h3> <button class="btn back-butt" onclick="goBack()"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button> </section> </div> </div> </div> <div class="container mt-3 pb-3"> <form id="addChapterForm"> <div class="row"> <div class="col-sm-3"> <div class="form-group"> <label for="headerSelect">Select Header</label> <select name="header_id" id="headerSelect" class="form-control w-100"></select> </div> </div> <div class="col-sm-3"> <div class="form-group"> <label for="courseSelect">Select Course</label> <select name="course_id" id="courseSelect" class="form-control w-100"></select> </div> </div> <div class="col-sm-3"> <div class="form-group"> <label for="syllabusSelect">Select Syllabus Name</label> <select name="syllabus_id" id="syllabusSelect" class="form-control w-100"></select> </div> </div> <div class="col-sm-3"> <div class="form-group"> <label for="chapterName">Add Chapter Name</label> <input type="text" name="chapter_name" id="chapterName" class="form-control" required/> </div> </div> </div> <div class="row mt-3 mb-3"> <div class="col-sm-9"></div> <div class="col-sm-3"> <button type="submit " class="btn btn-primary mt-4 w-100 submit">Submit</button> </div> </div> </form> </div> </main> <div class="container mt-5"> <div class="row"> <div class="col-sm-12"> <h3>Chapter List</h3> </div> <table id="chapterTable" class="display chapterTable mt-4"> <thead class="table-light"> <tr > <th>ID</th> <th>Header Name</th> <th>Course Name</th> <th>Syllabus Name</th> <th>Chapter Name</th> <th>Actions</th> </tr> </thead> <tbody id="chapterTableBody"> </tbody> </table> <nav aria-label="Page navigation"> <ul class="pagination" id="pagination"> </ul> </nav> </div> </div> </div> <div class="modal fade" id="editChapterModal" tabindex="-1" role="dialog" aria-labelledby="editChapterModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="editChapterModalLabel">Edit Chapter</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <form id="editChapterForm"> <div class="modal-body"> <input type="hidden" name="chapter_id" id="editChapterId"> <div class="form-group"> <label for="editHeaderSelect">Header Name</label> <select class="form-control" id="editHeaderSelect" name="header_id" onchange="updateCourses()"></select> </div> <br> <div class="form-group"> <label for="editCourseSelect">Course Name</label> <select class="form-control" id="editCourseSelect" name="course_id" onchange="updateSyllabi()"></select> </div> <br> <div class="form-group"> <label for="editSyllabusSelect">Syllabus Name</label> <select class="form-control" id="editSyllabusSelect" name="syllabus_id"></select> </div> <br> <div class="form-group"> <label for="editChapterName">Chapter Name</label> <input type="text" class="form-control" id="editChapterName" name="chapter_name"> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-primary submit">Update</button> </div> </form> </div> </div> </div> <script> $(document).ready(function () { $('#chapterTable').DataTable({ "processing": false, "serverSide": true, "ajax": { "url": "<?php echo base_url('add_chapters/get_chapters_list') ?>", "type": "GET" }, "columns": [ { "data": "id" }, { "data": "header_name" }, { "data": "course_name" }, { "data": "syllabus_name" }, { "data": "chapter_name" }, { "data": null, "render": function (data, type, row) { return ` <button class="btn btn-sm btn-primary submit px-3 mr-4" onclick="editChapter(${row.id})">Edit</button> <button class="btn btn-sm btn-danger ml-4" onclick="deleteChapter(${row.id})">Delete</button> `; } } ], "order": [[0, "asc"]], "columnDefs": [ { "orderable": true, "targets": [0, 1, 2, 3, 4] }, { "orderable": false, "targets": [5] } ] }); }); </script> <script> function editChapter(chapterId) { $.ajax({ url: "<?php echo base_url('add_chapters/get_chapter') ?>/" + chapterId, type: "GET", success: function(response) { if (response.result === 1) { let data = response.data; $('#editChapterId').val(data.id); $('#editChapterName').val(data.chapter_name); populateDropdowns(data); $('#editChapterModal').modal('show'); } else { alert(response.message); } }, error: function(xhr, status, error) { console.error('Error fetching chapter data:', error); } }); } function populateDropdowns(data) { $.ajax({ url: "<?php echo base_url('add_chapters/get_headers1') ?>", type: "GET", success: function(response) { if (response.result === 1 && response.data) { let options = ''; response.data.forEach(item => { let selected = item.id === data.header_id ? 'selected' : ''; options += `<option value="${item.id}" ${selected}>${item.header_name}</option>`; }); $('#editHeaderSelect').html(options); } else { console.error('Failed to fetch headers.'); } }, error: function(xhr, status, error) { console.error('Error fetching headers:', error); } }); updateCourses(data.header_id, data.course_id); updateSyllabi(data.header_id, data.course_id, data.syllabus_id); } function updateCourses(selectedHeaderId, selectedCourseId) { let headerId = selectedHeaderId || $('#editHeaderSelect').val(); $.ajax({ url: "<?php echo base_url('add_chapters/get_courses1') ?>/" + headerId, type: "GET", success: function(response) { if (response.result === 1 && response.data) { let options = ''; response.data.forEach(item => { let selected = item.id === selectedCourseId ? 'selected' : ''; options += `<option value="${item.id}" ${selected}>${item.value_name}</option>`; }); $('#editCourseSelect').html(options); if (!selectedCourseId) updateSyllabi(headerId, $('#editCourseSelect').val()); } else { console.error('Failed to fetch courses.'); } }, error: function(xhr, status, error) { console.error('Error fetching courses:', error); } }); } function updateSyllabi(selectedHeaderId, selectedCourseId, selectedSyllabusId) { let headerId = selectedHeaderId || $('#editHeaderSelect').val(); let courseId = selectedCourseId || $('#editCourseSelect').val(); $.ajax({ url: "<?php echo base_url('add_chapters/get_syllabus1') ?>/" + headerId + "/" + courseId, type: "GET", success: function(response) { if (response.result === 1 && response.data) { let options = ''; response.data.forEach(item => { let selected = item.id === selectedSyllabusId ? 'selected' : ''; options += `<option value="${item.id}" ${selected}>${item.syllabus_name}</option>`; }); $('#editSyllabusSelect').html(options); } else { console.error('Failed to fetch syllabi.'); } }, error: function(xhr, status, error) { console.error('Error fetching syllabi:', error); } }); } </script> <script> $(document).ready(function() { $('#editChapterForm').submit(function(event) { event.preventDefault(); let chapterId = $('#editChapterId').val(); let headerId = $('#editHeaderSelect').val(); let courseId = $('#editCourseSelect').val(); let syllabusId = $('#editSyllabusSelect').val(); let chapterName = $('#editChapterName').val(); if (!headerId || !courseId || !syllabusId || !chapterName) { alert('All fields are required.'); return; } $.ajax({ url: "<?php echo base_url('add_chapters/update_chapter') ?>", type: "POST", data: { chapter_id: chapterId, header_id: headerId, course_id: courseId, syllabus_id: syllabusId, chapter_name: chapterName }, success: function(response) { if (response.result === 1) { Swal.fire({ icon: "success", title: response.message, showConfirmButton: false, timer: 2000 }); $('#editChapterModal').modal('hide'); $('#chapterTable').DataTable().ajax.reload(null, false); } else { alert(response.message); } }, error: function(xhr, status, error) { alert('An error occurred: ' + xhr.responseText); } }); }); }); </script> <script> function deleteChapter(chapterId) { Swal.fire({ title: 'Are you sure?', text: "You won't be able to revert this!", icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, delete it!' }).then((result) => { if (result.isConfirmed) { $.ajax({ url: '<?php echo base_url('add_chapters/delete_chapter/') ?>' + chapterId, method: 'POST', dataType: 'json', success: function (response) { if (response.result == 1) { Swal.fire({ icon: "success", title: response.message, showConfirmButton: false, timer: 2000 }); $('#chapterTable').DataTable().ajax.reload(null, false); } else { Swal.fire({ icon: "error", title: "Failed to delete chapter", showConfirmButton: false, timer: 2000 }); } }, error: function () { console.log('Error deleting chapter.'); } }); } }); } </script> <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.10.8/dist/sweetalert2.all.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11.10.8/dist/sweetalert2.min.css" rel="stylesheet"> <script> const headerSelect = $('#headerSelect'); const courseSelect = $('#courseSelect'); const syllabusSelect = $('#syllabusSelect'); function fetchCoursesAndSyllabus(headerId) { $.ajax({ url: '<?= base_url('add_chapters/get_courses/') ?>' + headerId, method: 'GET', dataType: 'json', success: function (data) { courseSelect.empty().append('<option value="">Select</option>'); $.each(data, function (index, item) { courseSelect.append('<option value="' + item.id + '">' + item.value_name + '</option>'); }); }, error: function () { console.log('Error fetching courses.'); } }); } function fetchSyllabus(headerId, courseId) { $.ajax({ url: '<?= base_url('add_chapters/get_syllabus/') ?>' + headerId + '/' + courseId, method: 'GET', dataType: 'json', success: function (data) { syllabusSelect.empty().append('<option value="">Select</option>'); $.each(data, function (index, item) { syllabusSelect.append('<option value="' + item.id + '">' + item.syllabus_name + '</option>'); }); }, error: function () { console.log('Error fetching syllabus.'); } }); } headerSelect.change(function () { const selectedHeaderId = $(this).val(); fetchCoursesAndSyllabus(selectedHeaderId); }); courseSelect.change(function () { const selectedHeaderId = headerSelect.val(); const selectedCourseId = $(this).val(); fetchSyllabus(selectedHeaderId, selectedCourseId); }); $.ajax({ url: '<?= base_url('add_chapters/get_headers') ?>', method: 'GET', dataType: 'json', success: function (data) { headerSelect.empty().append('<option value="">Select</option>'); $.each(data, function (index, item) { headerSelect.append('<option value="' + item.id + '">' + item.header_name + '</option>'); }); }, error: function () { console.log('Error fetching headers.'); } }); $('#addChapterForm').submit(function(event) { event.preventDefault(); const formData = new FormData(this); $.ajax({ url: 'add_chapters/add_chapter_form', type: 'POST', data: formData, processData: false, contentType: false, success: function(response) { if (response.result == 1) { Swal.fire({ icon: "success", title: response.message, showConfirmButton: false, timer: 2000 }); $('#addChapterForm')[0].reset(); $('#chapterTable').DataTable().ajax.reload(null, false); } }, error: function(jqXHR, textStatus, errorThrown) { console.log('Error details:', jqXHR, textStatus, errorThrown); alert('An error occurred: ' + textStatus + ' ' + errorThrown); } }); }); </script> <script> window.onload = function () { const leftDiv = document.getElementById('leftDiv'); const rightDiv = document.getElementById('rightDiv'); const rightDivHeight = rightDiv.offsetHeight; leftDiv.style.height = rightDivHeight + 'px'; }; </script> <script> function goBack() { window.location.href = 'dashboard'; } </script> </script> <?php echo view('includes/footer'); ?>