evaluation.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Evaluation
  5. * @package chamilo.gradebook
  6. */
  7. class Evaluation implements GradebookItem
  8. {
  9. private $id;
  10. private $name;
  11. private $description;
  12. private $user_id;
  13. private $course_code;
  14. private $category;
  15. private $created_at;
  16. private $weight;
  17. private $eval_max;
  18. private $visible;
  19. private $evaluation_type_id;
  20. public function __construct()
  21. {
  22. }
  23. public function get_id()
  24. {
  25. return $this->id;
  26. }
  27. public function get_name()
  28. {
  29. return $this->name;
  30. }
  31. public function get_description()
  32. {
  33. return $this->description;
  34. }
  35. public function get_user_id()
  36. {
  37. return $this->user_id;
  38. }
  39. public function get_course_code()
  40. {
  41. return $this->course_code;
  42. }
  43. public function get_category_id()
  44. {
  45. return $this->category;
  46. }
  47. public function get_date()
  48. {
  49. return $this->created_at;
  50. }
  51. public function get_weight()
  52. {
  53. return $this->weight;
  54. }
  55. public function get_max()
  56. {
  57. return $this->eval_max;
  58. }
  59. public function get_type()
  60. {
  61. return $this->type;
  62. }
  63. public function is_visible()
  64. {
  65. return $this->visible;
  66. }
  67. public function get_locked()
  68. {
  69. return $this->locked;
  70. }
  71. public function is_locked()
  72. {
  73. return isset($this->locked) && $this->locked == 1 ? true : false ;
  74. }
  75. public function set_id($id)
  76. {
  77. $this->id = $id;
  78. }
  79. public function set_name($name)
  80. {
  81. $this->name = $name;
  82. }
  83. public function set_description($description)
  84. {
  85. $this->description = $description;
  86. }
  87. public function set_user_id($user_id)
  88. {
  89. $this->user_id = $user_id;
  90. }
  91. public function set_course_code($course_code)
  92. {
  93. $this->course_code = $course_code;
  94. }
  95. public function set_category_id($category_id)
  96. {
  97. $this->category = $category_id;
  98. }
  99. public function set_date($date)
  100. {
  101. $this->created_at = $date;
  102. }
  103. public function set_weight($weight)
  104. {
  105. $this->weight = $weight;
  106. }
  107. public function set_max($max)
  108. {
  109. $this->eval_max = $max;
  110. }
  111. public function set_visible($visible)
  112. {
  113. $this->visible = $visible;
  114. }
  115. public function set_type($type)
  116. {
  117. $this->type = $type;
  118. }
  119. public function set_locked($locked)
  120. {
  121. $this->locked = $locked;
  122. }
  123. public function get_evaluation_type_id() {
  124. return isset($this->evaluation_type_id) ? $this->evaluation_type_id : 0;
  125. }
  126. public function set_evaluation_type_id($id)
  127. {
  128. $this->evaluation_type_id = intval($id);
  129. }
  130. /**
  131. * Retrieve evaluations and return them as an array of Evaluation objects
  132. * @param int $id evaluation id
  133. * @param int $user_id user id (evaluation owner)
  134. * @param string $course_code course code
  135. * @param int $category_id parent category
  136. * @param $visible visible
  137. */
  138. public static function load(
  139. $id = null,
  140. $user_id = null,
  141. $course_code = null,
  142. $category_id = null,
  143. $visible = null,
  144. $locked = null
  145. ) {
  146. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  147. $sql = 'SELECT * FROM '.$tbl_grade_evaluations;
  148. $paramcount = 0;
  149. if (isset ($id)) {
  150. $sql.= ' WHERE id = '.intval($id);
  151. $paramcount ++;
  152. }
  153. if (isset ($user_id)) {
  154. if ($paramcount != 0) $sql .= ' AND';
  155. else $sql .= ' WHERE';
  156. $sql .= ' user_id = '.intval($user_id);
  157. $paramcount ++;
  158. }
  159. if (isset ($course_code) && $course_code <> '-1') {
  160. if ($paramcount != 0) $sql .= ' AND';
  161. else $sql .= ' WHERE';
  162. $sql .= " course_code = '".Database::escape_string($course_code)."'";
  163. $paramcount ++;
  164. }
  165. if (isset ($category_id)) {
  166. if ($paramcount != 0) $sql .= ' AND';
  167. else $sql .= ' WHERE';
  168. $sql .= ' category_id = '.intval($category_id);
  169. $paramcount ++;
  170. }
  171. if (isset ($visible)) {
  172. if ($paramcount != 0) $sql .= ' AND';
  173. else $sql .= ' WHERE';
  174. $sql .= ' visible = '.intval($visible);
  175. $paramcount ++;
  176. }
  177. if (isset ($locked)) {
  178. if ($paramcount != 0) $sql .= ' AND';
  179. else $sql .= ' WHERE';
  180. $sql .= ' locked = '.intval($locked);
  181. $paramcount ++;
  182. }
  183. if (isset($name)) {
  184. if ($paramcount != 0) $sql .= ' AND';
  185. else $sql .= ' WHERE';
  186. $sql .= " name = '".Database::escape_string($name)."'";
  187. $paramcount ++;
  188. }
  189. $result = Database::query($sql);
  190. $alleval = Evaluation::create_evaluation_objects_from_sql_result($result);
  191. return $alleval;
  192. }
  193. private static function create_evaluation_objects_from_sql_result($result)
  194. {
  195. $alleval=array();
  196. if (Database::num_rows($result)) {
  197. while ($data = Database::fetch_array($result)) {
  198. $eval= new Evaluation();
  199. $eval->set_id($data['id']);
  200. $eval->set_name($data['name']);
  201. $eval->set_description($data['description']);
  202. $eval->set_user_id($data['user_id']);
  203. $eval->set_course_code($data['course_code']);
  204. $eval->set_category_id($data['category_id']);
  205. $eval->set_date(api_get_local_time($data['created_at']));
  206. $eval->set_weight($data['weight']);
  207. $eval->set_max($data['max']);
  208. $eval->set_visible($data['visible']);
  209. $eval->set_type($data['type']);
  210. $eval->set_locked($data['locked']);
  211. $alleval[]=$eval;
  212. }
  213. }
  214. return $alleval;
  215. }
  216. /**
  217. * Insert this evaluation into the database
  218. */
  219. public function add()
  220. {
  221. if (isset($this->name) && isset($this->user_id) && isset($this->weight) && isset ($this->eval_max) && isset($this->visible)) {
  222. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  223. $sql = 'INSERT INTO '.$tbl_grade_evaluations
  224. .' (name, user_id, weight, max, visible';
  225. if (isset($this->description)) {
  226. $sql .= ',description';
  227. }
  228. if (isset($this->course_code)) {
  229. $sql .= ', course_code';
  230. }
  231. if (isset($this->category)) {
  232. $sql .= ', category_id';
  233. }
  234. $sql .= ', created_at';
  235. $sql .= ', evaluation_type_id';
  236. $sql .= ',type';
  237. $sql .= ") VALUES ('".Database::escape_string($this->get_name())."'"
  238. .','.intval($this->get_user_id())
  239. .','.floatval($this->get_weight())
  240. .','.intval($this->get_max())
  241. .','.intval($this->is_visible());
  242. if (isset($this->description)) {
  243. $sql .= ",'".Database::escape_string($this->get_description())."'";
  244. }
  245. if (isset($this->course_code)) {
  246. $sql .= ",'".Database::escape_string($this->get_course_code())."'";
  247. }
  248. if (isset($this->category)) {
  249. $sql .= ','.intval($this->get_category_id());
  250. }
  251. $sql .= ','.intval($this->get_evaluation_type_id());
  252. if (empty($this->type)) {
  253. $this->type = 'evaluation';
  254. }
  255. $sql .= ", '".api_get_utc_datetime()."'";
  256. $sql .= ',\''.Database::escape_string($this->type).'\'';
  257. $sql .= ")";
  258. Database::query($sql);
  259. $this->set_id(Database::insert_id());
  260. } else {
  261. die('Error in Evaluation add: required field empty');
  262. }
  263. }
  264. public function add_evaluation_log($idevaluation)
  265. {
  266. if (!empty($idevaluation)) {
  267. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  268. $tbl_grade_linkeval_log = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG);
  269. $eval=new Evaluation();
  270. $dateobject=$eval->load ($idevaluation,null,null,null,null);
  271. $arreval=get_object_vars($dateobject[0]);
  272. if (!empty($arreval['id'])) {
  273. $sql_eval='SELECT weight from '.$tbl_grade_evaluations.' WHERE id='.$arreval['id'];
  274. $rs=Database::query($sql_eval);
  275. $row_old_weight=Database::fetch_array($rs,'ASSOC');
  276. $current_date=api_get_utc_datetime();
  277. $sql="INSERT INTO ".$tbl_grade_linkeval_log."(id_linkeval_log,name,description,created_at,weight,visible,type,user_id_log)
  278. VALUES('".Database::escape_string($arreval['id'])."','".Database::escape_string($arreval['name'])."','".Database::escape_string($arreval['description'])."','".$current_date."','".Database::escape_string($row_old_weight['weight'])."','".Database::escape_string($arreval['visible'])."','evaluation',".api_get_user_id().")";
  279. Database::query($sql);
  280. }
  281. }
  282. }
  283. /**
  284. * Update the properties of this evaluation in the database
  285. */
  286. public function save()
  287. {
  288. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  289. $sql = 'UPDATE '.$tbl_grade_evaluations
  290. ." SET name = '".Database::escape_string($this->get_name())."'"
  291. .', description = ';
  292. if (isset($this->description)) {
  293. $sql .= "'".Database::escape_string($this->get_description())."'";
  294. }else {
  295. $sql .= 'null';
  296. }
  297. $sql .= ', user_id = '.intval($this->get_user_id())
  298. .', course_code = ';
  299. if (isset($this->course_code)) {
  300. $sql .= "'".Database::escape_string($this->get_course_code())."'";
  301. } else {
  302. $sql .= 'null';
  303. }
  304. $sql .= ', category_id = ';
  305. if (isset($this->category)) {
  306. $sql .= intval($this->get_category_id());
  307. } else {
  308. $sql .= 'null';
  309. }
  310. $sql .= ', weight = "'.Database::escape_string($this->get_weight()).'" '
  311. .', max = '.Database::escape_string($this->get_max())
  312. .', visible = '.intval($this->is_visible())
  313. .' WHERE id = '.intval($this->id);
  314. //recorded history
  315. $eval_log=new Evaluation();
  316. $eval_log->add_evaluation_log($this->id);
  317. Database::query($sql);
  318. }
  319. /**
  320. * Delete this evaluation from the database
  321. */
  322. public function delete()
  323. {
  324. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  325. $sql = 'DELETE FROM '.$tbl_grade_evaluations.' WHERE id = '.intval($this->id);
  326. Database::query($sql);
  327. }
  328. /**
  329. * Check if an evaluation name (with the same parent category) already exists
  330. * @param $name name to check (if not given, the name property of this object will be checked)
  331. * @param $parent parent category
  332. */
  333. public function does_name_exist($name, $parent)
  334. {
  335. if (!isset ($name)) {
  336. $name = $this->name;
  337. $parent = $this->category;
  338. }
  339. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  340. $sql = 'SELECT count(id) AS number'
  341. .' FROM '.$tbl_grade_evaluations
  342. ." WHERE name = '".Database::escape_string($name)."'";
  343. if (api_is_allowed_to_edit()) {
  344. $parent = Category::load($parent);
  345. $courseId = $parent[0]->get_course_id();
  346. if (isset($courseId) && $courseId != '0') {
  347. $main_course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  348. $sql .= " AND user_id IN (
  349. SELECT user_id FROM $main_course_user_table
  350. WHERE c_id = ".Database::escape_string($courseId)." AND status = ".COURSEMANAGER
  351. .')';
  352. } else {
  353. $sql .= ' AND user_id = '.api_get_user_id();
  354. }
  355. }else {
  356. $sql .= ' AND user_id = '.api_get_user_id();
  357. }
  358. if (!isset ($parent)) {
  359. $sql.= ' AND category_id is null';
  360. } else {
  361. $sql.= ' AND category_id = '.intval($parent);
  362. }
  363. $result = Database::query($sql);
  364. $number=Database::fetch_row($result);
  365. return ($number[0] != 0);
  366. }
  367. /**
  368. * Are there any results for this evaluation yet ?
  369. * The 'max' property should not be changed then.
  370. */
  371. public function has_results() {
  372. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  373. $sql='SELECT count(id) AS number FROM '.$tbl_grade_results
  374. .' WHERE evaluation_id = '.intval($this->id);
  375. $result = Database::query($sql);
  376. $number=Database::fetch_row($result);
  377. return ($number[0] != 0);
  378. }
  379. /**
  380. * Delete all results for this evaluation
  381. */
  382. public function delete_results() {
  383. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  384. $sql = 'DELETE FROM '.$tbl_grade_results.' WHERE evaluation_id = '.intval($this->id);
  385. Database::query($sql);
  386. }
  387. /**
  388. * Delete this evaluation and all underlying results.
  389. */
  390. public function delete_with_results(){
  391. $this->delete_results();
  392. $this->delete();
  393. }
  394. /**
  395. * Check if the given score is possible for this evaluation
  396. */
  397. public function is_valid_score ($score) {
  398. return (is_numeric($score) && $score >= 0 && $score <= $this->eval_max);
  399. }
  400. /**
  401. * Calculate the score of this evaluation
  402. * @param int $stud_id (default: all students who have results for this eval - then the average is returned)
  403. * @return array (score, max) if student is given
  404. * array (sum of scores, number of scores) otherwise
  405. * or null if no scores available
  406. */
  407. public function calc_score($stud_id = null)
  408. {
  409. $results = Result::load(null,$stud_id,$this->id);
  410. $rescount = 0;
  411. $sum = 0;
  412. foreach ($results as $res) {
  413. $score = $res->get_score();
  414. if ((!empty ($score)) || ($score == '0')) {
  415. $rescount++;
  416. $sum += ($score / $this->get_max());
  417. }
  418. }
  419. if ($rescount == 0) {
  420. return null;
  421. }
  422. else if (isset($stud_id)) {
  423. return array ($score, $this->get_max());
  424. } else {
  425. return array ($sum, $rescount);
  426. }
  427. }
  428. /**
  429. * Generate an array of possible categories where this evaluation can be moved to.
  430. * Notice: its own parent will be included in the list: it's up to the frontend
  431. * to disable this element.
  432. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  433. */
  434. public function get_target_categories() {
  435. // - course independent evaluation
  436. // -> movable to root or other course independent categories
  437. // - evaluation inside a course
  438. // -> movable to root, independent categories or categories inside the course
  439. $user = (api_is_platform_admin() ? null : api_get_user_id());
  440. $targets = array();
  441. $level = 0;
  442. $root = array(0, get_lang('RootCat'), $level);
  443. $targets[] = $root;
  444. if (isset($this->course_code) && !empty($this->course_code)) {
  445. $crscats = Category::load(null,null,$this->course_code,0);
  446. foreach ($crscats as $cat) {
  447. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  448. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  449. }
  450. }
  451. $indcats = Category::load(null,$user,0,0);
  452. foreach ($indcats as $cat) {
  453. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  454. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  455. }
  456. return $targets;
  457. }
  458. /**
  459. * Internal function used by get_target_categories()
  460. */
  461. private function add_target_subcategories($targets, $level, $catid)
  462. {
  463. $subcats = Category::load(null,null,null,$catid);
  464. foreach ($subcats as $cat) {
  465. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  466. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  467. }
  468. return $targets;
  469. }
  470. /**
  471. * Move this evaluation to the given category.
  472. * If this evaluation moves from inside a course to outside,
  473. * its course code is also changed.
  474. */
  475. public function move_to_cat($cat)
  476. {
  477. $this->set_category_id($cat->get_id());
  478. if ($this->get_course_code() != $cat->get_course_code()) {
  479. $this->set_course_code($cat->get_course_code());
  480. }
  481. $this->save();
  482. }
  483. /**
  484. * Retrieve evaluations where a student has results for
  485. * and return them as an array of Evaluation objects
  486. * @param $cat_id parent category (use 'null' to retrieve them in all categories)
  487. * @param $stud_id student id
  488. */
  489. public function get_evaluations_with_result_for_student ($cat_id = null, $stud_id)
  490. {
  491. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  492. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  493. $sql = 'SELECT * FROM '.$tbl_grade_evaluations
  494. .' WHERE id IN'
  495. .'(SELECT evaluation_id FROM '.$tbl_grade_results
  496. .' WHERE user_id = '.intval($stud_id).' AND score IS NOT NULL)';
  497. if (!api_is_allowed_to_edit()) {
  498. $sql .= ' AND visible = 1';
  499. }
  500. if (isset($cat_id)) {
  501. $sql .= ' AND category_id = '.intval($cat_id);
  502. } else {
  503. $sql .= ' AND category_id >= 0';
  504. }
  505. $result = Database::query($sql);
  506. $alleval = Evaluation::create_evaluation_objects_from_sql_result($result);
  507. return $alleval;
  508. }
  509. /**
  510. * Get a list of students that do not have a result record for this evaluation
  511. */
  512. public function get_not_subscribed_students ($first_letter_user = '')
  513. {
  514. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  515. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  516. $sql = 'SELECT user_id,lastname,firstname,username FROM '.$tbl_user
  517. ." WHERE lastname LIKE '".Database::escape_string($first_letter_user)."%'"
  518. .' AND status = '.STUDENT
  519. .' AND user_id NOT IN'
  520. .' (SELECT user_id FROM '.$tbl_grade_results
  521. .' WHERE evaluation_id = '.intval($this->id)
  522. .' )'
  523. .' ORDER BY lastname';
  524. $result = Database::query($sql);
  525. $db_users = Database::store_result($result);
  526. return $db_users;
  527. }
  528. /**
  529. * Find evaluations by name
  530. * @param string $name_mask search string
  531. * @return array evaluation objects matching the search criterium
  532. * @todo can be written more efficiently using a new (but very complex) sql query
  533. */
  534. public function find_evaluations ($name_mask,$selectcat)
  535. {
  536. $rootcat = Category::load($selectcat);
  537. $evals = $rootcat[0]->get_evaluations((api_is_allowed_to_create_course() ? null : api_get_user_id()), true);
  538. $foundevals = array();
  539. foreach ($evals as $eval) {
  540. if (!(api_strpos(api_strtolower($eval->get_name()), api_strtolower($name_mask)) === false)) {
  541. $foundevals[] = $eval;
  542. }
  543. }
  544. return $foundevals;
  545. }
  546. public function get_item_type()
  547. {
  548. return 'E';
  549. }
  550. public function get_icon_name()
  551. {
  552. return $this->has_results() ? 'evalnotempty' : 'evalempty';
  553. }
  554. /**
  555. * Locks an evaluation, only one who can unlock it is the platform administrator.
  556. * @param int locked 1 or unlocked 0
  557. *
  558. **/
  559. function lock($locked)
  560. {
  561. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  562. $sql = "UPDATE $table_evaluation SET locked = '".intval($locked)."' WHERE id='".intval($this->id)."'";
  563. Database::query($sql);
  564. }
  565. function check_lock_permissions()
  566. {
  567. if (api_is_platform_admin()) {
  568. return true;
  569. } else {
  570. if ($this->is_locked()) {
  571. api_not_allowed();
  572. }
  573. }
  574. }
  575. function delete_linked_data()
  576. {
  577. }
  578. }