<?php // $Id: profile.php 10642 2008-07-10 08:36:24Z zefredz $
/**
 * CLAROLINE
 *
 * This script prupose to user to edit his own profile
 *
 * @version 1.8 $Revision: 10642 $
 *
 * @copyright 2001-2008 Universite catholique de Louvain (UCL)
 *
 * @license http://www.gnu.org/copyleft/gpl.html (GPL) GENERAL PUBLIC LICENSE
 *
 * @see http://www.claroline.net/wiki/Auth/
 *
 * @author Claro Team <cvs@claroline.net>
 *
 * @package Auth
 *
 */


/**
 * Claroline Shibboleth / Switch AAI
 *
 * Customization: Show link for changing authsource to Shibboleth
 *
 * @version 0.4
 *
 * @author Daniel Streiff <daniel.streiff@fh-htwchur.ch>
 *
 */

/*=====================================================================
Init Section
=====================================================================*/

$cidReset = TRUE;
$gidReset = TRUE;
$uidRequired = TRUE;

require '../inc/claro_init_global.inc.php';

if( ! claro_is_user_authenticated() ) claro_disp_auth_form();

$messageList = array();
$display = '';
$error = false;

// include configuration files
include claro_get_conf_repository() . 'user_profile.conf.php'; // find this file to modify values.

// include library files
include_once get_path('incRepositorySys') . '/lib/user.lib.php';
include_once get_path('incRepositorySys') . '/lib/sendmail.lib.php';
include_once get_path('incRepositorySys') . '/lib/fileManage.lib.php';

$nameTools = get_lang('My User Account');

// define display
define('DISP_PROFILE_FORM',__LINE__);
define('DISP_MOREINFO_FORM',__LINE__);
define('DISP_REQUEST_COURSE_CREATOR_STATUS',__LINE__);
define('DISP_REQUEST_REVOQUATION',__LINE__);

$display = DISP_PROFILE_FORM;

/*=====================================================================
CONTROLER Section
=====================================================================*/

$extraInfoDefList = get_userInfoExtraDefinitionList();


$user_data = user_initialise();
$user_data = user_get_properties(claro_get_current_user_id());

$acceptedCmdList = array( 'exCCstatus'
                        , 'exRevoquation'
                        , 'reqCCstatus'
                        , 'reqRevoquation'
                        , 'editExtraInfo'
                        , 'exMoreInfo'
                        );

if ( isset($_REQUEST['cmd']) && in_array($_REQUEST['cmd'],$acceptedCmdList) )
{
    $cmd = $_REQUEST['cmd'];
}
else
{
    $cmd = '';
}

