legal.lib.php 7.7 KB

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