settings.lib.php 59 KB

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