FormValidator.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once api_get_path(LIBRARY_PATH).'pear/HTML/QuickForm.php';
  4. require_once api_get_path(LIBRARY_PATH).'pear/HTML/QuickForm/advmultiselect.php';
  5. /**
  6. * Filter
  7. */
  8. define('NO_HTML', 1);
  9. define('STUDENT_HTML', 2);
  10. define('TEACHER_HTML', 3);
  11. define('STUDENT_HTML_FULLPAGE', 4);
  12. define('TEACHER_HTML_FULLPAGE', 5);
  13. /**
  14. * Objects of this class can be used to create/manipulate/validate user input.
  15. */
  16. class FormValidator extends HTML_QuickForm
  17. {
  18. /**
  19. * Create a form validator based on an array of form data:
  20. *
  21. * array(
  22. * 'name' => 'zombie_report_parameters', //optional
  23. * 'method' => 'GET', //optional
  24. * 'items' => array(
  25. * array(
  26. * 'name' => 'ceiling',
  27. * 'label' => 'Ceiling', //optional
  28. * 'type' => 'date',
  29. * 'default' => date() //optional
  30. * ),
  31. * array(
  32. * 'name' => 'active_only',
  33. * 'label' => 'ActiveOnly',
  34. * 'type' => 'checkbox',
  35. * 'default' => true
  36. * ),
  37. * array(
  38. * 'name' => 'submit_button',
  39. * 'type' => 'style_submit_button',
  40. * 'value' => get_lang('Search'),
  41. * 'attributes' => array('class' => 'search')
  42. * )
  43. * )
  44. * );
  45. *
  46. * @param array form_data
  47. * @return FormValidator
  48. */
  49. static function create($form_data)
  50. {
  51. if (empty($form_data)) {
  52. return null;
  53. }
  54. $form_name = isset($form_data['name']) ? $form_data['name'] : 'form';
  55. $form_method = isset($form_data['method']) ? $form_data['method'] : 'POST';
  56. $form_action = isset($form_data['action']) ? $form_data['action'] : '';
  57. $form_target = isset($form_data['target']) ? $form_data['target'] : '';
  58. $form_attributes = isset($form_data['attributes']) ? $form_data['attributes'] : null;
  59. $form_track_submit = isset($form_data['track_submit']) ? $form_data['track_submit'] : true;
  60. $result = new FormValidator($form_name, $form_method, $form_action, $form_target, $form_attributes, $form_track_submit);
  61. $defaults = array();
  62. foreach ($form_data['items'] as $item) {
  63. $name = $item['name'];
  64. $type = isset($item['type']) ? $item['type'] : 'text';
  65. $label = isset($item['label']) ? $item['label'] : '';
  66. if ($type == 'wysiwyg') {
  67. $element = $result->add_html_editor($name, $label);
  68. } else {
  69. $element = $result->addElement($type, $name, $label);
  70. }
  71. if (isset($item['attributes'])) {
  72. $attributes = $item['attributes'];
  73. $element->setAttributes($attributes);
  74. }
  75. if (isset($item['value'])) {
  76. $value = $item['value'];
  77. $element->setValue($value);
  78. }
  79. if (isset($item['default'])) {
  80. $defaults[$name] = $item['default'];
  81. }
  82. if (isset($item['rules'])) {
  83. $rules = $item['rules'];
  84. foreach ($rules as $rule) {
  85. $message = $rule['message'];
  86. $type = $rule['type'];
  87. $format = isset($rule['format']) ? $rule['format'] : null;
  88. $validation = isset($rule['validation']) ? $rule['validation'] : 'server';
  89. $force = isset($rule['force']) ? $rule['force'] : false;
  90. $result->addRule($name, $message, $type, $format, $validation, $reset, $force);
  91. }
  92. }
  93. }
  94. $result->setDefaults($defaults);
  95. return $result;
  96. }
  97. var $with_progress_bar = false;
  98. /**
  99. * Constructor
  100. * @param string $form_name Name of the form
  101. * @param string $method (optional Method ('post' (default) or 'get')
  102. * @param string $action (optional Action (default is $PHP_SELF)
  103. * @param string $target (optional Form's target defaults to '_self'
  104. * @param mixed $attributes (optional) Extra attributes for <form> tag
  105. * @param bool $track_submit (optional) Whether to track if the form was
  106. * submitted by adding a special hidden field (default = true)
  107. */
  108. function __construct($form_name, $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
  109. {
  110. // Default form class.
  111. if (is_array($attributes) && !isset($attributes['class']) || empty($attributes)) {
  112. $attributes['class'] = 'form-horizontal';
  113. }
  114. parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
  115. // Load some custom elements and rules
  116. $dir = api_get_path(LIBRARY_PATH) . 'formvalidator/';
  117. $this->registerElementType('html_editor', $dir . 'Element/html_editor.php', 'HTML_QuickForm_html_editor');
  118. $this->registerElementType('date_range_picker', $dir . 'Element/DateRangePicker.php', 'DateRangePicker');
  119. $this->registerElementType('date_time_picker', $dir . 'Element/DateTimePicker.php', 'DateTimePicker');
  120. $this->registerElementType('date_picker', $dir . 'Element/DatePicker.php', 'DatePicker');
  121. $this->registerElementType('datepicker', $dir . 'Element/datepicker_old.php', 'HTML_QuickForm_datepicker');
  122. $this->registerElementType('datepickerdate', $dir . 'Element/datepickerdate.php', 'HTML_QuickForm_datepickerdate');
  123. $this->registerElementType('receivers', $dir . 'Element/receivers.php', 'HTML_QuickForm_receivers');
  124. $this->registerElementType('select_language', $dir . 'Element/select_language.php', 'HTML_QuickForm_Select_Language');
  125. $this->registerElementType('select_ajax', $dir . 'Element/select_ajax.php', 'HTML_QuickForm_Select_Ajax');
  126. $this->registerElementType('select_theme', $dir . 'Element/select_theme.php', 'HTML_QuickForm_Select_Theme');
  127. $this->registerElementType('style_submit_button', $dir . 'Element/style_submit_button.php', 'HTML_QuickForm_stylesubmitbutton');
  128. $this->registerElementType('style_reset_button', $dir . 'Element/style_reset_button.php', 'HTML_QuickForm_styleresetbutton');
  129. $this->registerElementType('button', $dir . 'Element/style_submit_button.php', 'HTML_QuickForm_stylesubmitbutton');
  130. $this->registerElementType('captcha', 'HTML/QuickForm/CAPTCHA.php', 'HTML_QuickForm_CAPTCHA');
  131. $this->registerElementType('CAPTCHA_Image', 'HTML/QuickForm/CAPTCHA/Image.php', 'HTML_QuickForm_CAPTCHA_Image');
  132. $this->registerRule('date', null, 'HTML_QuickForm_Rule_Date', $dir . 'Rule/Date.php');
  133. $this->registerRule('datetime', null, 'DateTimeRule', $dir . 'Rule/DateTimeRule.php');
  134. $this->registerRule('date_compare', null, 'HTML_QuickForm_Rule_DateCompare', $dir . 'Rule/DateCompare.php');
  135. $this->registerRule('html', null, 'HTML_QuickForm_Rule_HTML', $dir . 'Rule/HTML.php');
  136. $this->registerRule('username_available', null, 'HTML_QuickForm_Rule_UsernameAvailable', $dir . 'Rule/UsernameAvailable.php');
  137. $this->registerRule('username', null, 'HTML_QuickForm_Rule_Username', $dir . 'Rule/Username.php');
  138. $this->registerRule('filetype', null, 'HTML_QuickForm_Rule_Filetype', $dir . 'Rule/Filetype.php');
  139. $this->registerRule('multiple_required', 'required', 'HTML_QuickForm_Rule_MultipleRequired', $dir . 'Rule/MultipleRequired.php');
  140. $this->registerRule('url', null, 'HTML_QuickForm_Rule_Url', $dir . 'Rule/Url.php');
  141. $this->registerRule('telephone', null, 'HTML_QuickForm_Rule_Telephone', $dir . 'Rule/Telephone.php');
  142. $this->registerRule('compare_fields', null, 'HTML_QuickForm_Compare_Fields', $dir . 'Rule/CompareFields.php');
  143. $this->registerRule('CAPTCHA', 'rule', 'HTML_QuickForm_Rule_CAPTCHA', 'HTML/QuickForm/Rule/CAPTCHA.php');
  144. // Modify the default templates
  145. $renderer = & $this->defaultRenderer();
  146. //Form template
  147. $form_template = '<form{attributes}>
  148. <fieldset>
  149. {content}
  150. <div class="clear"></div>
  151. </fieldset>
  152. {hidden}
  153. </form>';
  154. $renderer->setFormTemplate($form_template);
  155. //Element template
  156. if (isset($attributes['class']) && $attributes['class'] == 'well form-inline') {
  157. $element_template = ' {label} {element} ';
  158. $renderer->setElementTemplate($element_template);
  159. } elseif (isset($attributes['class']) && $attributes['class'] == 'form-search') {
  160. $element_template = ' {label} {element} ';
  161. $renderer->setElementTemplate($element_template);
  162. } else {
  163. $element_template = '
  164. <div class="control-group {error_class}">
  165. <label class="control-label" {label-for}>
  166. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  167. {label}
  168. </label>
  169. <div class="controls">
  170. {element}
  171. <!-- BEGIN label_3 -->
  172. {label_3}
  173. <!-- END label_3 -->
  174. <!-- BEGIN label_2 -->
  175. <p class="help-block">{label_2}</p>
  176. <!-- END label_2 -->
  177. <!-- BEGIN error -->
  178. <span class="help-inline">{error}</span>
  179. <!-- END error -->
  180. </div>
  181. </div>';
  182. $renderer->setElementTemplate($element_template);
  183. //Display a gray div in the buttons
  184. $button_element_template_simple = '<div class="form-actions">{label} {element}</div>';
  185. $renderer->setElementTemplate($button_element_template_simple, 'submit_in_actions');
  186. //Display a gray div in the buttons + makes the button available when scrolling
  187. $button_element_template_in_bottom = '<div class="form-actions bottom_actions bg-form">{label} {element}</div>';
  188. $renderer->setElementTemplate($button_element_template_in_bottom, 'submit_fixed_in_bottom');
  189. //When you want to group buttons use something like this
  190. /* $group = array();
  191. $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll'));
  192. $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll'));
  193. $form->addGroup($group, 'buttons_in_action');
  194. */
  195. $renderer->setElementTemplate($button_element_template_simple, 'buttons_in_action');
  196. $button_element_template_simple_right = '<div class="form-actions"> <div class="pull-right">{label} {element}</div></div>';
  197. $renderer->setElementTemplate($button_element_template_simple_right, 'buttons_in_action_right');
  198. /*
  199. $renderer->setElementTemplate($button_element_template, 'submit_button');
  200. $renderer->setElementTemplate($button_element_template, 'submit');
  201. $renderer->setElementTemplate($button_element_template, 'button');
  202. *
  203. */
  204. }
  205. //Set Header template
  206. $renderer->setHeaderTemplate('<legend>{header}</legend>');
  207. //Set required field template
  208. HTML_QuickForm::setRequiredNote('<span class="form_required">*</span> <small>' . get_lang('ThisFieldIsRequired') . '</small>');
  209. $required_note_template = <<<EOT
  210. <div class="control-group">
  211. <div class="controls">{requiredNote}</div>
  212. </div>
  213. EOT;
  214. $renderer->setRequiredNoteTemplate($required_note_template);
  215. }
  216. /**
  217. * Adds a text field to the form.
  218. * A trim-filter is attached to the field.
  219. * @param string $label The label for the form-element
  220. * @param string $name The element name
  221. * @param boolean $required (optional) Is the form-element required (default=true)
  222. * @param array $attributes (optional) List of attributes for the form-element
  223. */
  224. function add_textfield($name, $label, $required = true, $attributes = array())
  225. {
  226. $this->addElement('text', $name, $label, $attributes);
  227. $this->applyFilter($name, 'trim');
  228. if ($required) {
  229. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  230. }
  231. }
  232. /**
  233. * date_range_picker element creates 2 hidden fields
  234. * elementName + "_start" elementName "_end"
  235. * @param string $name
  236. * @param string $label
  237. * @param bool $required
  238. * @param array $attributes
  239. */
  240. public function addDateRangePicker($name, $label, $required = true, $attributes = array())
  241. {
  242. $this->addElement('date_range_picker', $name, $label, $attributes);
  243. $this->addElement('hidden', $name.'_start');
  244. $this->addElement('hidden', $name.'_end');
  245. if ($required) {
  246. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  247. }
  248. }
  249. /**
  250. * @param string $name
  251. * @param string $value
  252. */
  253. function add_hidden($name, $value)
  254. {
  255. $this->addElement('hidden', $name, $value);
  256. }
  257. public function add_textarea($name, $label, $attributes = array())
  258. {
  259. $this->addElement('textarea', $name, $label, $attributes);
  260. }
  261. public function add_button($name, $label, $attributes = array())
  262. {
  263. $this->addElement('button', $name, $label, $attributes);
  264. }
  265. public function add_checkbox($name, $label, $trailer = '', $attributes = array())
  266. {
  267. $this->addElement('checkbox', $name, $label, $trailer, $attributes);
  268. }
  269. public function add_radio($name, $label, $options = '')
  270. {
  271. $group = array();
  272. foreach ($options as $key => $value) {
  273. $group[] = $this->createElement('radio', null, null, $value, $key);
  274. }
  275. $this->addGroup($group, $name, $label);
  276. }
  277. public function add_select($name, $label, $options = '', $attributes = array())
  278. {
  279. $this->addElement('select', $name, $label, $options, $attributes);
  280. }
  281. public function add_label($label, $text)
  282. {
  283. $this->addElement('label', $label, $text);
  284. }
  285. public function add_header($text)
  286. {
  287. $this->addElement('header', $text);
  288. }
  289. public function add_file($name, $label, $attributes = array())
  290. {
  291. $this->addElement('file', $name, $label, $attributes);
  292. }
  293. public function add_html($snippet)
  294. {
  295. $this->addElement('html', $snippet);
  296. }
  297. /**
  298. * Adds a HTML-editor to the form to fill in a title.
  299. * A trim-filter is attached to the field.
  300. * A HTML-filter is attached to the field (cleans HTML)
  301. * A rule is attached to check for unwanted HTML
  302. * @param string $name
  303. * @param string $label The label for the form-element
  304. * @param boolean $required (optional) Is the form-element required (default=true)
  305. * @param boolean $full_page (optional) When it is true, the editor loads completed html code for a full page.
  306. * @param array $editor_config (optional) Configuration settings for the online editor.
  307. */
  308. function add_html_editor($name, $label, $required = true, $full_page = false, $config = null)
  309. {
  310. $this->addElement('html_editor', $name, $label, 'rows="15" cols="80"', $config);
  311. $this->applyFilter($name, 'trim');
  312. $html_type = STUDENT_HTML;
  313. if (!empty($_SESSION['status'])) {
  314. $html_type = $_SESSION['status'] == COURSEMANAGER ? TEACHER_HTML : STUDENT_HTML;
  315. }
  316. if (is_array($config)) {
  317. if (isset($config['FullPage'])) {
  318. $full_page = is_bool($config['FullPage']) ? $config['FullPage'] : ($config['FullPage'] === 'true');
  319. } else {
  320. $config['FullPage'] = $full_page;
  321. }
  322. } else {
  323. $config = array('FullPage' => (bool) $full_page);
  324. }
  325. if ($full_page) {
  326. $html_type = isset($_SESSION['status']) && $_SESSION['status'] == COURSEMANAGER ? TEACHER_HTML_FULLPAGE : STUDENT_HTML_FULLPAGE;
  327. //First *filter* the HTML (markup, indenting, ...)
  328. //$this->applyFilter($name,'html_filter_teacher_fullpage');
  329. } else {
  330. //First *filter* the HTML (markup, indenting, ...)
  331. //$this->applyFilter($name,'html_filter_teacher');
  332. }
  333. if ($required) {
  334. $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required');
  335. }
  336. if ($full_page) {
  337. $el = $this->getElement($name);
  338. $el->fullPage = true;
  339. }
  340. // Add rule to check not-allowed HTML
  341. //$this->addRule($name, get_lang('SomeHTMLNotAllowed'), 'html', $html_type);
  342. }
  343. /**
  344. * Adds a datepicker element to the form
  345. * A rule is added to check if the date is a valid one
  346. * @param string $label The label for the form-element
  347. * @param string $name The element name
  348. * @deprecated
  349. */
  350. function add_datepicker($name, $label)
  351. {
  352. $this->addElement('datepicker', $name, $label, array('form_name' => $this->getAttribute('name')));
  353. $this->_elements[$this->_elementIndex[$name]]->setLocalOption('minYear', 1900); // TODO: Now - 9 years
  354. $this->addRule($name, get_lang('InvalidDate'), 'date');
  355. }
  356. /**
  357. * Adds a date picker date element to the form
  358. * A rule is added to check if the date is a valid one
  359. * @param string $label The label for the form-element
  360. * @param string $name The element name
  361. * @deprecated
  362. */
  363. public function add_datepickerdate($name, $label)
  364. {
  365. $this->addElement('datepickerdate', $name, $label, array('form_name' => $this->getAttribute('name')));
  366. $this->_elements[$this->_elementIndex[$name]]->setLocalOption('minYear', 1900); // TODO: Now - 9 years
  367. $this->addRule($name, get_lang('InvalidDate'), 'date');
  368. }
  369. /**
  370. * Adds a timewindow element to the form.
  371. * 2 datepicker elements are added and a rule to check if the first date is
  372. * before the second one.
  373. * @param string $label The label for the form-element
  374. * @param string $name The element name
  375. * @deprecated
  376. */
  377. public function add_timewindow($name_1, $name_2, $label_1, $label_2)
  378. {
  379. $this->add_datepicker($name_1, $label_1);
  380. $this->add_datepicker($name_2, $label_2);
  381. $this->addRule(array($name_1, $name_2), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
  382. }
  383. /**
  384. * Adds a button to the form to add resources.
  385. * @deprecated
  386. */
  387. function add_resource_button()
  388. {
  389. $group = array();
  390. $group[] = $this->createElement('static', 'add_resource_img', null, '<img src="' . api_get_path(WEB_IMG_PATH) . 'attachment.gif" alt="' . get_lang('Attachment') . '"/>');
  391. $group[] = $this->createElement('submit', 'add_resource', get_lang('Attachment'), 'class="link_alike"');
  392. $this->addGroup($group);
  393. }
  394. /**
  395. * Adds a progress bar to the form.
  396. *
  397. * Once the user submits the form, a progress bar (animated gif) is
  398. * displayed. The progress bar will disappear once the page has been
  399. * reloaded.
  400. *
  401. * @param int $delay (optional) The number of seconds between the moment the user
  402. * @param string $label (optional) Custom label to be shown
  403. * submits the form and the start of the progress bar.
  404. */
  405. public function add_progress_bar($delay = 2, $label = '')
  406. {
  407. if (empty($label)) {
  408. $label = get_lang('PleaseStandBy');
  409. }
  410. $this->with_progress_bar = true;
  411. $this->updateAttributes("onsubmit=\"javascript: myUpload.start('dynamic_div','" . api_get_path(WEB_IMG_PATH) . "progress_bar.gif','" . $label . "','" . $this->getAttribute('id') . "')\"");
  412. $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
  413. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  414. }
  415. /**
  416. * Uses new functions (php 5.2) for displaying real upload progress.
  417. * @param string $upload_id The value of the field UPLOAD_IDENTIFIER, the second parameter (XXX) of the $form->addElement('file', XXX) sentence
  418. * @param string $element_after The first element of the form (to place at first UPLOAD_IDENTIFIER)
  419. * @param int $delay (optional) The frequency of the xajax call
  420. * @param bool $wait_after_upload (optional)
  421. */
  422. public function add_real_progress_bar($upload_id, $element_after, $delay = 2, $wait_after_upload = false)
  423. {
  424. if (!function_exists('uploadprogress_get_info')) {
  425. $this->add_progress_bar($delay);
  426. return;
  427. }
  428. if (!class_exists('xajax')) {
  429. require_once api_get_path(LIBRARY_PATH) . 'xajax/xajax.inc.php';
  430. }
  431. $xajax_upload = new xajax(api_get_path(WEB_LIBRARY_PATH) . 'upload.xajax.php');
  432. $xajax_upload->registerFunction('updateProgress');
  433. // IMPORTANT : must be the first element of the form
  434. $el = $this->insertElementBefore(FormValidator::createElement('html', '<input type="hidden" name="UPLOAD_IDENTIFIER" value="' . $upload_id . '" />'), $element_after);
  435. $this->addElement('html', '<br />');
  436. // Add div-element where the progress bar is to be displayed
  437. $this->addElement('html', '
  438. <div id="dynamic_div_container" style="display:none">
  439. <div id="dynamic_div_label">' . get_lang('UploadFile') . '</div>
  440. <div id="dynamic_div_frame" style="width:214px; height:12px; border:1px solid grey; background-image:url(' . api_get_path(WEB_IMG_PATH) . 'real_upload_frame.gif);">
  441. <div id="dynamic_div_filled" style="width:0%;height:100%;background-image:url(' . api_get_path(WEB_IMG_PATH) . 'real_upload_step.gif);background-repeat:repeat-x;background-position:center;"></div>
  442. </div>
  443. </div>');
  444. if ($wait_after_upload) {
  445. $this->addElement('html', '
  446. <div id="dynamic_div_waiter_container" style="display:none">
  447. <div id="dynamic_div_waiter_label">
  448. ' . get_lang('SlideshowConversion') . '
  449. </div>
  450. <div id="dynamic_div_waiter_frame">
  451. <img src="' . api_get_path(WEB_IMG_PATH) . 'real_upload_frame.gif" />
  452. </div>
  453. </div>
  454. ');
  455. }
  456. // Get the xajax code
  457. $this->addElement('html', $xajax_upload->getJavascript(api_get_path(WEB_LIBRARY_PATH) . 'xajax'));
  458. // Get the upload code
  459. $this->addElement('html', '<script language="javascript" src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/upload.js" type="text/javascript"></script>');
  460. $this->addElement('html', '<script type="text/javascript">var myUpload = new upload(' . (abs(intval($delay)) * 1000) . ');</script>');
  461. if (!$wait_after_upload) {
  462. $wait_after_upload = 0;
  463. }
  464. // Add the upload event
  465. $this->updateAttributes("onsubmit=\"javascript: myUpload.startRealUpload('dynamic_div','" . $upload_id . "','" . $this->getAttribute('id') . "'," . $wait_after_upload . ")\"");
  466. }
  467. /**
  468. * This function has been created for avoiding changes directly within QuickForm class.
  469. * When we use it, the element is threated as 'required' to be dealt during validation.
  470. * @param array $element The array of elements
  471. * @param string $message The message displayed
  472. */
  473. function add_multiple_required_rule($elements, $message)
  474. {
  475. $this->_required[] = $elements[0];
  476. $this->addRule($elements, $message, 'multiple_required');
  477. }
  478. /**
  479. * Displays the form.
  480. * If an element in the form didn't validate, an error message is showed
  481. * asking the user to complete the form.
  482. */
  483. public function display()
  484. {
  485. echo $this->return_form();
  486. }
  487. /**
  488. * Returns the HTML code of the form.
  489. * If an element in the form didn't validate, an error message is showed
  490. * asking the user to complete the form.
  491. *
  492. * @return string $return_value HTML code of the form
  493. *
  494. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, august 2006
  495. */
  496. public function return_form()
  497. {
  498. $error = false;
  499. $addDateLibraries = false;
  500. $dateElementTypes = array('date_range_picker', 'date_time_picker', 'date_picker', 'datepicker', 'datetimepicker');
  501. /** @var HTML_QuickForm_element $element */
  502. foreach ($this->_elements as $element) {
  503. if (in_array($element->getType(), $dateElementTypes)) {
  504. $addDateLibraries = true;
  505. }
  506. if (!is_null(parent::getElementError($element->getName()))) {
  507. $error = true;
  508. break;
  509. }
  510. }
  511. $return_value = '';
  512. $js = null;
  513. if ($addDateLibraries) {
  514. $js .= '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/daterange/moment.min.js" type="text/javascript"></script>';
  515. $js .= '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/datetimepicker/jquery-ui-timepicker-addon.js" type="text/javascript"></script>';
  516. $js .= '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/datetimepicker/jquery-ui-timepicker-addon.css" rel="stylesheet" type="text/css" />';
  517. $js .= '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/daterange/daterangepicker.js" type="text/javascript"></script>';
  518. $js .= '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/daterange/daterangepicker-bs2.css" rel="stylesheet" type="text/css" />';
  519. $isoCode = api_get_language_isocode();
  520. if ($isoCode != 'en') {
  521. $js .= api_get_js('jquery-ui/jquery-ui-i18n.min.js');
  522. $js .= '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/datetimepicker/i18n/jquery-ui-timepicker-'.$isoCode.'.js" type="text/javascript"></script>';
  523. $js .= '<script>
  524. $(function(){
  525. moment.lang("'.$isoCode.'");
  526. $.datepicker.setDefaults($.datepicker.regional["'.$isoCode.'"]);
  527. });
  528. </script>';
  529. }
  530. }
  531. if ($error) {
  532. $return_value = Display::return_message(get_lang('FormHasErrorsPleaseComplete'), 'warning');
  533. }
  534. $return_value .= $js;
  535. $return_value .= parent::toHtml();
  536. // Add div-element which is to hold the progress bar
  537. if (isset($this->with_progress_bar) && $this->with_progress_bar) {
  538. $return_value .= '<div id="dynamic_div" style="display:block; margin-left:40%; margin-top:10px; height:50px;"></div>';
  539. }
  540. return $return_value;
  541. }
  542. }
  543. /**
  544. * Cleans HTML text
  545. * @param string $html HTML to clean
  546. * @param int $mode (optional)
  547. * @return string The cleaned HTML
  548. */
  549. function html_filter($html, $mode = NO_HTML)
  550. {
  551. require_once api_get_path(LIBRARY_PATH) . 'formvalidator/Rule/HTML.php';
  552. $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
  553. $cleaned_html = kses($html, $allowed_tags);
  554. return $cleaned_html;
  555. }
  556. function html_filter_teacher($html)
  557. {
  558. return html_filter($html, TEACHER_HTML);
  559. }
  560. function html_filter_student($html)
  561. {
  562. return html_filter($html, STUDENT_HTML);
  563. }
  564. function html_filter_teacher_fullpage($html)
  565. {
  566. return html_filter($html, TEACHER_HTML_FULLPAGE);
  567. }
  568. function html_filter_student_fullpage($html)
  569. {
  570. return html_filter($html, STUDENT_HTML_FULLPAGE);
  571. }
  572. /**
  573. * Cleans telephone text
  574. * @param string $telephone Telephone number to clean
  575. * @return string The cleaned telephone number
  576. */
  577. function telephone_filter($telephone)
  578. {
  579. $telephone= trim ($telephone,'(');
  580. $telephone= trim ($telephone,')');
  581. $telephone= ltrim ($telephone,'+');
  582. $telephone= ltrim ($telephone,'0');
  583. return $telephone;
  584. }