<?php
/**
 * Phoebe pedagogic planner
 *
 * PHP version 5
 *
 * LICENSE: The GNU General Public License (GPL), version 2. See LICENSE.txt
 *
 * Copyright (c) 2006-2008 JISC/HEFCE/University of Oxford
 *
 * @author      David Balch <david.balch@conted.ox.ac.uk>
 * @license     http://opensource.org/licenses/gpl-license.php GPL2
 * @link        http://phoebe-project.conted.ox.ac.uk/
 */


/* Save the value for a user preference */
$content['settings'] = 'dbWrite';
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/core.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/session.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/utils.php');

/*if(!$_POST) {
    $_POST = $_GET;
}*/

// Sanitise data before sending to database
if(preg_match('/^(\w_?)+$/', $_POST['id'])) {
    $id = $_POST['id'];
}
if($_POST['value'] != '') {
    $value = $_POST['value'];
}

if(isset($id) && isset($value)) {
    //echo $id;
    //echo $value;
    // Update value
    $sql = 'UPDATE users '.
           'SET '.$id.' = '.$mdb2->quote($value).' '.
           'WHERE user_id = '.$user_id;
    //echo $sql;
    
    $insert = $mdb2->queryAll($sql);
    if (PEAR::isError($insert)) {
        echo 'Error saving new value - '.$insert->getMessage();
    } else {
        // Update session values
        $_SESSION['userprefs'][$id] = $value;
        echo $value;
    }
} else {
    echo "Error saving value - invalid parameters:\n";
    //echo "id: ".$id;
    //echo "\nvalue: ".$value;
}
?>