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