evaluation.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 int $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 = 'SELECT weight from '.$tbl_grade_evaluations.'
  318. WHERE id='.$arreval['id'];
  319. $rs = Database::query($sql);
  320. $row_old_weight = Database::fetch_array($rs, 'ASSOC');
  321. $current_date = api_get_utc_datetime();
  322. $params = [
  323. 'id_linkeval_log' => $arreval['id'],
  324. 'name' => $arreval['name'],
  325. 'description' => $arreval['description'],
  326. 'created_at' => $current_date,
  327. 'weight' => $row_old_weight['weight'],
  328. 'visible' => $arreval['visible'],
  329. 'type' => 'evaluation',
  330. 'user_id_log' => api_get_user_id()
  331. ];
  332. Database::insert($tbl_grade_linkeval_log, $params);
  333. }
  334. }
  335. }
  336. /**
  337. * Update the properties of this evaluation in the database
  338. */
  339. public function save()
  340. {
  341. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  342. $sql = 'UPDATE '.$tbl_grade_evaluations
  343. ." SET name = '".Database::escape_string($this->get_name())."'"
  344. .', description = ';
  345. if (isset($this->description)) {
  346. $sql .= "'".Database::escape_string($this->get_description())."'";
  347. }else {
  348. $sql .= 'null';
  349. }
  350. $sql .= ', user_id = '.intval($this->get_user_id())
  351. .', course_code = ';
  352. if (isset($this->course_code)) {
  353. $sql .= "'".Database::escape_string($this->get_course_code())."'";
  354. } else {
  355. $sql .= 'null';
  356. }
  357. $sql .= ', category_id = ';
  358. if (isset($this->category)) {
  359. $sql .= intval($this->get_category_id());
  360. } else {
  361. $sql .= 'null';
  362. }
  363. $sql .= ', weight = "'.Database::escape_string($this->get_weight()).'" '
  364. .', max = '.intval($this->get_max())
  365. .', visible = '.intval($this->is_visible())
  366. .' WHERE id = '.intval($this->id);
  367. //recorded history
  368. $eval_log = new Evaluation();
  369. $eval_log->add_evaluation_log($this->id);
  370. Database::query($sql);
  371. }
  372. /**
  373. * Delete this evaluation from the database
  374. */
  375. public function delete()
  376. {
  377. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  378. $sql = 'DELETE FROM '.$tbl_grade_evaluations.' WHERE id = '.intval($this->id);
  379. Database::query($sql);
  380. }
  381. /**
  382. * Check if an evaluation name (with the same parent category) already exists
  383. * @param $name name to check (if not given, the name property of this object will be checked)
  384. * @param $parent parent category
  385. */
  386. public function does_name_exist($name, $parent)
  387. {
  388. if (!isset ($name)) {
  389. $name = $this->name;
  390. $parent = $this->category;
  391. }
  392. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  393. $sql = 'SELECT count(id) AS number'
  394. .' FROM '.$tbl_grade_evaluations
  395. ." WHERE name = '".Database::escape_string($name)."'";
  396. if (api_is_allowed_to_edit()) {
  397. $parent = Category::load($parent);
  398. $code = $parent[0]->get_course_code();
  399. $courseInfo = api_get_course_info($code);
  400. $courseId = $courseInfo['real_id'];
  401. if (isset($code) && $code != '0') {
  402. $main_course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  403. $sql .= ' AND user_id IN (
  404. SELECT user_id FROM '.$main_course_user_table.'
  405. WHERE
  406. c_id = '.$courseId.' AND
  407. status = '.COURSEMANAGER.'
  408. )';
  409. } else {
  410. $sql .= ' AND user_id = '.api_get_user_id();
  411. }
  412. }else {
  413. $sql .= ' AND user_id = '.api_get_user_id();
  414. }
  415. if (!isset ($parent)) {
  416. $sql.= ' AND category_id is null';
  417. } else {
  418. $sql.= ' AND category_id = '.intval($parent);
  419. }
  420. $result = Database::query($sql);
  421. $number=Database::fetch_row($result);
  422. return $number[0] != 0;
  423. }
  424. /**
  425. * Are there any results for this evaluation yet ?
  426. * The 'max' property should not be changed then.
  427. */
  428. public function has_results()
  429. {
  430. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  431. $sql = 'SELECT count(id) AS number
  432. FROM '.$tbl_grade_results.'
  433. WHERE evaluation_id = '.intval($this->id);
  434. $result = Database::query($sql);
  435. $number=Database::fetch_row($result);
  436. return ($number[0] != 0);
  437. }
  438. /**
  439. * Delete all results for this evaluation
  440. */
  441. public function delete_results()
  442. {
  443. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  444. $sql = 'DELETE FROM '.$tbl_grade_results.'
  445. WHERE evaluation_id = '.intval($this->id);
  446. Database::query($sql);
  447. }
  448. /**
  449. * Delete this evaluation and all underlying results.
  450. */
  451. public function delete_with_results()
  452. {
  453. $this->delete_results();
  454. $this->delete();
  455. }
  456. /**
  457. * Check if the given score is possible for this evaluation
  458. */
  459. public function is_valid_score($score)
  460. {
  461. return is_numeric($score) && $score >= 0 && $score <= $this->eval_max;
  462. }
  463. /**
  464. * Calculate the score of this evaluation
  465. * @param int $stud_id (default: all students who have results for this eval - then the average is returned)
  466. * @param string $type (best, average, ranking)
  467. * @return array (score, max) if student is given
  468. * array (sum of scores, number of scores) otherwise
  469. * or null if no scores available
  470. */
  471. public function calc_score($stud_id = null, $type = null)
  472. {
  473. $results = Result::load(null, $stud_id, $this->id);
  474. $rescount = 0;
  475. $sum = 0;
  476. $bestResult = 0;
  477. $weight = 0;
  478. $sumResult = 0;
  479. $students = [];
  480. /** @var Result $res */
  481. foreach ($results as $res) {
  482. $score = $res->get_score();
  483. if (!empty($score) || $score == '0') {
  484. $rescount++;
  485. $sum += $score / $this->get_max();
  486. $sumResult += $score;
  487. if ($score > $bestResult) {
  488. $bestResult = $score;
  489. }
  490. $weight = $this->get_max();
  491. }
  492. $students[$res->get_user_id()] = $score;
  493. }
  494. if ($rescount == 0) {
  495. return null;
  496. } else if (isset($stud_id)) {
  497. return array($score, $this->get_max());
  498. } else {
  499. switch ($type) {
  500. case 'best':
  501. return array($bestResult, $weight);
  502. break;
  503. case 'average':
  504. return array($sumResult/$rescount, $weight);
  505. break;
  506. case 'ranking':
  507. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  508. break;
  509. default:
  510. return array($sum, $rescount);
  511. break;
  512. }
  513. }
  514. }
  515. /**
  516. * Generate an array of possible categories where this evaluation can be moved to.
  517. * Notice: its own parent will be included in the list: it's up to the frontend
  518. * to disable this element.
  519. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  520. */
  521. public function get_target_categories()
  522. {
  523. // - course independent evaluation
  524. // -> movable to root or other course independent categories
  525. // - evaluation inside a course
  526. // -> movable to root, independent categories or categories inside the course
  527. $user = (api_is_platform_admin() ? null : api_get_user_id());
  528. $targets = array();
  529. $level = 0;
  530. $root = array(0, get_lang('RootCat'), $level);
  531. $targets[] = $root;
  532. if (isset($this->course_code) && !empty($this->course_code)) {
  533. $crscats = Category::load(null,null,$this->course_code,0);
  534. foreach ($crscats as $cat) {
  535. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  536. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  537. }
  538. }
  539. $indcats = Category::load(null,$user,0,0);
  540. foreach ($indcats as $cat) {
  541. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  542. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  543. }
  544. return $targets;
  545. }
  546. /**
  547. * Internal function used by get_target_categories()
  548. */
  549. private function add_target_subcategories($targets, $level, $catid)
  550. {
  551. $subcats = Category::load(null,null,null,$catid);
  552. foreach ($subcats as $cat) {
  553. $targets[] = array ($cat->get_id(), $cat->get_name(), $level+1);
  554. $targets = $this->add_target_subcategories($targets, $level+1, $cat->get_id());
  555. }
  556. return $targets;
  557. }
  558. /**
  559. * Move this evaluation to the given category.
  560. * If this evaluation moves from inside a course to outside,
  561. * its course code is also changed.
  562. */
  563. public function move_to_cat($cat)
  564. {
  565. $this->set_category_id($cat->get_id());
  566. if ($this->get_course_code() != $cat->get_course_code()) {
  567. $this->set_course_code($cat->get_course_code());
  568. }
  569. $this->save();
  570. }
  571. /**
  572. * Retrieve evaluations where a student has results for
  573. * and return them as an array of Evaluation objects
  574. * @param int $cat_id parent category (use 'null' to retrieve them in all categories)
  575. * @param int $stud_id student id
  576. */
  577. public static function get_evaluations_with_result_for_student($cat_id = null, $stud_id)
  578. {
  579. $tbl_grade_evaluations = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  580. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  581. $sql = 'SELECT * FROM '.$tbl_grade_evaluations.'
  582. WHERE id IN (
  583. SELECT evaluation_id FROM '.$tbl_grade_results.'
  584. WHERE user_id = '.intval($stud_id).' AND score IS NOT NULL
  585. )';
  586. if (!api_is_allowed_to_edit()) {
  587. $sql .= ' AND visible = 1';
  588. }
  589. if (isset($cat_id)) {
  590. $sql .= ' AND category_id = '.intval($cat_id);
  591. } else {
  592. $sql .= ' AND category_id >= 0';
  593. }
  594. $result = Database::query($sql);
  595. $alleval = Evaluation::create_evaluation_objects_from_sql_result($result);
  596. return $alleval;
  597. }
  598. /**
  599. * Get a list of students that do not have a result record for this evaluation
  600. */
  601. public function get_not_subscribed_students($first_letter_user = '')
  602. {
  603. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  604. $tbl_grade_results = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  605. $sql = 'SELECT user_id,lastname,firstname,username FROM '.$tbl_user
  606. ." WHERE lastname LIKE '".Database::escape_string($first_letter_user)."%'"
  607. .' AND status = '.STUDENT
  608. .' AND user_id NOT IN'
  609. .' (SELECT user_id FROM '.$tbl_grade_results
  610. .' WHERE evaluation_id = '.intval($this->id)
  611. .' )'
  612. .' ORDER BY lastname';
  613. $result = Database::query($sql);
  614. $users = Database::store_result($result);
  615. return $users;
  616. }
  617. /**
  618. * Find evaluations by name
  619. * @param string $name_mask search string
  620. * @return array evaluation objects matching the search criterium
  621. * @todo can be written more efficiently using a new (but very complex) sql query
  622. */
  623. public function find_evaluations($name_mask,$selectcat)
  624. {
  625. $rootcat = Category::load($selectcat);
  626. $evals = $rootcat[0]->get_evaluations((api_is_allowed_to_create_course() ? null : api_get_user_id()), true);
  627. $foundevals = array();
  628. foreach ($evals as $eval) {
  629. if (!(api_strpos(api_strtolower($eval->get_name()), api_strtolower($name_mask)) === false)) {
  630. $foundevals[] = $eval;
  631. }
  632. }
  633. return $foundevals;
  634. }
  635. public function get_item_type()
  636. {
  637. return 'E';
  638. }
  639. public function get_icon_name()
  640. {
  641. return $this->has_results() ? 'evalnotempty' : 'evalempty';
  642. }
  643. /**
  644. * Locks an evaluation, only one who can unlock it is the platform administrator.
  645. * @param int locked 1 or unlocked 0
  646. *
  647. **/
  648. function lock($locked)
  649. {
  650. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  651. $sql = "UPDATE $table_evaluation SET locked = '".intval($locked)."' WHERE id='".intval($this->id)."'";
  652. Database::query($sql);
  653. }
  654. function check_lock_permissions()
  655. {
  656. if (api_is_platform_admin()) {
  657. return true;
  658. } else {
  659. if ($this->is_locked()) {
  660. api_not_allowed();
  661. }
  662. }
  663. }
  664. function delete_linked_data()
  665. {
  666. }
  667. }