catform.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /**
  13. * Extends formvalidator with add&edit forms
  14. * @author Stijn Konings
  15. * @package chamilo.gradebook
  16. */
  17. class CatForm extends FormValidator {
  18. const TYPE_ADD = 1;
  19. const TYPE_EDIT = 2;
  20. const TYPE_MOVE = 3;
  21. const TYPE_SELECT_COURSE = 4;
  22. private $category_object;
  23. /**
  24. * Builds a form containing form items based on a given parameter
  25. * @param int form_type 1=add, 2=edit,3=move,4=browse
  26. * @param obj cat_obj the category object
  27. * @param string form name
  28. * @param method method
  29. */
  30. function CatForm($form_type, $category_object,$form_name,$method = 'post',$action=null) {
  31. parent :: __construct($form_name, $method, $action);
  32. $this->form_type = $form_type;
  33. if (isset ($category_object)) {
  34. $this->category_object = $category_object;
  35. }
  36. if ($this->form_type == self :: TYPE_EDIT) {
  37. $this->build_editing_form();
  38. } elseif ($this->form_type == self :: TYPE_ADD) {
  39. $this->build_add_form();
  40. } elseif ($this->form_type == self :: TYPE_MOVE) {
  41. $this->build_move_form();
  42. } elseif ($this->form_type == self :: TYPE_SELECT_COURSE) {
  43. $this->build_select_course_form();
  44. }
  45. $this->setDefaults();
  46. }
  47. /**
  48. * This function will build a move form that will allow the user to move a category to
  49. * a another
  50. */
  51. protected function build_move_form() {
  52. $renderer =& $this->defaultRenderer();
  53. $renderer->setElementTemplate('<span>{element}</span> ');
  54. $this->addElement('static',null,null,'"'.$this->category_object->get_name().'" ');
  55. $this->addElement('static',null,null,get_lang('MoveTo'). ' : ');
  56. $select = $this->addElement('select','move_cat',null,null);
  57. foreach ($this->category_object->get_target_categories() as $cat) {
  58. for ($i=0;$i<$cat[2];$i++) {
  59. $line .= '--';
  60. }
  61. if ($cat[0] != $this->category_object->get_parent_id()) {
  62. $select->addoption($line.' '.$cat[1],$cat[0]);
  63. } else {
  64. $select->addoption($line.' '.$cat[1],$cat[0],'disabled');
  65. }
  66. $line = '';
  67. }
  68. $this->addElement('submit', null, get_lang('Ok'));
  69. }
  70. /**
  71. * This function builds an 'add category form, if parent id is 0, it will only
  72. * show courses
  73. */
  74. protected function build_add_form() {
  75. //check if we are a root category
  76. //if so, you can only choose between courses
  77. if ($this->category_object->get_parent_id() == '0') {
  78. //$select = $this->addElement('select','select_course',array(get_lang('PickACourse'),'test'), null);
  79. $coursecat = Category :: get_not_created_course_categories(api_get_user_id());
  80. if (count($coursecat)==0) {
  81. //$select->addoption(get_lang('CourseIndependent'),'COURSEINDEPENDENT','disabled');
  82. } else {
  83. //$select->addoption(get_lang('CourseIndependent'),'COURSEINDEPENDENT');
  84. }
  85. //only return courses that are not yet created by the teacher
  86. if (!empty($coursecat)) {
  87. foreach($coursecat as $row) {
  88. //$select->addoption($row[1],$row[0]);
  89. }
  90. } else {
  91. //$select->addoption($row[1],$row[0]);
  92. }
  93. $this->setDefaults(array(
  94. 'select_course' => $this->category_object->get_course_code(),
  95. 'hid_user_id' => $this->category_object->get_user_id(),
  96. 'hid_parent_id' => $this->category_object->get_parent_id()
  97. ));
  98. } else {
  99. $this->setDefaults(array(
  100. 'hid_user_id' => $this->category_object->get_user_id(),
  101. 'hid_parent_id' => $this->category_object->get_parent_id()
  102. ));
  103. $this->addElement('hidden','course_code', $this->category_object->get_course_code());
  104. }
  105. $this->build_basic_form();
  106. }
  107. /**
  108. * Builds an form to edit a category
  109. */
  110. protected function build_editing_form() {
  111. $skills = $this->category_object->get_skills_for_select();
  112. $course_code = api_get_course_id();
  113. $session_id = api_get_session_id();
  114. //Freeze or not
  115. $test_cats = Category :: load(null, null, $course_code, null, null, $session_id, false); //already init
  116. $links = null;
  117. if (isset($test_cats[0])) {
  118. $links = $test_cats[0]->get_links();
  119. }
  120. $grade_model_id = $this->category_object->get_grade_model_id();
  121. if (empty($links)) {
  122. $grade_model_id = 0;
  123. }
  124. $category_name = $this->category_object->get_name();
  125. //The main course category:
  126. if (isset($this->category_object) && $this->category_object->get_parent_id() == 0) {
  127. if (empty($category_name)) {
  128. $category_name = $course_code;
  129. }
  130. }
  131. $this->setDefaults(
  132. array(
  133. 'name' => $category_name,
  134. 'description' => $this->category_object->get_description(),
  135. 'hid_user_id' => $this->category_object->get_user_id(),
  136. 'hid_parent_id' => $this->category_object->get_parent_id(),
  137. 'grade_model_id' => $grade_model_id,
  138. 'skills' => $skills,
  139. 'weight' => $this->category_object->get_weight(),
  140. 'visible' => $this->category_object->is_visible(),
  141. 'certif_min_score' => $this->category_object->get_certificate_min_score(),
  142. )
  143. );
  144. $this->addElement('hidden','hid_id', $this->category_object->get_id());
  145. $this->addElement('hidden','course_code', $this->category_object->get_course_code());
  146. $this->build_basic_form();
  147. }
  148. private function build_basic_form() {
  149. $this->addElement('hidden', 'zero', 0);
  150. $this->add_textfield('name', get_lang('CategoryName'), true, array('class'=>'span3','maxlength'=>'50'));
  151. $this->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  152. if (isset($this->category_object) && $this->category_object->get_parent_id() == 0) {
  153. //we can't change the root category
  154. $this->freeze('name');
  155. }
  156. $global_weight = api_get_setting('gradebook_default_weight');
  157. if (isset($global_weight)) {
  158. $value = $global_weight;
  159. } else {
  160. $value = 100;
  161. }
  162. $this->add_textfield('weight', array(get_lang('TotalWeight'), get_lang('TotalSumOfWeights')), true, array('value'=>$value, 'class'=>'span1','maxlength'=>'5'));
  163. $this->addRule('weight',get_lang('ThisFieldIsRequired'),'required');
  164. if (api_is_platform_admin() || api_is_drh()) {
  165. if (api_get_setting('allow_skills_tool') == 'true') {
  166. //the magic should be here
  167. $skills = $this->category_object->get_skills();
  168. $this->addElement('select', 'skills', array(get_lang('Skills'), get_lang('SkillsAchievedWhenAchievingThisGradebook')), null, array('id'=>'skills', 'multiple'=>'multiple'));
  169. $content = '';
  170. if (!empty($skills)) {
  171. foreach($skills as $skill) {
  172. $content .= Display::tag('li', $skill['name'].'<a id="deleteskill_'.$skill['id'].'" class="closebutton" href="#"></a>', array('id'=>'skill_'.$skill['id'], 'class'=>'bit-box'));
  173. }
  174. }
  175. $this->addElement('label', null, Display::tag('ul', $content, array('class'=>'holder holder_simple')));
  176. }
  177. }
  178. if (isset($this->category_object) && $this->category_object->get_parent_id() == 0) {
  179. $this->add_textfield('certif_min_score', get_lang('CertificateMinScore'),false,array('class'=>'span1','maxlength'=>'5'));
  180. $this->addRule('certif_min_score', get_lang('ThisFieldIsRequired'), 'required');
  181. $this->addRule('certif_min_score',get_lang('OnlyNumbers'),'numeric');
  182. $this->addRule(array('certif_min_score', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  183. } else {
  184. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  185. }
  186. $this->addElement('hidden','hid_user_id');
  187. $this->addElement('hidden','hid_parent_id');
  188. $this->addElement('textarea', 'description', get_lang('Description'), array('class'=>'span3','cols' => '34'));
  189. if (isset($this->category_object) && $this->category_object->get_parent_id() == 0 && (api_is_platform_admin() || api_get_setting('teachers_can_change_grade_model_settings') == 'true')) {
  190. //Getting grade models
  191. $obj = new GradeModel();
  192. $obj->fill_grade_model_select_in_form($this, 'grade_model_id', $this->category_object->get_grade_model_id());
  193. /*
  194. $grade_models = $obj->get_all();
  195. $options = array(-1 => get_lang('None'));
  196. foreach ($grade_models as $item) {
  197. $options[$item['id']] = $item['name'];
  198. }
  199. $this->addElement('select', 'grade_model_id', array(get_lang('GradeModel'), get_lang('OnlyActiveWhenThereAreAnyComponents')), $options);
  200. *
  201. */
  202. //Freeze or not
  203. $course_code = api_get_course_id();
  204. $session_id = api_get_session_id();
  205. $test_cats = Category :: load(null, null, $course_code, null, null, $session_id, false); //already init
  206. $links = null;
  207. if (!empty($test_cats[0])) {
  208. $links = $test_cats[0]->get_links();
  209. }
  210. if (count($test_cats) > 1 || !empty($links)) {
  211. if (api_get_setting('gradebook_enable_grade_model') == 'true') {
  212. $this->freeze('grade_model_id');
  213. }
  214. }
  215. }
  216. if ($this->form_type == self :: TYPE_ADD) {
  217. $this->addElement('style_submit_button', null, get_lang('AddCategory'), 'class="save"');
  218. } else {
  219. $this->addElement('hidden','editcat', intval($_GET['editcat']));
  220. $this->addElement('style_submit_button', null, get_lang('EditCategory'), 'class="save"');
  221. }
  222. //if (!empty($grading_contents)) {
  223. $this->addRule('weight', get_lang('OnlyNumbers'), 'numeric');
  224. //$this->addRule('weight',get_lang('NoDecimals'),'nopunctuation');
  225. $this->addRule(array ('weight', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  226. //}
  227. $setting = api_get_setting('tool_visible_by_default_at_creation');
  228. $visibility_default = 1;
  229. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  230. $visibility_default = 0;
  231. }
  232. $this->setDefaults(array('visible' => $visibility_default));
  233. }
  234. /**
  235. * This function builds an 'select course' form in the add category process,
  236. * if parent id is 0, it will only show courses
  237. */
  238. protected function build_select_course_form() {
  239. $select = $this->addElement('select','select_course',array(get_lang('PickACourse'),'test'), null);
  240. $coursecat = Category :: get_all_courses(api_get_user_id());
  241. //only return courses that are not yet created by the teacher
  242. foreach($coursecat as $row) {
  243. $select->addoption($row[1],$row[0]);
  244. }
  245. $this->setDefaults(array(
  246. 'hid_user_id' => $this->category_object->get_user_id(),
  247. 'hid_parent_id' => $this->category_object->get_parent_id()
  248. ));
  249. $this->addElement('hidden','hid_user_id');
  250. $this->addElement('hidden','hid_parent_id');
  251. $this->addElement('submit', null, get_lang('Ok'));
  252. }
  253. function display() {
  254. parent :: display();
  255. }
  256. function setDefaults($defaults = array(), $filter = null) {
  257. parent :: setDefaults($defaults, $filter);
  258. }
  259. }