konfigurasi dasar ci : link
struktur pembagian view : link
Flowchart
Database
pakar_answer
question_id int 9 not null key auto increment answer int 2 not null user_id int 9 not null kategori_id int 2 not null
pakar_kategori
id_kategori int 5 key auto increment nama_kategori varchar 20
pakar_question
id_question int 9 not null key auto increment kategori_id int 11 pertanyaan varchar 255 is_img int 1 not null image varchar 255
pakar_result
id_result int 9 not null key auto increment user_id int 9 not null question_id int 9 not null kategori_id int 9 not null sts int 1 not null dt_create date
pakar_user
id_user int 9 not null key auto increment username varchar 50 password varchar 50
Controller
PHP < ? php
defined ( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );
class Question extends CI_Controller {
function __construct () {
parent :: __construct ();
$this -> load -> model ( 'M_Question' );
}
[lanjutan controller]
} ? >
[Lanjutan controller] lanjutan code controller
login and register section
function regis (){
$this -> load -> view ( 'v_regis.php' );
}
function do_regis (){
$data = array (
'username' => $this -> input -> post ( 'username' ),
'password' => md5 ( $this -> input -> post ( 'pass' )),
);
$this -> db -> insert ( 'pakar_user' , $data );
if ( $this -> db -> affected_rows () > 0 ){
$this -> session -> set_flashdata ( 'sukses' , ' berhasil' );
redirect ( base_url () . 'Question/login' );
} else {
$this -> session -> set_flashdata ( 'error' , ' gagal' );
redirect ( base_url () . '/Question/regis' );
}
}
function cek_user (){
$user = $this -> input -> post ( 'username' );
$cek = $this -> db -> query ( " SELECT * from pakar_user WHERE username = ' $user ' limit 1 " );
if ( $cek -> num_rows () > 0 ){
$return = array (
'status' => false ,
'message' => '' ,
);
} else {
$return = array (
'status' => true ,
'message' => '' ,
);
}
echo json_encode ( $return );
}
function login (){
$this -> load -> view ( 'v_login.php' );
}
function do_login (){
// Melakukan validasi input username dan password
$this -> form_validation -> set_rules ( 'username' , 'username' , 'required|trim|xss_clean' );
$this -> form_validation -> set_rules ( 'password' , 'password' , 'required|trim|xss_clean' );
if ( $this -> form_validation -> run () == FALSE ) {
$this -> load -> view ( 'v_login' );
} else {
$username = $this -> input -> post ( 'username' );
$password = $this -> input -> post ( 'password' );
// Memberi nama variabel baru untuk input username dan password
$user = $username ;
$pass = md5 ( $password );
// Melakukan cek ke database, apakah username dan password yang diinputkan cocok atau tidak
$cek = $this -> M_Question -> cek ( $user , $pass );
// Jika username dan password yang diinputkan cocok
if ( $cek -> num_rows () > 0 ) {
//Buat session username, dan level untuk nantinya ditampilkan
foreach ( $cek -> result () as $qad ) {
$sess_data [ 'username' ] = $qad -> username ;
$sess_data [ 'id_user' ] = $qad -> id_user ;
$this -> session -> set_userdata ( $sess_data );
}
$id = $this -> session -> userdata ( "quiz_kategori" );
redirect ( "Question/masuk/ $id " );
} else {
$this -> session -> set_flashdata ( 'result_login' , '<br>Username atau Password yang anda masukkan salah.' );
redirect ( base_url ( 'Question/masuk' ));
}
}
}
function kategori (){
$this -> load -> view ( 'v_kategori.php' );
}
function masuk (){
$id = $this -> uri -> segment ( '3' );
$sess_data [ 'quiz_kategori' ] = " $id " ;
$this -> session -> set_userdata ( $sess_data );
$user = $this -> session -> userdata ( "id_user" );
// jika belum ada username
if ( $user == '' ){
$this -> load -> view ( 'v_login.php' );
} else {
$cek = $this -> db -> query ( " SELECT * from pakar_result where kategori_id = $id and user_id = $user " );
if ( $cek -> num_rows () > 0 ){
//second time
$sts = $cek -> row_array ()[ 'sts' ];
//cek sts quiz finish or not
if ( $sts == 1 ){
//if finish -> out jika telah selesai munculkan hasilnya
if ( $this -> db -> affected_rows () > 0 ){
$this -> session -> set_flashdata ( 'error' , '<br>Quiz telah selesai anda kerjakan !' );
}
redirect ( base_url () . "Question/result" , 'refresh' );
} else {
$id_result = $this -> db -> query ( " SELECT id_result from pakar_result where kategori_id = $id and user_id = $user " )-> row_array ();
$id_result1 = $id_result [ 'id_result' ];
$data = array (
'id_result' => $cek -> row_array ()[ 'id_result' ],
'data_view' => $this -> M_Question -> data_quiz ( $user , $id )-> result (),
);
$this -> template -> load ( 'templates/template_1' , 'v_question' , $data );
}
} else {
// jika
$data1 = array (
'user_id' => $user ,
'kategori_id' => $id ,
);
$this -> db -> insert ( "pakar_result" , $data1 );
$id_result1 = $this -> db -> insert_id ();
$data = array (
'id_result' => $id_result1 ,
'data_view' => $this -> M_Question -> data_quiz ( $user , $id )-> result (),
);
$this -> template -> load ( 'templates/template_1' , 'v_question' , $data );
}
}
}
function upd_question (){
sleep ( 2 );
$id_usr = $this -> session -> userdata ( 'id_user' );
$id = $this -> input -> post ( 'id' );
$opt = $this -> input -> post ( 'opt' );
$jns = $this -> input -> post ( 'jns' );
$cek = $this -> db -> query ( " SELECT * FROM pakar_answer where user_id = $id_usr and question_id = $id and kategori_id = $jns " );
$data = array (
'question_id' => $id ,
'kategori_id' => $jns ,
'user_id' => $id_usr ,
'answer' => $opt ,
);
if ( $cek -> num_rows () > 0 ){
$this -> db -> where ( 'user_id' , $id_usr );
$this -> db -> where ( 'kategori_id' , $jns );
$this -> db -> where ( 'question_id' , $id );
$this -> db -> update ( 'pakar_answer' , $data );
} else {
$this -> db -> insert ( 'pakar_answer' , $data );
}
if ( $this -> db -> affected_rows () > 0 ){
$return = array (
'status' => true ,
'message' => 'Update Berhasil' ,
);
} else {
$return = array (
'status' => false ,
'message' => 'Update gagal' ,
);
}
echo json_encode ( $return );
}
function submit (){
$id = $this -> input -> post ( "id_result" );
$data = array (
'sts' => 1 ,
);
$this -> db -> where ( 'id_result' , $id );
$this -> db -> update ( 'pakar_result' , $data );
redirect ( base_url () . "Question/result" , 'refresh' );
}
function result (){
$id = $this -> session -> userdata ( "quiz_kategori" );
$userID = $this -> session -> userdata ( "id_user" );
$data [ 'result' ] = $this -> M_Question -> result ( $id , $userID )-> row_array ()[ 'result' ];
$this -> template -> load ( 'templates/template_1' , 'v_result' , $data );
}
function resetResult (){
$id_usr = $this -> session -> userdata ( 'id_user' );
$jns = $this -> session -> userdata ( 'quiz_kategori' );
$this -> db -> where ( 'user_id' , $id_usr );
$this -> db -> where ( 'kategori_id' , $jns );
$this -> db -> delete ( 'pakar_result' );
if ( $this -> db -> affected_rows () > 0 ){
$this -> db -> where ( 'user_id' , $id_usr );
$this -> db -> where ( 'kategori_id' , $jns );
$this -> db -> delete ( 'pakar_answer' );
redirect ( base_url () . "Question/Kategori" , 'refresh' );
}
}
Model
< ? php
if ( !defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );
class M_Question extends CI_Model {
... .
}
? >
function cek ( $username , $password ) {
$this -> db -> where ( "username" , $username );
$this -> db -> where ( "password" , $password );
return $this -> db -> get ( "pakar_user" );
}
function data_quiz ( $userID , $kategori ){
return $this -> db -> query (
" SELECT * from
( SELECT * FROM pakar_question where kategori_id = $kategori ) a
LEFT JOIN ( select question_id,answer from pakar_answer where kategori_id = $kategori and user_id = $userID ) b on question_id = id_question
ORDER BY rand ()"
);
}
function result ( $id , $userID ){
if ( $id == 5 ){
return $this -> db -> query (
" SELECT
( case
when tot_ya >= 9 then 'perkembangan anak sesuai dengan tahap perkembangannya'
when tot_ya >= 7 then 'perkembangan anak meragukan'
when tot_ya <= 6 then 'kurang, kemungkinan ada penyimpangan '
else ''
end ) result
from (
SELECT sum ( case when answer = 1 then 1 else 0 end ) tot_ya from pakar_answer where kategori_id = $id and user_id = $userID
) a"
);
} else if ( $id == 2 ){
return $this -> db -> query (
" SELECT
( case
when tot_ya >= 9 then 'perkembangan anak sesuai dengan tahap perkembangannya'
when tot_ya >= 7 then 'perkembangan anak meragukan'
when tot_ya <= 6 then 'kurang, kemungkinan ada penyimpangan '
else ''
end ) result
from (
SELECT sum ( case when answer = 1 then 1 else 0 end ) tot_ya from pakar_answer where kategori_id = $id and user_id = $userID
) a"
);
} else {
}
}
View
view kategori
<! DOCTYPE html >
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< meta http-equiv = "X-UA-Compatible" content = "ie=edge" >
< title >Document</ title >
</ head >
< body >
< a href = "http://localhost/sistem_pakar/Question/masuk/1" >Kategori 1</ a >< br >
< a href = "http://localhost/sistem_pakar/Question/masuk/2" >Kategori 2</ a >< br >
< a href = "http://localhost/sistem_pakar/Question/masuk/3" >Kategori 3</ a >< br >
< a href = "http://localhost/sistem_pakar/Question/masuk/4" >Kategori 4</ a >< br >
< a href = "http://localhost/sistem_pakar/Question/masuk/5" >Kategori 5</ a >
</ body >
</ html >
view login
< style >
#judul { font-family: Arial Black ; color: black ;}
</ style >
< ?php $this->load->view('templates/includes/header'); ?>
< body >
< div class = "account-pages" ></ div >
< div class = "clearfix" ></ div >
< div class = "wrapper-page" >
< div class = " card-box" >
< div class = "panel-heading text-center" >
< p class = "text-center" >Silahkan Masukan username dan password</ p >
</ div >
< div class = "panel-body" >
< form class = "form-horizontal m-t-5" action = " < ?php echo base_url('Question/do_login'); ?>" method = "post" >
< div class = "row" >
<!-- Validasi error -->
[code validasi]
<!-- Validasi error end -->
</ div >
< div class = "form-group" >
< div class = "col-xs-12 field" >
< input class = "form-control" type = "text" required = "" placeholder = "Username" name = "username" >
</ div >
</ div >
< div class = "form-group" >
< div class = "col-xs-12 field" >
< input class = "form-control" type = "password" required = "" placeholder = "Password" name = "password" >
</ div >
</ div >
< div class = "form-group text-center m-t-20" >
< div class = "col-xs-12" >
< button class = "btn btn-success btn-block text-uppercase waves-effect waves-light" type = "submit" >Log In</ button >
</ div >
</ div >
</ form >
< div class = "row" >
< div class = "col-sm-12 text-center" >
< p >Belum punya akun, < a href = " < ?php echo base_url()?>/Question/regis" class = "text-primary m-l-5" >< b >Daftar</ b ></ a ></ p >
</ div >
</ div >
</ div >
</ div >
</ div >
</ div >
</ div >
<!-- footer-->
< ?php $this->load->view('templates/includes/footer'); ?>
<!-- script -->
< ?php $this->load->view('templates/includes/script'); ?>
<!-- end script -->
</ body >
</ html >
[code validasi] lanjutan code view login
< ? php
// Validasi error, jika username atau password tidak cocok
if ( validation_errors () || $this -> session -> flashdata ( 'result_login' )) {
? >
< div class = "alert alert-danger animated fadeInDown" role = "alert" >
< button type = "button" class = "close" data - dismiss = "alert" > × </ button >
< strong > Peringatan !</ strong >
< ? php
// Menampilkan error
echo validation_errors ();
// Session data users
echo $this -> session -> flashdata ( 'result_login' ); ? >
</ div >
< ? php } ? >
< ? php
// Validasi error, jika username atau password tidak cocok
if ( $this -> session -> flashdata ( 'sukses' )) {
? >
< div class = "alert alert-success animated fadeInDown" role = "alert" >
< button type = "button" class = "close" data - dismiss = "alert" > × </ button >
< strong > Sukses </ strong >
< ? php
// Menampilkan error
echo validation_errors ();
// Session data users
echo $this -> session -> flashdata ( 'sukses' ); ? >
</ div >
< ? php } ? >
view question
< style >
.timer {
display: -webkit-inline-box;
}
</ style >
<!-- content main-->
< div class = "row" >
< form role = "form" id = 'form' data-parsley-validate novalidate action = " < ?php echo base_url()." Question /submit/"; ? >" method="post" enctype="multipart/form-data">
< div class = "col-sm-8" >
< ?php $no=1; foreach($data_view as $data){ ?>
< div class = "card-box " >
< h4 class = "text-dark " ></ h4 >
< h4 class = "m-t-0 " style = "line-height: 150%;" >< b > < ?php echo "$data->pertanyaan"; ?> </ b ></ h4 >
< div class = "row" >
< div class = "col-sm-6" >
< div class = "radio radio-custom" >
< input class = "rad_btn" type = "radio" name = "radio_ < ?php echo $no; ?>" id = "radio1_ < ?php echo $no; ?>" <?php echo ($data- >answer==1) ? "checked":""; ?> value="1" data-id=" < ?php echo $data->id_question; ?>" data-jns=" < ?php echo $data->kategori_id; ?>">
< label for = "radio1_ < ?php echo $no; ?>" >
Ya
</ label >
</ div >
< div class = "radio radio-custom" >
< input class = "rad_btn" type = "radio" name = "radio_ < ?php echo $no; ?>" id = "radio2_ < ?php echo $no; ?>" <?php echo ($data- >answer==2) ? "checked":""; ?> required="" value="2" data-id=" < ?php echo $data->id_question; ?>" data-jns=" < ?php echo $data->kategori_id; ?>">
< label for = "radio2_ < ?php echo $no; ?>" >
Tidak
</ label >
</ div >
</ div >
</ div >
</ div >
< ?php $no++; } ?>
< div class = "card-box" >
< a href = " < ?php echo base_url().'Question/kategori/'?>" class = "btn btn-primary" >Back</ a >
< input type = "hidden" name = 'id_result' id = "id_result" value = " < ?php echo $id_result; ?>" >
< input type = "submit" class = "btn btn-success" value = "Selesai" >
</ div >
</ form >
</ div >
</ div >
<!-- end content main-->
[java script question]
[java script view question] lanjutan code view question
<!-- script -->
< ?php $this->load->view('templates/includes/script'); ?>
<!-- end script -->
<!-- Parsly js untuk validasi form -->
<!-- https://www.jqueryscript.net/time-clock/countdown-timer-controls.html -->
< script type = "text/javascript" src = "<?php echo base_url() ?>templates/assets/plugins/parsleyjs/parsley.min.js" ></ script >
< script type = "text/javascript" >
$ ( document ). ready ( function () {
$ ( 'form' ). parsley ();
});
$ ( ".radio" ). on ( "click" , ".rad_btn" , function (){
var jns = $ ( this ). data ( 'jns' );
var id = $ ( this ). data ( 'id' );
var opt = $ ( this ). val ();
$ ( '+ label' , this ). after ( "<i class='loading fa-ul fa fa-spinner fa-spin'></i>" );
update_ck ( id , opt , jns );
});
function update_ck ( id , opt , jns ){
$ . ajax ({
url : "<?php echo base_url();?>Question/upd_question" ,
method : "POST" ,
dataType : "JSON" , //ingat pakai ini klw tidak tidak terbaca hasil ajaxnya
data :{ id : id , opt : opt , jns : jns },
success : function ( data ){
if ( data . status == true ){
$ ( '.loading' ). remove ();
} else {
alert ( "gagal" );
$ ( '.loading' ). remove ();
}
}
})
}
</ script >
<!-- Parsly js end -->
view regis
< style >
#judul { font-family: Arial Black ; color: black ;}
</ style >
< ?php $this->load->view('templates/includes/header'); ?>
< body >
< div class = "account-pages" ></ div >
< div class = "clearfix" ></ div >
< div class = "wrapper-page" >
< div class = " card-box" >
< div class = "panel-body" >
<!-- alert -->
< ?php if($this->session->flashdata('error')){?>
< div class = "alert alert-danger alert-dismissable" >
< button type = "button" class = "close" data-dismiss = "alert" aria-hidden = "true" >×</ button >< strong >Error</ strong > < ?php
echo $this->session->flashdata('error'); ?>
</ div >
< ?php } ?>
<!-- end alert -->
< form id = "form" class = "form-horizontal m-t-5" action = " < ?php echo base_url()?>Question/do_regis" method = "post" >
< div class = "form-group" >
< div class = "col-xs-12 field" >
< input class = "form-control" type = "text" required = "" name = "username" placeholder = "Username" id = "user1" name = "username" >
< ul class = "parsley-errors-list filled" id = "error_nama" >< li class = "parsley-required" >Data telah ada</ li ></ ul >
</ div >
</ div >
< div class = "form-group" >
< div class = "col-xs-12 field" >
< input class = "form-control" type = "password" required = "" name = "pass" placeholder = "Password" id = "pass1" name = "password" >
</ div >
</ div >
< div class = "form-group" >
< div class = "col-xs-12 field" >
< input class = "form-control" type = "password" required = "" placeholder = "Confirm Password" data-parsley-equalto = "#pass1" >
</ div >
</ div >
< div class = "form-group text-center m-t-20" >
< div class = "col-xs-12" >
< input class = "btn btn-success btn-block text-uppercase " type = "submit" value = "Daftar" >
</ div >
</ div >
</ form >
</ div >
</ div >
< div class = "row" >
< div class = "col-sm-12 text-center" >
< p >kembali ke halaman login < a href = " < ?php echo base_url().'/Question/login'?>" class = "text-primary m-l-5" >< b >Login</ b ></ a ></ p >
</ div >
</ div >
</ div >
</ div >
</ div >
< div class = "col-sm-12 p-20" ></ div >
[java script view regis]
</ body >
</ html >
[java script view regis] lanjutan code view regis
<!-- footer-->
< ?php $this->load->view('templates/includes/footer'); ?>
<!-- script -->
< ?php $this->load->view('templates/includes/script'); ?>
<!-- Parsly js untuk validasi form -->
< script type = "text/javascript" src = "<?php echo base_url() ?>templates/assets/plugins/parsleyjs/parsley.min.js" ></ script >
<!-- Parsly js end -->
<!-- Sweet-Alert -->
< script src = "<?php echo base_url() ?>templates/assets/plugins/bootstrap-sweetalert/sweet-alert.min.js" ></ script >
< script src = "<?php echo base_url() ?>templates/assets/pages/jquery.sweet-alert.init.js" ></ script >
<!-- submit form -->
< script >
$ ( document ). ready ( function () {
// $('form').parsley();
$ ( "#error_nama" ). hide ();
});
$ ( '#user1' ). change ( function (){
var username = $ ( this ). val ();
$ . ajax ({
url : "<?php echo base_url();?>Question/cek_user" ,
method : "POST" ,
data : { username : username },
async : true ,
dataType : 'json' ,
success : function ( data ){ // Ketika proses pengiriman berhasil
if ( data . status == true ){
$ ( "#error_nama" ). hide ();
$ ( "#user1" ). removeAttr ( 'style' );
} else {
$ ( '#user1' ). val ( "" );
$ ( "#error_nama" ). show ();
$ ( "#user1" ). attr ( 'style' , 'color:red' );
$ ( "#user1" ). attr ( 'style' , 'border: 1px solid #f00' );
}
}
});
});
</ script >
<!-- end submit form -->
<!-- end script -->
view result
<!-- main content-->
< div class = "row" >
< div class = "masonry-container" >
< div class = "col-sm-12" >
< div id = "btn_video" class = "card-box" style = "text-align: center !important;" >
< h4 class = "header-title m-t-0" >< b >Hasil</ b ></ h4 >
< p > < ?php echo "$result"; ?></ p >
</ div >
</ div >
</ div >
</ div > <!-- End row -->
< div class = "row" >
< div class = "col-sm-12" >
< a href = " < ?php echo base_url().'Question/kategori/'?>" class = "btn btn-primary" >Back</ a >
< a href = " < ?php echo base_url().'Question/resetResult/'?>" class = "btn btn-warning" >Reset</ a >
</ div >
</ div >
<!-- end main content-->
<!-- script -->
[java script view result]
<!-- end script -->
[java script view result] lanjutan code view result
< ?php $this->load->view('templates/includes/script'); ?>
<!-- masonry plugin js -->
< script type = "text/javascript" src = "<?php echo base_url()?>templates/assets/plugins/isotope/js/isotope.pkgd.min.js" ></ script >
< script type = "text/javascript" >
$ ( window ). load ( function (){
var $container = $ ( '.masonry-container' );
$container . isotope ({
filter : '*'
});
});
$ ( '#btn_quiz' ). click (() => {
var id = $ ( '#id_komik' ). val ();
window . location . href = '<?php echo base_url()?>Siswa/Komik/quiz/' + id ;
});
</ script >
<!-- end -->