[moodle] / contrib / patches / dfws / webservice / apis / user.lib.php Repository:

View of /contrib/patches/dfws/webservice/apis/user.lib.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (download) (annotate)
Tue Sep 16 00:57:13 2008 WST (14 months, 1 week ago) by tusefomal
Branch: MAIN
Changes since 1.3: +2 -2 lines
Correcting a minimal typing mistake.
<?php
/**
 * Created on 05/03/2008
 * 
 * users control api
 *
 * @author Ferran Recio
 * @author David Castro Garcia
 */

/**
 * public api functions definition
 */
function mdl_user_info () {
	$info = new stdClass;
	//api name
	$info->description = 'Users control api';
	//api version
	$info->version = 2008030701;
	//data structures and arrays
	$info->structs = array();
	//user_list
	$stru = new stdClass;
	$stru->name = 'user_list';
	$stru->type = 'array';
	$stru->elems = 'user';
	$info->structs[$stru->name] = $stru;
	//user
	$stru = new stdClass;
	$stru->name = 'user';
	$stru->type = 'struct';
	
	$stru->elems = array(
				    'id' => 'integer',
				    'auth' => 'string',
				    'username' => 'string',
				    'idnumber' => 'string',
				    'deleted' =>'integer',
				    'firstname' => 'string',
				    'lastname' => 'string',
				    'email' => 'string',
				    'institution' => 'string',
				    'department' => 'string',
				    'address' => 'string',
				    'city' => 'string',
				    'country' => 'string',
				    'lang' => 'string',
				    'lastaccess' => 'integer',
				    'url' => 'string',
				    'description' => 'string'
			);
	$info->structs[$stru->name] = $stru;
	
	//function list
	$info->functions = array();
	
	$func = new stdClass;
	$func->name = 'mdl_user_add_instance';
	$func->params = array('user' => 'array');
	$func->doc = "add a user";
	$func->ret = 'integer';
	$info->functions[$func->name] = $func;
	
	$func = new stdClass;
	$func->name = 'mdl_user_add_instances';
	$func->params = array('users' => 'array');
	$func->doc = "add a user";
	$func->ret = 'id_list';
	$info->functions[$func->name] = $func;
	
	$func = new stdClass;
	$func->name = 'mdl_user_delete_instance';
	$func->params = array('userid'=>'integer');
	$func->doc = "delete a user";
	$func->ret = 'boolean';
	$info->functions[$func->name] = $func;
	
	$func = new stdClass;
	$func->name = 'mdl_user_get_instance';
	$func->params = array('userid' => 'integer');
	$func->doc = "return a user";
	$func->ret = 'user';
	$info->functions[$func->name] = $func;
	
	$func = new stdClass;
	$func->name = 'mdl_user_get_current';
	$func->params = array();
	$func->doc = "return a user";
	$func->ret = 'user';
	$info->functions[$func->name] = $func;
	
	//Moodle docs requirement batch user management
	$func = new stdClass;
	$func->name = 'mdl_user_get_users';
	$func->params = array('ids' => 'id_list');
	$func->doc = "return a set of users";
	$func->ret = 'user_list';
	$info->functions[$func->name] = $func;
	
	$func = new stdClass;
	$func->name = 'mdl_user_delete_users';
	$func->params = array('ids' => 'id_list');
	$func->doc = "deletes a set of users";
	$func->ret = 'boolean';
	$info->functions[$func->name] = $func;
	
	return $info;
}

/**
 * Creates an User with given information. Required fields are:
 * -username
 * -idnumber
 * -firstname
 * -lastname
 * -email
 * 
 * And there's some interesting fields:
 * -password
 * -auth
 * -confirmed
 * -timezone
 * -country
 * -emailstop
 * -theme
 * -lang
 * -mailformat
 * 
 * @param assoc array or object $user
 * 
 * @return userid or -1 if goes wrong
 */
function mdl_user_add_instance($user) {
	global $CFG;
	mdl_require_callable('mdl_user_add_instance');
	//TODO: We have to check the Authorization
	//TODO: Errors Returned
	
	if (is_array($user)) $user = (object) $user;
	//check some fields
	if (!isset($user->password)) $user->password = '';
	if (!isset($user->auth)) $user->auth = 'manual';
	$required = array('username','idnumber','firstname','lastname','email');
	foreach ($required as $req) {
		if (!isset($user->{$req})) return -1;
	}
	
	$record = create_user_record($user->username, $user->password, $user->auth);//TODO:configure
	if ($record) {
		$user->id = $record->id;
		if (update_record('user',$user)) return $record->id;
		mdl_user_delete_instance($record->id);
		delete_records('user','id',$record->id);
	}
	return -1;
}

