catform.class.php 14 KB

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