settings.lib.php 56 KB

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