linkaddeditform.class.php 7.1 KB

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