course_request.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 string
  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. $db_name = $course_request_info['db_name'];
  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($user_info['firstname'], $user_info['lastname'], null, null, $course_language);
  267. } else {
  268. $user_id = $course_request_info['code'];
  269. }
  270. }
  271. if ($course_language != $course_request_info['course_language']) {
  272. $user_info = api_get_user_info($user_id);
  273. if (is_array($user_info)) {
  274. $tutor_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $course_language);
  275. }
  276. }
  277. $sql = sprintf('UPDATE %s SET
  278. code = "%s", user_id = "%s", directory = "%s", db_name = "%s",
  279. course_language = "%s", title = "%s", description = "%s", category_code = "%s",
  280. tutor_name = "%s", visual_code = "%s", request_date = "%s",
  281. objetives = "%s", target_audience = "%s", status = "%s", info = "%s", exemplary_content = "%s"
  282. WHERE id = '.$id, Database::get_main_table(TABLE_MAIN_COURSE_REQUEST),
  283. Database::escape_string($code),
  284. intval($user_id),
  285. Database::escape_string($directory),
  286. Database::escape_string($db_name),
  287. Database::escape_string($course_language),
  288. Database::escape_string($title),
  289. Database::escape_string($description),
  290. Database::escape_string($category_code),
  291. Database::escape_string($tutor_name),
  292. Database::escape_string($visual_code),
  293. Database::escape_string($request_date),
  294. Database::escape_string($objetives),
  295. Database::escape_string($target_audience),
  296. Database::escape_string($status),
  297. Database::escape_string($info),
  298. Database::escape_string($exemplary_content)
  299. );
  300. $result_sql = Database::query($sql);
  301. return $result_sql !== false;
  302. }
  303. /**
  304. * Deletes a given course request.
  305. * @param int/string $id The id (an integer number) of the corresponding database record.
  306. * @return bool Returns TRUE on success or FALSE on failure.
  307. */
  308. public static function delete_course_request($id)
  309. {
  310. $id = (int)$id;
  311. $sql = "DELETE FROM ".Database :: get_main_table(TABLE_MAIN_COURSE_REQUEST)." WHERE id = ".$id;
  312. $result = Database::query($sql);
  313. return $result !== false;
  314. }
  315. /**
  316. * @param null $status
  317. * @return bool
  318. */
  319. public static function count_course_requests($status = null)
  320. {
  321. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE_REQUEST);
  322. if (is_null($status)) {
  323. $sql = "SELECT COUNT(id) AS number FROM ".$course_table;
  324. } else {
  325. $status = (int)$status;
  326. $sql = "SELECT COUNT(id) AS number FROM ".$course_table." WHERE status = ".$status;
  327. }
  328. $result = Database::fetch_array(Database::query($sql));
  329. if (is_array($result)) {
  330. return $result['number'];
  331. }
  332. return false;
  333. }
  334. /**
  335. * Gets all the information about a course request using its database id as an access key.
  336. * @param int/string $id The id (an integer number) of the corresponding database record.
  337. * @return array/bool Returns the requested data as an array or FALSE on failure.
  338. */
  339. public static function get_course_request_info($id)
  340. {
  341. $id = (int)$id;
  342. $sql = "SELECT *
  343. FROM ".Database :: get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  344. WHERE id = ".$id;
  345. $result = Database::query($sql);
  346. if (Database::num_rows($result) > 0) {
  347. return Database::fetch_array($result);
  348. }
  349. return false;
  350. }
  351. /**
  352. * Gets the code of a given course request using its database id as an access key.
  353. * @param int/string $id The id (an integer number) of the corresponding database record.
  354. * @return string/bool Returns the requested requested code or FALSE on failure.
  355. */
  356. public static function get_course_request_code($id)
  357. {
  358. $id = (int)$id;
  359. $sql = "SELECT code
  360. FROM ".Database :: get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  361. WHERE id = ".$id;
  362. $result = Database::query($sql);
  363. if (Database::num_rows($result) > 0) {
  364. $result_array = Database::fetch_array($result, 'NUM');
  365. if (is_array($result_array)) {
  366. return $result_array[0];
  367. }
  368. }
  369. return false;
  370. }
  371. /**
  372. * Accepts a given by its id course request. The requested course gets created immediately after the request acceptance.
  373. * @param int/string $id The id (an integer number) of the corresponding database record.
  374. * @return string/bool Returns the code of the newly created course or FALSE on failure.
  375. */
  376. public static function accept_course_request($id)
  377. {
  378. $id = (int)$id;
  379. // Retrieve request's data
  380. $course_request_info = self::get_course_request_info($id);
  381. if (!is_array($course_request_info)) {
  382. return false;
  383. }
  384. // Make all the checks again before the new course creation.
  385. /*if (CourseManager::course_code_exists($wanted_code)) {
  386. return false;
  387. }*/
  388. $user_id = (int)$course_request_info['user_id'];
  389. if ($user_id <= 0) {
  390. return false;
  391. }
  392. $user_info = api_get_user_info($user_id);
  393. if (!is_array($user_info)) {
  394. return false;
  395. }
  396. // Create the requested course
  397. $params = array();
  398. $params['title'] = $course_request_info['title'];
  399. $params['course_category'] = $course_request_info['category_code'];
  400. $params['course_language'] = $course_request_info['course_language'];
  401. $params['exemplary_content'] = intval($course_request_info['exemplary_content']) > 0;
  402. $params['wanted_code'] = $course_request_info['code'];
  403. $params['user_id'] = $course_request_info['user_id'];
  404. $params['tutor_name'] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  405. $course_info = CourseManager::create_course($params);
  406. if (!empty($course_info)) {
  407. // Mark the request as accepted.
  408. $sql = "UPDATE ".Database :: get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  409. SET status = ".COURSE_REQUEST_ACCEPTED."
  410. WHERE id = ".$id;
  411. Database::query($sql);
  412. // E-mail notification.
  413. // E-mail language: The platform language seems to be the best choice
  414. $email_language = api_get_setting('platformLanguage');
  415. $email_subject = sprintf(get_lang('CourseRequestAcceptedEmailSubject', null, $email_language), '['.api_get_setting('siteName').']', $course_info['code']);
  416. $email_body = get_lang('Dear', null, $email_language).' ';
  417. $email_body .= api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $email_language).",\n\n";
  418. $email_body .= sprintf(get_lang('CourseRequestAcceptedEmailText', null, $email_language), $wanted_code, $course_info['code'], api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/')."\n";
  419. $email_body .= "\n".get_lang('Formula', null, $email_language)."\n";
  420. $email_body .= api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, null, $email_language)."\n";
  421. $email_body .= get_lang('Manager', null, $email_language).' '.api_get_setting('siteName')."\n";
  422. $email_body .= get_lang('Phone', null, $email_language).': '.api_get_setting('administratorTelephone')."\n";
  423. $email_body .= get_lang('Email', null, $email_language).': '.api_get_setting('emailAdministrator', null, $email_language)."\n";
  424. $email_body .= "\n".get_lang('CourseRequestLegalNote', null, $email_language)."\n";
  425. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  426. $sender_email = api_get_setting('emailAdministrator');
  427. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  428. $recipient_email = $user_info['mail'];
  429. $extra_headers = 'Bcc: '.$sender_email;
  430. $additionalParameters = array(
  431. 'smsType' => SmsPlugin::COURSE_OPENING_REQUEST_CODE_APPROVED,
  432. 'userId' => $user_id,
  433. 'courseCode' => $course_info['code']
  434. );
  435. api_mail_html(
  436. $recipient_name,
  437. $recipient_email,
  438. $email_subject,
  439. $email_body,
  440. $sender_name,
  441. $sender_email,
  442. null,
  443. null,
  444. null,
  445. $additionalParameters
  446. );
  447. return $course_info['code'];
  448. }
  449. return false;
  450. }
  451. /**
  452. * Rejects a given course request.
  453. * @param int/string $id The id (an integer number) of the corresponding database record.
  454. * @return bool Returns TRUE on success or FALSE on failure.
  455. */
  456. public static function reject_course_request($id)
  457. {
  458. $id = (int)$id;
  459. // Retrieve request's data
  460. $course_request_info = self::get_course_request_info($id);
  461. if (!is_array($course_request_info)) {
  462. return false;
  463. }
  464. $user_id = intval($course_request_info['user_id']);
  465. if ($user_id <= 0) {
  466. return false;
  467. }
  468. $user_info = api_get_user_info($user_id);
  469. if (!is_array($user_info)) {
  470. return false;
  471. }
  472. $code = $course_request_info['code'];
  473. $sql = "UPDATE ".Database :: get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  474. SET status = ".COURSE_REQUEST_REJECTED."
  475. WHERE id = ".$id;
  476. if (Database::query($sql) === false) {
  477. return false;
  478. }
  479. // E-mail notification.
  480. // E-mail language: The platform language seems to be the best choice.
  481. $email_language = api_get_setting('platformLanguage');
  482. $email_subject = sprintf(get_lang('CourseRequestRejectedEmailSubject', null, $email_language), '['.api_get_setting('siteName').']', $code);
  483. $email_body = get_lang('Dear', null, $email_language).' ';
  484. $email_body .= api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $email_language).",\n\n";
  485. $email_body .= sprintf(get_lang('CourseRequestRejectedEmailText', null, $email_language), $code)."\n";
  486. $email_body .= "\n".get_lang('Formula', null, $email_language)."\n";
  487. $email_body .= api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, null, $email_language)."\n";
  488. $email_body .= get_lang('Manager', null, $email_language).' '.api_get_setting('siteName')."\n";
  489. $email_body .= get_lang('Phone', null, $email_language).': '.api_get_setting('administratorTelephone')."\n";
  490. $email_body .= get_lang('Email', null, $email_language).': '.api_get_setting('emailAdministrator', null, $email_language)."\n";
  491. $email_body .= "\n".get_lang('CourseRequestLegalNote', null, $email_language)."\n";
  492. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  493. $sender_email = api_get_setting('emailAdministrator');
  494. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  495. $recipient_email = $user_info['mail'];
  496. $extra_headers = 'Bcc: '.$sender_email;
  497. $additionalParameters = array(
  498. 'smsType' => SmsPlugin::COURSE_OPENING_REQUEST_CODE_REJECTED,
  499. 'userId' => $user_id,
  500. 'courseCode' => $code
  501. );
  502. api_mail_html(
  503. $recipient_name,
  504. $recipient_email,
  505. $email_subject,
  506. $email_body,
  507. $sender_name,
  508. $sender_email,
  509. null,
  510. null,
  511. null,
  512. $additionalParameters
  513. );
  514. return true;
  515. }
  516. /**
  517. * Asks the author (through e-mail) for additional information about the given course request.
  518. * @param int/string $id The database primary id of the given request.
  519. * @return bool Returns TRUE on success or FALSE on failure.
  520. */
  521. public static function ask_for_additional_info($id)
  522. {
  523. $id = (int)$id;
  524. // Retrieve request's data
  525. $course_request_info = self::get_course_request_info($id);
  526. if (!is_array($course_request_info)) {
  527. return false;
  528. }
  529. $user_id = intval($course_request_info['user_id']);
  530. if ($user_id <= 0) {
  531. return false;
  532. }
  533. $user_info = api_get_user_info($user_id);
  534. if (!is_array($user_info)) {
  535. return false;
  536. }
  537. $code = $course_request_info['code'];
  538. $info = intval($course_request_info['info']);
  539. // Error is to be returned on a repeated attempt for asking additional information.
  540. if (!empty($info)) {
  541. return false;
  542. }
  543. // E-mail notification.
  544. // E-mail language: The platform language seems to be the best choice.
  545. $email_language = api_get_setting('platformLanguage');
  546. $email_subject = sprintf(get_lang('CourseRequestAskInfoEmailSubject', null, $email_language), '['.api_get_setting('siteName').']', $code);
  547. $email_body = get_lang('Dear', null, $email_language).' ';
  548. $email_body .= api_get_person_name($user_info['firstname'], $user_info['lastname'], null, null, $email_language).",\n\n";
  549. $email_body .= sprintf(get_lang('CourseRequestAskInfoEmailText', null, $email_language), $code)."\n";
  550. $email_body .= "\n".get_lang('Formula', null, $email_language)."\n";
  551. $email_body .= api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n";
  552. $email_body .= get_lang('Manager', null, $email_language).' '.api_get_setting('siteName')."\n";
  553. $email_body .= get_lang('Phone', null, $email_language).': '.api_get_setting('administratorTelephone')."\n";
  554. $email_body .= get_lang('Email', null, $email_language).': '.api_get_setting('emailAdministrator')."\n";
  555. $email_body .= "\n".get_lang('CourseRequestLegalNote', null, $email_language)."\n";
  556. $sender_name = api_get_person_name(
  557. api_get_setting('administratorName'),
  558. api_get_setting('administratorSurname'),
  559. null,
  560. PERSON_NAME_EMAIL_ADDRESS
  561. );
  562. $sender_email = api_get_setting('emailAdministrator');
  563. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  564. $recipient_email = $user_info['mail'];
  565. $extra_headers = 'Bcc: '.$sender_email;
  566. $additionalParameters = array(
  567. 'smsType' => SmsPlugin::COURSE_OPENING_REQUEST_CODE,
  568. 'userId' => $user_id,
  569. 'courseCode' => $code
  570. );
  571. $result = api_mail_html(
  572. $recipient_name,
  573. $recipient_email,
  574. $email_subject,
  575. $email_body,
  576. $sender_name,
  577. $sender_email,
  578. null,
  579. null,
  580. null,
  581. $additionalParameters
  582. );
  583. if (!$result) {
  584. return false;
  585. }
  586. // Marking the fact that additional information about the request has been asked.
  587. $sql = "UPDATE ".Database :: get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  588. SET info = 1 WHERE id = ".$id;
  589. $result = Database::query($sql) !== false;
  590. return $result;
  591. }
  592. /**
  593. * Checks whether additional information about the given course request has been asked.
  594. * @param int/string $id The database primary id of the given request.
  595. * @return bool Returns TRUE if additional information has been asked or FALSE otherwise.
  596. */
  597. public static function additional_info_asked($id)
  598. {
  599. $id = (int)$id;
  600. $sql = "SELECT id FROM ".Database :: get_main_table(TABLE_MAIN_COURSE_REQUEST)."
  601. WHERE (id = ".$id." AND info > 0)";
  602. $result = Database::num_rows(Database::query($sql));
  603. return !empty($result);
  604. }
  605. }