linkaddeditform.class.php 7.2 KB

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