settings.lib.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. * The function that retrieves all the possible settings for a certain config setting
  14. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  15. */
  16. function get_settings_options($var)
  17. {
  18. $table_settings_options = Database :: get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  19. $sql = "SELECT * FROM $table_settings_options WHERE variable='$var'";
  20. $result = Database::query($sql);
  21. while ($row = Database::fetch_array($result))
  22. {
  23. $temp_array = array ('value' => $row['value'], 'display_text' => $row['display_text']);
  24. $settings_options_array[] = $temp_array;
  25. }
  26. return $settings_options_array;
  27. }
  28. /**
  29. * This function allows easy activating and inactivating of plugins
  30. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  31. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  32. */
  33. function handle_plugins()
  34. {
  35. global $SettingsStored;
  36. $userplugins = array();
  37. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  38. if (isset($_POST['submit_plugins']))
  39. {
  40. store_plugins();
  41. // add event to system log
  42. $time = time();
  43. $user_id = api_get_user_id();
  44. $category = $_GET['category'];
  45. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  46. Display :: display_confirmation_message(get_lang('SettingsStored'));
  47. }
  48. //echo get_lang('AvailablePlugins')."<br/>
  49. echo '<br />';
  50. /* We scan the plugin directory. Each folder is a potential plugin. */
  51. $pluginpath = api_get_path(SYS_PLUGIN_PATH);
  52. $handle = @opendir($pluginpath);
  53. while (false !== ($file = readdir($handle)))
  54. {
  55. if ($file <> '.' AND $file <> '..' AND is_dir(api_get_path(SYS_PLUGIN_PATH).$file))
  56. {
  57. $possibleplugins[] = $file;
  58. }
  59. }
  60. @closedir($handle);
  61. /* for each of the possible plugin dirs we check if a file plugin.php (that contains all the needed information about this plugin)
  62. can be found in the dir.
  63. this plugin.php file looks like
  64. $plugin_info['title']='The title of the plugin'; //
  65. $plugin_info['comment']="Some comment about the plugin";
  66. $plugin_info['location']=array("loginpage_menu", "campushomepage_menu","banner"); // the possible locations where the plugins can be used
  67. $plugin_info['version']='0.1 alpha'; // The version number of the plugin
  68. $plugin_info['author']='Patrick Cool'; // The author of the plugin
  69. */
  70. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.$_GET['category'].'">';
  71. echo "<table class=\"data_table\">";
  72. echo "<tr>";
  73. echo "<th>";
  74. echo get_lang('Plugin');
  75. echo "</th>";
  76. echo "<th>";
  77. echo get_lang('LoginPageMainArea');
  78. echo "</th>";
  79. echo "<th>";
  80. echo get_lang('LoginPageMenu');
  81. echo "</th>";
  82. echo "<th>";
  83. echo get_lang('CampusHomepageMainArea');
  84. echo "</th>";
  85. echo "<th>";
  86. echo get_lang('CampusHomepageMenu');
  87. echo "</th>";
  88. echo "<th>";
  89. echo get_lang('MyCoursesMainArea');
  90. echo "</th>";
  91. echo "<th>";
  92. echo get_lang('MyCoursesMenu');
  93. echo "</th>";
  94. echo "<th>";
  95. echo get_lang('Header');
  96. echo "</th>";
  97. echo "<th>";
  98. echo get_lang('Footer');
  99. echo "</th>";
  100. echo "</tr>";
  101. /* We retrieve all the active plugins. */
  102. //$sql = "SELECT * FROM $table_settings_current WHERE category='Plugins'";
  103. //$result = Database::query($sql);
  104. $result = api_get_settings('Plugins');
  105. //while ($row = Database::fetch_array($result))
  106. foreach($result as $row)
  107. {
  108. $usedplugins[$row['variable']][] = $row['selected_value'];
  109. }
  110. /* We display all the possible plugins and the checkboxes */
  111. foreach ($possibleplugins as $testplugin)
  112. {
  113. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$testplugin."/plugin.php";
  114. if (file_exists($plugin_info_file))
  115. {
  116. $plugin_info = array();
  117. include ($plugin_info_file);
  118. echo "<tr>";
  119. echo "<td>";
  120. foreach ($plugin_info as $key => $value)
  121. {
  122. if ($key <> 'location')
  123. {
  124. if ($key == 'title')
  125. {
  126. $value = '<strong>'.$value.'</strong>';
  127. }
  128. echo get_lang(ucwords($key)).': '.$value.'<br />';
  129. }
  130. }
  131. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$testplugin.'/readme.txt'))
  132. {
  133. echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$testplugin."/readme.txt'>readme.txt</a>";
  134. }
  135. echo "</td>";
  136. // column: LoginPageMainArea
  137. if(empty($usedplugins))
  138. {
  139. $usedplugins = array();
  140. }
  141. display_plugin_cell('loginpage_main', $plugin_info, $testplugin, $usedplugins);
  142. display_plugin_cell('loginpage_menu', $plugin_info, $testplugin, $usedplugins);
  143. display_plugin_cell('campushomepage_main', $plugin_info, $testplugin, $usedplugins);
  144. display_plugin_cell('campushomepage_menu', $plugin_info, $testplugin, $usedplugins);
  145. display_plugin_cell('mycourses_main', $plugin_info, $testplugin, $usedplugins);
  146. display_plugin_cell('mycourses_menu', $plugin_info, $testplugin, $usedplugins);
  147. display_plugin_cell('header', $plugin_info, $testplugin, $usedplugins);
  148. display_plugin_cell('footer', $plugin_info, $testplugin, $usedplugins);
  149. echo "</tr>";
  150. }
  151. }
  152. echo '</table>';
  153. echo '<br />';
  154. echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
  155. echo '<br />';
  156. }
  157. function display_plugin_cell($location, $plugin_info, $current_plugin, $active_plugins)
  158. {
  159. echo "<td align=\"center\">";
  160. if (in_array($location, $plugin_info['location']))
  161. {
  162. if (isset($active_plugins[$location]) && is_array($active_plugins[$location])
  163. && in_array($current_plugin, $active_plugins[$location]))
  164. {
  165. $checked = "checked";
  166. }
  167. else
  168. {
  169. $checked = '';
  170. }
  171. echo '<input type="checkbox" name="'.$current_plugin.'-'.$location.'" value="true" '.$checked.'/>';
  172. }
  173. echo "</td>";
  174. }
  175. /**
  176. * This function allows the platform admin to choose the default stylesheet
  177. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  178. * @author Julio Montoya <gugli100@gmail.com>, Chamilo
  179. */
  180. function handle_stylesheets()
  181. {
  182. global $_configuration;
  183. // Current style
  184. $currentstyle = api_get_setting('stylesheets');
  185. $is_style_changeable=false;
  186. if ($_configuration['access_url']!=1) {
  187. $style_info = api_get_settings('stylesheets','',1,0);
  188. $url_info = api_get_access_url($_configuration['access_url']);
  189. if ($style_info[0]['access_url_changeable']==1 && $url_info['active']==1) {
  190. $is_style_changeable=true;
  191. echo '<div class="actions" id="stylesheetuploadlink">';
  192. Display::display_icon('theme_add.gif');
  193. echo '<a href="" onclick="document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\';return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  194. echo '</div>';
  195. }
  196. } else {
  197. $is_style_changeable=true;
  198. echo '<div class="actions" id="stylesheetuploadlink">';
  199. Display::display_icon('theme_add.gif');
  200. echo '<a href="" onclick="document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\';return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  201. echo '</div>';
  202. }
  203. $form = new FormValidator('stylesheet_upload','post','settings.php?category=stylesheets&showuploadform=true');
  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');
  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->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'), array('class'=>'save'));
  211. if( $form->validate() AND is_writable(api_get_path(SYS_CODE_PATH).'css/')) {
  212. $values = $form->exportValues();
  213. $picture_element = & $form->getElement('new_stylesheet');
  214. $picture = $picture_element->getValue();
  215. upload_stylesheet($values, $picture);
  216. // add event to system log
  217. $time = time();
  218. $user_id = api_get_user_id();
  219. $category = $_GET['category'];
  220. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  221. Display::display_confirmation_message(get_lang('StylesheetAdded'));
  222. } else {
  223. if (!is_writable(api_get_path(SYS_CODE_PATH).'css/'))
  224. {
  225. Display::display_error_message(api_get_path(SYS_CODE_PATH).'css/'.get_lang('IsNotWritable'));
  226. }
  227. else
  228. {
  229. if ($_GET['showuploadform'] == 'true')
  230. {
  231. echo '<div id="newstylesheetform">';
  232. }
  233. else
  234. {
  235. echo '<div id="newstylesheetform" style="display: none;">';
  236. }
  237. // uploading a new stylesheet
  238. if ($_configuration['access_url']==1)
  239. {
  240. $form->display();
  241. }
  242. else
  243. {
  244. if ($is_style_changeable)
  245. {
  246. $form->display();
  247. }
  248. }
  249. echo '</div>';
  250. }
  251. }
  252. // Preview of the stylesheet
  253. echo '<div><iframe src="style_preview.php" width="100%" height="300" name="preview"></iframe></div>';
  254. ?>
  255. <script type="text/javascript">
  256. function load_preview(selectobj){
  257. var style_dir = selectobj.options[selectobj.selectedIndex].value;
  258. parent.preview.location='style_preview.php?style=' + style_dir;
  259. }
  260. </script>
  261. <?php
  262. echo '<form name="stylesheets" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  263. echo '<br /><select name="style" onChange="load_preview(this)" >';
  264. $list_of_styles = array();
  265. $list_of_names = array();
  266. if ($handle = @opendir(api_get_path(SYS_PATH).'main/css/')) {
  267. $counter=1;
  268. while (false !== ($style_dir = readdir($handle))) {
  269. if(substr($style_dir,0,1)=='.') { //skip dirs starting with a '.'
  270. continue;
  271. }
  272. $dirpath = api_get_path(SYS_PATH).'main/css/'.$style_dir;
  273. if (is_dir($dirpath)) {
  274. if ($style_dir != '.' && $style_dir != '..') {
  275. if ($currentstyle == $style_dir OR ($style_dir == 'chamilo' AND !$currentstyle)) {
  276. $selected = 'selected="true"';
  277. } else {
  278. $selected = '';
  279. }
  280. $show_name=ucwords(str_replace('_',' ', $style_dir));
  281. if ($is_style_changeable) {
  282. $list_of_styles[$style_dir] = "<option value=\"".$style_dir."\" ".$selected." /> $show_name </option>";
  283. $list_of_names[$style_dir] = $show_name;
  284. //echo "<input type=\"radio\" name=\"style\" value=\"".$style_dir."\" ".$selected." onClick=\"parent.preview.location='style_preview.php?style=".$style_dir."';\"/>";
  285. //echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  286. } else {
  287. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  288. }
  289. echo "<br />";
  290. $counter++;
  291. }
  292. }
  293. }
  294. @closedir($handle);
  295. }
  296. //Sort styles in alphabetical order
  297. asort($list_of_names);
  298. foreach($list_of_names as $style_dir=>$item) {
  299. echo $list_of_styles[$style_dir];
  300. }
  301. //echo '</select><br />';
  302. echo '</select>&nbsp;&nbsp;';
  303. //var_dump($list_of_names);
  304. if ($is_style_changeable){
  305. echo '<button class="save" type="submit" name="submit_stylesheets"> '.get_lang('SaveSettings').' </button></form>';
  306. }
  307. }
  308. /**
  309. * creates the folder (if needed) and uploads the stylesheet in it
  310. *
  311. * @param array $values the values of the form
  312. * @param array $picture the values of the uploaded file
  313. *
  314. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  315. * @version May 2008
  316. * @since Dokeos 1.8.5
  317. */
  318. function upload_stylesheet($values,$picture)
  319. {
  320. // valid name for the stylesheet folder
  321. $style_name = api_preg_replace('/[^A-Za-z0-9]/', '', $values['name_stylesheet']);
  322. // create the folder if needed
  323. if (!is_dir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'))
  324. {
  325. mkdir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/', api_get_permissions_for_new_directories());
  326. }
  327. $info = pathinfo($picture['name']);
  328. if($info['extension'] == 'zip') {
  329. // Try to open the file and extract it in the theme
  330. $zip = new ZipArchive();
  331. if($zip->open($picture['tmp_name'])) {
  332. // Make sure all files inside the zip are images or css
  333. $numFiles = $zip->numFiles;
  334. $valid = true;
  335. $single_directory = true;
  336. $invalid_files = array();
  337. for($i =0; $i < $numFiles; $i++) {
  338. $file = $zip->statIndex($i);
  339. if(substr($file['name'], -1) != "/") {
  340. $path_parts = pathinfo($file['name']);
  341. if(!in_array($path_parts['extension'], array('jpg', 'jpeg', 'png', 'gif', 'css'))) {
  342. $valid = false;
  343. $invalid_files[] = $file['name'];
  344. }
  345. }
  346. if(strpos($file['name'], "/") === false) {
  347. $single_directory = false;
  348. }
  349. }
  350. if (!$valid) {
  351. $error_string = '<ul>';
  352. foreach($invalid_files as $invalid_file) {
  353. $error_string .= '<li>'.$invalid_file.'</li>';
  354. }
  355. $error_string .= '</ul>';
  356. Display::display_error_message(get_lang('ErrorStylesheetFilesExtensionsInsideZip').$error_string, false);
  357. } else {
  358. // If the zip does not contain a single directory, extract it
  359. if (!$single_directory) {
  360. // Extract zip file
  361. $zip->extractTo(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/');
  362. } else {
  363. $extraction_path = api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/';
  364. for($i = 0; $i < $numFiles; $i++) {
  365. $entry = $zip->getNameIndex($i);
  366. if ( substr($entry, -1) == '/') continue;
  367. $pos_slash = strpos($entry, '/');
  368. $entry_without_first_dir = substr($entry, $pos_slash + 1);
  369. // If there is still a slash, we need to make sure the directories are created
  370. if(strpos($entry_without_first_dir, '/') !== false) {
  371. if(!is_dir($extraction_path.dirname($entry_without_first_dir))) {
  372. // Create it
  373. @mkdir($extraction_path.dirname($entry_without_first_dir), $mode = 0777, true);
  374. }
  375. }
  376. $fp = $zip->getStream($entry);
  377. $ofp = fopen( $extraction_path. dirname($entry_without_first_dir).'/'.basename($entry), 'w');
  378. while ( ! feof($fp)) {
  379. fwrite($ofp, fread($fp, 8192));
  380. }
  381. fclose($fp);
  382. fclose($ofp);
  383. }
  384. }
  385. }
  386. $zip->close();
  387. } else {
  388. Display::display_error_message(get_lang('ErrorReadingZip').$info['extension'], false);
  389. }
  390. } else {
  391. // Simply move the file
  392. move_uploaded_file($picture['tmp_name'], api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'.$picture['name']);
  393. }
  394. }
  395. /**
  396. * This function allows easy activating and inactivating of plugins
  397. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  398. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  399. */
  400. function store_plugins()
  401. {
  402. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  403. global $_configuration;
  404. // Step 1 : we remove all the plugins
  405. //$sql = "DELETE FROM $table_settings_current WHERE category='Plugins'";
  406. //Database::query($sql);
  407. $r = api_delete_category_settings('Plugins',$_configuration['access_url']);
  408. // step 2: looping through all the post values we only store these which are really a valid plugin location.
  409. foreach ($_POST as $form_name => $formvalue)
  410. {
  411. $form_name_elements = explode("-", $form_name);
  412. if (is_valid_plugin_location($form_name_elements[1]))
  413. {
  414. //$sql = "INSERT into $table_settings_current (variable,category,selected_value) VALUES ('".$form_name_elements['1']."','Plugins','".$form_name_elements['0']."')";
  415. //Database::query($sql);
  416. api_add_setting($form_name_elements['0'],$form_name_elements['1'],$form_name_elements['0'],null,'Plugins',$form_name_elements['0'],null,null,null,$_configuration['access_url'],1);
  417. }
  418. }
  419. }
  420. /**
  421. * Check if the post information is really a valid plugin location.
  422. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  423. */
  424. function is_valid_plugin_location($location)
  425. {
  426. $valid_locations=array('loginpage_main', 'loginpage_menu', 'campushomepage_main', 'campushomepage_menu', 'mycourses_main', 'mycourses_menu','header', 'footer');
  427. if (in_array($location, $valid_locations))
  428. {
  429. return true;
  430. }
  431. else
  432. {
  433. return false;
  434. }
  435. }
  436. /**
  437. * This function allows the platform admin to choose which should be the default stylesheet
  438. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  439. */
  440. function store_stylesheets()
  441. {
  442. global $_configuration;
  443. // Database Table Definitions
  444. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  445. // Insert the stylesheet
  446. $style = Database::escape_string($_POST['style']);
  447. if (is_style($style))
  448. {
  449. /*
  450. $sql = 'UPDATE '.$table_settings_current.' SET
  451. selected_value = "'.$style.'"
  452. WHERE variable = "stylesheets"
  453. AND category = "stylesheets"';
  454. Database::query($sql);
  455. */
  456. api_set_setting('stylesheets',$style,null,'stylesheets',$_configuration['access_url']);
  457. }
  458. return true;
  459. }
  460. /**
  461. * This function checks if the given style is a recognize style that exists in the css directory as
  462. * a standalone directory.
  463. * @param string Style
  464. * @return bool True if this style is recognized, false otherwise
  465. */
  466. function is_style($style)
  467. {
  468. $dir = api_get_path(SYS_PATH).'main/css/';
  469. $dirs = scandir($dir);
  470. $style = str_replace(array('/','\\'),array('',''),$style); //avoid slashes or backslashes
  471. if (in_array($style,$dirs) && is_dir($dir.$style))
  472. {
  473. return true;
  474. }
  475. return false;
  476. }
  477. /**
  478. * Search options
  479. * TODO: support for multiple site. aka $_configuration['access_url'] == 1
  480. * @author Marco Villegas <marvil07@gmail.com>
  481. */
  482. function handle_search() {
  483. global $SettingsStored, $_configuration;
  484. $search_enabled = api_get_setting('search_enabled');
  485. $settings = api_get_settings('Search');
  486. if ($search_enabled !== 'true' || count($settings) < 1) {
  487. Display::display_error_message(get_lang('SearchFeatureNotEnabledComment'));
  488. return;
  489. }
  490. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  491. $form = new FormValidator('search-options', 'post', api_get_self().'?category=Search');
  492. $renderer = & $form->defaultRenderer();
  493. $renderer->setHeaderTemplate('<div class="sectiontitle">{header}</div>'."\n");
  494. $renderer->setElementTemplate('<div class="sectioncomment">{label}</div>'."\n".'<div class="sectionvalue">{element}</div>'."\n");
  495. //search_show_unlinked_results
  496. $form->addElement('header', null, get_lang('SearchShowUnlinkedResultsTitle'));
  497. $form->addElement('label', null, get_lang('SearchShowUnlinkedResultsComment'));
  498. $values = get_settings_options('search_show_unlinked_results');
  499. $group = array ();
  500. foreach ($values as $key => $value) {
  501. $element = & $form->createElement('radio', 'search_show_unlinked_results', '', get_lang($value['display_text']), $value['value']);
  502. $group[] = $element;
  503. }
  504. $form->addGroup($group, 'search_show_unlinked_results', get_lang('SearchShowUnlinkedResultsComment'), '<br />', false);
  505. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  506. //search_prefilter_prefix
  507. $form->addElement('header', null, get_lang('SearchPrefilterPrefix'));
  508. $form->addElement('label', null, get_lang('SearchPrefilterPrefixComment'));
  509. $specific_fields = get_specific_field_list();
  510. $sf_values = array();
  511. foreach ($specific_fields as $sf) {
  512. $sf_values[$sf['code']] = $sf['name'];
  513. }
  514. $group = array ();
  515. $form->addElement('select', 'search_prefilter_prefix', get_lang('SearchPrefilterPrefix'), $sf_values, '');
  516. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  517. //$form->addRule('search_show_unlinked_results', get_lang('ThisFieldIsRequired'), 'required');
  518. $form->addElement('style_submit_button', 'search-options-save', get_lang('Ok'));
  519. $form->setDefaults($default_values);
  520. if( $form->validate()) {
  521. $formvalues = $form->exportValues();
  522. $r = api_set_settings_category('Search','false',$_configuration['access_url']);
  523. // Save the settings
  524. foreach ($formvalues as $key => $value)
  525. {
  526. $result = api_set_setting($key, $value, null, null);
  527. }
  528. Display :: display_confirmation_message($SettingsStored);
  529. } else {
  530. echo '<div id="search-options-form">';
  531. $form->display();
  532. echo '</div>';
  533. }
  534. }
  535. /**
  536. * wrapper for the templates
  537. *
  538. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  539. * @version August 2008
  540. * @since Dokeos 1.8.6
  541. */
  542. function handle_templates()
  543. {
  544. if ($_GET['action'] != 'add') {
  545. echo "<div class=\"actions\" style=\"margin-left:1px\" >";
  546. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('template_add.gif', get_lang('AddTemplate')).get_lang('AddTemplate').'</a>';
  547. echo "</div>";
  548. }
  549. if ($_GET['action'] == 'add' OR ( $_GET['action'] == 'edit' AND is_numeric($_GET['id']))) {
  550. add_edit_template();
  551. // add event to system log
  552. $time = time();
  553. $user_id = api_get_user_id();
  554. $category = $_GET['category'];
  555. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  556. } else {
  557. if ($_GET['action'] == 'delete' and is_numeric($_GET['id'])) {
  558. delete_template($_GET['id']);
  559. // add event to system log
  560. $time = time();
  561. $user_id = api_get_user_id();
  562. $category = $_GET['category'];
  563. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  564. }
  565. display_templates();
  566. }
  567. }
  568. /**
  569. * Display a sortable table with all the templates that the platform administrator has defined.
  570. *
  571. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  572. * @version August 2008
  573. * @since Dokeos 1.8.6
  574. */
  575. function display_templates()
  576. {
  577. $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data',1);
  578. $table->set_additional_parameters(array('category'=>Security::remove_XSS($_GET['category'])));
  579. $table->set_header(0, get_lang('Image'), true, array ('style' => 'width:101px;'));
  580. $table->set_header(1, get_lang('Title'));
  581. $table->set_header(2, get_lang('Actions'), false, array ('style' => 'width:50px;'));
  582. $table->set_column_filter(2,'actions_filter');
  583. $table->set_column_filter(0,'image_filter');
  584. $table->display();
  585. }
  586. /**
  587. * Get the number of templates that are defined by the platform admin.
  588. *
  589. * @return integer
  590. *
  591. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  592. * @version August 2008
  593. * @since Dokeos 1.8.6
  594. */
  595. function get_number_of_templates()
  596. {
  597. // Database table definition
  598. $table_system_template = Database :: get_main_table('system_template');
  599. // The sql statement
  600. $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
  601. $result = Database::query($sql);
  602. $row = Database::fetch_array($result);
  603. // returning the number of templates
  604. return $row['total'];
  605. }
  606. /**
  607. * Get all the template data for the sortable table
  608. *
  609. * @param integer $from the start of the limit statement
  610. * @param integer $number_of_items the number of elements that have to be retrieved from the database
  611. * @param integer $column the column that is
  612. * @param string $direction the sorting direction (ASC or DESC�
  613. * @return array
  614. *
  615. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  616. * @version August 2008
  617. * @since Dokeos 1.8.6
  618. */
  619. function get_template_data($from, $number_of_items, $column, $direction)
  620. {
  621. // Database table definition
  622. $table_system_template = Database :: get_main_table('system_template');
  623. // the sql statement
  624. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  625. $sql .= " ORDER BY col$column $direction ";
  626. $sql .= " LIMIT $from,$number_of_items";
  627. $result = Database::query($sql);
  628. while ($row = Database::fetch_array($result)) {
  629. $row['1'] = get_lang($row['1']);
  630. $return[] = $row;
  631. }
  632. // returning all the information for the sortable table
  633. return $return;
  634. }
  635. /**
  636. * display the edit and delete icons in the sortable table
  637. *
  638. * @param integer $id the id of the template
  639. * @return html code for the link to edit and delete the template
  640. *
  641. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  642. * @version August 2008
  643. * @since Dokeos 1.8.6
  644. */
  645. function actions_filter($id)
  646. {
  647. $return .= '<a href="settings.php?category=Templates&amp;action=edit&amp;id='.Security::remove_XSS($id).'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  648. $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.gif', get_lang('Delete')).'</a>';
  649. return $return;
  650. }
  651. /**
  652. * Display the image of the template in the sortable table
  653. *
  654. * @param string $image the image
  655. * @return html code for the image
  656. *
  657. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  658. * @version August 2008
  659. * @since Dokeos 1.8.6
  660. */
  661. function image_filter($image)
  662. {
  663. if (!empty($image))
  664. {
  665. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
  666. }
  667. else
  668. {
  669. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
  670. }
  671. }
  672. /**
  673. * 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
  674. *
  675. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  676. * @version August 2008
  677. * @since Dokeos 1.8.6
  678. */
  679. function add_edit_template()
  680. {
  681. // initiate the object
  682. $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.Security::remove_XSS($_GET['action']).'&id='.Security::remove_XSS($_GET['id']));
  683. // settting the form elements: the header
  684. if ($_GET['action'] == 'add') {
  685. $title = get_lang('AddTemplate');
  686. }
  687. else
  688. {
  689. $title = get_lang('EditTemplate');
  690. }
  691. $form->addElement('header', '', $title);
  692. // settting the form elements: the title of the template
  693. $form->add_textfield('title', get_lang('Title'), false);
  694. // settting the form elements: the content of the template (wysiwyg editor)
  695. $form->addElement('html_editor', 'template_text', get_lang('Text'), null, array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
  696. // settting the form elements: the form to upload an image to be used with the template
  697. $form->addElement('file','template_image',get_lang('Image'),'');
  698. // settting the form elements: a little bit information about the template image
  699. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  700. // getting all the information of the template when editing a template
  701. if ($_GET['action'] == 'edit') {
  702. // Database table definition
  703. $table_system_template = Database :: get_main_table('system_template');
  704. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($_GET['id'])."'";
  705. $result = Database::query($sql);
  706. $row = Database::fetch_array($result);
  707. $defaults['template_id'] = intval($_GET['id']);
  708. $defaults['template_text'] = $row['content'];
  709. //forcing a get_lang
  710. $defaults['title'] = get_lang($row['title']);
  711. // adding an extra field: a hidden field with the id of the template we are editing
  712. $form->addElement('hidden','template_id');
  713. // adding an extra field: a preview of the image that is currently used
  714. if (!empty($row['image']))
  715. {
  716. $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').'"/>');
  717. }
  718. else
  719. {
  720. $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').'"/>');
  721. }
  722. // setting the information of the template that we are editing
  723. $form->setDefaults($defaults);
  724. }
  725. // settting the form elements: the submit button
  726. $form->addElement('style_submit_button' , 'submit', get_lang('Ok') ,'class="save"');
  727. // setting the rules: the required fields
  728. $form->addRule('title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  729. $form->addRule('template_text', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  730. // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
  731. if( $form->validate() )
  732. {
  733. $check = Security::check_token('post');
  734. if ($check) {
  735. // exporting the values
  736. $values = $form->exportValues();
  737. // upload the file
  738. if (!empty($_FILES['template_image']['name']))
  739. {
  740. include_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  741. $upload_ok = process_uploaded_file($_FILES['template_image']);
  742. if ($upload_ok)
  743. {
  744. // Try to add an extension to the file if it hasn't one
  745. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  746. // upload dir
  747. $upload_dir = api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/';
  748. // create dir if not exists
  749. if (!is_dir($upload_dir)) {
  750. mkdir($upload_dir, api_get_permissions_for_new_directories());
  751. }
  752. // resize image to max default and upload
  753. require_once (api_get_path(LIBRARY_PATH).'image.lib.php');
  754. $temp = new image($_FILES['template_image']['tmp_name']);
  755. $picture_infos=@getimagesize($_FILES['template_image']['tmp_name']);
  756. $max_width_for_picture = 100;
  757. if ($picture_infos[0]>$max_width_for_picture)
  758. {
  759. $thumbwidth = $max_width_for_picture;
  760. if (empty($thumbwidth) or $thumbwidth==0) {
  761. $thumbwidth=$max_width_for_picture;
  762. }
  763. $new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
  764. $temp->resize($thumbwidth,$new_height,0);
  765. }
  766. $type=$picture_infos[2];
  767. switch (!empty($type))
  768. {
  769. case 2 : $temp->send_image('JPG',$upload_dir.$new_file_name);
  770. break;
  771. case 3 : $temp->send_image('PNG',$upload_dir.$new_file_name);
  772. break;
  773. case 1 : $temp->send_image('GIF',$upload_dir.$new_file_name);
  774. break;
  775. }
  776. }
  777. }
  778. // store the information in the database (as insert or as update)
  779. $table_system_template = Database :: get_main_table('system_template');
  780. if ($_GET['action'] == 'add') {
  781. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  782. $sql = "INSERT INTO $table_system_template (title, content, image) VALUES ('".Database::escape_string($values['title'])."','".$content_template."','".Database::escape_string($new_file_name)."')";
  783. $result = Database::query($sql);
  784. // display a feedback message
  785. Display::display_confirmation_message(get_lang('TemplateAdded'));
  786. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('template_add.gif', get_lang('AddTemplate')).get_lang('AddTemplate').'</a>';
  787. } else {
  788. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  789. $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."',
  790. content = '".$content_template."'";
  791. if (!empty($new_file_name))
  792. {
  793. $sql .= ", image = '".Database::escape_string($new_file_name)."'";
  794. }
  795. $sql .= " WHERE id='".Database::escape_string($_GET['id'])."'";
  796. $result = Database::query($sql);
  797. // display a feedback message
  798. Display::display_confirmation_message(get_lang('TemplateEdited'));
  799. }
  800. }
  801. Security::clear_token();
  802. display_templates();
  803. }
  804. else
  805. {
  806. $token = Security::get_token();
  807. $form->addElement('hidden','sec_token');
  808. $form->setConstants(array('sec_token' => $token));
  809. // display the form
  810. $form->display();
  811. }
  812. }
  813. /**
  814. * Delete a template
  815. *
  816. * @param integer $id the id of the template that has to be deleted
  817. *
  818. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  819. * @version August 2008
  820. * @since Dokeos 1.8.6
  821. */
  822. function delete_template($id)
  823. {
  824. // first we remove the image
  825. $table_system_template = Database :: get_main_table('system_template');
  826. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  827. $result = Database::query($sql);
  828. $row = Database::fetch_array($result);
  829. if (!empty($row['image']))
  830. {
  831. @unlink(api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
  832. }
  833. // now we remove it from the database
  834. $sql = "DELETE FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  835. $result = Database::query($sql);
  836. // display a feedback message
  837. Display::display_confirmation_message(get_lang('TemplateDeleted'));
  838. }
  839. /**
  840. * Returns the list of timezone identifiers used to populate the select
  841. *
  842. * @return array List of timezone identifiers
  843. *
  844. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  845. * @since Chamilo 1.8.7
  846. */
  847. function select_timezone_value() {
  848. return api_get_timezones();
  849. }
  850. /**
  851. * Returns an array containing the list of options used to populate the gradebook_number_decimals variable
  852. *
  853. * @return array List of gradebook_number_decimals options
  854. *
  855. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  856. */
  857. function select_gradebook_number_decimals() {
  858. return array('0', '1', '2');
  859. }
  860. /**
  861. * Updates the gradebook score custom values using the scoredisplay class of the
  862. * gradebook module
  863. *
  864. * @param array List of gradebook score custom values
  865. *
  866. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  867. */
  868. function update_gradebook_score_display_custom_values($values) {
  869. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
  870. $scoredisplay = ScoreDisplay::instance();
  871. $scores = $values['gradebook_score_display_custom_values_endscore'];
  872. $displays = $values['gradebook_score_display_custom_values_displaytext'];
  873. $nr_displays = count($displays);
  874. $final = array();
  875. for($i = 1; $i < $nr_displays; $i++) {
  876. if(!empty($scores[$i]) && !empty($displays[$i])) {
  877. $final[$i]['score'] = $scores[$i];
  878. $final[$i]['display'] = $displays[$i];
  879. }
  880. }
  881. $scoredisplay->update_custom_score_display_settings($final);
  882. }