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 = array(
  11. array(
  12. 'name' => 'courselegal',
  13. 'type' => 'text'
  14. )
  15. );
  16. /**
  17. * @return CourseLegalPlugin
  18. */
  19. public static function create()
  20. {
  21. static $result = null;
  22. return $result ? $result : $result = new self();
  23. }
  24. /**
  25. *
  26. */
  27. protected function __construct()
  28. {
  29. parent::__construct(
  30. '0.1',
  31. 'Julio Montoya',
  32. array(
  33. 'tool_enable' => 'boolean'
  34. )
  35. );
  36. }
  37. /**
  38. * @return string
  39. */
  40. public function getTeacherLink()
  41. {
  42. $link = null;
  43. if (api_is_allowed_to_edit()) {
  44. $url = api_get_path(WEB_PLUGIN_PATH).'courselegal/start.php?'.api_get_cidreq();
  45. $link = Display::url(
  46. $this->get_lang('CourseLegal'),
  47. $url,
  48. array('class' => 'btn')
  49. );
  50. }
  51. return $link;
  52. }
  53. /**
  54. * @param int $userId
  55. * @param int $courseId
  56. * @param int $sessionId
  57. *
  58. * @return array
  59. */
  60. public function getUserAcceptedLegal($userId, $courseId, $sessionId)
  61. {
  62. $userId = intval($userId);
  63. $courseId = intval($courseId);
  64. $sessionId = intval($sessionId);
  65. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  66. $sql = "SELECT *
  67. FROM $table
  68. WHERE user_id = $userId AND c_id = $courseId AND session_id = $sessionId";
  69. $result = Database::query($sql);
  70. $data = array();
  71. if (Database::num_rows($result) > 0) {
  72. $data = Database::fetch_array($result, 'ASSOC');
  73. }
  74. return $data;
  75. }
  76. /**
  77. * @param int $userId
  78. * @param string $courseCode
  79. * @param int $sessionId
  80. *
  81. * @return bool
  82. */
  83. public function isUserAcceptedLegal($userId, $courseCode, $sessionId)
  84. {
  85. $courseInfo = api_get_course_info($courseCode);
  86. $courseId = $courseInfo['real_id'];
  87. $result = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  88. if (!empty($result)) {
  89. if ($result['mail_agreement'] == 1 &&
  90. $result['web_agreement'] == 1
  91. ) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. /**
  98. * @param int $userId
  99. * @param int $courseCode
  100. * @param int $sessionId
  101. *
  102. * @return bool
  103. */
  104. public function saveUserLegal($userId, $courseCode, $sessionId)
  105. {
  106. $courseInfo = api_get_course_info($courseCode);
  107. $courseId = $courseInfo['real_id'];
  108. $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  109. $id = false;
  110. if (empty($data)) {
  111. $table = Database::get_main_table(
  112. 'session_rel_course_rel_user_legal'
  113. );
  114. $uniqueId = api_get_unique_id();
  115. $values = array(
  116. 'user_id' => $userId,
  117. 'c_id' => $courseId,
  118. 'session_id' => $sessionId,
  119. 'web_agreement' => 1,
  120. 'web_agreement_date' => api_get_utc_datetime(),
  121. 'mail_agreement_link' => $uniqueId
  122. );
  123. $id = Database::insert($table, $values);
  124. $this->sendMailLink($uniqueId, $userId, $courseId, $sessionId);
  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. array('mail_agreement_link' => $uniqueId),
  144. array('id = ? ' => array($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. array('id = ? ' => array($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 = array(
  207. 'mail_agreement' => 1,
  208. 'mail_agreement_date' => api_get_utc_datetime()
  209. );
  210. Database::update($table, $values, array('id = ?' => array($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 = array();
  232. if (!empty($filePath)) {
  233. $dataFile = array(
  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. * @return array
  262. */
  263. public function getUserAgreementList($courseId, $sessionId, $order = null)
  264. {
  265. $courseId = intval($courseId);
  266. $sessionId = intval($sessionId);
  267. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  268. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  269. $sql = "SELECT *
  270. FROM $table s INNER JOIN $userTable u
  271. ON u.user_id = s.user_id
  272. WHERE c_id = $courseId AND session_id = $sessionId ";
  273. if (!empty($order)) {
  274. $sql .= $order;
  275. }
  276. $result = Database::query($sql);
  277. $data = array();
  278. if (Database::num_rows($result) > 0) {
  279. $data = Database::store_result($result, 'ASSOC');
  280. }
  281. return $data;
  282. }
  283. /**
  284. * @param int $courseId
  285. * @param int $sessionId
  286. */
  287. public function removePreviousAgreements($courseId, $sessionId)
  288. {
  289. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  290. $sessionId = intval($sessionId);
  291. $courseId = intval($courseId);
  292. $sql = "DELETE FROM $table
  293. WHERE c_id = '$courseId' AND session_id = $sessionId ";
  294. Database::query($sql);
  295. }
  296. /**
  297. * @param array $values
  298. * @param array $file $_FILES['uploaded_file']
  299. * @param bool $deleteFile
  300. */
  301. public function save($values, $file = array(), $deleteFile = false)
  302. {
  303. $table = Database::get_main_table('session_rel_course_legal');
  304. $courseId = $values['c_id'];
  305. $sessionId = $values['session_id'];
  306. $conditions = array(
  307. 'c_id' => $courseId,
  308. 'session_id' => $sessionId,
  309. );
  310. $course = api_get_course_info_by_id($courseId);
  311. $legalData = $this->getData($courseId, $sessionId);
  312. $coursePath = api_get_path(SYS_COURSE_PATH).$course['directory'].'/courselegal';
  313. $uploadResult = $coursePath.'/'.$legalData['filename'];
  314. if (!is_dir($coursePath)) {
  315. mkdir($coursePath, api_get_permissions_for_new_directories());
  316. }
  317. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  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 = array(
  354. 'content' => $values['content'],
  355. );
  356. if (!empty($fileName)) {
  357. $updateParams['filename'] = $fileName;
  358. }
  359. Database::update(
  360. $table,
  361. $updateParams,
  362. array('id = ? ' => $id)
  363. );
  364. }
  365. if ($deleteFile) {
  366. Database::update(
  367. $table,
  368. array('filename' => ''),
  369. array('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 = array(
  414. 'c_id = ? AND session_id = ? ' => array(
  415. $courseId,
  416. $sessionId
  417. )
  418. );
  419. $result = Database::select('*', $table, array('where' => $conditions));
  420. $legalData = isset($result) && !empty($result) ? current($result) : array();
  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. array('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. }