course_request.lib.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Course request manager.
  5. *
  6. * @package chamilo.library
  7. *
  8. * @author José Manuel Abuin Mosquera <chema@cesga.es>, 2010
  9. * @author Bruno Rubio Gayo <brubio@cesga.es>, 2010
  10. * Centro de Supercomputacion de Galicia (CESGA)
  11. * @author Ivan Tcholakov <ivantcholakov@gmail.com> (technical adaptation for Chamilo 1.8.8), 2010
  12. *
  13. * @package chamilo.library
  14. */
  15. class CourseRequestManager
  16. {
  17. /**
  18. * Checks whether a given course code has been already occupied.
  19. *
  20. * @param string $wanted_course_code the code to be checked
  21. *
  22. * @return bool
  23. * Returns TRUE if there is created:
  24. * - a course with the same code OR visual_code (visualcode).
  25. * - a course request with the same code as the given one, or
  26. * Othewise returns FALSE.
  27. */
  28. public static function course_code_exists($wanted_course_code)
  29. {
  30. if ($code_exists = CourseManager::course_code_exists($wanted_course_code)) {
  31. return $code_exists;
  32. }
  33. $table_course_request = Database::get_main_table(TABLE_MAIN_COURSE_REQUEST);
  34. $wanted_course_code = Database::escape_string($wanted_course_code);
  35. $sql = sprintf(
  36. 'SELECT COUNT(id) AS number FROM %s WHERE visual_code = "%s"',
  37. $table_course_request,
  38. $wanted_course_code
  39. );
  40. $result = Database::fetch_array(Database::query($sql));
  41. return $result['number'] > 0;
  42. }
  43. /**
  44. * Creates a new course request within the database.
  45. *
  46. * @param string $wanted_code the code for the created in the future course
  47. * @param string $title
  48. * @param string $description
  49. * @param string $category_code
  50. * @param string $course_language
  51. * @param string $objectives
  52. * @param string $target_audience
  53. * @param int $user_id
  54. *
  55. * @return mixed the database id of the newly created course request or FALSE on failure
  56. */
  57. public static function create_course_request(
  58. $wanted_code,
  59. $title,
  60. $description,
  61. $category_code,
  62. $course_language,
  63. $objectives,
  64. $target_audience,
  65. $user_id,
  66. $exemplary_content
  67. ) {
  68. $wanted_code = trim($wanted_code);
  69. $user_id = (int) $user_id;
  70. $exemplary_content = (bool) $exemplary_content ? 1 : 0;
  71. if ($wanted_code == '') {
  72. return false;
  73. }
  74. if (self::course_code_exists($wanted_code)) {
  75. return false;
  76. }
  77. if ($user_id <= 0) {
  78. return false;
  79. }
  80. $user_info = api_get_user_info($user_id);
  81. if (!is_array($user_info)) {
  82. return false;
  83. }
  84. $tutor_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $course_language);
  85. $request_date = api_get_utc_datetime();
  86. $status = COURSE_REQUEST_PENDING;
  87. $info = 0;
  88. $keys = AddCourse::define_course_keys($wanted_code, '');
  89. if (!count($keys)) {
  90. return false;
  91. }
  92. $visual_code = $keys['currentCourseCode'];
  93. $code = $keys['currentCourseId'];
  94. $db_name = isset($keys['currentCourseDbName']) ? $keys['currentCourseDbName'] : null;
  95. $directory = $keys['currentCourseRepository'];
  96. // @todo user entity
  97. $sql = sprintf(
  98. 'INSERT INTO %s (
  99. code, user_id, directory, db_name,
  100. course_language, title, description, category_code,
  101. tutor_name, visual_code, request_date,
  102. objetives, target_audience, status, info, exemplary_content)
  103. VALUES (
  104. "%s", "%s", "%s", "%s",
  105. "%s", "%s", "%s", "%s",
  106. "%s", "%s", "%s",
  107. "%s", "%s", "%s", "%s", "%s");',
  108. Database::get_main_table(TABLE_MAIN_COURSE_REQUEST),
  109. Database::escape_string($code),
  110. Database::escape_string($user_id),
  111. Database::escape_string($directory),
  112. Database::escape_string($db_name),
  113. Database::escape_string($course_language),
  114. Database::escape_string($title),
  115. Database::escape_string($description),
  116. Database::escape_string($category_code),
  117. Database::escape_string($tutor_name),
  118. Database::escape_string($visual_code),
  119. Database::escape_string($request_date),
  120. Database::escape_string($objectives),
  121. Database::escape_string($target_audience),
  122. Database::escape_string($status),
  123. Database::escape_string($info),
  124. Database::escape_string($exemplary_content)
  125. );
  126. $result_sql = Database::query($sql);
  127. if (!$result_sql) {
  128. return false;
  129. }
  130. $last_insert_id = Database::insert_id();
  131. // E-mail notifications.
  132. // E-mail language: The platform language seems to be the best choice.
  133. $email_language = api_get_setting('platformLanguage');
  134. $email_subject = sprintf(get_lang('CourseRequestEmailSubject', null, $email_language), '['.api_get_setting('siteName').']', $code);
  135. $email_body = get_lang('CourseRequestMailOpening', null, $email_language)."\n\n";
  136. $email_body .= get_lang('CourseName', null, $email_language).': '.$title."\n";
  137. $email_body .= get_lang('Fac', null, $email_language).': '.$category_code."\n";
  138. $email_body .= get_lang('CourseCode', null, $email_language).': '.$code."\n";
  139. $email_body .= get_lang('Professor', null, $email_language).': '.api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $email_language)."\n";
  140. $email_body .= get_lang('Email', null, $email_language).': '.$user_info['mail']."\n";
  141. $email_body .= get_lang('Description', null, $email_language).': '.$description."\n";
  142. $email_body .= get_lang('Objectives', null, $email_language).': '.$objectives."\n";
  143. $email_body .= get_lang('TargetAudience', null, $email_language).': '.$target_audience."\n";
  144. $email_body .= get_lang('Ln', null, $email_language).': '.$course_language."\n";
  145. $email_body .= get_lang('FillWithExemplaryContent', null, $email_language).': '.($exemplary_content ? get_lang('Yes', null, $email_language) : get_lang('No', null, $email_language))."\n";
  146. // Sending an e-mail to the platform administrator.
  147. $email_body_admin = $email_body;
  148. $email_body_admin .= "\n".get_lang('CourseRequestPageForApproval', null, $email_language).' '.api_get_path(WEB_CODE_PATH).'admin/course_request_edit.php?id='.$last_insert_id."\n";
  149. $email_body_admin .= "\n".get_lang('CourseRequestLegalNote', null, $email_language)."\n";
  150. $sender_name_teacher = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  151. $sender_email_teacher = $user_info['mail'];
  152. $recipient_name_admin = api_get_person_name(
  153. api_get_setting('administratorName'),
  154. api_get_setting('administratorSurname'),
  155. null,
  156. PERSON_NAME_EMAIL_ADDRESS
  157. );
  158. $recipient_email_admin = api_get_setting('emailAdministrator');
  159. $userInfo = api_get_user_info($user_id);
  160. $additionalParameters = [
  161. 'smsType' => SmsPlugin::NEW_COURSE_SUGGESTED_TEACHER,
  162. 'userId' => $user_id,
  163. 'userUsername' => $userInfo['username'],
  164. ];
  165. api_mail_html(
  166. $recipient_name_admin,
  167. $recipient_email_admin,
  168. $email_subject,
  169. $email_body_admin,
  170. $sender_name_teacher,
  171. $sender_email_teacher,
  172. null,
  173. null,
  174. null,
  175. $additionalParameters
  176. );
  177. // Sending an e-mail to the requestor.
  178. $email_body_teacher = get_lang('Dear', null, $email_language).' ';
  179. $email_body_teacher .= api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $email_language).",\n\n";
  180. $email_body_teacher .= $email_body;
  181. $email_body_teacher .= "\n".get_lang('Formula', null, $email_language)."\n";
  182. $email_body_teacher .= api_get_person_name(
  183. api_get_setting('administratorName'),
  184. api_get_setting('administratorSurname'),
  185. null,
  186. null,
  187. $email_language
  188. )."\n";
  189. $email_body_teacher .= get_lang('Manager', null, $email_language).' '.api_get_setting('siteName')."\n";
  190. $email_body_teacher .= get_lang('Phone', null, $email_language).': '.api_get_setting('administratorTelephone')."\n";
  191. $email_body_teacher .= get_lang('Email', null, $email_language).': '.api_get_setting('emailAdministrator', null, $email_language)."\n";
  192. $email_body_teacher .= "\n".get_lang('CourseRequestLegalNote', null, $email_language)."\n";
  193. // Swap the sender and the recipient.
  194. $sender_name_admin = $recipient_name_admin;
  195. $sender_email_admin = $recipient_email_admin;
  196. $recipient_name_teacher = $sender_name_teacher;
  197. $recipient_email_teacher = $sender_email_teacher;
  198. $additionalParameters = [
  199. 'smsType' => SmsPlugin::COURSE_OPENING_REQUEST_CODE_REGISTERED,
  200. 'userId' => $user_info['user_id'],
  201. 'courseCode' => $wanted_code,
  202. ];
  203. api_mail_html(
  204. $recipient_name_teacher,
  205. $recipient_email_teacher,
  206. $email_subject,
  207. $email_body_teacher,
  208. $sender_name_admin,
  209. $sender_email_admin,
  210. null,
  211. null,
  212. null,
  213. $additionalParameters
  214. );
  215. return $last_insert_id;
  216. }
  217. /**
  218. * Updates a given course request in the database.
  219. *
  220. * @param int $id the id (an integer number) of the corresponding database record
  221. * @param string $wanted_code the code for the created in the future course
  222. * @param string $title
  223. * @param string $description
  224. * @param string $category_code
  225. * @param string $course_language
  226. * @param string $objectives
  227. * @param string $target_audience
  228. * @param int $user_id
  229. *
  230. * @return bool returns TRUE on success or FALSE on failure
  231. */
  232. public static function update_course_request(
  233. $id,
  234. $wanted_code,
  235. $title,
  236. $description,
  237. $category_code,
  238. $course_language,
  239. $objectives,
  240. $target_audience,
  241. $user_id,
  242. $exemplary_content
  243. ) {
  244. $id = (int) $id;
  245. $wanted_code = trim($wanted_code);
  246. $user_id = (int) $user_id;
  247. $exemplary_content = (bool) $exemplary_content ? 1 : 0;
  248. if ($wanted_code == '') {
  249. return false;
  250. }
  251. if ($user_id <= 0) {
  252. return false;
  253. }
  254. // Retrieve request data
  255. $course_request_info = self::get_course_request_info($id);
  256. if (!is_array($course_request_info)) {
  257. return false;
  258. }
  259. $code = $wanted_code;
  260. $tutor_name = $course_request_info['tutor_name'];
  261. $directory = $course_request_info['directory'];
  262. $visual_code = $course_request_info['visual_code'];
  263. $request_date = $course_request_info['request_date'];
  264. $status = $course_request_info['status'];
  265. $info = $course_request_info['info'];
  266. if ($wanted_code != $course_request_info['code']) {
  267. if (self::course_code_exists($wanted_code)) {
  268. return false;
  269. }
  270. $keys = AddCourse::define_course_keys($wanted_code, '');
  271. if (count($keys)) {
  272. $visual_code = $keys['currentCourseCode'];
  273. $code = $keys['currentCourseId'];
  274. $db_name = $keys['currentCourseDbName'];
  275. $directory = $keys['currentCourseRepository'];
  276. } else {
  277. return false;
  278. }
  279. }
  280. if ($user_id != $course_request_info['code']) {
  281. $user_info = api_get_user_info($user_id);
  282. if (is_array($user_info)) {
  283. $tutor_name = api_get_person_name(
  284. $user_info['firstname'],
  285. $user_info['lastname'],
  286. null,
  287. null,
  288. $course_language
  289. );
  290. } else {
  291. $user_id = $course_request_info['code'];
  292. }
  293. }
  294. if ($course_language != $course_request_info['course_language']) {
  295. $user_info = api_get_user_info($user_id);
  296. if (is_array($user_info)) {
  297. $tutor_name = api_get_person_name(
  298. $user_info['firstname'],
  299. $user_info['lastname'],
  300. null,
  301. null,
  302. $course_language
  303. );
  304. }
  305. }
  306. // @todo use entity
  307. $sql = sprintf(
  308. 'UPDATE %s SET
  309. code = "%s", user_id = "%s", directory = "%s", db_name = "%s",
  310. course_language = "%s", title = "%s", description = "%s", category_code = "%s",
  311. tutor_name = "%s", visual_code = "%s", request_date = "%s",
  312. objetives = "%s", target_audience = "%s", status = "%s", info = "%s", exemplary_content = "%s"
  313. WHERE id = '.$id,
  314. Database::get_main_table(TABLE_MAIN_COURSE_REQUEST),
  315. Database::escape_string($code),
  316. intval($user_id),
  317. Database::escape_string($directory),
  318. Database::escape_string($db_name),
  319. Database::escape_string($course_language),
  320. Database::escape_string($title),
  321. Database::escape_string($description),
  322. Database::escape_string($category_code),
  323. Database::escape_string($tutor_name),
  324. Database::escape_string($visual_code),
  325. Database::escape_string($request_date),
  326. Database::escape_string($objectives),
  327. Database::escape_string($target_audience),
  328. Database::escape_string($status),
  329. Database::escape_string($info),
  330. Database::escape_string($exemplary_content)
  331. );
  332. $result_sql = Database::query($sql);
  333. return $result_sql !== false;
  334. }
  335. /**
  336. * Deletes a given course request.
  337. *
  338. * @param int $id the id (an integer number) of the corresponding database record
  339. *
  340. * @return bool returns TRUE on success or FALSE on failure
  341. */
  342. public static function delete_course_request($id)
  343. {
  344. $id = (int) $id;
  345. $sql = "DELETE FROM ".Database::get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  346. WHERE id = ".$id;
  347. $result = Database::query($sql);
  348. return $result !== false;
  349. }
  350. /**
  351. * Returns the number of course requests in the course_request table (optionally matching a status).
  352. *
  353. * @param int $status
  354. *
  355. * @return bool
  356. */
  357. public static function count_course_requests($status = null)
  358. {
  359. $course_table = Database::get_main_table(TABLE_MAIN_COURSE_REQUEST);
  360. if (is_null($status)) {
  361. $sql = "SELECT COUNT(id) AS number FROM ".$course_table;
  362. } else {
  363. $status = (int) $status;
  364. $sql = "SELECT COUNT(id) AS number FROM ".$course_table."
  365. WHERE status = ".$status;
  366. }
  367. $result = Database::fetch_array(Database::query($sql));
  368. if (is_array($result)) {
  369. return $result['number'];
  370. }
  371. return false;
  372. }
  373. /**
  374. * Gets all the information about a course request using its database id as an access key.
  375. *
  376. * @param int $id the id (an integer number) of the corresponding database record
  377. *
  378. * @return string|bool returns the requested data as an array or FALSE on failure
  379. */
  380. public static function get_course_request_info($id)
  381. {
  382. $id = (int) $id;
  383. $sql = "SELECT *
  384. FROM ".Database::get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  385. WHERE id = ".$id;
  386. $result = Database::query($sql);
  387. if (Database::num_rows($result) > 0) {
  388. return Database::fetch_array($result);
  389. }
  390. return false;
  391. }
  392. /**
  393. * Gets the code of a given course request using its database id as an access key.
  394. *
  395. * @param int $id the id (an integer number) of the corresponding database record
  396. *
  397. * @return string|bool returns the requested requested code or FALSE on failure
  398. */
  399. public static function get_course_request_code($id)
  400. {
  401. $id = (int) $id;
  402. $sql = "SELECT code
  403. FROM ".Database::get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  404. WHERE id = ".$id;
  405. $result = Database::query($sql);
  406. if (Database::num_rows($result) > 0) {
  407. $result_array = Database::fetch_array($result, 'NUM');
  408. if (is_array($result_array)) {
  409. return $result_array[0];
  410. }
  411. }
  412. return false;
  413. }
  414. /**
  415. * Accepts a given by its id course request. The requested course gets created immediately after the request acceptance.
  416. *
  417. * @param int $id the id (an integer number) of the corresponding database record
  418. *
  419. * @return string|bool returns the code of the newly created course or FALSE on failure
  420. */
  421. public static function accept_course_request($id)
  422. {
  423. $id = (int) $id;
  424. // Retrieve request's data
  425. $course_request_info = self::get_course_request_info($id);
  426. if (!is_array($course_request_info)) {
  427. return false;
  428. }
  429. // Make all the checks again before the new course creation.
  430. /*if (CourseManager::course_code_exists($wanted_code)) {
  431. return false;
  432. }*/
  433. $user_id = (int) $course_request_info['user_id'];
  434. if ($user_id <= 0) {
  435. return false;
  436. }
  437. $user_info = api_get_user_info($user_id);
  438. if (!is_array($user_info)) {
  439. return false;
  440. }
  441. // Create the requested course
  442. $params = [];
  443. $params['title'] = $course_request_info['title'];
  444. $params['course_category'] = $course_request_info['category_code'];
  445. $params['course_language'] = $course_request_info['course_language'];
  446. $params['exemplary_content'] = intval($course_request_info['exemplary_content']) > 0;
  447. $params['wanted_code'] = $course_request_info['code'];
  448. $params['user_id'] = $course_request_info['user_id'];
  449. $params['tutor_name'] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  450. $course_info = CourseManager::create_course($params);
  451. if (!empty($course_info)) {
  452. // Mark the request as accepted.
  453. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  454. SET status = ".COURSE_REQUEST_ACCEPTED."
  455. WHERE id = ".$id;
  456. Database::query($sql);
  457. // E-mail notification.
  458. // E-mail language: The platform language seems to be the best choice
  459. $email_language = api_get_setting('platformLanguage');
  460. $email_subject = sprintf(get_lang('CourseRequestAcceptedEmailSubject', null, $email_language), '['.api_get_setting('siteName').']', $course_info['code']);
  461. $email_body = get_lang('Dear', null, $email_language).' ';
  462. $email_body .= api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $email_language).",\n\n";
  463. $email_body .= sprintf(
  464. get_lang(
  465. 'CourseRequestAcceptedEmailText',
  466. null,
  467. $email_language
  468. ),
  469. $course_info['code'],
  470. $course_info['code'],
  471. api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/'
  472. )."\n";
  473. $email_body .= "\n".get_lang('Formula', null, $email_language)."\n";
  474. $email_body .= api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, null, $email_language)."\n";
  475. $email_body .= get_lang('Manager', null, $email_language).' '.api_get_setting('siteName')."\n";
  476. $email_body .= get_lang('Phone', null, $email_language).': '.api_get_setting('administratorTelephone')."\n";
  477. $email_body .= get_lang('Email', null, $email_language).': '.api_get_setting('emailAdministrator', null, $email_language)."\n";
  478. $email_body .= "\n".get_lang('CourseRequestLegalNote', null, $email_language)."\n";
  479. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  480. $sender_email = api_get_setting('emailAdministrator');
  481. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  482. $recipient_email = $user_info['mail'];
  483. $additionalParameters = [
  484. 'smsType' => SmsPlugin::COURSE_OPENING_REQUEST_CODE_APPROVED,
  485. 'userId' => $user_id,
  486. 'courseCode' => $course_info['code'],
  487. ];
  488. api_mail_html(
  489. $recipient_name,
  490. $recipient_email,
  491. $email_subject,
  492. $email_body,
  493. $sender_name,
  494. $sender_email,
  495. null,
  496. null,
  497. null,
  498. $additionalParameters
  499. );
  500. return $course_info['code'];
  501. }
  502. return false;
  503. }
  504. /**
  505. * Rejects a given course request.
  506. *
  507. * @param int $id the id (an integer number) of the corresponding database record
  508. *
  509. * @return bool returns TRUE on success or FALSE on failure
  510. */
  511. public static function reject_course_request($id)
  512. {
  513. $id = (int) $id;
  514. // Retrieve request's data
  515. $course_request_info = self::get_course_request_info($id);
  516. if (!is_array($course_request_info)) {
  517. return false;
  518. }
  519. $user_id = intval($course_request_info['user_id']);
  520. if ($user_id <= 0) {
  521. return false;
  522. }
  523. $user_info = api_get_user_info($user_id);
  524. if (!is_array($user_info)) {
  525. return false;
  526. }
  527. $code = $course_request_info['code'];
  528. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  529. SET status = ".COURSE_REQUEST_REJECTED."
  530. WHERE id = ".$id;
  531. if (Database::query($sql) === false) {
  532. return false;
  533. }
  534. // E-mail notification.
  535. // E-mail language: The platform language seems to be the best choice.
  536. $email_language = api_get_setting('platformLanguage');
  537. $email_subject = sprintf(get_lang('CourseRequestRejectedEmailSubject', null, $email_language), '['.api_get_setting('siteName').']', $code);
  538. $email_body = get_lang('Dear', null, $email_language).' ';
  539. $email_body .= api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $email_language).",\n\n";
  540. $email_body .= sprintf(get_lang('CourseRequestRejectedEmailText', null, $email_language), $code)."\n";
  541. $email_body .= "\n".get_lang('Formula', null, $email_language)."\n";
  542. $email_body .= api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, null, $email_language)."\n";
  543. $email_body .= get_lang('Manager', null, $email_language).' '.api_get_setting('siteName')."\n";
  544. $email_body .= get_lang('Phone', null, $email_language).': '.api_get_setting('administratorTelephone')."\n";
  545. $email_body .= get_lang('Email', null, $email_language).': '.api_get_setting('emailAdministrator', null, $email_language)."\n";
  546. $email_body .= "\n".get_lang('CourseRequestLegalNote', null, $email_language)."\n";
  547. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  548. $sender_email = api_get_setting('emailAdministrator');
  549. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  550. $recipient_email = $user_info['mail'];
  551. $extra_headers = 'Bcc: '.$sender_email;
  552. $additionalParameters = [
  553. 'smsType' => SmsPlugin::COURSE_OPENING_REQUEST_CODE_REJECTED,
  554. 'userId' => $user_id,
  555. 'courseCode' => $code,
  556. ];
  557. api_mail_html(
  558. $recipient_name,
  559. $recipient_email,
  560. $email_subject,
  561. $email_body,
  562. $sender_name,
  563. $sender_email,
  564. null,
  565. null,
  566. null,
  567. $additionalParameters
  568. );
  569. return true;
  570. }
  571. /**
  572. * Asks the author (through e-mail) for additional information about the given course request.
  573. *
  574. * @param int $id the database primary id of the given request
  575. *
  576. * @return bool returns TRUE on success or FALSE on failure
  577. */
  578. public static function ask_for_additional_info($id)
  579. {
  580. $id = (int) $id;
  581. // Retrieve request's data
  582. $course_request_info = self::get_course_request_info($id);
  583. if (!is_array($course_request_info)) {
  584. return false;
  585. }
  586. $user_id = intval($course_request_info['user_id']);
  587. if ($user_id <= 0) {
  588. return false;
  589. }
  590. $user_info = api_get_user_info($user_id);
  591. if (!is_array($user_info)) {
  592. return false;
  593. }
  594. $code = $course_request_info['code'];
  595. $info = intval($course_request_info['info']);
  596. // Error is to be returned on a repeated attempt for asking additional information.
  597. if (!empty($info)) {
  598. return false;
  599. }
  600. // E-mail notification.
  601. // E-mail language: The platform language seems to be the best choice.
  602. $email_language = api_get_setting('platformLanguage');
  603. $email_subject = sprintf(get_lang('CourseRequestAskInfoEmailSubject', null, $email_language), '['.api_get_setting('siteName').']', $code);
  604. $email_body = get_lang('Dear', null, $email_language).' ';
  605. $email_body .= api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $email_language).",\n\n";
  606. $email_body .= sprintf(get_lang('CourseRequestAskInfoEmailText', null, $email_language), $code)."\n";
  607. $email_body .= "\n".get_lang('Formula', null, $email_language)."\n";
  608. $email_body .= api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n";
  609. $email_body .= get_lang('Manager', null, $email_language).' '.api_get_setting('siteName')."\n";
  610. $email_body .= get_lang('Phone', null, $email_language).': '.api_get_setting('administratorTelephone')."\n";
  611. $email_body .= get_lang('Email', null, $email_language).': '.api_get_setting('emailAdministrator')."\n";
  612. $email_body .= "\n".get_lang('CourseRequestLegalNote', null, $email_language)."\n";
  613. $sender_name = api_get_person_name(
  614. api_get_setting('administratorName'),
  615. api_get_setting('administratorSurname'),
  616. null,
  617. PERSON_NAME_EMAIL_ADDRESS
  618. );
  619. $sender_email = api_get_setting('emailAdministrator');
  620. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  621. $recipient_email = $user_info['mail'];
  622. $extra_headers = 'Bcc: '.$sender_email;
  623. $additionalParameters = [
  624. 'smsType' => SmsPlugin::COURSE_OPENING_REQUEST_CODE,
  625. 'userId' => $user_id,
  626. 'courseCode' => $code,
  627. ];
  628. $result = api_mail_html(
  629. $recipient_name,
  630. $recipient_email,
  631. $email_subject,
  632. $email_body,
  633. $sender_name,
  634. $sender_email,
  635. null,
  636. null,
  637. null,
  638. $additionalParameters
  639. );
  640. if (!$result) {
  641. return false;
  642. }
  643. // Marking the fact that additional information about the request has been asked.
  644. $sql = "UPDATE ".Database::get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  645. SET info = 1 WHERE id = ".$id;
  646. $result = Database::query($sql) !== false;
  647. return $result;
  648. }
  649. /**
  650. * Checks whether additional information about the given course request has been asked.
  651. *
  652. * @param int $id the database primary id of the given request
  653. *
  654. * @return bool returns TRUE if additional information has been asked or FALSE otherwise
  655. */
  656. public static function additional_info_asked($id)
  657. {
  658. $id = (int) $id;
  659. $sql = "SELECT id FROM ".Database::get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  660. WHERE (id = ".$id." AND info > 0)";
  661. $result = Database::num_rows(Database::query($sql));
  662. return !empty($result);
  663. }
  664. }