linkaddeditform.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Form used to add or edit links
  5. * @author Stijn Konings
  6. * @author Bert Steppé
  7. * @package chamilo.gradebook
  8. */
  9. class LinkAddEditForm extends FormValidator
  10. {
  11. const TYPE_ADD = 1;
  12. const TYPE_EDIT = 2;
  13. /**
  14. * Constructor
  15. * To add link, define category_object and link_type
  16. * To edit link, define link_object
  17. */
  18. public function __construct(
  19. $form_type,
  20. $category_object,
  21. $link_type,
  22. $link_object,
  23. $form_name,
  24. $action = null
  25. ) {
  26. parent::__construct($form_name, 'post', $action);
  27. // set or create link object
  28. if (isset($link_object)) {
  29. $link = $link_object;
  30. } elseif (isset($link_type) && isset($category_object)) {
  31. $link = LinkFactory:: create($link_type);
  32. $link->set_course_code(api_get_course_id());
  33. $link->set_category_id($category_object[0]->get_id());
  34. } else {
  35. die('LinkAddEditForm error: define link_type/category_object or link_object');
  36. }
  37. $defaults = array();
  38. if (!empty($_GET['editlink'])) {
  39. $this->addElement('header', '', get_lang('EditLink'));
  40. }
  41. // ELEMENT: name
  42. if ($form_type == self::TYPE_ADD || $link->is_allowed_to_change_name()) {
  43. if ($link->needs_name_and_description()) {
  44. $this->addText('name', get_lang('Name'), true, array('size'=>'40', 'maxlength'=>'40'));
  45. } else {
  46. $select = $this->addElement('select', 'select_link', get_lang('ChooseItem'));
  47. foreach ($link->get_all_links() as $newlink) {
  48. $select->addoption($newlink[1], $newlink[0]);
  49. }
  50. }
  51. } else {
  52. $this->addElement(
  53. 'label',
  54. get_lang('Name'),
  55. '<span class="freeze">'.$link->get_name().' ['.$link->get_type_name().']</span>'
  56. );
  57. $this->addElement(
  58. 'hidden',
  59. 'name_link',
  60. $link->get_name(),
  61. array('id' => 'name_link')
  62. );
  63. }
  64. if (count($category_object) == 1) {
  65. $this->addElement('hidden', 'select_gradebook', $category_object[0]->get_id());
  66. } else {
  67. $select_gradebook = $this->addElement(
  68. 'select',
  69. 'select_gradebook',
  70. get_lang('SelectGradebook'),
  71. array(),
  72. array('id' => 'hide_category_id')
  73. );
  74. $this->addRule('select_gradebook', get_lang('ThisFieldIsRequired'), 'nonzero');
  75. $default_weight = 0;
  76. if (!empty($category_object)) {
  77. foreach ($category_object as $my_cat) {
  78. if ($my_cat->get_course_code() == api_get_course_id()) {
  79. $grade_model_id = $my_cat->get_grade_model_id();
  80. if (empty($grade_model_id)) {
  81. if ($my_cat->get_parent_id() == 0) {
  82. $default_weight = $my_cat->get_weight();
  83. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  84. } else {
  85. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  86. }
  87. } else {
  88. $select_gradebook->addoption(get_lang('Select'), 0);
  89. }
  90. if ($link->get_category_id() == $my_cat->get_id()) {
  91. $default_weight = $my_cat->get_weight();
  92. }
  93. }
  94. }
  95. }
  96. }
  97. $this->addFloat(
  98. 'weight_mask',
  99. array(
  100. get_lang('Weight'),
  101. null,
  102. ' [0 .. <span id="max_weight">'.$category_object[0]->get_weight(
  103. ).'</span>] ',
  104. ),
  105. true,
  106. array(
  107. 'size' => '4',
  108. 'maxlength' => '5'
  109. )
  110. );
  111. $this->addElement('hidden', 'weight');
  112. if ($form_type == self::TYPE_EDIT) {
  113. $parent_cat = Category::load($link->get_category_id());
  114. if ($parent_cat[0]->get_parent_id() == 0) {
  115. $values['weight'] = $link->get_weight();
  116. } else {
  117. $cat = Category::load($parent_cat[0]->get_parent_id());
  118. //$global_weight = $cat[0]->get_weight();
  119. //$values['weight'] = $link->get_weight()/$parent_cat[0]->get_weight()*$global_weight;
  120. //var_dump($global_weight, $link->get_weight(), $parent_cat[0]->get_weight());
  121. //$weight = $parent_cat[0]->get_weight()* $link->get_weight() / $global_weight;
  122. //$values['weight'] = $weight;
  123. $values['weight'] = $link->get_weight();
  124. }
  125. $defaults['weight_mask'] = $values['weight'];
  126. $defaults['select_gradebook'] = $link->get_category_id();
  127. }
  128. // ELEMENT: max
  129. if ($link->needs_max()) {
  130. if ($form_type == self::TYPE_EDIT && $link->has_results()) {
  131. $this->addText(
  132. 'max',
  133. get_lang('QualificationNumeric'),
  134. false,
  135. array(
  136. 'size' => '4',
  137. 'maxlength' => '5',
  138. 'disabled' => 'disabled',
  139. )
  140. );
  141. } else {
  142. $this->addText('max', get_lang('QualificationNumeric'), true, array('size' => '4', 'maxlength' => '5'));
  143. $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
  144. $this->addRule(
  145. 'max',
  146. get_lang('NegativeValue'),
  147. 'compare',
  148. '>=',
  149. 'server',
  150. false,
  151. false,
  152. 0
  153. );
  154. }
  155. if ($form_type == self::TYPE_EDIT) {
  156. $defaults['max'] = $link->get_max();
  157. }
  158. }
  159. // ELEMENT: description
  160. if ($link->needs_name_and_description()) {
  161. $this->addElement(
  162. 'textarea',
  163. 'description',
  164. get_lang('Description'),
  165. array('rows' => '3', 'cols' => '34')
  166. );
  167. if ($form_type == self::TYPE_EDIT) {
  168. $defaults['description'] = $link->get_description();
  169. }
  170. }
  171. // ELEMENT: visible
  172. $visible = ($form_type == self::TYPE_EDIT && $link->is_visible()) ? '1' : '0';
  173. $this->addElement('checkbox', 'visible', null, get_lang('Visible'), $visible);
  174. if ($form_type == self::TYPE_EDIT) {
  175. $defaults['visible'] = $link->is_visible();
  176. }
  177. // ELEMENT: add results
  178. if ($form_type == self::TYPE_ADD && $link->needs_results()) {
  179. $this->addElement('checkbox', 'addresult', get_lang('AddResult'));
  180. }
  181. // submit button
  182. if ($form_type == self::TYPE_ADD) {
  183. $this->addButtonCreate(get_lang('CreateLink'));
  184. } else {
  185. $this->addButtonUpdate(get_lang('LinkMod'));
  186. }
  187. if ($form_type == self::TYPE_ADD) {
  188. $setting = api_get_setting('tool_visible_by_default_at_creation');
  189. $visibility_default = 1;
  190. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  191. $visibility_default = 0;
  192. }
  193. $defaults['visible'] = $visibility_default;
  194. }
  195. // set default values
  196. $this->setDefaults($defaults);
  197. }
  198. }