catform.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once dirname(__FILE__).'/../../../inc/global.inc.php';
  4. require_once dirname(__FILE__).'/../be.inc.php';
  5. /**
  6. * Class CatForm
  7. *
  8. * @author Stijn Konings
  9. * @package chamilo.gradebook
  10. */
  11. class CatForm extends FormValidator
  12. {
  13. const TYPE_ADD = 1;
  14. const TYPE_EDIT = 2;
  15. const TYPE_MOVE = 3;
  16. const TYPE_SELECT_COURSE = 4;
  17. private $category_object;
  18. /**
  19. * Builds a form containing form items based on a given parameter
  20. * @param int form_type 1=add, 2=edit,3=move,4=browse
  21. * @param obj cat_obj the category object
  22. * @param string form name
  23. * @param method method
  24. */
  25. public function CatForm(
  26. $form_type,
  27. $category_object,
  28. $form_name,
  29. $method = 'post',
  30. $action = null
  31. ) {
  32. parent :: __construct($form_name, $method, $action);
  33. $this->form_type = $form_type;
  34. if (isset ($category_object)) {
  35. $this->category_object = $category_object;
  36. }
  37. if ($this->form_type == self :: TYPE_EDIT) {
  38. $this->build_editing_form();
  39. } elseif ($this->form_type == self :: TYPE_ADD) {
  40. $this->build_add_form();
  41. } elseif ($this->form_type == self :: TYPE_MOVE) {
  42. $this->build_move_form();
  43. } elseif ($this->form_type == self :: TYPE_SELECT_COURSE) {
  44. $this->build_select_course_form();
  45. }
  46. $this->setDefaults();
  47. }
  48. /**
  49. * This function will build a move form that will allow the user to move a category to
  50. * a another
  51. */
  52. protected function build_move_form()
  53. {
  54. $renderer =& $this->defaultRenderer();
  55. $renderer->setElementTemplate('<span>{element}</span> ');
  56. $this->addElement(
  57. 'static',
  58. null,
  59. null,
  60. '"' . $this->category_object->get_name() . '" '
  61. );
  62. $this->addElement('static',null,null,get_lang('MoveTo'). ' : ');
  63. $select = $this->addElement('select','move_cat',null,null);
  64. $line = null;
  65. foreach ($this->category_object->get_target_categories() as $cat) {
  66. for ($i=0;$i<$cat[2];$i++) {
  67. $line .= '--';
  68. }
  69. if ($cat[0] != $this->category_object->get_parent_id()) {
  70. $select->addoption($line.' '.$cat[1],$cat[0]);
  71. } else {
  72. $select->addoption($line.' '.$cat[1],$cat[0],'disabled');
  73. }
  74. $line = '';
  75. }
  76. $this->addElement('submit', null, get_lang('Ok'));
  77. }
  78. /**
  79. * This function builds an 'add category form, if parent id is 0, it will only
  80. * show courses
  81. */
  82. protected function build_add_form()
  83. {
  84. //check if we are a root category
  85. //if so, you can only choose between courses
  86. if ($this->category_object->get_parent_id() == '0') {
  87. //$select = $this->addElement('select','select_course',array(get_lang('PickACourse'),'test'), null);
  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->get_skills_for_select();
  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->get_certificate_min_score(),
  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->add_textfield(
  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->add_textfield('weight', array(get_lang('TotalWeight'), get_lang('TotalSumOfWeights')), true, array('value'=>$value, 'class'=>'span1','maxlength'=>'5'));
  207. $this->addRule('weight',get_lang('ThisFieldIsRequired'),'required');
  208. if (api_is_platform_admin() || api_is_drh()) {
  209. if (api_get_setting('allow_skills_tool') == 'true') {
  210. //the magic should be here
  211. $skills = $this->category_object->get_skills();
  212. $skillToSelect = array();
  213. foreach ($skills as $skill) {
  214. $skillToSelect[$skill['id']] = $skill['name'];
  215. }
  216. $this->addElement(
  217. 'select',
  218. 'skills',
  219. array(
  220. get_lang('Skills'),
  221. get_lang('SkillsAchievedWhenAchievingThisGradebook')
  222. ),
  223. $skillToSelect,
  224. array('id' => 'skills', 'multiple' => 'multiple')
  225. );
  226. $content = '';
  227. if (!empty($skills)) {
  228. foreach($skills as $skill) {
  229. $content .= Display::tag(
  230. 'li',
  231. $skill['name'] . '<a id="deleteskill_' . $skill['id'] . '" class="closebutton" href="#"></a>',
  232. array(
  233. 'id' => 'skill_' . $skill['id'],
  234. 'class' => 'bit-box'
  235. )
  236. );
  237. }
  238. }
  239. $this->addElement(
  240. 'label',
  241. null,
  242. Display::tag(
  243. 'ul',
  244. $content,
  245. array('class' => 'holder holder_simple')
  246. )
  247. );
  248. }
  249. }
  250. if (isset($this->category_object) && $this->category_object->get_parent_id(
  251. ) == 0
  252. ) {
  253. $this->add_textfield(
  254. 'certif_min_score',
  255. get_lang('CertificateMinScore'),
  256. false,
  257. array('class' => 'span1', 'maxlength' => '5')
  258. );
  259. $this->addRule(
  260. 'certif_min_score',
  261. get_lang('ThisFieldIsRequired'),
  262. 'required'
  263. );
  264. $this->addRule(
  265. 'certif_min_score',
  266. get_lang('OnlyNumbers'),
  267. 'numeric'
  268. );
  269. $this->addRule(
  270. array('certif_min_score', 'zero'),
  271. get_lang('NegativeValue'),
  272. 'compare',
  273. '>='
  274. );
  275. } else {
  276. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  277. }
  278. $this->addElement('hidden','hid_user_id');
  279. $this->addElement('hidden','hid_parent_id');
  280. $this->addElement(
  281. 'textarea',
  282. 'description',
  283. get_lang('Description'),
  284. array('class' => 'span3', 'cols' => '34')
  285. );
  286. if (isset($this->category_object) &&
  287. $this->category_object->get_parent_id() == 0 &&
  288. (api_is_platform_admin() || api_get_setting(
  289. 'teachers_can_change_grade_model_settings'
  290. ) == 'true')
  291. ) {
  292. //Getting grade models
  293. $obj = new GradeModel();
  294. $obj->fill_grade_model_select_in_form($this, 'grade_model_id', $this->category_object->get_grade_model_id());
  295. /*
  296. $grade_models = $obj->get_all();
  297. $options = array(-1 => get_lang('None'));
  298. foreach ($grade_models as $item) {
  299. $options[$item['id']] = $item['name'];
  300. }
  301. $this->addElement('select', 'grade_model_id', array(get_lang('GradeModel'), get_lang('OnlyActiveWhenThereAreAnyComponents')), $options);
  302. *
  303. */
  304. //Freeze or not
  305. $course_code = api_get_course_id();
  306. $session_id = api_get_session_id();
  307. $test_cats = Category :: load(
  308. null,
  309. null,
  310. $course_code,
  311. null,
  312. null,
  313. $session_id,
  314. false
  315. ); //already init
  316. $links = null;
  317. if (!empty($test_cats[0])) {
  318. $links = $test_cats[0]->get_links();
  319. }
  320. if (count($test_cats) > 1 || !empty($links)) {
  321. if (api_get_setting('gradebook_enable_grade_model') == 'true') {
  322. $this->freeze('grade_model_id');
  323. }
  324. }
  325. }
  326. if ($this->form_type == self :: TYPE_ADD) {
  327. $this->addElement(
  328. 'style_submit_button',
  329. null,
  330. get_lang('AddCategory'),
  331. 'class="save"'
  332. );
  333. } else {
  334. $this->addElement('hidden','editcat', intval($_GET['editcat']));
  335. $this->addElement(
  336. 'style_submit_button',
  337. null,
  338. get_lang('EditCategory'),
  339. 'class="save"'
  340. );
  341. }
  342. //if (!empty($grading_contents)) {
  343. $this->addRule('weight', get_lang('OnlyNumbers'), 'numeric');
  344. //$this->addRule('weight',get_lang('NoDecimals'),'nopunctuation');
  345. $this->addRule(
  346. array('weight', 'zero'),
  347. get_lang('NegativeValue'),
  348. 'compare',
  349. '>='
  350. );
  351. //}
  352. $setting = api_get_setting('tool_visible_by_default_at_creation');
  353. $visibility_default = 1;
  354. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  355. $visibility_default = 0;
  356. }
  357. $this->setDefaults(array('visible' => $visibility_default));
  358. }
  359. /**
  360. * This function builds an 'select course' form in the add category process,
  361. * if parent id is 0, it will only show courses
  362. */
  363. protected function build_select_course_form()
  364. {
  365. $select = $this->addElement('select','select_course',array(get_lang('PickACourse'),'test'), null);
  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. }