CourseLegalPlugin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class CourseLegalPlugin.
  5. */
  6. class CourseLegalPlugin extends Plugin
  7. {
  8. public $isCoursePlugin = true;
  9. // When creating a new course this settings are added to the course
  10. public $course_settings = [
  11. [
  12. 'name' => 'courselegal',
  13. 'type' => 'text',
  14. ],
  15. ];
  16. protected function __construct()
  17. {
  18. parent::__construct(
  19. '0.1',
  20. 'Julio Montoya',
  21. [
  22. 'tool_enable' => 'boolean',
  23. ]
  24. );
  25. }
  26. /**
  27. * @return CourseLegalPlugin
  28. */
  29. public static function create()
  30. {
  31. static $result = null;
  32. return $result ? $result : $result = new self();
  33. }
  34. /**
  35. * @return string
  36. */
  37. public function getTeacherLink()
  38. {
  39. $link = null;
  40. if (api_is_allowed_to_edit()) {
  41. $url = api_get_path(WEB_PLUGIN_PATH).'courselegal/start.php?'.api_get_cidreq();
  42. $link = Display::url(
  43. $this->get_lang('CourseLegal'),
  44. $url,
  45. ['class' => 'btn']
  46. );
  47. }
  48. return $link;
  49. }
  50. /**
  51. * @param int $userId
  52. * @param int $courseId
  53. * @param int $sessionId
  54. *
  55. * @return array
  56. */
  57. public function getUserAcceptedLegal($userId, $courseId, $sessionId)
  58. {
  59. $userId = intval($userId);
  60. $courseId = intval($courseId);
  61. $sessionId = intval($sessionId);
  62. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  63. $sql = "SELECT *
  64. FROM $table
  65. WHERE user_id = $userId AND c_id = $courseId AND session_id = $sessionId";
  66. $result = Database::query($sql);
  67. $data = [];
  68. if (Database::num_rows($result) > 0) {
  69. $data = Database::fetch_array($result, 'ASSOC');
  70. }
  71. return $data;
  72. }
  73. /**
  74. * @param int $userId
  75. * @param string $courseCode
  76. * @param int $sessionId
  77. *
  78. * @return bool
  79. */
  80. public function isUserAcceptedLegal($userId, $courseCode, $sessionId)
  81. {
  82. $courseInfo = api_get_course_info($courseCode);
  83. $courseId = $courseInfo['real_id'];
  84. $result = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  85. if (!empty($result)) {
  86. if ($result['mail_agreement'] == 1 &&
  87. $result['web_agreement'] == 1
  88. ) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. /**
  95. * @param int $userId
  96. * @param int $courseCode
  97. * @param int $sessionId
  98. * @param bool $sendEmail Optional. Indicate whether the mail must be sent. Default is true
  99. *
  100. * @return mixed
  101. */
  102. public function saveUserLegal($userId, $courseCode, $sessionId, $sendEmail = true)
  103. {
  104. $courseInfo = api_get_course_info($courseCode);
  105. $courseId = $courseInfo['real_id'];
  106. $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  107. $id = false;
  108. if (empty($data)) {
  109. $table = Database::get_main_table(
  110. 'session_rel_course_rel_user_legal'
  111. );
  112. $uniqueId = api_get_unique_id();
  113. $values = [
  114. 'user_id' => $userId,
  115. 'c_id' => $courseId,
  116. 'session_id' => $sessionId,
  117. 'web_agreement' => 1,
  118. 'web_agreement_date' => api_get_utc_datetime(),
  119. 'mail_agreement_link' => $uniqueId,
  120. ];
  121. $id = Database::insert($table, $values);
  122. if ($sendEmail) {
  123. $this->sendMailLink($uniqueId, $userId, $courseId, $sessionId);
  124. }
  125. }
  126. return $id;
  127. }
  128. /**
  129. * @param int $userId
  130. * @param int $courseId
  131. * @param int $sessionId
  132. */
  133. public function updateMailAgreementLink($userId, $courseId, $sessionId)
  134. {
  135. $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  136. if (!empty($data)) {
  137. $table = Database::get_main_table(
  138. 'session_rel_course_rel_user_legal'
  139. );
  140. $uniqueId = api_get_unique_id();
  141. Database::update(
  142. $table,
  143. ['mail_agreement_link' => $uniqueId],
  144. ['id = ? ' => [$data['id']]]
  145. );
  146. $this->sendMailLink($uniqueId, $userId, $courseId, $sessionId);
  147. }
  148. }
  149. /**
  150. * @param int $userId
  151. * @param int $courseId
  152. * @param int $sessionId
  153. */
  154. public function deleteUserAgreement($userId, $courseId, $sessionId)
  155. {
  156. $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  157. if (!empty($data)) {
  158. $table = Database::get_main_table(
  159. 'session_rel_course_rel_user_legal'
  160. );
  161. Database::delete(
  162. $table,
  163. ['id = ? ' => [$data['id']]]
  164. );
  165. }
  166. }
  167. /**
  168. * @param string $uniqueId
  169. * @param int $userId
  170. * @param int $courseId
  171. * @param int $sessionId
  172. */
  173. public function sendMailLink($uniqueId, $userId, $courseId, $sessionId)
  174. {
  175. $courseInfo = api_get_course_info_by_id($courseId);
  176. $courseCode = $courseInfo['code'];
  177. $url = api_get_path(WEB_CODE_PATH).'course_info/legal.php?web_agreement_link='.$uniqueId.'&course_code='.Security::remove_XSS($courseCode).'&session_id='.$sessionId;
  178. $courseUrl = Display::url($url, $url);
  179. $sessionInfo = api_get_session_info($sessionId);
  180. $sesstionTitle = null;
  181. if (!empty($sessionInfo)) {
  182. $sesstionTitle = ' ('.$sessionInfo['name'].')';
  183. }
  184. $courseTitle = $courseInfo['title'].$sesstionTitle;
  185. $subject = $this->get_lang("MailAgreement");
  186. $message = sprintf($this->get_lang("MailAgreementWasSentWithClickX"), $courseTitle, $courseUrl);
  187. MessageManager::send_message_simple($userId, $subject, $message);
  188. }
  189. /**
  190. * @param string $link
  191. * @param int $userId
  192. * @param int $courseId
  193. * @param int $sessionId
  194. *
  195. * @return bool
  196. */
  197. public function saveUserMailLegal($link, $userId, $courseId, $sessionId)
  198. {
  199. $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  200. if (empty($data)) {
  201. return null;
  202. }
  203. if ($data['mail_agreement_link'] == $link) {
  204. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  205. $id = $data['id'];
  206. $values = [
  207. 'mail_agreement' => 1,
  208. 'mail_agreement_date' => api_get_utc_datetime(),
  209. ];
  210. Database::update($table, $values, ['id = ?' => [$id]]);
  211. }
  212. }
  213. /**
  214. * @param int $courseId
  215. * @param int $sessionId
  216. * @param string $filePath
  217. */
  218. public function warnUsersByEmail($courseId, $sessionId, $filePath = null)
  219. {
  220. $courseInfo = api_get_course_info_by_id($courseId);
  221. $courseCode = $courseInfo['code'];
  222. if (empty($sessionId)) {
  223. $students = CourseManager::get_student_list_from_course_code($courseCode, false);
  224. } else {
  225. $students = CourseManager::get_student_list_from_course_code($courseCode, true, $sessionId);
  226. }
  227. $url = api_get_course_url($courseCode, $sessionId);
  228. $url = Display::url($url, $url);
  229. $subject = $this->get_lang("AgreementUpdated");
  230. $message = sprintf($this->get_lang("AgreementWasUpdatedClickHere"), $url);
  231. $dataFile = [];
  232. if (!empty($filePath)) {
  233. $dataFile = [
  234. 'path' => $filePath,
  235. 'filename' => basename($filePath),
  236. ];
  237. $message = sprintf($this->get_lang("AgreementWasUpdatedClickHere"), $url)." \n";
  238. $message .= $this->get_lang("TheAgreementIsAttachedInThisEmail");
  239. }
  240. if (!empty($students)) {
  241. foreach ($students as $student) {
  242. $userInfo = api_get_user_info($student['user_id']);
  243. api_mail_html(
  244. $userInfo['complete_name'],
  245. $userInfo['email'],
  246. $subject,
  247. $message,
  248. null,
  249. null,
  250. null,
  251. $dataFile
  252. );
  253. //MessageManager::send_message_simple($student['user_id'], $subject, $message);
  254. }
  255. }
  256. }
  257. /**
  258. * @param int $courseId
  259. * @param int $sessionId
  260. * @param string $order
  261. *
  262. * @return array
  263. */
  264. public function getUserAgreementList($courseId, $sessionId, $order = null)
  265. {
  266. $courseId = intval($courseId);
  267. $sessionId = intval($sessionId);
  268. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  269. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  270. $sql = "SELECT *
  271. FROM $table s INNER JOIN $userTable u
  272. ON u.user_id = s.user_id
  273. WHERE c_id = $courseId AND session_id = $sessionId ";
  274. if (!empty($order)) {
  275. $sql .= $order;
  276. }
  277. $result = Database::query($sql);
  278. $data = [];
  279. if (Database::num_rows($result) > 0) {
  280. $data = Database::store_result($result, 'ASSOC');
  281. }
  282. return $data;
  283. }
  284. /**
  285. * @param int $courseId
  286. * @param int $sessionId
  287. */
  288. public function removePreviousAgreements($courseId, $sessionId)
  289. {
  290. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  291. $sessionId = intval($sessionId);
  292. $courseId = intval($courseId);
  293. $sql = "DELETE FROM $table
  294. WHERE c_id = '$courseId' AND session_id = $sessionId ";
  295. Database::query($sql);
  296. }
  297. /**
  298. * @param array $values
  299. * @param array $file $_FILES['uploaded_file']
  300. * @param bool $deleteFile
  301. */
  302. public function save($values, $file = [], $deleteFile = false)
  303. {
  304. $table = Database::get_main_table('session_rel_course_legal');
  305. $courseId = $values['c_id'];
  306. $sessionId = $values['session_id'];
  307. $conditions = [
  308. 'c_id' => $courseId,
  309. 'session_id' => $sessionId,
  310. ];
  311. $course = api_get_course_info_by_id($courseId);
  312. $legalData = $this->getData($courseId, $sessionId);
  313. $coursePath = api_get_path(SYS_COURSE_PATH).$course['directory'].'/courselegal';
  314. $uploadResult = $coursePath.'/'.$legalData['filename'];
  315. if (!is_dir($coursePath)) {
  316. mkdir($coursePath, api_get_permissions_for_new_directories());
  317. }
  318. $uploadOk = process_uploaded_file($file, false);
  319. $fileName = null;
  320. if ($uploadOk) {
  321. $uploadResult = handle_uploaded_document(
  322. $course,
  323. $file,
  324. $coursePath,
  325. '/',
  326. api_get_user_id(),
  327. api_get_group_id(),
  328. null,
  329. false,
  330. false,
  331. false,
  332. true
  333. );
  334. if ($uploadResult) {
  335. $fileName = basename($uploadResult);
  336. // Delete old one if exists.
  337. if ($legalData) {
  338. if (!empty($legalData['filename'])) {
  339. $fileToDelete = $coursePath.'/'.$legalData['filename'];
  340. if (file_exists($fileToDelete)) {
  341. unlink($fileToDelete);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. $conditions['content'] = $values['content'];
  348. $conditions['filename'] = $fileName;
  349. if (empty($legalData)) {
  350. $id = Database::insert($table, $conditions);
  351. } else {
  352. $id = $legalData['id'];
  353. $updateParams = [
  354. 'content' => $values['content'],
  355. ];
  356. if (!empty($fileName)) {
  357. $updateParams['filename'] = $fileName;
  358. }
  359. Database::update(
  360. $table,
  361. $updateParams,
  362. ['id = ? ' => $id]
  363. );
  364. }
  365. if ($deleteFile) {
  366. Database::update(
  367. $table,
  368. ['filename' => ''],
  369. ['id = ? ' => $id]
  370. );
  371. if (!empty($legalData['filename'])) {
  372. $fileToDelete = $coursePath.'/'.$legalData['filename'];
  373. if (file_exists($fileToDelete)) {
  374. unlink($fileToDelete);
  375. }
  376. }
  377. }
  378. if (isset($values['remove_previous_agreements']) &&
  379. !empty($values['remove_previous_agreements'])
  380. ) {
  381. $this->removePreviousAgreements($courseId, $sessionId);
  382. }
  383. $warnUsers = isset($values['warn_users_by_email']) ? $values['warn_users_by_email'] : null;
  384. switch ($warnUsers) {
  385. case '1':
  386. // Nothing
  387. break;
  388. case '2':
  389. // Send mail
  390. $this->warnUsersByEmail($courseId, $sessionId);
  391. break;
  392. case '3':
  393. // Send mail + attachment if exists.
  394. if (!empty($legalData['filename'])) {
  395. $this->warnUsersByEmail(
  396. $courseId,
  397. $sessionId,
  398. $uploadResult
  399. );
  400. }
  401. break;
  402. }
  403. }
  404. /**
  405. * @param int $courseId
  406. * @param int $sessionId
  407. *
  408. * @return array|mixed
  409. */
  410. public function getData($courseId, $sessionId)
  411. {
  412. $table = Database::get_main_table('session_rel_course_legal');
  413. $conditions = [
  414. 'c_id = ? AND session_id = ? ' => [
  415. $courseId,
  416. $sessionId,
  417. ],
  418. ];
  419. $result = Database::select('*', $table, ['where' => $conditions]);
  420. $legalData = isset($result) && !empty($result) ? current($result) : [];
  421. return $legalData;
  422. }
  423. /**
  424. * @param int $courseId
  425. * @param int $sessionId
  426. *
  427. * @return string
  428. */
  429. public function getCurrentFile($courseId, $sessionId)
  430. {
  431. $data = $this->getData($courseId, $sessionId);
  432. if (isset($data['filename']) && !empty($data['filename'])) {
  433. $course = api_get_course_info_by_id($courseId);
  434. $coursePath = api_get_path(SYS_COURSE_PATH).$course['directory'].'/courselegal';
  435. $file = $coursePath.'/'.$data['filename'];
  436. if (file_exists($file)) {
  437. return Display::url(
  438. $data['filename'],
  439. api_get_path(WEB_COURSE_PATH).$course['directory'].'/courselegal/'.$data['filename'],
  440. ['target' => '_blank']
  441. );
  442. }
  443. }
  444. }
  445. public function install()
  446. {
  447. $table = Database::get_main_table('session_rel_course_legal');
  448. $sql = "CREATE TABLE IF NOT EXISTS $table (
  449. id int PRIMARY KEY AUTO_INCREMENT,
  450. c_id int,
  451. session_id int,
  452. content text,
  453. filename varchar(255)
  454. )";
  455. Database::query($sql);
  456. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  457. $sql = "CREATE TABLE IF NOT EXISTS $table (
  458. id int PRIMARY KEY AUTO_INCREMENT,
  459. user_id int,
  460. c_id int,
  461. session_id int,
  462. web_agreement varchar(255),
  463. web_agreement_date datetime,
  464. mail_agreement varchar(255),
  465. mail_agreement_date datetime,
  466. mail_agreement_link varchar(255)
  467. )";
  468. Database::query($sql);
  469. // Installing course settings
  470. $this->install_course_fields_in_all_courses(false);
  471. }
  472. public function uninstall()
  473. {
  474. $table = Database::get_main_table('session_rel_course_legal');
  475. $sql = "DROP TABLE $table ";
  476. Database::query($sql);
  477. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  478. $sql = "DROP TABLE $table ";
  479. Database::query($sql);
  480. // Deleting course settings
  481. $this->uninstall_course_fields_in_all_courses($this->course_settings);
  482. }
  483. }