commit
1ccf25ae33
@ -0,0 +1,132 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
/** |
||||||
|
* Course expiration reminder. |
||||||
|
* @package chamilo.cron |
||||||
|
* @author Imanol Losada <imanol.losada@beeznest.com> |
||||||
|
*/ |
||||||
|
require_once __DIR__ . '/../inc/global.inc.php'; |
||||||
|
|
||||||
|
/** |
||||||
|
* Initialization |
||||||
|
*/ |
||||||
|
if (php_sapi_name() != 'cli') { |
||||||
|
exit; //do not run from browser |
||||||
|
} |
||||||
|
|
||||||
|
// Days before expiration date to send reminders |
||||||
|
define("OFFSET", 2); |
||||||
|
$today = gmdate("Y-m-d"); |
||||||
|
$expirationDate = gmdate("Y-m-d", strtotime($today." + ".OFFSET." day")); |
||||||
|
|
||||||
|
$query = "SELECT DISTINCT category.session_id, certificate.user_id FROM ". |
||||||
|
Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY)." AS category |
||||||
|
LEFT JOIN ".Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE). |
||||||
|
" AS certificate ON category.id = certificate.cat_id |
||||||
|
INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION). |
||||||
|
" AS session ON category.session_id = session.id |
||||||
|
WHERE session.date_end BETWEEN '$today' AND '$expirationDate' AND category.session_id IS NOT NULL"; |
||||||
|
|
||||||
|
$sessionId = 0; |
||||||
|
$userIds = array(); |
||||||
|
$sessions = array(); |
||||||
|
$result = Database::query($query); |
||||||
|
|
||||||
|
while ($row = Database::fetch_array($result)) { |
||||||
|
if ($sessionId != $row['session_id']) { |
||||||
|
$sessionId = $row['session_id']; |
||||||
|
$userIds = array(); |
||||||
|
} |
||||||
|
if (!is_null($row['user_id'])) { |
||||||
|
array_push($userIds, $row['user_id']); |
||||||
|
} |
||||||
|
$sessions[$sessionId] = $userIds; |
||||||
|
} |
||||||
|
|
||||||
|
$usersToBeReminded = array(); |
||||||
|
|
||||||
|
foreach ($sessions as $sessionId => $userIds) { |
||||||
|
$userId = 0; |
||||||
|
$userIds = $userIds ? " AND id_user NOT IN (".implode(",", $userIds).")" : null; |
||||||
|
$query = "SELECT sessionUser.id_session, sessionUser.id_user, session.name, session.date_end FROM ". |
||||||
|
Database::get_main_table(TABLE_MAIN_SESSION_USER)." AS sessionUser |
||||||
|
INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION). |
||||||
|
" AS session ON sessionUser.id_session = session.id |
||||||
|
WHERE id_session = $sessionId$userIds"; |
||||||
|
$result = Database::query($query); |
||||||
|
while ($row = Database::fetch_array($result)) { |
||||||
|
$usersToBeReminded[$row['id_user']][$row['id_session']] = |
||||||
|
array( |
||||||
|
'name' => $row['name'], |
||||||
|
'date_end' => $row['date_end'] |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if ($usersToBeReminded) { |
||||||
|
$today = date_create($today); |
||||||
|
$platformLanguage = api_get_setting("platformLanguage"); |
||||||
|
$subject = sprintf( |
||||||
|
get_lang("MailCronCourseExpirationReminderSubject", null, $platformLanguage), |
||||||
|
api_get_setting("Institution") |
||||||
|
); |
||||||
|
$administrator = array( |
||||||
|
'completeName' => api_get_person_name( |
||||||
|
api_get_setting("administratorName"), |
||||||
|
api_get_setting("administratorSurname"), |
||||||
|
null, |
||||||
|
PERSON_NAME_EMAIL_ADDRESS |
||||||
|
), |
||||||
|
'email' => api_get_setting("emailAdministrator") |
||||||
|
); |
||||||
|
echo "\n======================================================================\n\n"; |
||||||
|
foreach ($usersToBeReminded as $userId => $sessions) { |
||||||
|
$user = api_get_user_info($userId); |
||||||
|
$userCompleteName = api_get_person_name( |
||||||
|
$user['firstname'], |
||||||
|
$user['lastname'], |
||||||
|
null, |
||||||
|
PERSON_NAME_EMAIL_ADDRESS |
||||||
|
); |
||||||
|
foreach ($sessions as $sessionId => $session) { |
||||||
|
$daysRemaining = date_diff($today, date_create($session['date_end'])); |
||||||
|
$join = " INNER JOIN ".Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION)."ON id = access_url_id"; |
||||||
|
$result = Database::select( |
||||||
|
'url', |
||||||
|
Database::get_main_table(TABLE_MAIN_ACCESS_URL).$join, |
||||||
|
array( |
||||||
|
'where' => array( |
||||||
|
'session_id = ?' => array( |
||||||
|
$sessionId |
||||||
|
) |
||||||
|
), |
||||||
|
'limit' => '1' |
||||||
|
) |
||||||
|
); |
||||||
|
$body = sprintf( |
||||||
|
get_lang('MailCronCourseExpirationReminderBody', null, $platformLanguage), |
||||||
|
$userCompleteName, |
||||||
|
$session['name'], |
||||||
|
$session['date_end'], |
||||||
|
$daysRemaining->format("%d"), |
||||||
|
$result[0]['url'], |
||||||
|
api_get_setting("siteName") |
||||||
|
); |
||||||
|
api_mail_html( |
||||||
|
$userCompleteName, |
||||||
|
$user['email'], |
||||||
|
$subject, |
||||||
|
$body, |
||||||
|
$administrator['completeName'], |
||||||
|
$administrator['email'] |
||||||
|
); |
||||||
|
echo "Email sent to $userCompleteName (".$user['email'].")\n"; |
||||||
|
echo "Session: ".$session['name']."\n"; |
||||||
|
echo "Date end: ".$session['date_end']."\n"; |
||||||
|
echo "Days remaining: ".$daysRemaining->format("%d")."\n\n"; |
||||||
|
} |
||||||
|
echo "======================================================================\n\n"; |
||||||
|
} |
||||||
|
} else { |
||||||
|
echo "No users to be reminded\n"; |
||||||
|
} |
||||||
@ -0,0 +1,202 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
/** |
||||||
|
* This script moves all courses to a session in the past (closing the month before) |
||||||
|
* @package chamilo.tests.scripts |
||||||
|
*/ |
||||||
|
/** |
||||||
|
* Init |
||||||
|
*/ |
||||||
|
// comment exit statement before executing |
||||||
|
exit; |
||||||
|
require __DIR__ . '/../../main/inc/global.inc.php'; |
||||||
|
|
||||||
|
$debug = 1; |
||||||
|
|
||||||
|
// List of tables that will need an update |
||||||
|
$tables = array( |
||||||
|
'c_announcement' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_attendance' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_blog' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_calendar_event' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_chat_connected' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_course_description' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_document' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_dropbox_category' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_dropbox_file' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_dropbox_post' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_forum_category' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_forum_forum' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_forum_thread' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_forum_thread_qualify' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_forum_thread_qualify_log' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_glossary' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_group_info' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_item_property' => array('c' => 'c_id', 's' => 'id_session'), |
||||||
|
'c_link' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_link_category' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_lp' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_lp_view' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_notebook' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_quiz' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_student_publication' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_survey' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_survey_invitation' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_thematic' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_tool' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_tool_intro' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_wiki' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_wiki_mailcue' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'gradebook_category' => array('c' => 'course_code', 's' => 'session_id'), |
||||||
|
//'session_rel_course', |
||||||
|
//'session_rel_course_rel_user', |
||||||
|
//'session_rel_user', |
||||||
|
'track_course_ranking' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'track_e_access' => array('access_cours_code' => 'c_id', 's' => 'access_session_id'), |
||||||
|
'track_e_attempt' => array('c' => 'course_code', 's' => 'session_id'), |
||||||
|
'track_e_course_access' => array('c' => 'course_code', 's' => 'session_id'), |
||||||
|
'track_e_downloads' => array('c' => 'down_cours_id', 's' => 'down_session_id'), |
||||||
|
'track_e_exercices' => array('c' => 'exe_cours_id', 's' => 'session_id'), |
||||||
|
'track_e_item_property' => array('c' => 'course_id', 's' => 'session_id'), |
||||||
|
'track_e_lastaccess' => array('c' => 'access_cours_code', 's' => 'access_session_id'), |
||||||
|
'track_e_links' => array('c' => 'links_cours_id', 's' => 'links_session_id'), |
||||||
|
'track_e_online' => array('c' => 'course', 's' => 'session_id'), |
||||||
|
'track_e_uploads' => array('c' => 'upload_cours_id', 's' => 'upload_session_id'), |
||||||
|
'user_rel_course_vote' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
); |
||||||
|
// Users related tables. From those tables above, only a few have data related |
||||||
|
// to users. Other data need not be changed, otherwise the resources will only |
||||||
|
// be visible from a specific session. |
||||||
|
$userTables = array( |
||||||
|
'c_attendance' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_blog' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_calendar_event' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_chat_connected' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_dropbox_category' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_dropbox_file' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_dropbox_post' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_forum_thread' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_forum_thread_qualify' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_forum_thread_qualify_log' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_group_info' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_lp_view' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_notebook' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_student_publication' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_wiki' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'c_wiki_mailcue' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'gradebook_category' => array('c' => 'course_code', 's' => 'session_id'), |
||||||
|
//'session_rel_course', |
||||||
|
//'session_rel_course_rel_user', |
||||||
|
//'session_rel_user', |
||||||
|
'track_course_ranking' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'track_e_access' => array('c' => 'c_id', 's' => 'access_session_id'), |
||||||
|
'track_e_attempt' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'track_e_course_access' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'track_e_downloads' => array('c' => 'c_id', 's' => 'down_session_id'), |
||||||
|
'track_e_exercices' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'track_e_item_property' => array('c' => 'course_id', 's' => 'session_id'), |
||||||
|
'track_e_lastaccess' => array('c' => 'c_id', 's' => 'access_session_id'), |
||||||
|
'track_e_links' => array('c' => 'c_id', 's' => 'links_session_id'), |
||||||
|
'track_e_online' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
'track_e_uploads' => array('c' => 'upload_cours_id', 's' => 'upload_session_id'), |
||||||
|
'user_rel_course_vote' => array('c' => 'c_id', 's' => 'session_id'), |
||||||
|
); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Create the sessions |
||||||
|
* For each existing course, create a session that ends on the last day of the |
||||||
|
* past month and starts 2 years before that. |
||||||
|
*/ |
||||||
|
$year = date('Y'); |
||||||
|
$month = date('m'); |
||||||
|
$end = api_strtotime($year.'-'.$month.'-01 00:00:00') - 1; |
||||||
|
$start = $end - (2*365*86400); |
||||||
|
|
||||||
|
// Prepare a list of admin users to avoid removing their relation to the base course |
||||||
|
$sql = 'SELECT user_id FROM admin'; |
||||||
|
$resultAdmin = Database::query($sql); |
||||||
|
$admins = array(); |
||||||
|
while ($row = Database::fetch_assoc($resultAdmin)) { |
||||||
|
$admins[] = $row['user_id']; |
||||||
|
} |
||||||
|
|
||||||
|
$res = Database::select('id, title, code', TABLE_MAIN_COURSE); |
||||||
|
foreach ($res as $course) { |
||||||
|
if ($debug) { |
||||||
|
echo $course['title'] . PHP_EOL; |
||||||
|
} |
||||||
|
$sessionTitle = $course['title'] . ' ' . $month . '-' . $year . ' - a'; |
||||||
|
$startDate = ($year-2) . '-' . $month . '-01'; |
||||||
|
$endDate = $year . '-' . $month . '-01'; |
||||||
|
$id = SessionManager::create_session( |
||||||
|
$sessionTitle, |
||||||
|
$startDate, |
||||||
|
$endDate, |
||||||
|
0, |
||||||
|
0, |
||||||
|
0, |
||||||
|
'info@contidosdixitais.com', |
||||||
|
0, |
||||||
|
SESSION_VISIBLE_READ_ONLY |
||||||
|
); |
||||||
|
while ($id == 'SessionNameAlreadyExists') { |
||||||
|
if ($debug) { |
||||||
|
echo "Could not create session $sessionTitle" . PHP_EOL; |
||||||
|
} |
||||||
|
// Increase the last letter |
||||||
|
$sessionTitle = substr($sessionTitle, 0, -1) . chr(ord(substr($sessionTitle, -1, 1))+1); |
||||||
|
$id = SessionManager::create_session( |
||||||
|
$sessionTitle, |
||||||
|
$startDate, |
||||||
|
$endDate, |
||||||
|
0, |
||||||
|
0, |
||||||
|
0, |
||||||
|
'info@contidosdixitais.com', |
||||||
|
0, |
||||||
|
SESSION_VISIBLE_READ_ONLY |
||||||
|
); |
||||||
|
} |
||||||
|
if ($debug) { |
||||||
|
echo "Session $sessionTitle created with ID $id" . PHP_EOL; |
||||||
|
} |
||||||
|
SessionManager::add_courses_to_session($id, array($course['code'])); |
||||||
|
$resultUsers = Database::query("SELECT user_id FROM " . Database::get_main_table(TABLE_MAIN_COURSE_USER). " WHERE course_code = '" . $course['code'] . "'"); |
||||||
|
$users = array(); |
||||||
|
while ($row = Database::fetch_assoc($resultUsers)) { |
||||||
|
$users[] = $row['user_id']; |
||||||
|
} |
||||||
|
if ($debug) { |
||||||
|
echo count($users) . " users in course " . $course['title'] . " will be moved to session $id (unless they're admins)" . PHP_EOL; |
||||||
|
} |
||||||
|
SessionManager::subscribe_users_to_session_course($users, $id, $course['code']); |
||||||
|
foreach ($userTables as $table => $fields) { |
||||||
|
//c_id + course_id = int, others = char |
||||||
|
if ($fields['c'] == 'c_id' or $fields['c'] == 'course_id') { |
||||||
|
$sql = "UPDATE $table SET " . $fields['s'] . " = $id WHERE " . $fields['c'] . " = " . $course['id']; |
||||||
|
} else { |
||||||
|
$sql = "UPDATE $table SET " . $fields['s'] . " = $id WHERE " . $fields['c'] . " = '" . $course['code'] . "'"; |
||||||
|
} |
||||||
|
if ($debug) { |
||||||
|
echo $sql . PHP_EOL; |
||||||
|
} |
||||||
|
$resultChange = Database::query($sql); |
||||||
|
} |
||||||
|
// Now clean up by un-subscribing the user from the course itself manually |
||||||
|
// to avoid deleting other stuff |
||||||
|
foreach ($users as $user) { |
||||||
|
if (in_array($user, $admins)) { |
||||||
|
// Skip un-subscribing of admin users |
||||||
|
continue; |
||||||
|
} |
||||||
|
$sql = "DELETE FROM course_rel_user WHERE user_id = $user AND course_code = '" . $course['code'] . "'"; |
||||||
|
if ($debug) { |
||||||
|
echo $sql . PHP_EOL; |
||||||
|
} |
||||||
|
$resultRemove = Database::query($sql); |
||||||
|
} |
||||||
|
} |
||||||
|
if ($debug) { |
||||||
|
echo "End of moving process" . PH_EOL; |
||||||
|
} |
||||||
Loading…
Reference in new issue