catform.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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('class' => 'span3', '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. if (api_is_platform_admin() || api_is_drh()) {
  216. if (api_get_setting('allow_skills_tool') == 'true') {
  217. // The magic should be here
  218. $skills = $this->category_object->get_skills();
  219. $skillToSelect = array();
  220. foreach ($skills as $skill) {
  221. $skillToSelect[$skill['id']] = $skill['name'];
  222. }
  223. $this->addElement(
  224. 'select',
  225. 'skills',
  226. array(
  227. get_lang('Skills'),
  228. get_lang('SkillsAchievedWhenAchievingThisGradebook')
  229. ),
  230. $skillToSelect,
  231. array('id' => 'skills', 'multiple' => 'multiple')
  232. );
  233. $content = '';
  234. if (!empty($skills)) {
  235. foreach ($skills as $skill) {
  236. $content .= Display::tag(
  237. 'li',
  238. $skill['name'] . '<a id="deleteskill_' . $skill['id'] . '" class="closebutton" href="#"></a>',
  239. array(
  240. 'id' => 'skill_' . $skill['id'],
  241. 'class' => 'bit-box'
  242. )
  243. );
  244. }
  245. }
  246. $this->addElement(
  247. 'label',
  248. null,
  249. Display::tag(
  250. 'ul',
  251. $content,
  252. array('class' => 'holder holder_simple')
  253. )
  254. );
  255. }
  256. }
  257. if (isset($this->category_object) &&
  258. $this->category_object->get_parent_id() == 0
  259. ) {
  260. $this->addText(
  261. 'certif_min_score',
  262. get_lang('CertificateMinScore'),
  263. false,
  264. array('class' => 'span1', 'maxlength' => '5')
  265. );
  266. $this->addRule(
  267. 'certif_min_score',
  268. get_lang('ThisFieldIsRequired'),
  269. 'required'
  270. );
  271. $this->addRule(
  272. 'certif_min_score',
  273. get_lang('OnlyNumbers'),
  274. 'numeric'
  275. );
  276. $this->addRule(
  277. array('certif_min_score', 'zero'),
  278. get_lang('NegativeValue'),
  279. 'compare',
  280. '>='
  281. );
  282. } else {
  283. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  284. }
  285. $this->addElement('hidden', 'hid_user_id');
  286. $this->addElement('hidden', 'hid_parent_id');
  287. $this->addElement(
  288. 'textarea',
  289. 'description',
  290. get_lang('Description')
  291. );
  292. if (isset($this->category_object) &&
  293. $this->category_object->get_parent_id() == 0 &&
  294. (api_is_platform_admin() || api_get_setting(
  295. 'teachers_can_change_grade_model_settings'
  296. ) == 'true')
  297. ) {
  298. //Getting grade models
  299. $obj = new GradeModel();
  300. $obj->fill_grade_model_select_in_form($this, 'grade_model_id', $this->category_object->get_grade_model_id());
  301. /*
  302. $grade_models = $obj->get_all();
  303. $options = array(-1 => get_lang('None'));
  304. foreach ($grade_models as $item) {
  305. $options[$item['id']] = $item['name'];
  306. }
  307. $this->addElement('select', 'grade_model_id', array(get_lang('GradeModel'), get_lang('OnlyActiveWhenThereAreAnyComponents')), $options);
  308. *
  309. */
  310. //Freeze or not
  311. $course_code = api_get_course_id();
  312. $session_id = api_get_session_id();
  313. $test_cats = Category :: load(
  314. null,
  315. null,
  316. $course_code,
  317. null,
  318. null,
  319. $session_id,
  320. false
  321. ); //already init
  322. $links = null;
  323. if (!empty($test_cats[0])) {
  324. $links = $test_cats[0]->get_links();
  325. }
  326. if (count($test_cats) > 1 || !empty($links)) {
  327. if (api_get_setting('gradebook_enable_grade_model') == 'true') {
  328. $this->freeze('grade_model_id');
  329. }
  330. }
  331. $generateCertificatesParams = array();
  332. if ($this->category_object->getGenerateCertificates()) {
  333. $generateCertificatesParams['checked'] = 'checked';
  334. }
  335. $this->addElement(
  336. 'checkbox',
  337. 'generate_certificates',
  338. null,
  339. get_lang('GenerateCertificates'),
  340. $generateCertificatesParams
  341. );
  342. }
  343. if (!empty($session_id)) {
  344. $isRequirementCheckbox = $this->addCheckBox(
  345. 'is_requirement',
  346. [
  347. null,
  348. get_lang('ConsiderThisGradebookAsRequirementForASessionSequence')
  349. ],
  350. get_lang('IsRequirement')
  351. );
  352. }
  353. if ($this->category_object->getIsRequirement()) {
  354. $isRequirementCheckbox->setChecked(true);
  355. }
  356. if ($this->form_type == self :: TYPE_ADD) {
  357. $this->addButtonCreate(get_lang('AddCategory'));
  358. } else {
  359. $this->addElement('hidden', 'editcat', intval($_GET['editcat']));
  360. $this->addButtonUpdate(get_lang('EditCategory'));
  361. }
  362. //if (!empty($grading_contents)) {
  363. $this->addRule('weight', get_lang('OnlyNumbers'), 'numeric');
  364. //$this->addRule('weight',get_lang('NoDecimals'),'nopunctuation');
  365. $this->addRule(
  366. array('weight', 'zero'),
  367. get_lang('NegativeValue'),
  368. 'compare',
  369. '>='
  370. );
  371. //}
  372. $setting = api_get_setting('tool_visible_by_default_at_creation');
  373. $visibility_default = 1;
  374. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  375. $visibility_default = 0;
  376. }
  377. $this->setDefaults(array('visible' => $visibility_default));
  378. }
  379. /**
  380. * This function builds an 'select course' form in the add category process,
  381. * if parent id is 0, it will only show courses
  382. */
  383. protected function build_select_course_form()
  384. {
  385. $select = $this->addElement('select','select_course',array(get_lang('PickACourse'),'test'), null);
  386. $coursecat = Category :: get_all_courses(api_get_user_id());
  387. //only return courses that are not yet created by the teacher
  388. foreach($coursecat as $row) {
  389. $select->addoption($row[1],$row[0]);
  390. }
  391. $this->setDefaults(array(
  392. 'hid_user_id' => $this->category_object->get_user_id(),
  393. 'hid_parent_id' => $this->category_object->get_parent_id()
  394. ));
  395. $this->addElement('hidden','hid_user_id');
  396. $this->addElement('hidden','hid_parent_id');
  397. $this->addElement('submit', null, get_lang('Ok'));
  398. }
  399. function display()
  400. {
  401. parent :: display();
  402. }
  403. function setDefaults($defaults = array(), $filter = null)
  404. {
  405. parent::setDefaults($defaults, $filter);
  406. }
  407. }