File manager - Edit - /home/missmand/public_html/learning/old/main/couple/model.php
Back
<?php /* For licensing terms, see /license.txt */ /** * Model CRUD for hp_startup table, which links HP and STARTUPS for cooperation purposes * @author Batiste Roger <batiste.roger@live.fr> * @package chamilo.couple */ /* Table columns * id --- * hp_id id of an hp in table hp * stratup_id same in strartup * couple_type type of the couple. Can be 0 (DS) or 1 (CE) */ // Require subrole functions require_once(api_get_path(SYS_PATH) . 'main/subrole/subrole.php'); require_once(api_get_path(SYS_PATH) . 'main/couple/error.php'); require_once(api_get_path(SYS_PATH) . 'main/couple/notification.php'); // Couple Types define ('DS', 'DS'); define ('CE', 'CE'); define ('TU', 'TU'); /** * Checks that this couple_type is valid. True is valid. * @param string $type * @return ($type == DS || $type == CE || $type == TU) */ function couple_type_is_valid ($type) { return ($type == DS || $type == CE || $type == TU); } /** * Get table name for this couple type * @param string $type * @return string table_name */ function couple_type_to_table ($type) { assert(couple_type_is_valid($type)); return 'couple_' . strtolower($type); } /** * Shortcut for coop_gt_partner if you only need the id * @param type $user_id * @param type $couple_type * @return type */ function couple_get_partner_id ($user_id, $couple_type) { $tmp = couple_get_partner($user_id, $couple_type); return $tmp['user_id']; } /** Get your partner's info * @param int $user_id my user id. I can be a hp, a su,a tutor. Whatever. * @param int $couple_type one of the possible couple type (DS, CE, TU) defined in model.php * @return array something like {user_id : 12, couple_id : 2} */ function couple_get_partner ($user_id, $couple_type) { assert(UserManager::is_user_id_valid($user_id)); assert(couple_type_is_valid($couple_type)); if ($couple_type == TU) { return couple_get_tutor($user_id); } else { return couple_get_coop_partner($user_id); } } function couple_get_coop_partner_aux($user_id, $couple_type) { assert(UserManager::is_user_id_valid($user_id)); // Determin type $table_couple = couple_type_to_table($couple_type); // Get useful info / user_id $my_subrole = get_subrole($user_id); if ($my_subrole == ADMIN) { return null; } $my_subrole_id = user_id_to_subrole_id($user_id, $my_subrole); $i_am_hp = user_has_subrole($user_id, HP); $my_subrole_prefix = ($i_am_hp ? 'hp' : 'su'); // echo "<b>id?$user_id subrole?$my_subrole subid?$my_subrole_id prefix?$my_subrole_prefix</b><br/>"; // Partner (tu / su) $partner_subrole = ($i_am_hp ? STARTUP : HP ); $partner_prefix = ($i_am_hp ? 'su' : 'hp'); $table_partner = subrole_to_table($partner_subrole); // SQL $sql = "SELECT *, couple.id as couple_id "; $sql .= "FROM $table_couple couple "; $sql .= "JOIN $table_partner partner "; $sql .= "ON partner.id = couple." . $partner_prefix . "_id "; $sql .= "WHERE couple." . $my_subrole_prefix . "_id = $my_subrole_id "; $result = Database::query($sql); if (Database::num_rows($result) > 0) { $row = Database::fetch_array($result, 'ASSOC'); $row['type'] = strtolower($couple_type); $row['hp_id'] = ($i_am_hp ? $user_id : $row['user_id']); $row['pa_id'] = ($i_am_hp ? $row['user_id'] : $user_id); $row['info'] = api_get_user_info($row['user_id']); return $row; } } function couple_get_with ($tu_user_id, $student_user_id) { $tutor_subrole_id = user_id_to_subrole_id($tu_user_id, TUTOR); $student_subrole = get_subrole($student_user_id); $student_subrole_id = user_id_to_subrole_id($student_user_id, $student_subrole); $student_prefix = ($student_subrole == HP ? 'hp' : 'su'); $sql = "SELECT * FROM couple_tu WHERE tu_id = '$tutor_subrole_id' AND user_id = '$student_subrole_id' AND user_type = '$student_prefix'"; $result = Database::query($sql); if (Database::num_rows($result) > 0) { $row = Database::fetch_array($result, 'ASSOC'); $row[$student_prefix . '_id'] = $student_user_id; $row['student_user_id'] = $student_user_id; $row['tu_user_id'] = $tu_user_id; $row['couple_id'] = $row['id']; $row['type'] = 'tu'; $row['info'] = api_get_user_info($student_user_id); return $row; } } function couple_get_from_id ($couple_id, $couple_type) { $table = couple_type_to_table($couple_type); $sql = "SELECT * FROM $table WHERE id = '$couple_id'"; $result = Database::query($sql); if (Database::num_rows($result) > 0) { $row = Database::fetch_array($result, 'ASSOC'); $row['couple_id'] = $couple_id; $row['type'] = $couple_type; return $row; } } /** * Find the tutor of a given user (user_id) * @param int $user_id * @return array tutor */ function couple_get_tutor($user_id) { // Get useful info / user_id $my_subrole = get_subrole($user_id); if ($my_subrole == ADMIN) { return null; } $my_subrole_id = user_id_to_subrole_id($user_id, $my_subrole); $subrole = get_subrole($user_id); $my_subrole_prefix = ($subrole == HP ? 'hp' : 'su'); // SQL $sql = "SELECT *, id as couple_id "; $sql .= "FROM couple_tu "; $sql .= "WHERE user_id = $my_subrole_id AND user_type = '$my_subrole_prefix'"; $result = Database::query($sql); if (Database::num_rows($result) > 0) { $row = Database::fetch_array($result, 'ASSOC'); $tutor_subrole_id = subrole_id_to_user_id($row['tu_id'], TUTOR); $row['type'] = 'tu'; $row['student_user_id'] = subrole_id_to_user_id($row['user_id'], $row['user_type'] == 'hp' ? HP : STARTUP); $row[$my_subrole_prefix . '_id'] = $user_id; $row['pa_id'] = $tutor_subrole_id; $row['tu_id'] = $tutor_subrole_id; $row['tu_user_id'] = $row['tu_id']; $row['info'] = api_get_user_info($tutor_subrole_id); return $row; } else { // Assign a tutor for this user and call the function again error_log('Warning : no tutor assigned to ' + $my_subrole_prefix + ' ' + $my_subrole_id); couple_set_tutor (couple_get_tutor_default_id(), $my_subrole_id, $my_subrole_prefix); return couple_get_tutor($user_id); } } /** * Set tutor_id for startup to the tu_id of its partner (the hp) * After this method is called, both su and hp will share the same tutor * @param int $su_user_id */ function couple_merge_tutor ($su_user_id) { // Make sure // This is a startup // It has a coop // Who has a tutor if (user_has_subrole($su_user_id, STARTUP)) { $su_id = user_id_to_subrole_id($su_user_id, STARTUP); $partner = couple_get_coop_partner($su_user_id); if (!empty($partner)) { $partner_user_id = $partner['user_id']; $tutor = couple_get_tutor($partner_user_id); $tutor_id = $tutor['tu_id']; // Make sure the already is such a row in the table $sql = "SELECT * FROM couple_tu WHERE user_id = '$su_user_id' AND user_type = 'su'"; if (Database::num_rows(Database::query($sql)) == 0) { $sql = "INSERT INTO couple_tu SET tu_id = '$tutor_id', user_id = '$su_id', user_type = 'su'"; Database::query($sql); } else { $sql = "UPDATE couple_tu SET tu_id = '$tutor_id' WHERE user_id = '$su_id' AND user_type = 'su'"; Database::query($sql); } } } } function couple_set_tutor ($tu_user_id, $user_id, $user_type) { $tu_id = user_id_to_subrole_id($tu_user_id, TUTOR); $sql = "SELECT * FROM couple_tu WHERE user_id = '$user_id' AND user_type = '$user_type'"; if (Database::num_rows(Database::query($sql)) == 0) { $sql = "INSERT INTO couple_tu SET tu_id = '$tu_id', user_id = '$user_id', user_type = '$user_type'"; Database::query($sql); } else { error_throw("Couple already exists, user $user_id, type $user_type", 'model.php:couple_get_tutor'); } } function couple_get_tutor_default_id() { $sql = "SELECT user_id FROM tutor"; $res = db_query($sql); return $res[0]['user_id']; } /** * Shortcut to getting the right coop type. * CE is tested first. * @return coop_get_partner ($user_id, (DE ou CE selon)) */ function couple_get_coop_partner ($user_id = null) { if (empty($user_id)) $user_id = api_get_user_id(); assert(UserManager::is_user_id_valid($user_id)); $ce = couple_get_coop_partner_aux($user_id, CE); if (empty($ce)) { $ds = couple_get_coop_partner_aux($user_id, DS); return $ds; } return $ce; } /** * Read all couples in all couple_xx tables * @return array : {{hp_id, su_id, type}, {hp_id, su_id, type}, ...} */ function couple_get_all($show_names = false) { $types = array(TU, CE, DS); $couples = array(); foreach ($types as $type) { couple_get_by_type ($type, &$couples, $show_names = false); } return $couples; } function couple_get_by_type ($type, &$couples, $show_names_ce = false) { $table = couple_type_to_table($type); $sql = "SELECT * FROM $table"; $res = Database::query($sql); if (Database::num_rows($res) > 0) { while ($row = Database::fetch_array($res, 'ASSOC')) { if (isset($row['hp_id'])) { $row['hp_id'] = subrole_id_to_user_id($row['hp_id'], HP); } else { $row['user_id'] = subrole_id_to_user_id($row['user_id'], ($row['user_type'] == 'hp' ? HP : STARTUP)); } if ($type != TU) { $row['pa_id'] = subrole_id_to_user_id($row['su_id'], STARTUP); } else { $row['pa_id'] = subrole_id_to_user_id($row['tu_id'], TUTOR); } if ($show_names_ce) { if ($type != CE) error_throw ("Type CE expected when 'show_names_ce'", "model.php : couple_get_by_type"); $hp_info = UserManager::get_user_info_by_id($row['hp_id']); $su_info = UserManager::get_user_info_by_id($row['pa_id']); $row['hp_firstname'] = $hp_info['firstname']; $row['hp_lastname'] = $hp_info['lastname']; $row['su_firstname'] = $su_info['firstname']; $row['su_lastname'] = $su_info['lastname']; } $row['type'] = $type; array_push($couples, $row); } } return $couples; // Only used as shortcut when not called from couple_get_all } /** * Create an entry in couple_xx * @param type $couple_type xx = TU / CE / DS * @param type $hp_user_id * @param type $partner_user_id */ function couple_create ($couple_type_string, $hp_user_id, $partner_user_id) { $couple_type = strtoupper($couple_type_string); // Test that these user_ids exist try { error_test_param (UserManager::is_user_id_valid($hp_user_id), "User id (hp) $hp_user_id does not exist"); error_test_param (UserManager::is_user_id_valid($partner_user_id), "User id (pa) $partner_user_id does not exist"); } catch (Exception $e) { return false; } // Test that the couple type is valid try { error_test_param (couple_type_is_valid($couple_type), "Couple type $couple_type is not valid"); } catch (Exception $e) { return false; } try { // Test that they have that subroles error_test_param (user_has_subrole($hp_user_id, HP), "User $hp_user_id is not a " . HP); $partner_subrole = ($couple_type == TU ? TUTOR : STARTUP); error_test_param (user_has_subrole($partner_user_id, $partner_subrole), "User $hp_user_id is not a $partner_subrole"); } catch (Exception $e) { return null; } // Test that this creation won't generate a double-coop or double-tutor for the hp / su $hp_subrole_id = user_id_to_subrole_id($hp_user_id, HP); if ($couple_type == TU) { $table = couple_type_to_table($couple_type); $sql = "SELECT * FROM $table WHERE hp_id = $hp_subrole_id"; $res = Database::query($sql); error_test_param (Database::num_rows($res) == 0, "Such a couple already exist"); } else { foreach (array(DS, CE) as $type) { $partner_prefix = ($couple_type == TU ? 'tu' : 'su'); $partner_subrole_id = user_id_to_subrole_id($partner_user_id, $partner_subrole); $table = couple_type_to_table($type); $sql_hp = "SELECT * FROM $table WHERE hp_id = $hp_subrole_id"; $sql_ps = "SELECT * FROM $table WHERE " . $partner_prefix . "_id = $partner_subrole_id"; foreach (array($sql_hp, $sql_ps) as $sql_query) { $res = db_query($sql_query, false); error_test_param (Database::num_rows($res) == 0, "Such a couple already exist : $sql_query"); } } } // Call unsafe method return array(couple_create_unsafe($couple_type, $hp_user_id, $partner_user_id)); } function couple_create_unsafe ($couple_type, $hp_user_id, $partner_user_id) { // Get variables $table = couple_type_to_table($couple_type); $hp_subrole_id = user_id_to_subrole_id($hp_user_id, HP); $partner_subrole = ($couple_type == TU ? TUTOR : STARTUP); $partner_prefix = ($couple_type == TU ? 'tu' : 'su'); $partner_subrole_id = user_id_to_subrole_id($partner_user_id, $partner_subrole); $sql = "INSERT INTO $table SET hp_id = $hp_subrole_id, " . $partner_prefix . "_id = $partner_subrole_id"; Database::query($sql); $info_pa = UserManager::get_user_info_by_id($partner_user_id); $info_hp = UserManager::get_user_info_by_id($hp_user_id); // Send notifications (TODO : TEXT) // TO HP notification_create( $hp_user_id, "Votre Mission Startup commence!", "Vous travaillez désormais avec " . $info_pa['firstname'] . " " . $info_pa['lastname'] . " de la société \"" . $info_pa['company'] . "\". L'aventure commence dans la section 'Coopération'!", api_get_path(WEB_PATH) . 'main/cooperation/index.php'); // TO STARTUP if ($partner_subrole == STARTUP) notification_create( $partner_user_id, "Votre Coopération commence!", "Vous travaillez désormais avec " . $info_hp['firstname'] . " " . $info_hp['lastname'] . ". L'aventure commence dans la section \"Coopération\"!", api_get_path(WEB_PATH) . 'main/cooperation/index.php'); return "Couple created : $sql"; } // Delete any couple with this user_id and with same type function couple_delete ($couple_id, $couple_type) { error_log("couple_delete : $couple_id, $couple_type"); error_test_param(couple_type_is_valid($couple_type), "Invalid couple type in couple_delete"); $table = couple_type_to_table($couple_type); $safe_id = Database::escape_string(trim($couple_id)); $sql = "DELETE FROM $table WHERE id = '$safe_id';"; db_query($sql, false); } /** * Tests that the given couple_id exists * @param type $couple_id * @param type $type * @return boolean */ function couple_id_is_valid ($couple_id, $type) { // Reject empty params if (empty($couple_id) || empty($type)) return false; // Reject wrong types if (!couple_type_is_valid($type)) return false; $table = couple_type_to_table($type); $id = intval($couple_id); $sql = "SELECT id FROM $table WHERE id = '$id'"; return (Database::num_rows(Database::query($sql)) > 0); } function couple_get ($couple_id, $type) { // Reject empty params if (empty($couple_id) || empty($type)) return false; // Reject wrong types if (!couple_type_is_valid($type)) return false; $table = couple_type_to_table($type); $sql = "SELECT * FROM $table WHERE id = '$couple_id'"; return Database::fetch_assoc(Database::query($sql)); } function couple_get_hp ($couple_id, $type) { $couple = couple_get($couple_id, $type); if ($couple) return $couple['hp_id']; } function couple_get_firstname_of($user_id) { $info = UserManager::get_user_info_by_id($user_id); return $info['firstname']; } function couple_get_firstnames ($couple_id, $type) { $hp_id = couple_get_hp ($couple_id, $type); $hp_user_id = subrole_id_to_user_id($hp_id, HP); $pa = couple_get_partner ($hp_user_id, $type); $pa_user_id = $pa['user_id']; return array('hp' => couple_get_firstname_of($hp_user_id), 'pa' => couple_get_firstname_of($pa_user_id)); } function couple_tutor_get_coops ($user_id) { // Read all couples / HP.tutor.id = $user_id $ce = couple_tutor_get_coop($user_id, CE); $ds = couple_tutor_get_coop($user_id, DS); return array_merge($ce, $ds); } /** Don't use this outside from get_coops * Params are unchecked * @param type $user_id * @param type $type */ function couple_tutor_get_coop ($user_id, $type) { $tutor_id = user_id_to_subrole_id($user_id, TUTOR); $table = couple_type_to_table($type); $sql = "SELECT ctu.user_id as hp_id, cce.su_id as su_id, ctu.tu_id as tu_id, cce.id as couple_id " . "FROM $table cce " . "JOIN couple_tu ctu ON cce.hp_id = ctu.user_id " . "WHERE ctu.user_type = 'hp' AND ctu.tu_id = '$tutor_id'"; $result = Database::query($sql); $coops_array = array(); while ($row = Database::fetch_assoc($result)) { // Some useful 'artificial' columns are added $row['couple_type'] = $type; $row['hp_info'] = UserManager::get_user_info_by_id(subrole_id_to_user_id($row['hp_id'], HP)); $row['su_info'] = UserManager::get_user_info_by_id(subrole_id_to_user_id($row['su_id'], STARTUP)); $coops_array[] = $row; } return $coops_array; } function couple_tutor_get_students ($user_id) { $tu_id = user_id_to_subrole_id($user_id, TUTOR); $sql = "SELECT * FROM couple_tu WHERE tu_id = '$tu_id'"; $result = Database::query($sql); $students_array = array(); while ($row = Database::fetch_assoc($result)) { $subrole = $row['user_type'] == 'hp' ? HP : STARTUP; $row['name'] = subrole_id_to_user_fullname($row['user_id'], $subrole); $row['user_id'] = subrole_id_to_user_id($row['user_id'], $subrole); // $row['info'] = print_r(UserManager::get_user_info_by_id($row['user_id']), true); $students_array[] = $row; } return $students_array; } function couple_get_unmatched ($type = HP) { $table = subrole_to_table($type); $prefix = $type == HP ? 'hp' : 'su'; $prefix_id = $prefix . '_id'; $ce = couple_type_to_table(CE); $ds = couple_type_to_table(DS); $sql = "SELECT * " . "FROM $table " . "JOIN user ON $table.user_id = user.user_id " . "WHERE " . "NOT EXISTS (SELECT * FROM $ce WHERE $ce.$prefix_id = $table.id) " . "AND " . "NOT EXISTS (SELECT * FROM $ds WHERE $ds.$prefix_id = $table.id);"; $result = db_query($sql); // var_dump($result); exit; return $result; } function couple_includes_user($couple_id, $couple_type, $user_id) { $user_subrole = get_subrole($user_id); $subrole_id = user_id_to_subrole_id($user_id, $user_subrole); $couple = couple_get_from_id($couple_id, $couple_type); if ($couple_type == TU) { if ($user_subrole == TUTOR) { return $couple['tu_id'] == $subrole_id; } else { return $couple['user_id'] == $subrole_id; } } else { $subrole_prefix = subrole_to_prefix($user_subrole); return $couple[$subrole_prefix . '_id'] == $subrole_id || $user_subrole == TUTOR; // TODO $user_subrole == TUTOR fast fix, returns true too often } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings