linkaddeditform.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 LinkAddEditForm(
  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. $this->addElement('hidden', 'zero', 0);
  39. if (!empty($_GET['editlink'])) {
  40. $this->addElement('header', '', get_lang('EditLink'));
  41. }
  42. // ELEMENT: name
  43. if ($form_type == self :: TYPE_ADD || $link->is_allowed_to_change_name()) {
  44. if ($link->needs_name_and_description()) {
  45. $this->addText('name', get_lang('Name'), true, array('size'=>'40', 'maxlength'=>'40'));
  46. } else {
  47. $select = $this->addElement('select', 'select_link', get_lang('ChooseItem'));
  48. foreach ($link->get_all_links() as $newlink) {
  49. $select->addoption($newlink[1],$newlink[0]);
  50. }
  51. }
  52. } else {
  53. $this->addElement('label',get_lang('Name'), '<span class="freeze">'.$link->get_name().' ['.$link->get_type_name().']</span>');
  54. $this->addElement('hidden','name_link',$link->get_name(),array('id'=>'name_link'));
  55. }
  56. if (count($category_object) == 1) {
  57. $this->addElement('hidden', 'select_gradebook', $category_object[0]->get_id());
  58. } else {
  59. $select_gradebook = $this->addElement(
  60. 'select',
  61. 'select_gradebook',
  62. get_lang('SelectGradebook'),
  63. array(),
  64. array('id' => 'hide_category_id')
  65. );
  66. $this->addRule('select_gradebook', get_lang('ThisFieldIsRequired'), 'nonzero');
  67. $default_weight = 0;
  68. if (!empty($category_object)) {
  69. foreach ($category_object as $my_cat) {
  70. if ($my_cat->get_course_code() == api_get_course_id()) {
  71. $grade_model_id = $my_cat->get_grade_model_id();
  72. if (empty($grade_model_id)) {
  73. if ($my_cat->get_parent_id() == 0 ) {
  74. $default_weight = $my_cat->get_weight();
  75. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  76. } else {
  77. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  78. }
  79. } else {
  80. $select_gradebook->addoption(get_lang('Select'), 0);
  81. }
  82. if ($link->get_category_id() == $my_cat->get_id()) {
  83. $default_weight = $my_cat->get_weight();
  84. }
  85. }
  86. }
  87. }
  88. }
  89. $this->addText(
  90. 'weight_mask',
  91. array(get_lang('Weight'), null, ' [0 .. <span id="max_weight">'.$category_object[0]->get_weight().'</span>] '),
  92. true,
  93. array(
  94. 'size' => '4',
  95. 'maxlength' => '5',
  96. 'class' => 'span1'
  97. )
  98. );
  99. $this->addElement('hidden', 'weight');
  100. /*
  101. // ELEMENT: weight
  102. $this->addText('weight', array(get_lang('Weight'), null, '/ <span id="max_weight">'.$default_weight.'</span>'), true, array (
  103. 'size' => '4',
  104. 'maxlength' => '5',
  105. 'class' => 'span1'
  106. ));*/
  107. $this->addRule('weight_mask',get_lang('OnlyNumbers'),'numeric');
  108. $this->addRule(array ('weight_mask', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  109. if ($form_type == self :: TYPE_EDIT) {
  110. $parent_cat = Category :: load($link->get_category_id());
  111. if ($parent_cat[0]->get_parent_id() == 0) {
  112. $values['weight'] = $link->get_weight();
  113. } else {
  114. $cat = Category :: load($parent_cat[0]->get_parent_id());
  115. //$global_weight = $cat[0]->get_weight();
  116. //$values['weight'] = $link->get_weight()/$parent_cat[0]->get_weight()*$global_weight;
  117. //var_dump($global_weight, $link->get_weight(), $parent_cat[0]->get_weight());
  118. //$weight = $parent_cat[0]->get_weight()* $link->get_weight() / $global_weight;
  119. //$values['weight'] = $weight;
  120. $values['weight'] = $link->get_weight() ;
  121. }
  122. $defaults['weight_mask'] = $values['weight'] ;
  123. $defaults['select_gradebook'] = $link->get_category_id();
  124. }
  125. // ELEMENT: max
  126. if ($link->needs_max()) {
  127. if ($form_type == self :: TYPE_EDIT && $link->has_results()) {
  128. $this->addText('max', get_lang('QualificationNumeric'), false, array ('size' => '4','maxlength' => '5', 'disabled' => 'disabled'));
  129. } else {
  130. $this->addText('max', get_lang('QualificationNumeric'), true, array ('size' => '4','maxlength' => '5'));
  131. $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
  132. $this->addRule(array ('max', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  133. }
  134. if ($form_type == self :: TYPE_EDIT) {
  135. $defaults['max'] = $link->get_max();
  136. }
  137. }
  138. // ELEMENT: description
  139. if ($link->needs_name_and_description()) {
  140. $this->addElement('textarea', 'description', get_lang('Description'), array ('rows' => '3','cols' => '34'));
  141. if ($form_type == self :: TYPE_EDIT) {
  142. $defaults['description'] = $link->get_description();
  143. }
  144. }
  145. // ELEMENT: visible
  146. $visible = ($form_type == self :: TYPE_EDIT && $link->is_visible()) ? '1' : '0';
  147. $this->addElement('checkbox', 'visible', null, get_lang('Visible'), $visible);
  148. if ($form_type == self :: TYPE_EDIT) {
  149. $defaults['visible'] = $link->is_visible();
  150. }
  151. // ELEMENT: add results
  152. if ($form_type == self :: TYPE_ADD && $link->needs_results()) {
  153. $this->addElement('checkbox', 'addresult', get_lang('AddResult'));
  154. }
  155. // submit button
  156. if ($form_type == self :: TYPE_ADD) {
  157. $this->addButtonCreate(get_lang('CreateLink'));
  158. } else {
  159. $this->addButtonUpdate(get_lang('LinkMod'));
  160. }
  161. if ($form_type == self :: TYPE_ADD) {
  162. $setting = api_get_setting('tool_visible_by_default_at_creation');
  163. $visibility_default = 1;
  164. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  165. $visibility_default = 0;
  166. }
  167. $defaults['visible'] = $visibility_default;
  168. }
  169. // set default values
  170. $this->setDefaults($defaults);
  171. }
  172. }