manage_official_communities.php from pointcarre at Krugle
Show manage_official_communities.php syntax highlighted
<?php // $Id: manage_official_communities.php 470 2006-10-09 12:07:26Z evie_em $
/*
==============================================================================
Dokeos - elearning and course management software
Pointcarre - variant of Dokeos
Copyright (c) Evie, Free University of Brussels
For a full list of contributors, see "credits.txt".
The full license can be read in "license.txt".
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See the GNU General Public License for more details.
http://pointcarre.sourceforge.net/
http://www.dokeos.com/
==============================================================================
*/
/**
==============================================================================
* Script with several community-related functions:
* - create Dokeos courses "communities" based on VUB opleidingen
* - change the introduction text
* - add all teachers of the specifief opleiding to the community (with course
* (admin rights)
* - add the Dokeos class based on the opleiding to the community
* (research: is this dynamic?)
*
* @package dokeos.admin
==============================================================================
*/
/*
==============================================================================
INIT SECTION
==============================================================================
*/
$langFile='admin';
$cidReset=true;
include('../inc/claro_init_global.inc.php');
api_protect_admin_script();
/*
-----------------------------------------------------------
Libraries
-----------------------------------------------------------
*/
require_once('../curriculum/curriculum_database.lib.php');
require_once('../curriculum/curriculum_logic.lib.php');
/*
-----------------------------------------------------------
Constants
-----------------------------------------------------------
*/
define ("CREATE_OFFICIAL_COMMUNITIES", "create_official_communities");
define ("OFFICIAL_COMMUNITY_COURSE_PREFIX", "VUBCOM");
define ("MANAGE_COURSE_TOOL_VISIBILITY", "manage_course_tool_visibility");
define ("CHANGE_COMMUNITY_INTRO_TEXT", "change_community_intro_text");
define ("ADD_CLASSES", "add_classes");
define ("ADD_TEACHERS", "add_teachers");
define ("ORGANISE_PERSONAL_COURSE_CATEGORY_COMMUNITIES", "organise_personal_course_categories_communities");
define ("COMMUNITY_PERSONAL_CATEGORY", "Communities");
/*
-----------------------------------------------------------
Header
-----------------------------------------------------------
*/
$tool_name = get_lang('ManageOfficialCommunities');
$interbredcrump[] = array ("url" => "index.php", "name" => get_lang('AdministrationTools'));
Display::display_header($tool_name);
api_display_tool_title($tool_name);
/*
==============================================================================
LOGIC FUNCTIONS
==============================================================================
*/
/**
* This function assumes that all official classes have already been added to Dokeos. This can be done using the create_update_official_class_list() function.
*/
function add_student_class_relations_to_dokeos()
{
$student_table = CurriculumDatabase::get_vub_student_table();
$study_year_table = CurriculumDatabase::get_vub_studyyear_table();
$registration_table = CurriculumDatabase::get_vub_registration_table();
$dokeos_user_table = Database::get_main_table(MAIN_USER_TABLE);
$dokeos_class_user_table = Database::get_main_table(MAIN_CLASS_USER_TABLE);
$current_academic_year = CurriculumLogic::get_current_academic_year();
$sql_query = "SELECT s.rolnr, sj.stjcode FROM $student_table s, $registration_table i, $study_year_table sj WHERE s.rolnr = i.rolnr AND i.stjcode = sj.stjcode AND i.academiejaar='$current_academic_year' AND sj.academiejaar='$current_academic_year'";
$sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
//get list of the official student-class relations
while ($result = mysql_fetch_array($sql_result))
{
//echo $result['rolnr'] . ', ' . $result['stjcode'] . '<br/>';
$official_user_class_list[] = $result;
}
//process list
foreach ($official_user_class_list as $this_user_class)
{
$student_number = $this_user_class['rolnr'];
$class_code = $this_user_class['stjcode'];
$user_id = CurriculumDatabase::get_user_id_number_from_student_number($student_number);
$class_id = get_class_id_from_code($class_code);
$sql_query = "INSERT IGNORE INTO $dokeos_class_user_table VALUES ('$class_id', '$user_id')";
api_sql_query($sql_query, __FILE__, __LINE__);
}
Display::display_normal_message('The official user-class relations have been added to the Dokeos database.');
}
function get_class_id_from_code($class_code)
{
$dokeos_class_table = Database::get_main_table(MAIN_CLASS_TABLE);
$sql_query = "SELECT id FROM $dokeos_class_table WHERE code='$class_code'";
$sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
$result = mysql_fetch_array($sql_result);
return $result["id"];
}
function display_and_create_communities()
{
$study_year_table = CurriculumDatabase::get_vub_studyyear_table();
$sql_query = "SELECT DISTINCT(opleiding_nummer), opleiding_naam FROM $study_year_table";
$sql_result = api_sql_query($sql_query);
$row = 0;
$column_header[$row ++] = "opleidingsnummer";
$column_header[$row ++] = "opleidingsnaam";
$column_header[$row ++] = "Dokeos cursuscode";
$column_header[$row ++] = "bestaat reeds";
$properties["width"] = "100%";
$properties["cellspacing"] = "1";
Display :: display_complex_table_header($properties, $column_header);
while ($course_result = mysql_fetch_array($sql_result))
{
$course_code = OFFICIAL_COMMUNITY_COURSE_PREFIX . $course_result['opleiding_nummer'];
$already_exists = CourseManager::is_course_created($course_code);
if ($already_exists) $exists_output = "<font color='green'>Ja</font>";
else $exists_output = "<font color='red'>Nee, aanmaken...</font>";
$row = 0;
$table_row[$row ++] = $course_result['opleiding_nummer'];
$table_row[$row ++] = $course_result['opleiding_naam'];
$table_row[$row ++] = $course_code;
$table_row[$row ++] = $exists_output;
Display :: display_table_row($bgcolor, $table_row, true);
if (! $already_exists )
{
//search user id of teacher with username = 'nn'
$dokeos_main_user_table = Database::get_main_table(MAIN_USER_TABLE);
$login = 'nn';
$userid_sql_query = "SELECT * FROM $dokeos_main_user_table WHERE username='$login'";
$userid_sql_result = api_sql_query($userid_sql_query, __FILE__, __LINE__);
$userid_result = mysql_fetch_array($userid_sql_result);
$dokeos_user_id = $userid_result['user_id'];
//create the course with teacher NN
$faculty_code = "COM";
$course_title = "Community - " . $course_result['opleiding_naam'];
$teacher_description = "Docenten " . $course_result['opleiding_naam'];
$message = CurriculumLogic::attempt_create_community_course($dokeos_user_id, $teacher_description, $course_code, $course_title, $faculty_code);
Display::display_normal_message($message);
}
}
Display :: display_table_footer();
}
function set_course_tool_visibility_defaults()
{
$course_table = Database::get_main_table(MAIN_COURSE_TABLE);
$query = "select * from $course_table where `code` LIKE '" . OFFICIAL_COMMUNITY_COURSE_PREFIX . "%'";
$sql_result = mysql_query($query);
while ($course_result = mysql_fetch_array($sql_result))
{
$course_tool_table = Database::get_course_table(TOOL_LIST_TABLE, $course_result['db_name']);
//default: only wiki and forum are visible
$tool_query = "UPDATE $course_tool_table SET visibility = '1' WHERE link LIKE '%wiki/index.php'";
api_sql_query($tool_query);
$tool_query = "UPDATE $course_tool_table SET visibility = '1' WHERE link LIKE '%phpbb/index.php'";
api_sql_query($tool_query);
//set to invisible
$tool_query = "UPDATE $course_tool_table SET visibility = '0' WHERE link LIKE '%calendar/agenda.php'";
api_sql_query($tool_query);
$tool_query = "UPDATE $course_tool_table SET visibility = '0' WHERE link LIKE '%document/document.php'";
api_sql_query($tool_query);
$tool_query = "UPDATE $course_tool_table SET visibility = '0' WHERE link LIKE '%link/link.php'";
api_sql_query($tool_query);
$tool_query = "UPDATE $course_tool_table SET visibility = '0' WHERE link LIKE '%scorm/scormdocument.php'";
api_sql_query($tool_query);
$tool_query = "UPDATE $course_tool_table SET visibility = '0' WHERE link LIKE '%course_description/'";
api_sql_query($tool_query);
//INSERT INTO dokeos_main.course_module VALUES ('', 'wiki', 'wiki/index.php', 'wiki.gif', '5', '2', 'basic');
echo $course_result['code'] . ' - tools into starting positions...<br/>';
}
}
function change_community_intro_text()
{
$course_table = Database::get_main_table(MAIN_COURSE_TABLE);
$query = "select * from $course_table where `code` LIKE '" . OFFICIAL_COMMUNITY_COURSE_PREFIX . "%'";
$sql_result = mysql_query($query);
while ($course_result = mysql_fetch_array($sql_result))
{
$course_intro_table = Database::get_course_table(TOOL_INTRO_TABLE, $course_result['db_name']);
$intro_text = "Welkom op de community ruimte voor uw opleiding.
In deze community zijn alle betrokken docenten en studenten automatisch
ingeschreven. Enkele tools zoals forum en wiki zijn reeds automatisch
beschikbaar voor de studenten. Verdere inrichting van deze ruimte
berust op het initiatief van de docenten. Elke docent met een vak in de
opleiding heeft hier cursusbeheerderrechten.";
$intro_text = addslashes($intro_text);
$tool_query = "UPDATE $course_intro_table SET intro_text = '$intro_text' WHERE id = 'course_homepage'";
api_sql_query($tool_query);
echo $course_result['code'] . ' - setting introduction text...<br/>';
}
}
/*
function get_dokeos_vub_course_code($official_course_code)
{
return OFFICIAL_COMMUNITY_COURSE_PREFIX . $official_course_code;
}
function get_official_code_from_dokeos_code($dokeos_code)
{
$official_code = substr($dokeos_code, strlen(OFFICIAL_COMMUNITY_COURSE_PREFIX), strlen($dokeos_code));
return $official_code;
}
*/
function subscribe_classes_to_communities()
{
//$query1="INSERT IGNORE INTO $tbl_course_class(course_code,class_id) VALUES";
//$query2="INSERT IGNORE INTO $tbl_course_user(course_code,user_id,status) VALUES";
$course_table = Database::get_main_table(MAIN_COURSE_TABLE);
$course_class_table = Database::get_main_table(MAIN_COURSE_CLASS_TABLE);
$course_user_table = Database::get_main_table(MAIN_COURSE_USER_TABLE);
$query = "select * from $course_table where `code` LIKE '" . OFFICIAL_COMMUNITY_COURSE_PREFIX . "%'";
$sql_result = mysql_query($query);
while ($course_result = mysql_fetch_array($sql_result))
{
//for all official community courses...
$course_code = $course_result['code'];
$class_code = substr($course_code, strlen(OFFICIAL_COMMUNITY_COURSE_PREFIX), strlen($course_code));
$class_id = get_class_id_from_code($class_code);
$course_class_query = "INSERT IGNORE INTO $course_class_table (course_code,class_id) VALUES ('$course_code', '$class_id')";
api_sql_query($course_class_query);
add_class_users_to_course($class_id, $course_code);
echo $course_result['code'] . ' - managing class subscriptions...<br/>';
}
}
function add_class_users_to_course($class_id, $course_code)
{
$class_user_table = Database::get_main_table(MAIN_CLASS_USER_TABLE);
$sql_query = "SELECT user_id FROM $class_user_table WHERE class_id='$class_id'";
$sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
while($result = mysql_fetch_array($sql_result))
{
$user_id = $result['user_id'];
CourseManager::add_user_to_course($user_id, $course_code);
//CourseManager::set_user_in_course_status($user_id, $course_code, STUDENT);
}
}
function subscribe_teachers_to_communities()
{
$study_year_table = CurriculumDatabase::get_vub_studyyear_table();
$teacher_table = CurriculumDatabase::get_vub_teacher_table();
$course_table = CurriculumDatabase::get_vub_course_table();
$educationprogram_table = CurriculumDatabase::get_vub_education_program_table();
$programblock_table = CurriculumDatabase::get_vub_programblock_table();
$pbcomp_table = CurriculumDatabase::get_vub_pbcomposition_table();
$current_academic_year = CurriculumLogic::get_current_academic_year();
//working but horrible query
//matches teachers to opleiding_nummer
//-> now we can grant teachers course admin rights on the relevant opleidingscommunity
$sql_query = "SELECT DISTINCT(op.stjcode), sampb.oocode, t.titcode, t.naam, s.opleiding_nummer, s.opleiding_naam FROM $educationprogram_table op, $programblock_table pb, $pbcomp_table sampb, $course_table o, $teacher_table t, $study_year_table s WHERE op.academiejaar = '$current_academic_year' AND pb.academiejaar = '$current_academic_year' AND sampb.oojaar = '$current_academic_year' AND op.opcode = pb.opcode AND pb.pbcode = sampb.pbcode AND sampb.oocode = o.oocode AND o.titcode = t.titcode AND op.stjcode = s.stjcode";
$sql_result = api_sql_query($sql_query);
while($result = mysql_fetch_array($sql_result))
{
//determine Dokeos course code of community course
$course_code = OFFICIAL_COMMUNITY_COURSE_PREFIX . $result['opleiding_nummer'];
//determine Dokeos user id of teacher
$teacher_code = $result['titcode'];
$teacher_name = $result['naam'];
$personnel_code = CurriculumDatabase::get_personnel_number_from_teacher_code($teacher_code);
$user_id = CurriculumDatabase::get_user_id_number_from_personnel_code($personnel_code);
CourseManager::add_user_to_course($user_id, $course_code);
CourseManager::set_user_in_course_status($user_id, $course_code, COURSEMANAGER);
echo "Add teacher $teacher_name ($user_id) to $course_code with course admin rights<br/>";
}
}
/**
* For all users
* - if the user has no courses, skip
* - is there a course category called "communities"
* - if not create it
* - sort all community courses in there
*/
function organise_personal_course_categories()
{
$course_user_table = Database::get_main_table(MAIN_COURSE_USER_TABLE);
$dokeos_user_list = UserManager::get_user_list();
foreach ($dokeos_user_list as $this_user)
{
$user_id = $this_user['user_id'];
$login = $this_user['username'];
$course_list = get_course_list_of_user($user_id);
$number_courses = count($course_list);
if ($number_courses == 0)
{
echo "$login ($user_id)" . " - " . $number_courses . " courses, skip user" . "<br/>";
continue;
}
else
{
//create "communities" personal course category if it doesn't exist yet
$category_id = get_id_personal_course_category($user_id, COMMUNITY_PERSONAL_CATEGORY);
if ( isset ($category_id) && $category_id != '')
{
echo "$login ($user_id)" . " - " . $number_courses . " courses, existing catid $category_id" . "<br/>";
}
else
{
echo "$login ($user_id)" . " - " . $number_courses . " courses, creating category " . COMMUNITY_PERSONAL_CATEGORY . "<br/>";
create_personal_course_category($user_id, COMMUNITY_PERSONAL_CATEGORY);
$category_id = get_id_personal_course_category($user_id, COMMUNITY_PERSONAL_CATEGORY);
}
//organise all community courses into that personal course category
foreach ($course_list as $this_course)
{
$course_title = $this_course['title'];
$course_code = $this_course['code'];
$pos = strpos ( $course_title, "Community");
if ($pos === false)
{
//echo $course_title . " - not a community course, leave as is";
}
else
{
//echo $course_title . " - community course, move into communities category";
$sql_query = "UPDATE $course_user_table SET user_course_cat = '$category_id' WHERE user_id = '$user_id' AND course_code = '$course_code'";
api_sql_query($sql_query, __FILE__, __LINE__);
}
}
}
}
}
function get_course_list_of_user($user_id)
{
$course_table = Database::get_main_table(MAIN_COURSE_TABLE);
$course_user_table = Database::get_main_table(MAIN_COURSE_USER_TABLE);
$sql_query = " SELECT *
FROM $course_table course
LEFT JOIN $course_user_table course_user
ON course.`code` = course_user.`course_code`
WHERE course_user.`user_id` = '$user_id'
";
$sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
while ( $result = mysql_fetch_array($sql_result) )
{
$result_array[] = $result;
}
return $result_array;
}
function get_id_personal_course_category($user_id, $category)
{
$personal_user_course_category_table = Database::get_user_personal_table(USER_COURSE_CATEGORY_TABLE);
$sql_query="SELECT * FROM $personal_user_course_category_table WHERE user_id = '$user_id' AND title = '$category'";
$sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
$result = mysql_fetch_array($sql_result);
return $result['id'];
}
function create_personal_course_category($user_id, $category_title)
{
$personal_user_course_category_table = Database::get_user_personal_table(USER_COURSE_CATEGORY_TABLE);
$sql_query="INSERT INTO $personal_user_course_category_table (user_id, title) VALUES ('".$user_id."', '".addslashes($category_title)."')";
api_sql_query($sql_query, __FILE__, __LINE__);
}
/*
==============================================================================
DISPLAY FUNCTIONS
==============================================================================
*/
function display_main_options()
{
echo "The current academic year is set to " . CurriculumLogic::get_current_academic_year() . ".<br/>";
echo 'What do you want to do?';
echo '<ul>';
echo "<li><a href=\"".$_SERVER['PHP_SELF']."?action=".CREATE_OFFICIAL_COMMUNITIES."\">Create the official communities (based on VUB opleidingen)</a></li>";
echo "<li><a href=\"".$_SERVER['PHP_SELF']."?action=".MANAGE_COURSE_TOOL_VISIBILITY."\">Set the course tool visibilities to default starting positions (only wiki and forum are available)</a></li>";
echo "<li><a href=\"".$_SERVER['PHP_SELF']."?action=".CHANGE_COMMUNITY_INTRO_TEXT."\">Change the community courses intro text</a></li>";
echo "<li><a href=\"".$_SERVER['PHP_SELF']."?action=".ADD_TEACHERS."\">Add all teachers of the 'opleiding' to the relevant community with course admin rights</a></li>";
echo "<li><a href=\"".$_SERVER['PHP_SELF']."?action=".ADD_CLASSES."\">Add the 'opleiding' classes to the relevant communities</a></li>";
echo "<li><a href=\"".$_SERVER['PHP_SELF']."?action=".ORGANISE_PERSONAL_COURSE_CATEGORY_COMMUNITIES."\">Organise the community courses for each user into a personal course category called 'Communities'.</a></li>";
echo '</ul>';
}
/*
==============================================================================
MAIN CODE
==============================================================================
*/
$action = $_REQUEST['action'];
display_main_options();
if ($action == CREATE_OFFICIAL_COMMUNITIES)
{
display_and_create_communities();
}
else if ($action == MANAGE_COURSE_TOOL_VISIBILITY)
{
set_course_tool_visibility_defaults();
}
else if ($action == CHANGE_COMMUNITY_INTRO_TEXT)
{
change_community_intro_text();
}
else if ($action == ADD_CLASSES)
{
subscribe_classes_to_communities();
}
else if ($action == ADD_TEACHERS)
{
subscribe_teachers_to_communities();
}
else if ($action == ORGANISE_PERSONAL_COURSE_CATEGORY_COMMUNITIES)
{
organise_personal_course_categories();
}
/*
==============================================================================
FOOTER
==============================================================================
*/
Display::display_footer();
?>
See more files for this project here