evaluation.class.php 28 KB

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