legal.lib.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. $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 SET
  34. language_id = '".$language."',
  35. content = '".$content."',
  36. changes= '".$changes."',
  37. type = '".$type."',
  38. version = '".intval($version)."',
  39. date = '".$time."'";
  40. 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 SET
  46. changes= '".$changes."',
  47. type = '".$type."',
  48. date = '".$time."'
  49. WHERE legal_id= $id ";
  50. Database::query($sql);
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. public static function delete($id)
  57. {
  58. /*
  59. $legal_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  60. $id = intval($id);
  61. $sql = "DELETE FROM $legal_table WHERE legal_id = '".$id."'";
  62. */
  63. }
  64. /**
  65. * Gets the last version of a Term and condition by language
  66. * @param int $language language id
  67. * @return array all the info of a Term and condition
  68. */
  69. public static function get_last_condition_version($language)
  70. {
  71. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  72. $language= Database::escape_string($language);
  73. $sql = "SELECT version FROM $legal_conditions_table
  74. WHERE language_id = '".$language."'
  75. 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 $language language id
  87. * @return array all the info of a Term and condition
  88. */
  89. public static function get_last_condition ($language)
  90. {
  91. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  92. $language= Database::escape_string($language);
  93. $sql = "SELECT * FROM $legal_conditions_table
  94. WHERE language_id = '".$language."'
  95. ORDER BY version DESC LIMIT 1 ";
  96. $result = Database::query($sql);
  97. return Database::fetch_array($result);
  98. }
  99. /**
  100. * Gets the last version of a Term and condition by language
  101. * @param int $language language id
  102. * @return boolean | int the version or false if does not exist
  103. */
  104. public static function get_last_version($language)
  105. {
  106. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  107. $language= Database::escape_string($language);
  108. $sql = "SELECT version FROM $legal_conditions_table
  109. WHERE language_id = '".$language."'
  110. ORDER BY version DESC 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. /*// scroll box
  131. case 0:
  132. $preview ='<fieldset>
  133. <legend>'.get_lang('TermsAndConditions').'</legend>';
  134. $preview .= '<div class="form-item">
  135. <label>'.get_lang('TermsAndConditions').': </label>
  136. <div class="resizable-textarea">
  137. <span>
  138. <textarea id="" class="form-textarea resizable textarea-processed" readonly="readonly" name="" rows="10" cols="60">';
  139. $preview .= $term_preview['content'];
  140. $preview .= '</textarea>
  141. <div class="grippie" style="margin-right: -2px;"/>
  142. </span>
  143. </div>
  144. </div>
  145. <div id="edit-legal-accept-wrapper" class="form-item">
  146. <label class="option" for="edit-legal-accept">
  147. <input id="edit-legal-accept" class="form-checkbox" type="checkbox" value="1" name="legal_accept"/>
  148. <strong>'.get_lang('Accept').'</strong>
  149. '.get_lang('TermsAndConditions').'
  150. </label>
  151. </div>
  152. </fieldset>';
  153. break;*/
  154. // HTML
  155. case 0:
  156. if (!empty($term_preview['content'])) {
  157. $preview = '<div class="legal-terms">'.$term_preview['content'].'</div><br />';
  158. }
  159. $preview .= get_lang('ByClickingRegisterYouAgreeTermsAndConditions');
  160. break;
  161. // Page link
  162. case 1:
  163. $preview ='<fieldset>
  164. <legend>'.get_lang('TermsAndConditions').'</legend>';
  165. $preview .= '<div id="legal-accept-wrapper" class="form-item">
  166. <label class="option" for="legal-accept">
  167. <input id="legal-accept" type="checkbox" value="1" name="legal_accept"/>
  168. '.get_lang('IHaveReadAndAgree').'
  169. <a href="#">'.get_lang('TermsAndConditions').'</a>
  170. </label>
  171. </div>
  172. </fieldset>';
  173. break;
  174. default:
  175. break;
  176. }
  177. return $preview;
  178. }
  179. /**
  180. * Get the terms and condition table (only for maintenance)
  181. * @param int offset
  182. * @param int number of items
  183. * @param int column
  184. * @return array
  185. */
  186. public static function get_legal_data($from, $number_of_items, $column)
  187. {
  188. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  189. $lang_table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  190. $from = intval($from);
  191. $number_of_items = intval($number_of_items);
  192. $column = intval($column);
  193. $sql = "SELECT version, original_name as language, content, changes, type, FROM_UNIXTIME(date)
  194. FROM $legal_conditions_table inner join $lang_table l on(language_id = l.id) ";
  195. $sql .= "ORDER BY language, version ASC ";
  196. $sql .= "LIMIT $from, $number_of_items ";
  197. $result = Database::query($sql);
  198. $legals = array ();
  199. $versions = array ();
  200. while ($legal = Database::fetch_array($result)) {
  201. // max 2000 chars
  202. //echo strlen($legal[1]); echo '<br>';
  203. $versions[]=$legal[0];
  204. $languages[]=$legal[1];
  205. if (strlen($legal[2])>2000)
  206. $legal[2]= substr($legal[2],0,2000).' ... ';
  207. if ($legal[4]==0)
  208. $legal[4]= get_lang('HTMLText');
  209. elseif($legal[4]==1)
  210. $legal[4]=get_lang('PageLink');
  211. $legals[] = $legal;
  212. }
  213. return $legals;
  214. }
  215. /**
  216. * Gets the number of terms and conditions available
  217. * @return int
  218. */
  219. public static function count()
  220. {
  221. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  222. $sql = "SELECT count(*) as count_result FROM $legal_conditions_table ORDER BY legal_id DESC ";
  223. $result = Database::query($sql);
  224. $url = Database::fetch_array($result,'ASSOC');
  225. $result = $url['count_result'];
  226. return $result;
  227. }
  228. /**
  229. * Get type of terms and conditions
  230. * @param int The legal id
  231. * @param int The language id
  232. * @return int The current type of terms and conditions
  233. */
  234. public static function get_type_of_terms_and_conditions($legal_id,$language_id)
  235. {
  236. $legal_conditions_table = Database::get_main_table(TABLE_MAIN_LEGAL);
  237. $legal_id = intval($legal_id);
  238. $language_id = Database::escape_string($language_id);
  239. $sql = 'SELECT type FROM '.$legal_conditions_table.' WHERE legal_id="'.$legal_id.'" AND language_id="'.$language_id.'"';
  240. $rs = Database::query($sql);
  241. return Database::result($rs,0,'type');
  242. }
  243. }