legal.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class LegalManager.
  5. *
  6. * @package chamilo.legal
  7. */
  8. class LegalManager
  9. {
  10. /**
  11. * Constructor.
  12. */
  13. public function __construct()
  14. {
  15. }
  16. /**
  17. * Add a new Term and Condition.
  18. *
  19. * @param int $language language id
  20. * @param string $content content
  21. * @param int $type term and condition type (0 for HTML text or 1 for link to another page)
  22. * @param string $changes explain changes
  23. * @param array $extraFieldValuesToSave
  24. *
  25. * @return int
  26. */
  27. public static function add($language, $content, $type, $changes, $extraFieldValuesToSave = [])
  28. {
  29. $legalTable = Database::get_main_table(TABLE_MAIN_LEGAL);
  30. $last = self::get_last_condition($language);
  31. $type = (int) $type;
  32. $time = time();
  33. $changeList = [];
  34. if (isset($last['id'])) {
  35. $id = $last['id'];
  36. // Check if extra fields changed
  37. $extraFieldValue = new ExtraFieldValue('terms_and_condition');
  38. $values = $extraFieldValue->getAllValuesByItem($id);
  39. $oldValues = array_column($values, 'value', 'variable');
  40. foreach ($extraFieldValuesToSave as $key => $value) {
  41. if (is_numeric(strpos($key, 'extra_'))) {
  42. $replace = str_replace('extra_', '', $key);
  43. if (isset($oldValues[$replace])) {
  44. if ($value != $oldValues[$replace]) {
  45. $changeList[] = $replace;
  46. }
  47. } else {
  48. // It means there's a new extra field that was not included before.
  49. $changeList[] = $replace;
  50. }
  51. }
  52. }
  53. }
  54. if ($last['content'] != $content || !empty($changeList)) {
  55. $version = self::getLastVersion($language);
  56. $version++;
  57. $params = [
  58. 'language_id' => $language,
  59. 'content' => $content,
  60. 'changes' => $changes,
  61. 'type' => $type,
  62. 'version' => $version,
  63. 'date' => $time,
  64. ];
  65. $id = Database::insert($legalTable, $params);
  66. self::updateExtraFields($id, $extraFieldValuesToSave);
  67. return $id;
  68. } elseif ($last['type'] != $type && $language == $last['language_id']) {
  69. // Update
  70. $id = $last['id'];
  71. $params = [
  72. 'changes' => $changes,
  73. 'type' => $type,
  74. 'date' => $time,
  75. ];
  76. Database::update($legalTable, $params, ['id = ?' => $id]);
  77. self::updateExtraFields($id, $extraFieldValuesToSave);
  78. return $id;
  79. }
  80. return 0;
  81. }
  82. /**
  83. * @param int $itemId
  84. * @param array $values
  85. *
  86. * @return bool
  87. */
  88. public static function updateExtraFields($itemId, $values)
  89. {
  90. if (empty($itemId)) {
  91. return false;
  92. }
  93. $extraFieldValues = new ExtraFieldValue('terms_and_condition');
  94. $values['item_id'] = $itemId;
  95. $extraFieldValues->saveFieldValues($values);
  96. return true;
  97. }
  98. /**
  99. * @param int $id
  100. */
  101. public static function delete($id)
  102. {
  103. /*
  104. $legalTable = Database::get_main_table(TABLE_MAIN_LEGAL);
  105. $id = (int) $id;
  106. $sql = "DELETE FROM $legalTable WHERE id = '".$id."'";
  107. */
  108. }
  109. /**
  110. * Gets the last version of a Term and condition by language.
  111. *
  112. * @param int $language language id
  113. *
  114. * @return int
  115. */
  116. public static function getLastVersion($language)
  117. {
  118. $table = Database::get_main_table(TABLE_MAIN_LEGAL);
  119. $language = (int) $language;
  120. $sql = "SELECT version FROM $table
  121. WHERE language_id = $language
  122. ORDER BY id DESC LIMIT 1 ";
  123. $result = Database::query($sql);
  124. $row = Database::fetch_array($result);
  125. if (Database::num_rows($result) > 0) {
  126. return (int) $row['version'];
  127. }
  128. return 0;
  129. }
  130. /**
  131. * Gets the data of a Term and condition by language.
  132. *
  133. * @param int $language language id
  134. *
  135. * @return array all the info of a Term and condition
  136. */
  137. public static function get_last_condition($language)
  138. {
  139. $table = Database::get_main_table(TABLE_MAIN_LEGAL);
  140. $language = (int) $language;
  141. $sql = "SELECT * FROM $table
  142. WHERE language_id = $language
  143. ORDER BY version DESC
  144. LIMIT 1 ";
  145. $result = Database::query($sql);
  146. $result = Database::fetch_array($result, 'ASSOC');
  147. if (isset($result['content'])) {
  148. $result['content'] = self::replaceTags($result['content']);
  149. }
  150. return $result;
  151. }
  152. /**
  153. * Check if an specific version of an agreement exists.
  154. *
  155. * @param int $language
  156. * @param int $version
  157. *
  158. * @return bool
  159. */
  160. public static function hasVersion($language, $version)
  161. {
  162. $table = Database::get_main_table(TABLE_MAIN_LEGAL);
  163. $language = (int) $language;
  164. $version = (int) $version;
  165. if (empty($language)) {
  166. return false;
  167. }
  168. $sql = "SELECT version FROM $table
  169. WHERE
  170. language_id = $language AND
  171. version = $version
  172. LIMIT 1 ";
  173. $result = Database::query($sql);
  174. if (Database::num_rows($result) > 0) {
  175. return true;
  176. }
  177. return false;
  178. }
  179. /**
  180. * @param string $content
  181. *
  182. * @return string
  183. */
  184. public static function replaceTags($content)
  185. {
  186. if (strpos($content, '{{sessions}}')) {
  187. $sessionListToString = '';
  188. $sessionList = SessionManager::get_sessions_by_user(api_get_user_id());
  189. if ($sessionList) {
  190. $sessionListToString = get_lang('SessionList').'<ul>';
  191. foreach ($sessionList as $session) {
  192. $sessionListToString .= '<li>'.$session['session_name'].'</li>';
  193. }
  194. $sessionListToString .= '<ul>';
  195. }
  196. $content = str_replace('{{sessions}}', $sessionListToString, $content);
  197. }
  198. return $content;
  199. }
  200. /**
  201. * Gets the last version of a Term and condition by language.
  202. *
  203. * @param int $language language id
  204. *
  205. * @return bool | int the version or false if does not exist
  206. */
  207. public static function get_last_version($language)
  208. {
  209. $table = Database::get_main_table(TABLE_MAIN_LEGAL);
  210. $language = (int) $language;
  211. $sql = "SELECT version FROM $table
  212. WHERE language_id = '$language'
  213. ORDER BY version DESC
  214. LIMIT 1 ";
  215. $result = Database::query($sql);
  216. if (Database::num_rows($result) > 0) {
  217. $version = Database::fetch_array($result);
  218. $version = explode(':', $version[0]);
  219. return $version[0];
  220. }
  221. return false;
  222. }
  223. /**
  224. * Show the last condition.
  225. *
  226. * @param array $term_preview with type and content i.e array('type'=>'1', 'content'=>'hola');
  227. *
  228. * @return string html preview
  229. */
  230. public static function show_last_condition($term_preview)
  231. {
  232. $preview = '';
  233. switch ($term_preview['type']) {
  234. case 0:
  235. if (!empty($term_preview['content'])) {
  236. $preview = '<div class="terms-conditions">
  237. <div id="legal-terms" class="scrollbar-inner">'.$term_preview['content'].'</div>
  238. </div>';
  239. }
  240. $preview .= get_lang('ByClickingRegisterYouAgreeTermsAndConditions');
  241. $courseInfo = api_get_course_info();
  242. if (api_get_setting('load_term_conditions_section') === 'course' && empty($courseInfo)) {
  243. $preview = '';
  244. }
  245. break;
  246. // Page link
  247. case 1:
  248. $preview = '<fieldset>
  249. <legend>'.get_lang('TermsAndConditions').'</legend>';
  250. $preview .= '<div id="legal-accept-wrapper" class="form-item">
  251. <label class="option" for="legal-accept">
  252. <input id="legal-accept" type="checkbox" value="1" name="legal_accept"/>
  253. '.get_lang('IHaveReadAndAgree').'
  254. <a href="#">'.get_lang('TermsAndConditions').'</a>
  255. </label>
  256. </div>
  257. </fieldset>';
  258. break;
  259. default:
  260. break;
  261. }
  262. return $preview;
  263. }
  264. /**
  265. * Get the terms and condition table (only for maintenance).
  266. *
  267. * @param int $from
  268. * @param int $number_of_items
  269. * @param int $column
  270. *
  271. * @return array
  272. */
  273. public static function get_legal_data($from, $number_of_items, $column)
  274. {
  275. $table = Database::get_main_table(TABLE_MAIN_LEGAL);
  276. $lang_table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  277. $from = (int) $from;
  278. $number_of_items = (int) $number_of_items;
  279. $column = (int) $column;
  280. $sql = "SELECT version, original_name as language, content, changes, type, FROM_UNIXTIME(date)
  281. FROM $table
  282. INNER JOIN $lang_table l
  283. ON (language_id = l.id)
  284. ORDER BY language, version ASC
  285. LIMIT $from, $number_of_items ";
  286. $result = Database::query($sql);
  287. $legals = [];
  288. while ($legal = Database::fetch_array($result)) {
  289. // max 2000 chars
  290. $languages[] = $legal[1];
  291. if (strlen($legal[2]) > 2000) {
  292. $legal[2] = substr($legal[2], 0, 2000).' ... ';
  293. }
  294. if ($legal[4] == 0) {
  295. $legal[4] = get_lang('HTMLText');
  296. } elseif ($legal[4] == 1) {
  297. $legal[4] = get_lang('PageLink');
  298. }
  299. $legals[] = $legal;
  300. }
  301. return $legals;
  302. }
  303. /**
  304. * Gets the number of terms and conditions available.
  305. *
  306. * @return int
  307. */
  308. public static function count()
  309. {
  310. $table = Database::get_main_table(TABLE_MAIN_LEGAL);
  311. $sql = "SELECT count(*) as count_result
  312. FROM $table
  313. ORDER BY id DESC ";
  314. $result = Database::query($sql);
  315. $url = Database::fetch_array($result, 'ASSOC');
  316. $result = $url['count_result'];
  317. return $result;
  318. }
  319. /**
  320. * Get type of terms and conditions.
  321. * Type 0 is HTML Text
  322. * Type 1 is a link to a different terms and conditions page.
  323. *
  324. * @param int $legal_id
  325. * @param int $language_id
  326. *
  327. * @return mixed The current type of terms and conditions (int) or false on error
  328. */
  329. public static function get_type_of_terms_and_conditions($legal_id, $language_id)
  330. {
  331. $table = Database::get_main_table(TABLE_MAIN_LEGAL);
  332. $legal_id = (int) $legal_id;
  333. $language_id = (int) $language_id;
  334. $sql = "SELECT type FROM $table
  335. WHERE id = $legal_id AND language_id = $language_id";
  336. $rs = Database::query($sql);
  337. return Database::result($rs, 0, 'type');
  338. }
  339. /**
  340. * @param int $userId
  341. */
  342. public static function sendLegal($userId)
  343. {
  344. $userInfo = api_get_user_info($userId);
  345. $senderInfo = api_get_user_info();
  346. $subject = get_lang('SendTermsSubject');
  347. $webPath = api_get_path(WEB_PATH);
  348. // @ofaj
  349. $link = '<a href="'.$webPath.'contrat.php">'.$webPath.'contrat.php</a>';
  350. $content = sprintf(
  351. get_lang('SendTermsDescriptionToUrlX'),
  352. $userInfo['firstName'],
  353. $link,
  354. $senderInfo['firstName']
  355. );
  356. MessageManager::send_message_simple($userId, $subject, $content);
  357. Display::addFlash(Display::return_message(get_lang('Sent')));
  358. $extraFieldValue = new ExtraFieldValue('user');
  359. $value = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'termactivated');
  360. if ($value === false || $value['value'] != 1) {
  361. $extraFieldInfo = $extraFieldValue->getExtraField()->get_handler_field_info_by_field_variable('termactivated');
  362. if ($extraFieldInfo) {
  363. $newParams = [
  364. 'item_id' => $userId,
  365. 'field_id' => $extraFieldInfo['id'],
  366. 'value' => 1,
  367. 'comment' => '',
  368. ];
  369. $extraFieldValue->save($newParams);
  370. }
  371. }
  372. }
  373. /**
  374. * @param int $userId
  375. */
  376. public static function deleteLegal($userId)
  377. {
  378. $extraFieldValue = new ExtraFieldValue('user');
  379. $value = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'legal_accept');
  380. $result = $extraFieldValue->delete($value['id']);
  381. if ($result) {
  382. Display::addFlash(Display::return_message(get_lang('Deleted')));
  383. }
  384. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  385. $userId,
  386. 'termactivated'
  387. );
  388. if ($value) {
  389. $extraFieldValue->delete($value['id']);
  390. }
  391. }
  392. /**
  393. * @return array
  394. */
  395. public static function getTreatmentTypeList()
  396. {
  397. return [
  398. 'privacy_terms_collection' => 'collection',
  399. 'privacy_terms_recording' => 'recording',
  400. 'privacy_terms_organization' => 'organization',
  401. 'privacy_terms_structure' => 'structure',
  402. 'privacy_terms_conservation' => 'conservation',
  403. 'privacy_terms_adaptation' => 'adaptation',
  404. 'privacy_terms_extraction' => 'extraction',
  405. 'privacy_terms_consultation' => 'consultation',
  406. 'privacy_terms_usage' => 'usage',
  407. 'privacy_terms_communication' => 'communication',
  408. 'privacy_terms_interconnection' => 'interconnection',
  409. 'privacy_terms_limitation' => 'limitation',
  410. 'privacy_terms_deletion' => 'deletion',
  411. 'privacy_terms_destruction' => 'destruction',
  412. 'privacy_terms_profiling' => 'profiling',
  413. ];
  414. }
  415. }