category.class.php 85 KB

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