evalform.class.php 22 KB

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