linkaddeditform.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos Latinoamerica SAC
  6. Copyright (c) 2006 Dokeos SPRL
  7. Copyright (c) 2006 Ghent University (UGent)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. require_once (dirname(__FILE__).'/../../../inc/global.inc.php');
  21. require_once (dirname(__FILE__).'/../be.inc.php');
  22. require_once (dirname(__FILE__).'/../gradebook_functions.inc.php');
  23. require_once (api_get_path(LIBRARY_PATH) . 'groupmanager.lib.php');
  24. require_once (api_get_path(LIBRARY_PATH) . 'formvalidator/FormValidator.class.php');
  25. /**
  26. * Form used to add or edit links
  27. * @author Stijn Konings
  28. * @author Bert Stepp�
  29. */
  30. class LinkAddEditForm extends FormValidator
  31. {
  32. const TYPE_ADD = 1;
  33. const TYPE_EDIT = 2;
  34. /**
  35. * Constructor
  36. * To add link, define category_object and link_type
  37. * To edit link, define link_object
  38. */
  39. function LinkAddEditForm($form_type, $category_object, $link_type, $link_object, $form_name, $action = null) {
  40. parent :: __construct($form_name, 'post', $action);
  41. // set or create link object
  42. if (isset ($link_object)) {
  43. $link = $link_object;
  44. } elseif (isset ($link_type) && isset ($category_object)) {
  45. $link = LinkFactory :: create ($link_type);
  46. $cc = $category_object->get_course_code();
  47. if (empty($cc) && !empty($_GET['course_code'])) {
  48. $link->set_course_code(Database::escape_string($_GET['course_code']));
  49. } else {
  50. $link->set_course_code($category_object->get_course_code());
  51. }
  52. } else {
  53. die ('LinkAddEditForm error: define link_type/category_object or link_object');
  54. }
  55. $defaults = array();
  56. $this->addElement('hidden', 'zero', 0);
  57. // ELEMENT: name
  58. if ($form_type == self :: TYPE_ADD || $link->is_allowed_to_change_name()) {
  59. if ($link->needs_name_and_description()) {
  60. $this->add_textfield('name',
  61. get_lang('Name'),
  62. true,
  63. array('size'=>'40',
  64. 'maxlength'=>'40'));
  65. } else {
  66. $select = $this->addElement('select',
  67. 'select_link',
  68. get_lang('ChooseExercise'));
  69. foreach ($link->get_all_links() as $newlink)
  70. $select->addoption($newlink[1],$newlink[0]);
  71. }
  72. } else {
  73. $this->addElement('static',
  74. 'label',
  75. get_lang('Name'),
  76. $link->get_name().' ['.$link->get_type_name().']');
  77. }
  78. // ELEMENT: weight
  79. $this->add_textfield('weight', get_lang('Weight'),true,array('size'=>'4','maxlength'=>'4'));
  80. $this->addRule('weight',get_lang('OnlyNumbers'),'numeric');
  81. $this->addRule(array ('weight', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  82. if ($form_type == self :: TYPE_EDIT) {
  83. $defaults['weight'] = $link->get_weight();
  84. }
  85. // ELEMENT: max
  86. if ($link->needs_max()) {
  87. if ($form_type == self :: TYPE_EDIT && $link->has_results()) {
  88. $this->add_textfield('max', get_lang('Max'), false, array ('size' => '4','maxlength' => '4', 'disabled' => 'disabled'));
  89. } else {
  90. $this->add_textfield('max', get_lang('Max'), true, array ('size' => '4','maxlength' => '4'));
  91. $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
  92. $this->addRule(array ('max', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  93. }
  94. if ($form_type == self :: TYPE_EDIT) {
  95. $defaults['max'] = $link->get_max();
  96. }
  97. }
  98. // ELEMENT: date
  99. $this->add_datepicker('date',get_lang('Date'));
  100. $defaults['date'] = ($form_type == self :: TYPE_EDIT ? $link->get_date() : time());
  101. // ELEMENT: description
  102. if ($link->needs_name_and_description()) {
  103. $this->addElement('textarea', 'description', get_lang('Description'), array ('rows' => '3','cols' => '34'));
  104. if ($form_type == self :: TYPE_EDIT) {
  105. $defaults['description'] = $link->get_description();
  106. }
  107. }
  108. // ELEMENT: visible
  109. $visible = ($form_type == self :: TYPE_EDIT && $link->is_visible()) ? '1' : '0';
  110. $this->addElement('checkbox', 'visible',get_lang('Visible'),null,$visible);
  111. if ($form_type == self :: TYPE_EDIT) {
  112. $defaults['visible'] = $link->is_visible();
  113. }
  114. // ELEMENT: add results
  115. if ($form_type == self :: TYPE_ADD && $link->needs_results()) {
  116. $this->addElement('checkbox', 'addresult', get_lang('AddResult'));
  117. }
  118. // submit button
  119. if ($form_type == self :: TYPE_ADD) {
  120. $this->addElement('submit', null, get_lang('Add'));
  121. } else {
  122. $this->addElement('submit', null, get_lang('Edit'));
  123. }
  124. // set default values
  125. $this->setDefaults($defaults);
  126. }
  127. }