catform.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. switch ($this->form_type) {
  41. case self::TYPE_EDIT:
  42. $this->build_editing_form();
  43. break;
  44. case self::TYPE_ADD:
  45. $this->build_add_form();
  46. break;
  47. case self::TYPE_MOVE:
  48. $this->build_move_form();
  49. break;
  50. case self::TYPE_SELECT_COURSE:
  51. $this->build_select_course_form();
  52. break;
  53. }
  54. $this->setDefaults();
  55. }
  56. /**
  57. * This function will build a move form that will allow the user to move a category to
  58. * a another.
  59. */
  60. protected function build_move_form()
  61. {
  62. $renderer = &$this->defaultRenderer();
  63. $renderer->setCustomElementTemplate('<span>{element}</span> ');
  64. $this->addElement(
  65. 'static',
  66. null,
  67. null,
  68. '"'.$this->category_object->get_name().'" '
  69. );
  70. $this->addElement('static', null, null, get_lang('Move to').' : ');
  71. $select = $this->addElement('select', 'move_cat', null, null);
  72. $line = null;
  73. foreach ($this->category_object->get_target_categories() as $cat) {
  74. for ($i = 0; $i < $cat[2]; $i++) {
  75. $line .= '--';
  76. }
  77. if ($cat[0] != $this->category_object->get_parent_id()) {
  78. $select->addoption($line.' '.$cat[1], $cat[0]);
  79. } else {
  80. $select->addoption($line.' '.$cat[1], $cat[0], 'disabled');
  81. }
  82. $line = '';
  83. }
  84. $this->addElement('submit', null, get_lang('Validate'));
  85. }
  86. /**
  87. * This function builds an 'add category form, if parent id is 0, it will only
  88. * show courses.
  89. */
  90. protected function build_add_form()
  91. {
  92. // check if we are a root category
  93. // if so, you can only choose between courses
  94. if ($this->category_object->get_parent_id() == '0') {
  95. $this->setDefaults(
  96. [
  97. 'select_course' => $this->category_object->get_course_code(),
  98. 'hid_user_id' => $this->category_object->get_user_id(),
  99. 'hid_parent_id' => $this->category_object->get_parent_id(),
  100. ]
  101. );
  102. } else {
  103. $this->setDefaults(
  104. [
  105. 'hid_user_id' => $this->category_object->get_user_id(),
  106. 'hid_parent_id' => $this->category_object->get_parent_id(),
  107. ]
  108. );
  109. $this->addElement(
  110. 'hidden',
  111. 'course_code',
  112. $this->category_object->get_course_code()
  113. );
  114. }
  115. $this->build_basic_form();
  116. }
  117. /**
  118. * Builds an form to edit a category.
  119. */
  120. protected function build_editing_form()
  121. {
  122. $skills = $this->category_object->getSkillsForSelect();
  123. $course_code = api_get_course_id();
  124. $session_id = api_get_session_id();
  125. $test_cats = Category::load(
  126. null,
  127. null,
  128. $course_code,
  129. null,
  130. null,
  131. $session_id,
  132. false
  133. );
  134. $links = null;
  135. if (isset($test_cats[0])) {
  136. $links = $test_cats[0]->get_links();
  137. }
  138. $grade_model_id = $this->category_object->get_grade_model_id();
  139. if (empty($links)) {
  140. $grade_model_id = 0;
  141. }
  142. $category_name = $this->category_object->get_name();
  143. // The main course category:
  144. if (isset($this->category_object) && $this->category_object->get_parent_id() == 0) {
  145. if (empty($category_name)) {
  146. $category_name = $course_code;
  147. }
  148. }
  149. $this->setDefaults(
  150. [
  151. 'name' => $category_name,
  152. 'description' => $this->category_object->get_description(),
  153. 'hid_user_id' => $this->category_object->get_user_id(),
  154. 'hid_parent_id' => $this->category_object->get_parent_id(),
  155. 'grade_model_id' => $grade_model_id,
  156. 'skills' => $skills,
  157. 'weight' => $this->category_object->get_weight(),
  158. 'visible' => $this->category_object->is_visible(),
  159. 'certif_min_score' => $this->category_object->getCertificateMinScore(),
  160. 'generate_certificates' => $this->category_object->getGenerateCertificates(),
  161. 'is_requirement' => $this->category_object->getIsRequirement(),
  162. ]
  163. );
  164. $this->addElement('hidden', 'hid_id', $this->category_object->get_id());
  165. $this->addElement(
  166. 'hidden',
  167. 'course_code',
  168. $this->category_object->get_course_code()
  169. );
  170. $this->build_basic_form();
  171. }
  172. /**
  173. * This function builds an 'select course' form in the add category process,
  174. * if parent id is 0, it will only show courses.
  175. */
  176. protected function build_select_course_form()
  177. {
  178. $select = $this->addElement(
  179. 'select',
  180. 'select_course',
  181. [get_lang('Pick a course'), 'test'],
  182. null
  183. );
  184. $courses = Category::get_all_courses(api_get_user_id());
  185. //only return courses that are not yet created by the teacher
  186. foreach ($courses as $row) {
  187. $select->addoption($row[1], $row[0]);
  188. }
  189. $this->setDefaults([
  190. 'hid_user_id' => $this->category_object->get_user_id(),
  191. 'hid_parent_id' => $this->category_object->get_parent_id(),
  192. ]);
  193. $this->addElement('hidden', 'hid_user_id');
  194. $this->addElement('hidden', 'hid_parent_id');
  195. $this->addElement('submit', null, get_lang('Validate'));
  196. }
  197. private function build_basic_form()
  198. {
  199. $this->addText(
  200. 'name',
  201. get_lang('Category name'),
  202. true,
  203. ['maxlength' => '50']
  204. );
  205. $this->addRule('name', get_lang('Required field'), 'required');
  206. if (isset($this->category_object) &&
  207. $this->category_object->get_parent_id() == 0
  208. ) {
  209. // we can't change the root category
  210. $this->freeze('name');
  211. }
  212. $global_weight = api_get_setting('gradebook_default_weight');
  213. $value = 100;
  214. if (isset($global_weight)) {
  215. $value = $global_weight;
  216. }
  217. $this->addFloat(
  218. 'weight',
  219. [
  220. get_lang('Total weight'),
  221. get_lang('The sum of all weights of activities inside this assessment has to be equivalent to this number.'),
  222. ],
  223. true,
  224. ['value' => $value, 'maxlength' => '5']
  225. );
  226. $skillsDefaults = [];
  227. $allowSkillEdit = api_is_platform_admin() || api_is_drh();
  228. if (api_get_configuration_value('skills_teachers_can_assign_skills')) {
  229. $allowSkillEdit = $allowSkillEdit || api_is_allowed_to_edit();
  230. }
  231. if ($allowSkillEdit) {
  232. if (Skill::isToolAvailable()) {
  233. $skillSelect = $this->addElement(
  234. 'select_ajax',
  235. 'skills',
  236. [
  237. get_lang('Skills'),
  238. get_lang('Skills obtained when achieving this assessment'),
  239. ],
  240. null,
  241. [
  242. 'id' => 'skills',
  243. 'multiple' => 'multiple',
  244. 'url' => api_get_path(WEB_AJAX_PATH).'skill.ajax.php?a=search_skills',
  245. ]
  246. );
  247. // The magic should be here
  248. $skills = $this->category_object->get_skills();
  249. foreach ($skills as $skill) {
  250. $skillsDefaults[] = $skill['id'];
  251. $skillSelect->addOption($skill['name'], $skill['id']);
  252. }
  253. }
  254. }
  255. $defaultCertification = 0;
  256. if (!empty($this->category_object)) {
  257. $defaultCertification = $this->category_object->getCertificateMinScore();
  258. }
  259. if (isset($this->category_object) &&
  260. $this->category_object->get_parent_id() == 0
  261. ) {
  262. $model = ExerciseLib::getCourseScoreModel();
  263. if (empty($model)) {
  264. $this->addText(
  265. 'certif_min_score',
  266. get_lang('Minimum certification score'),
  267. true,
  268. ['maxlength' => '5']
  269. );
  270. } else {
  271. $questionWeighting = $value;
  272. $defaultCertification = api_number_format($this->category_object->getCertificateMinScore(), 2);
  273. $select = $this->addSelect(
  274. 'certif_min_score',
  275. get_lang('Minimum certification score'),
  276. [],
  277. ['disable_js' => true]
  278. );
  279. foreach ($model['score_list'] as $item) {
  280. $i = api_number_format($item['score_to_qualify'] / 100 * $questionWeighting, 2);
  281. $model = ExerciseLib::getModelStyle($item, $i);
  282. $attributes = ['class' => $item['css_class']];
  283. if ($defaultCertification == $i) {
  284. $attributes['selected'] = 'selected';
  285. }
  286. $select->addOption($model, $i, $attributes);
  287. }
  288. $select->updateSelectWithSelectedOption($this);
  289. }
  290. $this->addRule(
  291. 'certif_min_score',
  292. get_lang('Only numbers'),
  293. 'numeric'
  294. );
  295. $this->addRule(
  296. 'certif_min_score',
  297. get_lang('Negative value'),
  298. 'compare',
  299. '>=',
  300. 'server',
  301. false,
  302. false,
  303. 0
  304. );
  305. } else {
  306. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  307. }
  308. $this->addElement('hidden', 'hid_user_id');
  309. $this->addElement('hidden', 'hid_parent_id');
  310. $this->addElement(
  311. 'textarea',
  312. 'description',
  313. get_lang('Description')
  314. );
  315. if (isset($this->category_object) &&
  316. $this->category_object->get_parent_id() == 0 &&
  317. (api_is_platform_admin() || api_get_setting('teachers_can_change_grade_model_settings') == 'true')
  318. ) {
  319. // Getting grade models
  320. $obj = new GradeModel();
  321. $obj->fill_grade_model_select_in_form(
  322. $this,
  323. 'grade_model_id',
  324. $this->category_object->get_grade_model_id()
  325. );
  326. // Freeze or not
  327. $course_code = api_get_course_id();
  328. $session_id = api_get_session_id();
  329. $test_cats = Category::load(
  330. null,
  331. null,
  332. $course_code,
  333. null,
  334. null,
  335. $session_id,
  336. false
  337. );
  338. $links = null;
  339. if (!empty($test_cats[0])) {
  340. $links = $test_cats[0]->get_links();
  341. }
  342. if (count($test_cats) > 1 || !empty($links)) {
  343. if (api_get_setting('gradebook_enable_grade_model') == 'true') {
  344. $this->freeze('grade_model_id');
  345. }
  346. }
  347. $generateCertificatesParams = [];
  348. if ($this->category_object->getGenerateCertificates()) {
  349. $generateCertificatesParams['checked'] = 'checked';
  350. }
  351. $this->addElement(
  352. 'checkbox',
  353. 'generate_certificates',
  354. null,
  355. get_lang('Generate certificates'),
  356. $generateCertificatesParams
  357. );
  358. }
  359. if (!empty($session_id)) {
  360. $isRequirementCheckbox = $this->addCheckBox(
  361. 'is_requirement',
  362. [
  363. null,
  364. get_lang('Consider this gradebook as a requirement to complete the course (influences the sessions sequences)'),
  365. ],
  366. get_lang('Is requirement')
  367. );
  368. }
  369. if ($this->category_object->getIsRequirement()) {
  370. $isRequirementCheckbox->setChecked(true);
  371. }
  372. $documentId = $this->category_object->getDocumentId();
  373. if (!empty($documentId)) {
  374. $documentData = DocumentManager::get_document_data_by_id($documentId, api_get_course_id());
  375. if (!empty($documentData)) {
  376. $this->addLabel(get_lang('Certificate'), $documentData['title']);
  377. }
  378. }
  379. if ($this->form_type == self::TYPE_ADD) {
  380. $this->addButtonCreate(get_lang('Add category'));
  381. } else {
  382. $this->addElement('hidden', 'editcat', intval($_GET['editcat']));
  383. $this->addButtonUpdate(get_lang('Edit this category'));
  384. }
  385. $setting = api_get_setting('tool_visible_by_default_at_creation');
  386. $visibility_default = 1;
  387. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  388. $visibility_default = 0;
  389. }
  390. $this->setDefaults(
  391. [
  392. 'visible' => $visibility_default,
  393. 'skills' => $skillsDefaults,
  394. 'certif_min_score' => (string) $defaultCertification,
  395. ]
  396. );
  397. }
  398. }