legal.lib.php 7.3 KB

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