evaluation.class.php 18 KB

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