Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic a3 #32

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions App/ta2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php
namespace WCS;
require_once 'Work-Cell-Scheduler/Config/global.php';

class Workers {


private $db=NULL;
private $workerID=NULL;
private $name=NULL;
private $subcell=NULL;
private $rate=NULL;


function __construct(){
//print "WCS/Pers>";
$this->db = new \mysqli(\WCS\Config::$dbhost,\WCS\Config::$dbuser,\WCS\Config::$dbpassword,\WCS\Config::$dbdatabase);
if($this->db===NULL){
die("Error unable to connect to database");
}
}

function __destruct() {
if($this->db!=NULL){
$this->db->close();
}
}

public function SetID($ID){
if(preg_match('/^[0-9]+$/', $ID)){
$this->workerID=$ID;
RETURN TRUE;
}
RETURN FALSE;
}

function SetName($name){
if(preg_match('/^\s*$/',$name)){
return FALSE;
}
$this->name=$name;
return TRUE;
}

public function SetSubcell($subcell){
if(preg_match('/^[0-9]+$/', $subcell)){
$this->subcell=$subcell;
RETURN TRUE;
}
RETURN FALSE;
}

public function SetRate($rate){
if(preg_match('/^[0-9]+$/', $rate)){
$this->rate=$rate;
RETURN TRUE;
}
RETURN FALSE;
}
public function insert(){
$stmt=$this->db->prepare("INSERT INTO Workers (workerID,name,subcell,rate) VALUES (?,?,?,?)");
if($stmt===FALSE){
error_log("WCS/Workers.insert> stmt:".$this->db->error);
return FALSE;
}
if($stmt->bind_param('isid',$this->workerID,$this->name,$this->subcell,$this->rate)===FALSE){
error_log("WCS/Workers.insert> bind_param:".$this->db->error);
return FALSE;
}
if($stmt->execute()===FALSE){
if($this->db->errno==1062){ // Duplicate.
return FALSE;
}
error_log("WCS/Workers.insert> execute:".$this->db->errno." ".$this->db->error);
return FALSE;
}
return TRUE;
}

public function delete(){
$stmt=$this->db->prepare("DELETE FROM Workers Where workerID=?");
if ($stmt===FALSE){
error_log("WCS/Workers.delete> stmt:" . $this->db->error);
return FALSE;
}
if($stmt->bind_param('i', $this->workerID)===FALSE){
error_log("WCS/Workers.delete> bind_param:".$this->db->error);
return FALSE;
}
if ($stmt->execute()===FALSE){
error_log("WCS/Workers.delete> execute:".$this->db->errno." ".$this->db->error);
return FALSE;
}
return TRUE;
}
public function getworkerID() {
$stmt=$this->db->prepare("SELECT DISTINCT workerID FROM Workers ORDER BY workerID");
if($stmt===FALSE){
die("prepare error ".$this->db->error);
}
if($stmt->bind_result($ID)===FALSE){
die("bind error ".$this->db->error);
}
if($stmt->execute()===FALSE){
die("execute error ".$this->db->error);
}
$workerID=array();
while($stmt->fetch()){
$workerID[]=$ID;

}
return $workerID;


}

public function getname() {
$stmt=$this->db->prepare("SELECT DISTINCT name FROM Workers ORDER BY name");
if($stmt===FALSE){
die("prepare error ".$this->db->error);
}
if($stmt->bind_result($name)===FALSE){
die("bind error ".$this->db->error);
}
if($stmt->execute()===FALSE){
die("execute error ".$this->db->error);
}
$names=array();
while($stmt->fetch()){
$names[]=$name;

}
return $names;


}

public function getsubcell() {
$stmt=$this->db->prepare("SELECT DISTINCT subcell FROM Workers ORDER BY subcell");
if($stmt===FALSE){
die("prepare error ".$this->db->error);
}
if($stmt->bind_result($subcell)===FALSE){
die("bind error ".$this->db->error);
}
if($stmt->execute()===FALSE){
die("execute error ".$this->db->error);
}
$subcells=array();
while($stmt->fetch()){
$subcells[]=$subcell;

}
return $subcells;


}
public function getrate() {
$stmt=$this->db->prepare("SELECT DISTINCT rate FROM Workers WHERE workerID=? AND subcell=?");
if($stmt===FALSE){
die("prepare error ".$this->db->error);
}
if($stmt->bind_param('ii',$this->workerID,$this->subcell)===FALSE){
die("bind error ".$this->db->error);
}
if($stmt->bind_result($rate)===FALSE){
die("bind error ".$this->db->error);
}
if($stmt->execute()===FALSE){
die("execute error ".$this->db->error);
}
$rates=array();
while($stmt->fetch()){
$rates[]=$rate;

}
return $rates;


}
}






?>

36 changes: 36 additions & 0 deletions App/ta2test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
// Worker Test Copyright 2014 by WebIS Spring 2014 License Apache 2.0
require_once 'Work-Cell-Scheduler/TDD/validator.php';
include 'Work-Cell-Scheduler/Config/local.php';
require_once 'ta2.php';

class WorkersTestCase extends WebIS\Validator {

protected static $__CLASS__=__CLASS__;

function testworkers(){
$w= new \WCS\Workers();
$this->assertTrue($w->SetID("102"));
$this->assertFalse($w->SetID("MARK"));
$this->assertFalse($w->SetName(" "));
$this->assertTrue($w->SetName("Mark Dintelman"));
$this->assertTrue($w->SetSubcell("10"));
$this->assertFalse($w->SetSubcell("Mark"));
$this->assertTrue($w->SetRate("9"));
$this->assertTrue($w->delete());
$this->assertTrue($w->insert());
$this->assertEquals(array("102","103",),$w->getworkerID());
$this->assertEquals(array("Mark Dintelman","Max Smith"),$w->getname());
$this->assertEquals(array("10","11"),$w->getsubcell());
$this->assertEquals(array("9","10"),$w->getrate());





}

}
if (!defined('PHPUnit_MAIN_METHOD')) {
WorkersTestCase::main();
}
Loading