evaluation.class.php 24 KB

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