legal.lib.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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(LegalManager::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['legal_id'];
  46. $params = [
  47. 'changes' => $changes,
  48. 'type' => $type,
  49. 'date' => $time
  50. ];
  51. Database::update($legal_table, $params, ['legal_id => ?' => $id]);
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. public static function delete($id)
  58. {
  59. /*
  60. $legal_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  61. $id = intval($id);
  62. $sql = "DELETE FROM $legal_table WHERE legal_id = '".$id."'";
  63. */
  64. }
  65. /**
  66. * Gets the last version of a Term and condition by language
  67. * @param int $language language id
  68. * @return array all the info of a Term and condition
  69. */
  70. public static function get_last_condition_version($language)
  71. {
  72. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  73. $language= Database::escape_string($language);
  74. $sql = "SELECT version FROM $legal_conditions_table
  75. WHERE language_id = '".$language."'
  76. ORDER BY legal_id DESC LIMIT 1 ";
  77. $result = Database::query($sql);
  78. $row = Database::fetch_array($result);
  79. if (Database::num_rows($result) > 0) {
  80. return $row['version'];
  81. } else {
  82. return 0;
  83. }
  84. }
  85. /**
  86. * Gets the data of a Term and condition by language
  87. * @param int $language language id
  88. * @return array all the info of a Term and condition
  89. */
  90. public static function get_last_condition ($language)
  91. {
  92. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  93. $language= Database::escape_string($language);
  94. $sql = "SELECT * FROM $legal_conditions_table
  95. WHERE language_id = '".$language."'
  96. ORDER BY version DESC
  97. LIMIT 1 ";
  98. $result = Database::query($sql);
  99. return Database::fetch_array($result);
  100. }
  101. /**
  102. * Gets the last version of a Term and condition by language
  103. * @param int $language language id
  104. * @return boolean | int the version or false if does not exist
  105. */
  106. public static function get_last_version($language)
  107. {
  108. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  109. $language = intval($language);
  110. $sql = "SELECT version FROM $legal_conditions_table
  111. WHERE language_id = '".$language."'
  112. ORDER BY version DESC
  113. LIMIT 1 ";
  114. $result = Database::query($sql);
  115. if (Database::num_rows($result)>0){
  116. $version = Database::fetch_array($result);
  117. $version = explode(':',$version[0]);
  118. return $version[0];
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * Show the last condition
  125. * @param array with type and content i.e array('type'=>'1', 'content'=>'hola');
  126. *
  127. * @return string html preview
  128. */
  129. public static function show_last_condition($term_preview)
  130. {
  131. $preview = '';
  132. switch ($term_preview['type']) {
  133. case 0:
  134. if (!empty($term_preview['content'])) {
  135. $preview = '<div class="legal-terms">'.$term_preview['content'].'</div><br />';
  136. }
  137. $preview .= get_lang('ByClickingRegisterYouAgreeTermsAndConditions');
  138. break;
  139. // Page link
  140. case 1:
  141. $preview ='<fieldset>
  142. <legend>'.get_lang('TermsAndConditions').'</legend>';
  143. $preview .= '<div id="legal-accept-wrapper" class="form-item">
  144. <label class="option" for="legal-accept">
  145. <input id="legal-accept" type="checkbox" value="1" name="legal_accept"/>
  146. '.get_lang('IHaveReadAndAgree').'
  147. <a href="#">'.get_lang('TermsAndConditions').'</a>
  148. </label>
  149. </div>
  150. </fieldset>';
  151. break;
  152. default:
  153. break;
  154. }
  155. return $preview;
  156. }
  157. /**
  158. * Get the terms and condition table (only for maintenance)
  159. * @param int offset
  160. * @param int number of items
  161. * @param int column
  162. * @return array
  163. */
  164. public static function get_legal_data($from, $number_of_items, $column)
  165. {
  166. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  167. $lang_table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  168. $from = intval($from);
  169. $number_of_items = intval($number_of_items);
  170. $column = intval($column);
  171. $sql = "SELECT version, original_name as language, content, changes, type, FROM_UNIXTIME(date)
  172. FROM $legal_conditions_table inner join $lang_table l on(language_id = l.id) ";
  173. $sql .= "ORDER BY language, version ASC ";
  174. $sql .= "LIMIT $from, $number_of_items ";
  175. $result = Database::query($sql);
  176. $legals = array ();
  177. $versions = array ();
  178. while ($legal = Database::fetch_array($result)) {
  179. // max 2000 chars
  180. //echo strlen($legal[1]); echo '<br>';
  181. $versions[]=$legal[0];
  182. $languages[]=$legal[1];
  183. if (strlen($legal[2])>2000)
  184. $legal[2]= substr($legal[2],0,2000).' ... ';
  185. if ($legal[4]==0)
  186. $legal[4]= get_lang('HTMLText');
  187. elseif($legal[4]==1)
  188. $legal[4]=get_lang('PageLink');
  189. $legals[] = $legal;
  190. }
  191. return $legals;
  192. }
  193. /**
  194. * Gets the number of terms and conditions available
  195. * @return int
  196. */
  197. public static function count()
  198. {
  199. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  200. $sql = "SELECT count(*) as count_result
  201. FROM $legal_conditions_table
  202. ORDER BY legal_id DESC ";
  203. $result = Database::query($sql);
  204. $url = Database::fetch_array($result,'ASSOC');
  205. $result = $url['count_result'];
  206. return $result;
  207. }
  208. /**
  209. * Get type of terms and conditions
  210. * @param int The legal id
  211. * @param int The language id
  212. * @return int The current type of terms and conditions
  213. */
  214. public static function get_type_of_terms_and_conditions($legal_id, $language_id)
  215. {
  216. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  217. $legal_id = intval($legal_id);
  218. $language_id = intval($language_id);
  219. $sql = 'SELECT type FROM '.$legal_conditions_table.'
  220. WHERE legal_id="'.$legal_id.'" AND language_id="'.$language_id.'"';
  221. $rs = Database::query($sql);
  222. return Database::result($rs,0,'type');
  223. }
  224. }