category.class.php 80 KB

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