catform.class.php 12 KB

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