/**
 * Create a lot of users un the system
 * 
 * @param array of users
 * 
 * @return assoc array of username and ids
 */
function mdl_user_add_instances($users) {
	global $CFG;
	mdl_require_callable('mdl_user_add_instances');
	
	//detect if is a single user or a group of
	if (!is_array($users)) {
		if (is_object($users)) {
			$users = array($users);
		} else {
			return array(-1);
		}
	} else {
		 $first = $users[array_pop(array_keys($users))];
		 if (!is_array($first) && !is_object($first)) $users = array($users);
	}
	//lets add some users
	$res = array();
	foreach ($users as $user) {
		$curruser = '';
		if (is_array($user) && isset($user['username'])) $curruser = $user['username'];
		if (is_object($user) && isset($user->username)) $curruser = $user->username;
		if (!$curruser) return array(-1);
		
		$res[$curruser] = mdl_user_add_instance($user);
	}
	return $res;
	
}

/**
 * Deletes an User selected by a field
 * 
 * @param integer $id
 * @param string type='idnumber'
 * 
 * @return boolean
 */
function mdl_user_delete_instance($userid,$type='idnumber') {
	mdl_require_callable('mdl_user_delete_instance');
	//TODO: We have to check the Authorization
	//TODO: Errors Returned
	//check type
	$types = array('id','idnumber','username','email');
	if(!in_array($type,$types)) return false;
	
	
	$user = get_record ('user',$type,$userid);
	if (!$user) return false;
	return delete_user($user);
}

/**
 * Deletes some User selected by a field
 * 
 * @param integer $id
 * @param string type='idnumber'
 * 
 * @return assoc array of id-booleans
 */
function mdl_user_delete_instances($userids, $type='idnumber') {
	mdl_require_callable('mdl_user_delete_instance');
	//TODO: We have to check the Authorization
	//TODO: Errors Returned
	if (!is_array($userids)) $userids = array($userids);
	$res = array();
	foreach ($userids as $userid) {
		$res[$userid] = mdl_user_delete_instance ($userid,$type);
	}
}

/**
 * Get a User
 * 
 * @param string $id
 * 
 * @return User
 */
function mdl_user_get_instance($userid) {
	mdl_require_callable('mdl_user_get_instance');
	//TODO: We have to check the Authorization
	//TODO: Errors Returned
	$fields = 'id, auth,username, idnumber, firstname, lastname, email, ' .
			'institution, deleted, lastaccess, lang, ' .
			'department, address, city, country, url, description';
	$user = get_record ('user','id',$userid,'','','','',$fields);
	if ($user) return $user;
	return mdl_get_empty_complex ('user');
}

/**
 * returns the current user
 * 
 * @return User
 */
function mdl_user_get_current () {
	global $USER,$_SERVER,$HTTP_SERVER_VARS,$_GET,$_SESSION;
	mdl_require_callable('mdl_user_get_current');
	//print_r($_SESSION);
	//print_object($USER);
	//mdl_restore_api_session ($sessid,$showit=false)
	return mdl_user_get_instance($USER->id);
	//return mdl_user_get_instance(2);
}

/**
 * returns a set of users
 * 
 * @param $ids id_list or a single integer
 * 
 * @return user_list
 */
function mdl_user_get_users ($ids) {
	$res = array();
	if (!is_array($ids)) $ids = array($ids);
	foreach ($ids as $id) {
		$user = mdl_user_get_instance($id);
		if (!$user->id) $user->id = -$id;
		$res[] = $user;
	}
	return $res;
}

/**
 * returns a set of users
 * 
 * @param $ids id_list or a single integer
 * 
 * @return boolean
 */
function mdl_user_delete_users ($ids) {
	$res = true;
	if (!is_array($ids)) $ids = array($ids);
	foreach ($ids as $id) {
		$res = $res&&mdl_user_delete_instance($id);
	}
	return $res;
}

?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7