Commit 693c3475 authored by Donal McMullan's avatar Donal McMullan
Browse files

New classes - possible kickstart for ORM

parent 114988a1
<?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 auth-internal
* @author Donal McMullan <donal@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
class Application {
const UNINITIALIZED = 0;
const INITIALIZED = 1;
const PERSISTENT = 2;
private $initialized = self::UNINITIALIZED;
private $changed = false;
private $oldname;
private $members = array('name' => '',
'displayname' => '',
'xmlrpcserverurl' => ''
);
public function __construct($result = null) {
if(null == $result) {
return;
}
$this->populate($result);
$this->initialized = self::PERSISTENT;
}
protected function populate($result) {
$values = get_object_vars($result);
foreach ($values as $key => $value) {
$this->__set($key, $value);
}
$this->oldname = $result->name;
}
public static function findByName($name) {
$result = get_record('application', 'name', $name);
if (false == $result) {
throw new ParamOutOfRangeException(addslashes($name) .' is not an application.');
}
return new Application($result);
}
public function __set($name, $value) {
if (!array_key_exists($name, $this->members)) {
throw new ParamOutOfRangeException(addslashes($name) .' is not a member of Application.');
}
if ($value != $this->members[$name]) {
$this->members[$name] = $value;
$this->changed = true;
}
if (!empty($this->members['name']) && !empty($this->members['xmlrpcserverurl'])) {
$this->initialized = self::INITIALIZED;
}
return $this;
}
public function __get($name) {
if (array_key_exists($name, $this->members)) {
return $this->members[$name];
}
return null;
}
public function commit() {
if ($this->initialized == self::UNINITIALIZED) {
return false;
}
if (false == $this->changed) return true;
if (empty($this->members['displayname'])) {
$this->members['displayname'] = $this->members['name'];
}
if (false == $this->changed) return true;
$application = new stdClass();
$application->name = $this->members['name'];
$application->displayname = $this->members['displayname'];
$application->xmlrpcserverurl = $this->members['xmlrpcserverurl'];
if ($this->initialized == self::INITIALIZED) {
$this->initialized = self::PERSISTENT;
return insert_record('application',$application);
} elseif ($this->initialized == self::PERSISTENT) {
return update_record('application', $application, array('name' => $application->oldname) );
}
return false;
}
}
?>
<?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 auth-internal
* @author Donal McMullan <donal@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();
global $CFG;
require_once($CFG->docroot .'/include/eLearning/application.php');
class ApplicationSet implements Iterator {
protected $resultset = array();
function __construct() {
$this->resultset = get_records_assoc('application');
}
/////////////////////////////////////////////////////////
// Iterator stuff
public function rewind() {
reset($this->resultset);
}
public function current() {
if (false === current($this->resultset)) {
return false;
}
if (!is_a(current($this->resultset), 'Application')) {
$key = key($this->resultset);
$current = current($this->resultset);
$this->resultset[$key] = new Application($current);
}
return current($this->resultset);
}
public function key() {
return key($this->resultset);
}
public function next() {
next($this->resultset);
return $this->current();
}
public function valid() {
return $this->current() !== false;
}
// End of iterator stuff
/////////////////////////////////////////////////////////
}
?>
<?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 auth-internal
* @author Donal McMullan <donal@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();
global $CFG;
class Host {
protected $wwwroot = '';
protected $displayname = '';
protected $ipaddress = '';
protected $deleted = 0;
protected $publickey = '';
protected $publickeyexpires = 0;
protected $portno = 80;
protected $lastconnecttime = 0;
protected $application = '';
protected $theyssoin = 0;
protected $wessoout = 0;
protected $institution = '';
function __construct($resultset = null) {
if (is_null($resultset)) {
return $this;
}
$this->populate($resultset);
}
function __get($name) {
if (isset($this->{$name})) {
return $this->{$name};
}
return;
}
function __set($name, $value) {
if (isset($this->{$name})) {
return $this->{$name};
}
return;
}
function findByWwwroot($wwwroot) {
global $CFG;
if (!is_string($name) || strlen($name) < 1 || strlen($name) > 255) {
throw new SystemException();
}
$result = get_record('host', 'wwwroot', $wwwroot);
if (false == $result) {
throw new SystemException('Unknown wwwroot: '.addslashes($wwwroot));
}
$this->populate($result);
}
function commit($result) {
$this->wwwroot = $result->wwwroot;
$this->name = $result->name;
$this->ipaddress = $result->ipaddress;
$this->deleted = $result->deleted;
$this->publickey = $result->publickey;
$this->publickeyexpires = $result->publickeyexpires;
$this->portno = $result->portno;
$this->lastconnecttime = $result->lastconnecttime;
$this->application = $result->application;
$this->theyssoin = $result->theyssoin;
$this->wessoout = $result->wessoout;
$this->institution = $result->institution;
}
protected function populate($result) {
$this->wwwroot = $result->wwwroot;
$this->name = $result->name;
$this->ipaddress = $result->ipaddress;
$this->deleted = $result->deleted;
$this->publickey = $result->publickey;
$this->publickeyexpires = $result->publickeyexpires;
$this->portno = $result->portno;
$this->lastconnecttime = $result->lastconnecttime;
$this->application = $result->application;
$this->theyssoin = $result->theyssoin;
$this->wessoout = $result->wessoout;
$this->institution = $result->institution;
}
}
?>
<?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 auth-internal
* @author Donal McMullan <donal@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();
global $CFG;
require_once($CFG->docroot .'/include/eLearning/peer.php');
class HostSet implements Iterator {
protected $resultset = array();
protected $institution = '';
function __construct($institution = null) {
if (is_null($institution)) {
return;
}
$this->findByInstitution($institution);
}
function findByInstitution($institution) {
global $CFG;
$sql = 'SELECT
h.*
FROM
'.$CFG->dbprefix.'host h
WHERE
h.institution = ?
ORDER BY
h.wwwroot';
$this->resultset = get_records_sql_assoc($sql, array('institution' => $institution));
if (false == $this->resultset) {
return false;
}
$this->institution = $institution;
return true;
}
function findByWwwroot($wwwroot) {
global $CFG;
$len = strlen($wwwroot);
if (!is_string($wwwroot) || $len < 1 || $len > 255) {
throw new ParamOutOfRangeException('WWWROOT: '.addslashes($wwwroot).' is out of range');
}
$sql = 'SELECT
h2.*
FROM
'.$CFG->dbprefix.'host h1,
'.$CFG->dbprefix.'host h2
WHERE
h1.institution = h2.institution AND
h1.wwwroot = ?
ORDER BY
h2.wwwroot';
$this->resultset = get_records_sql_assoc($sql, array('wwwroot' => $wwwroot));
if (false == $this->resultset) {
throw new ParamOutOfRangeException('Unknown wwwroot: '.addslashes($wwwroot));
}
}
/////////////////////////////////////////////////////////
// Iterator stuff
public function rewind() {
reset($this->resultset);
}
public function current() {
if (false === current($this->resultset)) {
return false;
}
if (!is_a(current($this->resultset), 'Peer')) {
$key = key($this->resultset);
$current = current($this->resultset);
$this->resultset[$key] = new Peer($current);
}
return current($this->resultset);
}
public function key() {
return key($this->resultset);
}
public function next() {
next($this->resultset);
return $this->current();
}
public function valid() {
return $this->current() !== false;
}
// End of iterator stuff
/////////////////////////////////////////////////////////
}
?>
<?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 auth-internal
* @author Nigel McNie <nigel@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();
global $CFG;
require($CFG->docroot .'/include/eLearning/hostset.php');
class Institution {
const UNINITIALIZED = 0;
const INITIALIZED = 1;
const PERSISTENT = 2;
protected $initialized = self::UNINITIALIZED;
protected $hostset;
protected $members = array(
'name' => '',
'displayname' => '',
'registerallowed' => 1,
'updateuserinfoonlogin' => 0,
'defaultaccountlifetime' => null,
'defaultaccountinactiveexpire' => null,
'defaultaccountinactivewarn' => 0
);
function __construct($name = null) {
if (is_null($name)) {
return $this;
}
if ($this->findByName($name)) {
throw new ParamOutOfRangeException('No such institution');
}
}
function __get($name) {
if ($name == 'hosts') {
if (count($this->hosts) == 0 && $this->ready) {
$this->getHostData();
}
}
if (array_key_exists($name, $this->members)) {
return $this->members[$name];
}
return null;
}
public function __set($name, $value) {
if (!is_string($name) | !array_key_exists($name, $this->members)) {
throw new ParamOutOfRangeException();
}
if ($name == 'name') {
if (!is_string($value) || empty($value) || strlen($value) > 255) {
throw new ParamOutOfRangeException("'name' should be a string between 1 and 255 characters in length");
}
} elseif ($name == 'displayname') {
if (!is_string($value) || empty($value) || strlen($value) > 255) {
throw new ParamOutOfRangeException("'displayname' ($value) should be a string between 1 and 255 characters in length");
}
} elseif ($name == 'registerallowed') {
if (!is_numeric($value) || $value < 0 || $value > 1) {
throw new ParamOutOfRangeException("'registerallowed' should be zero or one");
}
} elseif ($name == 'updateuserinfoonlogin') {
if (!is_numeric($value) || $value < 0 || $value > 1) {
throw new ParamOutOfRangeException("'updateuserinfoonlogin' should be zero or one");
}
} elseif ($name == 'defaultaccountlifetime') {
if (!empty($value) && (!is_numeric($value) || $value < 0 || $value > 9999999999)) {
throw new ParamOutOfRangeException("'defaultaccountlifetime' should be a number between 1 and 9,999,999,999");
}
} elseif ($name == 'defaultaccountinactiveexpire') {
if (!empty($value) && (!is_string($value) || empty($value) || strlen($value) > 255)) {
throw new ParamOutOfRangeException("'defaultaccountinactiveexpire' should be a number between 1 and 9,999,999,999");
}
} elseif ($name == 'defaultaccountinactivewarn') {
if (!empty($value) && strlen($value) > 255) {
throw new ParamOutOfRangeException("'defaultaccountinactivewarn' should be a number between 1 and 9,999,999,999");
}
}
$this->members[$name] = $value;
}
function findByName($name) {
global $CFG;
if (!is_string($name) || strlen($name) < 1 || strlen($name) > 255) {
throw new ParamOutOfRangeException("'name' must be a string.");
}
$result = get_record('institution', 'name', $name);
if (false == $result) {
return false;
}
$this->initialized = self::PERSISTENT;
$this->populate($result);
return $this;
}
function findByWwwroot($wwwroot) {
global $CFG;
if (!is_string($wwwroot) || strlen($wwwroot) < 1 || strlen($wwwroot) > 255) {
throw new SystemException();
}
$this->hostset = new HostSet();
$this->hostset->findByWwwroot($wwwroot);
if (false == $a_host = $this->hostset->current()) {
return false;
}
$institution = $a_host->institution;
$result = get_record('institution', 'name', $institution);
if (false == $result) {
throw new SystemException('Invalid Institution name');
}
$this->initialized = self::PERSISTENT;
$this->populate($result);
}
function getHostData() {
$this->hostset = new HostSet();
$this->hostset->findByInstitution($this->name);
}
function initialise($name, $displayname) {
if (empty($name) || !is_string($name)) {
return false;
}
$this->name = $name;
if (empty($displayname) || !is_string($displayname)) {
return false;
}
$this->displayname = $displayname;
$this->initialized = max(self::INITIALIZED, $this->initialized);
return true;
}
function verifyReady() {
if (empty($this->members['name']) || !is_string($this->members['name'])) {
return false;
}
if (empty($this->members['displayname']) || !is_string($this->members['displayname'])) {
return false;