category.class.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\GradebookCategory;
  4. use ChamiloSession as Session;
  5. /**
  6. * Class Category
  7. * Defines a gradebook Category object.
  8. *
  9. * @package chamilo.gradebook
  10. */
  11. class Category implements GradebookItem
  12. {
  13. public $studentList;
  14. public $evaluations;
  15. public $links;
  16. public $subCategories;
  17. /** @var GradebookCategory */
  18. public $entity;
  19. private $id;
  20. private $name;
  21. private $description;
  22. private $user_id;
  23. private $course_code;
  24. private $parent;
  25. private $weight;
  26. private $visible;
  27. private $certificate_min_score;
  28. private $session_id;
  29. private $skills = [];
  30. private $grade_model_id;
  31. private $generateCertificates;
  32. private $isRequirement;
  33. private $courseDependency;
  34. private $minimumToValidate;
  35. private $documentId;
  36. /** @var int */
  37. private $gradeBooksToValidateInDependence;
  38. /**
  39. * Consctructor.
  40. */
  41. public function __construct()
  42. {
  43. $this->id = 0;
  44. $this->name = null;
  45. $this->description = null;
  46. $this->user_id = 0;
  47. $this->course_code = null;
  48. $this->parent = 0;
  49. $this->weight = 0;
  50. $this->visible = false;
  51. $this->certificate_min_score = 0;
  52. $this->session_id = 0;
  53. $this->grade_model_id = 0;
  54. $this->generateCertificates = false;
  55. $this->isRequirement = false;
  56. $this->courseDependency = [];
  57. $this->documentId = 0;
  58. $this->minimumToValidate = null;
  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. /**
  82. * @return int
  83. */
  84. public function get_user_id()
  85. {
  86. return $this->user_id;
  87. }
  88. /**
  89. * @return int|null
  90. */
  91. public function getCertificateMinScore()
  92. {
  93. if (!empty($this->certificate_min_score)) {
  94. return $this->certificate_min_score;
  95. } else {
  96. return null;
  97. }
  98. }
  99. /**
  100. * @return string
  101. */
  102. public function get_course_code()
  103. {
  104. return $this->course_code;
  105. }
  106. /**
  107. * @return int
  108. */
  109. public function get_parent_id()
  110. {
  111. return $this->parent;
  112. }
  113. /**
  114. * @return int
  115. */
  116. public function get_weight()
  117. {
  118. return $this->weight;
  119. }
  120. /**
  121. * @return bool
  122. */
  123. public function is_locked()
  124. {
  125. return isset($this->locked) && $this->locked == 1 ? true : false;
  126. }
  127. /**
  128. * @return bool
  129. */
  130. public function is_visible()
  131. {
  132. return $this->visible;
  133. }
  134. /**
  135. * Get $isRequirement.
  136. *
  137. * @return int
  138. */
  139. public function getIsRequirement()
  140. {
  141. return $this->isRequirement;
  142. }
  143. /**
  144. * @param int $id
  145. */
  146. public function set_id($id)
  147. {
  148. $this->id = $id;
  149. }
  150. /**
  151. * @param string $name
  152. */
  153. public function set_name($name)
  154. {
  155. $this->name = $name;
  156. }
  157. /**
  158. * @param string $description
  159. */
  160. public function set_description($description)
  161. {
  162. $this->description = $description;
  163. }
  164. /**
  165. * @param int $user_id
  166. */
  167. public function set_user_id($user_id)
  168. {
  169. $this->user_id = $user_id;
  170. }
  171. /**
  172. * @param string $course_code
  173. */
  174. public function set_course_code($course_code)
  175. {
  176. $this->course_code = $course_code;
  177. }
  178. /**
  179. * @param float $min_score
  180. */
  181. public function set_certificate_min_score($min_score = null)
  182. {
  183. $this->certificate_min_score = $min_score;
  184. }
  185. /**
  186. * @param int $parent
  187. */
  188. public function set_parent_id($parent)
  189. {
  190. $this->parent = (int) $parent;
  191. }
  192. /**
  193. * Filters to int and sets the session ID.
  194. *
  195. * @param int The session ID from the Dokeos course session
  196. */
  197. public function set_session_id($session_id = 0)
  198. {
  199. $this->session_id = (int) $session_id;
  200. }
  201. /**
  202. * @param $weight
  203. */
  204. public function set_weight($weight)
  205. {
  206. $this->weight = $weight;
  207. }
  208. /**
  209. * @param $visible
  210. */
  211. public function set_visible($visible)
  212. {
  213. $this->visible = $visible;
  214. }
  215. /**
  216. * @param int $id
  217. */
  218. public function set_grade_model_id($id)
  219. {
  220. $this->grade_model_id = $id;
  221. }
  222. /**
  223. * @param $locked
  224. */
  225. public function set_locked($locked)
  226. {
  227. $this->locked = $locked;
  228. }
  229. /**
  230. * Set $isRequirement.
  231. *
  232. * @param int $isRequirement
  233. */
  234. public function setIsRequirement($isRequirement)
  235. {
  236. $this->isRequirement = $isRequirement;
  237. }
  238. /**
  239. * @param $value
  240. */
  241. public function setCourseListDependency($value)
  242. {
  243. $this->courseDependency = [];
  244. $unserialized = UnserializeApi::unserialize('not_allowed_classes', $value, true);
  245. if (false !== $unserialized) {
  246. $this->courseDependency = $unserialized;
  247. }
  248. }
  249. /**
  250. * Course id list.
  251. *
  252. * @return array
  253. */
  254. public function getCourseListDependency()
  255. {
  256. return $this->courseDependency;
  257. }
  258. /**
  259. * @param int $value
  260. */
  261. public function setMinimumToValidate($value)
  262. {
  263. $this->minimumToValidate = $value;
  264. }
  265. public function getMinimumToValidate()
  266. {
  267. return $this->minimumToValidate;
  268. }
  269. /**
  270. * @return int|null
  271. */
  272. public function get_grade_model_id()
  273. {
  274. if ($this->grade_model_id < 0) {
  275. return null;
  276. }
  277. return $this->grade_model_id;
  278. }
  279. /**
  280. * @return string
  281. */
  282. public function get_type()
  283. {
  284. return 'category';
  285. }
  286. /**
  287. * @param bool $from_db
  288. *
  289. * @return array|resource
  290. */
  291. public function get_skills($from_db = true)
  292. {
  293. if ($from_db) {
  294. $categoryId = $this->get_id();
  295. $gradebook = new Gradebook();
  296. $skills = $gradebook->getSkillsByGradebook($categoryId);
  297. } else {
  298. $skills = $this->skills;
  299. }
  300. return $skills;
  301. }
  302. /**
  303. * @return array
  304. */
  305. public function getSkillsForSelect()
  306. {
  307. $skills = $this->get_skills();
  308. $skill_select = [];
  309. if (!empty($skills)) {
  310. foreach ($skills as $skill) {
  311. $skill_select[$skill['id']] = $skill['name'];
  312. }
  313. }
  314. return $skill_select;
  315. }
  316. /**
  317. * Set the generate_certificates value.
  318. *
  319. * @param int $generateCertificates
  320. */
  321. public function setGenerateCertificates($generateCertificates)
  322. {
  323. $this->generateCertificates = $generateCertificates;
  324. }
  325. /**
  326. * Get the generate_certificates value.
  327. *
  328. * @return int
  329. */
  330. public function getGenerateCertificates()
  331. {
  332. return $this->generateCertificates;
  333. }
  334. /**
  335. * @param int $id
  336. * @param int $session_id
  337. *
  338. * @return array
  339. */
  340. public static function loadSessionCategories(
  341. $id = null,
  342. $session_id = null
  343. ) {
  344. if (isset($id) && (int) $id === 0) {
  345. $cats = [];
  346. $cats[] = self::create_root_category();
  347. return $cats;
  348. }
  349. $courseInfo = api_get_course_info_by_id(api_get_course_int_id());
  350. $courseCode = $courseInfo['code'];
  351. $session_id = (int) $session_id;
  352. if (!empty($session_id)) {
  353. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  354. $sql = 'SELECT id, course_code
  355. FROM '.$table.'
  356. WHERE session_id = '.$session_id;
  357. $result_session = Database::query($sql);
  358. if (Database::num_rows($result_session) > 0) {
  359. $categoryList = [];
  360. while ($data_session = Database::fetch_array($result_session)) {
  361. $parent_id = $data_session['id'];
  362. if ($data_session['course_code'] == $courseCode) {
  363. $categories = self::load($parent_id);
  364. $categoryList = array_merge($categoryList, $categories);
  365. }
  366. }
  367. return $categoryList;
  368. }
  369. }
  370. }
  371. /**
  372. * Retrieve categories and return them as an array of Category objects.
  373. *
  374. * @param int $id category id
  375. * @param int $user_id (category owner)
  376. * @param string $course_code
  377. * @param int $parent_id parent category
  378. * @param bool $visible
  379. * @param int $session_id (in case we are in a session)
  380. * @param bool $order_by Whether to show all "session"
  381. * categories (true) or hide them (false) in case there is no session id
  382. *
  383. * @return array
  384. */
  385. public static function load(
  386. $id = null,
  387. $user_id = null,
  388. $course_code = null,
  389. $parent_id = null,
  390. $visible = null,
  391. $session_id = null,
  392. $order_by = null
  393. ) {
  394. //if the category given is explicitly 0 (not null), then create
  395. // a root category object (in memory)
  396. if (isset($id) && (int) $id === 0) {
  397. $cats = [];
  398. $cats[] = self::create_root_category();
  399. return $cats;
  400. }
  401. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  402. $sql = 'SELECT * FROM '.$table;
  403. $paramcount = 0;
  404. if (isset($id)) {
  405. $sql .= ' WHERE id = '.intval($id);
  406. $paramcount++;
  407. }
  408. if (isset($user_id)) {
  409. $user_id = intval($user_id);
  410. if ($paramcount != 0) {
  411. $sql .= ' AND';
  412. } else {
  413. $sql .= ' WHERE';
  414. }
  415. $sql .= ' user_id = '.intval($user_id);
  416. $paramcount++;
  417. }
  418. if (isset($course_code)) {
  419. if ($paramcount != 0) {
  420. $sql .= ' AND';
  421. } else {
  422. $sql .= ' WHERE';
  423. }
  424. if ($course_code == '0') {
  425. $sql .= ' course_code is null ';
  426. } else {
  427. $sql .= " course_code = '".Database::escape_string($course_code)."'";
  428. }
  429. /*if ($show_session_categories !== true) {
  430. // a query on the course should show all
  431. // the categories inside sessions for this course
  432. // otherwise a special parameter is given to ask explicitely
  433. $sql .= " AND (session_id IS NULL OR session_id = 0) ";
  434. } else {*/
  435. if (empty($session_id)) {
  436. $sql .= ' AND (session_id IS NULL OR session_id = 0) ';
  437. } else {
  438. $sql .= ' AND session_id = '.(int) $session_id.' ';
  439. }
  440. //}
  441. $paramcount++;
  442. }
  443. if (isset($parent_id)) {
  444. if ($paramcount != 0) {
  445. $sql .= ' AND ';
  446. } else {
  447. $sql .= ' WHERE ';
  448. }
  449. $sql .= ' parent_id = '.intval($parent_id);
  450. $paramcount++;
  451. }
  452. if (isset($visible)) {
  453. if ($paramcount != 0) {
  454. $sql .= ' AND';
  455. } else {
  456. $sql .= ' WHERE';
  457. }
  458. $sql .= ' visible = '.intval($visible);
  459. }
  460. if (!empty($order_by)) {
  461. if (!empty($order_by) && $order_by != '') {
  462. $sql .= ' '.$order_by;
  463. }
  464. }
  465. $result = Database::query($sql);
  466. $categories = [];
  467. if (Database::num_rows($result) > 0) {
  468. $categories = self::create_category_objects_from_sql_result(
  469. $result
  470. );
  471. }
  472. return $categories;
  473. }
  474. /**
  475. * Create a category object from a GradebookCategory entity.
  476. *
  477. * @param GradebookCategory $gradebookCategory The entity
  478. *
  479. * @return \Category
  480. */
  481. public static function createCategoryObjectFromEntity(GradebookCategory $gradebookCategory)
  482. {
  483. $category = new Category();
  484. $category->set_id($gradebookCategory->getId());
  485. $category->set_name($gradebookCategory->getName());
  486. $category->set_description($gradebookCategory->getDescription());
  487. $category->set_user_id($gradebookCategory->getUserId());
  488. $category->set_course_code($gradebookCategory->getCourseCode());
  489. $category->set_parent_id($gradebookCategory->getParentId());
  490. $category->set_weight($gradebookCategory->getWeight());
  491. $category->set_visible($gradebookCategory->getVisible());
  492. $category->set_session_id($gradebookCategory->getSessionId());
  493. $category->set_certificate_min_score($gradebookCategory->getCertifMinScore());
  494. $category->set_grade_model_id($gradebookCategory->getGradeModelId());
  495. $category->set_locked($gradebookCategory->getLocked());
  496. $category->setGenerateCertificates($gradebookCategory->getGenerateCertificates());
  497. $category->setIsRequirement($gradebookCategory->getIsRequirement());
  498. return $category;
  499. }
  500. /**
  501. * Insert this category into the database.
  502. */
  503. public function add()
  504. {
  505. if (isset($this->name) && '-1' == $this->name) {
  506. return false;
  507. }
  508. if (isset($this->name) && isset($this->user_id)) {
  509. $em = Database::getManager();
  510. $category = new GradebookCategory();
  511. $category->setName($this->name);
  512. $category->setDescription($this->description);
  513. $category->setUserId($this->user_id);
  514. $category->setCourseCode($this->course_code);
  515. $category->setParentId($this->parent);
  516. $category->setWeight($this->weight);
  517. $category->setVisible($this->visible);
  518. $category->setCertifMinScore($this->certificate_min_score);
  519. $category->setSessionId($this->session_id);
  520. $category->setGenerateCertificates($this->generateCertificates);
  521. $category->setGradeModelId($this->grade_model_id);
  522. $category->setIsRequirement($this->isRequirement);
  523. $category->setLocked(false);
  524. $em->persist($category);
  525. $em->flush();
  526. $id = $category->getId();
  527. $this->set_id($id);
  528. if (!empty($id)) {
  529. $parent_id = $this->get_parent_id();
  530. $grade_model_id = $this->get_grade_model_id();
  531. if ($parent_id == 0) {
  532. //do something
  533. if (isset($grade_model_id) &&
  534. !empty($grade_model_id) &&
  535. $grade_model_id != '-1'
  536. ) {
  537. $obj = new GradeModel();
  538. $components = $obj->get_components($grade_model_id);
  539. $default_weight_setting = api_get_setting('gradebook_default_weight');
  540. $default_weight = 100;
  541. if (isset($default_weight_setting)) {
  542. $default_weight = $default_weight_setting;
  543. }
  544. foreach ($components as $component) {
  545. $gradebook = new Gradebook();
  546. $params = [];
  547. $params['name'] = $component['acronym'];
  548. $params['description'] = $component['title'];
  549. $params['user_id'] = api_get_user_id();
  550. $params['parent_id'] = $id;
  551. $params['weight'] = $component['percentage'] / 100 * $default_weight;
  552. $params['session_id'] = api_get_session_id();
  553. $params['course_code'] = $this->get_course_code();
  554. $gradebook->save($params);
  555. }
  556. }
  557. }
  558. }
  559. $gradebook = new Gradebook();
  560. $gradebook->updateSkillsToGradeBook(
  561. $this->id,
  562. $this->get_skills(false)
  563. );
  564. return $id;
  565. }
  566. }
  567. /**
  568. * Update the properties of this category in the database.
  569. *
  570. * @todo fix me
  571. */
  572. public function save()
  573. {
  574. $em = Database::getManager();
  575. $gradebookCategory = $em
  576. ->getRepository('ChamiloCoreBundle:GradebookCategory')
  577. ->find($this->id);
  578. if (empty($gradebookCategory)) {
  579. return false;
  580. }
  581. $gradebookCategory->setName($this->name);
  582. $gradebookCategory->setDescription($this->description);
  583. $gradebookCategory->setUserId($this->user_id);
  584. $gradebookCategory->setCourseCode($this->course_code);
  585. $gradebookCategory->setParentId($this->parent);
  586. $gradebookCategory->setWeight($this->weight);
  587. $gradebookCategory->setVisible($this->visible);
  588. $gradebookCategory->setCertifMinScore($this->certificate_min_score);
  589. $gradebookCategory->setGenerateCertificates(
  590. $this->generateCertificates
  591. );
  592. $gradebookCategory->setGradeModelId($this->grade_model_id);
  593. $gradebookCategory->setIsRequirement($this->isRequirement);
  594. $em->merge($gradebookCategory);
  595. $em->flush();
  596. if (!empty($this->id)) {
  597. $parent_id = $this->get_parent_id();
  598. $grade_model_id = $this->get_grade_model_id();
  599. if ($parent_id == 0) {
  600. if (isset($grade_model_id) &&
  601. !empty($grade_model_id) &&
  602. $grade_model_id != '-1'
  603. ) {
  604. $obj = new GradeModel();
  605. $components = $obj->get_components($grade_model_id);
  606. $default_weight_setting = api_get_setting('gradebook_default_weight');
  607. $default_weight = 100;
  608. if (isset($default_weight_setting)) {
  609. $default_weight = $default_weight_setting;
  610. }
  611. $final_weight = $this->get_weight();
  612. if (!empty($final_weight)) {
  613. $default_weight = $this->get_weight();
  614. }
  615. foreach ($components as $component) {
  616. $gradebook = new Gradebook();
  617. $params = [];
  618. $params['name'] = $component['acronym'];
  619. $params['description'] = $component['title'];
  620. $params['user_id'] = api_get_user_id();
  621. $params['parent_id'] = $this->id;
  622. $params['weight'] = $component['percentage'] / 100 * $default_weight;
  623. $params['session_id'] = api_get_session_id();
  624. $params['course_code'] = $this->get_course_code();
  625. $gradebook->save($params);
  626. }
  627. }
  628. }
  629. }
  630. $gradebook = new Gradebook();
  631. $gradebook->updateSkillsToGradeBook(
  632. $this->id,
  633. $this->get_skills(false),
  634. true
  635. );
  636. }
  637. /**
  638. * Update link weights see #5168.
  639. *
  640. * @param type $new_weight
  641. */
  642. public function updateChildrenWeight($new_weight)
  643. {
  644. $links = $this->get_links();
  645. $old_weight = $this->get_weight();
  646. if (!empty($links)) {
  647. foreach ($links as $link_item) {
  648. if (isset($link_item)) {
  649. $new_item_weight = $new_weight * $link_item->get_weight() / $old_weight;
  650. $link_item->set_weight($new_item_weight);
  651. $link_item->save();
  652. }
  653. }
  654. }
  655. }
  656. /**
  657. * Delete this evaluation from the database.
  658. */
  659. public function delete()
  660. {
  661. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  662. $sql = 'DELETE FROM '.$table.' WHERE id = '.intval($this->id);
  663. Database::query($sql);
  664. }
  665. /**
  666. * Delete the gradebook categories from a course, including course sessions.
  667. *
  668. * @param string $courseCode
  669. */
  670. public static function deleteFromCourse($courseCode)
  671. {
  672. $em = Database::getManager();
  673. $categories = $em
  674. ->createQuery(
  675. 'SELECT DISTINCT gc.sessionId
  676. FROM ChamiloCoreBundle:GradebookCategory gc WHERE gc.courseCode = :code'
  677. )
  678. ->setParameter('code', $courseCode)
  679. ->getResult();
  680. foreach ($categories as $category) {
  681. $cats = self::load(
  682. null,
  683. null,
  684. $courseCode,
  685. null,
  686. null,
  687. (int) $category['sessionId']
  688. );
  689. if (!empty($cats)) {
  690. /** @var self $cat */
  691. foreach ($cats as $cat) {
  692. $cat->delete_all();
  693. }
  694. }
  695. }
  696. }
  697. /**
  698. * Show message resource delete.
  699. *
  700. * @param string $courseCode
  701. *
  702. * @return mixed
  703. */
  704. public function show_message_resource_delete($courseCode)
  705. {
  706. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  707. $sql = 'SELECT count(*) AS num
  708. FROM '.$table.'
  709. WHERE
  710. course_code = "'.Database::escape_string($courseCode).'" AND
  711. visible = 3';
  712. $res = Database::query($sql);
  713. $option = Database::fetch_array($res, 'ASSOC');
  714. if ($option['num'] >= 1) {
  715. return '&nbsp;&nbsp;<span class="resource-deleted">(&nbsp;'.get_lang('ResourceDeleted').'&nbsp;)</span>';
  716. } else {
  717. return false;
  718. }
  719. }
  720. /**
  721. * Shows all information of an category.
  722. *
  723. * @param int $categoryId
  724. *
  725. * @return array
  726. */
  727. public function showAllCategoryInfo($categoryId)
  728. {
  729. $categoryId = (int) $categoryId;
  730. if (empty($categoryId)) {
  731. return [];
  732. }
  733. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  734. $sql = 'SELECT * FROM '.$table.'
  735. WHERE id = '.$categoryId;
  736. $result = Database::query($sql);
  737. $row = Database::fetch_array($result, 'ASSOC');
  738. return $row;
  739. }
  740. /**
  741. * Checks if the certificate is available for the given user in this category.
  742. *
  743. * @param int $user_id User ID
  744. *
  745. * @return bool True if conditions match, false if fails
  746. */
  747. public function is_certificate_available($user_id)
  748. {
  749. $score = $this->calc_score(
  750. $user_id,
  751. null,
  752. $this->course_code,
  753. $this->session_id
  754. );
  755. if (isset($score) && isset($score[0])) {
  756. // Get a percentage score to compare to minimum certificate score
  757. // $certification_score = $score[0] / $score[1] * 100;
  758. // Get real score not a percentage.
  759. $certification_score = $score[0];
  760. if ($certification_score >= $this->certificate_min_score) {
  761. return true;
  762. }
  763. }
  764. return false;
  765. }
  766. /**
  767. * Is this category a course ?
  768. * A category is a course if it has a course code and no parent category.
  769. */
  770. public function is_course()
  771. {
  772. return isset($this->course_code) && !empty($this->course_code)
  773. && (!isset($this->parent) || $this->parent == 0);
  774. }
  775. /**
  776. * Calculate the score of this category.
  777. *
  778. * @param int $stud_id student id (default: all students - then the average is returned)
  779. * @param $type
  780. * @param string $course_code
  781. * @param int $session_id
  782. *
  783. * @return array (score sum, weight sum)
  784. * or null if no scores available
  785. */
  786. public function calc_score(
  787. $stud_id = null,
  788. $type = null,
  789. $course_code = '',
  790. $session_id = null
  791. ) {
  792. $key = 'category:'.$this->id.'student:'.(int) $stud_id.'type:'.$type.'course:'.$course_code.'session:'.(int) $session_id;
  793. $useCache = api_get_configuration_value('gradebook_use_apcu_cache');
  794. $cacheAvailable = api_get_configuration_value('apc') && $useCache;
  795. if ($cacheAvailable) {
  796. $cacheDriver = new \Doctrine\Common\Cache\ApcuCache();
  797. if ($cacheDriver->contains($key)) {
  798. return $cacheDriver->fetch($key);
  799. }
  800. }
  801. // Classic
  802. if (!empty($stud_id) && $type == '') {
  803. if (!empty($course_code)) {
  804. $cats = $this->get_subcategories(
  805. $stud_id,
  806. $course_code,
  807. $session_id
  808. );
  809. $evals = $this->get_evaluations($stud_id, false, $course_code);
  810. $links = $this->get_links($stud_id, false, $course_code);
  811. } else {
  812. $cats = $this->get_subcategories($stud_id);
  813. $evals = $this->get_evaluations($stud_id);
  814. $links = $this->get_links($stud_id);
  815. }
  816. // Calculate score
  817. $count = 0;
  818. $ressum = 0;
  819. $weightsum = 0;
  820. if (!empty($cats)) {
  821. /** @var Category $cat */
  822. foreach ($cats as $cat) {
  823. $cat->set_session_id($session_id);
  824. $cat->set_course_code($course_code);
  825. $cat->setStudentList($this->getStudentList());
  826. $score = $cat->calc_score(
  827. $stud_id,
  828. null,
  829. $course_code,
  830. $session_id
  831. );
  832. $catweight = 0;
  833. if ($cat->get_weight() != 0) {
  834. $catweight = $cat->get_weight();
  835. $weightsum += $catweight;
  836. }
  837. if (isset($score) && !empty($score[1]) && !empty($catweight)) {
  838. $ressum += $score[0] / $score[1] * $catweight;
  839. }
  840. }
  841. }
  842. $students = [];
  843. if (!empty($evals)) {
  844. /** @var Evaluation $eval */
  845. foreach ($evals as $eval) {
  846. $eval->setStudentList($this->getStudentList());
  847. $evalres = $eval->calc_score($stud_id);
  848. if (isset($evalres) && $eval->get_weight() != 0) {
  849. $evalweight = $eval->get_weight();
  850. $weightsum += $evalweight;
  851. if (!empty($evalres[1])) {
  852. $ressum += $evalres[0] / $evalres[1] * $evalweight;
  853. }
  854. } else {
  855. if ($eval->get_weight() != 0) {
  856. $evalweight = $eval->get_weight();
  857. $weightsum += $evalweight;
  858. }
  859. }
  860. }
  861. }
  862. if (!empty($links)) {
  863. /** @var EvalLink|ExerciseLink $link */
  864. foreach ($links as $link) {
  865. $link->setStudentList($this->getStudentList());
  866. if ($session_id) {
  867. $link->set_session_id($session_id);
  868. }
  869. $linkres = $link->calc_score($stud_id, null);
  870. if (!empty($linkres) && $link->get_weight() != 0) {
  871. $students[$stud_id] = $linkres[0];
  872. $linkweight = $link->get_weight();
  873. $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1];
  874. $count++;
  875. $weightsum += $linkweight;
  876. $ressum += $linkres[0] / $link_res_denom * $linkweight;
  877. } else {
  878. // Adding if result does not exists
  879. if ($link->get_weight() != 0) {
  880. $linkweight = $link->get_weight();
  881. $weightsum += $linkweight;
  882. }
  883. }
  884. }
  885. }
  886. } else {
  887. if (!empty($course_code)) {
  888. $cats = $this->get_subcategories(
  889. null,
  890. $course_code,
  891. $session_id
  892. );
  893. $evals = $this->get_evaluations(null, false, $course_code);
  894. $links = $this->get_links(null, false, $course_code);
  895. } else {
  896. $cats = $this->get_subcategories(null);
  897. $evals = $this->get_evaluations(null);
  898. $links = $this->get_links(null);
  899. }
  900. // Calculate score
  901. $count = 0;
  902. $ressum = 0;
  903. $weightsum = 0;
  904. $bestResult = 0;
  905. if (!empty($cats)) {
  906. /** @var Category $cat */
  907. foreach ($cats as $cat) {
  908. $cat->setStudentList($this->getStudentList());
  909. $score = $cat->calc_score(
  910. null,
  911. $type,
  912. $course_code,
  913. $session_id
  914. );
  915. $catweight = 0;
  916. if ($cat->get_weight() != 0) {
  917. $catweight = $cat->get_weight();
  918. $weightsum += $catweight;
  919. }
  920. if (isset($score) && !empty($score[1]) && !empty($catweight)) {
  921. $ressum += $score[0] / $score[1] * $catweight;
  922. if ($ressum > $bestResult) {
  923. $bestResult = $ressum;
  924. }
  925. }
  926. }
  927. }
  928. if (!empty($evals)) {
  929. /** @var Evaluation $eval */
  930. foreach ($evals as $eval) {
  931. $evalres = $eval->calc_score(null, $type);
  932. $eval->setStudentList($this->getStudentList());
  933. if (isset($evalres) && $eval->get_weight() != 0) {
  934. $evalweight = $eval->get_weight();
  935. $weightsum += $evalweight;
  936. $count++;
  937. if (!empty($evalres[1])) {
  938. $ressum += $evalres[0] / $evalres[1] * $evalweight;
  939. }
  940. if ($ressum > $bestResult) {
  941. $bestResult = $ressum;
  942. }
  943. } else {
  944. if ($eval->get_weight() != 0) {
  945. $evalweight = $eval->get_weight();
  946. $weightsum += $evalweight;
  947. }
  948. }
  949. }
  950. }
  951. if (!empty($links)) {
  952. /** @var EvalLink|ExerciseLink $link */
  953. foreach ($links as $link) {
  954. $link->setStudentList($this->getStudentList());
  955. if ($session_id) {
  956. $link->set_session_id($session_id);
  957. }
  958. $linkres = $link->calc_score($stud_id, $type);
  959. if (!empty($linkres) && $link->get_weight() != 0) {
  960. $students[$stud_id] = $linkres[0];
  961. $linkweight = $link->get_weight();
  962. $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1];
  963. $count++;
  964. $weightsum += $linkweight;
  965. $ressum += $linkres[0] / $link_res_denom * $linkweight;
  966. if ($ressum > $bestResult) {
  967. $bestResult = $ressum;
  968. }
  969. } else {
  970. // Adding if result does not exists
  971. if ($link->get_weight() != 0) {
  972. $linkweight = $link->get_weight();
  973. $weightsum += $linkweight;
  974. }
  975. }
  976. }
  977. }
  978. }
  979. switch ($type) {
  980. case 'best':
  981. if (empty($bestResult)) {
  982. if ($cacheAvailable) {
  983. $cacheDriver->save($key, null);
  984. }
  985. return null;
  986. }
  987. if ($cacheAvailable) {
  988. $cacheDriver->save($key, [$bestResult, $weightsum]);
  989. }
  990. return [$bestResult, $weightsum];
  991. break;
  992. case 'average':
  993. if (empty($ressum)) {
  994. if ($cacheAvailable) {
  995. $cacheDriver->save($key, null);
  996. }
  997. return null;
  998. }
  999. if ($cacheAvailable) {
  1000. $cacheDriver->save($key, [$ressum, $weightsum]);
  1001. }
  1002. return [$ressum, $weightsum];
  1003. break;
  1004. case 'ranking':
  1005. // category ranking is calculated in gradebook_data_generator.class.php
  1006. // function get_data
  1007. return null;
  1008. return AbstractLink::getCurrentUserRanking($stud_id, []);
  1009. break;
  1010. default:
  1011. if ($cacheAvailable) {
  1012. $cacheDriver->save($key, [$ressum, $weightsum]);
  1013. }
  1014. return [$ressum, $weightsum];
  1015. break;
  1016. }
  1017. }
  1018. /**
  1019. * Delete this category and every subcategory, evaluation and result inside.
  1020. */
  1021. public function delete_all()
  1022. {
  1023. $cats = self::load(null, null, $this->course_code, $this->id, null);
  1024. $evals = Evaluation::load(
  1025. null,
  1026. null,
  1027. $this->course_code,
  1028. $this->id,
  1029. null
  1030. );
  1031. $links = LinkFactory::load(
  1032. null,
  1033. null,
  1034. null,
  1035. null,
  1036. $this->course_code,
  1037. $this->id,
  1038. null
  1039. );
  1040. if (!empty($cats)) {
  1041. /** @var Category $cat */
  1042. foreach ($cats as $cat) {
  1043. $cat->delete_all();
  1044. $cat->delete();
  1045. }
  1046. }
  1047. if (!empty($evals)) {
  1048. /** @var Evaluation $eval */
  1049. foreach ($evals as $eval) {
  1050. $eval->delete_with_results();
  1051. }
  1052. }
  1053. if (!empty($links)) {
  1054. /** @var AbstractLink $link */
  1055. foreach ($links as $link) {
  1056. $link->delete();
  1057. }
  1058. }
  1059. $this->delete();
  1060. }
  1061. /**
  1062. * Return array of Category objects where a student is subscribed to.
  1063. *
  1064. * @param int $stud_id
  1065. * @param string $course_code
  1066. * @param int $session_id
  1067. *
  1068. * @return array
  1069. */
  1070. public function get_root_categories_for_student(
  1071. $stud_id,
  1072. $course_code = null,
  1073. $session_id = null
  1074. ) {
  1075. $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1076. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  1077. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1078. $course_code = Database::escape_string($course_code);
  1079. $session_id = (int) $session_id;
  1080. $sql = "SELECT * FROM $table WHERE parent_id = 0";
  1081. if (!api_is_allowed_to_edit()) {
  1082. $sql .= ' AND visible = 1';
  1083. //proceed with checks on optional parameters course & session
  1084. if (!empty($course_code)) {
  1085. // TODO: considering it highly improbable that a user would get here
  1086. // if he doesn't have the rights to view this course and this
  1087. // session, we don't check his registration to these, but this
  1088. // could be an improvement
  1089. if (!empty($session_id)) {
  1090. $sql .= " AND course_code = '".$course_code."' AND session_id = ".$session_id;
  1091. } else {
  1092. $sql .= " AND course_code = '".$course_code."' AND session_id is null OR session_id=0";
  1093. }
  1094. } else {
  1095. //no optional parameter, proceed as usual
  1096. $sql .= ' AND course_code in
  1097. (
  1098. SELECT c.code
  1099. FROM '.$main_course_user_table.' cu INNER JOIN '.$courseTable.' c
  1100. ON (cu.c_id = c.id)
  1101. WHERE cu.user_id = '.intval($stud_id).'
  1102. AND cu.status = '.STUDENT.'
  1103. )';
  1104. }
  1105. } elseif (api_is_allowed_to_edit() && !api_is_platform_admin()) {
  1106. //proceed with checks on optional parameters course & session
  1107. if (!empty($course_code)) {
  1108. // TODO: considering it highly improbable that a user would get here
  1109. // if he doesn't have the rights to view this course and this
  1110. // session, we don't check his registration to these, but this
  1111. // could be an improvement
  1112. $sql .= " AND course_code = '".$course_code."'";
  1113. if (!empty($session_id)) {
  1114. $sql .= " AND session_id = ".$session_id;
  1115. } else {
  1116. $sql .= 'AND session_id IS NULL OR session_id = 0';
  1117. }
  1118. } else {
  1119. $sql .= ' AND course_code IN
  1120. (
  1121. SELECT c.code
  1122. FROM '.$main_course_user_table.' cu INNER JOIN '.$courseTable.' c
  1123. ON (cu.c_id = c.id)
  1124. WHERE
  1125. cu.user_id = '.api_get_user_id().' AND
  1126. cu.status = '.COURSEMANAGER.'
  1127. )';
  1128. }
  1129. } elseif (api_is_platform_admin()) {
  1130. if (isset($session_id) && $session_id != 0) {
  1131. $sql .= ' AND session_id='.$session_id;
  1132. } else {
  1133. $sql .= ' AND coalesce(session_id,0)=0';
  1134. }
  1135. }
  1136. $result = Database::query($sql);
  1137. $cats = self::create_category_objects_from_sql_result($result);
  1138. // course independent categories
  1139. if (empty($course_code)) {
  1140. $cats = $this->getIndependentCategoriesWithStudentResult(
  1141. 0,
  1142. $stud_id,
  1143. $cats
  1144. );
  1145. }
  1146. return $cats;
  1147. }
  1148. /**
  1149. * Return array of Category objects where a teacher is admin for.
  1150. *
  1151. * @param int $user_id (to return everything, use 'null' here)
  1152. * @param string $course_code (optional)
  1153. * @param int $session_id (optional)
  1154. *
  1155. * @return array
  1156. */
  1157. public function get_root_categories_for_teacher(
  1158. $user_id,
  1159. $course_code = null,
  1160. $session_id = null
  1161. ) {
  1162. if ($user_id == null) {
  1163. return self::load(null, null, $course_code, 0, null, $session_id);
  1164. }
  1165. $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
  1166. $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1167. $tbl_grade_categories = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1168. $sql = 'SELECT * FROM '.$tbl_grade_categories.'
  1169. WHERE parent_id = 0 ';
  1170. if (!empty($course_code)) {
  1171. $sql .= " AND course_code = '".Database::escape_string($course_code)."' ";
  1172. if (!empty($session_id)) {
  1173. $sql .= " AND session_id = ".(int) $session_id;
  1174. }
  1175. } else {
  1176. $sql .= ' AND course_code in
  1177. (
  1178. SELECT c.code
  1179. FROM '.$main_course_user_table.' cu
  1180. INNER JOIN '.$courseTable.' c
  1181. ON (cu.c_id = c.id)
  1182. WHERE user_id = '.intval($user_id).'
  1183. )';
  1184. }
  1185. $result = Database::query($sql);
  1186. $cats = self::create_category_objects_from_sql_result($result);
  1187. // course independent categories
  1188. if (isset($course_code)) {
  1189. $indcats = self::load(
  1190. null,
  1191. $user_id,
  1192. $course_code,
  1193. 0,
  1194. null,
  1195. $session_id
  1196. );
  1197. $cats = array_merge($cats, $indcats);
  1198. }
  1199. return $cats;
  1200. }
  1201. /**
  1202. * Can this category be moved to somewhere else ?
  1203. * The root and courses cannot be moved.
  1204. *
  1205. * @return bool
  1206. */
  1207. public function is_movable()
  1208. {
  1209. return !(!isset($this->id) || $this->id == 0 || $this->is_course());
  1210. }
  1211. /**
  1212. * Generate an array of possible categories where this category can be moved to.
  1213. * Notice: its own parent will be included in the list: it's up to the frontend
  1214. * to disable this element.
  1215. *
  1216. * @return array 2-dimensional array - every element contains 3 subelements (id, name, level)
  1217. */
  1218. public function get_target_categories()
  1219. {
  1220. // the root or a course -> not movable
  1221. if (!$this->is_movable()) {
  1222. return null;
  1223. } else {
  1224. // otherwise:
  1225. // - course independent category
  1226. // -> movable to root or other independent categories
  1227. // - category inside a course
  1228. // -> movable to root, independent categories or categories inside the course
  1229. $user = api_is_platform_admin() ? null : api_get_user_id();
  1230. $targets = [];
  1231. $level = 0;
  1232. $root = [0, get_lang('RootCat'), $level];
  1233. $targets[] = $root;
  1234. if (isset($this->course_code) && !empty($this->course_code)) {
  1235. $crscats = self::load(null, null, $this->course_code, 0);
  1236. foreach ($crscats as $cat) {
  1237. if ($this->can_be_moved_to_cat($cat)) {
  1238. $targets[] = [
  1239. $cat->get_id(),
  1240. $cat->get_name(),
  1241. $level + 1,
  1242. ];
  1243. $targets = $this->addTargetSubcategories(
  1244. $targets,
  1245. $level + 1,
  1246. $cat->get_id()
  1247. );
  1248. }
  1249. }
  1250. }
  1251. $indcats = self::load(null, $user, 0, 0);
  1252. foreach ($indcats as $cat) {
  1253. if ($this->can_be_moved_to_cat($cat)) {
  1254. $targets[] = [$cat->get_id(), $cat->get_name(), $level + 1];
  1255. $targets = $this->addTargetSubcategories(
  1256. $targets,
  1257. $level + 1,
  1258. $cat->get_id()
  1259. );
  1260. }
  1261. }
  1262. return $targets;
  1263. }
  1264. }
  1265. /**
  1266. * Move this category to the given category.
  1267. * If this category moves from inside a course to outside,
  1268. * its course code must be changed, as well as the course code
  1269. * of all underlying categories and evaluations. All links will
  1270. * be deleted as well !
  1271. */
  1272. public function move_to_cat($cat)
  1273. {
  1274. $this->set_parent_id($cat->get_id());
  1275. if ($this->get_course_code() != $cat->get_course_code()) {
  1276. $this->set_course_code($cat->get_course_code());
  1277. $this->applyCourseCodeToChildren();
  1278. }
  1279. $this->save();
  1280. }
  1281. /**
  1282. * Generate an array of all categories the user can navigate to.
  1283. */
  1284. public function get_tree()
  1285. {
  1286. $targets = [];
  1287. $level = 0;
  1288. $root = [0, get_lang('RootCat'), $level];
  1289. $targets[] = $root;
  1290. // course or platform admin
  1291. if (api_is_allowed_to_edit()) {
  1292. $user = api_is_platform_admin() ? null : api_get_user_id();
  1293. $cats = self::get_root_categories_for_teacher($user);
  1294. foreach ($cats as $cat) {
  1295. $targets[] = [
  1296. $cat->get_id(),
  1297. $cat->get_name(),
  1298. $level + 1,
  1299. ];
  1300. $targets = $this->add_subtree(
  1301. $targets,
  1302. $level + 1,
  1303. $cat->get_id(),
  1304. null
  1305. );
  1306. }
  1307. } else {
  1308. // student
  1309. $cats = $this->get_root_categories_for_student(api_get_user_id());
  1310. foreach ($cats as $cat) {
  1311. $targets[] = [
  1312. $cat->get_id(),
  1313. $cat->get_name(),
  1314. $level + 1,
  1315. ];
  1316. $targets = $this->add_subtree(
  1317. $targets,
  1318. $level + 1,
  1319. $cat->get_id(),
  1320. 1
  1321. );
  1322. }
  1323. }
  1324. return $targets;
  1325. }
  1326. /**
  1327. * Generate an array of courses that a teacher hasn't created a category for.
  1328. *
  1329. * @param int $user_id
  1330. *
  1331. * @return array 2-dimensional array - every element contains 2 subelements (code, title)
  1332. */
  1333. public function get_not_created_course_categories($user_id)
  1334. {
  1335. $tbl_main_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  1336. $tbl_main_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1337. $tbl_grade_categories = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1338. $user_id = (int) $user_id;
  1339. $sql = 'SELECT DISTINCT(code), title
  1340. FROM '.$tbl_main_courses.' cc, '.$tbl_main_course_user.' cu
  1341. WHERE
  1342. cc.id = cu.c_id AND
  1343. cu.status = '.COURSEMANAGER;
  1344. if (!api_is_platform_admin()) {
  1345. $sql .= ' AND cu.user_id = '.$user_id;
  1346. }
  1347. $sql .= ' AND cc.code NOT IN
  1348. (
  1349. SELECT course_code FROM '.$tbl_grade_categories.'
  1350. WHERE
  1351. parent_id = 0 AND
  1352. course_code IS NOT NULL
  1353. )';
  1354. $result = Database::query($sql);
  1355. $cats = [];
  1356. while ($data = Database::fetch_array($result)) {
  1357. $cats[] = [$data['code'], $data['title']];
  1358. }
  1359. return $cats;
  1360. }
  1361. /**
  1362. * Generate an array of all courses that a teacher is admin of.
  1363. *
  1364. * @param int $user_id
  1365. *
  1366. * @return array 2-dimensional array - every element contains 2 subelements (code, title)
  1367. */
  1368. public function get_all_courses($user_id)
  1369. {
  1370. $tbl_main_courses = Database::get_main_table(TABLE_MAIN_COURSE);
  1371. $tbl_main_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1372. $sql = 'SELECT DISTINCT(code), title
  1373. FROM '.$tbl_main_courses.' cc, '.$tbl_main_course_user.' cu
  1374. WHERE cc.id = cu.c_id AND cu.status = '.COURSEMANAGER;
  1375. if (!api_is_platform_admin()) {
  1376. $sql .= ' AND cu.user_id = '.intval($user_id);
  1377. }
  1378. $result = Database::query($sql);
  1379. $cats = [];
  1380. while ($data = Database::fetch_array($result)) {
  1381. $cats[] = [$data['code'], $data['title']];
  1382. }
  1383. return $cats;
  1384. }
  1385. /**
  1386. * Apply the same visibility to every subcategory, evaluation and link.
  1387. */
  1388. public function apply_visibility_to_children()
  1389. {
  1390. $cats = self::load(null, null, null, $this->id, null);
  1391. $evals = Evaluation::load(null, null, null, $this->id, null);
  1392. $links = LinkFactory::load(
  1393. null,
  1394. null,
  1395. null,
  1396. null,
  1397. null,
  1398. $this->id,
  1399. null
  1400. );
  1401. if (!empty($cats)) {
  1402. foreach ($cats as $cat) {
  1403. $cat->set_visible($this->is_visible());
  1404. $cat->save();
  1405. $cat->apply_visibility_to_children();
  1406. }
  1407. }
  1408. if (!empty($evals)) {
  1409. foreach ($evals as $eval) {
  1410. $eval->set_visible($this->is_visible());
  1411. $eval->save();
  1412. }
  1413. }
  1414. if (!empty($links)) {
  1415. foreach ($links as $link) {
  1416. $link->set_visible($this->is_visible());
  1417. $link->save();
  1418. }
  1419. }
  1420. }
  1421. /**
  1422. * Check if a category contains evaluations with a result for a given student.
  1423. *
  1424. * @param int $studentId
  1425. *
  1426. * @return bool
  1427. */
  1428. public function hasEvaluationsWithStudentResults($studentId)
  1429. {
  1430. $evals = Evaluation::get_evaluations_with_result_for_student(
  1431. $this->id,
  1432. $studentId
  1433. );
  1434. if (count($evals) != 0) {
  1435. return true;
  1436. } else {
  1437. $cats = self::load(
  1438. null,
  1439. null,
  1440. null,
  1441. $this->id,
  1442. api_is_allowed_to_edit() ? null : 1
  1443. );
  1444. /** @var Category $cat */
  1445. foreach ($cats as $cat) {
  1446. if ($cat->hasEvaluationsWithStudentResults($studentId)) {
  1447. return true;
  1448. }
  1449. }
  1450. return false;
  1451. }
  1452. }
  1453. /**
  1454. * Retrieve all categories inside a course independent category
  1455. * that should be visible to a student.
  1456. *
  1457. * @param int $categoryId parent category
  1458. * @param int $studentId
  1459. * @param array $cats optional: if defined, the categories will be added to this array
  1460. *
  1461. * @return array
  1462. */
  1463. public function getIndependentCategoriesWithStudentResult(
  1464. $categoryId,
  1465. $studentId,
  1466. $cats = []
  1467. ) {
  1468. $creator = api_is_allowed_to_edit() && !api_is_platform_admin() ? api_get_user_id() : null;
  1469. $categories = self::load(
  1470. null,
  1471. $creator,
  1472. '0',
  1473. $categoryId,
  1474. api_is_allowed_to_edit() ? null : 1
  1475. );
  1476. if (!empty($categories)) {
  1477. /** @var Category $category */
  1478. foreach ($categories as $category) {
  1479. if ($category->hasEvaluationsWithStudentResults($studentId)) {
  1480. $cats[] = $category;
  1481. }
  1482. }
  1483. }
  1484. return $cats;
  1485. }
  1486. /**
  1487. * Return the session id (in any case, even if it's null or 0).
  1488. *
  1489. * @return int Session id (can be null)
  1490. */
  1491. public function get_session_id()
  1492. {
  1493. return $this->session_id;
  1494. }
  1495. /**
  1496. * Get appropriate subcategories visible for the user (and optionally the course and session).
  1497. *
  1498. * @param int $studentId student id (default: all students)
  1499. * @param string $course_code Course code (optional)
  1500. * @param int $session_id Session ID (optional)
  1501. * @param bool $order
  1502. *
  1503. * @return array Array of subcategories
  1504. */
  1505. public function get_subcategories(
  1506. $studentId = null,
  1507. $course_code = null,
  1508. $session_id = null,
  1509. $order = null
  1510. ) {
  1511. // 1 student
  1512. if (isset($studentId)) {
  1513. // Special case: this is the root
  1514. if ($this->id == 0) {
  1515. return $this->get_root_categories_for_student($studentId, $course_code, $session_id);
  1516. } else {
  1517. return self::load(
  1518. null,
  1519. null,
  1520. $course_code,
  1521. $this->id,
  1522. api_is_allowed_to_edit() ? null : 1,
  1523. $session_id,
  1524. $order
  1525. );
  1526. }
  1527. } else {
  1528. // All students
  1529. // Course admin
  1530. if (api_is_allowed_to_edit() && !api_is_platform_admin()) {
  1531. // root
  1532. if ($this->id == 0) {
  1533. // inside a course
  1534. return $this->get_root_categories_for_teacher(
  1535. api_get_user_id(),
  1536. $course_code,
  1537. $session_id,
  1538. false
  1539. );
  1540. } elseif (!empty($this->course_code)) {
  1541. return self::load(
  1542. null,
  1543. null,
  1544. $this->course_code,
  1545. $this->id,
  1546. null,
  1547. $session_id,
  1548. $order
  1549. );
  1550. } elseif (!empty($course_code)) {
  1551. // course independent
  1552. return self::load(
  1553. null,
  1554. null,
  1555. $course_code,
  1556. $this->id,
  1557. null,
  1558. $session_id,
  1559. $order
  1560. );
  1561. } else {
  1562. return self::load(
  1563. null,
  1564. api_get_user_id(),
  1565. 0,
  1566. $this->id,
  1567. null
  1568. );
  1569. }
  1570. } elseif (api_is_platform_admin()) {
  1571. // platform admin
  1572. // we explicitly avoid listing subcats from another session
  1573. return self::load(
  1574. null,
  1575. null,
  1576. $course_code,
  1577. $this->id,
  1578. null,
  1579. $session_id,
  1580. $order
  1581. );
  1582. }
  1583. }
  1584. return [];
  1585. }
  1586. /**
  1587. * Get appropriate evaluations visible for the user.
  1588. *
  1589. * @param int $studentId student id (default: all students)
  1590. * @param bool $recursive process subcategories (default: no recursion)
  1591. * @param string $course_code
  1592. * @param int $sessionId
  1593. *
  1594. * @return array
  1595. */
  1596. public function get_evaluations(
  1597. $studentId = null,
  1598. $recursive = false,
  1599. $course_code = '',
  1600. $sessionId = 0
  1601. ) {
  1602. $evals = [];
  1603. $course_code = empty($course_code) ? $this->get_course_code() : $course_code;
  1604. $sessionId = empty($sessionId) ? $this->get_session_id() : $sessionId;
  1605. // 1 student
  1606. if (isset($studentId) && !empty($studentId)) {
  1607. // Special case: this is the root
  1608. if ($this->id == 0) {
  1609. $evals = Evaluation::get_evaluations_with_result_for_student(
  1610. 0,
  1611. $studentId
  1612. );
  1613. } else {
  1614. $evals = Evaluation::load(
  1615. null,
  1616. null,
  1617. $course_code,
  1618. $this->id,
  1619. api_is_allowed_to_edit() ? null : 1
  1620. );
  1621. }
  1622. } else {
  1623. // All students
  1624. // course admin
  1625. if ((api_is_allowed_to_edit() || api_is_drh() || api_is_session_admin()) &&
  1626. !api_is_platform_admin()
  1627. ) {
  1628. // root
  1629. if ($this->id == 0) {
  1630. $evals = Evaluation::load(
  1631. null,
  1632. api_get_user_id(),
  1633. null,
  1634. $this->id,
  1635. null
  1636. );
  1637. } elseif (isset($this->course_code) &&
  1638. !empty($this->course_code)
  1639. ) {
  1640. // inside a course
  1641. $evals = Evaluation::load(
  1642. null,
  1643. null,
  1644. $course_code,
  1645. $this->id,
  1646. null
  1647. );
  1648. } else {
  1649. // course independent
  1650. $evals = Evaluation::load(
  1651. null,
  1652. api_get_user_id(),
  1653. null,
  1654. $this->id,
  1655. null
  1656. );
  1657. }
  1658. } else {
  1659. $evals = Evaluation::load(
  1660. null,
  1661. null,
  1662. $course_code,
  1663. $this->id,
  1664. null
  1665. );
  1666. }
  1667. }
  1668. if ($recursive) {
  1669. $subcats = $this->get_subcategories(
  1670. $studentId,
  1671. $course_code,
  1672. $sessionId
  1673. );
  1674. if (!empty($subcats)) {
  1675. foreach ($subcats as $subcat) {
  1676. $subevals = $subcat->get_evaluations(
  1677. $studentId,
  1678. true,
  1679. $course_code
  1680. );
  1681. $evals = array_merge($evals, $subevals);
  1682. }
  1683. }
  1684. }
  1685. return $evals;
  1686. }
  1687. /**
  1688. * Get appropriate links visible for the user.
  1689. *
  1690. * @param int $studentId student id (default: all students)
  1691. * @param bool $recursive process subcategories (default: no recursion)
  1692. * @param string $course_code
  1693. * @param int $sessionId
  1694. *
  1695. * @return array
  1696. */
  1697. public function get_links(
  1698. $studentId = null,
  1699. $recursive = false,
  1700. $course_code = '',
  1701. $sessionId = 0
  1702. ) {
  1703. $links = [];
  1704. $course_code = empty($course_code) ? $this->get_course_code() : $course_code;
  1705. $sessionId = empty($sessionId) ? $this->get_session_id() : $sessionId;
  1706. // no links in root or course independent categories
  1707. if ($this->id == 0) {
  1708. } elseif (isset($studentId)) {
  1709. // 1 student $studentId
  1710. $links = LinkFactory::load(
  1711. null,
  1712. null,
  1713. null,
  1714. null,
  1715. $course_code,
  1716. $this->id,
  1717. api_is_allowed_to_edit() ? null : 1
  1718. );
  1719. } else {
  1720. // All students -> only for course/platform admin
  1721. $links = LinkFactory::load(
  1722. null,
  1723. null,
  1724. null,
  1725. null,
  1726. $course_code,
  1727. $this->id,
  1728. null
  1729. );
  1730. }
  1731. if ($recursive) {
  1732. $subcats = $this->get_subcategories(
  1733. $studentId,
  1734. $course_code,
  1735. $sessionId
  1736. );
  1737. if (!empty($subcats)) {
  1738. /** @var Category $subcat */
  1739. foreach ($subcats as $subcat) {
  1740. $sublinks = $subcat->get_links(
  1741. $studentId,
  1742. false,
  1743. $course_code,
  1744. $sessionId
  1745. );
  1746. $links = array_merge($links, $sublinks);
  1747. }
  1748. }
  1749. }
  1750. return $links;
  1751. }
  1752. /**
  1753. * Get all the categories from with the same given direct parent.
  1754. *
  1755. * @param int $catId Category parent ID
  1756. *
  1757. * @return array Array of Category objects
  1758. */
  1759. public function getCategories($catId)
  1760. {
  1761. $catId = (int) $catId;
  1762. $tblGradeCategories = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1763. $sql = 'SELECT * FROM '.$tblGradeCategories.'
  1764. WHERE parent_id = '.$catId;
  1765. $result = Database::query($sql);
  1766. $categories = self::create_category_objects_from_sql_result($result);
  1767. return $categories;
  1768. }
  1769. /**
  1770. * Gets the type for the current object.
  1771. *
  1772. * @return string 'C' to represent "Category" object type
  1773. */
  1774. public function get_item_type()
  1775. {
  1776. return 'C';
  1777. }
  1778. /**
  1779. * @param array $skills
  1780. */
  1781. public function set_skills($skills)
  1782. {
  1783. $this->skills = $skills;
  1784. }
  1785. public function get_date()
  1786. {
  1787. return null;
  1788. }
  1789. /**
  1790. * @return string
  1791. */
  1792. public function get_icon_name()
  1793. {
  1794. return 'cat';
  1795. }
  1796. /**
  1797. * Find category by name.
  1798. *
  1799. * @param string $name_mask search string
  1800. *
  1801. * @return array category objects matching the search criterium
  1802. */
  1803. public function find_category($name_mask, $allcat)
  1804. {
  1805. $categories = [];
  1806. foreach ($allcat as $search_cat) {
  1807. if (!(strpos(strtolower($search_cat->get_name()), strtolower($name_mask)) === false)) {
  1808. $categories[] = $search_cat;
  1809. }
  1810. }
  1811. return $categories;
  1812. }
  1813. /**
  1814. * This function, locks a category , only one who can unlock it is
  1815. * the platform administrator.
  1816. *
  1817. * @param int locked 1 or unlocked 0
  1818. *
  1819. * @return bool|null
  1820. * */
  1821. public function lock($locked)
  1822. {
  1823. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1824. $sql = "UPDATE $table SET locked = '".intval($locked)."'
  1825. WHERE id='".intval($this->id)."'";
  1826. Database::query($sql);
  1827. }
  1828. /**
  1829. * @param $locked
  1830. */
  1831. public function lockAllItems($locked)
  1832. {
  1833. if (api_get_setting('gradebook_locking_enabled') == 'true') {
  1834. $this->lock($locked);
  1835. $evals_to_lock = $this->get_evaluations();
  1836. if (!empty($evals_to_lock)) {
  1837. foreach ($evals_to_lock as $item) {
  1838. $item->lock($locked);
  1839. }
  1840. }
  1841. $link_to_lock = $this->get_links();
  1842. if (!empty($link_to_lock)) {
  1843. foreach ($link_to_lock as $item) {
  1844. $item->lock($locked);
  1845. }
  1846. }
  1847. $event_type = LOG_GRADEBOOK_UNLOCKED;
  1848. if ($locked == 1) {
  1849. $event_type = LOG_GRADEBOOK_LOCKED;
  1850. }
  1851. Event::addEvent($event_type, LOG_GRADEBOOK_ID, $this->id);
  1852. }
  1853. }
  1854. /**
  1855. * Generates a certificate for this user if everything matches.
  1856. *
  1857. * @param int $category_id gradebook id
  1858. * @param int $user_id
  1859. * @param bool $sendNotification
  1860. *
  1861. * @return array
  1862. */
  1863. public static function generateUserCertificate(
  1864. $category_id,
  1865. $user_id,
  1866. $sendNotification = false
  1867. ) {
  1868. $user_id = (int) $user_id;
  1869. $category_id = (int) $category_id;
  1870. // Generating the total score for a course
  1871. $cats_course = self::load(
  1872. $category_id,
  1873. null,
  1874. null,
  1875. null,
  1876. null,
  1877. null,
  1878. false
  1879. );
  1880. /** @var Category $category */
  1881. $category = $cats_course[0];
  1882. if (empty($category)) {
  1883. return false;
  1884. }
  1885. $sessionId = $category->get_session_id();
  1886. $courseCode = $category->get_course_code();
  1887. $courseInfo = api_get_course_info($courseCode);
  1888. $courseId = $courseInfo['real_id'];
  1889. $userFinishedCourse = self::userFinishedCourse(
  1890. $user_id,
  1891. $category,
  1892. true
  1893. );
  1894. if (!$userFinishedCourse) {
  1895. return false;
  1896. }
  1897. $skillToolEnabled = Skill::hasAccessToUserSkill(
  1898. api_get_user_id(),
  1899. $user_id
  1900. );
  1901. $userHasSkills = false;
  1902. if ($skillToolEnabled) {
  1903. $skill = new Skill();
  1904. $skill->addSkillToUser(
  1905. $user_id,
  1906. $category,
  1907. $courseId,
  1908. $sessionId
  1909. );
  1910. $objSkillRelUser = new SkillRelUser();
  1911. $userSkills = $objSkillRelUser->getUserSkills(
  1912. $user_id,
  1913. $courseId,
  1914. $sessionId
  1915. );
  1916. $userHasSkills = !empty($userSkills);
  1917. if ($userHasSkills) {
  1918. return [
  1919. 'badge_link' => Display::toolbarButton(
  1920. get_lang('ExportBadges'),
  1921. api_get_path(WEB_CODE_PATH)."gradebook/get_badges.php?user=$user_id",
  1922. 'external-link'
  1923. ),
  1924. ];
  1925. }
  1926. }
  1927. // Block certification links depending gradebook configuration (generate certifications)
  1928. if (empty($category->getGenerateCertificates())) {
  1929. return false;
  1930. }
  1931. $cattotal = self::load($category_id);
  1932. $scoretotal = $cattotal[0]->calc_score($user_id);
  1933. // Do not remove this the gradebook/lib/fe/gradebooktable.class.php
  1934. // file load this variable as a global
  1935. $scoredisplay = ScoreDisplay::instance();
  1936. $my_score_in_gradebook = $scoredisplay->display_score(
  1937. $scoretotal,
  1938. SCORE_SIMPLE
  1939. );
  1940. $my_certificate = GradebookUtils::get_certificate_by_user_id(
  1941. $category->get_id(),
  1942. $user_id
  1943. );
  1944. if (empty($my_certificate)) {
  1945. GradebookUtils::registerUserInfoAboutCertificate(
  1946. $category_id,
  1947. $user_id,
  1948. $my_score_in_gradebook,
  1949. api_get_utc_datetime()
  1950. );
  1951. $my_certificate = GradebookUtils::get_certificate_by_user_id(
  1952. $category->get_id(),
  1953. $user_id
  1954. );
  1955. }
  1956. $html = [];
  1957. if (!empty($my_certificate)) {
  1958. $certificate_obj = new Certificate(
  1959. $my_certificate['id'],
  1960. 0,
  1961. $sendNotification
  1962. );
  1963. $fileWasGenerated = $certificate_obj->isHtmlFileGenerated();
  1964. if (!empty($fileWasGenerated)) {
  1965. $url = api_get_path(WEB_PATH).'certificates/index.php?id='.$my_certificate['id'];
  1966. $certificates = Display::toolbarButton(
  1967. get_lang('DisplayCertificate'),
  1968. $url,
  1969. 'eye',
  1970. 'primary',
  1971. ['target' => '_blank']
  1972. );
  1973. $exportToPDF = Display::url(
  1974. Display::return_icon(
  1975. 'pdf.png',
  1976. get_lang('ExportToPDF'),
  1977. [],
  1978. ICON_SIZE_MEDIUM
  1979. ),
  1980. "$url&action=export"
  1981. );
  1982. $hideExportLink = api_get_setting('hide_certificate_export_link');
  1983. $hideExportLinkStudent = api_get_setting('hide_certificate_export_link_students');
  1984. if ($hideExportLink === 'true' || (api_is_student() && $hideExportLinkStudent === 'true')) {
  1985. $exportToPDF = null;
  1986. }
  1987. $html = [
  1988. 'certificate_link' => $certificates,
  1989. 'pdf_link' => $exportToPDF,
  1990. 'pdf_url' => "$url&action=export",
  1991. ];
  1992. if ($skillToolEnabled && $userHasSkills) {
  1993. $html['badge_link'] = Display::toolbarButton(
  1994. get_lang('ExportBadges'),
  1995. api_get_path(WEB_CODE_PATH)."gradebook/get_badges.php?user=$user_id",
  1996. 'external-link'
  1997. );
  1998. }
  1999. }
  2000. return $html;
  2001. }
  2002. }
  2003. /**
  2004. * @param int $catId
  2005. * @param array $userList
  2006. */
  2007. public static function generateCertificatesInUserList($catId, $userList)
  2008. {
  2009. if (!empty($userList)) {
  2010. foreach ($userList as $userInfo) {
  2011. self::generateUserCertificate($catId, $userInfo['user_id']);
  2012. }
  2013. }
  2014. }
  2015. /**
  2016. * @param int $catId
  2017. * @param array $userList
  2018. */
  2019. public static function exportAllCertificates(
  2020. $catId,
  2021. $userList = []
  2022. ) {
  2023. $orientation = api_get_configuration_value('certificate_pdf_orientation');
  2024. $params['orientation'] = 'landscape';
  2025. if (!empty($orientation)) {
  2026. $params['orientation'] = $orientation;
  2027. }
  2028. $params['left'] = 0;
  2029. $params['right'] = 0;
  2030. $params['top'] = 0;
  2031. $params['bottom'] = 0;
  2032. $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
  2033. $pdf = new PDF($page_format, $params['orientation'], $params);
  2034. $certificate_list = GradebookUtils::get_list_users_certificates($catId, $userList);
  2035. $certificate_path_list = [];
  2036. if (!empty($certificate_list)) {
  2037. foreach ($certificate_list as $index => $value) {
  2038. $list_certificate = GradebookUtils::get_list_gradebook_certificates_by_user_id(
  2039. $value['user_id'],
  2040. $catId
  2041. );
  2042. foreach ($list_certificate as $value_certificate) {
  2043. $certificate_obj = new Certificate($value_certificate['id']);
  2044. $certificate_obj->generate(['hide_print_button' => true]);
  2045. if ($certificate_obj->isHtmlFileGenerated()) {
  2046. $certificate_path_list[] = $certificate_obj->html_file;
  2047. }
  2048. }
  2049. }
  2050. }
  2051. if (!empty($certificate_path_list)) {
  2052. // Print certificates (without the common header/footer/watermark
  2053. // stuff) and return as one multiple-pages PDF
  2054. $pdf->html_to_pdf(
  2055. $certificate_path_list,
  2056. get_lang('Certificates'),
  2057. null,
  2058. false,
  2059. false
  2060. );
  2061. }
  2062. }
  2063. /**
  2064. * @param int $catId
  2065. */
  2066. public static function deleteAllCertificates($catId)
  2067. {
  2068. $certificate_list = GradebookUtils::get_list_users_certificates($catId);
  2069. if (!empty($certificate_list)) {
  2070. foreach ($certificate_list as $index => $value) {
  2071. $list_certificate = GradebookUtils::get_list_gradebook_certificates_by_user_id(
  2072. $value['user_id'],
  2073. $catId
  2074. );
  2075. foreach ($list_certificate as $value_certificate) {
  2076. $certificate_obj = new Certificate($value_certificate['id']);
  2077. $certificate_obj->delete(true);
  2078. }
  2079. }
  2080. }
  2081. }
  2082. /**
  2083. * Check whether a user has finished a course by its gradebook.
  2084. *
  2085. * @param int $userId The user ID
  2086. * @param \Category $category Optional. The gradebook category.
  2087. * To check by the gradebook category
  2088. * @param bool $recalculateScore Whether recalculate the score
  2089. *
  2090. * @return bool
  2091. */
  2092. public static function userFinishedCourse(
  2093. $userId,
  2094. \Category $category,
  2095. $recalculateScore = false
  2096. ) {
  2097. if (empty($category)) {
  2098. return false;
  2099. }
  2100. $currentScore = self::getCurrentScore(
  2101. $userId,
  2102. $category,
  2103. $recalculateScore
  2104. );
  2105. $minCertificateScore = $category->getCertificateMinScore();
  2106. $passedCourse = $currentScore >= $minCertificateScore;
  2107. return $passedCourse;
  2108. }
  2109. /**
  2110. * Get the current score (as percentage) on a gradebook category for a user.
  2111. *
  2112. * @param int $userId The user id
  2113. * @param Category $category The gradebook category
  2114. * @param bool $recalculate
  2115. *
  2116. * @return float The score
  2117. */
  2118. public static function getCurrentScore(
  2119. $userId,
  2120. $category,
  2121. $recalculate = false
  2122. ) {
  2123. if (empty($category)) {
  2124. return 0;
  2125. }
  2126. if ($recalculate) {
  2127. return self::calculateCurrentScore(
  2128. $userId,
  2129. $category
  2130. );
  2131. }
  2132. $resultData = Database::select(
  2133. '*',
  2134. Database::get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_LOG),
  2135. [
  2136. 'where' => [
  2137. 'category_id = ? AND user_id = ?' => [$category->get_id(), $userId],
  2138. ],
  2139. 'order' => 'registered_at DESC',
  2140. 'limit' => '1',
  2141. ],
  2142. 'first'
  2143. );
  2144. if (empty($resultData)) {
  2145. return 0;
  2146. }
  2147. return $resultData['score'];
  2148. }
  2149. /**
  2150. * Register the current score for a user on a category gradebook.
  2151. *
  2152. * @param float $score The achieved score
  2153. * @param int $userId The user id
  2154. * @param int $categoryId The gradebook category
  2155. *
  2156. * @return int The insert id
  2157. */
  2158. public static function registerCurrentScore($score, $userId, $categoryId)
  2159. {
  2160. return Database::insert(
  2161. Database::get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_LOG),
  2162. [
  2163. 'category_id' => intval($categoryId),
  2164. 'user_id' => intval($userId),
  2165. 'score' => api_float_val($score),
  2166. 'registered_at' => api_get_utc_datetime(),
  2167. ]
  2168. );
  2169. }
  2170. /**
  2171. * @return array
  2172. */
  2173. public function getStudentList()
  2174. {
  2175. return $this->studentList;
  2176. }
  2177. /**
  2178. * @param array $list
  2179. */
  2180. public function setStudentList($list)
  2181. {
  2182. $this->studentList = $list;
  2183. }
  2184. /**
  2185. * @return string
  2186. */
  2187. public static function getUrl()
  2188. {
  2189. $url = Session::read('gradebook_dest');
  2190. if (empty($url)) {
  2191. // We guess the link
  2192. $courseInfo = api_get_course_info();
  2193. if (!empty($courseInfo)) {
  2194. return api_get_path(WEB_CODE_PATH).'gradebook/index.php?'.api_get_cidreq().'&';
  2195. } else {
  2196. return api_get_path(WEB_CODE_PATH).'gradebook/gradebook.php?';
  2197. }
  2198. }
  2199. return $url;
  2200. }
  2201. /**
  2202. * Destination is index.php or gradebook.php.
  2203. *
  2204. * @param string $url
  2205. */
  2206. public static function setUrl($url)
  2207. {
  2208. switch ($url) {
  2209. case 'gradebook.php':
  2210. $url = api_get_path(WEB_CODE_PATH).'gradebook/gradebook.php?';
  2211. break;
  2212. case 'index.php':
  2213. $url = api_get_path(WEB_CODE_PATH).'gradebook/index.php?'.api_get_cidreq().'&';
  2214. break;
  2215. }
  2216. Session::write('gradebook_dest', $url);
  2217. }
  2218. /**
  2219. * @return int
  2220. */
  2221. public function getGradeBooksToValidateInDependence()
  2222. {
  2223. return $this->gradeBooksToValidateInDependence;
  2224. }
  2225. /**
  2226. * @param int $value
  2227. *
  2228. * @return Category
  2229. */
  2230. public function setGradeBooksToValidateInDependence($value)
  2231. {
  2232. $this->gradeBooksToValidateInDependence = $value;
  2233. return $this;
  2234. }
  2235. /**
  2236. * Return HTML code with links to download and view certificate.
  2237. *
  2238. * @param array $certificate
  2239. *
  2240. * @return string
  2241. */
  2242. public static function getDownloadCertificateBlock(array $certificate)
  2243. {
  2244. if (!isset($certificate['pdf_url'])) {
  2245. return '';
  2246. }
  2247. $downloadLink = Display::toolbarButton(
  2248. get_lang('DownloadCertificatePdf'),
  2249. $certificate['pdf_url'],
  2250. 'file-pdf-o'
  2251. );
  2252. $viewLink = $certificate['certificate_link'];
  2253. return "
  2254. <div class='panel panel-default'>
  2255. <div class='panel-body'>
  2256. <h3 class='text-center'>".get_lang('NowDownloadYourCertificateClickHere')."</h3>
  2257. <div class='text-center'>$downloadLink $viewLink</div>
  2258. </div>
  2259. </div>
  2260. ";
  2261. }
  2262. /**
  2263. * Find a gradebook category by the certificate ID.
  2264. *
  2265. * @param int $id certificate id
  2266. *
  2267. * @throws \Doctrine\ORM\NonUniqueResultException
  2268. *
  2269. * @return Category|null
  2270. */
  2271. public static function findByCertificate($id)
  2272. {
  2273. $category = Database::getManager()
  2274. ->createQuery('SELECT c.catId FROM ChamiloCoreBundle:GradebookCertificate c WHERE c.id = :id')
  2275. ->setParameters(['id' => $id])
  2276. ->getOneOrNullResult();
  2277. if (empty($category)) {
  2278. return null;
  2279. }
  2280. $category = self::load($category['catId']);
  2281. if (empty($category)) {
  2282. return null;
  2283. }
  2284. return $category[0];
  2285. }
  2286. /**
  2287. * @param int $value
  2288. */
  2289. public function setDocumentId($value)
  2290. {
  2291. $this->documentId = (int) $value;
  2292. }
  2293. /**
  2294. * @return int
  2295. */
  2296. public function getDocumentId()
  2297. {
  2298. return $this->documentId;
  2299. }
  2300. /**
  2301. * @return Category
  2302. */
  2303. private static function create_root_category()
  2304. {
  2305. $cat = new Category();
  2306. $cat->set_id(0);
  2307. $cat->set_name(get_lang('RootCat'));
  2308. $cat->set_description(null);
  2309. $cat->set_user_id(0);
  2310. $cat->set_course_code(null);
  2311. $cat->set_parent_id(null);
  2312. $cat->set_weight(0);
  2313. $cat->set_visible(1);
  2314. $cat->setGenerateCertificates(0);
  2315. $cat->setIsRequirement(false);
  2316. return $cat;
  2317. }
  2318. /**
  2319. * @param Doctrine\DBAL\Driver\Statement|null $result
  2320. *
  2321. * @return array
  2322. */
  2323. private static function create_category_objects_from_sql_result($result)
  2324. {
  2325. $categories = [];
  2326. $allow = api_get_configuration_value('allow_gradebook_stats');
  2327. if ($allow) {
  2328. $em = Database::getManager();
  2329. $repo = $em->getRepository('ChamiloCoreBundle:GradebookCategory');
  2330. }
  2331. while ($data = Database::fetch_array($result)) {
  2332. $cat = new Category();
  2333. $cat->set_id($data['id']);
  2334. $cat->set_name($data['name']);
  2335. $cat->set_description($data['description']);
  2336. $cat->set_user_id($data['user_id']);
  2337. $cat->set_course_code($data['course_code']);
  2338. $cat->set_parent_id($data['parent_id']);
  2339. $cat->set_weight($data['weight']);
  2340. $cat->set_visible($data['visible']);
  2341. $cat->set_session_id($data['session_id']);
  2342. $cat->set_certificate_min_score($data['certif_min_score']);
  2343. $cat->set_grade_model_id($data['grade_model_id']);
  2344. $cat->set_locked($data['locked']);
  2345. $cat->setGenerateCertificates($data['generate_certificates']);
  2346. $cat->setIsRequirement($data['is_requirement']);
  2347. $cat->setCourseListDependency(isset($data['depends']) ? $data['depends'] : []);
  2348. $cat->setMinimumToValidate(isset($data['minimum_to_validate']) ? $data['minimum_to_validate'] : null);
  2349. $cat->setGradeBooksToValidateInDependence(isset($data['gradebooks_to_validate_in_dependence']) ? $data['gradebooks_to_validate_in_dependence'] : null);
  2350. $cat->setDocumentId($data['document_id']);
  2351. if ($allow) {
  2352. $cat->entity = $repo->find($data['id']);
  2353. }
  2354. $categories[] = $cat;
  2355. }
  2356. return $categories;
  2357. }
  2358. /**
  2359. * Internal function used by get_target_categories().
  2360. *
  2361. * @param array $targets
  2362. * @param int $level
  2363. * @param int $catid
  2364. *
  2365. * @return array
  2366. */
  2367. private function addTargetSubcategories($targets, $level, $catid)
  2368. {
  2369. $subcats = self::load(null, null, null, $catid);
  2370. foreach ($subcats as $cat) {
  2371. if ($this->can_be_moved_to_cat($cat)) {
  2372. $targets[] = [
  2373. $cat->get_id(),
  2374. $cat->get_name(),
  2375. $level + 1,
  2376. ];
  2377. $targets = $this->addTargetSubcategories(
  2378. $targets,
  2379. $level + 1,
  2380. $cat->get_id()
  2381. );
  2382. }
  2383. }
  2384. return $targets;
  2385. }
  2386. /**
  2387. * Internal function used by get_target_categories() and addTargetSubcategories()
  2388. * Can this category be moved to the given category ?
  2389. * Impossible when origin and target are the same... children won't be processed
  2390. * either. (a category can't be moved to one of its own children).
  2391. */
  2392. private function can_be_moved_to_cat($cat)
  2393. {
  2394. return $cat->get_id() != $this->get_id();
  2395. }
  2396. /**
  2397. * Internal function used by move_to_cat().
  2398. */
  2399. private function applyCourseCodeToChildren()
  2400. {
  2401. $cats = self::load(null, null, null, $this->id, null);
  2402. $evals = Evaluation::load(null, null, null, $this->id, null);
  2403. $links = LinkFactory::load(
  2404. null,
  2405. null,
  2406. null,
  2407. null,
  2408. null,
  2409. $this->id,
  2410. null
  2411. );
  2412. /** @var Category $cat */
  2413. foreach ($cats as $cat) {
  2414. $cat->set_course_code($this->get_course_code());
  2415. $cat->save();
  2416. $cat->applyCourseCodeToChildren();
  2417. }
  2418. foreach ($evals as $eval) {
  2419. $eval->set_course_code($this->get_course_code());
  2420. $eval->save();
  2421. }
  2422. foreach ($links as $link) {
  2423. $link->delete();
  2424. }
  2425. }
  2426. /**
  2427. * Internal function used by get_tree().
  2428. *
  2429. * @param int $level
  2430. * @param int|null $visible
  2431. *
  2432. * @return array
  2433. */
  2434. private function add_subtree($targets, $level, $catid, $visible)
  2435. {
  2436. $subcats = self::load(null, null, null, $catid, $visible);
  2437. if (!empty($subcats)) {
  2438. foreach ($subcats as $cat) {
  2439. $targets[] = [
  2440. $cat->get_id(),
  2441. $cat->get_name(),
  2442. $level + 1,
  2443. ];
  2444. $targets = self::add_subtree(
  2445. $targets,
  2446. $level + 1,
  2447. $cat->get_id(),
  2448. $visible
  2449. );
  2450. }
  2451. }
  2452. return $targets;
  2453. }
  2454. /**
  2455. * Calculate the current score on a gradebook category for a user.
  2456. *
  2457. * @param int $userId The user id
  2458. * @param Category $category The gradebook category
  2459. *
  2460. * @return float The score
  2461. */
  2462. private static function calculateCurrentScore(
  2463. $userId,
  2464. $category
  2465. ) {
  2466. if (empty($category)) {
  2467. return 0;
  2468. }
  2469. $courseEvaluations = $category->get_evaluations(
  2470. $userId,
  2471. true
  2472. );
  2473. $courseLinks = $category->get_links($userId, true);
  2474. $evaluationsAndLinks = array_merge($courseEvaluations, $courseLinks);
  2475. $categoryScore = 0;
  2476. for ($i = 0; $i < count($evaluationsAndLinks); $i++) {
  2477. $item = $evaluationsAndLinks[$i];
  2478. $score = $item->calc_score($userId);
  2479. $itemValue = 0;
  2480. if (!empty($score)) {
  2481. $divider = $score[1] == 0 ? 1 : $score[1];
  2482. $itemValue = $score[0] / $divider * $item->get_weight();
  2483. }
  2484. $categoryScore += $itemValue;
  2485. }
  2486. return api_float_val($categoryScore);
  2487. }
  2488. }