Hacked By AnonymousFox
creation.php 0000644 00000004300 15200264307 0007055 0 ustar 00 <?php
/* For licensing terms, see /license.txt */
/**
* This file contains methods to create users with subroles and to register these users to the courses.
* @author Batiste Roger <batiste.roger@live.fr>
* @package chamilo.cooperation
*/
// BAT /BAT
// How to call this file : require_once(api_get_path(SYS_PATH) . 'main/subrole/creation.php');
/*
* High level explanation on rights :
* - STARTUPs don't have access to courses
* - HPs have access to all the courses. They are automatically registered for @ the courses.
* - TUTORs are teachers for all the courses. They are automatically registered for @ of them.
*
* Consequently, we have to register new HPs and TUTORs to @ courses.
* Consequently, when a course is created, we have to register @HPs and TUTORs to it.
*
* This is why we need this file.
*
*/
/* ***************************************************
* User creation
* **************************************************/
/**
* Creation of a HP or TUTOR
* - Register in TABLE_MAIN_HP or TABLE_MAIN_TUTOR
* - for @ course, subscribe user
* @param int User ID
* @param string subrole (in HP, TUTOR, STARTUP)
*/
function add_user_as_subrole($userid, $subrole) {
// Set this userid in the table
set_user_subrole($userid, $subrole);
// Nothing to do with startups
if ($subrole == STARTUP) return;
// Subscribe to @ courses if HP or Tutor
$TABLECOURS = Database::get_main_table(TABLE_MAIN_COURSE);
$sql = "SELECT * FROM $TABLECOURS";
$result = Database::query($sql);
for ($i = 0; $i < Database::num_rows($result); $i++) {
$course_codes = Database::fetch_array($result, 'ASSOC');
$code = $course_codes['code'];
if ($subrole == TUTOR) {
CourseManager::add_user_to_course($userid, $code, COURSEMANAGER);
} else {
CourseManager::add_user_to_course($userid, $code);
}
}
}
/* ***************************************************
* Course creation
* **************************************************/
/**
* Creation of a Course
* - for @ hp, subscribe to this course
* - for @ tutor, subscribe to this course
* @param int Course ID ???
*/ admin.php 0000644 00000012542 15200264307 0006350 0 ustar 00 <?php
/* For licensing terms, see /license.txt */
/**
* CR(U)D for subroles. This is admin only feature.
* It was not placed in admin folder in order to loosely couple /subrole to Chamilo.
* @author Batiste Roger <batiste.roger@live.fr>
* @package chamilo.cooperation
*/
// ** Strange stuff
require_once '../inc/global.inc.php';
global $_configuration;
$current_access_url_id = api_get_current_access_url_id();
$this_section = SECTION_PLATFORM_ADMIN;
api_protect_admin_script(true);
// ********************************
// *** Test that I am an admin ****
// ********************************
if (!api_is_platform_admin()) {
// You should not be there
echo "You are not allowed to view this page.";
exit;
}
// ********************************
// *** Test and perform action ****
// ********************************
// Test
$valid_actions = array('create', 'delete');
if (isset($_GET["action"]) && in_array($_GET["action"], $valid_actions)) {
$action = $_GET["action"];
} else {
$action = null;
}
// Perform
$hp = Security::remove_XSS($_GET['hp']);
$su = Security::remove_XSS($_GET['su']);
$type = Security::remove_XSS($_GET['type']);
require_once(api_get_path(SYS_PATH) . 'main/cooperation/couple_model.php');
switch ($action) {
case 'create' :
couple_create($hp, $su, $type, USER);
break;
case 'delete' :
couple_delete($hp, $su, $type);
break;
}
// ********************************
// *** List @ couples (DB) ********
// ********************************
function get_number_of_couples() {
return Database::num_rows(couple_read());
}
function get_couple_data_as_array() {
$result = couple_read();
$couples = array();
for ($i = 0; $i < get_number_of_couples(); $i++) {
$row = Database::fetch_array($result, 'ASSOC');
$couples[$i] = $row;
require_once(api_get_path(SYS_PATH) . 'main/subrole/subrole.php');
$couples[$i]['hp_name'] = subrole_id_to_user_fullname($row['hp_id'], HP);
$couples[$i]['su_name'] = subrole_id_to_user_fullname($row['startup_id'], STARTUP);
}
return $couples;
}
// ********************************
// *** Create table to display ****
// ********************************
$couples = get_couple_data_as_array();
$table = '<table class="data_table"><tbody>';
$table .= '<tr><th>HP</th><th>SU</th><th>Type</th><th>Action</th><tr>';
foreach ($couples as $couple) {
$hp = $couple['hp_id'];
$hp_name = $couple['hp_name'] . " ($hp)";
$su = $couple['startup_id'];
$su_name = $couple['su_name'] . " ($su)";
$type = $couple['couple_type'];
$type_name = coop_type($couple['couple_type']);
$delete_link = "<a href='admin.php?action=delete&hp=$hp&su=$su&type=$type'>delete</a>";
$table .= "<tr><th>$hp_name</th><th>$su_name</th><th>$type_name</th><th>$delete_link</th><tr>";
}
$table .= '</tbody></table>';
// ********************************
// *** Create creation form *******
// ********************************
function list_subrole_X_by_name ($subrole) {
// Tests for debug
assert(in_array($subrole, allowed_subroles()));
// Call
$table = subrole_to_table($subrole);
assert(!empty($table));
$sql = "SELECT id, user_id FROM $table";
$result = Database::query($sql);
$users = array();
for ($i = 0; $i < Database::num_rows($result); $i++) {
$row = Database::fetch_array($result, 'ASSOC');
$info = api_get_user_info($row['user_id']);
$users[$i]['user_id'] = $row['user_id'];
$users[$i]['name'] = api_get_person_name($info['firstname'], $info['lastname']);
}
return $users;
}
function create_select ($type = HP) {
$subrole = ($type == HP ? 'hp' : 'su');
$form = '<select name="' . $subrole . '" id="' . $subrole . '">';
$list_hp = list_subrole_X_by_name($type);
$first = true;
foreach ($list_hp as $row) {
$id = $row['user_id'];
$name = $row['name'];
if (!empty($name)) {
if ($first) {
$form .= '<option selected value="' . $id . '">' . $name . '</option>';
$first = false;
} else {
$form .= '<option value="' . $id . '">' . $name . '</option>';
}
}
}
$form .= '</select>';
return $form;
}
function create_radio ($type = DS) {
assert($type == DS || $type == CE);
$selected = ($type == CE ? 'checked' : null);
return '<input ' . $selected . ' type="radio" name="type" value="' . $type . '" id="' . coop_type($type) . '" />' . coop_type($type, false) . " ($type) <br />";
}
// For subroles to be understood
require_once(api_get_path(SYS_PATH) . 'main/subrole/subrole.php');
$form = "<form action='admin.php' method='get'>";
$form .= "<p>";
$form .= create_select(HP);
$form .= "</p>";
$form .= "<p>";
$form .= create_select(STARTUP);
$form .= "</p>";
$form .= "<p>";
$form .= create_radio(DS);
$form .= create_radio(CE);
$form .= "</p>";
$form .= "<input type='submit' name='action' value='create'/>";
$form .= "</form>";
// ********************************
// *** Display @ ******************
// ********************************
$tpl = new Template('Couples');
// $tpl->assign('actions', $actions);
// $tpl->assign('message', $message);
$table_title = '<h3>List</h3>';
$form_title = '<h3>Create</h3>';
$content = $table_title . $table . $form_title . $form;
$tpl->assign('content', $content);
$tpl->display_one_col_template(); form.php 0000644 00000002565 15200264307 0006227 0 ustar 00 <?php
function create_subrole_choice ($form) {
require_once(api_get_path(SYS_PATH) . 'main/subrole/subrole.php');
// Get array with all roles
$subroles = allowed_subroles();
array_push($subroles, ADMIN);
// Create array with nice ucfirst names
$subrole_names = allowed_subroles_names();
// Add element to form
$form->addElement('select', 'subrole', 'Subrole', $subrole_names,
array('id' => 'subrole_select', 'class'=>'chzn-select', 'onchange' => 'javascript: display_drh_list();'));
}
function get_subrole_info($form) {
$subroles = allowed_subroles(true);
$subrole_code = $form->getSubmitValue('subrole'); // Value in forme : 0, 1, 2, ...
if (!isset($subroles[$subrole_code])) $subrole_code = 0; // If value is wrong, set to first allowed value
$subrole_val = $subroles[$subrole_code]; // Value as string : 'tutor', ...
$status = status_for_subrole($subrole_val);
$is_admin = ($subrole_code == ADMIN);
$info = array('code' => $subrole_code, 'value' => $subrole_val, 'status' => $status, 'is_admin' => $is_admin);
// var_dump($info);
return $info;
}
function subrole_to_code ($role) {
$subroles = allowed_subroles(true);
$key = array_keys($subroles, $role);
return $key[0];
}
function code_to_subrole ($code) {
$subroles = allowed_subroles(true);
return $subroles[$code];
} subrole.php 0000644 00000026422 15200264307 0006735 0 ustar 00 <?php
/* For licensing terms, see /license.txt */
/**
* This file contains methods to manipulate subroles, ie startup, hp and tutor.
* @author Batiste Roger <batiste.roger@live.fr>
* @package chamilo.cooperation
*/
// BAT /BAT
// How to call this file : require_once(api_get_path(SYS_PATH) . 'main/subrole/subrole.php');
/* ***************************************************
* Utils methods to access roles
* **************************************************/
/* Lists all subroles */
define("STARTUP", 'startup');
define("HP", 'hp');
define("TUTOR", 'tutor');
define("RH", 'rh');
define("ADMIN", 'admin'); // admins are not managed in this file
require_once(api_get_path(SYS_PATH) . 'main/couple/error.php');
/**
* Returns array with subroles
* @param bool $include_admin (default false) determins if admin should be considered as a subrole
* @return array(string)
*/
function allowed_subroles($include_admin = false) {
if ($include_admin) {
return array(STARTUP, HP, TUTOR, RH, ADMIN);
} else {
return array(STARTUP, HP, TUTOR, RH);
}
}
/* To display subroles */
function allowed_subroles_names() {
$names = array();
foreach (allowed_subroles(true) as $subrole) {
array_push($names, ucfirst($subrole));
}
return $names;
}
/**
* Returns that table corresponding to this subrole
* @param type $subrole
* @return string|null
*/
function subrole_to_table ($subrole) {
$tables = array(TUTOR => TABLE_MAIN_TUTOR, STARTUP => TABLE_MAIN_STARTUP, HP => TABLE_MAIN_HP, RH => TABLE_MAIN_RH);
if (in_array($subrole, $tables)) {
return $tables[$subrole];
}
// wrong role
error_throw('subrole is not valid, cant convert to table (' . $subrole . ')', 'subrole.php:subrole_to_table');
}
/**
* Returns the prefix for this subrole
* WARNING : assumes that subrole is safe
* @param type $subrole
* @return string
*/
function subrole_to_prefix ($subrole) {
switch($subrole) {
case STARTUP : return 'su'; break;
case HP : return 'hp'; break;
case TUTOR : return 'tu'; break;
case RH : return 'rh'; break;
case ADMIN : return 'admin'; break;
}
}
/* ***************************************************
* Test type for given user_id
* **************************************************/
/**
* Return a valid id, current user if user_id is null
* Utilisation : $valide_user_id = check_user_id($user_id);
* @param int User ID. If none provided, will use current user.
*/
function check_user_id($user_id = null, $die_if_null = false)
{
$user_id = intval($user_id);
if (empty($user_id)) {
if ($die_if_null) {
error_throw ("user_id is empty => DIE", "subrole.php:check_user_id");
} else {
$user_id = api_get_user_id();
}
}
// Make sure this id is valid
error_test_param(UserManager::is_user_id_valid($user_id), "subrole.php:check_user_id user_id $user_id is not valid");
return $user_id;
}
/**
* Private function to avoid repeating the same code over and over.
* @param int User ID. If none provided, will use current user.
* @param constant TableName. Example: TABLE_MAIN_ADMIN
*/
function check_subrole($userid = null, $subrole_table = null)
{
// Get a valid user id
$user_id = check_user_id($userid);
// Call the DB
$sql = "SELECT * FROM $subrole_table WHERE user_id = $user_id";
$res = Database::query($sql);
// Check that there is one and only one row with this id
$is_admin = (Database::num_rows($res) === 1);
return $is_admin;
}
/**
* Returns user's subrole if any, or null if there is no subrole
* @param int User ID. If none provided, will use current user.
* @return specified user's subrole, or null if he has no subrole.
*/
function get_subrole($user_id = null) {
// Test for subroles
foreach (allowed_subroles() as $role) {
if (user_has_subrole($user_id, $role)) {
return $role;
}
}
// Test for admin
if (UserManager::is_admin($user_id)) {
return ADMIN;
}
// Else, nothing
error_throw("User has no subrole :(");
}
/**
* Reminder for admin tests
* => api_is_platform_admin_by_id($user_id);
*/
/**
* Checks whether the user given as user id is in the $role table.
* @param int User ID. If none provided, will use current user.
* @return true if has subrole, else false
*/
function user_has_subrole ($user_id = null, $subrole = null) {
$user_id = check_user_id($user_id);
$allowed_subrole = allowed_subroles();
if (empty($subrole)) {
// $role is null, test for any role
foreach ($allowed_subrole as $possible_role) {
if (user_has_subrole($user_id, $possible_role)) {
return true;
}
}
return false;
} else if (!in_array($subrole, $allowed_subrole)) {
// $subrole is wrong, return false
if ($subrole != ADMIN) {
error_throw('Wrong role in subrole.php>user_has_subrole,' . " user_id : $user_id and subrole = $subrole");
} else {
return true;
}
} else {
// This is a correct role value
return check_subrole($user_id, subrole_to_table($subrole));
}
}
function user_can_coop ($user_id = null) {
$subrole = get_subrole($user_id);
return ($subrole == HP || $subrole == STARTUP);
}
function subrole_for_partner ($subrole) {
assert(($subrole == HP || $subrole == STARTUP));
return ($subrole == HP ? STARTUP : HP);
}
/* ***************************************************
* Set type for give user_id
* **************************************************/
/**
* Unset role for given user.
* @param int User ID. If none provided, will use current user.
* @param subtype constant.
*/
function unset_user_subrole($userid = null, $subrole = null) {
// Check user id
$user_id = check_user_id($userid);
// If no subrole is specified, unset all of them
if (empty($subrole)) {
foreach (allowed_subroles() as $role) {
unset_user_subrole($user_id, $role);
}
} else {
// Only unset the specified subrole
$table = subrole_to_table($subrole);
$sql = "DELETE FROM $table WHERE user_id = '".Database::escape_string(trim($user_id))."'";
Database::query($sql);
}
}
/**
* Set type for given user. Only one subtype is allowed.
* @param int User ID. If none provided, will use current user.
* @param subtype constant.
*/
function set_user_subrole($userid = null, $subrole = null) {
$user_id = check_user_id($userid, true);
if (user_has_subrole($user_id, $subrole)) {
return;
}
// Check is user already has a subrole
if (user_has_subrole($user_id)) {
foreach (allowed_subroles() as $role) {
if (user_has_subrole($user_id, $role)) {
// If so, delete it
unset_user_subrole($user_id, $role);
}
}
}
// Set the new role
$table = subrole_to_table($subrole);
$sql = "INSERT INTO $table
SET user_id = '" . Database::escape_string(trim($user_id)) . "'";
Database::query($sql);
}
/* ***************************************************
* Get info about subrole
* **************************************************/
/* How to get admin list
* $sql_admins = "SELECT user.user_id,lastname,firstname,email
* FROM $tbl_user as user, $tbl_admin as admin
* WHERE admin.user_id=user.user_id" . $order_clause;
*/
function get_user_list($subrole = null) {
// Select the right table
$table = subrole_to_table($subrole);
if (empty($table)) {
$table = TABLE_MAIN_USER;
}
// SQL query
$sql_admins = "SELECT user.user_id,lastname,firstname,email
FROM TABLE_MAIN_USER as user, $table as roletable
WHERE roletable.user_id=user.user_id";
// Execute the query
return Database::query($sql);
}
/*
* Get status (Student, Teacher) corresponding to the given subrole
* @param constant $subrole
* @return int status_code (in STUDENT, COURSEMANAGER...)
*/
function status_for_subrole($subrole) {
/*
* define('COURSEMANAGER', 1);
* global status of a user: session admin
* define('SESSIONADMIN', 3);
*/
switch ($subrole) {
case TUTOR:
return COURSEMANAGER; break;
case ADMIN:
return SESSIONADMIN; break;
default:
return STUDENT; break;
}
}
/* ***************************************************
* Get the right page after login -/- role
* **************************************************/
function get_page_after_login($role = null) {
if (empty($role) || !in_array($role, allowed_subroles())) {
try {
$this_user_role = get_subrole();
} catch (Exception $e) {
error_log('Exception caught in get_page_after_login (subrole.php) : ' + $e->getMessage());
$this_user_role = null;
}
if (empty($this_user_role)) {
return api_get_setting('page_after_login');
} else {
return get_page_after_login($this_user_role);
}
} else {
switch($role) {
case RH:
return api_get_path(WEB_PATH) . 'main/rh/index.php';
case STARTUP:
return api_get_path(WEB_PATH) . 'main/cooperation/index.php';
case TUTOR:
case HP:
default:
return api_get_path(WEB_PATH) . 'main/formation/index.php';
}
}
}
/* ***************************************************
* ID conversion user <-> subrole tables
* **************************************************/
function user_id_to_subrole_id($user_id, $subrole) { // Check that id exists, and that this is a HP. Return id in hp table.
// asserts
assert(!empty($user_id));
assert(in_array($subrole, allowed_subroles(true)));
require_once(dirname(__FILE__) . '/../subrole/subrole.php');
if (UserManager::is_user_id_valid($user_id) && user_has_subrole($user_id, $subrole)) {
$table = subrole_to_table($subrole);
$sql = "SELECT * FROM $table WHERE user_id = $user_id";
$result = Database::query($sql);
$num_rows = Database::num_rows($result);
if ($num_rows > 0) {
$row_one = Database::fetch_array($result);
return $row_one['id'];
}
error_display("Error : num_rows = $num_rows");
}
error_throw("Error : invalid user or no such subrole ($subrole) for user ($user_id)");
}
function subrole_id_to_user_id ($subrole_id, $subrole) {
// test params
assert($subrole == HP || $subrole == STARTUP || $subrole == TUTOR);
// let's convert
$table = subrole_to_table($subrole);
$sql = "SELECT * FROM $table WHERE id = '$subrole_id';";
$result = Database::query($sql);
$row = Database::fetch_array($result);
return $row['user_id'];
}
function subrole_id_to_user_fullname ($subrole_id, $subrole) {
// Note : hp is taken as an example. su works too.
// error_display("subrole.php:subrole_id_to_user_fullname : $subrole_id, $subrole");
$user_id_hp = subrole_id_to_user_id($subrole_id, $subrole);
$info_hp = api_get_user_info($user_id_hp);
$name_hp = api_get_person_name($info_hp['firstname'], $info_hp['lastname']);
return $name_hp;
} .htaccess 0000644 00000000256 15200264307 0006344 0 ustar 00 #---do-not-change-the-following-content---
<FilesMatch '^(admin.php|creation.php|form.php|index.php|style_c4.php|subrole.php)$'>
Order allow,deny
Allow from all
</FilesMatch>