catform.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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('MoveTo').' : ');
  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('Ok'));
  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('PickACourse'), '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('Ok'));
  196. }
  197. private function build_basic_form()
  198. {
  199. $this->addText(
  200. 'name',
  201. get_lang('CategoryName'),
  202. true,
  203. ['maxlength' => '50']
  204. );
  205. $this->addRule('name', get_lang('ThisFieldIsRequired'), '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('TotalWeight'),
  221. get_lang('TotalSumOfWeights'),
  222. ],
  223. true,
  224. ['value' => $value, 'maxlength' => '5']
  225. );
  226. $skillsDefaults = [];
  227. if (api_is_platform_admin() || api_is_drh()) {
  228. if (Skill::isToolAvailable()) {
  229. $skillSelect = $this->addElement(
  230. 'select_ajax',
  231. 'skills',
  232. [
  233. get_lang('Skills'),
  234. get_lang('SkillsAchievedWhenAchievingThisGradebook'),
  235. ],
  236. null,
  237. [
  238. 'id' => 'skills',
  239. 'multiple' => 'multiple',
  240. 'url' => api_get_path(WEB_AJAX_PATH).'skill.ajax.php?a=search_skills',
  241. ]
  242. );
  243. // The magic should be here
  244. $skills = $this->category_object->get_skills();
  245. foreach ($skills as $skill) {
  246. $skillsDefaults[] = $skill['id'];
  247. $skillSelect->addOption($skill['name'], $skill['id']);
  248. }
  249. }
  250. }
  251. $defaultCertification = 0;
  252. if (isset($this->category_object) &&
  253. $this->category_object->get_parent_id() == 0
  254. ) {
  255. $model = ExerciseLib::getCourseScoreModel();
  256. if (empty($model)) {
  257. $this->addText(
  258. 'certif_min_score',
  259. get_lang('CertificateMinScore'),
  260. true,
  261. ['maxlength' => '5']
  262. );
  263. } else {
  264. $questionWeighting = $value;
  265. $defaultCertification = api_number_format($this->category_object->getCertificateMinScore(), 2);
  266. $select = $this->addSelect(
  267. 'certif_min_score',
  268. get_lang('CertificateMinScore'),
  269. [],
  270. ['disable_js' => true]
  271. );
  272. foreach ($model['score_list'] as $item) {
  273. $i = api_number_format($item['score_to_qualify'] / 100 * $questionWeighting, 2);
  274. $model = ExerciseLib::getModelStyle($item, $i);
  275. $attributes = ['class' => $item['css_class']];
  276. if ($defaultCertification == $i) {
  277. $attributes['selected'] = 'selected';
  278. }
  279. $select->addOption($model, $i, $attributes);
  280. }
  281. $select->updateSelectWithSelectedOption($this);
  282. }
  283. $this->addRule(
  284. 'certif_min_score',
  285. get_lang('OnlyNumbers'),
  286. 'numeric'
  287. );
  288. $this->addRule(
  289. 'certif_min_score',
  290. get_lang('NegativeValue'),
  291. 'compare',
  292. '>=',
  293. 'server',
  294. false,
  295. false,
  296. 0
  297. );
  298. } else {
  299. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  300. }
  301. $this->addElement('hidden', 'hid_user_id');
  302. $this->addElement('hidden', 'hid_parent_id');
  303. $this->addElement(
  304. 'textarea',
  305. 'description',
  306. get_lang('Description')
  307. );
  308. if (isset($this->category_object) &&
  309. $this->category_object->get_parent_id() == 0 &&
  310. (api_is_platform_admin() || api_get_setting('teachers_can_change_grade_model_settings') == 'true')
  311. ) {
  312. // Getting grade models
  313. $obj = new GradeModel();
  314. $obj->fill_grade_model_select_in_form(
  315. $this,
  316. 'grade_model_id',
  317. $this->category_object->get_grade_model_id()
  318. );
  319. // Freeze or not
  320. $course_code = api_get_course_id();
  321. $session_id = api_get_session_id();
  322. $test_cats = Category::load(
  323. null,
  324. null,
  325. $course_code,
  326. null,
  327. null,
  328. $session_id,
  329. false
  330. );
  331. $links = null;
  332. if (!empty($test_cats[0])) {
  333. $links = $test_cats[0]->get_links();
  334. }
  335. if (count($test_cats) > 1 || !empty($links)) {
  336. if (api_get_setting('gradebook_enable_grade_model') == 'true') {
  337. $this->freeze('grade_model_id');
  338. }
  339. }
  340. $generateCertificatesParams = [];
  341. if ($this->category_object->getGenerateCertificates()) {
  342. $generateCertificatesParams['checked'] = 'checked';
  343. }
  344. $this->addElement(
  345. 'checkbox',
  346. 'generate_certificates',
  347. null,
  348. get_lang('GenerateCertificates'),
  349. $generateCertificatesParams
  350. );
  351. }
  352. if (!empty($session_id)) {
  353. $isRequirementCheckbox = $this->addCheckBox(
  354. 'is_requirement',
  355. [
  356. null,
  357. get_lang('ConsiderThisGradebookAsRequirementForASessionSequence'),
  358. ],
  359. get_lang('IsRequirement')
  360. );
  361. }
  362. if ($this->category_object->getIsRequirement()) {
  363. $isRequirementCheckbox->setChecked(true);
  364. }
  365. $documentId = $this->category_object->getDocumentId();
  366. if (!empty($documentId)) {
  367. $documentData = DocumentManager::get_document_data_by_id($documentId, api_get_course_id());
  368. if (!empty($documentData)) {
  369. $this->addLabel(get_lang('Certificate'), $documentData['title']);
  370. }
  371. }
  372. if ($this->form_type == self::TYPE_ADD) {
  373. $this->addButtonCreate(get_lang('AddCategory'));
  374. } else {
  375. $this->addElement('hidden', 'editcat', intval($_GET['editcat']));
  376. $this->addButtonUpdate(get_lang('EditCategory'));
  377. }
  378. $setting = api_get_setting('tool_visible_by_default_at_creation');
  379. $visibility_default = 1;
  380. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  381. $visibility_default = 0;
  382. }
  383. $this->setDefaults(
  384. [
  385. 'visible' => $visibility_default,
  386. 'skills' => $skillsDefaults,
  387. 'certif_min_score' => (string) $defaultCertification,
  388. ]
  389. );
  390. }
  391. }