evalform.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  31. $htmlHeadXtra[] = '<script type="text/javascript">
  32. function setFocus(){
  33. $("#title").focus();
  34. }
  35. $(window).load(function () {
  36. setFocus();
  37. });
  38. </script>';
  39. class EvalForm extends FormValidator
  40. {
  41. const TYPE_ADD= 1;
  42. const TYPE_EDIT= 2;
  43. const TYPE_MOVE= 3;
  44. const TYPE_RESULT_ADD= 4;
  45. const TYPE_RESULT_EDIT= 5;
  46. const TYPE_ALL_RESULTS_EDIT= 6;
  47. const TYPE_ADD_USERS_TO_EVAL= 7;
  48. private $evaluation_object;
  49. private $result_object;
  50. private $extra;
  51. /**
  52. * Builds a form containing form items based on a given parameter
  53. * @param int form_type 1=add, 2=edit,3=move,4=result_add
  54. * @param obj cat_obj the category object
  55. * @param obj res_obj the result object
  56. * @param string form name
  57. * @param method
  58. * @param action
  59. */
  60. function EvalForm($form_type, $evaluation_object, $result_object, $form_name, $method= 'post', $action= null, $extra1 = null, $extra2 = null)
  61. {
  62. parent :: __construct($form_name, $method, $action);
  63. if (isset ($evaluation_object)) {
  64. $this->evaluation_object= $evaluation_object;
  65. }
  66. if (isset ($result_object)) {
  67. $this->result_object= $result_object;
  68. }
  69. if (isset ($extra1)) {
  70. $this->extra = $extra1;
  71. }
  72. if ($form_type == self :: TYPE_EDIT) {
  73. $this->build_editing_form();
  74. } elseif ($form_type == self :: TYPE_ADD) {
  75. $this->build_add_form();
  76. } elseif ($form_type == self :: TYPE_MOVE) {
  77. $this->build_move_form();
  78. } elseif ($form_type == self :: TYPE_RESULT_ADD) {
  79. $this->build_result_add_form();
  80. } elseif ($form_type == self :: TYPE_RESULT_EDIT) {
  81. $this->build_result_edit_form();
  82. } elseif ($form_type == self :: TYPE_ALL_RESULTS_EDIT) {
  83. $this->build_all_results_edit_form();
  84. } elseif ($form_type == self :: TYPE_ADD_USERS_TO_EVAL) {
  85. $this->build_add_user_to_eval();
  86. }
  87. $this->setDefaults();
  88. }
  89. /**
  90. * This form will build a form to add users to an evaluation
  91. */
  92. protected function build_add_user_to_eval() {
  93. //$this->addElement('hidden', 'formSent');
  94. $this->addElement('header','label',get_lang('ChooseUser'));
  95. $select= $this->addElement('select', 'firstLetterUser', get_lang('FirstLetter'), null, array(
  96. 'onchange'=> 'document.add_users_to_evaluation.submit()'
  97. ));
  98. $result = '';
  99. $select->addOption('','');
  100. for ($i = 65; $i <= 90; $i ++) {
  101. $letter = chr($i);
  102. if (isset($this->extra) && $this->extra == $letter) {
  103. $select->addOption($letter,$letter,'selected');
  104. } else {
  105. $select->addOption($letter,$letter);
  106. }
  107. }
  108. $select= $this->addElement('select', 'add_users', null, null, array (
  109. 'multiple' => 'multiple',
  110. 'size' => '15',
  111. 'style' => 'width:250px'
  112. ));
  113. foreach ($this->evaluation_object->get_not_subscribed_students() as $user) {
  114. if ( (!isset($this->extra)) || empty($this->extra) || api_strtoupper(api_substr($user[1],0,1)) == $this->extra ) {
  115. $select->addoption($user[1] . ' ' . $user[2] . ' (' . $user[3] . ')', $user[0]);
  116. }
  117. }
  118. $this->addElement('submit', 'submit_button', get_lang('AddUserToEval'));
  119. // $this->setDefaults(array (
  120. // 'formSent' => '1'
  121. // ));
  122. }
  123. /**
  124. * This function builds a form to edit all results in an evaluation
  125. */
  126. protected function build_all_results_edit_form() {
  127. //extra field for check on maxvalue
  128. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  129. $this->addElement('hidden', 'minvalue', 0);
  130. $this->addElement('header','h1','<b>'.get_lang('EditResult').'</b>');
  131. $renderer = $this->defaultRenderer();
  132. $elementTemplateTwoLabel = '<div class="row">
  133. <div class="label">
  134. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  135. </div>
  136. <div class="formw">
  137. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element} / '.$this->evaluation_object->get_max().'
  138. </div>
  139. </div>';
  140. $results_and_users = array();
  141. foreach ($this->result_object as $result) {
  142. $user= get_user_info_from_id($result->get_user_id());
  143. $results_and_users[] = array ('result' => $result, 'user' => $user);
  144. }
  145. usort($results_and_users, array ('EvalForm', 'sort_by_user'));
  146. $defaults= array ();
  147. foreach ($results_and_users as $result_and_user) {
  148. $user = $result_and_user['user'];
  149. $result = $result_and_user['result'];
  150. $renderer =& $this->defaultRenderer();
  151. $this->add_textfield('score[' . $result->get_id() . ']',
  152. $this->build_stud_label($user['user_id'], $user['lastname'], $user['firstname']),
  153. false,
  154. array ('size' => 4,
  155. 'maxlength' => 4));
  156. $this->addRule('score[' . $result->get_id() . ']', get_lang('OnlyNumbers'), 'numeric');
  157. $this->addRule(array (
  158. 'score[' . $result->get_id() . ']', 'maxvalue'), get_lang('OverMax'), 'compare', '<=');
  159. $this->addRule(array (
  160. 'score[' . $result->get_id() . ']', 'minvalue'), get_lang('UnderMin'), 'compare', '>=');
  161. $defaults['score[' . $result->get_id() . ']']= $result->get_score();
  162. $renderer->setElementTemplate($elementTemplateTwoLabel,'score[' . $result->get_id() . ']');
  163. }
  164. $this->setDefaults($defaults);
  165. $this->addElement('style_submit_button', 'submit',get_lang('Ok'),'class="save"');
  166. }
  167. /**
  168. * This function builds a form to move an item to another category
  169. *
  170. */
  171. protected function build_move_form() {
  172. $renderer =& $this->defaultRenderer();
  173. $renderer->setElementTemplate('<span>{element}</span> ');
  174. $this->addElement('static', null, null, '"'.$this->evaluation_object->get_name().'" ');
  175. $this->addElement('static', null, null, get_lang('MoveTo').' : ');
  176. $select= $this->addElement('select', 'move_cat', null, null);
  177. foreach ($this->evaluation_object->get_target_categories() as $cat) {
  178. for ($i= 0; $i < $cat[2]; $i++) {
  179. $line .= '&mdash;';
  180. }
  181. $select->addoption($line . ' ' . $cat[1], $cat[0]);
  182. $line= '';
  183. }
  184. $this->addElement('style_submit_button' , 'submit', get_lang('Ok'),'class="save"');
  185. }
  186. /**
  187. * Builds a result form containing inputs for all students with a given course_code
  188. */
  189. protected function build_result_add_form() {
  190. $tblusers= get_users_in_course($this->evaluation_object->get_course_code());
  191. $nr_users= 0;
  192. //extra field for check on maxvalue
  193. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  194. $this->addElement('hidden', 'minvalue', 0);
  195. $this->addElement('header','h1','<b>'.get_lang('AddResult').'</b>');
  196. $renderer = $this->defaultRenderer();
  197. $elementTemplateTwoLabel = '<div class="row">
  198. <div class="label">
  199. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  200. </div>
  201. <div class="formw">
  202. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element} / '.$this->evaluation_object->get_max().'
  203. </div>
  204. </div>';
  205. foreach ($tblusers as $user) {
  206. $this->add_textfield('score[' . $user[0] . ']',
  207. $this->build_stud_label($user[0], $user[1], $user[2]),
  208. false,
  209. array ('size' => 4,
  210. 'maxlength' => 4));
  211. $this->addRule('score[' . $user[0] . ']', get_lang('OnlyNumbers'), 'numeric');
  212. $this->addRule(array (
  213. 'score[' . $user[0] . ']',
  214. 'maxvalue'
  215. ), get_lang('OverMax'), 'compare', '<=');
  216. $this->addRule(array (
  217. 'score[' . $user[0] . ']',
  218. 'minvalue'
  219. ), get_lang('UnderMin'), 'compare', '>=');
  220. $renderer->setElementTemplate($elementTemplateTwoLabel,'score[' . $user[0] . ']');
  221. $nr_users++;
  222. }
  223. $this->addElement('hidden', 'nr_users', $nr_users);
  224. $this->addElement('hidden', 'evaluation_id', $this->result_object->get_evaluation_id());
  225. $this->addElement('style_submit_button', 'submit', get_lang('Ok'),'class="save"');
  226. }
  227. /**
  228. * Builds a form to edit a result
  229. */
  230. protected function build_result_edit_form() {
  231. $this->setDefaults(array (
  232. 'score' => $this->result_object->get_score(),
  233. 'maximum' => $this->evaluation_object->get_max()
  234. ));
  235. $userinfo= api_get_user_info($this->result_object->get_user_id());
  236. $renderer =& $this->defaultRenderer();
  237. $renderer->setElementTemplate('<span>{element}</span> ');
  238. $this->addElement('static', null, null, api_get_person_name($userinfo['lastName'], $userinfo['firstName']));
  239. $this->add_textfield('score', get_lang('Result'), false, array (
  240. 'size' => '4',
  241. 'maxlength' => '4'
  242. ));
  243. $this->addElement('static', null, null,'/');
  244. $this->add_textfield('maximum', null, false, array (
  245. 'size' => '4',
  246. 'maxlength' => '4',
  247. 'disabled' => 'disabled'
  248. ));
  249. $this->addElement('style_submit_button', 'submit', get_lang('Edit'),'class="save"');
  250. $this->addElement('hidden', 'minvalue', 0);
  251. $this->addElement('hidden', 'hid_user_id', $this->result_object->get_user_id());
  252. $this->addElement('hidden', 'maxvalue', $this->evaluation_object->get_max());
  253. $this->addRule('score', get_lang('OnlyNumbers'), 'numeric',null,'client');
  254. $this->addRule(array (
  255. 'score',
  256. 'maxvalue'
  257. ), get_lang('OverMax'), 'compare', '<=','client');
  258. $this->addRule(array (
  259. 'score',
  260. 'minvalue'
  261. ), get_lang('UnderMin'), 'compare', '>=','client');
  262. }
  263. /**
  264. * Builds a form to add an evaluation
  265. */
  266. protected function build_add_form() {
  267. $this->setDefaults(array (
  268. '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()));
  269. $this->build_basic_form(0);
  270. if ($this->evaluation_object->get_course_code() == null) {
  271. $this->addElement('checkbox', 'adduser', get_lang('AddUserToEval'));
  272. } else {
  273. $this->addElement('checkbox', 'addresult', get_lang('AddResult'));
  274. }
  275. $this->addElement('style_submit_button', 'submit', get_lang('AddAssessment'),'class="add"');
  276. }
  277. /**
  278. * Builds a form to edit an evaluation
  279. */
  280. protected function build_editing_form() {
  281. $this->setDefaults(array (
  282. '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()));
  283. $id_current=isset($this->id)?$this->id :null;
  284. $this->addElement('hidden', 'hid_id',$id_current);
  285. $this->build_basic_form(1);
  286. $this->addElement('style_submit_button', 'submit', get_lang('ModifyEvaluation'),'class="save"');
  287. }
  288. /**
  289. * Builds a basic form that is used in add and edit
  290. */
  291. private function build_basic_form($edit= 0) {
  292. $form_title = get_lang('NewEvaluation');
  293. if ($_GET['editeval']==1)
  294. {
  295. $form_title = get_lang('EditEvaluation');
  296. }
  297. $this->addElement('header', '', $form_title);
  298. $this->addElement('hidden', 'zero', 0);
  299. $this->addElement('hidden', 'hid_user_id');
  300. $this->addElement('hidden', 'hid_category_id');
  301. $this->addElement('hidden', 'hid_course_code');
  302. $this->add_textfield('name', get_lang('EvaluationName'), true, array (
  303. 'size' => '54',
  304. 'maxlength' => '50',
  305. 'id' => 'title'
  306. ));
  307. $this->add_textfield('weight', get_lang('Weight'), true, array (
  308. 'size' => '4',
  309. 'maxlength' => '4'
  310. ));
  311. if ($edit) {
  312. if (!$this->evaluation_object->has_results()) {
  313. $this->add_textfield('max', get_lang('Max'), true, array (
  314. 'size' => '4',
  315. 'maxlength' => '4'
  316. ));
  317. } else {
  318. $this->add_textfield('max', get_lang('Max'), false, array (
  319. 'size' => '4',
  320. 'maxlength' => '4',
  321. 'disabled' => 'disabled'
  322. ));
  323. $this->addElement('static','label','','<small>'.get_lang('CannotChangeTheMaxNote').'</small>');
  324. }
  325. } else {
  326. $this->add_textfield('max', get_lang('Max'), true, array (
  327. 'size' => '4',
  328. 'maxlength' => '4'
  329. ));
  330. }
  331. /*$this->add_datepicker('date', get_lang('DateEval'));*/
  332. $this->addElement('textarea', 'description', get_lang('Description'), array (
  333. 'rows' => '3',
  334. 'cols' => '34'
  335. ));
  336. $this->addElement('checkbox', 'visible', get_lang('Visible'));
  337. $this->addRule('weight', get_lang('OnlyNumbers'), 'numeric');
  338. $this->addRule(array ('weight', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  339. $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
  340. $this->addRule(array ('max', 'zero'), get_lang('NegativeValue'), 'compare', '>=');
  341. }
  342. function display() {
  343. parent :: display();
  344. }
  345. function setDefaults($defaults= array ()) {
  346. parent :: setDefaults($defaults);
  347. }
  348. private function build_stud_label ($id, $lastname, $firstname) {
  349. $opendocurl_start = '';
  350. $opendocurl_end = '';
  351. // evaluation's origin is a link
  352. if ($this->evaluation_object->get_category_id() < 0) {
  353. $link = LinkFactory :: get_evaluation_link ($this->evaluation_object->get_id());
  354. $doc_url = $link->get_view_url($id);
  355. if ($doc_url != null) {
  356. $opendocurl_start .= '<a href="'. $doc_url . '" target="_blank">';
  357. $opendocurl_end = '</a>';
  358. }
  359. }
  360. return $opendocurl_start . api_get_person_name($firstname, $lastname) . $opendocurl_end;
  361. }
  362. function sort_by_user ($item1, $item2) {
  363. $user1 = $item1['user'];
  364. $user2 = $item2['user'];
  365. if (api_sort_by_first_name()) {
  366. $result = api_strcmp($user1['firstname'], $user2['firstname']);
  367. if ($result == 0) {
  368. return api_strcmp($user1['lastname'], $user2['lastname']);
  369. }
  370. } else {
  371. $result = api_strcmp($user1['lastname'], $user2['lastname']);
  372. if ($result == 0) {
  373. return api_strcmp($user1['firstname'], $user2['firstname']);
  374. }
  375. }
  376. return $result;
  377. }
  378. }