settings.lib.php 58 KB

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