legal.lib.php 7.6 KB

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