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