course_request.lib.php 29 KB

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