legal.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. * @param int $language language id
  19. * @param string $content content
  20. * @param int $type term and condition type (0 or 1)
  21. * @param string $changes explain changes
  22. * @return boolean success
  23. */
  24. public static function add($language, $content, $type, $changes)
  25. {
  26. $legal_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  27. $last = self::get_last_condition($language);
  28. $type = intval($type);
  29. $time = time();
  30. if ($last['content'] != $content) {
  31. $version = intval(self::get_last_condition_version($language));
  32. $version++;
  33. $params = [
  34. 'language_id' => $language,
  35. 'content' => $content,
  36. 'changes' => $changes,
  37. 'type' => $type,
  38. 'version' => intval($version),
  39. 'date' => $time
  40. ];
  41. Database::insert($legal_table, $params);
  42. return true;
  43. } elseif ($last['type'] != $type && $language == $last['language_id']) {
  44. // Update
  45. $id = $last['id'];
  46. $params = [
  47. 'changes' => $changes,
  48. 'type' => $type,
  49. 'date' => $time
  50. ];
  51. Database::update($legal_table, $params, ['id => ?' => $id]);
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. /**
  58. * @param int $id
  59. */
  60. public static function delete($id)
  61. {
  62. /*
  63. $legal_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  64. $id = intval($id);
  65. $sql = "DELETE FROM $legal_table WHERE id = '".$id."'";
  66. */
  67. }
  68. /**
  69. * Gets the last version of a Term and condition by language
  70. * @param int $language language id
  71. * @return array all the info of a Term and condition
  72. */
  73. public static function get_last_condition_version($language)
  74. {
  75. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  76. $language = Database::escape_string($language);
  77. $sql = "SELECT version FROM $legal_conditions_table
  78. WHERE language_id = '".$language."'
  79. ORDER BY id DESC LIMIT 1 ";
  80. $result = Database::query($sql);
  81. $row = Database::fetch_array($result);
  82. if (Database::num_rows($result) > 0) {
  83. return $row['version'];
  84. } else {
  85. return 0;
  86. }
  87. }
  88. /**
  89. * Gets the data of a Term and condition by language
  90. * @param int $language language id
  91. * @return array all the info of a Term and condition
  92. */
  93. public static function get_last_condition($language)
  94. {
  95. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  96. $language = Database::escape_string($language);
  97. $sql = "SELECT * FROM $legal_conditions_table
  98. WHERE language_id = '".$language."'
  99. ORDER BY version DESC
  100. LIMIT 1 ";
  101. $result = Database::query($sql);
  102. $result = Database::fetch_array($result, 'ASSOC');
  103. if (isset($result['content'])) {
  104. $result['content'] = self::replaceTags($result['content']);
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Check if an specific version of an agreement exists
  110. *
  111. * @param int $language
  112. * @param int $version
  113. *
  114. * @return bool
  115. */
  116. public static function hasVersion($language, $version)
  117. {
  118. $table = Database::get_main_table(TABLE_MAIN_LEGAL);
  119. $language = intval($language);
  120. $version = intval($version);
  121. if (empty($language)) {
  122. return false;
  123. }
  124. $sql = "SELECT version FROM $table
  125. WHERE
  126. language_id = '$language' AND
  127. version = '$version'
  128. LIMIT 1 ";
  129. $result = Database::query($sql);
  130. if (Database::num_rows($result) > 0) {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. /**
  137. * @param string $content
  138. * @return string
  139. */
  140. public static function replaceTags($content)
  141. {
  142. if (strpos($content, '{{sessions}}')) {
  143. $sessionListToString = '';
  144. $sessionList = SessionManager::get_sessions_by_user(api_get_user_id());
  145. if ($sessionList) {
  146. $sessionListToString = get_lang('SessionList').'<ul>';
  147. foreach ($sessionList as $session) {
  148. $sessionListToString .= '<li>'.$session['session_name'].'</li>';
  149. }
  150. $sessionListToString .= '<ul>';
  151. }
  152. $content = str_replace('{{sessions}}', $sessionListToString, $content);
  153. }
  154. return $content;
  155. }
  156. /**
  157. * Gets the last version of a Term and condition by language
  158. * @param int $language language id
  159. * @return boolean | int the version or false if does not exist
  160. */
  161. public static function get_last_version($language)
  162. {
  163. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  164. $language = intval($language);
  165. $sql = "SELECT version FROM $legal_conditions_table
  166. WHERE language_id = '".$language."'
  167. ORDER BY version DESC
  168. LIMIT 1 ";
  169. $result = Database::query($sql);
  170. if (Database::num_rows($result) > 0) {
  171. $version = Database::fetch_array($result);
  172. $version = explode(':', $version[0]);
  173. return $version[0];
  174. } else {
  175. return false;
  176. }
  177. }
  178. /**
  179. * Show the last condition
  180. * @param array $term_preview with type and content i.e array('type'=>'1', 'content'=>'hola');
  181. *
  182. * @return string html preview
  183. */
  184. public static function show_last_condition($term_preview)
  185. {
  186. $preview = '';
  187. switch ($term_preview['type']) {
  188. case 0:
  189. if (!empty($term_preview['content'])) {
  190. $preview = '<div class="legal-terms">'.$term_preview['content'].'</div><br />';
  191. }
  192. $preview .= get_lang('ByClickingRegisterYouAgreeTermsAndConditions');
  193. $courseInfo = api_get_course_info();
  194. if (api_get_setting('load_term_conditions_section') === 'course' && empty($courseInfo)) {
  195. $preview = '';
  196. }
  197. break;
  198. // Page link
  199. case 1:
  200. $preview = '<fieldset>
  201. <legend>'.get_lang('TermsAndConditions').'</legend>';
  202. $preview .= '<div id="legal-accept-wrapper" class="form-item">
  203. <label class="option" for="legal-accept">
  204. <input id="legal-accept" type="checkbox" value="1" name="legal_accept"/>
  205. '.get_lang('IHaveReadAndAgree').'
  206. <a href="#">'.get_lang('TermsAndConditions').'</a>
  207. </label>
  208. </div>
  209. </fieldset>';
  210. break;
  211. default:
  212. break;
  213. }
  214. return $preview;
  215. }
  216. /**
  217. * Get the terms and condition table (only for maintenance)
  218. * @param int $from
  219. * @param int $number_of_items
  220. * @param int $column
  221. * @return array
  222. */
  223. public static function get_legal_data($from, $number_of_items, $column)
  224. {
  225. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  226. $lang_table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  227. $from = intval($from);
  228. $number_of_items = intval($number_of_items);
  229. $column = intval($column);
  230. $sql = "SELECT version, original_name as language, content, changes, type, FROM_UNIXTIME(date)
  231. FROM $legal_conditions_table
  232. INNER JOIN $lang_table l
  233. ON (language_id = l.id)
  234. ORDER BY language, version ASC
  235. LIMIT $from, $number_of_items ";
  236. $result = Database::query($sql);
  237. $legals = array();
  238. while ($legal = Database::fetch_array($result)) {
  239. // max 2000 chars
  240. $languages[] = $legal[1];
  241. if (strlen($legal[2]) > 2000) {
  242. $legal[2] = substr($legal[2], 0, 2000).' ... ';
  243. }
  244. if ($legal[4] == 0) {
  245. $legal[4] = get_lang('HTMLText');
  246. } elseif ($legal[4] == 1) {
  247. $legal[4] = get_lang('PageLink');
  248. }
  249. $legals[] = $legal;
  250. }
  251. return $legals;
  252. }
  253. /**
  254. * Gets the number of terms and conditions available
  255. * @return int
  256. */
  257. public static function count()
  258. {
  259. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  260. $sql = "SELECT count(*) as count_result
  261. FROM $legal_conditions_table
  262. ORDER BY id DESC ";
  263. $result = Database::query($sql);
  264. $url = Database::fetch_array($result, 'ASSOC');
  265. $result = $url['count_result'];
  266. return $result;
  267. }
  268. /**
  269. * Get type of terms and conditions
  270. * @param int $legal_id
  271. * @param int $language_id
  272. * @return int The current type of terms and conditions
  273. */
  274. public static function get_type_of_terms_and_conditions($legal_id, $language_id)
  275. {
  276. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  277. $legal_id = intval($legal_id);
  278. $language_id = intval($language_id);
  279. $sql = 'SELECT type FROM '.$legal_conditions_table.'
  280. WHERE id = "'.$legal_id.'" AND language_id="'.$language_id.'"';
  281. $rs = Database::query($sql);
  282. return Database::result($rs, 0, 'type');
  283. }
  284. /**
  285. * @param int $userId
  286. */
  287. public static function sendLegal($userId)
  288. {
  289. $userInfo = api_get_user_info($userId);
  290. $subject = get_lang('SendTermsSubject');
  291. $webPath = api_get_path(WEB_PATH);
  292. $link = '<a href="'.$webPath.'courses/FORUMDAIDE/index.php">'.$webPath.'courses/FORUMDAIDE/index.php</a>';
  293. $content = sprintf(
  294. get_lang('SendTermsDescriptionToUrlX'),
  295. $userInfo['firstName'],
  296. $link
  297. );
  298. MessageManager::send_message_simple($userId, $subject, $content);
  299. Display::addFlash(Display::return_message(get_lang('Sent')));
  300. $extraFieldValue = new ExtraFieldValue('user');
  301. $value = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'termactivated');
  302. if ($value === false || $value[value]!=1) {
  303. $extraFieldInfo = $extraFieldValue->getExtraField()->get_handler_field_info_by_field_variable('termactivated');
  304. if ($extraFieldInfo) {
  305. $newParams = array(
  306. 'item_id' => $userId,
  307. 'field_id' => $extraFieldInfo['id'],
  308. 'value' => 1,
  309. 'comment' => ''
  310. );
  311. $extraFieldValue->save($newParams);
  312. }
  313. }
  314. }
  315. /**
  316. * @param int $userId
  317. */
  318. public static function deleteLegal($userId)
  319. {
  320. $extraFieldValue = new ExtraFieldValue('user');
  321. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  322. $userId,
  323. 'legal_accept'
  324. );
  325. $result = $extraFieldValue->delete($value['id']);
  326. if ($result) {
  327. Display::addFlash(Display::return_message(get_lang('Deleted')));
  328. }
  329. $value = $extraFieldValue->get_values_by_handler_and_field_variable(
  330. $userId,
  331. 'termactivated'
  332. );
  333. if ($value) {
  334. $extraFieldValue->delete($value['id']);
  335. }
  336. }
  337. }