legal.lib.php 7.6 KB

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