inscription.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script displays a form for registering new users.
  5. * @package chamilo.auth
  6. */
  7. use \ChamiloSession as Session;
  8. $language_file = array('registration', 'admin');
  9. //quick hack to adapt the registration form result to the selected registration language
  10. if (!empty($_POST['language'])) {
  11. $_GET['language'] = $_POST['language'];
  12. }
  13. require_once '../inc/global.inc.php';
  14. require_once api_get_path(CONFIGURATION_PATH).'profile.conf.php';
  15. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  16. $htmlHeadXtra[] = api_get_password_checker_js('#username', '#pass1');
  17. // User is not allowed if Terms and Conditions are disabled and
  18. // registration is disabled too.
  19. $isNotAllowedHere = api_get_setting('allow_terms_conditions') === 'false' &&
  20. api_get_setting('allow_registration') === 'false';
  21. if ($isNotAllowedHere) {
  22. api_not_allowed(true, get_lang('RegistrationDisabled'));
  23. }
  24. if (!empty($_SESSION['user_language_choice'])) {
  25. $user_selected_language = $_SESSION['user_language_choice'];
  26. } elseif (!empty($_SESSION['_user']['language'])) {
  27. $user_selected_language = $_SESSION['_user']['language'];
  28. } else {
  29. $user_selected_language = get_setting('platformLanguage');
  30. }
  31. $form = new FormValidator('registration');
  32. if (api_get_setting('allow_terms_conditions') == 'true') {
  33. $user_already_registered_show_terms = isset($_SESSION['term_and_condition']['user_id']);
  34. } else {
  35. $user_already_registered_show_terms = false;
  36. }
  37. // Direct Link Subscription feature #5299
  38. $course_code_redirect = isset($_REQUEST['c']) && !empty($_REQUEST['c']) ? $_REQUEST['c'] : null;
  39. $exercise_redirect = isset($_REQUEST['e']) && !empty($_REQUEST['e']) ? $_REQUEST['e'] : null;
  40. if (!empty($course_code_redirect)) {
  41. Session::write('course_redirect', $course_code_redirect);
  42. Session::write('exercise_redirect', $exercise_redirect);
  43. }
  44. if ($user_already_registered_show_terms == false) {
  45. if (api_is_western_name_order()) {
  46. // FIRST NAME and LAST NAME
  47. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  48. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  49. } else {
  50. // LAST NAME and FIRST NAME
  51. $form->addElement('text', 'lastname', get_lang('LastName'), array('size' => 40));
  52. $form->addElement('text', 'firstname', get_lang('FirstName'), array('size' => 40));
  53. }
  54. $form->applyFilter(array('lastname', 'firstname'), 'trim');
  55. $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
  56. $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
  57. // EMAIL
  58. $form->addElement('text', 'email', get_lang('Email'), array('size' => 40));
  59. if (api_get_setting('registration', 'email') == 'true') {
  60. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  61. }
  62. if (api_get_setting('login_is_email') == 'true') {
  63. $form->applyFilter('email', 'trim');
  64. if (api_get_setting('registration', 'email') != 'true') {
  65. $form->addRule('email', get_lang('ThisFieldIsRequired'), 'required');
  66. }
  67. $form->addRule('email', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  68. $form->addRule('email', get_lang('UserTaken'), 'username_available');
  69. }
  70. $form->addRule('email', get_lang('EmailWrong'), 'email');
  71. if (api_get_setting('openid_authentication') == 'true') {
  72. $form->addElement('text', 'openid', get_lang('OpenIDURL'), array('size' => 40));
  73. }
  74. // Enabled by Ivan Tcholakov, 06-APR-2009. CONFVAL_ASK_FOR_OFFICIAL_CODE = false by default.
  75. // OFFICIAL CODE
  76. if (CONFVAL_ASK_FOR_OFFICIAL_CODE) {
  77. $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => 40));
  78. if (api_get_setting('registration', 'officialcode') == 'true') {
  79. $form->addRule(
  80. 'official_code',
  81. get_lang('ThisFieldIsRequired'),
  82. 'required'
  83. );
  84. }
  85. }
  86. // USERNAME
  87. if (api_get_setting('login_is_email') != 'true') {
  88. $form->addElement('text', 'username', get_lang('UserName'), array('id' => 'username', 'size' => USERNAME_MAX_LENGTH));
  89. $form->applyFilter('username', 'trim');
  90. $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
  91. $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
  92. $form->addRule('username', get_lang('UsernameWrong'), 'username');
  93. $form->addRule('username', get_lang('UserTaken'), 'username_available');
  94. }
  95. // PASSWORD
  96. $form->addElement('password', 'pass1', get_lang('Pass'), array('id' => 'pass1', 'size' => 20, 'autocomplete' => 'off'));
  97. if (isset($_configuration['allow_strength_pass_checker']) && $_configuration['allow_strength_pass_checker']) {
  98. $form->addElement('label', null, '<div id="password_progress"></div>');
  99. }
  100. $form->addElement('password', 'pass2', get_lang('Confirmation'), array('id' => 'pass2', 'size' => 20, 'autocomplete' => 'off'));
  101. $form->addRule('pass1', get_lang('ThisFieldIsRequired'), 'required');
  102. $form->addRule('pass2', get_lang('ThisFieldIsRequired'), 'required');
  103. $form->addRule(array('pass1', 'pass2'), get_lang('PassTwo'), 'compare');
  104. if (CHECK_PASS_EASY_TO_FIND) {
  105. $form->addRule(
  106. 'password1',
  107. get_lang('PassTooEasy') . ': ' . api_generate_password(),
  108. 'callback',
  109. 'api_check_password'
  110. );
  111. }
  112. // PHONE
  113. $form->addElement('text', 'phone', get_lang('Phone'), array('size' => 20));
  114. if (api_get_setting('registration', 'phone') == 'true') {
  115. $form->addRule('phone', get_lang('ThisFieldIsRequired'), 'required');
  116. }
  117. // PICTURE
  118. /*if (api_get_setting('profile', 'picture') == 'true') {
  119. $form->addElement('file', 'picture', get_lang('AddPicture'));
  120. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  121. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  122. }*/
  123. // LANGUAGE
  124. if (api_get_setting('registration', 'language') == 'true') {
  125. $form->addElement('select_language', 'language', get_lang('Language'));
  126. }
  127. // STUDENT/TEACHER
  128. if (api_get_setting('allow_registration_as_teacher') != 'false') {
  129. $form->addElement('radio', 'status', get_lang('Profile'), get_lang('RegStudent'), STUDENT);
  130. $form->addElement('radio', 'status', null, get_lang('RegAdmin'), COURSEMANAGER);
  131. }
  132. $allowCaptcha = isset($_configuration['allow_captcha']) ? $_configuration['allow_captcha'] : false;
  133. if ($allowCaptcha) {
  134. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  135. $options = array(
  136. 'width' => 220,
  137. 'height' => 90,
  138. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  139. 'sessionVar' => basename(__FILE__, '.php'),
  140. 'imageOptions' => array(
  141. 'font_size' => 20,
  142. 'font_path' => api_get_path(LIBRARY_PATH).'pchart/fonts/',
  143. 'font_file' => 'tahoma.ttf',
  144. //'output' => 'gif'
  145. )
  146. );
  147. $captcha_question = $form->addElement('CAPTCHA_Image', 'captcha_question', '', $options);
  148. $form->addElement('static', null, null, get_lang('ClickOnTheImageForANewOne'));
  149. $form->addElement('text', 'captcha', get_lang('EnterTheLettersYouSee'), array('size' => 40));
  150. $form->addRule('captcha', get_lang('EnterTheCharactersYouReadInTheImage'), 'required', null, 'client');
  151. $form->addRule('captcha', get_lang('TheTextYouEnteredDoesNotMatchThePicture'), 'CAPTCHA', $captcha_question);
  152. }
  153. // EXTENDED FIELDS
  154. if (api_get_setting('extended_profile') == 'true' &&
  155. api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true'
  156. ) {
  157. $form->add_html_editor('competences', get_lang('MyCompetences'), false, false, array('ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130'));
  158. }
  159. if (api_get_setting('extended_profile') == 'true' &&
  160. api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true'
  161. ) {
  162. $form->add_html_editor('diplomas', get_lang('MyDiplomas'), false, false, array('ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130'));
  163. }
  164. if (api_get_setting('extended_profile') == 'true' &&
  165. api_get_setting('extendedprofile_registration', 'myteach') == 'true'
  166. ) {
  167. $form->add_html_editor('teach', get_lang('MyTeach'), false, false, array('ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130'));
  168. }
  169. if (api_get_setting('extended_profile') == 'true' &&
  170. api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true'
  171. ) {
  172. $form->add_html_editor('openarea', get_lang('MyPersonalOpenArea'), false, false, array('ToolbarSet' => 'register', 'Width' => '100%', 'Height' => '130'));
  173. }
  174. if (api_get_setting('extended_profile') == 'true') {
  175. if (api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true' &&
  176. api_get_setting('extendedprofile_registrationrequired', 'mycomptetences') == 'true'
  177. ) {
  178. $form->addRule('competences', get_lang('ThisFieldIsRequired'), 'required');
  179. }
  180. if (api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true' &&
  181. api_get_setting('extendedprofile_registrationrequired', 'mydiplomas') == 'true'
  182. ) {
  183. $form->addRule('diplomas', get_lang('ThisFieldIsRequired'), 'required');
  184. }
  185. if (api_get_setting('extendedprofile_registration', 'myteach') == 'true' &&
  186. api_get_setting('extendedprofile_registrationrequired', 'myteach') == 'true'
  187. ) {
  188. $form->addRule('teach', get_lang('ThisFieldIsRequired'), 'required');
  189. }
  190. if (api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true' &&
  191. api_get_setting('extendedprofile_registrationrequired', 'mypersonalopenarea') == 'true'
  192. ) {
  193. $form->addRule('openarea', get_lang('ThisFieldIsRequired'), 'required');
  194. }
  195. }
  196. // EXTRA FIELDS
  197. $extra_data = UserManager::get_extra_user_data(api_get_user_id(), true);
  198. UserManager::set_extra_fields_in_form($form, $extra_data, 'registration');
  199. }
  200. if (isset($_SESSION['user_language_choice']) && $_SESSION['user_language_choice'] != '') {
  201. $defaults['language'] = $_SESSION['user_language_choice'];
  202. } else {
  203. $defaults['language'] = api_get_setting('platformLanguage');
  204. }
  205. if (!empty($_GET['username'])) {
  206. $defaults['username'] = Security::remove_XSS($_GET['username']);
  207. }
  208. if (!empty($_GET['email'])) {
  209. $defaults['email'] = Security::remove_XSS($_GET['email']);
  210. }
  211. if (!empty($_GET['phone'])) {
  212. $defaults['phone'] = Security::remove_XSS($_GET['phone']);
  213. }
  214. if (api_get_setting('openid_authentication') == 'true' && !empty($_GET['openid'])) {
  215. $defaults['openid'] = Security::remove_XSS($_GET['openid']);
  216. }
  217. $defaults['status'] = STUDENT;
  218. if (is_array($extra_data)) {
  219. $defaults = array_merge($defaults, $extra_data);
  220. }
  221. $form->setDefaults($defaults);
  222. $content = null;
  223. if (!CustomPages::enabled()) {
  224. // Load terms & conditions from the current lang
  225. if (api_get_setting('allow_terms_conditions') == 'true') {
  226. $get = array_keys($_GET);
  227. if (isset($get)) {
  228. if ($get[0] == 'legal') {
  229. $language = api_get_interface_language();
  230. $language = api_get_language_id($language);
  231. $term_preview = LegalManager::get_last_condition($language);
  232. if (!$term_preview) {
  233. //look for the default language
  234. $language = api_get_setting('platformLanguage');
  235. $language = api_get_language_id($language);
  236. $term_preview = LegalManager::get_last_condition($language);
  237. }
  238. $tool_name = get_lang('TermsAndConditions');
  239. Display :: display_header($tool_name);
  240. if (!empty($term_preview['content'])) {
  241. echo $term_preview['content'];
  242. } else {
  243. echo get_lang('ComingSoon');
  244. }
  245. Display :: display_footer();
  246. exit;
  247. }
  248. }
  249. }
  250. $tool_name = get_lang('Registration', null, (!empty($_POST['language'])?$_POST['language']:$_user['language']));
  251. if (api_get_setting('allow_terms_conditions') == 'true' && $user_already_registered_show_terms) {
  252. $tool_name = get_lang('TermsAndConditions');
  253. }
  254. $home = api_get_path(SYS_PATH).'home/';
  255. if (api_is_multiple_url_enabled()) {
  256. $access_url_id = api_get_current_access_url_id();
  257. if ($access_url_id != -1) {
  258. $url_info = api_get_access_url($access_url_id);
  259. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  260. $clean_url = replace_dangerous_char($url);
  261. $clean_url = str_replace('/', '-', $clean_url);
  262. $clean_url .= '/';
  263. $home_old = api_get_path(SYS_PATH).'home/';
  264. $home = api_get_path(SYS_PATH).'home/'.$clean_url;
  265. }
  266. }
  267. if (file_exists($home.'register_top_'.$user_selected_language.'.html')) {
  268. $home_top_temp = @(string)file_get_contents($home.'register_top_'.$user_selected_language.'.html');
  269. $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
  270. $open = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  271. if (!empty($open)) {
  272. $content = '<div class="well_border">'.$open.'</div>';
  273. }
  274. }
  275. // Forbidden to self-register
  276. if ($isNotAllowedHere) {
  277. api_not_allowed(true, get_lang('RegistrationDisabled'));
  278. }
  279. if (api_get_setting('allow_registration') == 'approval') {
  280. $content .= Display::return_message(get_lang('YourAccountHasToBeApproved'));
  281. }
  282. //if openid was not found
  283. if (!empty($_GET['openid_msg']) && $_GET['openid_msg'] == 'idnotfound') {
  284. $content .= Display::return_message(get_lang('OpenIDCouldNotBeFoundPleaseRegister'));
  285. }
  286. }
  287. // Terms and conditions
  288. if (api_get_setting('allow_terms_conditions') == 'true') {
  289. $language = api_get_interface_language();
  290. $language = api_get_language_id($language);
  291. $term_preview = LegalManager::get_last_condition($language);
  292. if (!$term_preview) {
  293. //we load from the platform
  294. $language = api_get_setting('platformLanguage');
  295. $language = api_get_language_id($language);
  296. $term_preview = LegalManager::get_last_condition($language);
  297. //if is false we load from english
  298. if (!$term_preview) {
  299. $language = api_get_language_id('english'); //this must work
  300. $term_preview = LegalManager::get_last_condition($language);
  301. }
  302. }
  303. // Version and language
  304. $form->addElement('hidden', 'legal_accept_type', $term_preview['version'].':'.$term_preview['language_id']);
  305. $form->addElement('hidden', 'legal_info', $term_preview['legal_id'].':'.$term_preview['language_id']);
  306. if ($term_preview['type'] == 1) {
  307. $form->addElement(
  308. 'checkbox',
  309. 'legal_accept',
  310. null,
  311. get_lang('IHaveReadAndAgree').'&nbsp;<a href="inscription.php?legal" target="_blank">'.get_lang('TermsAndConditions').'</a>'
  312. );
  313. $form->addRule('legal_accept', get_lang('ThisFieldIsRequired'), 'required');
  314. } else {
  315. $preview = LegalManager::show_last_condition($term_preview);
  316. $form->addElement('label', null, $preview);
  317. }
  318. }
  319. $form->addElement('button', 'submit', get_lang('RegisterUser'), array('class' => 'btn btn-primary btn-large'));
  320. if ($form->validate()) {
  321. $values = $form->getSubmitValues(1);
  322. //make *sure* the login isn't too long
  323. $values['username'] = api_substr($values['username'], 0, USERNAME_MAX_LENGTH);
  324. if (api_get_setting('allow_registration_as_teacher') == 'false') {
  325. $values['status'] = STUDENT;
  326. }
  327. // Added by Ivan Tcholakov, 06-MAR-2008.
  328. if (empty($values['official_code'])) {
  329. $values['official_code'] = api_strtoupper($values['username']);
  330. }
  331. if (api_get_setting('login_is_email') == 'true') {
  332. $values['username'] = $values['email'];
  333. }
  334. if ($user_already_registered_show_terms &&
  335. api_get_setting('allow_terms_conditions') == 'true'
  336. ) {
  337. $user_id = $_SESSION['term_and_condition']['user_id'];
  338. $is_admin = UserManager::is_admin($user_id);
  339. Session::write('is_platformAdmin', $is_admin);
  340. } else {
  341. // Moved here to include extra fields when creating a user. Formerly placed after user creation
  342. // Register extra fields
  343. $extras = array();
  344. foreach ($values as $key => $value) {
  345. if (substr($key, 0, 6) == 'extra_') {
  346. //an extra field
  347. $extras[substr($key, 6)] = $value;
  348. } elseif (strpos($key, 'remove_extra_') !== false) {
  349. $extra_value = Security::filter_filename(urldecode(key($value)));
  350. // To remove from user_field_value and folder
  351. UserManager::update_extra_field_value(
  352. $user_id,
  353. substr($key, 13),
  354. $extra_value
  355. );
  356. }
  357. }
  358. $status = isset($values['status']) ? $values['status'] : null;
  359. $phone = isset($values['phone']) ? $values['phone'] : null;
  360. // Creates a new user
  361. $user_id = UserManager::create_user(
  362. $values['firstname'],
  363. $values['lastname'],
  364. $status,
  365. $values['email'],
  366. $values['username'],
  367. $values['pass1'],
  368. $values['official_code'],
  369. $values['language'],
  370. $phone,
  371. null,
  372. PLATFORM_AUTH_SOURCE,
  373. null,
  374. 1,
  375. 0,
  376. $extras,
  377. null,
  378. true
  379. );
  380. //update the extra fields
  381. $count_extra_field = count($extras);
  382. if ($count_extra_field > 0) {
  383. foreach ($extras as $key => $value) {
  384. // For array $value -> if exists key 'tmp_name' then must not be empty
  385. // This avoid delete from user field value table when doesn't upload a file
  386. if (is_array($value)) {
  387. if (array_key_exists('tmp_name', $value) && empty($value['tmp_name'])) {
  388. //Nothing to do
  389. } else {
  390. if (array_key_exists('tmp_name', $value)) {
  391. $value['tmp_name'] = Security::filter_filename($value['tmp_name']);
  392. }
  393. if (array_key_exists('name', $value)) {
  394. $value['name'] = Security::filter_filename($value['name']);
  395. }
  396. UserManager::update_extra_field_value($user_id, $key, $value);
  397. }
  398. } else {
  399. UserManager::update_extra_field_value($user_id, $key, $value);
  400. }
  401. }
  402. }
  403. if ($user_id) {
  404. // Storing the extended profile
  405. $store_extended = false;
  406. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_USER)." SET ";
  407. if (api_get_setting('extended_profile') == 'true' && api_get_setting('extendedprofile_registration', 'mycomptetences') == 'true') {
  408. $sql_set[] = "competences = '".Database::escape_string($values['competences'])."'";
  409. $store_extended = true;
  410. }
  411. if (api_get_setting('extended_profile') == 'true' && api_get_setting('extendedprofile_registration', 'mydiplomas') == 'true') {
  412. $sql_set[] = "diplomas = '".Database::escape_string($values['diplomas'])."'";
  413. $store_extended = true;
  414. }
  415. if (api_get_setting('extended_profile') == 'true' && api_get_setting('extendedprofile_registration', 'myteach') == 'true') {
  416. $sql_set[] = "teach = '".Database::escape_string($values['teach'])."'";
  417. $store_extended = true;
  418. }
  419. if (api_get_setting('extended_profile') == 'true' && api_get_setting('extendedprofile_registration', 'mypersonalopenarea') == 'true') {
  420. $sql_set[] = "openarea = '".Database::escape_string($values['openarea'])."'";
  421. $store_extended = true;
  422. }
  423. if ($store_extended) {
  424. $sql .= implode(',', $sql_set);
  425. $sql .= " WHERE user_id = ".intval($user_id)."";
  426. Database::query($sql);
  427. }
  428. // if there is a default duration of a valid account then we have to change the expiration_date accordingly
  429. if (api_get_setting('account_valid_duration') != '') {
  430. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_USER)."
  431. SET expiration_date='registration_date+1'
  432. WHERE user_id='".$user_id."'";
  433. Database::query($sql);
  434. }
  435. $course_code_redirect = Session::read('course_redirect');
  436. // Saving user to course if it was set.
  437. if (!empty($course_code_redirect)) {
  438. $course_info = api_get_course_info($course_code_redirect);
  439. if (!empty($course_info)) {
  440. if (in_array(
  441. $course_info['visibility'],
  442. array(
  443. COURSE_VISIBILITY_OPEN_PLATFORM,
  444. COURSE_VISIBILITY_OPEN_WORLD
  445. )
  446. )
  447. ) {
  448. CourseManager::subscribe_user(
  449. $user_id,
  450. $course_info['code']
  451. );
  452. }
  453. }
  454. }
  455. /* If the account has to be approved then we set the account to inactive,
  456. sent a mail to the platform admin and exit the page.*/
  457. if (api_get_setting('allow_registration') == 'approval') {
  458. $TABLE_USER = Database::get_main_table(TABLE_MAIN_USER);
  459. // 1. set account inactive
  460. $sql = "UPDATE $TABLE_USER SET active='0' WHERE user_id = ".$user_id;
  461. Database::query($sql);
  462. // 2. Send mail to all platform admin
  463. $emailsubject = get_lang('ApprovalForNewAccount', null, $values['language']).': '.$values['username'];
  464. $emailbody = get_lang('ApprovalForNewAccount', null, $values['language'])."\n";
  465. $emailbody .= get_lang('UserName', null, $values['language']).': '.$values['username']."\n";
  466. if (api_is_western_name_order()) {
  467. $emailbody .= get_lang('FirstName', null, $values['language']).': '.$values['firstname']."\n";
  468. $emailbody .= get_lang('LastName', null, $values['language']).': '.$values['lastname']."\n";
  469. } else {
  470. $emailbody .= get_lang('LastName', null, $values['language']).': '.$values['lastname']."\n";
  471. $emailbody .= get_lang('FirstName', null, $values['language']).': '.$values['firstname']."\n";
  472. }
  473. $emailbody .= get_lang('Email', null, $values['language']).': '.$values['email']."\n";
  474. $emailbody .= get_lang('Status', null, $values['language']).': '.$values['status']."\n\n";
  475. $url_edit = Display::url(
  476. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id,
  477. api_get_path(WEB_CODE_PATH).'admin/user_edit.php?user_id='.$user_id
  478. );
  479. $emailbody .= get_lang('ManageUser', null, $values['language']).": $url_edit";
  480. $admins = UserManager::get_all_administrators();
  481. foreach ($admins as $admin_info) {
  482. MessageManager::send_message(
  483. $admin_info['user_id'],
  484. $emailsubject,
  485. $emailbody,
  486. null,
  487. null,
  488. null,
  489. null,
  490. null,
  491. null,
  492. $user_id
  493. );
  494. }
  495. // 3. exit the page
  496. unset($user_id);
  497. Display :: display_header($tool_name);
  498. echo Display::page_header($tool_name);
  499. echo $content;
  500. Display::display_footer();
  501. exit;
  502. }
  503. }
  504. }
  505. // Terms & Conditions
  506. if (api_get_setting('allow_terms_conditions') == 'true') {
  507. // Update the terms & conditions.
  508. if (isset($values['legal_accept_type'])) {
  509. $cond_array = explode(':', $values['legal_accept_type']);
  510. if (!empty($cond_array[0]) && !empty($cond_array[1])) {
  511. $time = time();
  512. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  513. UserManager::update_extra_field_value($user_id, 'legal_accept', $condition_to_save);
  514. }
  515. }
  516. $values = api_get_user_info($user_id);
  517. }
  518. /* SESSION REGISTERING */
  519. /* @todo move this in a function */
  520. $_user['firstName'] = stripslashes($values['firstname']);
  521. $_user['lastName'] = stripslashes($values['lastname']);
  522. $_user['mail'] = $values['email'];
  523. $_user['language'] = $values['language'];
  524. $_user['user_id'] = $user_id;
  525. $is_allowedCreateCourse = isset($values['status']) && $values['status'] == 1;
  526. $usersCanCreateCourse = (api_get_setting('allow_users_to_create_courses') == 'true');
  527. Session::write('_user', $_user);
  528. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  529. // Stats
  530. event_login();
  531. // last user login date is now
  532. $user_last_login_datetime = 0; // used as a unix timestamp it will correspond to : 1 1 1970
  533. Session::write('user_last_login_datetime', $user_last_login_datetime);
  534. $recipient_name = api_get_person_name($values['firstname'], $values['lastname']);
  535. $text_after_registration = '<p>'.get_lang('Dear', null, $_user['language']).' '.stripslashes(Security::remove_XSS($recipient_name)).',<br /><br />'.get_lang('PersonalSettings',null,$_user['language']).".</p>";
  536. $form_data = array(
  537. 'button' => Display::button('next', get_lang('Next', null, $_user['language']), array('class' => 'btn btn-primary btn-large')),
  538. 'message' => null,
  539. 'action' => api_get_path(WEB_PATH).'user_portal.php'
  540. );
  541. if (api_get_setting('allow_terms_conditions') == 'true' && $user_already_registered_show_terms) {
  542. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  543. } else {
  544. if (!empty ($values['email'])) {
  545. $text_after_registration.= '<p>'.get_lang('MailHasBeenSent',null,$_user['language']).'.</p>';
  546. }
  547. if ($is_allowedCreateCourse) {
  548. if ($usersCanCreateCourse) {
  549. $form_data['message'] = '<p>'. get_lang('NowGoCreateYourCourse', null, $_user['language']). "</p>";
  550. }
  551. $form_data['action'] = '../create_course/add_course.php';
  552. if (api_get_setting('course_validation') == 'true') {
  553. $form_data['button'] = Display::button('next', get_lang('CreateCourseRequest', null, $_user['language']), array('class' => 'btn btn-primary btn-large'));
  554. } else {
  555. $form_data['button'] = Display::button('next', get_lang('CourseCreate', null, $_user['language']), array('class' => 'btn btn-primary btn-large'));
  556. $form_data['go_button'] = '&nbsp;&nbsp;<a href="'.api_get_path(WEB_PATH).'index.php'.'">'.Display::span(get_lang('Next', null, $_user['language']), array('class' => 'btn btn-primary btn-large')).'</a>';
  557. }
  558. } else {
  559. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  560. $form_data['action'] = 'courses.php?action=subscribe';
  561. $form_data['message'] = '<p>'. get_lang('NowGoChooseYourCourses', null, $_user['language']). ".</p>";
  562. } else {
  563. $form_data['action'] = api_get_path(WEB_PATH).'user_portal.php';
  564. }
  565. $form_data['button'] = Display::button('next', get_lang('Next', null, $_user['language']), array('class' => 'btn btn-primary btn-large'));
  566. }
  567. }
  568. /*
  569. * Direct course link see #5299
  570. *
  571. * You can send to your students an URL like this
  572. * http://chamilodev.beeznest.com/main/auth/inscription.php?c=ABC&e=3
  573. * Where "c" is the course code and "e" is the exercise Id, after a successful
  574. * registration the user will be sent to the course or exercise
  575. *
  576. */
  577. $course_code_redirect = Session::read('course_redirect');
  578. if (!empty($course_code_redirect)) {
  579. $course_info = api_get_course_info($course_code_redirect);
  580. if (!empty($course_info)) {
  581. if (in_array($course_info['visibility'], array(COURSE_VISIBILITY_OPEN_PLATFORM, COURSE_VISIBILITY_OPEN_WORLD))) {
  582. $user_id = api_get_user_id();
  583. if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'])) {
  584. $form_data['action'] = $course_info['course_public_url'];
  585. $form_data['message'] = sprintf(get_lang('YouHaveBeenRegisteredToCourseX'), $course_info['title']);
  586. $form_data['button'] = Display::button(
  587. 'next',
  588. get_lang('GoToCourse', null, $_user['language']),
  589. array('class' => 'btn btn-primary btn-large')
  590. );
  591. $exercise_redirect = intval(Session::read('exercise_redirect'));
  592. // Specifiy course ID as the current context does not
  593. // hold a global $_course array
  594. $objExercise = new Exercise($course_info['real_id']);
  595. $result = $objExercise->read($exercise_redirect);
  596. if (!empty($exercise_redirect) && !empty($result)) {
  597. $form_data['action'] = api_get_path(WEB_CODE_PATH).'exercice/overview.php?exerciseId='.$exercise_redirect.'&cidReq='.$course_info['code'];
  598. $form_data['message'] .= '<br />'.get_lang('YouCanAccessTheExercise');
  599. $form_data['button'] = Display::button(
  600. 'next',
  601. get_lang('Go', null, $_user['language']),
  602. array('class' => 'btn btn-primary btn-large')
  603. );
  604. }
  605. if (!empty($form_data['action'])) {
  606. header('Location: '.$form_data['action']);
  607. exit;
  608. }
  609. }
  610. }
  611. }
  612. }
  613. $form_register = new FormValidator('form_register', 'post', $form_data['action']);
  614. if (!empty($form_data['message'])) {
  615. $form_register->addElement('html', $form_data['message'].'<br /><br />');
  616. }
  617. if ($usersCanCreateCourse) {
  618. $form_register->addElement('html', $form_data['button']);
  619. } else {
  620. $form_register->addElement('html', $form_data['go_button']);
  621. }
  622. $text_after_registration .= $form_register->return_form();
  623. // Just in case
  624. Session::erase('course_redirect');
  625. Session::erase('exercise_redirect');
  626. if (CustomPages::enabled()) {
  627. CustomPages::display(
  628. CustomPages::REGISTRATION_FEEDBACK,
  629. array('info' => $text_after_registration)
  630. );
  631. } else {
  632. Display :: display_header($tool_name);
  633. echo Display::page_header($tool_name);
  634. echo $content;
  635. echo $text_after_registration;
  636. Display :: display_footer();
  637. }
  638. } else {
  639. // Custom pages
  640. if (CustomPages::enabled()) {
  641. CustomPages::display(CustomPages::REGISTRATION, array('form' => $form));
  642. } else {
  643. Display :: display_header($tool_name);
  644. echo Display::page_header($tool_name);
  645. echo $content;
  646. $form->display();
  647. Display :: display_footer();
  648. }
  649. }