<?php
include_once '../session/admin-session.php';
include_once '../classes/class_admin.php';
include_once '../classes/validation.php';
include_once '../classes/common.php';
include_once 'read-configration.php';
ob_start();

$ObjDB = new class_db();
$log = new common();
$objadm = new class_admin();
$val = new validation();

$sub_list = $working_curriculum;

$i = 1;
$sub_drop = array();
$edit_flag = 2;

$sub_code = '';
$sub_type = '';
$sub_show = '';
$type_show = '';
$pre_req = '';
$objective = '';
$topics = '';
$eval = '';
$reading = '';
$when_next = '';

if (!empty($_REQUEST['unique_code'])) {
    $edit_flag = 1;
    $unique_code = $val->ValidateString($_REQUEST['unique_code']);
}

if (!empty($_REQUEST['edit_sub_code']) && !empty($_REQUEST['edit_sub_list'])) {
    $edit_flag = 1;
    $sub_code = $val->ValidateString($_REQUEST['edit_sub_code']);
    $sub_list = $val->ValidateString($_REQUEST['edit_sub_list']);
}

if (!empty($unique_code) || (!empty($sub_code) && !empty($sub_list))) {
    if (!empty($sub_code) && !empty($sub_list)) {
       $qlist = "select a.*, b.sub_title from isr_course_content_tbl a, isr_curriculum_tbl b where a.sub_list = '$sub_list' and a.sub_code = '$sub_code' and a.sub_code = b.sub_code and a.sub_list = b.sub_list order by a.sub_code limit 1";
    } else {
        $qlist = "select a.*, b.sub_title from isr_course_content_tbl a, isr_curriculum_tbl b where a.unique_code = '$unique_code' and a.sub_code = b.sub_code and a.sub_list = b.sub_list order by a.sub_code limit 1";
    }

    $ObjDB->sproc_name = $qlist;
    $res_qlist = $ObjDB->SelectQuery();
    $found = mysqli_num_rows($res_qlist);

    if ($found > 0) {
        $sub_row = mysqli_fetch_array($res_qlist);
      
       $sub_code = $sub_row['sub_code'];
        $sub_title = $sub_row['sub_title'];
        $sub_type = $sub_row['sub_type'];
        $sub_show_edit = $sub_code . ' - ' . $sub_title;
        $type_show = $sub_type;
        $prereq = $sub_row['pre_req'];
        $goals = $sub_row['objective'];
        $topics = $sub_row['topics'];
        $eval = $sub_row['eval'];
        $reading = $sub_row['reading'];
        $when_next = $sub_row['when_next'];
    }
}

$query = "select sub_code from isr_course_content_tbl where sub_list = '$sub_list'";
$ObjDB->sproc_name = $query;
$ResLogin = $ObjDB->SelectQuery();

while ($row = mysqli_fetch_array($ResLogin)) {
    $sub_drop[$i] = $row['sub_code'];
    $i++;
}

$sub_drop_down = "('x', ";
for ($k = 1; $k < $i; $k++) {
    $sub_drop_down = $sub_drop_down . "'" . $sub_drop[$k] . "', ";
}
$sub_drop_down = substr($sub_drop_down, 0, -2) . ")";

$qsub = "select a.sub_code, b.sub_title from isr_sub_available_tbl a, isr_curriculum_tbl b where a.sub_list = '$sub_list' and a.sub_code = b.sub_code and a.sub_list = b.sub_list "
        . "and a.sub_code not in $sub_drop_down order by a.sub_code";
$ObjDB->sproc_name = $qsub;
$qsub_query = $ObjDB->SelectQuery();
$view_val = "<option value=''>Choose Subject</option>";
while ($sub_row = mysqli_fetch_array($qsub_query)) {
    $sub_code = $sub_row['sub_code'];
    $sub_title = $sub_row['sub_title'];
    $sub_show = $sub_code . ' - ' . $sub_title;
    $view_val .= '<OPTION VALUE=' . $sub_code . '>' . $sub_show . "</option>";
}


$qsub_type = $objadm->MasterCourseType();
$view_val_type = "<option value=''>Choose Type</option>";
while ($sub_row_type = mysqli_fetch_array($qsub_type)) {
    $type_code = $sub_row_type['type_code'];
    $type_name = $sub_row_type['type_name'];
    $type_show = $type_code . ' - ' . $type_name;

    $selected_type = "";
    if ($type_code == $sub_type) {
        $selected_type = "SELECTED";
    }

    $view_val_type .= "<OPTION VALUE='$type_code'" . $selected_type . ">" . $type_show . "</option>";
}



$db_semester_arr = array();

