evalform.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos Latinoamerica SAC
  6. Copyright (c) 2006 Dokeos SPRL
  7. Copyright (c) 2006 Ghent University (UGent)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. require_once (dirname(__FILE__).'/../../../inc/global.inc.php');
  21. require_once (dirname(__FILE__).'/../be.inc.php');
  22. require_once (dirname(__FILE__).'/../gradebook_functions.inc.php');
  23. require_once (api_get_path(LIBRARY_PATH) . 'groupmanager.lib.php');
  24. require_once (api_get_path(LIBRARY_PATH) . 'formvalidator/FormValidator.class.php');
  25. /**
  26. * Extends formvalidator with add&edit forms for evaluations
  27. * @author Stijn Konings
  28. * @package dokeos.gradebook
  29. */
  30. class EvalForm extends FormValidator
  31. {
  32. const TYPE_ADD= 1;
  33. const TYPE_EDIT= 2;
  34. const TYPE_MOVE= 3;
  35. const TYPE_RESULT_ADD= 4;
  36. const TYPE_RESULT_EDIT= 5;
  37. const TYPE_ALL_RESULTS_EDIT= 6;
  38. const TYPE_ADD_USERS_TO_EVAL= 7;
  39. private $evaluation_object;
  40. private $result_object;
  41. private $extra;
  42. /**
  43. * Builds a form containing form items based on a given parameter
  44. * @param int form_type 1=add, 2=edit,3=move,4=result_add
  45. * @param obj cat_obj the category object
  46. * @param obj res_obj the result object
  47. * @param string form name
  48. * @param method
  49. * @param action
  50. */
  51. function EvalForm($form_type, $evaluation_object, $result_object, $form_name, $method= 'post', $action= null, $extra1 = null, $extra2 = null)
  52. {
  53. parent :: __construct($form_name, $method, $action);
  54. if (isset ($evaluation_object)) {
  55. $this->evaluation_object= $evaluation_object;
  56. }
  57. if (isset ($result_object)) {
  58. $this->result_object= $result_object;
  59. }
  60. if (isset ($extra1)) {
  61. $this->extra = $extra1;
  62. }
  63. if ($form_type == self :: TYPE_EDIT) {
  64. $this->build_editing_form();
  65. } elseif ($form_type == self :: TYPE_ADD) {
  66. $this->build_add_form();
  67. } elseif ($form_type == self :: TYPE_MOVE) {
  68. $this->build_move_form();
  69. } elseif ($form_type == self :: TYPE_RESULT_ADD) {
  70. $this->build_result_add_form();
  71. } elseif ($form_type == self :: TYPE_RESULT_EDIT) {
  72. $this->build_result_edit_form();
  73. } elseif ($form_type == self :: TYPE_ALL_RESULTS_EDIT) {
  74. $this->build_all_results_edit_form();
  75. } elseif ($form_type == self :: TYPE_ADD_USERS_TO_EVAL) {
  76. $this->build_add_user_to_eval();
  77. }
  78. $this->setDefaults();
  79. }
  80. /**
  81. * This form will build a form to add users to an evaluation
  82. */
  83. protected function build_add_user_to_eval() {
  84. //$this->addElement('hidden', 'formSent');
  85. $this->addElement('header','label',get_lang('ChooseUser'));
  86. $select= $this->addElement('select', 'firstLetterUser', get_lang('FirstLetter'), null, array(
  87. 'onchange'=> 'document.add_users_to_evaluation.submit()'
  88. ));
  89. $result = '';
  90. $select->addOption('','');
  91. for ($i = 65; $i <= 90; $i ++) {
  92. $letter = chr($i);
  93. if (isset($this->extra) && $this->extra == $letter) {
  94. $select->addOption($letter,$letter,'selected');
  95. } else {
  96. $select->addOption($letter,$letter);
  97. }
  98. }
  99. $select= $this->addElement('select', 'add_users', null, null, array (
  100. 'multiple' => 'multiple',
  101. 'size' => '15',
  102. 'style' => 'width:250px'
  103. ));
  104. foreach ($this->evaluation_object->get_not_subscribed_students() as $user) {
  105. if ( (!isset($this->extra)) || empty($this->extra) || api_strtoupper(api_substr($user[1],0,1)) == $this->extra ) {
  106. $select->addoption($user[1] . ' ' . $user[2] . ' (' . $user[3] . ')', $user[0]);
  107. }
  108. }
  109. $this->addElement('submit', 'submit_button', get_lang('AddUserToEval'));
  110. // $this->setDefaults(array (
  111. // 'formSent' => '1'
  112. // ));
  113. }
  114. /**
  115. * This function builds a form to edit all results in an evaluation
  116. */
  117. protected function build_all_results_edit_form() {
  118. //extra field for check on maxvalue
  119. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  120. $this->addElement('hidden', 'minvalue', 0);
  121. $this->addElement('header','h1','<b>'.get_lang('EditResult').'</b>');
  122. $renderer = $this->defaultRenderer();
  123. $elementTemplateTwoLabel = '<div class="row">
  124. <div class="label">
  125. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  126. </div>
  127. <div class="formw">
  128. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element} / '.$this->evaluation_object->get_max().'
  129. </div>
  130. </div>';
  131. $results_and_users = array();
  132. foreach ($this->result_object as $result) {
  133. $user= get_user_info_from_id($result->get_user_id());
  134. $results_and_users[] = array ('result' => $result, 'user' => $user);
  135. }
  136. usort($results_and_users, array ('EvalForm', 'sort_by_user'));
  137. $defaults= array ();
  138. foreach ($results_and_users as $result_and_user) {
  139. $user = $result_and_user['user'];
  140. $result = $result_and_user['result'];
  141. $renderer =& $this->defaultRenderer();
  142. $this->add_textfield('score[' . $result->get_id() . ']',
  143. $this->build_stud_label($user['user_id'], $user['lastname'], $user['firstname']),
  144. false,
  145. array ('size' => 4,
  146. 'maxlength' => 4));
  147. $this->addRule('score[' . $result->get_id() . ']', get_lang('OnlyNumbers'), 'numeric');
  148. $this->addRule(array (
  149. 'score[' . $result->get_id() . ']', 'maxvalue'), get_lang('OverMax'), 'compare', '<=');
  150. $this->addRule(array (
  151. 'score[' . $result->get_id() . ']', 'minvalue'), get_lang('UnderMin'), 'compare', '>=');
  152. $defaults['score[' . $result->get_id() . ']']= $result->get_score();
  153. $renderer->setElementTemplate($elementTemplateTwoLabel,'score[' . $result->get_id() . ']');
  154. }
  155. $this->setDefaults($defaults);
  156. $this->addElement('style_submit_button', 'submit',get_lang('Ok'),'class="save"');
  157. }
  158. /**
  159. * This function builds a form to move an item to another category
  160. *
  161. */
  162. protected function build_move_form() {
  163. $renderer =& $this->defaultRenderer();
  164. $renderer->setElementTemplate('<span>{element}</span> ');
  165. $this->addElement('static', null, null, '"'.$this->evaluation_object->get_name().'" ');
  166. $this->addElement('static', null, null, get_lang('MoveTo').' : ');
  167. $select= $this->addElement('select', 'move_cat', null, null);
  168. foreach ($this->evaluation_object->get_target_categories() as $cat) {
  169. for ($i= 0; $i < $cat[2]; $i++) {
  170. $line .= '&mdash;';
  171. }
  172. $select->addoption($line . ' ' . $cat[1], $cat[0]);
  173. $line= '';
  174. }
  175. $this->addElement('style_submit_button' , 'submit', get_lang('Ok'),'class="save"');
  176. }
  177. /**
  178. * Builds a result form containing inputs for all students with a given course_code
  179. */
  180. protected function build_result_add_form() {
  181. $tblusers= get_users_in_course($this->evaluation_object->get_course_code());
  182. $nr_users= 0;
  183. //extra field for check on maxvalue
  184. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  185. $this->addElement('hidden', 'minvalue', 0);
  186. $this->addElement('header','h1','<b>'.get_lang('AddResult').'</b>');
  187. $renderer = $this->defaultRenderer();
  188. $elementTemplateTwoLabel = '<div class="row">
  189. <div class="label">
  190. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  191. </div>
  192. <div class="formw">
  193. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element} / '.$this->evaluation_object->get_max().'
  194. </div>
  195. </div>';
  196. foreach ($tblusers as $user) {
  197. $this->add_textfield('score[' . $user[0] . ']',
  198. $this->build_stud_label($user[0], $user[1], $user[2]),
  199. false,
  200. array ('size' => 4,
  201. 'maxlength' => 4));
  202. $this->addRule('score[' . $user[0] . ']', get_lang('OnlyNumbers'), 'numeric');
  203. $this->addRule(array (
  204. 'score[' . $user[0] . ']',
  205. 'maxvalue'
  206. ), get_lang('OverMax'), 'compare', '<=');
  207. $this->addRule(array (
  208. 'score[' . $user[0] . ']',
  209. 'minvalue'
  210. ), get_lang('UnderMin'), 'compare', '>=');
  211. $renderer->setElementTemplate($elementTemplateTwoLabel,'score[' . $user[0] . ']');
  212. $nr_users++;
  213. }
  214. $this->addElement('hidden', 'nr_users', $nr_users);
  215. $this->addElement('hidden', 'evaluation_id', $this->result_object->get_evaluation_id());
  216. $this->addElement('style_submit_button', 'submit', get_lang('Ok'),'class="save"');
  217. }
  218. /**
  219. * Builds a form to edit a result
  220. */
  221. protected function build_result_edit_form() {
  222. $this->setDefaults(array (
  223. 'score' => $this->result_object->get_score(),
  224. 'maximum' => $this->evaluation_object->get_max()
  225. ));
  226. $userinfo= api_get_user_info($this->result_object->get_user_id());
  227. $renderer =& $this->defaultRenderer();
  228. $renderer->setElementTemplate('<span>{element}</span> ');
  229. $this->addElement('static', null, null,$userinfo['lastName'] . ' ' . $userinfo['firstName']);
  230. $this->add_textfield('score', get_lang('Result'), false, array (
  231. 'size' => '4',
  232. 'maxlength' => '4'
  233. ));
  234. $this->addElement('static', null, null,'/');
  235. $this->add_textfield('maximum', null, false, array (
  236. 'size' => '4',
  237. 'maxlength' => '4',
  238. 'disabled' => 'disabled'
  239. ));
  240. $this->addElement('style_submit_button', 'submit', get_lang('Edit'),'class="save"');
  241. $this->addElement('hidden', 'minvalue', 0);
  242. $this->addElement('hidden', 'hid_user_id', $this->result_object->get_user_id());
  243. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  244. $this->addRule('score', get_lang('OnlyNumbers'), 'numeric',null,'client');
  245. $this->addRule(array (
  246. 'score',
  247. 'maxvalue'
  248. ), get_lang('OverMax'), 'compare', '<=','client');
  249. $this->addRule(array (
  250. 'score',
  251. 'minvalue'
  252. ), get_lang('UnderMin'), 'compare', '>=','client');
  253. }
  254. /**
  255. * Builds a form to add an evaluation
  256. */
  257. protected function build_add_form() {
  258. $this->setDefaults(array (
  259. 'hid_user_id' => $this->evaluation_object->get_user_id(), 'hid_category_id' => $this->evaluation_object->get_category_id(), 'hid_course_code' => $this->evaluation_object->get_course_code(), 'date' => time()));
  260. $this->build_basic_form(0);
  261. if ($this->evaluation_object->get_course_code() == null) {
  262. $this->addElement('checkbox', 'adduser', get_lang('AddUserToEval'));
  263. } else {
  264. $this->addElement('checkbox', 'addresult', get_lang('AddResult'));
  265. }
  266. $this->addElement('style_submit_button', 'submit', get_lang('AddAssessment'),'class="add"');
  267. }
  268. /**
  269. * Builds a form to edit an evaluation
  270. */
  271. protected function build_editing_form() {
  272. $this->setDefaults(array (
  273. 'hid_id' => $this->evaluation_object->get_id(), 'name' => $this->evaluation_object->get_name(), 'description' => $this->evaluation_object->get_description(), 'hid_user_id' => $this->evaluation_object->get_user_id(), 'hid_course_code' => $this->evaluation_object->get_course_code(), 'hid_category_id' => $this->evaluation_object->get_category_id(), 'date' => $this->evaluation_object->get_date(), 'weight' => $this->evaluation_object->get_weight(), 'max' => $this->evaluation_object->get_max(), 'visible' => $this->evaluation_object->is_visible()));
  274. $id_current=isset($this->id)?$this->id :null;
  275. $this->addElement('hidden', 'hid_id',$id_current);
  276. $this->build_basic_form(1);
  277. $this->addElement('style_submit_button', 'submit', get_lang('ModifyEvaluation'),'class="save"');
  278. }
  279. /**
  280. * Builds a basic form that is used in add and edit
  281. */
  282. private function build_basic_form($edit= 0) {
  283. $form_title = get_lang('NewEvaluation');
  284. if ($_GET['editeval']==1)
  285. {
  286. $form_title = get_lang('EditEvaluation');
  287. }
  288. $this->addElement('header', '', $form_title);
  289. $this->addElement('hidden', 'zero', 0);
  290. $this->addElement('hidden', 'hid_user_id');
  291. $this->addElement('hidden', 'hid_category_id');
  292. $this->addElement('hidden', 'hid_course_code');
  293. $this->add_textfield('name', get_lang('EvaluationName'), true, array (
  294. 'size' => '54',
  295. 'maxlength' => '50'
  296. ));
  297. $this->add_textfield('weight', get_lang('Weight'), true, array (
  298. 'size' => '4',
  299. 'maxlength' => '4'
  300. ));
  301. if ($edit) {
  302. if (!$this->evaluation_object->has_results()) {
  303. $this->add_textfield('max', get_lang('Max'), true, array (
  304. 'size' => '4',
  305. 'maxlength' => '4'
  306. ));
  307. } else {
  308. $this->add_textfield('max', get_lang('Max'), false, array (
  309. 'size' => '4',
  310. 'maxlength' => '4',
  311. 'disabled' => 'disabled'
  312. ));
  313. $this->addElement('static','label','','<small>'.get_lang('CannotChangeTheMaxNote').'</small>');
  314. }
  315. } else {
  316. $this->add_textfield('max', get_lang('Max'), true, array (
  317. 'size' => '4',
  318. 'maxlength' => '4'
  319. ));
  320. }
  321. /*$this->add_datepicker('date', get_lang('DateEval'));*/
  322. $this->addElement('textarea', 'description', get_lang('Description'), array (
  323. 'rows' => '3',
  324. 'cols' => '34'
  325. ));
  326. $this->addElement('checkbox', 'visible', get_lang('Visible'));
  327. $this->addRule('weight', get_lang('OnlyNumbers'), 'numeric');
  328. $this->addRule(array ('weight', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  329. $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
  330. $this->addRule(array ('max', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  331. }
  332. function display() {
  333. parent :: display();
  334. }
  335. function setDefaults($defaults= array ()) {
  336. parent :: setDefaults($defaults);
  337. }
  338. private function build_stud_label ($id, $lastname, $firstname) {
  339. $opendocurl_start = '';
  340. $opendocurl_end = '';
  341. // evaluation's origin is a link
  342. if ($this->evaluation_object->get_category_id() < 0) {
  343. $link = LinkFactory :: get_evaluation_link ($this->evaluation_object->get_id());
  344. $doc_url = $link->get_view_url($id);
  345. if ($doc_url != null) {
  346. $opendocurl_start .= '<a href="'. $doc_url . '" target="_blank">';
  347. $opendocurl_end = '</a>';
  348. }
  349. }
  350. return $opendocurl_start . $lastname . ' ' . $firstname . $opendocurl_end;
  351. }
  352. function sort_by_user ($item1, $item2) {
  353. $user1 = $item1['user'];
  354. $user2 = $item2['user'];
  355. $result = api_strcmp($user1['lastname'], $user2['lastname']);
  356. if ($result == 0) {
  357. return api_strcmp($user1['firstname'], $user2['firstname']);
  358. }
  359. return $result;
  360. }
  361. }