course_request.lib.php 28 KB

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