Commit 4af41c32 authored by Penny Leach's avatar Penny Leach
Browse files

account prefs stuff

parent 650d0306
......@@ -71,7 +71,9 @@ class Session {
'firstname' => '',
'lastname' => '',
'prefname' => '',
'email' => ''
'email' => '',
'accountprefs' => array(),
'activityprefs' => array(),
);
// Resume an existing session if required
if (isset($_COOKIE['PHPSESSID'])) {
......@@ -99,6 +101,33 @@ class Session {
return $this->defaults[$key];
}
/**
* This function returns a method for a particular
* activity type.
* or null if it's not set.
*
* @param string $key the activity type
*/
public function get_activity_preference($key) {
if (isset($_SESSION['activityprefs'][$key])) {
return $_SESSION['activityprefs'][$key];
}
return null;
}
/**
* This function returns a value for a particular
* account preference, or null if it's not set.
*
* @param string $key the field name
*/
public function get_account_preference($key) {
if (isset($_SESSION['accountprefs'][$key])) {
return $_SESSION['accountprefs'][$key];
}
return null;
}
/**
* Sets the session property keyed by $key.
*
......@@ -115,6 +144,16 @@ class Session {
$_SESSION[$key] = $value;
}
function set_account_preference($field, $value) {
set_account_preference($this->get('id'), $field, $value);
$_SESSION['accountprefs'][$field] = $value;
}
function set_activity_preference($activity, $method) {
set_activity_preference($this->get('id'), $activity, $method);
$_SESSION['activityprefs'][$activity] = $method;
}
/**
* Logs in the given user.
*
......@@ -132,6 +171,8 @@ class Session {
$this->set($key, (isset($USER->{$key})) ? $USER->{$key} : $this->defaults[$key]);
}
$this->set('logout_time', time() + get_config('session_timeout'));
$_SESSION['activityprefs'] = load_activity_preferences($this->get('id'));
$_SESSION['accountprefs'] = load_account_preferences($this->get('id'));
}
/**
......
......@@ -83,6 +83,7 @@ require('ddl.php');
require('constants.php');
require('web.php');
require('activity.php');
require('user.php');
// Database access functions
require('adodb/adodb-exceptions.inc.php');
......
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage core
* @author Penny Leach <penny@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
/**
* loads up activity preferences for a given user
*
* @param int $userid to load preferences for
* @todo caching
*/
function load_activity_preferences($userid) {
$prefs = array();
if (empty($userid)) {
throw new InvalidArgumentException("couldn't load activity preferences, no user id specified");
}
if ($prefs = get_records('usr_activity_preference', 'usr', $userid)) {
foreach ($prefs as $p) {
$prefs[$p->activity] = $p->method;
}
}
return $prefs;
}
/**
* loads up account preferences for a given user
* if you want them for the current user
* use $SESSION->accountprefs
*
* @param int $userid to load preferences for
* @todo caching
* @todo defaults?
*/
function load_account_preferences($userid) {
$prefs = array();
$expectedprefs = expected_account_preferences();
if (empty($userid)) {
throw new InvalidArgumentException("couldn't load account preferences, no user id specified");
}
if ($prefs = get_records('usr_account_preference', 'usr', $userid)) {
foreach ($prefs as $p) {
$prefs[$p->field] = $p->value;
}
}
foreach ($expectedprefs as $field => $default) {
if (!isset($prefs[$field])) {
$prefs[$field] = $default;
}
}
return $prefs;
}
/**
* sets a user preference in the database
* if you want to set it in the session as well
* use SESSION->set_account_preference
*
* @param int $userid user id to set preference for
* @param string $field preference field to set
* @param string $value preference value to set.
*/
function set_account_preference($userid, $field, $value) {
if (record_exists('usr_account_preference', 'usr', $userid, 'field', $field)) {
set_field('usr_account_preference', 'value', $value, 'usr', $userid, 'field', $field);
}
else {
try {
$pref = new StdClass;
$pref->usr = $userid;
$pref->field = $field;
$pref->value = $value;
insert_record('usr_account_preference', $pref);
}
catch (Exception $e) {
throw new InvalidArgumentException("Failed to insert account preference "
." $value for $field for user $userid");
}
}
}
/**
* sets an activity preference in the database
* if you want to set it in the session as well
* use $SESSION->set_activity_preference
*
* @param int $userid user id to set preference for
* @param string $activity activity type to set
* @param string $method notification method to set.
*/
function set_activity_preference($userid, $activity, $method) {
if (record_exists('usr_activity_preference', 'usr', $userid, 'activity', $activity)) {
set_field('usr_activity_preference', 'method', $method, 'usr', $userid, 'activity', $activity);
}
else {
try {
$pref = new StdClass;
$pref->usr = $userid;
$pref->activity = $activity;
$pref->method = $method;
insert_record('usr_activity_preference', $pref);
}
catch (Exception $e) {
throw new InvalidArgumentException("Failed to insert activity preference "
." $methodfor $activity for user $userid");
}
}
}
function expected_account_preferences() {
return array('friendscontrol' => 'auth',
'wysiwyg' => 1,
'messages' => 'allow',
'lang' => 'en.utf8'
);
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment