evaluation.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\GradebookEvaluation;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class Evaluation.
  7. *
  8. * @package chamilo.gradebook
  9. */
  10. class Evaluation implements GradebookItem
  11. {
  12. public $studentList;
  13. /** @var GradebookEvaluation */
  14. public $entity;
  15. private $id;
  16. private $name;
  17. private $description;
  18. private $user_id;
  19. private $course_code;
  20. /** @var Category */
  21. private $category;
  22. private $created_at;
  23. private $weight;
  24. private $eval_max;
  25. private $visible;
  26. private $sessionId;
  27. /**
  28. * Construct.
  29. */
  30. public function __construct()
  31. {
  32. }
  33. /**
  34. * @return Category
  35. */
  36. public function getCategory()
  37. {
  38. return $this->category;
  39. }
  40. /**
  41. * @param Category $category
  42. */
  43. public function setCategory($category)
  44. {
  45. $this->category = $category;
  46. }
  47. /**
  48. * @return int
  49. */
  50. public function get_category_id()
  51. {
  52. return $this->category->get_id();
  53. }
  54. /**
  55. * @param int $category_id
  56. */
  57. public function set_category_id($category_id)
  58. {
  59. $categories = Category::load($category_id);
  60. if (isset($categories[0])) {
  61. $this->setCategory($categories[0]);
  62. }
  63. }
  64. /**
  65. * @return int
  66. */
  67. public function get_id()
  68. {
  69. return (int) $this->id;
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function get_name()
  75. {
  76. return $this->name;
  77. }
  78. /**
  79. * @return string
  80. */
  81. public function get_description()
  82. {
  83. return $this->description;
  84. }
  85. public function get_user_id()
  86. {
  87. return $this->user_id;
  88. }
  89. public function get_course_code()
  90. {
  91. return $this->course_code;
  92. }
  93. /**
  94. * @return int
  95. */
  96. public function getSessionId()
  97. {
  98. return $this->sessionId;
  99. }
  100. /**
  101. * @param int $sessionId
  102. */
  103. public function setSessionId($sessionId)
  104. {
  105. $this->sessionId = (int) $sessionId;
  106. }
  107. public function get_date()
  108. {
  109. return $this->created_at;
  110. }
  111. public function get_weight()
  112. {
  113. return $this->weight;
  114. }
  115. public function get_max()
  116. {
  117. return $this->eval_max;
  118. }
  119. public function get_type()
  120. {
  121. return $this->type;
  122. }
  123. public function is_visible()
  124. {
  125. return $this->visible;
  126. }
  127. public function get_locked()
  128. {
  129. return $this->locked;
  130. }
  131. public function is_locked()
  132. {
  133. return isset($this->locked) && $this->locked == 1 ? true : false;
  134. }
  135. public function set_id($id)
  136. {
  137. $this->id = (int) $id;
  138. }
  139. public function set_name($name)
  140. {
  141. $this->name = $name;
  142. }
  143. public function set_description($description)
  144. {
  145. $this->description = $description;
  146. }
  147. public function set_user_id($user_id)
  148. {
  149. $this->user_id = $user_id;
  150. }
  151. public function set_course_code($course_code)
  152. {
  153. $this->course_code = $course_code;
  154. }
  155. public function set_date($date)
  156. {
  157. $this->created_at = $date;
  158. }
  159. public function set_weight($weight)
  160. {
  161. $this->weight = $weight;
  162. }
  163. public function set_max($max)
  164. {
  165. $this->eval_max = $max;
  166. }
  167. public function set_visible($visible)
  168. {
  169. $this->visible = $visible;
  170. }
  171. public function set_type($type)
  172. {
  173. $this->type = $type;
  174. }
  175. public function set_locked($locked)
  176. {
  177. $this->locked = $locked;
  178. }
  179. /**
  180. * Retrieve evaluations and return them as an array of Evaluation objects.
  181. *
  182. * @param int $id evaluation id
  183. * @param int $user_id user id (evaluation owner)
  184. * @param string $course_code course code
  185. * @param int $category_id parent category
  186. * @param int $visible visible
  187. *
  188. * @return array
  189. */
  190. public static function load(
  191. $id = null,
  192. $user_id = null,
  193. $course_code = null,
  194. $category_id = null,
  195. $visible = null,
  196. $locked = null
  197. ) {
  198. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  199. $sql = 'SELECT * FROM '.$table;
  200. $paramcount = 0;
  201. if (isset($id)) {
  202. $sql .= ' WHERE id = '.intval($id);
  203. $paramcount++;
  204. }
  205. if (isset($user_id)) {
  206. if ($paramcount != 0) {
  207. $sql .= ' AND';
  208. } else {
  209. $sql .= ' WHERE';
  210. }
  211. $sql .= ' user_id = '.intval($user_id);
  212. $paramcount++;
  213. }
  214. if (isset($course_code) && $course_code != '-1') {
  215. if ($paramcount != 0) {
  216. $sql .= ' AND';
  217. } else {
  218. $sql .= ' WHERE';
  219. }
  220. $sql .= " course_code = '".Database::escape_string($course_code)."'";
  221. $paramcount++;
  222. }
  223. if (isset($category_id)) {
  224. if ($paramcount != 0) {
  225. $sql .= ' AND';
  226. } else {
  227. $sql .= ' WHERE';
  228. }
  229. $sql .= ' category_id = '.intval($category_id);
  230. $paramcount++;
  231. }
  232. if (isset($visible)) {
  233. if ($paramcount != 0) {
  234. $sql .= ' AND';
  235. } else {
  236. $sql .= ' WHERE';
  237. }
  238. $sql .= ' visible = '.intval($visible);
  239. $paramcount++;
  240. }
  241. if (isset($locked)) {
  242. if ($paramcount != 0) {
  243. $sql .= ' AND';
  244. } else {
  245. $sql .= ' WHERE';
  246. }
  247. $sql .= ' locked = '.intval($locked);
  248. }
  249. $result = Database::query($sql);
  250. $allEval = self::create_evaluation_objects_from_sql_result($result);
  251. return $allEval;
  252. }
  253. /**
  254. * Insert this evaluation into the database.
  255. */
  256. public function add()
  257. {
  258. if (isset($this->name) &&
  259. isset($this->user_id) &&
  260. isset($this->weight) &&
  261. isset($this->eval_max) &&
  262. isset($this->visible)
  263. ) {
  264. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  265. $sql = 'INSERT INTO '.$table
  266. .' (name, user_id, weight, max, visible';
  267. if (isset($this->description)) {
  268. $sql .= ',description';
  269. }
  270. if (isset($this->course_code)) {
  271. $sql .= ', course_code';
  272. }
  273. if (isset($this->category)) {
  274. $sql .= ', category_id';
  275. }
  276. $sql .= ', created_at';
  277. $sql .= ',type';
  278. $sql .= ") VALUES ('".Database::escape_string($this->get_name())."'"
  279. .','.intval($this->get_user_id())
  280. .','.api_float_val($this->get_weight())
  281. .','.intval($this->get_max())
  282. .','.intval($this->is_visible());
  283. if (isset($this->description)) {
  284. $sql .= ",'".Database::escape_string($this->get_description())."'";
  285. }
  286. if (isset($this->course_code)) {
  287. $sql .= ",'".Database::escape_string($this->get_course_code())."'";
  288. }
  289. if (isset($this->category)) {
  290. $sql .= ','.intval($this->get_category_id());
  291. }
  292. if (empty($this->type)) {
  293. $this->type = 'evaluation';
  294. }
  295. $sql .= ", '".api_get_utc_datetime()."'";
  296. $sql .= ',\''.Database::escape_string($this->type).'\'';
  297. $sql .= ")";
  298. Database::query($sql);
  299. $this->set_id(Database::insert_id());
  300. } else {
  301. return false;
  302. //die('Error in Evaluation add: required field empty');
  303. }
  304. }
  305. /**
  306. * @param int $id
  307. */
  308. public function addEvaluationLog($id)
  309. {
  310. if (!empty($id)) {
  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($id, 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->addEvaluationLog($this->id);
  370. Database::query($sql);
  371. }
  372. /**
  373. * Delete this evaluation from the database.
  374. */
  375. public function delete()
  376. {
  377. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  378. $sql = 'DELETE FROM '.$table.'
  379. WHERE id = '.$this->get_id();
  380. Database::query($sql);
  381. }
  382. /**
  383. * Check if an evaluation name (with the same parent category) already exists.
  384. *
  385. * @param string $name to check (if not given, the name property of this object will be checked)
  386. * @param $parent parent category
  387. *
  388. * @return bool
  389. */
  390. public function does_name_exist($name, $parent)
  391. {
  392. if (!isset($name)) {
  393. $name = $this->name;
  394. $parent = $this->category;
  395. }
  396. $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  397. $sql = "SELECT count(id) AS number
  398. FROM $tbl_grade_evaluations
  399. WHERE name = '".Database::escape_string($name)."'";
  400. if (api_is_allowed_to_edit()) {
  401. $parent = Category::load($parent);
  402. $code = $parent[0]->get_course_code();
  403. $courseInfo = api_get_course_info($code);
  404. $courseId = $courseInfo['real_id'];
  405. if (isset($code) && $code != '0') {
  406. $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  407. $sql .= ' AND user_id IN (
  408. SELECT user_id FROM '.$main_course_user_table.'
  409. WHERE
  410. c_id = '.$courseId.' AND
  411. status = '.COURSEMANAGER.'
  412. )';
  413. } else {
  414. $sql .= ' AND user_id = '.api_get_user_id();
  415. }
  416. } else {
  417. $sql .= ' AND user_id = '.api_get_user_id();
  418. }
  419. if (!isset($parent)) {
  420. $sql .= ' AND category_id is null';
  421. } else {
  422. $sql .= ' AND category_id = '.intval($parent);
  423. }
  424. $result = Database::query($sql);
  425. $number = Database::fetch_row($result);
  426. return $number[0] != 0;
  427. }
  428. /**
  429. * Are there any results for this evaluation yet ?
  430. * The 'max' property should not be changed then.
  431. *
  432. * @return bool
  433. */
  434. public function has_results()
  435. {
  436. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  437. $sql = 'SELECT count(id) AS number
  438. FROM '.$table.'
  439. WHERE evaluation_id = '.intval($this->get_id());
  440. $result = Database::query($sql);
  441. $number = Database::fetch_row($result);
  442. return $number[0] != 0;
  443. }
  444. /**
  445. * Delete all results for this evaluation.
  446. */
  447. public function delete_results()
  448. {
  449. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  450. $sql = 'DELETE FROM '.$table.'
  451. WHERE evaluation_id = '.$this->get_id();
  452. Database::query($sql);
  453. }
  454. /**
  455. * Delete this evaluation and all underlying results.
  456. */
  457. public function delete_with_results()
  458. {
  459. $this->delete_results();
  460. $this->delete();
  461. }
  462. /**
  463. * Check if the given score is possible for this evaluation.
  464. */
  465. public function is_valid_score($score)
  466. {
  467. return is_numeric($score) && $score >= 0 && $score <= $this->eval_max;
  468. }
  469. /**
  470. * Calculate the score of this evaluation.
  471. *
  472. * @param int $stud_id (default: all students who have results for this eval - then the average is returned)
  473. * @param string $type (best, average, ranking)
  474. *
  475. * @return array (score, max) if student is given
  476. * array (sum of scores, number of scores) otherwise
  477. * or null if no scores available
  478. */
  479. public function calc_score($stud_id = null, $type = null)
  480. {
  481. $allowStats = api_get_configuration_value('allow_gradebook_stats');
  482. if ($allowStats) {
  483. $evaluation = $this->entity;
  484. if (!empty($evaluation)) {
  485. $weight = $evaluation->getMax();
  486. switch ($type) {
  487. case 'best':
  488. $bestResult = $evaluation->getBestScore();
  489. $result = [$bestResult, $weight];
  490. return $result;
  491. break;
  492. case 'average':
  493. $count = count($evaluation->getUserScoreList());
  494. if (empty($count)) {
  495. $result = [0, $weight];
  496. return $result;
  497. }
  498. $sumResult = array_sum($evaluation->getUserScoreList());
  499. $result = [$sumResult / $count, $weight];
  500. return $result;
  501. break;
  502. case 'ranking':
  503. $ranking = AbstractLink::getCurrentUserRanking($stud_id, $evaluation->getUserScoreList());
  504. return $ranking;
  505. break;
  506. default:
  507. $weight = $evaluation->getMax();
  508. if (!empty($stud_id)) {
  509. $scoreList = $evaluation->getUserScoreList();
  510. $result = [0, $weight];
  511. if (isset($scoreList[$stud_id])) {
  512. $result = [$scoreList[$stud_id], $weight];
  513. }
  514. return $result;
  515. } else {
  516. $studentCount = count($evaluation->getUserScoreList());
  517. $sumResult = array_sum($evaluation->getUserScoreList());
  518. $result = [$sumResult, $studentCount];
  519. }
  520. return $result;
  521. break;
  522. }
  523. }
  524. }
  525. $useSession = true;
  526. if (isset($stud_id) && empty($type)) {
  527. $key = 'result_score_student_list_'.api_get_course_int_id().'_'.api_get_session_id().'_'.$this->id.'_'.$stud_id;
  528. $data = Session::read('calc_score');
  529. $results = isset($data[$key]) ? $data[$key] : null;
  530. if ($useSession == false) {
  531. $results = null;
  532. }
  533. $results = null;
  534. if (empty($results)) {
  535. $results = Result::load(null, $stud_id, $this->id);
  536. Session::write('calc_score', [$key => $results]);
  537. }
  538. $score = 0;
  539. /** @var Result $res */
  540. foreach ($results as $res) {
  541. $score = $res->get_score();
  542. }
  543. return [$score, $this->get_max()];
  544. } else {
  545. $count = 0;
  546. $sum = 0;
  547. $bestResult = 0;
  548. $weight = 0;
  549. $sumResult = 0;
  550. $key = 'result_score_student_list_'.api_get_course_int_id().'_'.api_get_session_id().'_'.$this->id;
  551. $data = Session::read('calc_score');
  552. $allResults = isset($data[$key]) ? $data[$key] : null;
  553. if ($useSession == false) {
  554. $allResults = null;
  555. }
  556. if (empty($allResults)) {
  557. $allResults = Result::load(null, null, $this->id);
  558. Session::write($key, $allResults);
  559. }
  560. $students = [];
  561. /** @var Result $res */
  562. foreach ($allResults as $res) {
  563. $score = $res->get_score();
  564. if (!empty($score) || $score == '0') {
  565. $count++;
  566. $sum += $score / $this->get_max();
  567. $sumResult += $score;
  568. if ($score > $bestResult) {
  569. $bestResult = $score;
  570. }
  571. $weight = $this->get_max();
  572. }
  573. $students[$res->get_user_id()] = $score;
  574. }
  575. if (empty($count)) {
  576. return [null, null];
  577. }
  578. switch ($type) {
  579. case 'best':
  580. return [$bestResult, $weight];
  581. break;
  582. case 'average':
  583. return [$sumResult / $count, $weight];
  584. break;
  585. case 'ranking':
  586. $students = [];
  587. /** @var Result $res */
  588. foreach ($allResults as $res) {
  589. $score = $res->get_score();
  590. $students[$res->get_user_id()] = $score;
  591. }
  592. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  593. break;
  594. default:
  595. return [$sum, $count];
  596. break;
  597. }
  598. }
  599. }
  600. /**
  601. * Generate an array of possible categories where this evaluation can be moved to.
  602. * Notice: its own parent will be included in the list: it's up to the frontend
  603. * to disable this element.
  604. *
  605. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  606. */
  607. public function get_target_categories()
  608. {
  609. // - course independent evaluation
  610. // -> movable to root or other course independent categories
  611. // - evaluation inside a course
  612. // -> movable to root, independent categories or categories inside the course
  613. $user = api_is_platform_admin() ? null : api_get_user_id();
  614. $targets = [];
  615. $level = 0;
  616. $root = [0, get_lang('RootCat'), $level];
  617. $targets[] = $root;
  618. if (isset($this->course_code) && !empty($this->course_code)) {
  619. $crscats = Category::load(null, null, $this->course_code, 0);
  620. foreach ($crscats as $cat) {
  621. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  622. $targets = $this->addTargetSubcategories(
  623. $targets,
  624. $level + 1,
  625. $cat->get_id()
  626. );
  627. }
  628. }
  629. $indcats = Category::load(null, $user, 0, 0);
  630. foreach ($indcats as $cat) {
  631. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  632. $targets = $this->addTargetSubcategories(
  633. $targets,
  634. $level + 1,
  635. $cat->get_id()
  636. );
  637. }
  638. return $targets;
  639. }
  640. /**
  641. * Move this evaluation to the given category.
  642. * If this evaluation moves from inside a course to outside,
  643. * its course code is also changed.
  644. */
  645. public function move_to_cat($cat)
  646. {
  647. $this->set_category_id($cat->get_id());
  648. if ($this->get_course_code() != $cat->get_course_code()) {
  649. $this->set_course_code($cat->get_course_code());
  650. }
  651. $this->save();
  652. }
  653. /**
  654. * Retrieve evaluations where a student has results for
  655. * and return them as an array of Evaluation objects.
  656. *
  657. * @param int $cat_id parent category (use 'null' to retrieve them in all categories)
  658. * @param int $stud_id student id
  659. *
  660. * @return array
  661. */
  662. public static function get_evaluations_with_result_for_student($cat_id = null, $stud_id)
  663. {
  664. $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  665. $tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  666. $sql = 'SELECT * FROM '.$tbl_grade_evaluations.'
  667. WHERE id IN (
  668. SELECT evaluation_id FROM '.$tbl_grade_results.'
  669. WHERE user_id = '.intval($stud_id).' AND score IS NOT NULL
  670. )';
  671. if (!api_is_allowed_to_edit()) {
  672. $sql .= ' AND visible = 1';
  673. }
  674. if (isset($cat_id)) {
  675. $sql .= ' AND category_id = '.intval($cat_id);
  676. } else {
  677. $sql .= ' AND category_id >= 0';
  678. }
  679. $result = Database::query($sql);
  680. $alleval = self::create_evaluation_objects_from_sql_result($result);
  681. return $alleval;
  682. }
  683. /**
  684. * Get a list of students that do not have a result record for this evaluation.
  685. *
  686. * @param string $first_letter_user
  687. *
  688. * @return array
  689. */
  690. public function get_not_subscribed_students($first_letter_user = '')
  691. {
  692. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  693. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  694. $sql = "SELECT user_id,lastname,firstname,username
  695. FROM $tbl_user
  696. WHERE
  697. lastname LIKE '".Database::escape_string($first_letter_user)."%' AND
  698. status = ".STUDENT." AND user_id NOT IN (
  699. SELECT user_id FROM $table
  700. WHERE evaluation_id = ".$this->get_id()."
  701. )
  702. ORDER BY lastname";
  703. $result = Database::query($sql);
  704. $users = Database::store_result($result);
  705. return $users;
  706. }
  707. /**
  708. * Find evaluations by name.
  709. *
  710. * @param string $name_mask search string
  711. *
  712. * @return array evaluation objects matching the search criterium
  713. *
  714. * @todo can be written more efficiently using a new (but very complex) sql query
  715. */
  716. public function findEvaluations($name_mask, $selectcat)
  717. {
  718. $rootcat = Category::load($selectcat);
  719. $evals = $rootcat[0]->get_evaluations(
  720. (api_is_allowed_to_create_course() ? null : api_get_user_id()),
  721. true
  722. );
  723. $foundevals = [];
  724. foreach ($evals as $eval) {
  725. if (!(api_strpos(api_strtolower($eval->get_name()), api_strtolower($name_mask)) === false)) {
  726. $foundevals[] = $eval;
  727. }
  728. }
  729. return $foundevals;
  730. }
  731. public function get_item_type()
  732. {
  733. return 'E';
  734. }
  735. public function get_icon_name()
  736. {
  737. return $this->has_results() ? 'evalnotempty' : 'evalempty';
  738. }
  739. /**
  740. * Locks an evaluation, only one who can unlock it is the platform administrator.
  741. *
  742. * @param int locked 1 or unlocked 0
  743. */
  744. public function lock($locked)
  745. {
  746. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  747. $sql = "UPDATE $table_evaluation
  748. SET locked = '".intval($locked)."'
  749. WHERE id='".$this->get_id()."'";
  750. Database::query($sql);
  751. }
  752. public function check_lock_permissions()
  753. {
  754. if (api_is_platform_admin()) {
  755. return true;
  756. } else {
  757. if ($this->is_locked()) {
  758. api_not_allowed();
  759. }
  760. }
  761. }
  762. public function delete_linked_data()
  763. {
  764. }
  765. /**
  766. * @return mixed
  767. */
  768. public function getStudentList()
  769. {
  770. return $this->studentList;
  771. }
  772. /**
  773. * @param $list
  774. */
  775. public function setStudentList($list)
  776. {
  777. $this->studentList = $list;
  778. }
  779. /**
  780. * @param int $evaluationId
  781. */
  782. public static function generateStats($evaluationId)
  783. {
  784. $allowStats = api_get_configuration_value('allow_gradebook_stats');
  785. if ($allowStats) {
  786. $evaluation = self::load($evaluationId);
  787. $results = Result::load(null, null, $evaluationId);
  788. $sumResult = 0;
  789. $bestResult = 0;
  790. $average = 0;
  791. $scoreList = [];
  792. if (!empty($results)) {
  793. /** @var Result $result */
  794. foreach ($results as $result) {
  795. $score = $result->get_score();
  796. $scoreList[$result->get_user_id()] = $score;
  797. $sumResult += $score;
  798. if ($score > $bestResult) {
  799. $bestResult = $score;
  800. }
  801. }
  802. $average = $sumResult / count($results);
  803. }
  804. /** @var Evaluation $evaluation */
  805. $evaluation = $evaluation[0];
  806. $evaluation = $evaluation->entity;
  807. $evaluation
  808. ->setBestScore($bestResult)
  809. ->setAverageScore($average)
  810. ->setUserScoreList($scoreList)
  811. ;
  812. $em = Database::getManager();
  813. $em->persist($evaluation);
  814. $em->flush();
  815. }
  816. }
  817. /**
  818. * @param array $result
  819. *
  820. * @return array
  821. */
  822. private static function create_evaluation_objects_from_sql_result($result)
  823. {
  824. $alleval = [];
  825. $allow = api_get_configuration_value('allow_gradebook_stats');
  826. if ($allow) {
  827. $em = Database::getManager();
  828. $repo = $em->getRepository('ChamiloCoreBundle:GradebookEvaluation');
  829. }
  830. if (Database::num_rows($result)) {
  831. while ($data = Database::fetch_array($result)) {
  832. $eval = new Evaluation();
  833. $eval->set_id($data['id']);
  834. $eval->set_name($data['name']);
  835. $eval->set_description($data['description']);
  836. $eval->set_user_id($data['user_id']);
  837. $eval->set_course_code($data['course_code']);
  838. $eval->set_category_id($data['category_id']);
  839. $eval->set_date(api_get_local_time($data['created_at']));
  840. $eval->set_weight($data['weight']);
  841. $eval->set_max($data['max']);
  842. $eval->set_visible($data['visible']);
  843. $eval->set_type($data['type']);
  844. $eval->set_locked($data['locked']);
  845. $eval->setSessionId(api_get_session_id());
  846. if ($allow) {
  847. $eval->entity = $repo->find($data['id']);
  848. }
  849. $alleval[] = $eval;
  850. }
  851. }
  852. return $alleval;
  853. }
  854. /**
  855. * Internal function used by get_target_categories().
  856. *
  857. * @param array $targets
  858. * @param int $level
  859. * @param int $categoryId
  860. *
  861. * @return array
  862. */
  863. private function addTargetSubcategories($targets, $level, $categoryId)
  864. {
  865. $subcats = Category::load(null, null, null, $categoryId);
  866. foreach ($subcats as $cat) {
  867. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  868. $targets = $this->addTargetSubcategories(
  869. $targets,
  870. $level + 1,
  871. $cat->get_id()
  872. );
  873. }
  874. return $targets;
  875. }
  876. }