settings.lib.php 59 KB

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