settings.lib.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Library of the settings.php file
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. * @author Guillaume Viguier <guillaume@viguierjust.com>
  8. *
  9. * @since Chamilo 1.8.7
  10. * @package chamilo.admin
  11. */
  12. /**
  13. * This function allows easy activating and inactivating of regions
  14. * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
  15. */
  16. function handle_regions() {
  17. if (isset($_POST['submit_plugins'])) {
  18. store_regions();
  19. // Add event to the system log.
  20. $user_id = api_get_user_id();
  21. $category = $_GET['category'];
  22. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, api_get_utc_datetime(), $user_id);
  23. Display :: display_confirmation_message(get_lang('SettingsStored'));
  24. }
  25. $plugin_obj = new AppPlugin();
  26. $possible_plugins = $plugin_obj->read_plugins_from_path();
  27. $installed_plugins = $plugin_obj->get_installed_plugins();
  28. if (!empty($installed_plugins)) {
  29. $not_installed = array_diff($possible_plugins, $installed_plugins);
  30. } else {
  31. $not_installed = $possible_plugins;
  32. }
  33. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  34. echo '<table class="data_table">';
  35. echo '<tr>';
  36. echo '<th width="400px">';
  37. echo get_lang('Plugin');
  38. echo '</th><th>';
  39. echo get_lang('Regions');
  40. echo '</th>';
  41. echo '</th>';
  42. echo '</tr>';
  43. /* We display all the possible plugins and the checkboxes */
  44. $plugin_list = array();
  45. $my_plugin_list = $plugin_obj->get_plugin_regions();
  46. foreach($my_plugin_list as $plugin_item) {
  47. $plugin_list[$plugin_item] = $plugin_item;
  48. }
  49. foreach ($installed_plugins as $plugin) {
  50. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$plugin.'/plugin.php';
  51. if (file_exists($plugin_info_file)) {
  52. $plugin_info = array();
  53. require $plugin_info_file;
  54. if (isset($_GET['name']) && $_GET['name'] == $plugin) {
  55. echo '<tr class="row_selected">';
  56. } else {
  57. echo '<tr>';
  58. }
  59. echo '<td>';
  60. echo '<h4>'.$plugin_info['title'].' <small>v'.$plugin_info['version'].'</small></h4>';
  61. echo '<p>'.$plugin_info['comment'].'</p>';
  62. echo '</td><td>';
  63. $selected_plugins = $plugin_obj->get_areas_by_plugin($plugin);
  64. echo Display::select('plugin_'.$plugin.'[]', $plugin_list, $selected_plugins, array('multiple' => 'multiple', 'style' => 'width:500px'), true, get_lang('None'));
  65. echo '</td></tr>';
  66. }
  67. }
  68. echo '</table>';
  69. echo '<br />';
  70. echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
  71. echo '<br />';
  72. }
  73. function handle_extensions() {
  74. echo '<div class="page-header"><h2>'.get_lang('ConfigureExtensions').'</h2></div>';
  75. echo '<a class="btn" href="configure_extensions.php?display=ppt2lp">'.get_lang('Ppt2lp').'</a>';
  76. }
  77. /**
  78. * This function allows easy activating and inactivating of plugins
  79. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  80. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  81. * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
  82. */
  83. function handle_plugins() {
  84. $plugin_obj = new AppPlugin();
  85. if (isset($_POST['submit_plugins'])) {
  86. store_plugins();
  87. // Add event to the system log.
  88. $user_id = api_get_user_id();
  89. $category = $_GET['category'];
  90. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, api_get_utc_datetime(), $user_id);
  91. Display :: display_confirmation_message(get_lang('SettingsStored'));
  92. }
  93. $all_plugins = $plugin_obj->read_plugins_from_path();
  94. $installed_plugins = $plugin_obj->get_installed_plugins();
  95. $not_installed = array_diff($all_plugins, $installed_plugins);
  96. //Plugins NOT installed
  97. echo '<div class="page-header"><h2>'.get_lang('Plugins').'</h2></div>';
  98. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  99. echo '<table class="data_table">';
  100. echo '<tr>';
  101. echo '<th width="20px">';
  102. echo get_lang('Action');
  103. echo '</th><th>';
  104. echo get_lang('Description');
  105. echo '</th>';
  106. echo '</tr>';
  107. $plugin_list = array();
  108. $my_plugin_list = $plugin_obj->get_plugin_regions();
  109. foreach($my_plugin_list as $plugin_item) {
  110. $plugin_list[$plugin_item] = $plugin_item;
  111. }
  112. foreach ($all_plugins as $plugin) {
  113. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$plugin.'/plugin.php';
  114. if (file_exists($plugin_info_file)) {
  115. $plugin_info = array();
  116. require $plugin_info_file;
  117. if (in_array($plugin, $installed_plugins)) {
  118. echo '<tr class="row_selected">';
  119. } else {
  120. echo '<tr>';
  121. }
  122. //echo '<tr>';
  123. echo '<td>';
  124. //Checkbox
  125. if (in_array($plugin, $installed_plugins)) {
  126. echo '<input type="checkbox" name="plugin_'.$plugin.'[]" checked="checked">';
  127. } else {
  128. echo '<input type="checkbox" name="plugin_'.$plugin.'[]">';
  129. }
  130. echo '</td><td>';
  131. echo '<h4>'.$plugin_info['title'].' <small>v '.$plugin_info['version'].'</small></h4>';
  132. echo '<p>'.$plugin_info['comment'].'</p>';
  133. echo '<p>'.get_lang('Author').': '.$plugin_info['author'].'</p>';
  134. echo '<div class="btn-group">';
  135. if (in_array($plugin, $installed_plugins)) {
  136. echo Display::url(get_lang('Configure'), 'configure_plugin.php?name='.$plugin, array('class' => 'btn'));
  137. echo Display::url(get_lang('Regions'), 'settings.php?category=Regions&name='.$plugin, array('class' => 'btn'));
  138. }
  139. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$plugin.'/readme.txt')) {
  140. echo Display::url("readme.txt", api_get_path(WEB_PLUGIN_PATH).$plugin."/readme.txt", array('class' => 'btn ajax', '_target' => '_blank'));
  141. }
  142. echo '</div>';
  143. echo '</td></tr>';
  144. }
  145. }
  146. echo '</table>';
  147. echo '<br />';
  148. echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
  149. echo '<br />';
  150. }
  151. /**
  152. * This function allows the platform admin to choose the default stylesheet
  153. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  154. * @author Julio Montoya <gugli100@gmail.com>, Chamilo
  155. */
  156. function handle_stylesheets() {
  157. global $_configuration;
  158. // Current style.
  159. $currentstyle = api_get_setting('stylesheets');
  160. $is_style_changeable = false;
  161. if ($_configuration['access_url'] != 1) {
  162. $style_info = api_get_settings('stylesheets', '', 1, 0);
  163. $url_info = api_get_access_url($_configuration['access_url']);
  164. if ($style_info[0]['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  165. $is_style_changeable = true;
  166. echo '<div class="actions" id="stylesheetuploadlink">';
  167. Display::display_icon('upload_stylesheets.png',get_lang('UploadNewStylesheet'),'',ICON_SIZE_MEDIUM);
  168. echo '<a href="" onclick="javascript: document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\'; return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  169. echo '</div>';
  170. }
  171. } else {
  172. $is_style_changeable = true;
  173. echo '<div class="actions" id="stylesheetuploadlink">';
  174. Display::display_icon('upload_stylesheets.png',get_lang('UploadNewStylesheet'),'',ICON_SIZE_MEDIUM);
  175. echo '<a href="" onclick="javascript: document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\'; return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  176. echo '</div>';
  177. }
  178. $form = new FormValidator('stylesheet_upload', 'post', 'settings.php?category=stylesheets&showuploadform=true');
  179. $form->addElement('text', 'name_stylesheet', get_lang('NameStylesheet'), array('size' => '40', 'maxlength' => '40'));
  180. $form->addRule('name_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  181. $form->addElement('file', 'new_stylesheet', get_lang('UploadNewStylesheet'));
  182. $allowed_file_types = array('css', 'zip', 'jpeg', 'jpg', 'png', 'gif');
  183. $form->addRule('new_stylesheet', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  184. $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  185. $form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'), array('class'=>'save'));
  186. if ($form->validate() && is_writable(api_get_path(SYS_CODE_PATH).'css/')) {
  187. $values = $form->exportValues();
  188. $picture_element = & $form->getElement('new_stylesheet');
  189. $picture = $picture_element->getValue();
  190. $result = upload_stylesheet($values, $picture);
  191. // Add event to the system log.
  192. $user_id = api_get_user_id();
  193. $category = $_GET['category'];
  194. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, api_get_utc_datetime(), $user_id);
  195. if ($result) {
  196. Display::display_confirmation_message(get_lang('StylesheetAdded'));
  197. }
  198. } else {
  199. if (!is_writable(api_get_path(SYS_CODE_PATH).'css/')) {
  200. Display::display_error_message(api_get_path(SYS_CODE_PATH).'css/'.get_lang('IsNotWritable'));
  201. } else {
  202. if ($_GET['showuploadform'] == 'true') {
  203. echo '<div id="newstylesheetform">';
  204. } else {
  205. echo '<div id="newstylesheetform" style="display: none;">';
  206. }
  207. // Uploading a new stylesheet.
  208. if ($_configuration['access_url'] == 1) {
  209. $form->display();
  210. } else {
  211. if ($is_style_changeable) {
  212. $form->display();
  213. }
  214. }
  215. echo '</div>';
  216. }
  217. }
  218. ?>
  219. <script type="text/javascript">
  220. function load_preview(){
  221. $('#stylesheets_id').submit();
  222. }
  223. </script>
  224. <?php
  225. echo '<form id="stylesheets_id" name="stylesheets" class="form-search" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  226. echo '<br /><select name="style" onchange="load_preview(this)" >';
  227. $list_of_styles = array();
  228. $list_of_names = array();
  229. if ($handle = @opendir(api_get_path(SYS_PATH).'main/css/')) {
  230. $counter = 1;
  231. while (false !== ($style_dir = readdir($handle))) {
  232. if (substr($style_dir, 0, 1) == '.') { // Skip directories starting with a '.'
  233. continue;
  234. }
  235. $dirpath = api_get_path(SYS_PATH).'main/css/'.$style_dir;
  236. if (is_dir($dirpath)) {
  237. if ($style_dir != '.' && $style_dir != '..') {
  238. if (isset($_POST['style']) && $_POST['style'] == $style_dir) {
  239. $selected = 'selected="true"';
  240. } else {
  241. if (!isset($_POST['style']) && ($currentstyle == $style_dir || ($style_dir == 'chamilo' && !$currentstyle))) {
  242. $selected = 'selected="true"';
  243. } else {
  244. $selected = '';
  245. }
  246. }
  247. $show_name = ucwords(str_replace('_', ' ', $style_dir));
  248. if ($is_style_changeable) {
  249. $list_of_styles[$style_dir] = "<option value=\"".$style_dir."\" ".$selected." /> $show_name </option>";
  250. $list_of_names[$style_dir] = $show_name;
  251. //echo "<input type=\"radio\" name=\"style\" value=\"".$style_dir."\" ".$selected." onClick=\"parent.preview.location='style_preview.php?style=".$style_dir."';\"/>";
  252. //echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  253. } else {
  254. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  255. }
  256. echo '<br />';
  257. $counter++;
  258. }
  259. }
  260. }
  261. @closedir($handle);
  262. }
  263. //Sort styles in alphabetical order
  264. asort($list_of_names);
  265. foreach($list_of_names as $style_dir=>$item) {
  266. echo $list_of_styles[$style_dir];
  267. }
  268. //echo '</select><br />';
  269. echo '</select>&nbsp;&nbsp;';
  270. if ($is_style_changeable){
  271. echo '<button class="btn save" type="submit" name="submit_stylesheets"> '.get_lang('SaveSettings').' </button></form>';
  272. }
  273. }
  274. /**
  275. * Creates the folder (if needed) and uploads the stylesheet in it
  276. *
  277. * @param array $values the values of the form
  278. * @param array $picture the values of the uploaded file
  279. *
  280. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  281. * @version May 2008
  282. * @since Dokeos 1.8.5
  283. */
  284. function upload_stylesheet($values, $picture) {
  285. $result = false;
  286. // Valid name for the stylesheet folder.
  287. $style_name = api_preg_replace('/[^A-Za-z0-9]/', '', $values['name_stylesheet']);
  288. // Create the folder if needed.
  289. if (!is_dir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/')) {
  290. mkdir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/', api_get_permissions_for_new_directories());
  291. }
  292. $info = pathinfo($picture['name']);
  293. if ($info['extension'] == 'zip') {
  294. // Try to open the file and extract it in the theme.
  295. $zip = new ZipArchive();
  296. if ($zip->open($picture['tmp_name'])) {
  297. // Make sure all files inside the zip are images or css.
  298. $num_files = $zip->numFiles;
  299. $valid = true;
  300. $single_directory = true;
  301. $invalid_files = array();
  302. for ($i = 0; $i < $num_files; $i++) {
  303. $file = $zip->statIndex($i);
  304. if (substr($file['name'], -1) != '/') {
  305. $path_parts = pathinfo($file['name']);
  306. if (!in_array($path_parts['extension'], array('jpg', 'jpeg', 'png', 'gif', 'css'))) {
  307. $valid = false;
  308. $invalid_files[] = $file['name'];
  309. }
  310. }
  311. if (strpos($file['name'], '/') === false) {
  312. $single_directory = false;
  313. }
  314. }
  315. if (!$valid) {
  316. $error_string = '<ul>';
  317. foreach ($invalid_files as $invalid_file) {
  318. $error_string .= '<li>'.$invalid_file.'</li>';
  319. }
  320. $error_string .= '</ul>';
  321. Display::display_error_message(get_lang('ErrorStylesheetFilesExtensionsInsideZip').$error_string, false);
  322. } else {
  323. // If the zip does not contain a single directory, extract it.
  324. if (!$single_directory) {
  325. // Extract zip file.
  326. $zip->extractTo(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/');
  327. $result = true;
  328. } else {
  329. $extraction_path = api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/';
  330. for ($i = 0; $i < $num_files; $i++) {
  331. $entry = $zip->getNameIndex($i);
  332. if (substr($entry, -1) == '/') continue;
  333. $pos_slash = strpos($entry, '/');
  334. $entry_without_first_dir = substr($entry, $pos_slash + 1);
  335. // If there is still a slash, we need to make sure the directories are created.
  336. if (strpos($entry_without_first_dir, '/') !== false) {
  337. if (!is_dir($extraction_path.dirname($entry_without_first_dir))) {
  338. // Create it.
  339. @mkdir($extraction_path.dirname($entry_without_first_dir), $mode = 0777, true);
  340. }
  341. }
  342. $fp = $zip->getStream($entry);
  343. $ofp = fopen($extraction_path.dirname($entry_without_first_dir).'/'.basename($entry), 'w');
  344. while (!feof($fp)) {
  345. fwrite($ofp, fread($fp, 8192));
  346. }
  347. fclose($fp);
  348. fclose($ofp);
  349. }
  350. $result = true;
  351. }
  352. }
  353. $zip->close();
  354. } else {
  355. Display::display_error_message(get_lang('ErrorReadingZip').$info['extension'], false);
  356. }
  357. } else {
  358. // Simply move the file.
  359. move_uploaded_file($picture['tmp_name'], api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'.$picture['name']);
  360. $result = true;
  361. }
  362. return $result;
  363. }
  364. function store_regions() {
  365. $plugin_obj = new AppPlugin();
  366. // Get a list of all current 'Plugins' settings
  367. $installed_plugins = $plugin_obj->get_installed_plugins();
  368. $shortlist_installed = array();
  369. foreach ($installed_plugins as $plugin) {
  370. $shortlist_installed[] = $plugin['subkey'];
  371. }
  372. $shortlist_installed = array_flip(array_flip($shortlist_installed));
  373. $plugin_list = $plugin_obj->read_plugins_from_path();
  374. foreach ($plugin_list as $plugin) {
  375. if (isset($_POST['plugin_'.$plugin])) {
  376. $areas_to_installed = $_POST['plugin_'.$plugin];
  377. if (!empty($areas_to_installed)) {
  378. $plugin_obj->remove_all_regions($plugin);
  379. foreach ($areas_to_installed as $region) {
  380. if (!empty($region) && $region != '-1' ) {
  381. $plugin_obj->add_to_region($plugin, $region);
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. /**
  389. * This function allows easy activating and inactivating of plugins
  390. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  391. */
  392. function store_plugins() {
  393. $plugin_obj = new AppPlugin();
  394. // Get a list of all current 'Plugins' settings
  395. $plugin_list = $plugin_obj->read_plugins_from_path();
  396. $installed_plugins = array();
  397. foreach ($plugin_list as $plugin) {
  398. if (isset($_POST['plugin_'.$plugin])) {
  399. $plugin_obj->install($plugin);
  400. $installed_plugins[] = $plugin;
  401. }
  402. }
  403. if (!empty($installed_plugins)) {
  404. $remove_plugins = array_diff($plugin_list, $installed_plugins);
  405. } else {
  406. $remove_plugins = $plugin_list;
  407. }
  408. foreach ($remove_plugins as $plugin) {
  409. $plugin_obj->uninstall($plugin);
  410. }
  411. }
  412. /**
  413. * This function allows the platform admin to choose which should be the default stylesheet
  414. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  415. */
  416. function store_stylesheets() {
  417. global $_configuration;
  418. // Insert the stylesheet.
  419. $style = Database::escape_string($_POST['style']);
  420. if (is_style($style)) {
  421. api_set_setting('stylesheets', $style, null, 'stylesheets', $_configuration['access_url']);
  422. }
  423. return true;
  424. }
  425. /**
  426. * This function checks if the given style is a recognize style that exists in the css directory as
  427. * a standalone directory.
  428. * @param string Style
  429. * @return bool True if this style is recognized, false otherwise
  430. */
  431. function is_style($style) {
  432. $dir = api_get_path(SYS_PATH).'main/css/';
  433. $dirs = scandir($dir);
  434. $style = str_replace(array('/', '\\'), array('', ''), $style); // Avoid slashes or backslashes.
  435. if (in_array($style, $dirs) && is_dir($dir.$style)) {
  436. return true;
  437. }
  438. return false;
  439. }
  440. /**
  441. * Search options
  442. * TODO: support for multiple site. aka $_configuration['access_url'] == 1
  443. * @author Marco Villegas <marvil07@gmail.com>
  444. */
  445. function handle_search() {
  446. global $SettingsStored, $_configuration;
  447. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  448. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  449. $search_enabled = api_get_setting('search_enabled');
  450. $form = new FormValidator('search-options', 'post', api_get_self().'?category=Search');
  451. $renderer = & $form->defaultRenderer();
  452. //$renderer->setHeaderTemplate('<div class="sectiontitle">{header}</div>'."\n");
  453. //$renderer->setElementTemplate('<div class="sectioncomment">{label}</div>'."\n".'<div class="sectionvalue">{element}</div>'."\n");
  454. $renderer->setElementTemplate('<div class="row"><div class="label">{label}</div><div class="formw">{element}<!-- BEGIN label_2 --><span class="help-block">{label_2}</span><!-- END label_2 --></div></div>');
  455. $values = api_get_settings_options('search_enabled');
  456. $form->addElement('header', null, get_lang('SearchEnabledTitle'));
  457. $group = array ();
  458. if (is_array($values)) {
  459. foreach ($values as $key => $value) {
  460. $element = & $form->createElement('radio', 'search_enabled', '', get_lang($value['display_text']), $value['value']);
  461. /* $hide_element is not defined
  462. if ($hide_element) {
  463. $element->freeze();
  464. }
  465. */
  466. $group[] = $element;
  467. }
  468. }
  469. //SearchEnabledComment
  470. $form->addGroup($group, 'search_enabled', array(get_lang('SearchEnabledTitle'), get_lang('SearchEnabledComment')), '<br />', false);
  471. $search_enabled = api_get_setting('search_enabled');
  472. if ($form->validate()) {
  473. $formvalues = $form->exportValues();
  474. $r = api_set_settings_category('Search', 'false', $_configuration['access_url']);
  475. // Save the settings.
  476. foreach ($formvalues as $key => $value) {
  477. $result = api_set_setting($key, $value, null, null);
  478. }
  479. $search_enabled = $formvalues['search_enabled'];
  480. Display::display_confirmation_message($SettingsStored);
  481. }
  482. $specific_fields = get_specific_field_list();
  483. if ($search_enabled == 'true') {
  484. // Search_show_unlinked_results.
  485. //$form->addElement('header', null, get_lang('SearchShowUnlinkedResultsTitle'));
  486. //$form->addElement('label', null, get_lang('SearchShowUnlinkedResultsComment'));
  487. $values = api_get_settings_options('search_show_unlinked_results');
  488. $group = array ();
  489. foreach ($values as $key => $value) {
  490. $element = & $form->createElement('radio', 'search_show_unlinked_results', '', get_lang($value['display_text']), $value['value']);
  491. $group[] = $element;
  492. }
  493. $form->addGroup($group, 'search_show_unlinked_results', array(get_lang('SearchShowUnlinkedResultsTitle'),get_lang('SearchShowUnlinkedResultsComment')), '', false);
  494. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  495. // Search_prefilter_prefix.
  496. //$form->addElement('header', null, get_lang('SearchPrefilterPrefix'));
  497. //$form->addElement('label', null, get_lang('SearchPrefilterPrefixComment'));
  498. $sf_values = array();
  499. foreach ($specific_fields as $sf) {
  500. $sf_values[$sf['code']] = $sf['name'];
  501. }
  502. $group = array();
  503. $url = Display::div(Display::url(get_lang('AddSpecificSearchField'), 'specific_fields.php'), array('class'=>'sectioncomment'));
  504. if (empty($sf_values)) {
  505. $form->addElement('html', get_lang('SearchPrefilterPrefix').$url);
  506. } else {
  507. $form->addElement('select', 'search_prefilter_prefix', array(get_lang('SearchPrefilterPrefix'), $url), $sf_values, '');
  508. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  509. }
  510. }
  511. $default_values['search_enabled'] = $search_enabled;
  512. //$form->addRule('search_show_unlinked_results', get_lang('ThisFieldIsRequired'), 'required');
  513. $form->addElement('style_submit_button', 'submit', get_lang('Save'),'class="save"');
  514. $form->setDefaults($default_values);
  515. echo '<div id="search-options-form">';
  516. $form->display();
  517. echo '</div>';
  518. if ($search_enabled == 'true') {
  519. require_once api_get_path(LIBRARY_PATH).'sortabletable.class.php';
  520. $xapian_path = api_get_path(SYS_PATH).'searchdb';
  521. /*
  522. @todo Test the Xapian connection
  523. if (extension_loaded('xapian')) {
  524. require_once 'xapian.php';
  525. try {
  526. $db = new XapianDatabase($xapian_path.'/');
  527. } catch (Exception $e) {
  528. var_dump($e->getMessage());
  529. }
  530. require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
  531. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  532. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  533. $indexable = new IndexableChunk();
  534. $indexable->addValue("content", 'Test');
  535. $di = new DokeosIndexer();
  536. $di->connectDb(NULL, NULL, 'english');
  537. $di->addChunk($indexable);
  538. $did = $di->index();
  539. }
  540. */
  541. $xapian_loaded = Display::return_icon('bullet_green.gif', get_lang('Ok'));
  542. $dir_exists = Display::return_icon('bullet_green.gif', get_lang('Ok'));
  543. $dir_is_writable = Display::return_icon('bullet_green.gif', get_lang('Ok'));
  544. $specific_fields_exists = Display::return_icon('bullet_green.gif', get_lang('Ok'));
  545. //Testing specific fields
  546. if (empty($specific_fields)) {
  547. $specific_fields_exists = Display::return_icon('bullet_red.gif', get_lang('AddSpecificSearchField'));
  548. }
  549. //Testing xapian extension
  550. if (!extension_loaded('xapian')) {
  551. $xapian_loaded = Display::return_icon('bullet_red.gif', get_lang('Error'));
  552. }
  553. //Testing xapian searchdb path
  554. if (!is_dir($xapian_path)) {
  555. $dir_exists = Display::return_icon('bullet_red.gif', get_lang('Error'));
  556. }
  557. //Testing xapian searchdb path is writable
  558. if (!is_writable($xapian_path)) {
  559. $dir_is_writable = Display::return_icon('bullet_red.gif', get_lang('Error'));
  560. }
  561. $data[] = array(get_lang('XapianModuleInstalled'),$xapian_loaded);
  562. $data[] = array(get_lang('DirectoryExists').' - '.$xapian_path,$dir_exists);
  563. $data[] = array(get_lang('IsWritable').' - '.$xapian_path,$dir_is_writable);
  564. $data[] = array(get_lang('SpecificSearchFieldsAvailable') ,$specific_fields_exists);
  565. echo Display::tag('h3', get_lang('Settings'));
  566. $table = new SortableTableFromArray($data);
  567. $table->set_header(0, get_lang('Setting'), false);
  568. $table->set_header(1, get_lang('Status'), false);
  569. echo $table->display();
  570. //@todo windows support
  571. if (api_is_windows_os() == false) {
  572. $list_of_programs = array('pdftotext','ps2pdf', 'catdoc','html2text','unrtf', 'catppt', 'xls2csv');
  573. foreach($list_of_programs as $program) {
  574. $output = $ret_val = null;
  575. exec("which $program", $output, $ret_val);
  576. $icon = Display::return_icon('bullet_red.gif', get_lang('NotInstalled'));
  577. if (!empty($output[0])) {
  578. $icon = Display::return_icon('bullet_green.gif', get_lang('Installed'));
  579. }
  580. $data2[]= array($program, $output[0], $icon);
  581. }
  582. echo Display::tag('h3', get_lang('ProgramsNeededToConvertFiles'));
  583. $table = new SortableTableFromArray($data2);
  584. $table->set_header(0, get_lang('Program'), false);
  585. $table->set_header(1, get_lang('Path'), false);
  586. $table->set_header(2, get_lang('Status'), false);
  587. echo $table->display();
  588. } else {
  589. Display::display_warning_message(get_lang('YouAreUsingChamiloInAWindowsPlatformSadlyYouCantConvertDocumentsInOrderToSearchTheContentUsingThisTool'));
  590. }
  591. }
  592. }
  593. /**
  594. * Wrapper for the templates
  595. *
  596. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  597. * @version August 2008
  598. * @since Dokeos 1.8.6
  599. */
  600. function handle_templates() {
  601. if ($_GET['action'] != 'add') {
  602. echo '<div class="actions" style="margin-left: 1px;">';
  603. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('new_template.png', get_lang('AddTemplate'),'',ICON_SIZE_MEDIUM).'</a>';
  604. echo '</div>';
  605. }
  606. if ($_GET['action'] == 'add' || ($_GET['action'] == 'edit' && is_numeric($_GET['id']))) {
  607. add_edit_template();
  608. // Add event to the system log.
  609. $user_id = api_get_user_id();
  610. $category = $_GET['category'];
  611. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, api_get_utc_datetime(), $user_id);
  612. } else {
  613. if ($_GET['action'] == 'delete' && is_numeric($_GET['id'])) {
  614. delete_template($_GET['id']);
  615. // Add event to the system log
  616. $user_id = api_get_user_id();
  617. $category = $_GET['category'];
  618. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, api_get_utc_datetime(), $user_id);
  619. }
  620. display_templates();
  621. }
  622. }
  623. /**
  624. * Display a sortable table with all the templates that the platform administrator has defined.
  625. *
  626. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  627. * @version August 2008
  628. * @since Dokeos 1.8.6
  629. */
  630. function display_templates() {
  631. $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data', 1);
  632. $table->set_additional_parameters(array('category' => Security::remove_XSS($_GET['category'])));
  633. $table->set_header(0, get_lang('Image'), true, array('style' => 'width: 101px;'));
  634. $table->set_header(1, get_lang('Title'));
  635. $table->set_header(2, get_lang('Actions'), false, array('style' => 'width:50px;'));
  636. $table->set_column_filter(2, 'actions_filter');
  637. $table->set_column_filter(0, 'image_filter');
  638. $table->display();
  639. }
  640. /**
  641. * Gets the number of templates that are defined by the platform admin.
  642. *
  643. * @return integer
  644. *
  645. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  646. * @version August 2008
  647. * @since Dokeos 1.8.6
  648. */
  649. function get_number_of_templates() {
  650. // Database table definition.
  651. $table_system_template = Database :: get_main_table('system_template');
  652. // The sql statement.
  653. $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
  654. $result = Database::query($sql);
  655. $row = Database::fetch_array($result);
  656. // Returning the number of templates.
  657. return $row['total'];
  658. }
  659. /**
  660. * Gets all the template data for the sortable table.
  661. *
  662. * @param integer $from the start of the limit statement
  663. * @param integer $number_of_items the number of elements that have to be retrieved from the database
  664. * @param integer $column the column that is
  665. * @param string $direction the sorting direction (ASC or DESC�
  666. * @return array
  667. *
  668. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  669. * @version August 2008
  670. * @since Dokeos 1.8.6
  671. */
  672. function get_template_data($from, $number_of_items, $column, $direction) {
  673. // Database table definition.
  674. $table_system_template = Database :: get_main_table('system_template');
  675. // The sql statement.
  676. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  677. $sql .= " ORDER BY col$column $direction ";
  678. $sql .= " LIMIT $from,$number_of_items";
  679. $result = Database::query($sql);
  680. $return = array();
  681. while ($row = Database::fetch_array($result)) {
  682. $row['1'] = get_lang($row['1']);
  683. $return[] = $row;
  684. }
  685. // Returning all the information for the sortable table.
  686. return $return;
  687. }
  688. /**
  689. * display the edit and delete icons in the sortable table
  690. *
  691. * @param integer $id the id of the template
  692. * @return html code for the link to edit and delete the template
  693. *
  694. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  695. * @version August 2008
  696. * @since Dokeos 1.8.6
  697. */
  698. function actions_filter($id) {
  699. $return = '<a href="settings.php?category=Templates&amp;action=edit&amp;id='.Security::remove_XSS($id).'">'.Display::return_icon('edit.png', get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>';
  700. $return .= '<a href="settings.php?category=Templates&amp;action=delete&amp;id='.Security::remove_XSS($id).'" onClick="javascript:if(!confirm('."'".get_lang('ConfirmYourChoice')."'".')) return false;">'.Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>';
  701. return $return;
  702. }
  703. /**
  704. * Display the image of the template in the sortable table
  705. *
  706. * @param string $image the image
  707. * @return html code for the image
  708. *
  709. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  710. * @version August 2008
  711. * @since Dokeos 1.8.6
  712. */
  713. function image_filter($image) {
  714. if (!empty($image)) {
  715. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
  716. } else {
  717. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
  718. }
  719. }
  720. /**
  721. * Add (or edit) a template. This function displays the form and also takes care of uploading the image and storing the information in the database
  722. *
  723. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  724. * @version August 2008
  725. * @since Dokeos 1.8.6
  726. */
  727. function add_edit_template() {
  728. // Initialize the object.
  729. $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.Security::remove_XSS($_GET['action']).'&id='.Security::remove_XSS($_GET['id']));
  730. // Settting the form elements: the header.
  731. if ($_GET['action'] == 'add') {
  732. $title = get_lang('AddTemplate');
  733. } else {
  734. $title = get_lang('EditTemplate');
  735. }
  736. $form->addElement('header', '', $title);
  737. // Settting the form elements: the title of the template.
  738. $form->add_textfield('title', get_lang('Title'), false);
  739. // Settting the form elements: the content of the template (wysiwyg editor).
  740. $form->addElement('html_editor', 'template_text', get_lang('Text'), null, array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
  741. // Settting the form elements: the form to upload an image to be used with the template.
  742. $form->addElement('file','template_image',get_lang('Image'),'');
  743. // Settting the form elements: a little bit information about the template image.
  744. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  745. // Getting all the information of the template when editing a template.
  746. if ($_GET['action'] == 'edit') {
  747. // Database table definition.
  748. $table_system_template = Database :: get_main_table('system_template');
  749. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($_GET['id'])."'";
  750. $result = Database::query($sql);
  751. $row = Database::fetch_array($result);
  752. $defaults['template_id'] = intval($_GET['id']);
  753. $defaults['template_text'] = $row['content'];
  754. // Forcing get_lang().
  755. $defaults['title'] = get_lang($row['title']);
  756. // Adding an extra field: a hidden field with the id of the template we are editing.
  757. $form->addElement('hidden', 'template_id');
  758. // Adding an extra field: a preview of the image that is currently used.
  759. if (!empty($row['image'])) {
  760. $form->addElement('static', 'template_image_preview', '', '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$row['image'].'" alt="'.get_lang('TemplatePreview').'"/>');
  761. } else {
  762. $form->addElement('static', 'template_image_preview', '', '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>');
  763. }
  764. // Setting the information of the template that we are editing.
  765. $form->setDefaults($defaults);
  766. }
  767. // Settting the form elements: the submit button.
  768. $form->addElement('style_submit_button' , 'submit', get_lang('Ok') ,'class="save"');
  769. // Setting the rules: the required fields.
  770. $form->addRule('title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  771. $form->addRule('template_text', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  772. // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
  773. if ($form->validate()) {
  774. $check = Security::check_token('post');
  775. if ($check) {
  776. // Exporting the values.
  777. $values = $form->exportValues();
  778. // Upload the file.
  779. if (!empty($_FILES['template_image']['name'])) {
  780. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  781. $upload_ok = process_uploaded_file($_FILES['template_image']);
  782. if ($upload_ok) {
  783. // Try to add an extension to the file if it hasn't one.
  784. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  785. // The upload directory.
  786. $upload_dir = api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/';
  787. // Create the directory if it does not exist.
  788. if (!is_dir($upload_dir)) {
  789. mkdir($upload_dir, api_get_permissions_for_new_directories());
  790. }
  791. // Resize the preview image to max default and upload.
  792. $temp = new Image($_FILES['template_image']['tmp_name']);
  793. $picture_info = $temp->get_image_info();
  794. $max_width_for_picture = 100;
  795. if ($picture_info['width'] > $max_width_for_picture) {
  796. $thumbwidth = $max_width_for_picture;
  797. if (empty($thumbwidth) || $thumbwidth == 0) {
  798. $thumbwidth = $max_width_for_picture;
  799. }
  800. $new_height = round(($thumbwidth / $picture_info['width']) * $picture_info['height']);
  801. $temp->resize($thumbwidth, $new_height, 0);
  802. }
  803. $temp->send_image($upload_dir.$new_file_name);
  804. }
  805. }
  806. // Store the information in the database (as insert or as update).
  807. $table_system_template = Database :: get_main_table('system_template');
  808. if ($_GET['action'] == 'add') {
  809. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  810. $sql = "INSERT INTO $table_system_template (title, content, image) VALUES ('".Database::escape_string($values['title'])."','".$content_template."','".Database::escape_string($new_file_name)."')";
  811. $result = Database::query($sql);
  812. // Display a feedback message.
  813. Display::display_confirmation_message(get_lang('TemplateAdded'));
  814. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('new_template.png', get_lang('AddTemplate'),'',ICON_SIZE_MEDIUM).'</a>';
  815. } else {
  816. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  817. $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."', content = '".$content_template."'";
  818. if (!empty($new_file_name)) {
  819. $sql .= ", image = '".Database::escape_string($new_file_name)."'";
  820. }
  821. $sql .= " WHERE id='".Database::escape_string($_GET['id'])."'";
  822. $result = Database::query($sql);
  823. // Display a feedback message.
  824. Display::display_confirmation_message(get_lang('TemplateEdited'));
  825. }
  826. }
  827. Security::clear_token();
  828. display_templates();
  829. } else {
  830. $token = Security::get_token();
  831. $form->addElement('hidden','sec_token');
  832. $form->setConstants(array('sec_token' => $token));
  833. // Display the form.
  834. $form->display();
  835. }
  836. }
  837. /**
  838. * Delete a template
  839. *
  840. * @param integer $id the id of the template that has to be deleted
  841. *
  842. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  843. * @version August 2008
  844. * @since Dokeos 1.8.6
  845. */
  846. function delete_template($id) {
  847. // First we remove the image.
  848. $table_system_template = Database :: get_main_table('system_template');
  849. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  850. $result = Database::query($sql);
  851. $row = Database::fetch_array($result);
  852. if (!empty($row['image'])) {
  853. @unlink(api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
  854. }
  855. // Now we remove it from the database.
  856. $sql = "DELETE FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  857. $result = Database::query($sql);
  858. // Display a feedback message.
  859. Display::display_confirmation_message(get_lang('TemplateDeleted'));
  860. }
  861. /**
  862. * Returns the list of timezone identifiers used to populate the select
  863. *
  864. * @return array List of timezone identifiers
  865. *
  866. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  867. * @since Chamilo 1.8.7
  868. */
  869. function select_timezone_value() {
  870. return api_get_timezones();
  871. }
  872. /**
  873. * Returns an array containing the list of options used to populate the gradebook_number_decimals variable
  874. *
  875. * @return array List of gradebook_number_decimals options
  876. *
  877. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  878. */
  879. function select_gradebook_number_decimals() {
  880. return array('0', '1', '2');
  881. }
  882. /**
  883. * Updates the gradebook score custom values using the scoredisplay class of the
  884. * gradebook module
  885. *
  886. * @param array List of gradebook score custom values
  887. *
  888. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  889. */
  890. function update_gradebook_score_display_custom_values($values) {
  891. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
  892. $scoredisplay = ScoreDisplay::instance();
  893. $scores = $values['gradebook_score_display_custom_values_endscore'];
  894. $displays = $values['gradebook_score_display_custom_values_displaytext'];
  895. $nr_displays = count($displays);
  896. $final = array();
  897. for ($i = 1; $i < $nr_displays; $i++) {
  898. if (!empty($scores[$i]) && !empty($displays[$i])) {
  899. $final[$i]['score'] = $scores[$i];
  900. $final[$i]['display'] = $displays[$i];
  901. }
  902. }
  903. $scoredisplay->update_custom_score_display_settings($final);
  904. }
  905. function generate_settings_form($settings, $settings_by_access_list) {
  906. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  907. global $_configuration, $settings_to_avoid, $convert_byte_to_mega_list;
  908. $form = new FormValidator('settings', 'post', 'settings.php?category='.Security::remove_XSS($_GET['category']));
  909. $form ->addElement('hidden', 'search_field', Security::remove_XSS($_GET['search_field']));
  910. $default_values = array();
  911. $count_settings = count($settings);
  912. foreach ($settings as $row) {
  913. if (in_array($row['variable'], array_keys($settings_to_avoid))) { continue; }
  914. $anchor_name = $row['variable'].(!empty($row['subkey']) ? '_'.$row['subkey'] : '');
  915. $form->addElement('html',"\n<a name=\"$anchor_name\"></a>\n");
  916. ($count_settings % 10) < 5 ? $b = $count_settings - 10 : $b = $count_settings;
  917. if ($i % 10 == 0 and $i < $b AND $i != 0) {
  918. $form->addElement('html', '<div align="right">');
  919. $form->addElement('style_submit_button', null, get_lang('SaveSettings'), 'class="save"');
  920. $form->addElement('html', '</div>');
  921. }
  922. $i++;
  923. if ( $_configuration['multiple_access_urls']) {
  924. if (api_is_global_platform_admin()) {
  925. if ($row['access_url_changeable'] == '1') {
  926. $form->addElement('html', '<div style="float: right;"><a class="share_this_setting" data_status = "0" data_to_send = "'.$row['id'].'" href="javascript:void(0);">'.Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting')).'</a></div>');
  927. } else {
  928. $form->addElement('html', '<div style="float: right;"><a class="share_this_setting" data_status = "1" data_to_send = "'.$row['id'].'" href="javascript:void(0);">'.Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting')).'</a></div>');
  929. }
  930. }
  931. }
  932. $hideme = array();
  933. $hide_element = false;
  934. if ($_configuration['access_url'] != 1) {
  935. if ($row['access_url_changeable'] == 0) {
  936. // We hide the element in other cases (checkbox, radiobutton) we 'freeze' the element.
  937. $hide_element = true;
  938. $hideme = array('disabled');
  939. } elseif ($url_info['active'] == 1) {
  940. // We show the elements.
  941. if (empty($row['variable']))
  942. $row['variable'] = 0;
  943. if (empty($row['subkey']))
  944. $row['subkey'] = 0;
  945. if (empty($row['category']))
  946. $row['category'] = 0;
  947. if (is_array($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ])) {
  948. // We are sure that the other site have a selected value.
  949. if ($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ]['selected_value'] != '')
  950. $row['selected_value'] =$settings_by_access_list[$row['variable']] [$row['subkey']] [ $row['category'] ]['selected_value'];
  951. }
  952. // There is no else{} statement because we load the default $row['selected_value'] of the main Chamilo site.
  953. }
  954. }
  955. switch ($row['type']) {
  956. case 'textfield':
  957. if (in_array($row['variable'], $convert_byte_to_mega_list)) {
  958. $form->addElement('text', $row['variable'], array(get_lang($row['title']), get_lang($row['comment']), get_lang('MB')), array('maxlength' => '8'));
  959. $form->applyFilter($row['variable'], 'html_filter');
  960. $default_values[$row['variable']] = round($row['selected_value']/1024/1024, 1);
  961. } elseif ($row['variable'] == 'account_valid_duration') {
  962. $form->addElement('text', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), array('maxlength' => '5'));
  963. $form->applyFilter($row['variable'], 'html_filter');
  964. $default_values[$row['variable']] = $row['selected_value'];
  965. // For platform character set selection: Conversion of the textfield to a select box with valid values.
  966. } elseif ($row['variable'] == 'platform_charset') {
  967. $current_system_encoding = api_refine_encoding_id(trim($row['selected_value']));
  968. $valid_encodings = array_flip(api_get_valid_encodings());
  969. if (!isset($valid_encodings[$current_system_encoding])) {
  970. $is_alias_encoding = false;
  971. foreach ($valid_encodings as $encoding) {
  972. if (api_equal_encodings($encoding, $current_system_encoding)) {
  973. $is_alias_encoding = true;
  974. $current_system_encoding = $encoding;
  975. break;
  976. }
  977. }
  978. if (!$is_alias_encoding) {
  979. $valid_encodings[$current_system_encoding] = $current_system_encoding;
  980. }
  981. }
  982. foreach ($valid_encodings as $key => &$encoding) {
  983. if (api_is_encoding_supported($key) && Database::is_encoding_supported($key)) {
  984. $encoding = $key;
  985. } else {
  986. unset($valid_encodings[$key]);
  987. }
  988. }
  989. $form->addElement('select', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), $valid_encodings);
  990. $default_values[$row['variable']] = $current_system_encoding;
  991. } else {
  992. $form->addElement('text', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), $hideme);
  993. $form->applyFilter($row['variable'],'html_filter');
  994. $default_values[$row['variable']] = $row['selected_value'];
  995. }
  996. break;
  997. case 'textarea':
  998. if ($row['variable'] == 'header_extra_content') {
  999. $file = api_get_path(SYS_PATH).api_get_home_path().'header_extra_content.txt';
  1000. $value = '';
  1001. if (file_exists($file)) {
  1002. $value = file_get_contents($file);
  1003. }
  1004. $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10','cols'=>'50'), $hideme);
  1005. $default_values[$row['variable']] = $value;
  1006. } elseif ($row['variable'] == 'footer_extra_content') {
  1007. $file = api_get_path(SYS_PATH).api_get_home_path().'footer_extra_content.txt';
  1008. $value = '';
  1009. if (file_exists($file)) {
  1010. $value = file_get_contents($file);
  1011. }
  1012. $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10','cols'=>'50'), $hideme);
  1013. $default_values[$row['variable']] = $value;
  1014. } else {
  1015. $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10','cols'=>'50'), $hideme);
  1016. $default_values[$row['variable']] = $row['selected_value'];
  1017. }
  1018. break;
  1019. case 'radio':
  1020. $values = api_get_settings_options($row['variable']);
  1021. $group = array ();
  1022. if (is_array($values )) {
  1023. foreach ($values as $key => $value) {
  1024. $element = & $form->createElement('radio', $row['variable'], '', get_lang($value['display_text']), $value['value']);
  1025. if ($hide_element) {
  1026. $element->freeze();
  1027. }
  1028. $group[] = $element;
  1029. }
  1030. }
  1031. $form->addGroup($group, $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), '', false); //julio
  1032. $default_values[$row['variable']] = $row['selected_value'];
  1033. break;
  1034. case 'checkbox';
  1035. // 1. We collect all the options of this variable.
  1036. $sql = "SELECT * FROM $table_settings_current WHERE variable='".$row['variable']."' AND access_url = 1";
  1037. $result = Database::query($sql);
  1038. $group = array ();
  1039. while ($rowkeys = Database::fetch_array($result)) {
  1040. //if ($rowkeys['variable'] == 'course_create_active_tools' && $rowkeys['subkey'] == 'enable_search') { continue; }
  1041. // Profile tab option should be hidden when the social tool is enabled.
  1042. if (api_get_setting('allow_social_tool') == 'true') {
  1043. if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_profile') { continue; }
  1044. }
  1045. // Hiding the gradebook option.
  1046. if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_gradebook') { continue; }
  1047. $element = & $form->createElement('checkbox', $rowkeys['subkey'], '', get_lang($rowkeys['subkeytext']));
  1048. if ($row['access_url_changeable'] == 1) {
  1049. // 2. We look into the DB if there is a setting for a specific access_url.
  1050. $access_url = $_configuration['access_url'];
  1051. if (empty($access_url )) $access_url = 1;
  1052. $sql = "SELECT selected_value FROM $table_settings_current WHERE variable='".$rowkeys['variable']."' AND subkey='".$rowkeys['subkey']."' AND subkeytext='".$rowkeys['subkeytext']."' AND access_url = $access_url";
  1053. $result_access = Database::query($sql);
  1054. $row_access = Database::fetch_array($result_access);
  1055. if ($row_access['selected_value'] == 'true' && !$form->isSubmitted()) {
  1056. $element->setChecked(true);
  1057. }
  1058. } else {
  1059. if ($rowkeys['selected_value'] == 'true' && !$form->isSubmitted()) {
  1060. $element->setChecked(true);
  1061. }
  1062. }
  1063. if ($hide_element) {
  1064. $element->freeze();
  1065. }
  1066. $group[] = $element;
  1067. }
  1068. $form->addGroup($group, $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])),'');
  1069. break;
  1070. case 'link':
  1071. $form->addElement('static', null, array(get_lang($row['title']), get_lang($row['comment'])), get_lang('CurrentValue').' : '.$row['selected_value'], $hideme);
  1072. break;
  1073. /*
  1074. * To populate its list of options, the select type dynamically calls a function that must be called select_ + the name of the variable being displayed.
  1075. * The functions being called must be added to the file settings.lib.php.
  1076. */
  1077. case 'select':
  1078. $form->addElement('select', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), call_user_func('select_'.$row['variable']), $hideme);
  1079. $default_values[$row['variable']] = $row['selected_value'];
  1080. break;
  1081. case 'gradebook_ranking':
  1082. $value = explode('::', $row['selected_value']);
  1083. $form->addElement('text', 'gradebook_display['.$row['variable'].'][text]', array(get_lang($row['title']), get_lang($row['comment'])), array('class' => 'span1', 'value' => $value[0]), $hideme);
  1084. $form->addElement('text', 'gradebook_display['.$row['variable'].'][score]', array(get_lang($row['title']), get_lang($row['comment'])), array('class' => 'span3','value' => $value[1]), $hideme);
  1085. $renderer = $form -> defaultRenderer();
  1086. $renderer->setElementTemplate(' {label}<div class="controls"> {element} %= ', 'gradebook_display['.$row['variable'].'][text]');
  1087. $renderer->setElementTemplate(' {element}</div><br />', 'gradebook_display['.$row['variable'].'][score]');
  1088. break;
  1089. case 'custom':
  1090. /*$values = api_get_settings_options($row['variable']);
  1091. //$renderer = & $form->defaultRenderer();
  1092. //$renderer->setElementTemplate('{label} - {element}<!-- BEGIN label_2 --><span class="help-block">{label_2}</span><!-- END label_2 -->');
  1093. $numbers = array();
  1094. for($j=1;$j<=20;$j++) {
  1095. $numbers[$j] = $j;
  1096. }
  1097. if (!empty($values)) {
  1098. foreach($values as $option) {
  1099. $group = array();
  1100. $id = $option['id'];
  1101. $option_id = $row['variable']."[$id]";
  1102. $group[] = $form->createElement('text', $option_id.'[display_text]', array(get_lang($row['title']), get_lang($row['comment'])),'class="begin_model"');
  1103. $default_values[$option_id.'[display_text]'] = $option['display_text'];
  1104. $parts = api_grading_model_functions($option['value'], 'to_array');
  1105. $denominator = $parts['denominator'];
  1106. $j = 1;
  1107. foreach($parts['items'] as $item) {
  1108. $letter = $item['letter'];
  1109. $value = $item['value'];
  1110. $group[] =$form->createElement('static','<div>');
  1111. $class = 'number';
  1112. if ($j == 1) {
  1113. $class = 'first_number';
  1114. }
  1115. $group[] = $form->createElement('select', $option_id.'[items]['.$j.']', array('dd'), $numbers, array('class'=>$class));
  1116. $sum = ' ';
  1117. if ($j != count($parts['items'])) {
  1118. $sum = ' + ';
  1119. }
  1120. //$group[] =$form->createElement('static',' * '.$letter.$sum);
  1121. $default_values[$option_id.'[items]['.$j.']'] = $value;
  1122. $j++;
  1123. }
  1124. $group[] = $form->createElement('select', $option_id.'[denominator]', array('/'), $numbers,'class="denominator"');
  1125. $group[] = $form->createElement('button', "delete", get_lang('Delete'), array('type'=>'button', 'class' => 'btn btn-danger','id'=>$id, 'onclick'=>"delete_grading_model('$id');"));
  1126. $default_values[$option_id.'[denominator]'] = $denominator;
  1127. $form->addGroup($group, '', get_lang($row['title']), ' ');
  1128. }
  1129. }
  1130. //New Grading Model form
  1131. $group = array();
  1132. $group[] = $form->createElement('text', 'new_model', array(get_lang('AddNewModel')));
  1133. $group[] = $form->createElement('select', 'number_evaluations', array(''), $numbers,'');
  1134. $form->addGroup($group, '', get_lang('AddNewModel'), "&nbsp;&nbsp;".get_lang('NumberOfSubEvaluations')."&nbsp;");
  1135. $form->addElement('style_submit_button', null, get_lang('Add'), 'class="add"');
  1136. */
  1137. break;
  1138. /*
  1139. * Used to display custom values for the gradebook score display
  1140. */
  1141. /* this configuration is moved now inside gradebook tool
  1142. case 'gradebook_score_display_custom':
  1143. if(api_get_setting('gradebook_score_display_custom', 'my_display_custom') == 'false') {
  1144. $form->addElement('static', null, null, get_lang('GradebookActivateScoreDisplayCustom'));
  1145. } else {
  1146. // Get score displays.
  1147. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
  1148. $scoredisplay = ScoreDisplay::instance();
  1149. $customdisplays = $scoredisplay->get_custom_score_display_settings();
  1150. $nr_items = (count($customdisplays)!='0') ? count($customdisplays) : '1';
  1151. $form->addElement('hidden', 'gradebook_score_display_custom_values_maxvalue', '100');
  1152. $form->addElement('hidden', 'gradebook_score_display_custom_values_minvalue', '0');
  1153. $form->addElement('static', null, null, get_lang('ScoreInfo'));
  1154. $scorenull[] = $form->CreateElement('static', null, null, get_lang('Between'));
  1155. $form->setDefaults(array (
  1156. 'beginscore' => '0'
  1157. ));
  1158. $scorenull[] = $form->CreateElement('text', 'beginscore', null, array (
  1159. 'size' => 5,
  1160. 'maxlength' => 5,
  1161. 'disabled' => 'disabled'
  1162. ));
  1163. $scorenull[] = $form->CreateElement('static', null, null, ' %');
  1164. $form->addGroup($scorenull, '', '', ' ');
  1165. for ($counter= 1; $counter <= 20; $counter++) {
  1166. $renderer = $form->defaultRenderer();
  1167. $elementTemplateTwoLabel =
  1168. '<div id=' . $counter . ' style="display: '.(($counter<=$nr_items)?'inline':'none').';">
  1169. <p><!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  1170. <div class="formw"><!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> <b>'.get_lang('And').'</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp{element} % =';
  1171. $elementTemplateTwoLabel2 =
  1172. '<!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error -->&nbsp{element}
  1173. <a href="javascript:minItem(' . ($counter) . ')"><img style="display: '.(($counter >= $nr_items && $counter != 1) ? 'inline' : 'none').';" id="min-' . $counter . '" src="../img/gradebook_remove.gif" alt="'.get_lang('Delete').'" title="'.get_lang('Delete').'"></img></a>
  1174. <a href="javascript:plusItem(' . ($counter+1) . ')"><img style="display: '.(($counter >= $nr_items) ? 'inline' : 'none').';" id="plus-' . ($counter+1) . '" src="../img/gradebook_add.gif" alt="'.get_lang('Add').'" title="'.get_lang('Add').'"></img></a>
  1175. </div></p></div>';
  1176. $scorebetw= array ();
  1177. $form->addElement('text', 'gradebook_score_display_custom_values_endscore[' . $counter . ']', null, array (
  1178. 'size' => 5,
  1179. 'maxlength' => 5,
  1180. 'id' => 'txta-'.$counter
  1181. ));
  1182. $form->addElement('text', 'gradebook_score_display_custom_values_displaytext[' . $counter . ']', null,array (
  1183. 'size' => 40,
  1184. 'maxlength' => 40,
  1185. 'id' => 'txtb-'.$counter
  1186. ));
  1187. $renderer->setElementTemplate($elementTemplateTwoLabel,'gradebook_score_display_custom_values_endscore[' . $counter . ']');
  1188. $renderer->setElementTemplate($elementTemplateTwoLabel2,'gradebook_score_display_custom_values_displaytext[' . $counter . ']');
  1189. $form->addRule('gradebook_score_display_custom_values_endscore[' . $counter . ']', get_lang('OnlyNumbers'), 'numeric');
  1190. $form->addRule(array('gradebook_score_display_custom_values_endscore[' . $counter . ']', 'gradebook_score_display_custom_values_maxvalue'), get_lang('Over100'), 'compare', '<=');
  1191. $form->addRule(array('gradebook_score_display_custom_values_endscore[' . $counter . ']', 'gradebook_score_display_custom_values_minvalue'), get_lang('UnderMin'), 'compare', '>');
  1192. if ($customdisplays[$counter - 1]) {
  1193. $default_values['gradebook_score_display_custom_values_endscore['.$counter.']'] = $customdisplays[$counter - 1]['score'];
  1194. $default_values['gradebook_score_display_custom_values_displaytext['.$counter.']'] = $customdisplays[$counter - 1]['display'];
  1195. }
  1196. }
  1197. }
  1198. break;
  1199. */
  1200. }
  1201. if ($row['variable'] == 'pdf_export_watermark_enable') {
  1202. $url = PDF::get_watermark($course_code);
  1203. $form->addElement('file', 'pdf_export_watermark_path', get_lang('AddWaterMark'));
  1204. if ($url != false) {
  1205. $delete_url = '<a href="?delete_watermark">'.Display::return_icon('delete.png',get_lang('DelImage')).'</a>';
  1206. $form->addElement('html', '<a href="'.$url.'">'.$url.' '.$delete_url.'</a>');
  1207. }
  1208. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  1209. $form->addRule('pdf_export_watermark_path', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  1210. }
  1211. if ($row['variable'] == 'timezone_value') {
  1212. $timezone = $row['selected_value'];
  1213. if (empty($timezone)) {
  1214. $timezone = _api_get_timezone();
  1215. }
  1216. $form->addElement('html', sprintf(get_lang('LocalTimeUsingPortalTimezoneXIsY'), $timezone, api_get_local_time()));
  1217. }
  1218. }
  1219. if (!empty($settings)) {
  1220. $form->addElement('html', '<div style="text-align: right; clear: both;">');
  1221. $form->addElement('style_submit_button', null, get_lang('SaveSettings'), 'class="save"');
  1222. $form->addElement('html', '</div>');
  1223. $form->setDefaults($default_values);
  1224. }
  1225. return $form;
  1226. }
  1227. /**
  1228. * Searchs a platform setting
  1229. * @param string $search
  1230. * @return array
  1231. */
  1232. function search_setting($search) {
  1233. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  1234. $sql = "SELECT * FROM $table_settings_current GROUP BY variable ORDER BY id ASC ";
  1235. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1236. $settings = array();
  1237. $search = api_strtolower($search);
  1238. if (!empty($result)) {
  1239. foreach ($result as $setting) {
  1240. $found = false;
  1241. $title = api_strtolower(get_lang($setting['title']));
  1242. //try the title
  1243. if (strpos($title, $search) === false) {
  1244. $comment = api_strtolower(get_lang($setting['comment']));
  1245. //Try the comment
  1246. if (strpos($comment, $search) === false) {
  1247. //Try the variable name
  1248. if (strpos($setting['variable'], $search) === false) {
  1249. continue;
  1250. } else {
  1251. $found = true;
  1252. }
  1253. } else {
  1254. $found = true;
  1255. }
  1256. } else {
  1257. $found = true;
  1258. }
  1259. if ($found) {
  1260. $settings[] = $setting;
  1261. }
  1262. }
  1263. }
  1264. return $settings;
  1265. }