FormValidator.class.php 29 KB

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