Pages

Wednesday, February 23, 2011

Drupal Module - Mata Kuliah

<?php
/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 23, 2011
 * @name    : iphone
 */

/**
 * This module provide implementation of the access to the table
 *
 * There are available of the 4 table:
 * a. user        -> listing available of user
 * b. mk        -> listing of all offered mata kuliah(subject)
 * c. pilihan    -> listing of all selected mata kuliah
 * d. ava        -> listing of all non-selected mata kuliah
 *
 * The structures is like below:
 * user table:
 *         id int not null auto_increment key,
 *         username char(50)
 * mk table:
 *         id int not null auto_increment key,
 *         mata_kuliah char(50),
 *         hari char(10)
 * pilihan table:
 *         id int not null auto_incrment key,
 *         uid int,
 *         mkid int,
 * ava table:
 *         id int not null auto_increment key,
 *         uid int,
 *         mkid int,
 */

 
/*
 * user id: 1 on line 55, 102, 135, 94
 */
function iphone_menu(){
    $items = array();
   
    $items['iphone'] = array(
        'title' => 'Mata Kuliah',
        'page callback' => 'get_my_mk',
        'access callback' => TRUE,       
    );
   
    $items['iphone/default'] = array(
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'title' => 'Your Mat Kul',
        'weight' => 0,
    );
   
    $items['iphone/add'] = array(
        'type' => MENU_LOCAL_TASK,
        'title' => 'Add Mat Kul',
        'weight' => 1,
        'page callback' => 'add_av_mk',
        'access callback' => TRUE,
    );
   
    $items['iphone/remove/%'] = array(
        'type' => MENU_CALLBACK,
        'page callback' => 'remove_mk',
        'page arguments' => array(2),
        'access arguments' => array('access arguments page'),   
        'title' => 'Removed',
    );
   
    $items['iphone/addmk/%'] = array(
        'type' => MENU_CALLBACK,
        'page callback' => 'add_one',
        'page arguments' => array(2),
        'access arguments' => array('access arguments page'),
        'title' => 'Add One',
    );
   
    return $items;
}


/**
 * PART I - Your Mat Kul Primary Tabs
 *
 * Formatting the selected mata kuliah to table.
 */
function get_my_mk(){
    $i = 1;
   
    $output = '<h2>'.t('Luna Maya').'</h2>';
    $output .= '<div><table><tr><th>No</th><th>Mata Kuliah</th><th>Hari</th><th>Operation</th></tr>';
   
    $query = db_select('pilihan','p')->fields('p')->condition('uid',set_uid())->execute()->fetchAll();
    foreach($query as $value){
        $result = get_mk($value->mkid);
        $output .= '<tr>';
        $output .= '<td>'.$i++.'</td>';
        $output .= '<td>'.$result['mk'].'</td>';
        $output .= '<td>'.$result['hari'].'</td>';
        $output .= '<td><a href="http://localhost/drupal/iphone/remove/'.$value->mkid.'">Remove</a></td>';
        $output .= '</tr>';
    }
   
    $output .= '</table></div>';
   
    return $output;
}

/**
 * Just select mata kuliah and hari in mk table based on the pilihan id.
 */
function get_mk($mk_id){
    $query = db_select('mk','m')->fields('m')->condition('id',$mk_id)->execute()->fetchAll();
    $mk = array();
    foreach($query as $value){
        $mk['mk'] = $value->mata_kuliah;
        $mk['hari'] = $value->hari;       
    }
   
    return $mk;
}


/**
 * Delete the selected matakuliah
 */
function remove_mk($mkid){
    db_delete('pilihan')->condition('mkid',$mkid)->execute();
    db_insert('ava')->fields(array('uid'=>set_uid(),'mkid'=>$mkid))->execute();
   
    return get_my_mk();
}
/**********************************
 *    End of PART I
 **********************************/




/**
 *     PATH II - Add Mat Kul Primary Tab
 *
 *    Render the table based on the ava table
 */
function add_av_mk(){
    $i = 1;
    $output = '<div><h2>List Available Of Mata Kuliah</h2></div>';
    $output .= '<div><table><tr><th>No</th><th>Mata Kuliah</th><th>Hari</th><th>Operation</th></tr>';
    $query = db_select('ava','a')->fields('a')->condition('uid',set_uid())->execute()->fetchAll();
    foreach($query as $value){
        $result = get_av($value->mkid);
        $output .= '<td>'.$i++.'</td>';
        $output .= '<td>'.$result['mk'].'</td>';
        $output .= '<td>'.$result['hari'].'</td>';
        $output .= '<td><a href="http://localhost/drupal/iphone/addmk/'.$value->id.'">Add</a></td>';
        $output .= '</tr>';
    }
   
    $output .= '</table></div>';
   
    return $output;
}

/**
 * Get one line of mk table. Get the both info: mk and hari
 */
function get_av($mkid){
    $query = db_select('mk','m')->fields('m')->condition('id',$mkid)->execute()->fetchAll();
    $result = array();
    foreach($query as $value){
        $result['mk'] = $value->mata_kuliah;
        $result['hari'] = $value->hari;
    }
   
    return $result;
}


/**
 * Add one mata kuliah based on link 'Add' in the 'Add Mat Kul' primary tabs
 */
function add_one($ava_id){
    $query = db_select('ava','a')->fields('a')->condition('id',$ava_id)->execute()->fetchAll();
    $mk_id = null;
    foreach($query as $value){
        $mk_id = $value->mkid;
    }
   
    db_insert('pilihan')->fields(array('uid'=>set_uid(), 'mkid'=>$mk_id))->execute();
    db_delete('ava')->condition('id',$ava_id)->execute();
   
    return add_av_mk();
   
}

/**
 * END OF PART TWO
 */
 
/**
 * Helper function
 */
function set_uid(){
    return 2;
}

No comments:

Post a Comment