if ($_POST['submit_name']) {
    $sub_code = $val->ValidateString($_REQUEST['sub_code']);
    $sub_type = $val->ValidateString($_REQUEST['sub_type']);
    $prereq = $val->ValidateString($_REQUEST['prereq']);
    $goals = $val->ValidateString($_REQUEST['goals']);
    $topics = $val->ValidateString($_REQUEST['topics']);
    $eval = $val->ValidateString($_REQUEST['eval']);
    $reading = $val->ValidateString($_REQUEST['reading']);

    $sub_list = $val->ValidateString($_REQUEST['sub_list']);

    $when_next = $val->ValidateString($_REQUEST['when_next']);

    $objadm->sub_code = $sub_code;
    $objadm->sub_type = $sub_type;
    $objadm->prereq = $prereq;
    $objadm->goals = $goals;
    $objadm->topics = $topics;
    $objadm->eval = $eval;
    $objadm->reading = $reading;
    $objadm->when_next = $when_next;
    $objadm->admin_roll = $_SESSION['user_sesid'];

    $objadm->sub_list = $sub_list;

    $res_msg = $objadm->AddUpdateCourseContent();

    if ($res_msg == "SUCCESS") {
        $success_msg = '<div class="text-success">Detail is saved  Sucessfully!</div>';
        header("Location: course-content-list.php");
    } else {
        $success_msg = '<div class="text-danger">' . $res_msg . "</div>";
    }
}
?>
<div class="container-fluid">


    <div class="row">
        <div class="col-xl-12 col-xxl-12 col-sm-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title">To be filled by Course Coordinators</h5>
                    <p style="color: #c53639;"> Copy-paste from other apps lead to text formatting errors.<br>
                        If required, copy-paste ONLY from .txt type files.</p>
                </div>
                <div class="card-body">
                    <div class="col-xl-1 col-xxl-1 col-sm-1"></div>
                    <div class="col-xl-12 col-xxl-12 col-sm-12">
                        <?php echo $success_msg; ?><br/>
                        <form action="" method="POST">

                            <input type="hidden" name="sub_list" value="<?php echo $working_curriculum; ?>"/>
                            <div class="table-responsive">
                                <table class="table table-bordered align-middle" style="width: 900px;
                                       margin-left: 228px;">
                                    <tbody>
                                        <tr>
                                            <td><label for="sub_code" class="form-label">Select Course</label></td>
                                            <td>
                                                <select id="sub_code" class="form-select" required="" name="sub_code">
                                                    <?php if ($edit_flag == 2) { ?>                                                   
                                                        <?php echo $view_val; ?>
                                                    <?php } else {
                                                        ?>
                                                        <option value="<?php echo $sub_code; ?>"><?php echo $sub_show_edit; ?></option>                                     
                                                    <?php } ?>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label for="sub_type" class="form-label">Nature of Course</label></td>
                                            <td>
                                                <select id="sub_type" name="sub_type" class="form-select" required="">
                                                    <?php echo $view_val_type; ?>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label for="prereq" class="form-label">Pre-requisites, if any</label></td>
                                            <td><input type="text" name="prereq" id="prereq" class="form-control" required="" value="<?php echo $prereq; ?>"/></td>
                                        </tr>
                                        <tr>
                                            <td><label for="goals" class="form-label">Objectives</label></td>
                                            <td>
                                                <textarea id="goals" name="goals" class="form-control" rows="3" required=""><?php echo $goals; ?></textarea>
                                                <small class="text-muted">Relevance, goals, type of students for whom useful, outcome, etc.</small>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label for="topics" class="form-label">Course content</label></td>
                                            <td>
                                                <textarea id="topics" name="topics" class="form-control" rows="3" required=""><?php echo $topics; ?></textarea>
                                                <small class="text-muted">Details of topics with number of lectures for each</small>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label for="eval" class="form-label">Evaluation / Assessment</label></td>
                                            <td>
                                                <textarea id="eval" name="eval" class="form-control" rows="3" required=""><?php echo $eval; ?></textarea>
                                                <small class="text-muted">End Sem %; Mid Sem %; Others</small>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label for="reading" class="form-label">Suggested readings</label></td>
                                            <td>
                                                <textarea id="reading" name="reading" class="form-control" rows="3"><?php echo $reading; ?></textarea>
                                                <small class="text-muted">With full list of authors, publisher, year, edition etc.</small>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label for="when_next" class="form-label">When the course will be offered next</label></td>
                                            <td><input type="text" id="when_next" name="when_next" class="form-control" value="<?php echo $when_next; ?>"></td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" class="text-center">
                                                <input type="submit" name="submit_name" value="SUBMIT" class="btn btn-secondary" style="width: 11%;
                                                       float: left;
                                                       background: #a9181b;
                                                       border: #a9181b;"/>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </form>
                    </div>

                </div>

            </div>

        </div>
    </div>
</div>
<?php
$pagemaincontent = ob_get_contents();
ob_clean();
$PAGENAME = 'course-content-add';
$HTMLTITLE = '';
$HTMLDESCRIPTION = '';
$HTMLKEYWORDS = '';
include("admin-master.php");
?>