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