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. * @param boolean $sendEmail Optional. Indicate whether the mail must be sent. Default is true
  102. * @return mixed
  103. */
  104. public function saveUserLegal($userId, $courseCode, $sessionId, $sendEmail = true)
  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. if ($sendEmail) {
  125. $this->sendMailLink($uniqueId, $userId, $courseId, $sessionId);
  126. }
  127. }
  128. return $id;
  129. }
  130. /**
  131. * @param int $userId
  132. * @param int $courseId
  133. * @param int $sessionId
  134. */
  135. public function updateMailAgreementLink($userId, $courseId, $sessionId)
  136. {
  137. $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  138. if (!empty($data)) {
  139. $table = Database::get_main_table(
  140. 'session_rel_course_rel_user_legal'
  141. );
  142. $uniqueId = api_get_unique_id();
  143. Database::update(
  144. $table,
  145. array('mail_agreement_link' => $uniqueId),
  146. array('id = ? ' => array($data['id']))
  147. );
  148. $this->sendMailLink($uniqueId, $userId, $courseId, $sessionId);
  149. }
  150. }
  151. /**
  152. * @param int $userId
  153. * @param int $courseId
  154. * @param int $sessionId
  155. */
  156. public function deleteUserAgreement($userId, $courseId, $sessionId)
  157. {
  158. $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  159. if (!empty($data)) {
  160. $table = Database::get_main_table(
  161. 'session_rel_course_rel_user_legal'
  162. );
  163. Database::delete(
  164. $table,
  165. array('id = ? ' => array($data['id']))
  166. );
  167. }
  168. }
  169. /**
  170. * @param string $uniqueId
  171. * @param int $userId
  172. * @param int $courseId
  173. * @param int $sessionId
  174. */
  175. public function sendMailLink($uniqueId, $userId, $courseId, $sessionId)
  176. {
  177. $courseInfo = api_get_course_info_by_id($courseId);
  178. $courseCode = $courseInfo['code'];
  179. $url = api_get_path(WEB_CODE_PATH).'course_info/legal.php?web_agreement_link='.$uniqueId.'&course_code='.Security::remove_XSS($courseCode).'&session_id='.$sessionId;
  180. $courseUrl = Display::url($url, $url);
  181. $sessionInfo = api_get_session_info($sessionId);
  182. $sesstionTitle = null;
  183. if (!empty($sessionInfo)) {
  184. $sesstionTitle = ' ('.$sessionInfo['name'].')';
  185. }
  186. $courseTitle = $courseInfo['title'].$sesstionTitle;
  187. $subject = $this->get_lang("MailAgreement");
  188. $message = sprintf($this->get_lang("MailAgreementWasSentWithClickX"), $courseTitle, $courseUrl);
  189. MessageManager::send_message_simple($userId, $subject, $message);
  190. }
  191. /**
  192. * @param string $link
  193. * @param int $userId
  194. * @param int $courseId
  195. * @param int $sessionId
  196. *
  197. * @return bool
  198. */
  199. public function saveUserMailLegal($link, $userId, $courseId, $sessionId)
  200. {
  201. $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId);
  202. if (empty($data)) {
  203. return null;
  204. }
  205. if ($data['mail_agreement_link'] == $link) {
  206. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  207. $id = $data['id'];
  208. $values = array(
  209. 'mail_agreement' => 1,
  210. 'mail_agreement_date' => api_get_utc_datetime()
  211. );
  212. Database::update($table, $values, array('id = ?' => array($id)));
  213. }
  214. }
  215. /**
  216. * @param int $courseId
  217. * @param int $sessionId
  218. * @param string $filePath
  219. */
  220. public function warnUsersByEmail($courseId, $sessionId, $filePath = null)
  221. {
  222. $courseInfo = api_get_course_info_by_id($courseId);
  223. $courseCode = $courseInfo['code'];
  224. if (empty($sessionId)) {
  225. $students = CourseManager::get_student_list_from_course_code($courseCode, false);
  226. } else {
  227. $students = CourseManager::get_student_list_from_course_code($courseCode, true, $sessionId);
  228. }
  229. $url = api_get_course_url($courseCode, $sessionId);
  230. $url = Display::url($url, $url);
  231. $subject = $this->get_lang("AgreementUpdated");
  232. $message = sprintf($this->get_lang("AgreementWasUpdatedClickHere"), $url);
  233. $dataFile = array();
  234. if (!empty($filePath)) {
  235. $dataFile = array(
  236. 'path' => $filePath,
  237. 'filename' => basename($filePath),
  238. );
  239. $message = sprintf($this->get_lang("AgreementWasUpdatedClickHere"), $url)." \n";
  240. $message .= $this->get_lang("TheAgreementIsAttachedInThisEmail");
  241. }
  242. if (!empty($students)) {
  243. foreach ($students as $student) {
  244. $userInfo = api_get_user_info($student['user_id']);
  245. api_mail_html(
  246. $userInfo['complete_name'],
  247. $userInfo['email'],
  248. $subject,
  249. $message,
  250. null,
  251. null,
  252. null,
  253. $dataFile
  254. );
  255. //MessageManager::send_message_simple($student['user_id'], $subject, $message);
  256. }
  257. }
  258. }
  259. /**
  260. * @param int $courseId
  261. * @param int $sessionId
  262. * @param string $order
  263. * @return array
  264. */
  265. public function getUserAgreementList($courseId, $sessionId, $order = null)
  266. {
  267. $courseId = intval($courseId);
  268. $sessionId = intval($sessionId);
  269. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  270. $userTable = Database::get_main_table(TABLE_MAIN_USER);
  271. $sql = "SELECT *
  272. FROM $table s INNER JOIN $userTable u
  273. ON u.user_id = s.user_id
  274. WHERE c_id = $courseId AND session_id = $sessionId ";
  275. if (!empty($order)) {
  276. $sql .= $order;
  277. }
  278. $result = Database::query($sql);
  279. $data = array();
  280. if (Database::num_rows($result) > 0) {
  281. $data = Database::store_result($result, 'ASSOC');
  282. }
  283. return $data;
  284. }
  285. /**
  286. * @param int $courseId
  287. * @param int $sessionId
  288. */
  289. public function removePreviousAgreements($courseId, $sessionId)
  290. {
  291. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  292. $sessionId = intval($sessionId);
  293. $courseId = intval($courseId);
  294. $sql = "DELETE FROM $table
  295. WHERE c_id = '$courseId' AND session_id = $sessionId ";
  296. Database::query($sql);
  297. }
  298. /**
  299. * @param array $values
  300. * @param array $file $_FILES['uploaded_file']
  301. * @param bool $deleteFile
  302. */
  303. public function save($values, $file = array(), $deleteFile = false)
  304. {
  305. $table = Database::get_main_table('session_rel_course_legal');
  306. $courseId = $values['c_id'];
  307. $sessionId = $values['session_id'];
  308. $conditions = array(
  309. 'c_id' => $courseId,
  310. 'session_id' => $sessionId,
  311. );
  312. $course = api_get_course_info_by_id($courseId);
  313. $legalData = $this->getData($courseId, $sessionId);
  314. $coursePath = api_get_path(SYS_COURSE_PATH).$course['directory'].'/courselegal';
  315. $uploadResult = $coursePath.'/'.$legalData['filename'];
  316. if (!is_dir($coursePath)) {
  317. mkdir($coursePath, api_get_permissions_for_new_directories());
  318. }
  319. $uploadOk = process_uploaded_file($file, false);
  320. $fileName = null;
  321. if ($uploadOk) {
  322. $uploadResult = handle_uploaded_document(
  323. $course,
  324. $file,
  325. $coursePath,
  326. '/',
  327. api_get_user_id(),
  328. api_get_group_id(),
  329. null,
  330. false,
  331. false,
  332. false,
  333. true
  334. );
  335. if ($uploadResult) {
  336. $fileName = basename($uploadResult);
  337. // Delete old one if exists.
  338. if ($legalData) {
  339. if (!empty($legalData['filename'])) {
  340. $fileToDelete = $coursePath.'/'.$legalData['filename'];
  341. if (file_exists($fileToDelete)) {
  342. unlink($fileToDelete);
  343. }
  344. }
  345. }
  346. }
  347. }
  348. $conditions['content'] = $values['content'];
  349. $conditions['filename'] = $fileName;
  350. if (empty($legalData)) {
  351. $id = Database::insert($table, $conditions);
  352. } else {
  353. $id = $legalData['id'];
  354. $updateParams = array(
  355. 'content' => $values['content'],
  356. );
  357. if (!empty($fileName)) {
  358. $updateParams['filename'] = $fileName;
  359. }
  360. Database::update(
  361. $table,
  362. $updateParams,
  363. array('id = ? ' => $id)
  364. );
  365. }
  366. if ($deleteFile) {
  367. Database::update(
  368. $table,
  369. array('filename' => ''),
  370. array('id = ? ' => $id)
  371. );
  372. if (!empty($legalData['filename'])) {
  373. $fileToDelete = $coursePath . '/' . $legalData['filename'];
  374. if (file_exists($fileToDelete)) {
  375. unlink($fileToDelete);
  376. }
  377. }
  378. }
  379. if (isset($values['remove_previous_agreements']) &&
  380. !empty($values['remove_previous_agreements'])
  381. ) {
  382. $this->removePreviousAgreements($courseId, $sessionId);
  383. }
  384. $warnUsers = isset($values['warn_users_by_email']) ? $values['warn_users_by_email'] : null;
  385. switch ($warnUsers) {
  386. case '1':
  387. // Nothing
  388. break;
  389. case '2':
  390. // Send mail
  391. $this->warnUsersByEmail($courseId, $sessionId);
  392. break;
  393. case '3':
  394. // Send mail + attachment if exists.
  395. if (!empty($legalData['filename'])) {
  396. $this->warnUsersByEmail(
  397. $courseId,
  398. $sessionId,
  399. $uploadResult
  400. );
  401. }
  402. break;
  403. }
  404. }
  405. /**
  406. * @param int $courseId
  407. * @param int $sessionId
  408. *
  409. * @return array|mixed
  410. */
  411. public function getData($courseId, $sessionId)
  412. {
  413. $table = Database::get_main_table('session_rel_course_legal');
  414. $conditions = array(
  415. 'c_id = ? AND session_id = ? ' => array(
  416. $courseId,
  417. $sessionId
  418. )
  419. );
  420. $result = Database::select('*', $table, array('where' => $conditions));
  421. $legalData = isset($result) && !empty($result) ? current($result) : array();
  422. return $legalData;
  423. }
  424. /**
  425. * @param int $courseId
  426. * @param int $sessionId
  427. *
  428. * @return string
  429. */
  430. public function getCurrentFile($courseId, $sessionId)
  431. {
  432. $data = $this->getData($courseId, $sessionId);
  433. if (isset($data['filename']) && !empty($data['filename'])) {
  434. $course = api_get_course_info_by_id($courseId);
  435. $coursePath = api_get_path(SYS_COURSE_PATH).$course['directory'].'/courselegal';
  436. $file = $coursePath.'/'.$data['filename'];
  437. if (file_exists($file)) {
  438. return Display::url(
  439. $data['filename'],
  440. api_get_path(WEB_COURSE_PATH).$course['directory'].'/courselegal/'.$data['filename'],
  441. array('target' => '_blank')
  442. );
  443. }
  444. }
  445. }
  446. public function install()
  447. {
  448. $table = Database::get_main_table('session_rel_course_legal');
  449. $sql = "CREATE TABLE IF NOT EXISTS $table (
  450. id int PRIMARY KEY AUTO_INCREMENT,
  451. c_id int,
  452. session_id int,
  453. content text,
  454. filename varchar(255)
  455. )";
  456. Database::query($sql);
  457. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  458. $sql = "CREATE TABLE IF NOT EXISTS $table (
  459. id int PRIMARY KEY AUTO_INCREMENT,
  460. user_id int,
  461. c_id int,
  462. session_id int,
  463. web_agreement varchar(255),
  464. web_agreement_date datetime,
  465. mail_agreement varchar(255),
  466. mail_agreement_date datetime,
  467. mail_agreement_link varchar(255)
  468. )";
  469. Database::query($sql);
  470. // Installing course settings
  471. $this->install_course_fields_in_all_courses(false);
  472. }
  473. public function uninstall()
  474. {
  475. $table = Database::get_main_table('session_rel_course_legal');
  476. $sql = "DROP TABLE $table ";
  477. Database::query($sql);
  478. $table = Database::get_main_table('session_rel_course_rel_user_legal');
  479. $sql = "DROP TABLE $table ";
  480. Database::query($sql);
  481. // Deleting course settings
  482. $this->uninstall_course_fields_in_all_courses($this->course_settings);
  483. }
  484. }