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

create temp-table for org-assignements #5

Closed
wants to merge 2 commits into from
Closed
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
77 changes: 64 additions & 13 deletions Modules/OrgUnit/classes/class.ilObjOrgUnitTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
class ilObjOrgUnitTree {

/**
* @var null
*/
protected static $temporary_table_name = null;
/** @var ilObjOrgUnitTree */
private static $instance;
/** @var int[][] "employee" | "superior" => orgu ref id => role id */
Expand Down Expand Up @@ -316,11 +320,12 @@ public function getSuperiorsOfUser($user_id, $recursive = true) {
* @return int[]
*/
public function getLevelXOfUser($user_id, $level) {
$q = "SELECT orgu.obj_id, refr.ref_id FROM object_data orgu
INNER JOIN object_reference refr ON refr.obj_id = orgu.obj_id
INNER JOIN object_data roles ON roles.title LIKE CONCAT('il_orgu_superior_',refr.ref_id) OR roles.title LIKE CONCAT('il_orgu_employee_',refr.ref_id)
INNER JOIN rbac_ua rbac ON rbac.usr_id = " . $this->db->quote($user_id, "integer") . " AND roles.obj_id = rbac.rol_id
WHERE orgu.type = 'orgu' AND refr.deleted IS NULL";
$q = "SELECT object_reference.ref_id FROM rbac_ua
JOIN rbac_fa ON rbac_fa.rol_id = rbac_ua.rol_id
JOIN object_reference ON rbac_fa.parent = object_reference.ref_id
JOIN object_data ON object_data.obj_id = object_reference.obj_id
WHERE rbac_ua.usr_id = " . $this->db->quote($user_id, 'integer') . " AND object_data.type = 'orgu';";

$set = $this->db->query($q);
$orgu_ref_ids = array();
while ($res = $this->db->fetchAssoc($set)) {
Expand All @@ -347,17 +352,11 @@ public function getLevelXOfUser($user_id, $level) {
* @return int[]
*/
public function getOrgUnitOfUser($user_id, $ref_id = 0) {
$q = "SELECT orgu.obj_id, refr.ref_id FROM object_data orgu
INNER JOIN object_reference refr ON refr.obj_id = orgu.obj_id
INNER JOIN object_data roles ON roles.title LIKE CONCAT('il_orgu_superior_',refr.ref_id) OR roles.title LIKE CONCAT('il_orgu_employee_',refr.ref_id)
INNER JOIN rbac_ua rbac ON rbac.usr_id = " . $this->db->quote($user_id, "integer") . " AND roles.obj_id = rbac.rol_id
WHERE orgu.type = 'orgu' AND refr.deleted IS NULL";

/*$q = "SELECT object_reference.ref_id FROM rbac_ua
$q = "SELECT object_reference.ref_id FROM rbac_ua
JOIN rbac_fa ON rbac_fa.rol_id = rbac_ua.rol_id
JOIN object_reference ON rbac_fa.parent = object_reference.ref_id
JOIN object_data ON object_data.obj_id = object_reference.obj_id
WHERE rbac_ua.usr_id = " . $this->db->quote($user_id, 'integer') . " AND object_data.type = 'orgu';";*/
WHERE rbac_ua.usr_id = " . $this->db->quote($user_id, 'integer') . " AND object_data.type = 'orgu';";

$set = $this->db->query($q);
$orgu_ref_ids = array();
Expand All @@ -378,6 +377,58 @@ public function getOrgUnitOfUser($user_id, $ref_id = 0) {
}




/**
* Creates a temporary table with all orgu/user assignements. there will be three columns in the table orgu_usr_assignements (or specified table-name):
* ref_id: Reference-IDs of OrgUnits
* user_id: Assigned User-IDs
* path: Path-representation of the OrgUnit
*
* Usage:
* 1. Run ilObjOrgUnitTree::getInstance()->buildTempTableWithUsrAssignements(); in your code
* 2. use the table orgu_usr_assignements for your JOINS ans SELECTS
* 3. Run ilObjOrgUnitTree::getInstance()->dropTempTable(); to throw away the table
*
* @throws ilException
*
* @param string $temporary_table_name
*/
public function buildTempTableWithUsrAssignements($temporary_table_name = 'orgu_usr_assignements') {
if (self::$temporary_table_name === null) {
self::$temporary_table_name = $temporary_table_name;
} elseif ($temporary_table_name != self::$temporary_table_name) {
throw new ilException('there is already a temporary table for org-unit assignement: ' . self::$temporary_table_name);
}
$this->dropTempTable($temporary_table_name);
$q = "CREATE TEMPORARY TABLE IF NOT EXISTS " . $temporary_table_name . " AS (
SELECT object_reference.ref_id AS ref_id, rbac_ua.usr_id AS user_id, orgu_path_storage.path AS path
FROM rbac_ua
JOIN rbac_fa ON rbac_fa.rol_id = rbac_ua.rol_id
JOIN object_reference ON rbac_fa.parent = object_reference.ref_id
JOIN object_data ON object_data.obj_id = object_reference.obj_id
JOIN orgu_path_storage ON orgu_path_storage.ref_id = object_reference.ref_id
WHERE object_data.type = 'orgu' AND object_reference.deleted IS NULL
);";
$this->db->manipulate($q);
}


/**
* @param $temporary_table_name
* @return bool
*/
public function dropTempTable($temporary_table_name) {
if (self::$temporary_table_name === null || $temporary_table_name != self::$temporary_table_name) {
return false;
}
$q = "DROP TABLE IF EXISTS " . $temporary_table_name;
$this->db->manipulate($q);

return true;
}


public function getTitles($org_refs) {
$names = array();
foreach ($org_refs as $org_unit) {
Expand Down