settings.lib.php 56 KB

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