course_request.lib.php 30 KB

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