legal.lib.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if (!empty($term_preview['content'])) {
  149. $preview = '<div class="legal-terms">'.$term_preview['content'].'</div><br />';
  150. }
  151. $preview .= get_lang('ByClickingRegisterYouAgreeTermsAndConditions');
  152. break;
  153. // Page link
  154. case 1:
  155. $preview ='<fieldset>
  156. <legend>'.get_lang('TermsAndConditions').'</legend>';
  157. $preview .= '<div id="legal-accept-wrapper" class="form-item">
  158. <label class="option" for="legal-accept">
  159. <input id="legal-accept" type="checkbox" value="1" name="legal_accept"/>
  160. '.get_lang('IHaveReadAndAgree').'
  161. <a href="#">'.get_lang('TermsAndConditions').'</a>
  162. </label>
  163. </div>
  164. </fieldset>';
  165. break;
  166. default:
  167. break;
  168. }
  169. return $preview;
  170. }
  171. /**
  172. * Get the terms and condition table (only for maintenance)
  173. * @param int offset
  174. * @param int number of items
  175. * @param int column
  176. * @return array
  177. */
  178. public static function get_legal_data ($from, $number_of_items, $column) {
  179. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  180. $lang_table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  181. $from = intval($from);
  182. $number_of_items = intval($number_of_items);
  183. $column = intval($column);
  184. $sql = "SELECT version, original_name as language, content, changes, type, FROM_UNIXTIME(date)
  185. FROM $legal_conditions_table inner join $lang_table l on(language_id = l.id) ";
  186. $sql .= "ORDER BY language, version ASC ";
  187. $sql .= "LIMIT $from, $number_of_items ";
  188. $result = Database::query($sql);
  189. $legals = array ();
  190. $versions = array ();
  191. while ($legal = Database::fetch_array($result)) {
  192. // max 2000 chars
  193. //echo strlen($legal[1]); echo '<br>';
  194. $versions[]=$legal[0];
  195. $languages[]=$legal[1];
  196. if (strlen($legal[2])>2000)
  197. $legal[2]= substr($legal[2],0,2000).' ... ';
  198. if ($legal[4]==0)
  199. $legal[4]= get_lang('HTMLText');
  200. elseif($legal[4]==1)
  201. $legal[4]=get_lang('PageLink');
  202. $legals[] = $legal;
  203. }
  204. return $legals;
  205. }
  206. /**
  207. * Gets the number of terms and conditions available
  208. * @return int
  209. */
  210. public static function count() {
  211. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  212. $sql = "SELECT count(*) as count_result FROM $legal_conditions_table ORDER BY legal_id DESC ";
  213. $result = Database::query($sql);
  214. $url = Database::fetch_array($result,'ASSOC');
  215. $result = $url['count_result'];
  216. return $result;
  217. }
  218. /**
  219. * Get type of terms and conditions
  220. * @param int The legal id
  221. * @param int The language id
  222. * @return int The current type of terms and conditions
  223. */
  224. public static function get_type_of_terms_and_conditions ($legal_id,$language_id) {
  225. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  226. $legal_id=Database::escape_string($legal_id);
  227. $language_id=Database::escape_string($language_id);
  228. $sql='SELECT type FROM '.$legal_conditions_table.' WHERE legal_id="'.$legal_id.'" AND language_id="'.$language_id.'"';
  229. $rs=Database::query($sql);
  230. return Database::result($rs,0,'type');
  231. }
  232. }