course_request.lib.php 29 KB

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