OutcomeForm.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. class OutcomeForm extends EvalForm
  4. {
  5. /**
  6. * Builds a form containing form items based on a given parameter.
  7. *
  8. * @param int $form_type 1=add, 2=edit,3=move,4=result_add
  9. * @param Evaluation $evaluation_object the category object
  10. * @param obj $result_object the result object
  11. * @param string $form_name
  12. * @param string $method
  13. * @param string $action
  14. */
  15. public function __construct(
  16. $evaluation_object,
  17. $result_object,
  18. $form_name,
  19. $method = 'post',
  20. $action = null,
  21. $extra1 = null,
  22. $extra2 = null
  23. ) {
  24. parent::__construct(
  25. -1,
  26. $evaluation_object,
  27. $result_object,
  28. $form_name,
  29. $method,
  30. $action,
  31. $extra1,
  32. $extra2
  33. );
  34. $this->build_add_form();
  35. $this->setDefaults();
  36. }
  37. /**
  38. * Builds a basic form that is used in add and edit.
  39. *
  40. * @param int $edit
  41. *
  42. * @throws Exception
  43. */
  44. private function build_basic_form($edit = 0)
  45. {
  46. $this->addElement('header', get_plugin_lang('NewOutcomeFormTitle'));
  47. $this->addElement('hidden', 'hid_user_id');
  48. $this->addElement('hidden', 'hid_course_code');
  49. $this->addText(
  50. 'name',
  51. get_lang('EvaluationName'),
  52. true,
  53. [
  54. 'maxlength' => '50',
  55. 'id' => 'evaluation_title',
  56. ]
  57. );
  58. $cat_id = $this->evaluation_object->get_category_id();
  59. $session_id = api_get_session_id();
  60. $course_code = api_get_course_id();
  61. $all_categories = Category:: load(null, null, $course_code, null, null, $session_id, false);
  62. if (count($all_categories) == 1) {
  63. $this->addElement('hidden', 'hid_category_id', $cat_id);
  64. } else {
  65. $select_gradebook = $this->addElement(
  66. 'select',
  67. 'hid_category_id',
  68. get_lang('SelectGradebook'),
  69. [],
  70. ['id' => 'hid_category_id']
  71. );
  72. $this->addRule('hid_category_id', get_lang('ThisFieldIsRequired'), 'nonzero');
  73. $default_weight = 0;
  74. if (!empty($all_categories)) {
  75. foreach ($all_categories as $my_cat) {
  76. if ($my_cat->get_course_code() == api_get_course_id()) {
  77. $grade_model_id = $my_cat->get_grade_model_id();
  78. if (empty($grade_model_id)) {
  79. if ($my_cat->get_parent_id() == 0) {
  80. $default_weight = $my_cat->get_weight();
  81. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  82. $cats_added[] = $my_cat->get_id();
  83. } else {
  84. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  85. $cats_added[] = $my_cat->get_id();
  86. }
  87. } else {
  88. $select_gradebook->addoption(get_lang('Select'), 0);
  89. }
  90. if ($this->evaluation_object->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. [
  100. get_lang('Weight'),
  101. null,
  102. ' [0 .. <span id="max_weight">'.$all_categories[0]->get_weight().'</span>] ',
  103. ],
  104. true,
  105. [
  106. 'size' => '4',
  107. 'maxlength' => '5',
  108. ]
  109. );
  110. if ($edit) {
  111. if (!$this->evaluation_object->has_results()) {
  112. $this->addText(
  113. 'max',
  114. get_lang('QualificationNumeric'),
  115. true,
  116. [
  117. 'maxlength' => '5',
  118. ]
  119. );
  120. } else {
  121. $this->addText(
  122. 'max',
  123. [get_lang('QualificationNumeric'), get_lang('CannotChangeTheMaxNote')],
  124. false,
  125. [
  126. 'maxlength' => '5',
  127. 'disabled' => 'disabled',
  128. ]
  129. );
  130. }
  131. } else {
  132. $this->addText(
  133. 'max',
  134. get_lang('QualificationNumeric'),
  135. true,
  136. [
  137. 'maxlength' => '5',
  138. ]
  139. );
  140. $default_max = api_get_setting('gradebook_default_weight');
  141. $defaults['max'] = isset($default_max) ? $default_max : 100;
  142. $this->setDefaults($defaults);
  143. }
  144. $this->addElement('textarea', 'description', get_lang('Description'));
  145. $this->addRule('hid_category_id', get_lang('ThisFieldIsRequired'), 'required');
  146. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  147. $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
  148. $this->addRule(
  149. 'max',
  150. get_lang('NegativeValue'),
  151. 'compare',
  152. '>=',
  153. 'server',
  154. false,
  155. false,
  156. 0
  157. );
  158. $setting = api_get_setting('tool_visible_by_default_at_creation');
  159. $visibility_default = 1;
  160. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  161. $visibility_default = 0;
  162. }
  163. $this->setDefaults(['visible' => $visibility_default]);
  164. }
  165. /**
  166. *
  167. */
  168. protected function build_add_form()
  169. {
  170. $this->setDefaults(
  171. [
  172. 'hid_user_id' => $this->evaluation_object->get_user_id(),
  173. 'hid_category_id' => $this->evaluation_object->get_category_id(),
  174. 'hid_course_code' => $this->evaluation_object->get_course_code(),
  175. 'created_at' => api_get_utc_datetime(),
  176. ]
  177. );
  178. $this->build_basic_form();
  179. $this->addButtonCreate(get_lang('AddAssessment'), 'submit');
  180. }
  181. }