category.class.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Category
  5. * Defines a gradebook Category object
  6. * @package chamilo.gradebook
  7. */
  8. class Category implements GradebookItem
  9. {
  10. private $id;
  11. private $name;
  12. private $description;
  13. private $user_id;
  14. private $course_code;
  15. private $parent;
  16. private $weight;
  17. private $visible;
  18. private $certificate_min_score;
  19. private $session_id;
  20. private $skills = array();
  21. private $grade_model_id;
  22. public function __construct()
  23. {
  24. }
  25. // GETTERS AND SETTERS
  26. public function get_id()
  27. {
  28. return $this->id;
  29. }
  30. public function get_name()
  31. {
  32. return $this->name;
  33. }
  34. public function get_description()
  35. {
  36. return $this->description;
  37. }
  38. public function get_user_id()
  39. {
  40. return $this->user_id;
  41. }
  42. public function get_certificate_min_score()
  43. {
  44. if (!empty($this->certificate_min_score)) {
  45. return $this->certificate_min_score;
  46. } else {
  47. return null;
  48. }
  49. }
  50. public function get_course_code()
  51. {
  52. return $this->course_code;
  53. }
  54. public function get_parent_id()
  55. {
  56. return $this->parent;
  57. }
  58. public function get_weight()
  59. {
  60. return $this->weight;
  61. }
  62. public function is_locked()
  63. {
  64. return isset($this->locked) && $this->locked == 1 ? true : false;
  65. }
  66. public function is_visible()
  67. {
  68. return $this->visible;
  69. }
  70. public function set_id($id)
  71. {
  72. $this->id = $id;
  73. }
  74. public function set_name($name)
  75. {
  76. $this->name = $name;
  77. }
  78. public function set_description($description)
  79. {
  80. $this->description = $description;
  81. }
  82. public function set_user_id($user_id)
  83. {
  84. $this->user_id = $user_id;
  85. }
  86. public function set_course_code($course_code)
  87. {
  88. $this->course_code = $course_code;
  89. $courseInfo = api_get_course_info($course_code);
  90. $this->course_id = $courseInfo['real_id'];
  91. }
  92. public function set_course_id($courseId)
  93. {
  94. $this->course_id = $courseId;
  95. }
  96. public function get_course_id()
  97. {
  98. return $this->course_id;
  99. }
  100. public function set_certificate_min_score($min_score = null)
  101. {
  102. $this->certificate_min_score = $min_score;
  103. }
  104. public function set_parent_id($parent)
  105. {
  106. $this->parent = intval($parent);
  107. }
  108. /**
  109. * Filters to int and sets the session ID
  110. * @param int The session ID from the Dokeos course session
  111. */
  112. public function set_session_id($session_id = 0)
  113. {
  114. $this->session_id = (int)$session_id;
  115. }
  116. public function set_weight($weight)
  117. {
  118. $this->weight = $weight;
  119. }
  120. public function set_visible($visible)
  121. {
  122. $this->visible = $visible;
  123. }
  124. public function set_grade_model_id($id)
  125. {
  126. $this->grade_model_id = $id;
  127. }
  128. public function set_locked($locked)
  129. {
  130. $this->locked = $locked;
  131. }
  132. public function get_grade_model_id()
  133. {
  134. return $this->grade_model_id;
  135. }
  136. public function get_type()
  137. {
  138. return 'category';
  139. }
  140. public function get_skills($from_db = true)
  141. {
  142. if ($from_db) {
  143. $cat_id = $this->get_id();
  144. $gradebook = new Gradebook();
  145. $skills = $gradebook->get_skills_by_gradebook($cat_id);
  146. } else {
  147. $skills = $this->skills;
  148. }
  149. return $skills;
  150. }
  151. public function get_skills_for_select()
  152. {
  153. $skills = $this->get_skills();
  154. $skill_select = array();
  155. if (!empty($skills)) {
  156. foreach($skills as $skill) {
  157. $skill_select[$skill['id']] = $skill['name'];
  158. }
  159. }
  160. return $skill_select;
  161. }
  162. /**
  163. * @param int $id
  164. * @param int $session_id
  165. *
  166. * @return array
  167. */
  168. public static function load_session_categories($id = null, $session_id = null)
  169. {
  170. if (isset($id) && (int)$id === 0) {
  171. $cats = array();
  172. $cats[] = Category::create_root_category();
  173. return $cats;
  174. }
  175. $courseCode = api_get_course_info(api_get_course_id());
  176. $courseCode = $courseCode['code'];
  177. $session_id = intval($session_id);
  178. if (!empty($session_id)) {
  179. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  180. $sql = 'SELECT id, course_code FROM '.$tbl_grade_categories. '
  181. WHERE session_id = '.$session_id;
  182. $result_session = Database::query($sql);
  183. if (Database::num_rows($result_session) > 0) {
  184. $categoryList = array();
  185. while ($data_session = Database::fetch_array($result_session)) {
  186. $parent_id = $data_session['id'];
  187. if ($data_session['course_code'] == $courseCode) {
  188. $categories = Category::load($parent_id);
  189. $categoryList = array_merge($categoryList, $categories);
  190. //$allSubCategories = Category::load(null,null,null, $parent_id, null, $session_id, null);
  191. }
  192. }
  193. return $categoryList;
  194. }
  195. }
  196. }
  197. /**
  198. * Retrieve categories and return them as an array of Category objects
  199. * @param int category id
  200. * @param int user id (category owner)
  201. * @param string course code
  202. * @param int parent category
  203. * @param bool visible
  204. * @param int session id (in case we are in a session)
  205. * @param bool Whether to show all "session" categories (true) or hide them (false) in case there is no session id
  206. */
  207. public static function load(
  208. $id = null,
  209. $user_id = null,
  210. $course_code = null,
  211. $parent_id = null,
  212. $visible = null,
  213. $session_id = null,
  214. $order_by = null
  215. ) {
  216. //if the category given is explicitly 0 (not null), then create
  217. // a root category object (in memory)
  218. if (isset($id) && (int)$id === 0) {
  219. $cats = array();
  220. $cats[] = Category::create_root_category();
  221. return $cats;
  222. }
  223. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  224. $sql = 'SELECT * FROM '.$tbl_grade_categories;
  225. $paramcount = 0;
  226. if (isset($id)) {
  227. $id = Database::escape_string($id);
  228. $sql.= ' WHERE id = '.intval($id);
  229. $paramcount ++;
  230. }
  231. if (isset($user_id)) {
  232. $user_id = intval($user_id);
  233. if ($paramcount != 0) {
  234. $sql .= ' AND';
  235. } else {
  236. $sql .= ' WHERE';
  237. }
  238. $sql .= ' user_id = '.intval($user_id);
  239. $paramcount++;
  240. }
  241. if (isset($course_code)) {
  242. $course_code = Database::escape_string($course_code);
  243. if ($paramcount != 0) {
  244. $sql .= ' AND';
  245. } else {
  246. $sql .= ' WHERE';
  247. }
  248. if ($course_code == '0') {
  249. $sql .= ' course_code is null ';
  250. } else {
  251. $sql .= " course_code = '".Database::escape_string($course_code)."'";
  252. }
  253. /*if ($show_session_categories !== true) {
  254. // a query on the course should show all
  255. // the categories inside sessions for this course
  256. // otherwise a special parameter is given to ask explicitely
  257. $sql .= " AND (session_id IS NULL OR session_id = 0) ";
  258. } else {*/
  259. if (empty($session_id)) {
  260. $sql .= ' AND (session_id IS NULL OR session_id = 0) ';
  261. } else {
  262. $sql .= ' AND session_id = '.(int)$session_id.' ';
  263. }
  264. //}
  265. $paramcount++;
  266. }
  267. if (isset($parent_id)) {
  268. $parent_id = Database::escape_string($parent_id);
  269. if ($paramcount != 0) {
  270. $sql .= ' AND ';
  271. } else {
  272. $sql .= ' WHERE ';
  273. }
  274. $sql .= ' parent_id = '.intval($parent_id);
  275. $paramcount++;
  276. }
  277. if (isset($visible)) {
  278. $visible = Database::escape_string($visible);
  279. if ($paramcount != 0) {
  280. $sql .= ' AND';
  281. } else {
  282. $sql .= ' WHERE';
  283. }
  284. $sql .= ' visible = '.intval($visible);
  285. }
  286. if (!empty($order_by)) {
  287. if (!empty($order_by) && $order_by != '') {
  288. $sql .= ' '.$order_by;
  289. }
  290. }
  291. $result = Database::query($sql);
  292. $allcat = array();
  293. if (Database::num_rows($result) > 0) {
  294. $allcat = Category::create_category_objects_from_sql_result($result);
  295. }
  296. return $allcat;
  297. }
  298. private function create_root_category()
  299. {
  300. $cat = new Category();
  301. $cat->set_id(0);
  302. $cat->set_name(get_lang('RootCat'));
  303. $cat->set_description(null);
  304. $cat->set_user_id(0);
  305. $cat->set_course_code(null);
  306. $cat->set_parent_id(null);
  307. $cat->set_weight(0);
  308. $cat->set_visible(1);
  309. return $cat;
  310. }
  311. private static function create_category_objects_from_sql_result($result)
  312. {
  313. $allcat = array();
  314. while ($data = Database::fetch_array($result)) {
  315. $cat = new Category();
  316. $cat->set_id($data['id']);
  317. $cat->set_name($data['name']);
  318. $cat->set_description($data['description']);
  319. $cat->set_user_id($data['user_id']);
  320. $cat->set_course_code($data['course_code']);
  321. $cat->set_parent_id($data['parent_id']);
  322. $cat->set_weight($data['weight']);
  323. $cat->set_visible($data['visible']);
  324. $cat->set_session_id($data['session_id']);
  325. $cat->set_certificate_min_score($data['certif_min_score']);
  326. $cat->set_grade_model_id($data['grade_model_id']);
  327. $cat->set_locked($data['locked']);
  328. $allcat[] = $cat;
  329. }
  330. return $allcat;
  331. }
  332. /**
  333. * Insert this category into the database
  334. */
  335. public function add()
  336. {
  337. if (isset($this->name) && '-1' == $this->name) {
  338. return false;
  339. }
  340. if (isset($this->name) && isset($this->user_id)) {
  341. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  342. $sql = 'INSERT INTO '.$tbl_grade_categories.' (name,user_id,weight,visible';
  343. if (isset($this->description)) {
  344. $sql .= ',description';
  345. }
  346. if (isset($this->course_code)) {
  347. $sql .= ',course_code';
  348. }
  349. if (isset($this->parent)) {
  350. $sql .= ',parent_id';
  351. }
  352. if (!empty($this->session_id)) {
  353. $sql .= ', session_id';
  354. }
  355. if (isset($this->grade_model_id)) {
  356. $sql .= ', grade_model_id ';
  357. }
  358. if (isset($this->certificate_min_score) && !empty($this->certificate_min_score)) {
  359. $sql .= ', certif_min_score ';
  360. }
  361. /*
  362. $setting = api_get_setting('tool_visible_by_default_at_creation');
  363. $visible = 1;
  364. if (isset($setting['gradebook'])) {
  365. if ($setting['gradebook'] == 'false') {
  366. $visible = 0;
  367. }
  368. }*/
  369. $visible = intval($this->is_visible());
  370. $sql .= ") VALUES ('".Database::escape_string($this->get_name())."'"
  371. .','.intval($this->get_user_id())
  372. .','.Database::escape_string($this->get_weight())
  373. .','.$visible;
  374. if (isset($this->description)) {
  375. $sql .= ",'".Database::escape_string($this->get_description())."'";
  376. }
  377. if (isset($this->course_code)) {
  378. $sql .= ",'".Database::escape_string($this->get_course_code())."'";
  379. }
  380. if (isset($this->parent)) {
  381. $sql .= ','.intval($this->get_parent_id());
  382. }
  383. if (!empty($this->session_id)) {
  384. $sql .= ', '.intval($this->get_session_id());
  385. }
  386. if (isset($this->grade_model_id)) {
  387. $sql .= ', '.intval($this->get_grade_model_id());
  388. }
  389. if (isset($this->certificate_min_score) && !empty($this->certificate_min_score)) {
  390. $sql .= ', '.Database::escape_string($this->get_certificate_min_score());
  391. }
  392. $sql .= ')';
  393. Database::query($sql);
  394. $id = Database::insert_id();
  395. $this->set_id($id);
  396. if (!empty($id)) {
  397. $parent_id = $this->get_parent_id();
  398. $grade_model_id = $this->get_grade_model_id();
  399. if ($parent_id == 0) {
  400. //do something
  401. if (isset($grade_model_id) && !empty($grade_model_id) && $grade_model_id != '-1') {
  402. $obj = new GradeModel();
  403. $components = $obj->get_components($grade_model_id);
  404. $default_weight_setting = api_get_setting('gradebook_default_weight');
  405. $default_weight = 100;
  406. if (isset($default_weight_setting)) {
  407. $default_weight = $default_weight_setting;
  408. }
  409. foreach ($components as $component) {
  410. $gradebook = new Gradebook();
  411. $params = array();
  412. $params['name'] = $component['acronym'];
  413. $params['description'] = $component['title'];
  414. $params['user_id'] = api_get_user_id();
  415. $params['parent_id'] = $id;
  416. $params['weight'] = $component['percentage'] / 100 * $default_weight;
  417. $params['session_id'] = api_get_session_id();
  418. $params['course_code'] = $this->get_course_code();
  419. $gradebook->save($params);
  420. }
  421. }
  422. }
  423. }
  424. $gradebook = new Gradebook();
  425. $gradebook->update_skills_to_gradebook($this->id, $this->get_skills(false));
  426. return $id;
  427. }
  428. }
  429. /**
  430. * Update the properties of this category in the database
  431. * @todo fix me
  432. */
  433. public function save()
  434. {
  435. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  436. $sql = 'UPDATE '.$tbl_grade_categories." SET name = '".Database::escape_string(
  437. $this->get_name()
  438. )."'".', description = ';
  439. if (isset($this->description)) {
  440. $sql .= "'".Database::escape_string($this->get_description())."'";
  441. } else {
  442. $sql .= 'null';
  443. }
  444. $sql .= ', user_id = '.intval($this->get_user_id())
  445. .', course_code = ';
  446. if (isset($this->course_code)) {
  447. $sql .= "'".Database::escape_string($this->get_course_code())."'";
  448. } else {
  449. $sql .= 'null';
  450. }
  451. $sql .= ', parent_id = ';
  452. if (isset($this->parent)) {
  453. $sql .= intval($this->get_parent_id());
  454. } else {
  455. $sql .= 'null';
  456. }
  457. $sql .= ', certif_min_score = ';
  458. if (isset($this->certificate_min_score) && !empty($this->certificate_min_score)) {
  459. $sql .= Database::escape_string($this->get_certificate_min_score());
  460. } else {
  461. $sql .= 'null';
  462. }
  463. if (isset($this->grade_model_id)) {
  464. $sql .= ', grade_model_id = '.intval($this->get_grade_model_id());
  465. }
  466. $sql .= ', weight = '.Database::escape_string($this->get_weight())
  467. .', visible = '.intval($this->is_visible())
  468. .' WHERE id = '.intval($this->id);
  469. Database::query($sql);
  470. if (!empty($this->id)) {
  471. $parent_id = $this->get_parent_id();
  472. $grade_model_id = $this->get_grade_model_id();
  473. if ($parent_id == 0) {
  474. if (isset($grade_model_id) && !empty($grade_model_id) && $grade_model_id != '-1') {
  475. $obj = new GradeModel();
  476. $components = $obj->get_components($grade_model_id);
  477. $default_weight_setting = api_get_setting('gradebook_default_weight');
  478. $default_weight = 100;
  479. if (isset($default_weight_setting)) {
  480. $default_weight = $default_weight_setting;
  481. }
  482. $final_weight = $this->get_weight();
  483. if (!empty($final_weight)) {
  484. $default_weight = $this->get_weight();
  485. }
  486. foreach ($components as $component) {
  487. $gradebook = new Gradebook();
  488. $params = array();
  489. $params['name'] = $component['acronym'];
  490. $params['description'] = $component['title'];
  491. $params['user_id'] = api_get_user_id();
  492. $params['parent_id'] = $this->id;
  493. $params['weight'] = $component['percentage'] / 100 * $default_weight;
  494. $params['session_id'] = api_get_session_id();
  495. $params['course_code'] = $this->get_course_code();
  496. $gradebook->save($params);
  497. }
  498. }
  499. }
  500. }
  501. $gradebook = new Gradebook();
  502. $gradebook->update_skills_to_gradebook(
  503. $this->id,
  504. $this->get_skills(false),
  505. false
  506. );
  507. }
  508. /**
  509. * Update link weights see #5168
  510. * @param type $new_weight
  511. */
  512. function update_children_weight($new_weight)
  513. {
  514. $links = $this->get_links();
  515. $old_weight = $this->get_weight();
  516. if (!empty($links)) {
  517. foreach ($links as $link_item) {
  518. if (isset($link_item)) {
  519. $new_item_weight = $new_weight * $link_item->get_weight() / $old_weight;
  520. $link_item->set_weight($new_item_weight);
  521. $link_item->save();
  522. }
  523. }
  524. }
  525. }
  526. /**
  527. * Delete this evaluation from the database
  528. */
  529. public function delete()
  530. {
  531. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  532. $sql = 'DELETE FROM '.$tbl_grade_categories.' WHERE id = '.intval($this->id);
  533. Database::query($sql);
  534. }
  535. /**
  536. * Not delete this category from the database,when visible=3 is category eliminated
  537. */
  538. public function update_category_delete($course_id)
  539. {
  540. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  541. $sql = 'UPDATE '.$tbl_grade_categories.' SET visible=3 WHERE course_code ="'.Database::escape_string($course_id).'"';
  542. Database::query($sql);
  543. }
  544. /**
  545. * Show message resource delete
  546. */
  547. public function show_message_resource_delete($course_id)
  548. {
  549. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  550. $sql = 'SELECT count(*) AS num from '.$tbl_grade_categories.'
  551. WHERE
  552. course_code = "'.Database::escape_string($course_id).'" AND
  553. visible=3';
  554. $res = Database::query($sql);
  555. $option = Database::fetch_array($res, 'ASSOC');
  556. if ($option['num']>=1) {
  557. return '&nbsp;&nbsp;<span class="resource-deleted">(&nbsp;'.get_lang('ResourceDeleted').'&nbsp;)</span>';
  558. } else {
  559. return false;
  560. }
  561. }
  562. /**
  563. * Shows all information of an category
  564. */
  565. public function shows_all_information_an_category($selectcat = '')
  566. {
  567. if ($selectcat == '') {
  568. return null;
  569. } else {
  570. $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  571. $sql='SELECT name,description,user_id,course_code,parent_id,weight,visible,certif_min_score,session_id FROM '.$tbl_category.' c WHERE c.id='.intval($selectcat);
  572. $result = Database::query($sql);
  573. $row = Database::fetch_array($result, 'ASSOC');
  574. return $row;
  575. }
  576. }
  577. /**
  578. * Check if a category name (with the same parent category) already exists
  579. * @param $name name to check (if not given, the name property of this object will be checked)
  580. * @param $parent parent category
  581. */
  582. public function does_name_exist($name, $parent)
  583. {
  584. if (!isset ($name)) {
  585. $name = $this->name;
  586. $parent = $this->parent;
  587. }
  588. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  589. $sql = 'SELECT count(id) AS number'
  590. .' FROM '.$tbl_grade_categories
  591. ." WHERE name = '".Database::escape_string($name)."'";
  592. if (api_is_allowed_to_edit()) {
  593. $parent = Category::load($parent);
  594. $courseId = $parent[0]->get_course_id();
  595. if (isset($courseId) && !empty($courseId)) {
  596. $main_course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  597. $sql .= ' AND user_id IN ('
  598. .' SELECT user_id FROM '.$main_course_user_table
  599. ." WHERE c_id = '".Database::escape_string($courseId)."'"
  600. .' AND status = '.COURSEMANAGER
  601. .')';
  602. } else {
  603. $sql .= ' AND user_id = '.api_get_user_id();
  604. }
  605. } else {
  606. $sql .= ' AND user_id = '.api_get_user_id();
  607. }
  608. if (!isset ($parent)) {
  609. $sql .= ' AND parent_id is null';
  610. } else {
  611. $sql .= ' AND parent_id = '.intval($parent);
  612. }
  613. $result = Database::query($sql);
  614. $number = Database::fetch_row($result);
  615. return ($number[0] != 0);
  616. }
  617. /**
  618. * Checks if the certificate is available for the given user in this category
  619. * @param integer User ID
  620. * @return boolean True if conditions match, false if fails
  621. */
  622. public function is_certificate_available($user_id)
  623. {
  624. $score = $this->calc_score($user_id, $this->course_code);
  625. if (isset($score)) {
  626. $certification_score = ($score[0] / $score[1]) * 100; //get a percentage score to compare to minimum certificate score
  627. if ($certification_score >= $this->certificate_min_score) {
  628. return true;
  629. }
  630. }
  631. return false;
  632. }
  633. /**
  634. * Is this category a course ?
  635. * A category is a course if it has a course code and no parent category.
  636. */
  637. public function is_course()
  638. {
  639. return (isset($this->course_code) && !empty($this->course_code)
  640. && (!isset($this->parent) || $this->parent == 0));
  641. }
  642. /**
  643. * Calculate the score of this category
  644. * @param $stud_id student id (default: all students - then the average is returned)
  645. * @return array (score sum, weight sum)
  646. * or null if no scores available
  647. */
  648. public function calc_score($stud_id = null, $course_code = '', $session_id = null)
  649. {
  650. // get appropriate subcategories, evaluations and links
  651. if (!empty($course_code)) {
  652. $cats = $this->get_subcategories($stud_id, $course_code, $session_id);
  653. $evals = $this->get_evaluations($stud_id, false, $course_code);
  654. $links = $this->get_links($stud_id, false, $course_code);
  655. } else {
  656. $cats = $this->get_subcategories($stud_id);
  657. $evals = $this->get_evaluations($stud_id);
  658. $links = $this->get_links($stud_id);
  659. }
  660. // calculate score
  661. $rescount = 0;
  662. $ressum = 0;
  663. $weightsum = 0;
  664. if (!empty($cats)) {
  665. /** @var Category $cat */
  666. foreach ($cats as $cat) {
  667. $catres = $cat->calc_score($stud_id, $course_code, $session_id);
  668. if ($cat->get_weight() != 0) {
  669. $catweight = $cat->get_weight();
  670. $rescount++;
  671. $weightsum += $catweight;
  672. }
  673. if (isset($catres)) {
  674. $ressum += (($catres[0]/$catres[1]) * $catweight);
  675. }
  676. }
  677. }
  678. if (!empty($evals)) {
  679. /** @var Evaluation $eval */
  680. foreach ($evals as $eval) {
  681. $evalres = $eval->calc_score($stud_id);
  682. if (isset($evalres) && $eval->get_weight() != 0) {
  683. $evalweight = $eval->get_weight();
  684. $weightsum += $evalweight;
  685. $rescount++;
  686. $ressum += (($evalres[0]/$evalres[1]) * $evalweight);
  687. } else {
  688. if ($eval->get_weight() != 0) {
  689. $evalweight = $eval->get_weight();
  690. $weightsum += $evalweight;
  691. }
  692. }
  693. }
  694. }
  695. if (!empty($links)) {
  696. /** @var EvalLink $link */
  697. foreach ($links as $link) {
  698. $linkres = $link->calc_score($stud_id);
  699. if (isset($linkres) && $link->get_weight() != 0) {
  700. $linkweight = $link->get_weight();
  701. $link_res_denom = ($linkres[1] == 0) ? 1 : $linkres[1];
  702. $rescount++;
  703. $weightsum += $linkweight;
  704. $ressum += (($linkres[0] / $link_res_denom) * $linkweight);
  705. } else {
  706. //adding if result does not exists
  707. if ($link->get_weight() != 0) {
  708. $linkweight = $link->get_weight();
  709. $weightsum += $linkweight;
  710. }
  711. }
  712. }
  713. }
  714. if ($rescount == 0) {
  715. return null;
  716. } else {
  717. return array($ressum, $weightsum);
  718. }
  719. }
  720. /**
  721. * Delete this category and every subcategory, evaluation and result inside
  722. */
  723. public function delete_all()
  724. {
  725. $cats = Category::load(null, null, $this->course_code, $this->id, null);
  726. $evals = Evaluation::load(null, null, $this->course_code, $this->id, null);
  727. $links = LinkFactory::load(null, null, null, null, $this->course_code, $this->id, null);
  728. if (!empty($cats)) {
  729. foreach ($cats as $cat) {
  730. $cat->delete_all();
  731. $cat->delete();
  732. }
  733. }
  734. if (!empty($evals)) {
  735. foreach ($evals as $eval) {
  736. $eval->delete_with_results();
  737. }
  738. }
  739. if (!empty($links)) {
  740. foreach ($links as $link) {
  741. $link->delete();
  742. }
  743. }
  744. $this->delete();
  745. }
  746. /**
  747. * Return array of Category objects where a student is subscribed to.
  748. * @param int student id
  749. * @param string Course code
  750. * @param int Session id
  751. */
  752. public function get_root_categories_for_student($stud_id, $course_code = null, $session_id = null)
  753. {
  754. $main_course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  755. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  756. $sql = 'SELECT *'
  757. .' FROM '.$tbl_grade_categories
  758. .' WHERE parent_id = 0';
  759. if (!api_is_allowed_to_edit()) {
  760. $sql .= ' AND visible = 1';
  761. //proceed with checks on optional parameters course & session
  762. if (!empty($course_code)) {
  763. // TODO: considering it highly improbable that a user would get here
  764. // if he doesn't have the rights to view this course and this
  765. // session, we don't check his registration to these, but this
  766. // could be an improvement
  767. if (!empty($session_id)) {
  768. $sql .= " AND course_code = '".Database::escape_string($course_code)."'"
  769. ." AND session_id = ".(int)$session_id;
  770. } else {
  771. $sql .= " AND course_code = '".Database::escape_string($course_code)."' AND session_id is null OR session_id=0";
  772. }
  773. } else {
  774. //no optional parameter, proceed as usual
  775. $sql .= ' AND course_code in'
  776. .' (SELECT course_code'
  777. .' FROM '.$main_course_user_table
  778. .' WHERE user_id = '.intval($stud_id)
  779. .' AND status = '.STUDENT
  780. .')';
  781. }
  782. } elseif (api_is_allowed_to_edit() && !api_is_platform_admin()) {
  783. //proceed with checks on optional parameters course & session
  784. if (!empty($course_code)) {
  785. // TODO: considering it highly improbable that a user would get here
  786. // if he doesn't have the rights to view this course and this
  787. // session, we don't check his registration to these, but this
  788. // could be an improvement
  789. $sql .= " AND course_code = '".Database::escape_string($course_code)."'";
  790. if (!empty($session_id)) {
  791. $sql .= " AND session_id = ".(int)$session_id;
  792. } else {
  793. $sql .= "AND session_id IS NULL OR session_id=0";
  794. }
  795. } else {
  796. $sql .= ' AND course_code in'
  797. .' (SELECT course_code'
  798. .' FROM '.$main_course_user_table
  799. .' WHERE user_id = '.api_get_user_id()
  800. .' AND status = '.COURSEMANAGER
  801. .')';
  802. }
  803. } elseif (api_is_platform_admin()) {
  804. if (isset($session_id) && $session_id != 0) {
  805. $sql .= ' AND session_id='.intval($session_id);
  806. } else {
  807. $sql .= ' AND coalesce(session_id,0)=0';
  808. }
  809. }
  810. $result = Database::query($sql);
  811. $cats = Category::create_category_objects_from_sql_result($result);
  812. // Course independent categories
  813. if (empty($course_code)) {
  814. $cats = Category::get_independent_categories_with_result_for_student(0, $stud_id, $cats);
  815. }
  816. return $cats;
  817. }
  818. /**
  819. * Return array of Category objects where a teacher is admin for.
  820. * @param int user id (to return everything, use 'null' here)
  821. * @param string course code (optional)
  822. * @param int session id (optional)
  823. */
  824. public function get_root_categories_for_teacher($user_id, $course_code = null, $session_id = null)
  825. {
  826. if ($user_id == null) {
  827. return Category::load(null, null, $course_code, 0, null, $session_id);
  828. }
  829. $main_course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  830. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  831. $sql = 'SELECT *'
  832. .' FROM '.$tbl_grade_categories
  833. .' WHERE parent_id = 0';
  834. if (!empty($course_code)) {
  835. $sql .= " AND course_code = '".Database::escape_string($course_code)."' ";
  836. if (!empty($session_id)) {
  837. $sql .= " AND session_id = ".(int)$session_id;
  838. }
  839. } else {
  840. $sql .= ' AND course_code in'
  841. .' (SELECT course_code'
  842. .' FROM '.$main_course_user_table
  843. .' WHERE user_id = '.intval($user_id)
  844. .')';
  845. }
  846. $result = Database::query($sql);
  847. $cats = Category::create_category_objects_from_sql_result($result);
  848. // course independent categories
  849. if (isset($course_code)) {
  850. $indcats = Category::load(null, $user_id, $course_code, 0, null, $session_id);
  851. $cats = array_merge($cats, $indcats);
  852. }
  853. return $cats;
  854. }
  855. /**
  856. * Can this category be moved to somewhere else ?
  857. * The root and courses cannot be moved.
  858. */
  859. public function is_movable()
  860. {
  861. return (!(!isset ($this->id) || $this->id == 0 || $this->is_course()));
  862. }
  863. /**
  864. * Generate an array of possible categories where this category can be moved to.
  865. * Notice: its own parent will be included in the list: it's up to the frontend
  866. * to disable this element.
  867. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  868. */
  869. public function get_target_categories()
  870. {
  871. // the root or a course -> not movable
  872. if (!$this->is_movable()) {
  873. return null;
  874. } else {
  875. // - course independent category
  876. // -> movable to root or other independent categories
  877. // - category inside a course
  878. // -> movable to root, independent categories or categories inside the course
  879. $user = (api_is_platform_admin() ? null : api_get_user_id());
  880. $targets = array();
  881. $level = 0;
  882. $root = array(0, get_lang('RootCat'), $level);
  883. $targets[] = $root;
  884. if (isset($this->course_code) && !empty($this->course_code)) {
  885. $crscats = Category::load(null, null, $this->course_code, 0);
  886. foreach ($crscats as $cat) {
  887. if ($this->can_be_moved_to_cat($cat)) {
  888. $targets[] = array($cat->get_id(), $cat->get_name(), $level + 1);
  889. $targets = $this->add_target_subcategories($targets, $level + 1, $cat->get_id());
  890. }
  891. }
  892. }
  893. $indcats = Category::load(null, $user, 0, 0);
  894. foreach ($indcats as $cat) {
  895. if ($this->can_be_moved_to_cat($cat)) {
  896. $targets[] = array($cat->get_id(), $cat->get_name(), $level + 1);
  897. $targets = $this->add_target_subcategories($targets, $level + 1, $cat->get_id());
  898. }
  899. }
  900. return $targets;
  901. }
  902. }
  903. /**
  904. * Internal function used by get_target_categories()
  905. */
  906. private function add_target_subcategories($targets, $level, $catid)
  907. {
  908. $subcats = Category::load(null, null, null, $catid);
  909. foreach ($subcats as $cat) {
  910. if ($this->can_be_moved_to_cat($cat)) {
  911. $targets[] = array($cat->get_id(), $cat->get_name(), $level + 1);
  912. $targets = $this->add_target_subcategories($targets, $level + 1, $cat->get_id());
  913. }
  914. }
  915. return $targets;
  916. }
  917. /**
  918. * Internal function used by get_target_categories() and add_target_subcategories()
  919. * Can this category be moved to the given category ?
  920. * Impossible when origin and target are the same... children won't be processed
  921. * either. (a category can't be moved to one of its own children)
  922. */
  923. private function can_be_moved_to_cat($cat)
  924. {
  925. return ($cat->get_id() != $this->get_id());
  926. }
  927. /**
  928. * Move this category to the given category.
  929. * If this category moves from inside a course to outside,
  930. * its course code must be changed, as well as the course code
  931. * of all underlying categories and evaluations. All links will
  932. * be deleted as well !
  933. */
  934. public function move_to_cat($cat)
  935. {
  936. $this->set_parent_id($cat->get_id());
  937. if ($this->get_course_code() != $cat->get_course_code()) {
  938. $this->set_course_code($cat->get_course_code());
  939. $this->apply_course_code_to_children();
  940. }
  941. $this->save();
  942. }
  943. /**
  944. * Internal function used by move_to_cat()
  945. */
  946. private function apply_course_code_to_children()
  947. {
  948. $cats = Category::load(null, null, null, $this->id, null);
  949. $evals = Evaluation::load(null, null, null, $this->id, null);
  950. $links = LinkFactory::load(null, null, null, null, null, $this->id, null);
  951. foreach ($cats as $cat) {
  952. $cat->set_course_code($this->get_course_code());
  953. $cat->save();
  954. $cat->apply_course_code_to_children();
  955. }
  956. foreach ($evals as $eval) {
  957. $eval->set_course_code($this->get_course_code());
  958. $eval->save();
  959. }
  960. foreach ($links as $link) {
  961. $link->delete();
  962. }
  963. }
  964. /**
  965. * Generate an array of all categories the user can navigate to
  966. */
  967. public function get_tree()
  968. {
  969. $targets = array();
  970. $level = 0;
  971. $root = array(0, get_lang('RootCat'), $level);
  972. $targets[] = $root;
  973. // course or platform admin
  974. if (api_is_allowed_to_edit()) {
  975. $user = (api_is_platform_admin() ? null : api_get_user_id());
  976. $cats = Category::get_root_categories_for_teacher($user);
  977. foreach ($cats as $cat) {
  978. $targets[] = array($cat->get_id(), $cat->get_name(), $level + 1);
  979. $targets = Category::add_subtree($targets, $level + 1, $cat->get_id(), null);
  980. }
  981. } else { // student
  982. $cats = Category::get_root_categories_for_student(api_get_user_id());
  983. foreach ($cats as $cat) {
  984. $targets[] = array($cat->get_id(), $cat->get_name(), $level + 1);
  985. $targets = Category::add_subtree($targets, $level + 1, $cat->get_id(), 1);
  986. }
  987. }
  988. return $targets;
  989. }
  990. /**
  991. * Internal function used by get_tree()
  992. */
  993. private function add_subtree($targets, $level, $catid, $visible)
  994. {
  995. $subcats = Category::load(null, null, null, $catid, $visible);
  996. if (!empty($subcats)) {
  997. foreach ($subcats as $cat) {
  998. $targets[] = array($cat->get_id(), $cat->get_name(), $level + 1);
  999. $targets = Category::add_subtree($targets, $level + 1, $cat->get_id(), $visible);
  1000. }
  1001. }
  1002. return $targets;
  1003. }
  1004. /**
  1005. * Generate an array of courses that a teacher hasn't created a category for.
  1006. * @return array 2-dimensional array - every element contains 2 subelements (code, title)
  1007. */
  1008. public function get_not_created_course_categories($user_id)
  1009. {
  1010. $tbl_main_courses = Database :: get_main_table(TABLE_MAIN_COURSE);
  1011. $tbl_main_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1012. $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1013. $sql = 'SELECT DISTINCT(code), title
  1014. FROM '.$tbl_main_courses.' cc, '.$tbl_main_course_user.' cu
  1015. WHERE cc.c_id = cu.id AND cu.status = '.COURSEMANAGER;
  1016. if (!api_is_platform_admin()) {
  1017. $sql .= ' AND cu.user_id = '.$user_id;
  1018. }
  1019. $sql .= ' AND cc.code NOT IN
  1020. (
  1021. SELECT course_code FROM '.$tbl_grade_categories.'
  1022. WHERE parent_id = 0 AND course_code IS NOT null
  1023. )';
  1024. $result = Database::query($sql);
  1025. $cats = array();
  1026. while ($data = Database::fetch_array($result)) {
  1027. $cats[] = array($data['code'], $data['title']);
  1028. }
  1029. return $cats;
  1030. }
  1031. /**
  1032. * Generate an array of all courses that a teacher is admin of.
  1033. * @return array 2-dimensional array - every element contains 2 subelements (code, title)
  1034. */
  1035. public function get_all_courses($user_id)
  1036. {
  1037. $tbl_main_courses = Database :: get_main_table(TABLE_MAIN_COURSE);
  1038. $tbl_main_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1039. $sql = 'SELECT DISTINCT(code), title FROM '.$tbl_main_courses.' cc, '.$tbl_main_course_user.' cu'
  1040. .' WHERE cc.id = cu.c_id'
  1041. .' AND cu.status = '.COURSEMANAGER;
  1042. if (!api_is_platform_admin()) {
  1043. $sql .= ' AND cu.user_id = '.intval($user_id);
  1044. }
  1045. $result = Database::query($sql);
  1046. $cats = array();
  1047. while ($data = Database::fetch_array($result)) {
  1048. $cats[] = array($data['code'], $data['title']);
  1049. }
  1050. return $cats;
  1051. }
  1052. /**
  1053. * Apply the same visibility to every subcategory, evaluation and link
  1054. */
  1055. public function apply_visibility_to_children()
  1056. {
  1057. $cats = Category::load(null, null, null, $this->id, null);
  1058. $evals = Evaluation::load(null, null, null, $this->id, null);
  1059. $links = LinkFactory::load(null, null, null, null, null, $this->id, null);
  1060. if (!empty($cats)) {
  1061. foreach ($cats as $cat) {
  1062. $cat->set_visible($this->is_visible());
  1063. $cat->save();
  1064. $cat->apply_visibility_to_children();
  1065. }
  1066. }
  1067. if (!empty($evals)) {
  1068. foreach ($evals as $eval) {
  1069. $eval->set_visible($this->is_visible());
  1070. $eval->save();
  1071. }
  1072. }
  1073. if (!empty($links)) {
  1074. foreach ($links as $link) {
  1075. $link->set_visible($this->is_visible());
  1076. $link->save();
  1077. }
  1078. }
  1079. }
  1080. /**
  1081. * Check if a category contains evaluations with a result for a given student
  1082. */
  1083. public function has_evaluations_with_results_for_student($stud_id)
  1084. {
  1085. $evals = Evaluation::get_evaluations_with_result_for_student($this->id, $stud_id);
  1086. if (count($evals) != 0) {
  1087. return true;
  1088. } else {
  1089. $cats = Category::load(
  1090. null,
  1091. null,
  1092. null,
  1093. $this->id,
  1094. api_is_allowed_to_edit() ? null : 1
  1095. );
  1096. foreach ($cats as $cat) {
  1097. if ($cat->has_evaluations_with_results_for_student($stud_id)) {
  1098. return true;
  1099. }
  1100. }
  1101. return false;
  1102. }
  1103. }
  1104. /**
  1105. * Retrieve all categories inside a course independent category
  1106. * that should be visible to a student.
  1107. * @param $cat_id parent category
  1108. * @param $stud_id student id
  1109. * @param $cats optional: if defined, the categories will be added to this array
  1110. */
  1111. public function get_independent_categories_with_result_for_student($cat_id, $stud_id, $cats = array())
  1112. {
  1113. $creator = (api_is_allowed_to_edit() && !api_is_platform_admin()) ? api_get_user_id() : null;
  1114. $crsindcats = Category::load(
  1115. null,
  1116. $creator,
  1117. '0',
  1118. $cat_id,
  1119. api_is_allowed_to_edit() ? null : 1
  1120. );
  1121. if (!empty($crsindcats)) {
  1122. foreach ($crsindcats as $crsindcat) {
  1123. if ($crsindcat->has_evaluations_with_results_for_student($stud_id)) {
  1124. $cats[] = $crsindcat;
  1125. }
  1126. }
  1127. }
  1128. return $cats;
  1129. }
  1130. /**
  1131. * Return the session id (in any case, even if it's null or 0)
  1132. * @return int Session id (can be null)
  1133. */
  1134. public function get_session_id()
  1135. {
  1136. return $this->session_id;
  1137. }
  1138. /**
  1139. * Get appropriate subcategories visible for the user (and optionally the course and session)
  1140. * @param int $stud_id student id (default: all students)
  1141. * @param string Course code (optional)
  1142. * @param int Session ID (optional)
  1143. *
  1144. * @return array Array of subcategories
  1145. */
  1146. public function get_subcategories($stud_id = null, $course_code = null, $session_id = null, $order = null)
  1147. {
  1148. if (!empty($session_id)) {
  1149. /*$tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1150. $sql = 'SELECT id FROM '.$tbl_grade_categories. ' WHERE session_id = '.$session_id;
  1151. $result_session = Database::query($sql);
  1152. if (Database::num_rows($result_session) > 0) {
  1153. $data_session = Database::fetch_array($result_session);
  1154. $parent_id = $data_session['id'];
  1155. return Category::load(null, null, null, $parent_id, null, null, $order);
  1156. }*/
  1157. }
  1158. // 1 student
  1159. if (isset($stud_id)) {
  1160. // special case: this is the root
  1161. if ($this->id == 0) {
  1162. return Category::get_root_categories_for_student($stud_id, $course_code, $session_id);
  1163. } else {
  1164. return Category::load(null,null, $course_code, $this->id, api_is_allowed_to_edit() ? null : 1, $session_id, $order);
  1165. }
  1166. } else { // all students
  1167. // course admin
  1168. if (api_is_allowed_to_edit() && !api_is_platform_admin()) {
  1169. // root
  1170. if ($this->id == 0) {
  1171. return $this->get_root_categories_for_teacher(api_get_user_id(), $course_code, $session_id, false);
  1172. // inside a course
  1173. } elseif (!empty($this->course_code)) {
  1174. return Category::load(null, null, $this->course_code, $this->id, null, $session_id, $order);
  1175. } elseif (!empty($course_code)) {
  1176. return Category::load(null, null, $course_code, $this->id, null, $session_id, $order);
  1177. // course independent
  1178. } else {
  1179. return Category::load(null, api_get_user_id(), 0, $this->id, null);
  1180. }
  1181. } elseif (api_is_platform_admin()) {
  1182. // platform admin
  1183. //we explicitly avoid listing subcats from another session
  1184. return Category::load(null, null, $course_code, $this->id, null, $session_id, $order);
  1185. }
  1186. }
  1187. return array();
  1188. }
  1189. /**
  1190. * Get appropriate evaluations visible for the user
  1191. * @param int $stud_id student id (default: all students)
  1192. * @param boolean $recursive process subcategories (default: no recursion)
  1193. * @param string $course_code
  1194. *
  1195. * @return array
  1196. */
  1197. public function get_evaluations($stud_id = null, $recursive = false, $course_code = '')
  1198. {
  1199. $evals = array();
  1200. if (empty($course_code)) {
  1201. $course_code = api_get_course_id();
  1202. }
  1203. // 1 student
  1204. if (isset($stud_id) && !empty($stud_id)) {
  1205. // special case: this is the root
  1206. if ($this->id == 0) {
  1207. $evals = Evaluation::get_evaluations_with_result_for_student(0, $stud_id);
  1208. } else {
  1209. $evals = Evaluation::load(null, null, $course_code, $this->id, api_is_allowed_to_edit() ? null : 1);
  1210. }
  1211. } else { // all students
  1212. // course admin
  1213. if ((api_is_allowed_to_edit() || api_is_drh() || api_is_session_admin()) && !api_is_platform_admin()) {
  1214. // root
  1215. if ($this->id == 0) {
  1216. $evals = Evaluation::load(null, api_get_user_id(), null, $this->id, null);
  1217. } elseif (isset($this->course_code) && !empty($this->course_code)) {
  1218. // inside a course
  1219. $evals = Evaluation::load(null, null, $course_code, $this->id, null);
  1220. } else {
  1221. // course independent
  1222. $evals = Evaluation::load(null, api_get_user_id(), null, $this->id, null);
  1223. }
  1224. } elseif (api_is_platform_admin()) {
  1225. //platform admin
  1226. $evals = Evaluation::load(null, null, $course_code, $this->id, null);
  1227. }
  1228. }
  1229. if ($recursive) {
  1230. $subcats = $this->get_subcategories($stud_id, $course_code);
  1231. if (!empty($subcats)) {
  1232. foreach ($subcats as $subcat) {
  1233. $subevals = $subcat->get_evaluations($stud_id, true, $course_code);
  1234. //$this->debugprint($subevals);
  1235. $evals = array_merge($evals, $subevals);
  1236. }
  1237. }
  1238. }
  1239. return $evals;
  1240. }
  1241. /**
  1242. * Get appropriate links visible for the user
  1243. * @param int $stud_id student id (default: all students)
  1244. * @param boolean $recursive process subcategories (default: no recursion)
  1245. * @param string $course_code
  1246. * @param int $sessionId
  1247. *
  1248. * @return array
  1249. */
  1250. public function get_links($stud_id = null, $recursive = false, $course_code = '', $sessionId = null)
  1251. {
  1252. $links = array();
  1253. if (empty($course_code)) {
  1254. $course_code = api_get_course_id();
  1255. }
  1256. if (empty($sessionId)) {
  1257. $sessionId = api_get_session_id();
  1258. }
  1259. // no links in root or course independent categories
  1260. if ($this->id == 0) {
  1261. } elseif (isset($stud_id)) {
  1262. // 1 student $stud_id
  1263. $links = LinkFactory::load(
  1264. null,
  1265. null,
  1266. null,
  1267. null,
  1268. empty($this->course_code) ? null : $course_code,
  1269. $this->id,
  1270. api_is_allowed_to_edit() ? null : 1
  1271. );
  1272. } elseif (api_is_allowed_to_edit() || api_is_drh() || api_is_session_admin()) {
  1273. // all students -> only for course/platform admin
  1274. $links = LinkFactory::load(
  1275. null,
  1276. null,
  1277. null,
  1278. null,
  1279. empty($this->course_code) ? null : $this->course_code,
  1280. $this->id,
  1281. null
  1282. );
  1283. }
  1284. if ($recursive) {
  1285. $subcats = $this->get_subcategories(
  1286. $stud_id,
  1287. $course_code,
  1288. $sessionId
  1289. );
  1290. if (!empty($subcats)) {
  1291. foreach ($subcats as $subcat) {
  1292. $sublinks = $subcat->get_links($stud_id, false, $course_code, $sessionId);
  1293. $links = array_merge($links, $sublinks);
  1294. }
  1295. }
  1296. }
  1297. return $links;
  1298. }
  1299. /**
  1300. * Get all the categories from with the same given direct parent
  1301. * @param int $catId Category parent ID
  1302. * @return array Array of Category objects
  1303. */
  1304. public function getCategories($catId)
  1305. {
  1306. $tblGradeCategories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1307. $courseInfo = api_get_course_info(api_get_course_id());
  1308. $courseCode = $courseInfo['code'];
  1309. $sql='SELECT * FROM '.$tblGradeCategories.' WHERE parent_id = '.intval($catId);
  1310. $result = Database::query($sql);
  1311. $allcats = Category::create_category_objects_from_sql_result($result);
  1312. return $allcats;
  1313. }
  1314. /**
  1315. * Gets the type for the current object
  1316. * @return string 'C' to represent "Category" object type
  1317. */
  1318. public function get_item_type()
  1319. {
  1320. return 'C';
  1321. }
  1322. /**
  1323. * @param array $skills
  1324. */
  1325. public function set_skills($skills)
  1326. {
  1327. $this->skills = $skills;
  1328. }
  1329. /**
  1330. * @return null
  1331. */
  1332. public function get_date()
  1333. {
  1334. return null;
  1335. }
  1336. /**
  1337. * @return string
  1338. */
  1339. public function get_icon_name()
  1340. {
  1341. return 'cat';
  1342. }
  1343. /**
  1344. * Find category by name
  1345. * @param string $name_mask search string
  1346. * @return array category objects matching the search criterium
  1347. */
  1348. public function find_category($name_mask, $allcat)
  1349. {
  1350. $foundcats = array();
  1351. foreach ($allcat as $search_cat) {
  1352. if (!(strpos(strtolower($search_cat->get_name()), strtolower($name_mask)) === false)) {
  1353. $foundcats[] = $search_cat;
  1354. }
  1355. }
  1356. return $foundcats;
  1357. }
  1358. /**
  1359. * This function, locks a category , only one who can unlock it is the platform administrator.
  1360. * @param int locked 1 or unlocked 0
  1361. * @return bool
  1362. *
  1363. * */
  1364. public function lock($locked)
  1365. {
  1366. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1367. $sql = "UPDATE $table SET locked = '".intval($locked)."' WHERE id='".intval($this->id)."'";
  1368. Database::query($sql);
  1369. }
  1370. /**
  1371. * @param $locked
  1372. */
  1373. public function lock_all_items($locked)
  1374. {
  1375. if (api_get_setting('gradebook_locking_enabled') == 'true') {
  1376. $this->lock($locked);
  1377. $evals_to_lock = $this->get_evaluations();
  1378. if (!empty($evals_to_lock)) {
  1379. foreach ($evals_to_lock as $item) {
  1380. $item->lock($locked);
  1381. }
  1382. }
  1383. $link_to_lock = $this->get_links();
  1384. if (!empty($link_to_lock)) {
  1385. foreach ($link_to_lock as $item) {
  1386. $item->lock($locked);
  1387. }
  1388. }
  1389. $event_type = LOG_GRADEBOOK_UNLOCKED;
  1390. if ($locked == 1) {
  1391. $event_type = LOG_GRADEBOOK_LOCKED;
  1392. }
  1393. Event::addEvent($event_type, LOG_GRADEBOOK_ID, $this->id);
  1394. }
  1395. }
  1396. /**
  1397. * @param int $category_id
  1398. * @param int $user_id
  1399. * @return bool|string
  1400. */
  1401. public static function register_user_certificate($category_id, $user_id)
  1402. {
  1403. // generating the total score for a course
  1404. $cats_course = Category::load($category_id, null, null, null, null, api_get_session_id(), false);
  1405. $alleval_course = $cats_course[0]->get_evaluations($user_id, true);
  1406. $alllink_course = $cats_course[0]->get_links($user_id, true);
  1407. $evals_links = array_merge($alleval_course, $alllink_course);
  1408. //@todo move these in a function
  1409. $sum_categories_weight_array = array();
  1410. if (isset($cats_course) && !empty($cats_course)) {
  1411. $categories = Category::load(null, null, null, $category_id);
  1412. if (!empty($categories)) {
  1413. foreach ($categories as $category) {
  1414. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  1415. }
  1416. } else {
  1417. $sum_categories_weight_array[$category_id] = $cats_course[0]->get_weight();
  1418. }
  1419. }
  1420. $main_weight = $cats_course[0]->get_weight();
  1421. $item_total_value = 0;
  1422. $item_value = 0;
  1423. for ($count=0; $count < count($evals_links); $count++) {
  1424. $item = $evals_links[$count];
  1425. $score = $item->calc_score($user_id);
  1426. $divide = ( ($score[1])==0 ) ? 1 : $score[1];
  1427. $item_value = $score[0]/$divide*$item->get_weight();
  1428. /*var_dump($score[0], $divide, $item->get_weight(), $item_value);
  1429. echo '---';-*/
  1430. $item_total_value += $item_value;
  1431. }
  1432. $item_total_value = (float)$item_total_value;
  1433. $cattotal = Category::load($category_id);
  1434. $scoretotal = $cattotal[0]->calc_score($user_id);
  1435. //Do not remove this the gradebook/lib/fe/gradebooktable.class.php file load this variable as a global
  1436. $scoredisplay = ScoreDisplay::instance();
  1437. $my_score_in_gradebook = $scoredisplay->display_score($scoretotal, SCORE_SIMPLE);
  1438. //Show certificate
  1439. $certificate_min_score = $cats_course[0]->get_certificate_min_score();
  1440. // A student always sees only the teacher's repartition
  1441. $scoretotal_display = $scoredisplay->display_score($scoretotal, SCORE_DIV_PERCENT);
  1442. if (isset($certificate_min_score) && $item_total_value >= $certificate_min_score) {
  1443. $my_certificate = get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
  1444. if (empty($my_certificate)) {
  1445. register_user_info_about_certificate($category_id, $user_id, $my_score_in_gradebook, api_get_utc_datetime());
  1446. $my_certificate = get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
  1447. }
  1448. if (!empty($my_certificate)) {
  1449. $certificate_obj = new Certificate($my_certificate['id']);
  1450. $fileWasGenerated = $certificate_obj->html_file_is_generated();
  1451. $html = null;
  1452. if (!empty($fileWasGenerated)) {
  1453. $url = api_get_path(WEB_PATH) . 'certificates/index.php?id=' . $my_certificate['id'];
  1454. $certificates = Display::url(
  1455. Display::return_icon(
  1456. 'certificate.png',
  1457. get_lang('Certificates'),
  1458. array(),
  1459. 32
  1460. ),
  1461. $url,
  1462. array('target' => '_blank')
  1463. );
  1464. $html = '<div class="actions" align="right">';
  1465. $html .= $certificates;
  1466. $html .= '</div>';
  1467. }
  1468. return $html;
  1469. }
  1470. } else {
  1471. return false;
  1472. }
  1473. }
  1474. }