evaluation.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. $courseInfo = api_get_course_info($course_code);
  216. if ($courseInfo) {
  217. if ($paramcount != 0) {
  218. $sql .= ' AND';
  219. } else {
  220. $sql .= ' WHERE';
  221. }
  222. $sql .= " c_id = '".$courseInfo['real_id']."'";
  223. $paramcount++;
  224. }
  225. }
  226. if (isset($category_id)) {
  227. if ($paramcount != 0) {
  228. $sql .= ' AND';
  229. } else {
  230. $sql .= ' WHERE';
  231. }
  232. $sql .= ' category_id = '.intval($category_id);
  233. $paramcount++;
  234. }
  235. if (isset($visible)) {
  236. if ($paramcount != 0) {
  237. $sql .= ' AND';
  238. } else {
  239. $sql .= ' WHERE';
  240. }
  241. $sql .= ' visible = '.intval($visible);
  242. $paramcount++;
  243. }
  244. if (isset($locked)) {
  245. if ($paramcount != 0) {
  246. $sql .= ' AND';
  247. } else {
  248. $sql .= ' WHERE';
  249. }
  250. $sql .= ' locked = '.intval($locked);
  251. }
  252. $result = Database::query($sql);
  253. $allEval = self::create_evaluation_objects_from_sql_result($result);
  254. return $allEval;
  255. }
  256. /**
  257. * Insert this evaluation into the database.
  258. */
  259. public function add()
  260. {
  261. if (isset($this->name) &&
  262. isset($this->user_id) &&
  263. isset($this->weight) &&
  264. isset($this->eval_max) &&
  265. isset($this->visible)
  266. ) {
  267. if (empty($this->type)) {
  268. $this->type = 'evaluation';
  269. }
  270. $em = Database::getManager();
  271. $evaluation = new GradebookEvaluation();
  272. $evaluation
  273. ->setDescription($this->description)
  274. ->setCourse(api_get_course_entity())
  275. ->setName($this->get_name())
  276. ->setCategoryId($this->get_category_id())
  277. ->setUserId($this->get_user_id())
  278. ->setWeight(api_float_val($this->get_weight()))
  279. ->setMax($this->get_max())
  280. ->setVisible($this->is_visible())
  281. ->setType($this->type)
  282. ;
  283. $em->persist($evaluation);
  284. $em->flush();
  285. $this->set_id($evaluation->getId());
  286. }
  287. return false;
  288. }
  289. /**
  290. * @param int $id
  291. */
  292. public function addEvaluationLog($id)
  293. {
  294. if (!empty($id)) {
  295. $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  296. $tbl_grade_linkeval_log = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG);
  297. $eval = new Evaluation();
  298. $dateobject = $eval->load($id, null, null, null, null);
  299. $arreval = get_object_vars($dateobject[0]);
  300. if (!empty($arreval['id'])) {
  301. $sql = 'SELECT weight from '.$tbl_grade_evaluations.'
  302. WHERE id='.$arreval['id'];
  303. $rs = Database::query($sql);
  304. $row_old_weight = Database::fetch_array($rs, 'ASSOC');
  305. $current_date = api_get_utc_datetime();
  306. $params = [
  307. 'id_linkeval_log' => $arreval['id'],
  308. 'name' => $arreval['name'],
  309. 'description' => $arreval['description'],
  310. 'created_at' => $current_date,
  311. 'weight' => $row_old_weight['weight'],
  312. 'visible' => $arreval['visible'],
  313. 'type' => 'evaluation',
  314. 'user_id_log' => api_get_user_id(),
  315. ];
  316. Database::insert($tbl_grade_linkeval_log, $params);
  317. }
  318. }
  319. }
  320. /**
  321. * Update the properties of this evaluation in the database.
  322. */
  323. public function save()
  324. {
  325. $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  326. $sql = 'UPDATE '.$tbl_grade_evaluations
  327. ." SET name = '".Database::escape_string($this->get_name())."'"
  328. .', description = ';
  329. if (isset($this->description)) {
  330. $sql .= "'".Database::escape_string($this->get_description())."'";
  331. } else {
  332. $sql .= 'null';
  333. }
  334. $sql .= ', user_id = '.intval($this->get_user_id())
  335. .', c_id = ';
  336. if (isset($this->courseId)) {
  337. $sql .= "'".Database::escape_string($this->getCourseId())."'";
  338. } else {
  339. $sql .= 'null';
  340. }
  341. $sql .= ', category_id = ';
  342. if (isset($this->category)) {
  343. $sql .= intval($this->get_category_id());
  344. } else {
  345. $sql .= 'null';
  346. }
  347. $sql .= ', weight = "'.Database::escape_string($this->get_weight()).'" '
  348. .', max = '.intval($this->get_max())
  349. .', visible = '.intval($this->is_visible())
  350. .' WHERE id = '.intval($this->id);
  351. //recorded history
  352. $eval_log = new Evaluation();
  353. $eval_log->addEvaluationLog($this->id);
  354. Database::query($sql);
  355. }
  356. /**
  357. * Delete this evaluation from the database.
  358. */
  359. public function delete()
  360. {
  361. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  362. $sql = 'DELETE FROM '.$table.'
  363. WHERE id = '.$this->get_id();
  364. Database::query($sql);
  365. }
  366. /**
  367. * Check if an evaluation name (with the same parent category) already exists.
  368. *
  369. * @param string $name to check (if not given, the name property of this object will be checked)
  370. * @param $parent parent category
  371. *
  372. * @return bool
  373. */
  374. public function does_name_exist($name, $parent)
  375. {
  376. if (!isset($name)) {
  377. $name = $this->name;
  378. $parent = $this->category;
  379. }
  380. $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  381. $sql = "SELECT count(id) AS number
  382. FROM $tbl_grade_evaluations
  383. WHERE name = '".Database::escape_string($name)."'";
  384. if (api_is_allowed_to_edit()) {
  385. $parent = Category::load($parent);
  386. $courseId = $parent[0]->getCourseId();
  387. if (isset($courseId) && !empty($courseId)) {
  388. $table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  389. $sql .= ' AND user_id IN (
  390. SELECT user_id FROM '.$table.'
  391. WHERE
  392. c_id = '.$courseId.' AND
  393. status = '.COURSEMANAGER.'
  394. )';
  395. } else {
  396. $sql .= ' AND user_id = '.api_get_user_id();
  397. }
  398. } else {
  399. $sql .= ' AND user_id = '.api_get_user_id();
  400. }
  401. if (!isset($parent)) {
  402. $sql .= ' AND category_id is null';
  403. } else {
  404. $sql .= ' AND category_id = '.intval($parent);
  405. }
  406. $result = Database::query($sql);
  407. $number = Database::fetch_row($result);
  408. return $number[0] != 0;
  409. }
  410. /**
  411. * Are there any results for this evaluation yet ?
  412. * The 'max' property should not be changed then.
  413. *
  414. * @return bool
  415. */
  416. public function has_results()
  417. {
  418. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  419. $sql = 'SELECT count(id) AS number
  420. FROM '.$table.'
  421. WHERE evaluation_id = '.intval($this->get_id());
  422. $result = Database::query($sql);
  423. $number = Database::fetch_row($result);
  424. return $number[0] != 0;
  425. }
  426. /**
  427. * Delete all results for this evaluation.
  428. */
  429. public function delete_results()
  430. {
  431. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  432. $sql = 'DELETE FROM '.$table.'
  433. WHERE evaluation_id = '.$this->get_id();
  434. Database::query($sql);
  435. }
  436. /**
  437. * Delete this evaluation and all underlying results.
  438. */
  439. public function delete_with_results()
  440. {
  441. $this->delete_results();
  442. $this->delete();
  443. }
  444. /**
  445. * Check if the given score is possible for this evaluation.
  446. */
  447. public function is_valid_score($score)
  448. {
  449. return is_numeric($score) && $score >= 0 && $score <= $this->eval_max;
  450. }
  451. /**
  452. * Calculate the score of this evaluation.
  453. *
  454. * @param int $stud_id (default: all students who have results for this eval - then the average is returned)
  455. * @param string $type (best, average, ranking)
  456. *
  457. * @return array (score, max) if student is given
  458. * array (sum of scores, number of scores) otherwise
  459. * or null if no scores available
  460. */
  461. public function calc_score($stud_id = null, $type = null)
  462. {
  463. $allowStats = api_get_configuration_value('allow_gradebook_stats');
  464. if ($allowStats) {
  465. $evaluation = $this->entity;
  466. if (!empty($evaluation)) {
  467. $weight = $evaluation->getMax();
  468. switch ($type) {
  469. case 'best':
  470. $bestResult = $evaluation->getBestScore();
  471. $result = [$bestResult, $weight];
  472. return $result;
  473. break;
  474. case 'average':
  475. $count = count($evaluation->getUserScoreList());
  476. if (empty($count)) {
  477. $result = [0, $weight];
  478. return $result;
  479. }
  480. $sumResult = array_sum($evaluation->getUserScoreList());
  481. $result = [$sumResult / $count, $weight];
  482. return $result;
  483. break;
  484. case 'ranking':
  485. $ranking = AbstractLink::getCurrentUserRanking($stud_id, $evaluation->getUserScoreList());
  486. return $ranking;
  487. break;
  488. default:
  489. $weight = $evaluation->getMax();
  490. if (!empty($stud_id)) {
  491. $scoreList = $evaluation->getUserScoreList();
  492. $result = [0, $weight];
  493. if (isset($scoreList[$stud_id])) {
  494. $result = [$scoreList[$stud_id], $weight];
  495. }
  496. return $result;
  497. } else {
  498. $studentCount = count($evaluation->getUserScoreList());
  499. $sumResult = array_sum($evaluation->getUserScoreList());
  500. $result = [$sumResult, $studentCount];
  501. }
  502. return $result;
  503. break;
  504. }
  505. }
  506. }
  507. $useSession = true;
  508. if (isset($stud_id) && empty($type)) {
  509. $key = 'result_score_student_list_'.api_get_course_int_id().'_'.api_get_session_id().'_'.$this->id.'_'.$stud_id;
  510. $data = Session::read('calc_score');
  511. $results = isset($data[$key]) ? $data[$key] : null;
  512. if ($useSession == false) {
  513. $results = null;
  514. }
  515. $results = null;
  516. if (empty($results)) {
  517. $results = Result::load(null, $stud_id, $this->id);
  518. Session::write('calc_score', [$key => $results]);
  519. }
  520. $score = 0;
  521. /** @var Result $res */
  522. foreach ($results as $res) {
  523. $score = $res->get_score();
  524. }
  525. return [$score, $this->get_max()];
  526. } else {
  527. $count = 0;
  528. $sum = 0;
  529. $bestResult = 0;
  530. $weight = 0;
  531. $sumResult = 0;
  532. $key = 'result_score_student_list_'.api_get_course_int_id().'_'.api_get_session_id().'_'.$this->id;
  533. $data = Session::read('calc_score');
  534. $allResults = isset($data[$key]) ? $data[$key] : null;
  535. if ($useSession == false) {
  536. $allResults = null;
  537. }
  538. if (empty($allResults)) {
  539. $allResults = Result::load(null, null, $this->id);
  540. Session::write($key, $allResults);
  541. }
  542. $students = [];
  543. /** @var Result $res */
  544. foreach ($allResults as $res) {
  545. $score = $res->get_score();
  546. if (!empty($score) || $score == '0') {
  547. $count++;
  548. $sum += $score / $this->get_max();
  549. $sumResult += $score;
  550. if ($score > $bestResult) {
  551. $bestResult = $score;
  552. }
  553. $weight = $this->get_max();
  554. }
  555. $students[$res->get_user_id()] = $score;
  556. }
  557. if (empty($count)) {
  558. return [null, null];
  559. }
  560. switch ($type) {
  561. case 'best':
  562. return [$bestResult, $weight];
  563. break;
  564. case 'average':
  565. return [$sumResult / $count, $weight];
  566. break;
  567. case 'ranking':
  568. $students = [];
  569. /** @var Result $res */
  570. foreach ($allResults as $res) {
  571. $score = $res->get_score();
  572. $students[$res->get_user_id()] = $score;
  573. }
  574. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  575. break;
  576. default:
  577. return [$sum, $count];
  578. break;
  579. }
  580. }
  581. }
  582. /**
  583. * Generate an array of possible categories where this evaluation can be moved to.
  584. * Notice: its own parent will be included in the list: it's up to the frontend
  585. * to disable this element.
  586. *
  587. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  588. */
  589. public function get_target_categories()
  590. {
  591. // - course independent evaluation
  592. // -> movable to root or other course independent categories
  593. // - evaluation inside a course
  594. // -> movable to root, independent categories or categories inside the course
  595. $user = api_is_platform_admin() ? null : api_get_user_id();
  596. $targets = [];
  597. $level = 0;
  598. $root = [0, get_lang('Main folder'), $level];
  599. $targets[] = $root;
  600. if (isset($this->courseId) && !empty($this->courseId)) {
  601. $crscats = Category::load(null, null, $this->course_code, 0);
  602. foreach ($crscats as $cat) {
  603. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  604. $targets = $this->addTargetSubcategories($targets, $level + 1, $cat->get_id());
  605. }
  606. }
  607. $indcats = Category::load(null, $user, 0, 0);
  608. foreach ($indcats as $cat) {
  609. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  610. $targets = $this->addTargetSubcategories(
  611. $targets,
  612. $level + 1,
  613. $cat->get_id()
  614. );
  615. }
  616. return $targets;
  617. }
  618. /**
  619. * Move this evaluation to the given category.
  620. * If this evaluation moves from inside a course to outside,
  621. * its course code is also changed.
  622. */
  623. public function move_to_cat($cat)
  624. {
  625. $this->set_category_id($cat->get_id());
  626. if ($this->get_course_code() != $cat->get_course_code()) {
  627. $this->set_course_code($cat->get_course_code());
  628. }
  629. $this->save();
  630. }
  631. /**
  632. * Retrieve evaluations where a student has results for
  633. * and return them as an array of Evaluation objects.
  634. *
  635. * @param int $cat_id parent category (use 'null' to retrieve them in all categories)
  636. * @param int $stud_id student id
  637. *
  638. * @return array
  639. */
  640. public static function get_evaluations_with_result_for_student($cat_id = null, $stud_id)
  641. {
  642. $tbl_grade_evaluations = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  643. $tbl_grade_results = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  644. $sql = 'SELECT * FROM '.$tbl_grade_evaluations.'
  645. WHERE id IN (
  646. SELECT evaluation_id FROM '.$tbl_grade_results.'
  647. WHERE user_id = '.intval($stud_id).' AND score IS NOT NULL
  648. )';
  649. if (!api_is_allowed_to_edit()) {
  650. $sql .= ' AND visible = 1';
  651. }
  652. if (isset($cat_id)) {
  653. $sql .= ' AND category_id = '.intval($cat_id);
  654. } else {
  655. $sql .= ' AND category_id >= 0';
  656. }
  657. $result = Database::query($sql);
  658. $alleval = self::create_evaluation_objects_from_sql_result($result);
  659. return $alleval;
  660. }
  661. /**
  662. * Get a list of students that do not have a result record for this evaluation.
  663. *
  664. * @param string $first_letter_user
  665. *
  666. * @return array
  667. */
  668. public function get_not_subscribed_students($first_letter_user = '')
  669. {
  670. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  671. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT);
  672. $sql = "SELECT user_id,lastname,firstname,username
  673. FROM $tbl_user
  674. WHERE
  675. lastname LIKE '".Database::escape_string($first_letter_user)."%' AND
  676. status = ".STUDENT." AND user_id NOT IN (
  677. SELECT user_id FROM $table
  678. WHERE evaluation_id = ".$this->get_id()."
  679. )
  680. ORDER BY lastname";
  681. $result = Database::query($sql);
  682. $users = Database::store_result($result);
  683. return $users;
  684. }
  685. /**
  686. * Find evaluations by name.
  687. *
  688. * @param string $name_mask search string
  689. *
  690. * @return array evaluation objects matching the search criterium
  691. *
  692. * @todo can be written more efficiently using a new (but very complex) sql query
  693. */
  694. public function findEvaluations($name_mask, $selectcat)
  695. {
  696. $rootcat = Category::load($selectcat);
  697. $evals = $rootcat[0]->get_evaluations(
  698. (api_is_allowed_to_create_course() ? null : api_get_user_id()),
  699. true
  700. );
  701. $foundevals = [];
  702. foreach ($evals as $eval) {
  703. if (!(api_strpos(api_strtolower($eval->get_name()), api_strtolower($name_mask)) === false)) {
  704. $foundevals[] = $eval;
  705. }
  706. }
  707. return $foundevals;
  708. }
  709. public function get_item_type()
  710. {
  711. return 'E';
  712. }
  713. public function get_icon_name()
  714. {
  715. return $this->has_results() ? 'evalnotempty' : 'evalempty';
  716. }
  717. /**
  718. * Locks an evaluation, only one who can unlock it is the platform administrator.
  719. *
  720. * @param int locked 1 or unlocked 0
  721. */
  722. public function lock($locked)
  723. {
  724. $table_evaluation = Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION);
  725. $sql = "UPDATE $table_evaluation
  726. SET locked = '".intval($locked)."'
  727. WHERE id='".$this->get_id()."'";
  728. Database::query($sql);
  729. }
  730. public function check_lock_permissions()
  731. {
  732. if (api_is_platform_admin()) {
  733. return true;
  734. } else {
  735. if ($this->is_locked()) {
  736. api_not_allowed();
  737. }
  738. }
  739. }
  740. public function delete_linked_data()
  741. {
  742. }
  743. /**
  744. * @return mixed
  745. */
  746. public function getStudentList()
  747. {
  748. return $this->studentList;
  749. }
  750. /**
  751. * @param $list
  752. */
  753. public function setStudentList($list)
  754. {
  755. $this->studentList = $list;
  756. }
  757. /**
  758. * @param int $evaluationId
  759. */
  760. public static function generateStats($evaluationId)
  761. {
  762. $allowStats = api_get_configuration_value('allow_gradebook_stats');
  763. if ($allowStats) {
  764. $evaluation = self::load($evaluationId);
  765. $results = Result::load(null, null, $evaluationId);
  766. $sumResult = 0;
  767. $bestResult = 0;
  768. $average = 0;
  769. $scoreList = [];
  770. if (!empty($results)) {
  771. /** @var Result $result */
  772. foreach ($results as $result) {
  773. $score = $result->get_score();
  774. $scoreList[$result->get_user_id()] = $score;
  775. $sumResult += $score;
  776. if ($score > $bestResult) {
  777. $bestResult = $score;
  778. }
  779. }
  780. $average = $sumResult / count($results);
  781. }
  782. /** @var Evaluation $evaluation */
  783. $evaluation = $evaluation[0];
  784. $evaluation = $evaluation->entity;
  785. $evaluation
  786. ->setBestScore($bestResult)
  787. ->setAverageScore($average)
  788. ->setUserScoreList($scoreList)
  789. ;
  790. $em = Database::getManager();
  791. $em->persist($evaluation);
  792. $em->flush();
  793. }
  794. }
  795. /**
  796. * @param int $courseId
  797. *
  798. * @return Evaluation
  799. */
  800. public function setCourseId($courseId)
  801. {
  802. $this->courseId = $courseId;
  803. return $this;
  804. }
  805. /**
  806. * @param array $result
  807. *
  808. * @return array
  809. */
  810. private static function create_evaluation_objects_from_sql_result($result)
  811. {
  812. $alleval = [];
  813. $allow = api_get_configuration_value('allow_gradebook_stats');
  814. if ($allow) {
  815. $em = Database::getManager();
  816. $repo = $em->getRepository('ChamiloCoreBundle:GradebookEvaluation');
  817. }
  818. if (Database::num_rows($result)) {
  819. while ($data = Database::fetch_array($result)) {
  820. $eval = new Evaluation();
  821. $eval->set_id($data['id']);
  822. $eval->set_name($data['name']);
  823. $eval->set_description($data['description']);
  824. $eval->set_user_id($data['user_id']);
  825. $eval->setCourseId($data['c_id']);
  826. $courseInfo = api_get_course_info_by_id($data['c_id']);
  827. $eval->set_course_code($courseInfo['code']);
  828. $eval->set_category_id($data['category_id']);
  829. $eval->set_date(api_get_local_time($data['created_at']));
  830. $eval->set_weight($data['weight']);
  831. $eval->set_max($data['max']);
  832. $eval->set_visible($data['visible']);
  833. $eval->set_type($data['type']);
  834. $eval->set_locked($data['locked']);
  835. $eval->setSessionId(api_get_session_id());
  836. if ($allow) {
  837. $eval->entity = $repo->find($data['id']);
  838. }
  839. $alleval[] = $eval;
  840. }
  841. }
  842. return $alleval;
  843. }
  844. /**
  845. * Internal function used by get_target_categories().
  846. *
  847. * @param array $targets
  848. * @param int $level
  849. * @param int $categoryId
  850. *
  851. * @return array
  852. */
  853. private function addTargetSubcategories($targets, $level, $categoryId)
  854. {
  855. $subcats = Category::load(null, null, null, $categoryId);
  856. foreach ($subcats as $cat) {
  857. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  858. $targets = $this->addTargetSubcategories(
  859. $targets,
  860. $level + 1,
  861. $cat->get_id()
  862. );
  863. }
  864. return $targets;
  865. }
  866. }