if ( isset($_REQUEST['applyChange']) )
{
    $profile_editable = get_conf('profile_editable');

    // get params form the form
    if ( isset($_REQUEST['lastname']) && in_array('name',$profile_editable) )              $user_data['lastname'] = trim($_REQUEST['lastname']);
    if ( isset($_REQUEST['firstname']) && in_array('name',$profile_editable) )             $user_data['firstname'] = trim($_REQUEST['firstname']);
    if ( isset($_REQUEST['officialCode']) && in_array('official_code',$profile_editable) ) $user_data['officialCode'] = trim($_REQUEST['officialCode']);
    if ( isset($_REQUEST['username']) && in_array('login',$profile_editable) )             $user_data['username'] = trim($_REQUEST['username' ]);
    if ( isset($_REQUEST['old_password']) && in_array('password',$profile_editable) )      $user_data['old_password'] = trim($_REQUEST['old_password']);
    if ( isset($_REQUEST['password']) && in_array('password',$profile_editable) )          $user_data['password'] = trim($_REQUEST['password']);
    if ( isset($_REQUEST['password_conf']) && in_array('password',$profile_editable) )     $user_data['password_conf'] = trim($_REQUEST['password_conf']);
    if ( isset($_REQUEST['email']) && in_array('email',$profile_editable) )                $user_data['email'] = trim($_REQUEST['email']);
    if ( isset($_REQUEST['officialEmail']) && in_array('email',$profile_editable) )        $user_data['officialEmail'] = trim($_REQUEST['officialEmail']);
    if ( isset($_REQUEST['phone']) && in_array('phone',$profile_editable) )                $user_data['phone'] = trim($_REQUEST['phone']);
    if ( isset($_REQUEST['language']) && in_array('language',$profile_editable) )          $user_data['language'] = trim($_REQUEST['language']);

    // manage password.

    if (empty($user_data['password']) && empty($user_data['password_conf']))
    {
        unset ($user_data['password']);
        unset ($user_data['password_conf']);
    }

    // validate forum params

    $messageList['warning'] = user_validate_form_profile($user_data, claro_get_current_user_id());

    if ( count($messageList['warning']) == 0 )
    {
        // if no error update use setting
        user_set_properties(claro_get_current_user_id(), $user_data);
        event_default('PROFILE_UPDATE', array('user'=>claro_get_current_user_id()));

        // re-init the system to take new settings in account

        $uidReset = true;
        include '../inc/claro_init_local.inc.php';
        $messageList['info'][] = get_lang('The information have been modified') . '<br />' . "\n";

        // Initialise
        $user_data = user_get_properties(claro_get_current_user_id());

    } // end if $userSettingChangeAllowed
    else
    {
        // user validate form return error messages
        $error = true;
    }

}
elseif ( ! claro_is_allowed_to_create_course() && get_conf('can_request_course_creator_status')
&& 'exCCstatus' == $cmd )
{
    // send a request for course creator status
    profile_send_request_course_creator_status($_REQUEST['explanation']);
    $messageList['info'][] = get_lang('Your request to become a course creator has been sent to platform administrator(s).');
}
elseif (    get_conf('can_request_revoquation')
&& 'exRevoquation' == $cmd )
{
    // send a request for revoquation
    if (profile_send_request_revoquation($_REQUEST['explanation'], $_REQUEST['loginToDelete'],$_REQUEST['passwordToDelete']))
    $messageList['info'][] = get_lang('Your request to remove your account has been sent');
    else
    switch (claro_failure::get_last_failure())
    {
        case 'EXPLANATION_EMPTY' :
            $messageList['warn'][] = get_lang('You left some required fields empty');
            $messageList['info'][] = get_lang('Explain cannot be empty');
            $noQUERY_STRING = TRUE;
            $interbredcrump[]= array('url'=>$_SERVER['PHP_SELF'],'name' =>$nameTools);
            $nameTools = get_lang('Request to remove this account');
            $display = DISP_REQUEST_REVOQUATION;
        break;

    }
}
elseif (  ! claro_is_allowed_to_create_course() && get_conf('can_request_course_creator_status')
&& 'reqCCstatus' == $cmd )
{
    // display course creator status form
    $noQUERY_STRING = TRUE;
    $display = DISP_REQUEST_COURSE_CREATOR_STATUS;
    $interbredcrump[]= array('url'=>$_SERVER['PHP_SELF'],'name' =>$nameTools);
    $nameTools = get_lang('Request course creation status');
}
elseif ( get_conf('can_request_revoquation')
&& 'reqRevoquation' == $cmd )
{
    // display revoquation form
    $noQUERY_STRING = TRUE;
    $interbredcrump[]= array('url'=>$_SERVER['PHP_SELF'],'name' =>$nameTools);
    $nameTools = get_lang('Request to remove this account');
    $display = DISP_REQUEST_REVOQUATION;
}
elseif ( 'editExtraInfo' == $cmd && 0 < count($extraInfoDefList) )
{
    // display revoquation form
    $noQUERY_STRING = TRUE;
    $display = DISP_MOREINFO_FORM;
    $interbredcrump[]= array('url'=>$_SERVER['PHP_SELF'],'name' =>$nameTools);
    $nameTools = get_lang('Complementary fields');
    $userInfo = get_user_property_list(claro_get_current_user_id());

}
elseif ( 'exMoreInfo' == $cmd && 0 < count($extraInfoDefList)  )
{
    if (array_key_exists('extraInfoList',$_REQUEST))
    {
        foreach( $_REQUEST['extraInfoList'] as $extraInfoName=> $extraInfoValue)
        {
            set_user_property(claro_get_current_user_id(),$extraInfoName,$extraInfoValue,'userExtraInfo');
        }
    }
}


// Initialise
$user_data['userExtraInfoList'] =  get_user_property_list(claro_get_current_user_id());

$profileMenu =  array();

switch ( $display )
{
    case DISP_PROFILE_FORM :


        // display user tracking link

        $profileText = claro_text_zone::get_content('textzone_edit_profile_form');

        if( get_conf('is_trackingEnabled') )
        {
            // display user tracking link
            $profileMenu[] = '<a class="claroCmd" href="' . get_conf('urlAppend') . '/claroline/tracking/personnalLog.php' . claro_url_relay_context('?') . '">'
            .                 '<img src="' . get_conf('clarolineRepositoryWeb','/claroline') . '/img/statistics.gif" />' . get_lang('View my statistics')
            .                 '</a>'
            ;
        }

        // display request course creator status
        if ( ! claro_is_allowed_to_create_course() && get_conf('can_request_course_creator_status') )
        {
            $profileMenu[] = claro_html_cmd_link($_SERVER['PHP_SELF'] . '?cmd=reqCCstatus' . claro_url_relay_context('&amp;')
                                                , get_lang('Request course creation status') );
        }

        // display user revoquation
        if ( get_conf('can_request_revoquation') )
        {
            $profileMenu[] =  claro_html_cmd_link( $_SERVER['PHP_SELF']
                                                 . '?cmd=reqRevoquation' . claro_url_relay_context('&amp;')
                                                 , get_lang('Delete my account')
                                                 ) ;
        }

        /* Claroline Shibboleth / Switch AAI */

        if ( get_conf('claro_ShibbolethEnabled') )
        {
            // Configuration
            require(dirname(__FILE__) . '/extauth/shibboleth/shibboleth.lib.php');

            // check users authSource
            $sql = 'SELECT authSource
                    FROM `' . $tbl_user . '`
                    WHERE user_id = ' . $_uid;
            $result = claro_sql_query($sql);
            if ( mysql_num_rows($result) == 1 )
            {
                $uData = mysql_fetch_array($result);
                if ( !($uData['authSource'] == $shibbolethAuthSource) )
                {
                    $profileMenu[] = claro_html_cmd_link(get_conf('claro_ShibbolethPath') . 'shibbolethUser.php', $claro_ShibbolethUser );
                }
            }
        }

        break;
}




