legal.lib.php 14 KB

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