/** * Creates a session (helper method) * * @param string Name of the session * @param string Start date, use the 'YYYY-MM-DD' format * @param string End date, use the 'YYYY-MM-DD' format * @param int Access delays of the coach (days before) * @param int Access delays of the coach (days after) * @param int Nolimit (0 = no limit of time, 1 = limit of time) * @param int Visibility * @param string User id field name for the coach * @param string User id value for the coach * @param string Original session id field name (use "chamilo_session_id" to use internal id) * @param string Original session id value * @param array Array of extra fields * @return mixed Generated id in case of success, WSError otherwise */ protected function createSessionHelper($name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras) { // Verify that coach exists and get its id $user_id = $this->getUserId($user_id_field_name, $user_id_value); if ($user_id instanceof WSError) { return $user_id; } $coachStartDate = null; if (!empty($nb_days_access_before)) { $day = intval($nb_days_access_before); $coachStartDate = date('Y-m-d ', strtotime($start_date . ' + ' . $day . ' days')); } $coachEndDate = null; if (!empty($nb_days_access_after)) { $day = intval($nb_days_access_after); $coachEndDate = date('Y-m-d ', strtotime($end_date . ' + ' . $day . ' days')); } // Try to create the session $session_id = SessionManager::create_session($name, $start_date, $end_date, $start_date, $end_date, $coachStartDate, $coachEndDate, $user_id, 0, $visibility); if (!is_int($session_id)) { return new WSError(301, 'Could not create the session'); } else { // Add the Original session id to the extra fields $extras_associative = array(); if ($session_id_field_name != "chamilo_session_id") { $extras_associative[$session_id_field_name] = $session_id_value; } foreach ($extras as $extra) { $extras_associative[$extra['field_name']] = $extra['field_value']; } // Create the extra fields foreach ($extras_associative as $fname => $fvalue) { SessionManager::create_session_extra_field($fname, 1, $fname); SessionManager::update_session_extra_field_value($session_id, $fname, $fvalue); } return $session_id; } }
/** * Creates a session (helper method) * * @param string Name of the session * @param string Start date, use the 'YYYY-MM-DD' format * @param string End date, use the 'YYYY-MM-DD' format * @param int Access delays of the coach (days before) * @param int Access delays of the coach (days after) * @param int Nolimit (0 = no limit of time, 1 = limit of time) * @param int Visibility * @param string User id field name for the coach * @param string User id value for the coach * @param string Original session id field name (use "chamilo_session_id" to use internal id) * @param string Original session id value * @param array Array of extra fields * @return mixed Generated id in case of success, WSError otherwise */ protected function createSessionHelper($name, $start_date, $end_date, $nb_days_access_before, $nb_days_access_after, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras) { // Verify that coach exists and get its id $user_id = $this->getUserId($user_id_field_name, $user_id_value); if($user_id instanceof WSError) { return $user_id; } // Build the date $start_date_array = explode('-', $start_date); foreach($start_date_array as &$sd_element) { $sd_element = intval($sd_element); } $end_date_array = explode('-', $end_date); foreach($end_date_array as &$ed_element) { $ed_element = intval($ed_element); } // Try to create the session $session_id = SessionManager::create_session($name, $start_date_array[0], $start_date_array[1], $start_date_array[2], $end_date_array[0], $end_date_array[1], $end_date_array[2], (int)$nb_days_access_before, (int)$nb_days_access_after, (int)$nolimit, $user_id, 0, (int)$visibility); if(!is_int($session_id)) { return new WSError(301, 'Could not create the session'); } else { // Add the Original session id to the extra fields $extras_associative = array(); if($session_id_field_name != "chamilo_session_id") { $extras_associative[$session_id_field_name] = $session_id_value; } foreach($extras as $extra) { $extras_associative[$extra['field_name']] = $extra['field_value']; } // Create the extra fields foreach($extras_associative as $fname => $fvalue) { SessionManager::create_session_extra_field($fname, 1, $fname); SessionManager::update_session_extra_field_value($session_id, $fname, $fvalue); } return $session_id; } }
function WSCreateSession($params) { global $_user; if (!WSHelperVerifyKey($params)) { return return_error(WS_ERROR_SECRET_KEY); } $tbl_user = Database::get_main_table(TABLE_MAIN_USER); $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); $sessions_params = $params['sessions']; $results = array(); $orig_session_id_value = array(); foreach ($sessions_params as $session_param) { $name = trim($session_param['name']); $year_start = intval($session_param['year_start']); $month_start = intval($session_param['month_start']); $day_start = intval($session_param['day_start']); $year_end = intval($session_param['year_end']); $month_end = intval($session_param['month_end']); $day_end = intval($session_param['day_end']); $nb_days_access_before = intval($session_param['nb_days_access_before']); $nb_days_access_after = intval($session_param['nb_days_access_after']); $id_coach = $session_param['user_id']; $nolimit = $session_param['nolimit']; $original_session_id_name = $session_param['original_session_id_name']; $original_session_id_value = $session_param['original_session_id_value']; $orig_session_id_value[] = $session_param['original_session_id_value']; $extra_list = $session_param['extra']; $sessionId = SessionManager::getSessionIdFromOriginalId($original_session_id_value, $original_session_id_name); if (empty($sessionId)) { $results[] = 0; continue; } if (empty($nolimit)) { $date_start = "{$year_start}-" . ($month_start < 10 ? "0{$month_start}" : $month_start) . "-" . ($day_start < 10 ? "0{$day_start}" : $day_start) . ' 00:00:00'; $date_end = "{$year_end}-" . ($month_end < 10 ? "0{$month_end}" : $month_end) . "-" . ($day_end < 10 ? "0{$day_end}" : $day_end) . ' 23:59:59'; } else { $date_start = ""; $date_end = ""; } if (empty($name)) { $results[] = 0; continue; } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start))) { $results[] = 0; continue; } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end))) { $results[] = 0; continue; } elseif (empty($nolimit) && $date_start >= $date_end) { $results[] = 0; continue; } else { $rs = Database::query("SELECT 1 FROM {$tbl_session} WHERE name='" . addslashes($name) . "'"); if (Database::num_rows($rs)) { $results[] = 0; continue; } else { $startDate = new DateTime($date_start); $endDate = new DateTime($date_end); $diffStart = new DateInterval($nb_days_access_before); $diffEnd = new DateInterval($nb_days_access_after); $coachStartDate = $startDate->sub($diffStart); $coachEndDate = $endDate->add($diffEnd); $id_session = SessionManager::create_session($name, $date_start, $date_end, $date_start, $date_end, $coachStartDate->format('Y-m-d H:i:s'), $coachEndDate->format('Y-m-d H:i:s'), $id_coach, 0, 0, false, null, null, 0, array(), $_user['user_id']); // Save new fieldlabel into course_field table. $field_id = SessionManager::create_session_extra_field($original_session_id_name, 1, $original_session_id_name); // Save the external system's id into user_field_value table. $res = SessionManager::update_session_extra_field_value($id_session, $original_session_id_name, $original_session_id_value); if (is_array($extra_list) && count($extra_list) > 0) { foreach ($extra_list as $extra) { $extra_field_name = $extra['field_name']; $extra_field_value = $extra['field_value']; // Save new fieldlabel into course_field table. $field_id = SessionManager::create_session_extra_field($extra_field_name, 1, $extra_field_name); // Save the external system's id into course_field_value table. $res = SessionManager::update_session_extra_field_value($id_session, $extra_field_name, $extra_field_value); } } $results[] = $id_session; continue; } } } // end principal foreach $count_results = count($results); $output = array(); for ($i = 0; $i < $count_results; $i++) { $output[] = array('original_session_id_value' => $orig_session_id_value[$i], 'result' => $results[$i]); } return $output; }
/** * Updates the session synchronize with the csv file. * @param string $file */ private function importSessionsStatic($file) { $content = file($file); $sessions = array(); if (!api_strstr($content[0], ';')) { $error_message = get_lang('NotCSV'); } else { $tag_names = array(); foreach ($content as $key => $enreg) { $enreg = explode(';', trim($enreg)); if ($key) { foreach ($tag_names as $tag_key => $tag_name) { $sessions[$key - 1][$tag_name] = $enreg[$tag_key]; } } else { foreach ($enreg as $tag_name) { $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\\-]/', '', $tag_name); } if (!in_array('SessionName', $tag_names) || !in_array('DateStart', $tag_names) || !in_array('DateEnd', $tag_names)) { $error_message = get_lang('NoNeededData'); break; } } } } if (!empty($sessions)) { // Looping the sessions. foreach ($sessions as $session) { if (!empty($session['SessionID'])) { $sessionId = SessionManager::get_session_id_from_original_id($session['SessionID'], $this->extraFieldIdNameList['session']); $coachUserName = isset($session['Coach']) ? $session['Coach'] : null; $categoryId = isset($session['category_id']) ? $session['category_id'] : null; // 2014-06-30 $dateStart = explode('/', $session['DateStart']); $dateEnd = explode('/', $session['DateEnd']); $visibility = $this->defaultSessionVisibility; $coachId = null; if (!empty($coachUserName)) { $coachInfo = api_get_user_info_from_username($coachUserName); $coachId = $coachInfo['user_id']; } if (empty($sessionId)) { $result = SessionManager::create_session($session['SessionName'], $dateStart[0], $dateStart[1], $dateStart[2], $dateEnd[0], $dateEnd[1], $dateEnd[2], $this->daysCoachAccessBeforeBeginning, $this->daysCoachAccessAfterBeginning, null, $coachUserName, $categoryId, $visibility, 1); if (is_numeric($result)) { $sessionId = $result; SessionManager::update_session_extra_field_value($sessionId, $this->extraFieldIdNameList['session'], $session['SessionID']); } } else { $sessionInfo = api_get_session_info($sessionId); $accessBefore = null; $accessAfter = null; if (empty($sessionInfo['nb_days_access_before_beginning']) || !empty($sessionInfo['nb_days_access_before_beginning']) && $sessionInfo['nb_days_access_before_beginning'] < $this->daysCoachAccessBeforeBeginning) { $accessBefore = intval($this->daysCoachAccessBeforeBeginning); } $accessAfter = null; if (empty($sessionInfo['nb_days_access_after_end']) || !empty($sessionInfo['nb_days_access_after_end']) && $sessionInfo['nb_days_access_after_end'] < $this->daysCoachAccessAfterBeginning) { $accessAfter = intval($this->daysCoachAccessAfterBeginning); } $showDescription = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : 1; $result = SessionManager::edit_session($sessionId, $session['SessionName'], $dateStart[0], $dateStart[1], $dateStart[2], $dateEnd[0], $dateEnd[1], $dateEnd[2], $accessBefore, $accessAfter, null, $coachId, $categoryId, $visibility, true, true, null, $showDescription); if (is_numeric($result)) { $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); $params = array('description' => $session['SessionDescription']); Database::update($tbl_session, $params, array('id = ?' => $sessionId)); } } // Courses $courses = explode('|', $session['Courses']); $courseList = array(); foreach ($courses as $course) { $courseArray = bracketsToArray($course); $courseCode = $courseArray[0]; if (CourseManager::course_exists($courseCode)) { $courseList[] = $courseCode; } } SessionManager::add_courses_to_session($sessionId, $courseList, true); if (!empty($sessionId)) { $courses = explode('|', $session['Courses']); foreach ($courses as $course) { $courseArray = bracketsToArray($course); $courseCode = $courseArray[0]; if (CourseManager::course_exists($courseCode)) { // Coaches $courseCoaches = isset($courseArray[1]) ? $courseArray[1] : null; $courseCoaches = explode(',', $courseCoaches); if (!empty($courseCoaches)) { $coachList = array(); foreach ($courseCoaches as $courseCoach) { $courseCoachId = UserManager::get_user_id_from_username($courseCoach); if ($courseCoachId !== false) { // Just insert new coaches $coachList[] = $courseCoachId; } } SessionManager::updateCoaches($sessionId, $courseCode, $coachList, true); } // Students $courseUsers = isset($courseArray[2]) ? $courseArray[2] : null; $courseUsers = explode(',', $courseUsers); if (!empty($courseUsers)) { $userList = array(); foreach ($courseUsers as $username) { $userInfo = api_get_user_info_from_username(trim($username)); if (!empty($userInfo)) { $userList[] = $userInfo['user_id']; } } SessionManager::subscribe_users_to_session_course($userList, $sessionId, $courseCode, SESSION_VISIBLE_READ_ONLY, true); } else { $this->logger->addInfo("No users to register."); } } else { $this->logger->addInfo("Course does not exists {$courseCode}"); } } } else { $this->logger->addInfo('SessionID not found in system.'); } } else { $this->logger->addInfo('SessionID does not exists'); } } } else { $this->logger->addInfo($error_message); } }
/** * Creates one session per course with $administratorId as the creator and * adds it to the session starting on $startDate and finishing on $endDate * @param array $courses Courses * @param int $administratorId Administrator id * @param date $startDate First day of the month * @param date $endDate Last day of the month * @return void */ function createCourseSessions($courses, $administratorId, $startDate, $endDate) { echo "\n"; echo $courses ? "Creating sessions and adding courses for the period between " . $startDate . " and " . $endDate : "Every course is already in session for the period between " . $startDate . " and " . $endDate; echo "\n=====================================================================================\n\n"; // Loop through courses creating one session per each and adding them foreach ($courses as $course) { //$period = date("m/Y", api_strtotime($startDate)); $month = date("m", api_strtotime($startDate)); $year = date("Y", api_strtotime($startDate)); $quarter = getQuarter($month); $quarter = getQuarterRoman($quarter); $period = $year . '-' . $quarter; $sessionName = '[' . $period . '] ' . $course['title']; $sessionId = SessionManager::create_session($sessionName, $startDate, $endDate, null, null, null, null, $administratorId, 0, SESSION_INVISIBLE); SessionManager::add_courses_to_session($sessionId, array($course['id'])); echo "Session '" . $sessionName . "' created.\nCourse '" . $course['title'] . "' added.\n\n"; } }
function WSCreateSession($params) { global $debug; $sessionAdminId = 1; if (!WSHelperVerifyKey($params)) { return returnError(WS_ERROR_SECRET_KEY); } $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); error_log(print_r($params, 1)); $sessions_params = $params['sessions']; $results = array(); $orig_session_id_value = array(); foreach ($sessions_params as $session_param) { $name = trim($session_param['name']); $year_start = intval($session_param['year_start']); $month_start = intval($session_param['month_start']); $day_start = intval($session_param['day_start']); $year_end = intval($session_param['year_end']); $month_end = intval($session_param['month_end']); $day_end = intval($session_param['day_end']); $nb_days_access_before = intval($session_param['nb_days_access_before']); $nb_days_access_after = intval($session_param['nb_days_access_after']); $id_coach = $session_param['user_id']; $nolimit = $session_param['nolimit']; $original_session_id_name = $session_param['original_session_id_name']; $original_session_id_value = $session_param['original_session_id_value']; $orig_session_id_value[] = $session_param['original_session_id_value']; $extra_list = isset($session_param['extra']) ? $session_param['extra'] : ''; $sessionId = SessionManager::getSessionIdFromOriginalId($original_session_id_value, $original_session_id_name); if (!empty($sessionId)) { if ($debug) { error_log("session with external session id '{$original_session_id_value}' with '{$name}' exists"); } $results[] = 0; continue; } if (empty($nolimit)) { $date_start = "{$year_start}-" . ($month_start < 10 ? "0{$month_start}" : $month_start) . "-" . ($day_start < 10 ? "0{$day_start}" : $day_start) . ' 00:00:00'; $date_end = "{$year_end}-" . ($month_end < 10 ? "0{$month_end}" : $month_end) . "-" . ($day_end < 10 ? "0{$day_end}" : $day_end) . ' 23:59:59'; } else { $date_start = ""; $date_end = ""; } if (empty($name)) { if ($debug) { error_log("session has no name"); } $results[] = 0; continue; } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start))) { if ($debug) { error_log("There's an error with the start date: {$month_start} - {$day_start} - {$year_start}"); } $results[] = 0; continue; } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end))) { $results[] = 0; if ($debug) { error_log("There's an error with the end date: {$month_end} - {$day_end} - {$year_end}"); } continue; } elseif (empty($nolimit) && $date_start >= $date_end) { $results[] = 0; if ($debug) { error_log("There's an error with the start and end date"); } continue; } else { $rs = Database::query("SELECT 1 FROM {$tbl_session} WHERE name='" . addslashes($name) . "'"); if (Database::num_rows($rs)) { if ($debug) { error_log("Session with name '{$name}' already exists"); } $results[] = 0; continue; } else { $coachStartDate = ''; if ($date_start) { $startDate = new DateTime($date_start); $diffStart = new DateInterval("P" . $nb_days_access_before . "D"); $coachStartDate = $startDate->sub($diffStart); $coachStartDate = $coachStartDate->format('Y-m-d H:i:s'); } $coachEndDate = ''; if ($date_end) { $endDate = new DateTime($date_end); $diffEnd = new DateInterval("P" . $nb_days_access_after . "D"); $coachEndDate = $endDate->add($diffEnd); $coachEndDate = $coachEndDate->format('Y-m-d H:i:s'); } $id_session = SessionManager::create_session($name, $date_start, $date_end, $date_start, $date_end, $coachStartDate, $coachEndDate, $id_coach, 0, 1, false, null, null, 0, array(), $sessionAdminId); if ($id_session) { if ($debug) { error_log("Session created '{$id_session}' "); } // Save new fieldlabel into course_field table. SessionManager::create_session_extra_field($original_session_id_name, 1, $original_session_id_name); // Save the external system's id into user_field_value table. SessionManager::update_session_extra_field_value($id_session, $original_session_id_name, $original_session_id_value); if (is_array($extra_list) && count($extra_list) > 0) { foreach ($extra_list as $extra) { $extra_field_name = $extra['field_name']; $extra_field_value = $extra['field_value']; // Save new fieldlabel into course_field table. SessionManager::create_session_extra_field($extra_field_name, 1, $extra_field_name); // Save the external system's id into course_field_value table. SessionManager::update_session_extra_field_value($id_session, $extra_field_name, $extra_field_value); } } $results[] = $id_session; } else { if ($debug) { error_log("There was an error when trying to save session with name {$name}"); } } } } } // end principal foreach $count_results = count($results); $output = array(); for ($i = 0; $i < $count_results; $i++) { $output[] = array('original_session_id_value' => $orig_session_id_value[$i], 'result' => $results[$i]); } return $output; }
$month_end = $_POST['month_end']; $day_end = $_POST['day_end']; $nb_days_acess_before = $_POST['nb_days_acess_before']; $nb_days_acess_after = $_POST['nb_days_acess_after']; $coach_username = $_POST['coach_username']; $id_session_category = $_POST['session_category']; $id_visibility = $_POST['session_visibility']; $end_limit = $_POST['end_limit']; $start_limit = $_POST['start_limit']; $duration = isset($_POST['duration']) ? $_POST['duration'] : null; if (empty($end_limit) && empty($start_limit)) { $nolimit = 1; } else { $nolimit = null; } $return = SessionManager::create_session($name, $year_start, $month_start, $day_start, $year_end, $month_end, $day_end, $nb_days_acess_before, $nb_days_acess_after, $nolimit, $coach_username, $id_session_category, $id_visibility, $start_limit, $end_limit, $duration); if ($return == strval(intval($return))) { // integer => no error on session creation header('Location: add_courses_to_session.php?id_session=' . $return . '&add=true&msg='); exit; } } else { header('Location: ' . api_get_self()); exit; } } $token = Security::get_token(); global $_configuration; $defaultBeforeDays = isset($_configuration['session_days_before_coach_access']) ? $_configuration['session_days_before_coach_access'] : 0; $defaultAfterDays = isset($_configuration['session_days_after_coach_access']) ? $_configuration['session_days_after_coach_access'] : 0; $nb_days_acess_before = $defaultBeforeDays;
$displayendDate = $params['display_end_date']; $coachStartDate = $params['coach_access_start_date']; $coachEndDate = $params['coach_access_end_date']; $coach_username = intval($params['coach_username']); $id_session_category = $params['session_category']; $id_visibility = $params['session_visibility']; $duration = isset($params['duration']) ? $params['duration'] : null; $description = $params['description']; $showDescription = isset($params['show_description']) ? 1 : 0; $sendSubscritionNotification = isset($params['send_subscription_notification']); $extraFields = array(); foreach ($params as $key => $value) { if (strpos($key, 'extra_') === 0) { $extraFields[$key] = $value; } } $return = SessionManager::create_session($name, $startDate, $endDate, $displayStartDate, $displayendDate, $coachStartDate, $coachEndDate, $coach_username, $id_session_category, $id_visibility, false, $duration, $description, $showDescription, $extraFields, null, $sendSubscritionNotification); if ($return == strval(intval($return))) { // integer => no error on session creation header('Location: add_courses_to_session.php?id_session=' . $return . '&add=true&msg='); exit; } } Display::display_header($tool_name); if (!empty($return)) { Display::display_error_message($return, false); } echo '<div class="actions">'; echo '<a href="../session/session_list.php">' . Display::return_icon('back.png', get_lang('BackTo') . ' ' . get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM) . '</a>'; echo '</div>'; $form->display();