function WSEditSession($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']); $original_session_id_value = $session_param['original_session_id_value']; $original_session_id_name = $session_param['original_session_id_name']; $orig_session_id_value[] = $original_session_id_value; $coach_username = $session_param['coach_username']; $nolimit = $session_param['nolimit']; $id_coach = $session_param['user_id']; $extra_list = $session_param['extra']; $id = SessionManager::getSessionIdFromOriginalId($original_session_id_value, $original_session_id_name); if (empty($id)) { $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); $date_end = "{$year_end}-" . ($month_end < 10 ? "0{$month_end}" : $month_end) . "-" . ($day_end < 10 ? "0{$day_end}" : $day_end); } else { $date_start = ""; $date_end = ""; } if (empty($name)) { $results[] = 0; //SessionNameIsRequired continue; } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start))) { $results[] = 0; //InvalidStartDate continue; } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end))) { $results[] = 0; //InvalidEndDate continue; } elseif (empty($nolimit) && $date_start >= $date_end) { $results[] = 0; //StartDateShouldBeBeforeEndDate 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); $sessionInfo = api_get_session_info($id); SessionManager::edit_session($id, $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, $sessionInfo['session_category_id'], $sessionInfo['visibility'], $sessionInfo['description'], $sessionInfo['show_description'], $sessionInfo['duration'], null, $_user['user_id']); 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 the external system's id into session_field_value table. $res = SessionManager::update_session_extra_field_value($id, $extra_field_name, $extra_field_value); } } $results[] = 1; 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); } }
/** * Edits 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 True on success, WSError otherwise */ protected function editSessionHelper($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) { $session_id = $this->getSessionId($session_id_field_name, $session_id_value); if ($session_id instanceof WSError) { return $session_id; } else { // 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')); } $result_id = SessionManager::edit_session($session_id, $name, $start_date, $end_date, $start_date, $end_date, $coachStartDate, $coachEndDate, $user_id, 0, (int) $visibility); if (!is_int($result_id)) { return new WSError(302, 'Could not edit the session'); } else { if (!empty($extras)) { $extras_associative = array(); 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 true; } } }
function WSEditSession($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); $t_sf = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); $t_sfv = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES); $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_acess_before = intval($session_param['nb_days_access_before']); $nb_days_acess_after = intval($session_param['nb_days_access_after']); $original_session_id_value = $session_param['original_session_id_value']; $original_session_id_name = $session_param['original_session_id_name']; $orig_session_id_value[] = $original_session_id_value; $coach_username = $session_param['coach_username']; $nolimit = $session_param['nolimit']; $id_coach = $session_param['user_id']; $extra_list = $session_param['extra']; // Get session id from original session id $sql = "SELECT session_id FROM {$t_sf} sf,{$t_sfv} sfv WHERE sfv.field_id=sf.id AND field_variable='{$original_session_id_name}' AND field_value='{$original_session_id_value}'"; $res = Database::query($sql); $row = Database::fetch_row($res); $id = intval($row[0]); if (Database::num_rows($res) < 1) { $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); $date_end = "{$year_end}-" . ($month_end < 10 ? "0{$month_end}" : $month_end) . "-" . ($day_end < 10 ? "0{$day_end}" : $day_end); } else { $date_start = "000-00-00"; $date_end = "000-00-00"; } if (empty($name)) { $results[] = 0; //SessionNameIsRequired continue; } elseif (empty($nolimit) && (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start))) { $results[] = 0; //InvalidStartDate continue; } elseif (empty($nolimit) && (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end))) { $results[] = 0; //InvalidEndDate continue; } elseif (empty($nolimit) && $date_start >= $date_end) { $results[] = 0; //StartDateShouldBeBeforeEndDate continue; } else { $sql = "UPDATE {$tbl_session} SET " . "name='" . addslashes($name) . "', " . "date_start='" . $date_start . "', " . "date_end='" . $date_end . "', " . "id_coach='" . $id_coach . "', " . "session_admin_id='" . intval($_user['user_id']) . "', " . "nb_days_access_before_beginning='" . $nb_days_acess_before . "', " . "nb_days_access_after_end='" . $nb_days_acess_after . "'" . " WHERE id='" . $id . "'"; Database::query($sql); $id_session = Database::insert_id(); 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 the external system's id into session_field_value table. $res = SessionManager::update_session_extra_field_value($id, $extra_field_name, $extra_field_value); } } $results[] = 1; 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; }
/** * Edits 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 True on success, WSError otherwise */ protected function editSessionHelper($name, $start_date, $end_date, $coach_access_start_date, $coach_access_end_date, $nolimit, $visibility, $user_id_field_name, $user_id_value, $session_id_field_name, $session_id_value, $extras) { $session_id = $this->getSessionId($session_id_field_name, $session_id_value); if ($session_id instanceof WSError) { return $session_id; } else { // 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; } $params = array('id' => $session_id, 'name' => $name, 'access_start_date' => $start_date, 'access_end_date' => $end_date, 'coach_access_start_date' => $coach_access_start_date, 'coach_access_end_date' => $coach_access_end_date, 'visibility' => $visibility); $result_id = SessionManager::update($params); if (!is_int($result_id)) { return new WSError(302, 'Could not edit the session'); } else { if (!empty($extras)) { $extras_associative = array(); 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 true; } } }
/** * Edits 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 True on success, WSError otherwise */ protected function editSessionHelper($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) { $session_id = $this->getSessionId($session_id_field_name, $session_id_value); if($session_id instanceof WSError) { return $session_id; } else { // 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); } $result_id = SessionManager::edit_session($session_id, $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($result_id)) { return new WSError(302, 'Could not edit the session'); } else { if(!empty($extras)) { $extras_associative = array(); 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 true; } } }