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