catform.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 (!empty($this->category_object)) {
  253. $defaultCertification = $this->category_object->getCertificateMinScore();
  254. }
  255. if (isset($this->category_object) &&
  256. $this->category_object->get_parent_id() == 0
  257. ) {
  258. $model = ExerciseLib::getCourseScoreModel();
  259. if (empty($model)) {
  260. $this->addText(
  261. 'certif_min_score',
  262. get_lang('CertificateMinScore'),
  263. true,
  264. ['maxlength' => '5']
  265. );
  266. } else {
  267. $questionWeighting = $value;
  268. $defaultCertification = api_number_format($this->category_object->getCertificateMinScore(), 2);
  269. $select = $this->addSelect(
  270. 'certif_min_score',
  271. get_lang('CertificateMinScore'),
  272. [],
  273. ['disable_js' => true]
  274. );
  275. foreach ($model['score_list'] as $item) {
  276. $i = api_number_format($item['score_to_qualify'] / 100 * $questionWeighting, 2);
  277. $model = ExerciseLib::getModelStyle($item, $i);
  278. $attributes = ['class' => $item['css_class']];
  279. if ($defaultCertification == $i) {
  280. $attributes['selected'] = 'selected';
  281. }
  282. $select->addOption($model, $i, $attributes);
  283. }
  284. $select->updateSelectWithSelectedOption($this);
  285. }
  286. $this->addRule(
  287. 'certif_min_score',
  288. get_lang('OnlyNumbers'),
  289. 'numeric'
  290. );
  291. $this->addRule(
  292. 'certif_min_score',
  293. get_lang('NegativeValue'),
  294. 'compare',
  295. '>=',
  296. 'server',
  297. false,
  298. false,
  299. 0
  300. );
  301. } else {
  302. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  303. }
  304. $this->addElement('hidden', 'hid_user_id');
  305. $this->addElement('hidden', 'hid_parent_id');
  306. $this->addElement(
  307. 'textarea',
  308. 'description',
  309. get_lang('Description')
  310. );
  311. if (isset($this->category_object) &&
  312. $this->category_object->get_parent_id() == 0 &&
  313. (api_is_platform_admin() || api_get_setting('teachers_can_change_grade_model_settings') == 'true')
  314. ) {
  315. // Getting grade models
  316. $obj = new GradeModel();
  317. $obj->fill_grade_model_select_in_form(
  318. $this,
  319. 'grade_model_id',
  320. $this->category_object->get_grade_model_id()
  321. );
  322. // Freeze or not
  323. $course_code = api_get_course_id();
  324. $session_id = api_get_session_id();
  325. $test_cats = Category::load(
  326. null,
  327. null,
  328. $course_code,
  329. null,
  330. null,
  331. $session_id,
  332. false
  333. );
  334. $links = null;
  335. if (!empty($test_cats[0])) {
  336. $links = $test_cats[0]->get_links();
  337. }
  338. if (count($test_cats) > 1 || !empty($links)) {
  339. if (api_get_setting('gradebook_enable_grade_model') == 'true') {
  340. $this->freeze('grade_model_id');
  341. }
  342. }
  343. $generateCertificatesParams = [];
  344. if ($this->category_object->getGenerateCertificates()) {
  345. $generateCertificatesParams['checked'] = 'checked';
  346. }
  347. $this->addElement(
  348. 'checkbox',
  349. 'generate_certificates',
  350. null,
  351. get_lang('GenerateCertificates'),
  352. $generateCertificatesParams
  353. );
  354. }
  355. if (!empty($session_id)) {
  356. $isRequirementCheckbox = $this->addCheckBox(
  357. 'is_requirement',
  358. [
  359. null,
  360. get_lang('ConsiderThisGradebookAsRequirementForASessionSequence'),
  361. ],
  362. get_lang('IsRequirement')
  363. );
  364. }
  365. if ($this->category_object->getIsRequirement()) {
  366. $isRequirementCheckbox->setChecked(true);
  367. }
  368. $documentId = $this->category_object->getDocumentId();
  369. if (!empty($documentId)) {
  370. $documentData = DocumentManager::get_document_data_by_id($documentId, api_get_course_id());
  371. if (!empty($documentData)) {
  372. $this->addLabel(get_lang('Certificate'), $documentData['title']);
  373. }
  374. }
  375. if ($this->form_type == self::TYPE_ADD) {
  376. $this->addButtonCreate(get_lang('AddCategory'));
  377. } else {
  378. $this->addElement('hidden', 'editcat', intval($_GET['editcat']));
  379. $this->addButtonUpdate(get_lang('EditCategory'));
  380. }
  381. $setting = api_get_setting('tool_visible_by_default_at_creation');
  382. $visibility_default = 1;
  383. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  384. $visibility_default = 0;
  385. }
  386. $this->setDefaults(
  387. [
  388. 'visible' => $visibility_default,
  389. 'skills' => $skillsDefaults,
  390. 'certif_min_score' => (string) $defaultCertification,
  391. ]
  392. );
  393. }
  394. }