evalform.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. /**
  8. * Init
  9. */
  10. require_once dirname(__FILE__).'/../../../inc/global.inc.php';
  11. require_once dirname(__FILE__).'/../be.inc.php';
  12. require_once dirname(__FILE__).'/../gradebook_functions.inc.php';
  13. require_once api_get_path(LIBRARY_PATH) . 'groupmanager.lib.php';
  14. /**
  15. * Extends formvalidator with add&edit forms for evaluations
  16. * @author Stijn Konings
  17. * @package chamilo.gradebook
  18. */
  19. $htmlHeadXtra[] = '<script type="text/javascript">
  20. function setFocus(){
  21. $("#evaluation_title").focus();
  22. }
  23. $(document).ready(function () {
  24. setFocus();
  25. });
  26. </script>';
  27. class EvalForm extends FormValidator
  28. {
  29. const TYPE_ADD= 1;
  30. const TYPE_EDIT= 2;
  31. const TYPE_MOVE= 3;
  32. const TYPE_RESULT_ADD= 4;
  33. const TYPE_RESULT_EDIT= 5;
  34. const TYPE_ALL_RESULTS_EDIT= 6;
  35. const TYPE_ADD_USERS_TO_EVAL= 7;
  36. private $evaluation_object;
  37. private $result_object;
  38. private $extra;
  39. /**
  40. * Builds a form containing form items based on a given parameter
  41. * @param int form_type 1=add, 2=edit,3=move,4=result_add
  42. * @param obj cat_obj the category object
  43. * @param obj res_obj the result object
  44. * @param string form name
  45. * @param method
  46. * @param action
  47. */
  48. public function __construct($form_type, $evaluation_object, $result_object, $form_name, $method= 'post', $action= null, $extra1 = null, $extra2 = null) {
  49. parent::__construct($form_name, $method, $action);
  50. if (isset ($evaluation_object)) {
  51. $this->evaluation_object= $evaluation_object;
  52. }
  53. if (isset ($result_object)) {
  54. $this->result_object= $result_object;
  55. }
  56. if (isset ($extra1)) {
  57. $this->extra = $extra1;
  58. }
  59. switch ($form_type) {
  60. case self :: TYPE_EDIT:
  61. $this->build_editing_form();
  62. break;
  63. case self :: TYPE_ADD:
  64. $this->build_add_form();
  65. break;
  66. case self :: TYPE_MOVE:
  67. $this->build_editing_form();
  68. break;
  69. case self :: TYPE_RESULT_ADD:
  70. $this->build_result_add_form();
  71. break;
  72. case self :: TYPE_RESULT_EDIT:
  73. $this->build_result_edit_form();
  74. break;
  75. case self :: TYPE_ALL_RESULTS_EDIT:
  76. $this->build_all_results_edit_form();
  77. break;
  78. case self :: TYPE_ADD_USERS_TO_EVAL:
  79. $this->build_add_user_to_eval();
  80. break;
  81. }
  82. $this->setDefaults();
  83. }
  84. /**
  85. * This form will build a form to add users to an evaluation
  86. */
  87. protected function build_add_user_to_eval() {
  88. $this->addElement('header', get_lang('ChooseUser'));
  89. $select= $this->addElement('select', 'firstLetterUser', get_lang('FirstLetter'), null, array(
  90. 'onchange'=> 'document.add_users_to_evaluation.submit()'
  91. ));
  92. $select->addOption('','');
  93. for ($i = 65; $i <= 90; $i ++) {
  94. $letter = chr($i);
  95. if (isset($this->extra) && $this->extra == $letter) {
  96. $select->addOption($letter,$letter,'selected');
  97. } else {
  98. $select->addOption($letter,$letter);
  99. }
  100. }
  101. $select= $this->addElement('select', 'add_users', null, null, array (
  102. 'multiple' => 'multiple',
  103. 'size' => '15',
  104. 'style' => 'width:250px'
  105. ));
  106. foreach ($this->evaluation_object->get_not_subscribed_students() as $user) {
  107. if ( (!isset($this->extra)) || empty($this->extra) || api_strtoupper(api_substr($user[1],0,1)) == $this->extra ) {
  108. $select->addoption($user[1] . ' ' . $user[2] . ' (' . $user[3] . ')', $user[0]);
  109. }
  110. }
  111. $this->addElement('submit', 'submit_button', get_lang('AddUserToEval'));
  112. }
  113. /**
  114. * This function builds a form to edit all results in an evaluation
  115. */
  116. protected function build_all_results_edit_form() {
  117. //extra field for check on maxvalue
  118. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  119. $this->addElement('hidden', 'minvalue', 0);
  120. $this->addElement('header', get_lang('EditResult'));
  121. $renderer =& $this->defaultRenderer();
  122. // set new form template
  123. $form_template = '<form{attributes}>
  124. <table class="data_table" border="0" cellpadding="5" cellspacing="5">{content}
  125. </table>
  126. </form>';
  127. $renderer->setFormTemplate($form_template);
  128. if (api_is_western_name_order()){
  129. $renderer->setHeaderTemplate(
  130. '<tr>
  131. <th>'.get_lang('OfficialCode').'</th>
  132. <th>'.get_lang('UserName').'</th>
  133. <th>'.get_lang('FirstName').'</th>
  134. <th>'.get_lang('LastName').'</th>
  135. <th>'.get_lang('Qualify').'</th>
  136. </tr>'
  137. );
  138. } else {
  139. $renderer->setHeaderTemplate(
  140. '<tr>
  141. <th>'.get_lang('OfficialCode').'</th>
  142. <th>'.get_lang('UserName').'</th>
  143. <th>'.get_lang('LastName').'</th>
  144. <th>'.get_lang('FirstName').'</th>
  145. <th>'.get_lang('Qualify').'</th>
  146. </tr>'
  147. );
  148. }
  149. $template_submit = '<tr>
  150. <td colspan="4" ></td>
  151. <td >
  152. {element}
  153. <!-- BEGIN error --><br /><span style="color: #ff0000;font-size:10px">{error}</span><!-- END error -->
  154. </td>
  155. </tr>';
  156. $results_and_users = array();
  157. foreach ($this->result_object as $result) {
  158. $user= get_user_info_from_id($result->get_user_id());
  159. $results_and_users[] = array ('result' => $result, 'user' => $user);
  160. }
  161. usort($results_and_users, array ('EvalForm', 'sort_by_user'));
  162. $defaults = array();
  163. foreach ($results_and_users as $result_and_user) {
  164. $user = $result_and_user['user'];
  165. $result = $result_and_user['result'];
  166. $renderer = &$this->defaultRenderer();
  167. $this->add_textfield('score[' . $result->get_id() . ']',
  168. $this->build_stud_label($user['user_id'], $user['username'], $user['lastname'], $user['firstname']),
  169. false,
  170. array ('class' => "span2",
  171. 'maxlength' => 5));
  172. $this->addRule('score[' . $result->get_id() . ']', get_lang('OnlyNumbers'), 'numeric');
  173. $this->addRule(array (
  174. 'score[' . $result->get_id() . ']', 'maxvalue'), get_lang('OverMax'), 'compare', '<=');
  175. $this->addRule(array (
  176. 'score[' . $result->get_id() . ']', 'minvalue'), get_lang('UnderMin'), 'compare', '>=');
  177. $defaults['score[' . $result->get_id() . ']'] = $result->get_score();
  178. if (api_is_western_name_order() ) {
  179. $user_info = '<td align="left" >'.$user['firstname'].'</td>';
  180. $user_info .= '<td align="left" >'.$user['lastname'].'</td>';
  181. } else {
  182. $user_info = '<td align="left" >'.$user['lastname'].'</td>';
  183. $user_info .= '<td align="left" >'.$user['firstname'].'</td>';
  184. }
  185. $template = '<tr>
  186. <td align="left" >'.$user['official_code'].'</td>
  187. <td align="left" >'.$user['username'].'</td>
  188. '.$user_info.'
  189. <td align="left">{element} / '.$this->evaluation_object->get_max().'
  190. <!-- BEGIN error --><br /><span style="color: #ff0000;font-size:10px">{error}</span><!-- END error -->
  191. </td>
  192. </tr>';
  193. $renderer->setElementTemplate($template,'score[' . $result->get_id() . ']');
  194. }
  195. $this->setDefaults($defaults);
  196. $this->addElement('style_submit_button', 'submit',get_lang('EditResult'),'class="save"');
  197. $renderer->setElementTemplate($template_submit,'submit');
  198. }
  199. /**
  200. * This function builds a form to move an item to another category
  201. *
  202. */
  203. protected function build_move_form() {
  204. $renderer = & $this->defaultRenderer();
  205. $renderer->setElementTemplate('<span>{element}</span> ');
  206. $this->addElement('static', null, null, '"'.$this->evaluation_object->get_name().'" ');
  207. $this->addElement('static', null, null, get_lang('MoveTo').' : ');
  208. $select= $this->addElement('select', 'move_cat', null, null);
  209. foreach ($this->evaluation_object->get_target_categories() as $cat) {
  210. for ($i= 0; $i < $cat[2]; $i++) {
  211. $line .= '&mdash;';
  212. }
  213. $select->addoption($line . ' ' . $cat[1], $cat[0]);
  214. $line= '';
  215. }
  216. $this->addElement('style_submit_button' , 'submit', get_lang('Ok'),'class="save"');
  217. }
  218. /**
  219. * Builds a result form containing inputs for all students with a given course_code
  220. */
  221. protected function build_result_add_form() {
  222. $renderer =& $this->defaultRenderer();
  223. $renderer->setFormTemplate(
  224. '<form{attributes}>
  225. <table class="data_table">
  226. {content}
  227. </table>
  228. </form>'
  229. );
  230. $tblusers = get_users_in_course($this->evaluation_object->get_course_code());
  231. $nr_users = 0;
  232. //extra field for check on maxvalue
  233. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  234. $this->addElement('hidden', 'minvalue', 0);
  235. $this->addElement('header', get_lang('AddResult'));
  236. if (api_is_western_name_order()){
  237. $renderer->setHeaderTemplate(
  238. '<tr>
  239. <th>'.get_lang('OfficialCode').'</th>
  240. <th>'.get_lang('UserName').'</th>
  241. <th>'.get_lang('FirstName').'</th>
  242. <th>'.get_lang('LastName').'</th>
  243. <th>'.get_lang('Qualify').'</th>
  244. </tr>'
  245. );
  246. } else {
  247. $renderer->setHeaderTemplate(
  248. '<tr>
  249. <th>'.get_lang('OfficialCode').'</th>
  250. <th>'.get_lang('UserName').'</th>
  251. <th>'.get_lang('LastName').'</th>
  252. <th>'.get_lang('FirstName').'</th>
  253. <th>'.get_lang('Qualify').'</th>
  254. </tr>'
  255. );
  256. }
  257. foreach ($tblusers as $user) {
  258. $element_name = 'score['.$user[0].']';
  259. //user_id, user.username, lastname, firstname
  260. $this->add_textfield($element_name,
  261. $this->build_stud_label($user[0], $user[1], $user[2], $user[3]),
  262. false,
  263. array ('class' => 'span1','maxlength' => 5));
  264. $this->addRule($element_name, get_lang('OnlyNumbers'), 'numeric');
  265. $this->addRule(array($element_name,'maxvalue'), get_lang('OverMax'), 'compare', '<=');
  266. $this->addRule(array($element_name,'minvalue'), get_lang('UnderMin'), 'compare', '>=');
  267. if (api_is_western_name_order() ) {
  268. $user_info = '<td align="left" >'.$user[3].'</td>';
  269. $user_info .= '<td align="left" >'.$user[2].'</td>';
  270. } else {
  271. $user_info = '<td align="left" >'.$user[2].'</td>';
  272. $user_info .= '<td align="left" >'.$user[3].'</td>';
  273. }
  274. $nr_users++;
  275. $template = '<tr>
  276. <td align="left" >'.$user[4].'</td>
  277. <td align="left" >'.$user[1].'</td>
  278. '.$user_info.'
  279. <td align="left">{element} / '.$this->evaluation_object->get_max().'
  280. <!-- BEGIN error --><br /><span style="color: #ff0000;font-size:10px">{error}</span><!-- END error -->
  281. </td>
  282. </tr>';
  283. $renderer->setElementTemplate($template, $element_name);
  284. }
  285. $this->addElement('hidden', 'nr_users', $nr_users);
  286. $this->addElement('hidden', 'evaluation_id', $this->result_object->get_evaluation_id());
  287. $this->addElement('style_submit_button', 'submit', get_lang('AddResult'),'class="save"');
  288. $template_submit = '<tr>
  289. <td colspan="4" ></td>
  290. <td >
  291. {element}
  292. <!-- BEGIN error --><br /><span style="color: #ff0000;font-size:10px">{error}</span><!-- END error -->
  293. </td>
  294. </tr>';
  295. $renderer->setElementTemplate($template_submit, 'submit');
  296. }
  297. /**
  298. * Builds a form to edit a result
  299. */
  300. protected function build_result_edit_form() {
  301. $this->setDefaults(array (
  302. 'score' => $this->result_object->get_score(),
  303. 'maximum' => $this->evaluation_object->get_max()
  304. ));
  305. $userinfo= api_get_user_info($this->result_object->get_user_id());
  306. $renderer =& $this->defaultRenderer();
  307. $renderer->setElementTemplate('<span>{element}</span> ');
  308. $this->addElement('label', get_lang('User'), $userinfo['complete_name']);
  309. $this->add_textfield('score', array(get_lang('Score'), null, '/ '.$this->evaluation_object->get_max()), false, array (
  310. 'size' => '4',
  311. 'class' => 'span1',
  312. 'maxlength' => '5'
  313. ));
  314. /* $this->add_textfield('maximum', null, false, array (
  315. 'size' => '4',
  316. 'maxlength' => '5',
  317. 'disabled' => 'disabled'
  318. ));*/
  319. $this->addElement('style_submit_button', 'submit', get_lang('Edit'),'class="save"');
  320. $this->addElement('hidden', 'minvalue', 0);
  321. $this->addElement('hidden', 'hid_user_id', $this->result_object->get_user_id());
  322. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  323. $this->addRule('score', get_lang('OnlyNumbers'), 'numeric',null,'client');
  324. $this->addRule(array (
  325. 'score',
  326. 'maxvalue'
  327. ), get_lang('OverMax'), 'compare', '<=','client');
  328. $this->addRule(array (
  329. 'score',
  330. 'minvalue'
  331. ), get_lang('UnderMin'), 'compare', '>=','client');
  332. }
  333. /**
  334. * Builds a form to add an evaluation
  335. */
  336. protected function build_add_form() {
  337. $this->setDefaults(array ( 'hid_user_id' => $this->evaluation_object->get_user_id(),
  338. 'hid_category_id' => $this->evaluation_object->get_category_id(),
  339. 'hid_course_code' => $this->evaluation_object->get_course_code(), 'created_at' => api_get_utc_datetime()));
  340. $this->build_basic_form(0);
  341. if ($this->evaluation_object->get_course_code() == null) {
  342. $this->addElement('checkbox', 'adduser', null, get_lang('AddUserToEval'));
  343. } else {
  344. $this->addElement('checkbox', 'addresult', null, get_lang('AddResult'));
  345. }
  346. $this->addElement('style_submit_button', 'submit', get_lang('AddAssessment'),'class="add"');
  347. }
  348. /**
  349. * Builds a form to edit an evaluation
  350. */
  351. protected function build_editing_form() {
  352. $parent_cat = Category :: load($this->evaluation_object->get_category_id());
  353. if ($parent_cat[0]->get_parent_id() == 0) {
  354. $weight_mask = $this->evaluation_object->get_weight();
  355. } else {
  356. $cat = Category :: load($parent_cat[0]->get_parent_id());
  357. $global_weight = $cat[0]->get_weight();
  358. $weight_mask = $global_weight*$this->evaluation_object->get_weight()/$parent_cat[0]->get_weight() ;
  359. }
  360. $this->setDefaults(array ( 'hid_id' => $this->evaluation_object->get_id(),
  361. 'name' => $this->evaluation_object->get_name(),
  362. 'description' => $this->evaluation_object->get_description(),
  363. 'hid_user_id' => $this->evaluation_object->get_user_id(),
  364. 'hid_course_code' => $this->evaluation_object->get_course_code(),
  365. 'hid_category_id' => $this->evaluation_object->get_category_id(),
  366. 'created_at' => api_get_utc_datetime($this->evaluation_object->get_date()),
  367. 'weight' => $weight,
  368. 'weight_mask' => $weight_mask,
  369. 'max' => $this->evaluation_object->get_max(),
  370. 'visible' => $this->evaluation_object->is_visible()));
  371. $id_current=isset($this->id)?$this->id :null;
  372. $this->addElement('hidden', 'hid_id', $id_current);
  373. $this->build_basic_form(1);
  374. $this->addElement('style_submit_button', 'submit', get_lang('ModifyEvaluation'),'class="save"');
  375. }
  376. /**
  377. * Builds a basic form that is used in add and edit
  378. */
  379. private function build_basic_form($edit= 0) {
  380. $form_title = get_lang('NewEvaluation');
  381. if ($_GET['editeval']==1) {
  382. $form_title = get_lang('EditEvaluation');
  383. }
  384. $this->addElement('header', $form_title);
  385. $this->addElement('hidden', 'zero', 0);
  386. $this->addElement('hidden', 'hid_user_id');
  387. $this->addElement('hidden', 'hid_course_code');
  388. $this->add_textfield('name', get_lang('EvaluationName'), true, array (
  389. 'class' => 'span3',
  390. 'maxlength' => '50',
  391. 'id' => 'evaluation_title'
  392. ));
  393. $cat_id = $this->evaluation_object->get_category_id();
  394. $session_id = api_get_session_id();
  395. $course_code = api_get_course_id();
  396. $all_categories = Category :: load(null, null, $course_code, null, null, $session_id, false);
  397. if (count($all_categories) == 1) {
  398. $this->addElement('hidden', 'hid_category_id', $cat_id);
  399. } else {
  400. $select_gradebook = $this->addElement('select', 'hid_category_id', get_lang('SelectGradebook'), array(), array('id' => 'hid_category_id'));
  401. $this->addRule('hid_category_id', get_lang('ThisFieldIsRequired'), 'nonzero');
  402. $default_weight = 0;
  403. if (!empty($all_categories)) {
  404. foreach ($all_categories as $my_cat) {
  405. if ($my_cat->get_course_code() == api_get_course_id()) {
  406. $grade_model_id = $my_cat->get_grade_model_id();
  407. if (empty($grade_model_id)) {
  408. if ($my_cat->get_parent_id() == 0) {
  409. $default_weight = $my_cat->get_weight();
  410. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  411. $cats_added[] = $my_cat->get_id();
  412. } else {
  413. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  414. $cats_added[] = $my_cat->get_id();
  415. }
  416. } else {
  417. $select_gradebook->addoption(get_lang('Select'), 0);
  418. }
  419. if ($this->evaluation_object->get_category_id() == $my_cat->get_id()) {
  420. $default_weight = $my_cat->get_weight();
  421. }
  422. }
  423. }
  424. }
  425. }
  426. $this->add_textfield('weight_mask', array(get_lang('Weight'), null, ' [0 .. '.$all_categories[0]->get_weight().'] '), true, array (
  427. 'size' => '4',
  428. 'maxlength' => '5',
  429. 'class' => 'span1'
  430. ));
  431. /*$this->add_textfield('weight', array(null, null, '/ <span id="max_weight">'.$default_weight.'</span>'), true, array (
  432. 'size' => '4',
  433. 'maxlength' => '5',
  434. 'class' => 'span1'
  435. ));*/
  436. if ($edit) {
  437. if (!$this->evaluation_object->has_results()) {
  438. $this->add_textfield('max', get_lang('QualificationNumeric'), true, array (
  439. 'class' => 'span1',
  440. 'maxlength' => '5'
  441. ));
  442. } else {
  443. $this->add_textfield('max', array(get_lang('QualificationNumeric'), get_lang('CannotChangeTheMaxNote')), false, array (
  444. 'class' => 'span1',
  445. 'maxlength' => '5',
  446. 'disabled' => 'disabled'
  447. ));
  448. }
  449. } else {
  450. $this->add_textfield('max', get_lang('QualificationNumeric'), true, array (
  451. 'class' => 'span1',
  452. 'maxlength' => '5'
  453. ));
  454. }
  455. $this->addElement('textarea', 'description', get_lang('Description'), array (
  456. 'rows' => '3',
  457. 'class' => 'span3',
  458. ));
  459. //var_dump(HTML_QuickForm::getRegisteredRules() );
  460. $this->addRule('hid_category_id', get_lang('ThisFieldIsRequired'), 'required');
  461. $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
  462. $this->addRule('weight_mask', get_lang('OnlyNumbers'), 'numeric');
  463. $this->addRule(array('weight_mask', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  464. $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
  465. $this->addRule(array('max', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  466. }
  467. function display() {
  468. parent :: display();
  469. }
  470. function setDefaults($defaults= array ()) {
  471. parent :: setDefaults($defaults);
  472. }
  473. private function build_stud_label ($id, $username,$lastname, $firstname) {
  474. $opendocurl_start = '';
  475. $opendocurl_end = '';
  476. // evaluation's origin is a link
  477. if ($this->evaluation_object->get_category_id() < 0) {
  478. $link = LinkFactory :: get_evaluation_link ($this->evaluation_object->get_id());
  479. $doc_url = $link->get_view_url($id);
  480. if ($doc_url != null) {
  481. $opendocurl_start .= '<a href="'. $doc_url . '" target="_blank">';
  482. $opendocurl_end = '</a>';
  483. }
  484. }
  485. return $opendocurl_start . api_get_person_name($firstname, $lastname) .' ('.$username.')'. $opendocurl_end;
  486. }
  487. function sort_by_user ($item1, $item2) {
  488. $user1 = $item1['user'];
  489. $user2 = $item2['user'];
  490. if (api_sort_by_first_name()) {
  491. $result = api_strcmp($user1['firstname'], $user2['firstname']);
  492. if ($result == 0) {
  493. return api_strcmp($user1['lastname'], $user2['lastname']);
  494. }
  495. } else {
  496. $result = api_strcmp($user1['lastname'], $user2['lastname']);
  497. if ($result == 0) {
  498. return api_strcmp($user1['firstname'], $user2['firstname']);
  499. }
  500. }
  501. return $result;
  502. }
  503. }