catform.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class CatForm
  5. *
  6. * @author Stijn Konings
  7. * @package chamilo.gradebook
  8. */
  9. class CatForm extends FormValidator
  10. {
  11. const TYPE_ADD = 1;
  12. const TYPE_EDIT = 2;
  13. const TYPE_MOVE = 3;
  14. const TYPE_SELECT_COURSE = 4;
  15. private $category_object;
  16. /**
  17. * CatForm constructor.
  18. * Builds a form containing form items based on a given parameter
  19. * @param string $form_type 1=add, 2=edit,3=move,4=browse
  20. * @param string $category_object
  21. * @param string $form_name
  22. * @param string $method
  23. * @param null $action
  24. */
  25. public function __construct(
  26. $form_type,
  27. $category_object,
  28. $form_name,
  29. $method = 'post',
  30. $action = null
  31. ) {
  32. parent::__construct($form_name, $method, $action);
  33. $this->form_type = $form_type;
  34. if (isset($category_object)) {
  35. $this->category_object = $category_object;
  36. }
  37. if ($this->form_type == self::TYPE_EDIT) {
  38. $this->build_editing_form();
  39. } elseif ($this->form_type == self::TYPE_ADD) {
  40. $this->build_add_form();
  41. } elseif ($this->form_type == self::TYPE_MOVE) {
  42. $this->build_move_form();
  43. } elseif ($this->form_type == self::TYPE_SELECT_COURSE) {
  44. $this->build_select_course_form();
  45. }
  46. $this->setDefaults();
  47. }
  48. /**
  49. * This function will build a move form that will allow the user to move a category to
  50. * a another
  51. */
  52. protected function build_move_form()
  53. {
  54. $renderer = & $this->defaultRenderer();
  55. $renderer->setCustomElementTemplate('<span>{element}</span> ');
  56. $this->addElement(
  57. 'static',
  58. null,
  59. null,
  60. '"'.$this->category_object->get_name().'" '
  61. );
  62. $this->addElement('static', null, null, get_lang('MoveTo').' : ');
  63. $select = $this->addElement('select', 'move_cat', null, null);
  64. $line = null;
  65. foreach ($this->category_object->get_target_categories() as $cat) {
  66. for ($i = 0; $i < $cat[2]; $i++) {
  67. $line .= '--';
  68. }
  69. if ($cat[0] != $this->category_object->get_parent_id()) {
  70. $select->addoption($line.' '.$cat[1], $cat[0]);
  71. } else {
  72. $select->addoption($line.' '.$cat[1], $cat[0], 'disabled');
  73. }
  74. $line = '';
  75. }
  76. $this->addElement('submit', null, get_lang('Ok'));
  77. }
  78. /**
  79. * This function builds an 'add category form, if parent id is 0, it will only
  80. * show courses
  81. */
  82. protected function build_add_form()
  83. {
  84. //check if we are a root category
  85. //if so, you can only choose between courses
  86. if ($this->category_object->get_parent_id() == '0') {
  87. $coursecat = Category::get_not_created_course_categories(
  88. api_get_user_id()
  89. );
  90. if (count($coursecat) == 0) {
  91. //$select->addoption(get_lang('CourseIndependent'),'COURSEINDEPENDENT','disabled');
  92. } else {
  93. //$select->addoption(get_lang('CourseIndependent'),'COURSEINDEPENDENT');
  94. }
  95. //only return courses that are not yet created by the teacher
  96. if (!empty($coursecat)) {
  97. foreach ($coursecat as $row) {
  98. //$select->addoption($row[1],$row[0]);
  99. }
  100. } else {
  101. //$select->addoption($row[1],$row[0]);
  102. }
  103. $this->setDefaults(
  104. array(
  105. 'select_course' => $this->category_object->get_course_code(
  106. ),
  107. 'hid_user_id' => $this->category_object->get_user_id(),
  108. 'hid_parent_id' => $this->category_object->get_parent_id()
  109. )
  110. );
  111. } else {
  112. $this->setDefaults(
  113. array(
  114. 'hid_user_id' => $this->category_object->get_user_id(),
  115. 'hid_parent_id' => $this->category_object->get_parent_id()
  116. )
  117. );
  118. $this->addElement(
  119. 'hidden',
  120. 'course_code',
  121. $this->category_object->get_course_code()
  122. );
  123. }
  124. $this->build_basic_form();
  125. }
  126. /**
  127. * Builds an form to edit a category
  128. */
  129. protected function build_editing_form()
  130. {
  131. $skills = $this->category_object->get_skills_for_select();
  132. $course_code = api_get_course_id();
  133. $session_id = api_get_session_id();
  134. //Freeze or not
  135. $test_cats = Category::load(
  136. null,
  137. null,
  138. $course_code,
  139. null,
  140. null,
  141. $session_id,
  142. false
  143. ); //already init
  144. $links = null;
  145. if (isset($test_cats[0])) {
  146. $links = $test_cats[0]->get_links();
  147. }
  148. $grade_model_id = $this->category_object->get_grade_model_id();
  149. if (empty($links)) {
  150. $grade_model_id = 0;
  151. }
  152. $category_name = $this->category_object->get_name();
  153. // The main course category:
  154. if (isset($this->category_object) && $this->category_object->get_parent_id() == 0) {
  155. if (empty($category_name)) {
  156. $category_name = $course_code;
  157. }
  158. }
  159. $this->setDefaults(
  160. array(
  161. 'name' => $category_name,
  162. 'description' => $this->category_object->get_description(),
  163. 'hid_user_id' => $this->category_object->get_user_id(),
  164. 'hid_parent_id' => $this->category_object->get_parent_id(),
  165. 'grade_model_id' => $grade_model_id,
  166. 'skills' => $skills,
  167. 'weight' => $this->category_object->get_weight(),
  168. 'visible' => $this->category_object->is_visible(),
  169. 'certif_min_score' => $this->category_object->get_certificate_min_score(),
  170. 'generate_certificates' => $this->category_object->getGenerateCertificates(),
  171. 'is_requirement' => $this->category_object->getIsRequirement(),
  172. )
  173. );
  174. $this->addElement('hidden', 'hid_id', $this->category_object->get_id());
  175. $this->addElement(
  176. 'hidden',
  177. 'course_code',
  178. $this->category_object->get_course_code()
  179. );
  180. $this->build_basic_form();
  181. }
  182. /**
  183. *
  184. */
  185. private function build_basic_form()
  186. {
  187. $this->addText(
  188. 'name',
  189. get_lang('CategoryName'),
  190. true,
  191. array('maxlength' => '50')
  192. );
  193. $this->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  194. if (isset($this->category_object) &&
  195. $this->category_object->get_parent_id() == 0
  196. ) {
  197. //we can't change the root category
  198. $this->freeze('name');
  199. }
  200. $global_weight = api_get_setting('gradebook_default_weight');
  201. if (isset($global_weight)) {
  202. $value = $global_weight;
  203. } else {
  204. $value = 100;
  205. }
  206. $this->addFloat(
  207. 'weight',
  208. array(
  209. get_lang('TotalWeight'),
  210. get_lang('TotalSumOfWeights'),
  211. ),
  212. true,
  213. array('value' => $value, 'maxlength' => '5')
  214. );
  215. $skillsDefaults = [];
  216. if (api_is_platform_admin() || api_is_drh()) {
  217. if (api_get_setting('allow_skills_tool') == 'true') {
  218. $skillSelect = $this->addElement(
  219. 'select_ajax',
  220. 'skills',
  221. array(
  222. get_lang('Skills'),
  223. get_lang('SkillsAchievedWhenAchievingThisGradebook')
  224. ),
  225. null,
  226. [
  227. 'id' => 'skills',
  228. 'multiple' => 'multiple',
  229. 'url' => api_get_path(WEB_AJAX_PATH).'skill.ajax.php?a=search_skills'
  230. ]
  231. );
  232. // The magic should be here
  233. $skills = $this->category_object->get_skills();
  234. foreach ($skills as $skill) {
  235. $skillsDefaults[] = $skill['id'];
  236. $skillSelect->addOption($skill['name'], $skill['id']);
  237. }
  238. }
  239. }
  240. if (isset($this->category_object) &&
  241. $this->category_object->get_parent_id() == 0
  242. ) {
  243. $this->addText(
  244. 'certif_min_score',
  245. get_lang('CertificateMinScore'),
  246. true,
  247. array('maxlength' => '5')
  248. );
  249. $this->addRule(
  250. 'certif_min_score',
  251. get_lang('OnlyNumbers'),
  252. 'numeric'
  253. );
  254. $this->addRule(
  255. 'certif_min_score',
  256. get_lang('NegativeValue'),
  257. 'compare',
  258. '>=',
  259. 'server',
  260. false,
  261. false,
  262. 0
  263. );
  264. } else {
  265. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  266. }
  267. $this->addElement('hidden', 'hid_user_id');
  268. $this->addElement('hidden', 'hid_parent_id');
  269. $this->addElement(
  270. 'textarea',
  271. 'description',
  272. get_lang('Description')
  273. );
  274. if (isset($this->category_object) &&
  275. $this->category_object->get_parent_id() == 0 &&
  276. (api_is_platform_admin() || api_get_setting('teachers_can_change_grade_model_settings') == 'true')
  277. ) {
  278. // Getting grade models
  279. $obj = new GradeModel();
  280. $obj->fill_grade_model_select_in_form(
  281. $this,
  282. 'grade_model_id',
  283. $this->category_object->get_grade_model_id()
  284. );
  285. // Freeze or not
  286. $course_code = api_get_course_id();
  287. $session_id = api_get_session_id();
  288. $test_cats = Category::load(
  289. null,
  290. null,
  291. $course_code,
  292. null,
  293. null,
  294. $session_id,
  295. false
  296. ); //already init
  297. $links = null;
  298. if (!empty($test_cats[0])) {
  299. $links = $test_cats[0]->get_links();
  300. }
  301. if (count($test_cats) > 1 || !empty($links)) {
  302. if (api_get_setting('gradebook_enable_grade_model') == 'true') {
  303. $this->freeze('grade_model_id');
  304. }
  305. }
  306. $generateCertificatesParams = array();
  307. if ($this->category_object->getGenerateCertificates()) {
  308. $generateCertificatesParams['checked'] = 'checked';
  309. }
  310. $this->addElement(
  311. 'checkbox',
  312. 'generate_certificates',
  313. null,
  314. get_lang('GenerateCertificates'),
  315. $generateCertificatesParams
  316. );
  317. }
  318. if (!empty($session_id)) {
  319. $isRequirementCheckbox = $this->addCheckBox(
  320. 'is_requirement',
  321. [
  322. null,
  323. get_lang('ConsiderThisGradebookAsRequirementForASessionSequence')
  324. ],
  325. get_lang('IsRequirement')
  326. );
  327. }
  328. if ($this->category_object->getIsRequirement()) {
  329. $isRequirementCheckbox->setChecked(true);
  330. }
  331. if ($this->form_type == self::TYPE_ADD) {
  332. $this->addButtonCreate(get_lang('AddCategory'));
  333. } else {
  334. $this->addElement('hidden', 'editcat', intval($_GET['editcat']));
  335. $this->addButtonUpdate(get_lang('EditCategory'));
  336. }
  337. $setting = api_get_setting('tool_visible_by_default_at_creation');
  338. $visibility_default = 1;
  339. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  340. $visibility_default = 0;
  341. }
  342. $this->setDefaults(array('visible' => $visibility_default, 'skills' => $skillsDefaults));
  343. }
  344. /**
  345. * This function builds an 'select course' form in the add category process,
  346. * if parent id is 0, it will only show courses
  347. */
  348. protected function build_select_course_form()
  349. {
  350. $select = $this->addElement(
  351. 'select',
  352. 'select_course',
  353. array(get_lang('PickACourse'), 'test'),
  354. null
  355. );
  356. $coursecat = Category::get_all_courses(api_get_user_id());
  357. //only return courses that are not yet created by the teacher
  358. foreach ($coursecat as $row) {
  359. $select->addoption($row[1], $row[0]);
  360. }
  361. $this->setDefaults(array(
  362. 'hid_user_id' => $this->category_object->get_user_id(),
  363. 'hid_parent_id' => $this->category_object->get_parent_id()
  364. ));
  365. $this->addElement('hidden', 'hid_user_id');
  366. $this->addElement('hidden', 'hid_parent_id');
  367. $this->addElement('submit', null, get_lang('Ok'));
  368. }
  369. function display()
  370. {
  371. parent::display();
  372. }
  373. function setDefaults($defaults = array(), $filter = null)
  374. {
  375. parent::setDefaults($defaults, $filter);
  376. }
  377. }