Skip to content

Commit

Permalink
Merge pull request #225 from subjectsplus/agd-multidepartment
Browse files Browse the repository at this point in the history
staff member can belong to multiple departments, thanks Benton V
  • Loading branch information
agdarby committed Oct 14, 2015
2 parents ae99239 + 116a85a commit 9cdbc66
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 25 deletions.
35 changes: 26 additions & 9 deletions lib/SubjectsPlus/Control/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,30 @@ class Dropdown {
private $introselect;
private $show_keys;

public function __construct($dd_name, $optionsArray, $selected="", $truncation="", $introselect="", $show_keys = FALSE) {
public function __construct($dd_name, $optionsArray, $selected="", $truncation="", $introselect="", $show_keys = FALSE, $dropdown_type = 'single') {
$this->_dd_name = $dd_name;
$this->_optionsArray = $optionsArray;
$this->_selected = $selected;
$this->_truncation = $truncation;
$this->_introselect = $introselect;
$this->_show_keys = $show_keys;
$this->_dropdown_type = $dropdown_type;
}

public function display() {
$array_type = "";



if (is_array($this->_optionsArray[0])) {
$array_type = "multi";
}

$dd_text = "<select name=\"" . $this->_dd_name . "\">";
if ($this->_dropdown_type == "multi") {
$dd_text = '<select multiple name= "' . $this->_dd_name .'">'; // added benton
} else {
$dd_text = "<select name=\"" . $this->_dd_name . "\">";
}



// Add a first empty line if desired
if ($this->_introselect != "") {
Expand All @@ -53,6 +58,8 @@ public function display() {
if ($array_type == "multi") {
$our_selected = $value[0];
$label = $value[1];


} elseif ($this->_show_keys == TRUE) {
// we want to show both key and value
$our_selected = $key;
Expand All @@ -72,11 +79,21 @@ public function display() {
$dd_text .= "<option value=\"\" class=\"dropdown_list\"> -- " . strtoupper($sorter) . " -- </option>";
}

if ($our_selected === $this->_selected) {
$dd_text .= "\n<option value=\"$our_selected\" selected=\"selected\">$label</option>";
} else {
$dd_text .= "\n<option value=\"$our_selected\">$label</option>";
}
// check if our values are coming in a multidimensional array
if (is_array($this->_selected)) {
if (in_array($our_selected, $this->_selected)) {
$dd_text .= "<option value=\"$our_selected\" selected=\"selected\">$label</option>";
} else {
$dd_text .= "<option value=\"$our_selected\">$label</option>";
}
} else {
if ($our_selected === $this->_selected) {
$dd_text .= "<option value=\"$our_selected\" selected=\"selected\">$label</option>";
} else {
$dd_text .= "<option value=\"$our_selected\">$label</option>";
}
}


$current_type = $sorter;
}
Expand Down
108 changes: 99 additions & 9 deletions lib/SubjectsPlus/Control/Staff.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ class Staff {
//new sp4
private $_social_media;
private $_extra;
public $_ok_departments = array();

public function __construct($staff_id="", $flag="", $full_record = FALSE) {
//var_dump($_POST);

if ($flag == "" && $staff_id == "") {
$flag = "empty";
Expand All @@ -67,7 +69,7 @@ public function __construct($staff_id="", $flag="", $full_record = FALSE) {
$this->_fname = $_POST["fname"];
$this->_title = $_POST["title"];
$this->_tel = $_POST["tel"];
$this->_department_id = $_POST["department_id"];
//$this->_department_id = $_POST["department_id"];
$this->_staff_sort = $_POST["staff_sort"];
$this->_email = $_POST["email"];
$this->_user_type_id = $_POST["user_type_id"];
Expand Down Expand Up @@ -104,6 +106,10 @@ public function __construct($staff_id="", $flag="", $full_record = FALSE) {
//new sp4
$this->_social_media = $this->setSocialMediaDataPost();

// data stored in staff_department table
$this->_department_id = $_POST["department_id"]; // array
$this->_department_count = count($this->_department_id); // # of items in above array

if(isset($this->_extra)) {
$this->_extra = $_POST['extra'];
} else {
Expand All @@ -115,9 +121,11 @@ public function __construct($staff_id="", $flag="", $full_record = FALSE) {
break;
case "delete":
// kind of redundant, but just set up to delete appropriate tables?
// title_id only needed?
$this->_staff_id = $staff_id;

self::getAssociatedDepartments();

break;
case "forgot":
$this->_email = $_POST['email'];
Expand Down Expand Up @@ -181,7 +189,7 @@ public function __construct($staff_id="", $flag="", $full_record = FALSE) {
$this->_fullname = $this->_fname . " " . $this->_lname;
$this->_title = $staffArray[0]['title'];
$this->_tel = $staffArray[0]['tel'];
$this->_department_id = $staffArray[0]['department_id'];
//$this->_department_id = $staffArray[0]['department_id'];
$this->_staff_sort = $staffArray[0]['staff_sort'];
$this->_email = $staffArray[0]['email'];
$this->_ip = $staffArray[0]['ip'];
Expand Down Expand Up @@ -212,8 +220,19 @@ public function __construct($staff_id="", $flag="", $full_record = FALSE) {
$this->_lat_long = $staffArray[0]['lat_long'];

//new for sp4

$this->_social_media = $staffArray[0]['social_media'];
$this->_extra = $staffArray[0]['extra'];

///////////////////
// Query Departments table
// used to get our set of departments associated
// ////////////////

self::getAssociatedDepartments();
$this->_department_id = $this->_ok_departments;


}
}

Expand Down Expand Up @@ -242,7 +261,7 @@ public function outputForm($wintype="") {
$deptArray = $querierDept->query($qDept);

// create department dropdown
$deptMe = new Dropdown("department_id", $deptArray, $this->_department_id);
$deptMe = new Dropdown("department_id[]", $deptArray, $this->_department_id, "","","","multi");
$this->_departments = $deptMe->display();

///////////////
Expand Down Expand Up @@ -421,7 +440,7 @@ public function outputForm($wintype="") {

if ( $isDepartmentOmitted )
{
echo "<input type=\"hidden\" name=\"department_id\" id=\"department_id\" value=\"\" />";
echo "<input type=\"hidden\" name=\"department_id[]\" id=\"department_id\" value=\"\" />";
}else
{
echo "
Expand Down Expand Up @@ -912,7 +931,21 @@ public function deleteRecord() {

$this->_debug = "<p class=\"debug\">Delete from staff table(s) query: $q";

if ($delete_result != 0) {

if (isset($delete_result)) {
//delete department associations
$q2 = "DELETE FROM staff_department sd WHERE sd.staff_id = '" . $this->_staff_id . "'";
$delete_result2 = $db->exec($q2);

$this->_debug .= "<p>Del query 2: $q2";

} else {
// message
$this->_message = _("There was a problem with your delete (stage 1 of 2).");
return FALSE;
}

if (isset($delete_result2)) {

// /////////////////////
// Alter chchchanges table
Expand All @@ -921,7 +954,7 @@ public function deleteRecord() {

$updateChangeTable = changeMe("staff", "delete", $this->_staff_id, $this->_title, $_SESSION['staff_id']);

$this->_message = _("Thy will be done. Note that any subject guides associated with this user are now orphans.");
$this->_message = _("Thy will be done. Note that any subject guides associated with this user are now orphans. Pity the orphans.");
return false;
} else {
// message
Expand Down Expand Up @@ -968,7 +1001,7 @@ public function insertRecord() {
. $db->quote(scrubData($this->_lname)) . ","
. $db->quote(scrubData($this->_title)) . ","
. $db->quote(scrubData($this->_tel)) . ","
. $db->quote($this->_department_id) . ","
. $db->quote("") . ","
. $db->quote(scrubData($this->_staff_sort, "integer")) . ","
. $db->quote(scrubData($this->_email, "email")) . ","
. $db->quote(scrubData($this->_user_type_id, "integer")) . ","
Expand Down Expand Up @@ -1002,6 +1035,12 @@ public function insertRecord() {

$this->_staff_id = $db->last_id();

/////////////////////
// insert into staff_department
////////////////////

self::modifySD();

// create folder

if ($this->_staff_id) {
Expand Down Expand Up @@ -1088,12 +1127,13 @@ public function updateRecord() {
// NOTE: we don't update the password here; it's updated separately
/////////////////////

// oct 2015 removed department_id from this update, since it now has a table in v4 agd

$qUpStaff = "UPDATE staff SET
fname = " . $db->quote(scrubData($this->_fname)) . "," .
"lname = " . $db->quote(scrubData($this->_lname)) . "," .
"title = " . $db->quote(scrubData($this->_title)) . "," .
"tel = " . $db->quote(scrubData($this->_tel)) . "," .
"department_id = " . $department_id . "," .
"staff_sort = " . $db->quote(scrubData($this->_staff_sort, 'integer')) . "," .
"email = " . $db->quote(scrubData($this->_email, 'email')) . "," .
"user_type_id = " . $db->quote(scrubData($this->_user_type_id, 'integer')) . "," .
Expand Down Expand Up @@ -1123,6 +1163,22 @@ public function updateRecord() {
// echo $qUpStaff;
$rUpStaff = $db->exec($qUpStaff);

/////////////////////
// clear staff_department
/////////////////////

$qClearSD = "DELETE FROM staff_department WHERE staff_id = " . $this->_staff_id;

$rClearSD = $db->exec($qClearSD);

$this->_debug .= "<p>2. clear staff_department: $qClearSD</p>";

/////////////////////
// insert into staff_department
////////////////////

self::modifySD();

// /////////////////////
// Alter chchchanges table
// table, flag, item_id, title, staff_id
Expand Down Expand Up @@ -1279,7 +1335,41 @@ protected function setSocialMediaDataPost() {
return $data;
}

public function getAssociatedDepartments()
{

$db = new Querier();
$q2 = "SELECT d.department_id FROM department d, staff_department sd
WHERE d.department_id = sd.department_id AND sd.staff_id = " . $this->_staff_id;

$this->_departmenters = $db->query($q2);

foreach ($this->_departmenters as $value) {
$this->_ok_departments[] = $value[0];
}

$this->_debug .= "<p>Department query: $q2";
}

function modifySD()
{

$de_duped = array_unique($this->_department_id);

foreach ($de_duped as $value) {
if (is_numeric($value)) {
$db = new Querier;
$qUpSD = "INSERT INTO staff_department (staff_id, department_id) VALUES (
" . scrubData($this->_staff_id, 'integer') . ",
" . scrubData($value, 'integer') . ")";
$db = new Querier;
$rUpSD = $db->exec($qUpSD);

$this->_debug .= "<p>3. (insert staff_department loop) : $qUpSD</p>";

}
}
}

public function getMessage() {
return $this->_message;
Expand Down
13 changes: 7 additions & 6 deletions lib/SubjectsPlus/Control/StaffDisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ function writeTable($qualifier, $get_assoc_subs = 1, $print_display = 0) {

break;
case "By Department":
$q = "select distinct d.department_sort, staff.staff_sort, name, lname, fname, title, staff.tel, staff.email, d.department_id, d.telephone, staff.staff_id, staff.ptags
FROM department d, staff
WHERE d.department_id = staff.department_id
AND user_type_id = '1'
AND active = 1
order by department_sort, d.name, staff_sort desc, lname";
$q = "SELECT DISTINCT d.department_sort, s.staff_sort, name, lname, fname, title, s.tel, s.email, d.department_id, d.telephone, s.staff_id, s.ptags
FROM staff s, staff_department sd, department d
WHERE s.staff_id = sd.staff_id
AND sd.department_id = d.department_id
AND user_type_id = '1'
AND active =1
ORDER BY department_sort, d.name, staff_sort DESC, lname";

$db = new Querier;
$r = $db->query($q);
Expand Down
3 changes: 2 additions & 1 deletion subjects/staff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
* @date august, 2010
* @todo
*/

use SubjectsPlus\Control\Staff;
use SubjectsPlus\Control\StaffDisplay;
use SubjectsPlus\Control\CompleteMe;
use SubjectsPlus\Control\Querier;

include("../control/includes/config.php");
include("../control/includes/functions.php");
include("../control/includes/autoloader.php");
Expand Down

0 comments on commit 9cdbc66

Please sign in to comment.