/**********************************************************************
View Section
**********************************************************************/

// display header
include get_path('incRepositorySys') . '/claro_init_header.inc.php';

echo claro_html_tool_title($nameTools);

echo claro_html_msg_list($messageList);

switch ( $display )
{
    case DISP_PROFILE_FORM :

        // display form profile
        if ( trim ($profileText) != '')
        {
            echo '<div class="info profileEdit">'
            .    $profileText
            .    '</div>'
            ;
        }

        echo '<p>'
        .    claro_html_menu_horizontal($profileMenu)
        .    '</p>'
        .    user_html_form_profile($user_data)
        ;

        break;

    case DISP_MOREINFO_FORM :

        // display request course creator form
        echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">' . "\n"
        .    '<input type="hidden" name="cmd" value="exMoreInfo" />' . "\n"
        .    '<table>' . "\n"
        ;

        foreach ($extraInfoDefList as $extraInfoDef)
        {
            $currentValue = array_key_exists($extraInfoDef['propertyId'],$userInfo)
            ? $userInfo[$extraInfoDef['propertyId']]
            : $extraInfoDef['defaultValue'];
            $requirement = (bool) (TRUE == $extraInfoDef['required']);

            $labelExtraInfoDef = $extraInfoDef['label'];
            echo form_input_text('extraInfoList['.htmlentities($extraInfoDef['propertyId']).']',$currentValue,get_lang($labelExtraInfoDef),$requirement);

        }

        echo '<tr valign="top">' . "\n"
        .    '<td>' . get_lang('Submit') . ': </td>' . "\n"
        .    '<td><input type="submit" value="' . get_lang('Ok') . '">&nbsp; ' . "\n"
        .    claro_html_button($_SERVER['PHP_SELF'], get_lang('Cancel')) . "\n"
        .    '</td></tr>' . "\n"
        .     form_row('&nbsp;', '<small>' . get_lang('<span class="required">*</span> denotes required field') . '</small>')
        .    '</table>' . "\n"
        .    '</form>' . "\n"
        ;
        break;

    case DISP_REQUEST_COURSE_CREATOR_STATUS :


        echo '<p>' . get_lang('Fill the area to explain your motivation and submit your request. An e-mail will be sent to platform adminisrator(s).') . '</p>';

        // display request course creator form
        echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">' . "\n"
        .    '<input type="hidden" name="cmd" value="exCCstatus" />' . "\n"
        .    '<table>' . "\n"
        .    form_input_textarea('explanation','',get_lang('Comment'),true,6)
        .    '<tr valign="top">' . "\n"
        .    '<td>' . get_lang('Submit') . ': </td>' . "\n"
        .    '<td><input type="submit" value="' . get_lang('Ok') . '" />&nbsp; ' . "\n"
        .    claro_html_button($_SERVER['PHP_SELF'], get_lang('Cancel')) . "\n"
        .    '</td></tr>' . "\n"
        .    '</table>' . "\n"
        .    '</form>' . "\n"
        ;

        break;

    case DISP_REQUEST_REVOQUATION :
        if ( get_conf('can_request_revoquation') )
        {

            echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">' . "\n"
            .    '<input type="hidden" name="cmd" value="exRevoquation" />' . "\n"
            .    '<table>' . "\n"
            .    form_input_text('loginToDelete','',get_lang('Username'),true)
            .    form_input_password('passwordToDelete','',get_lang('Password'),true)
            .    form_input_textarea('explanation','',get_lang('Comment'),true,6)
            .    '<tr valign="top">' . "\n"
            .    '<td>' . get_lang('Delete my account') . ': </td>' . "\n"
            .    '<td><input type="submit" value="' . get_lang('Ok') . '">&nbsp; ' . "\n"
            .    claro_html_button($_SERVER['PHP_SELF'], get_lang('Cancel')) . "\n"
            .    '</td></tr>' . "\n"
            .    '</table>' . "\n"
            .    '</form>' . "\n"
            ;
        }
        break;

} // end switch display

// display footer

include get_path('incRepositorySys') . '/claro_init_footer.inc.php';

?>
