Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 11 | class Category implements GradebookItem |
||
| 12 | { |
||
| 13 | private $id; |
||
| 14 | private $name; |
||
| 15 | private $description; |
||
| 16 | private $user_id; |
||
| 17 | private $course_code; |
||
| 18 | private $courseId; |
||
| 19 | private $parent; |
||
| 20 | private $weight; |
||
| 21 | private $visible; |
||
| 22 | private $certificate_min_score; |
||
| 23 | private $session_id; |
||
| 24 | private $skills = array(); |
||
| 25 | private $grade_model_id; |
||
| 26 | private $generateCertificates; |
||
| 27 | private $isRequirement; |
||
| 28 | public $studentList; |
||
| 29 | |||
| 30 | public $evaluations; |
||
| 31 | public $links; |
||
| 32 | public $subCategories; |
||
| 33 | |||
| 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 = ''; |
||
| 44 | $this->courseId = 0; |
||
| 45 | $this->parent = 0; |
||
| 46 | $this->weight = 0; |
||
| 47 | $this->visible = false; |
||
| 48 | $this->certificate_min_score = 0; |
||
| 49 | $this->session_id = 0; |
||
| 50 | $this->grade_model_id = 0; |
||
| 51 | $this->generateCertificates = false; |
||
| 52 | $this->isRequirement = false; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return int |
||
| 57 | */ |
||
| 58 | public function get_id() |
||
| 59 | { |
||
| 60 | return $this->id; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function get_name() |
||
| 67 | { |
||
| 68 | return $this->name; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | public function get_description() |
||
| 75 | { |
||
| 76 | return $this->description; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return int |
||
| 81 | */ |
||
| 82 | public function get_user_id() |
||
| 83 | { |
||
| 84 | return $this->user_id; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return integer|null |
||
| 89 | */ |
||
| 90 | public function get_certificate_min_score() |
||
| 91 | { |
||
| 92 | if (!empty($this->certificate_min_score)) { |
||
| 93 | return $this->certificate_min_score; |
||
| 94 | } else { |
||
| 95 | return null; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function get_course_code() |
||
| 103 | { |
||
| 104 | return $this->course_code; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return integer |
||
| 109 | */ |
||
| 110 | public function get_parent_id() |
||
| 111 | { |
||
| 112 | return $this->parent; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return integer |
||
| 117 | */ |
||
| 118 | public function get_weight() |
||
| 119 | { |
||
| 120 | return $this->weight; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function is_locked() |
||
| 127 | { |
||
| 128 | return isset($this->locked) && $this->locked == 1 ? true : false; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return boolean |
||
| 133 | */ |
||
| 134 | public function is_visible() |
||
| 135 | { |
||
| 136 | return $this->visible; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get $isRequirement |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | public function getIsRequirement() |
||
| 144 | { |
||
| 145 | return $this->isRequirement; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param int $id |
||
| 150 | */ |
||
| 151 | public function set_id($id) |
||
| 152 | { |
||
| 153 | $this->id = $id; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $name |
||
| 158 | */ |
||
| 159 | public function set_name($name) |
||
| 160 | { |
||
| 161 | $this->name = $name; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $description |
||
| 166 | */ |
||
| 167 | public function set_description($description) |
||
| 168 | { |
||
| 169 | $this->description = $description; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param int $user_id |
||
| 174 | */ |
||
| 175 | public function set_user_id($user_id) |
||
| 176 | { |
||
| 177 | $this->user_id = $user_id; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $course_code |
||
| 182 | */ |
||
| 183 | public function set_course_code($course_code) |
||
| 184 | { |
||
| 185 | $this->course_code = $course_code; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param float $min_score |
||
| 190 | */ |
||
| 191 | public function set_certificate_min_score($min_score = null) |
||
| 192 | { |
||
| 193 | $this->certificate_min_score = $min_score; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param int $parent |
||
| 198 | */ |
||
| 199 | public function set_parent_id($parent) |
||
| 200 | { |
||
| 201 | $this->parent = intval($parent); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Filters to int and sets the session ID |
||
| 206 | * @param int The session ID from the Dokeos course session |
||
| 207 | */ |
||
| 208 | public function set_session_id($session_id = 0) |
||
| 209 | { |
||
| 210 | $this->session_id = (int) $session_id; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $weight |
||
| 215 | */ |
||
| 216 | public function set_weight($weight) |
||
| 217 | { |
||
| 218 | $this->weight = $weight; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param $visible |
||
| 223 | */ |
||
| 224 | public function set_visible($visible) |
||
| 225 | { |
||
| 226 | $this->visible = $visible; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param int $id |
||
| 231 | */ |
||
| 232 | public function set_grade_model_id($id) |
||
| 233 | { |
||
| 234 | $this->grade_model_id = $id; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param $locked |
||
| 239 | */ |
||
| 240 | public function set_locked($locked) |
||
| 241 | { |
||
| 242 | $this->locked = $locked; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Set $isRequirement |
||
| 247 | * @param int $isRequirement |
||
| 248 | */ |
||
| 249 | public function setIsRequirement($isRequirement) |
||
| 250 | { |
||
| 251 | $this->isRequirement = $isRequirement; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return null|integer |
||
| 256 | */ |
||
| 257 | public function get_grade_model_id() |
||
| 258 | { |
||
| 259 | if ($this->grade_model_id < 0) { |
||
| 260 | return null; |
||
| 261 | } |
||
| 262 | return $this->grade_model_id; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function get_type() |
||
| 269 | { |
||
| 270 | return 'category'; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param bool $from_db |
||
| 275 | * @return array|resource |
||
| 276 | */ |
||
| 277 | public function get_skills($from_db = true) |
||
| 278 | { |
||
| 279 | if ($from_db) { |
||
| 280 | $cat_id = $this->get_id(); |
||
| 281 | |||
| 282 | $gradebook = new Gradebook(); |
||
| 283 | $skills = $gradebook->get_skills_by_gradebook($cat_id); |
||
| 284 | } else { |
||
| 285 | $skills = $this->skills; |
||
| 286 | } |
||
| 287 | |||
| 288 | return $skills; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function get_skills_for_select() |
||
| 295 | { |
||
| 296 | $skills = $this->get_skills(); |
||
| 297 | $skill_select = array(); |
||
| 298 | if (!empty($skills)) { |
||
| 299 | foreach ($skills as $skill) { |
||
|
|
|||
| 300 | $skill_select[$skill['id']] = $skill['name']; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | return $skill_select; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set the generate_certificates value |
||
| 309 | * @param int $generateCertificates |
||
| 310 | */ |
||
| 311 | public function setGenerateCertificates($generateCertificates) |
||
| 312 | { |
||
| 313 | $this->generateCertificates = $generateCertificates; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get the generate_certificates value |
||
| 318 | * @return int |
||
| 319 | */ |
||
| 320 | public function getGenerateCertificates() |
||
| 321 | { |
||
| 322 | return $this->generateCertificates; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param int $id |
||
| 327 | * @param int $session_id |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | public static function load_session_categories($id = null, $session_id = null) |
||
| 332 | { |
||
| 333 | View Code Duplication | if (isset($id) && (int) $id === 0) { |
|
| 334 | $cats = array(); |
||
| 335 | $cats[] = self::create_root_category(); |
||
| 336 | return $cats; |
||
| 337 | } |
||
| 338 | |||
| 339 | $courseInfo = api_get_course_info_by_id(api_get_course_int_id()); |
||
| 340 | $courseCode = $courseInfo['code']; |
||
| 341 | $session_id = intval($session_id); |
||
| 342 | |||
| 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 | |||
| 359 | return $categoryList; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Retrieve categories and return them as an array of Category objects |
||
| 366 | * @param int $id category id |
||
| 367 | * @param int $user_id (category owner) |
||
| 368 | * @param string $course_code |
||
| 369 | * @param int $parent_id parent category |
||
| 370 | * @param bool $visible |
||
| 371 | * @param int $session_id (in case we are in a session) |
||
| 372 | * @param bool $order_by Whether to show all "session" |
||
| 373 | * categories (true) or hide them (false) in case there is no session id |
||
| 374 | * |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | public static function load( |
||
| 378 | $id = null, |
||
| 379 | $user_id = null, |
||
| 380 | $course_code = null, |
||
| 381 | $parent_id = null, |
||
| 382 | $visible = null, |
||
| 383 | $session_id = null, |
||
| 384 | $order_by = null |
||
| 385 | ) { |
||
| 386 | //if the category given is explicitly 0 (not null), then create |
||
| 387 | // a root category object (in memory) |
||
| 388 | View Code Duplication | if (isset($id) && (int) $id === 0) { |
|
| 389 | $cats = array(); |
||
| 390 | $cats[] = self::create_root_category(); |
||
| 391 | |||
| 392 | return $cats; |
||
| 393 | } |
||
| 394 | |||
| 395 | $tbl_grade_categories = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 396 | $sql = 'SELECT * FROM '.$tbl_grade_categories; |
||
| 397 | $paramcount = 0; |
||
| 398 | if (isset($id)) { |
||
| 399 | $sql .= ' WHERE id = '.intval($id); |
||
| 400 | $paramcount++; |
||
| 401 | } |
||
| 402 | |||
| 403 | View Code Duplication | if (isset($user_id)) { |
|
| 404 | $user_id = intval($user_id); |
||
| 405 | if ($paramcount != 0) { |
||
| 406 | $sql .= ' AND'; |
||
| 407 | } else { |
||
| 408 | $sql .= ' WHERE'; |
||
| 409 | } |
||
| 410 | $sql .= ' user_id = '.intval($user_id); |
||
| 411 | $paramcount++; |
||
| 412 | } |
||
| 413 | |||
| 414 | if (isset($course_code)) { |
||
| 415 | if ($paramcount != 0) { |
||
| 416 | $sql .= ' AND'; |
||
| 417 | } else { |
||
| 418 | $sql .= ' WHERE'; |
||
| 419 | } |
||
| 420 | |||
| 421 | if ($course_code == '0') { |
||
| 422 | $sql .= ' c_id is null '; |
||
| 423 | } else { |
||
| 424 | $courseInfo = api_get_course_info($course_code); |
||
| 425 | if ($courseInfo) { |
||
| 426 | $sql .= " c_id = '".intval($courseInfo['real_id'])."'"; |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | /*if ($show_session_categories !== true) { |
||
| 431 | // a query on the course should show all |
||
| 432 | // the categories inside sessions for this course |
||
| 433 | // otherwise a special parameter is given to ask explicitely |
||
| 434 | $sql .= " AND (session_id IS NULL OR session_id = 0) "; |
||
| 435 | } else {*/ |
||
| 436 | if (empty($session_id)) { |
||
| 437 | $sql .= ' AND (session_id IS NULL OR session_id = 0) '; |
||
| 438 | } else { |
||
| 439 | $sql .= ' AND session_id = '.(int) $session_id.' '; |
||
| 440 | } |
||
| 441 | //} |
||
| 442 | $paramcount++; |
||
| 443 | } |
||
| 444 | |||
| 445 | View Code Duplication | if (isset($parent_id)) { |
|
| 446 | if ($paramcount != 0) { |
||
| 447 | $sql .= ' AND '; |
||
| 448 | } else { |
||
| 449 | $sql .= ' WHERE '; |
||
| 450 | } |
||
| 451 | $sql .= ' parent_id = '.intval($parent_id); |
||
| 452 | $paramcount++; |
||
| 453 | } |
||
| 454 | |||
| 455 | View Code Duplication | if (isset($visible)) { |
|
| 456 | if ($paramcount != 0) { |
||
| 457 | $sql .= ' AND'; |
||
| 458 | } else { |
||
| 459 | $sql .= ' WHERE'; |
||
| 460 | } |
||
| 461 | $sql .= ' visible = '.intval($visible); |
||
| 462 | } |
||
| 463 | |||
| 464 | if (!empty($order_by)) { |
||
| 465 | if (!empty($order_by) && $order_by != '') { |
||
| 466 | $sql .= ' '.$order_by; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | $result = Database::query($sql); |
||
| 471 | |||
| 472 | $categories = array(); |
||
| 473 | if (Database::num_rows($result) > 0) { |
||
| 474 | $categories = self::create_category_objects_from_sql_result( |
||
| 475 | $result |
||
| 476 | ); |
||
| 477 | } |
||
| 478 | |||
| 479 | return $categories; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @return Category |
||
| 484 | */ |
||
| 485 | private static function create_root_category() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param Doctrine\DBAL\Driver\Statement|null $result |
||
| 504 | * |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | private static function create_category_objects_from_sql_result($result) |
||
| 508 | { |
||
| 509 | $categories = array(); |
||
| 510 | while ($data = Database::fetch_array($result)) { |
||
| 511 | $cat = new Category(); |
||
| 512 | $cat->set_id($data['id']); |
||
| 513 | $cat->set_name($data['name']); |
||
| 514 | $cat->set_description($data['description']); |
||
| 515 | $cat->set_user_id($data['user_id']); |
||
| 516 | $courseInfo = api_get_course_info_by_id($data['c_id']); |
||
| 517 | $cat->set_course_code($courseInfo['code']); |
||
| 518 | $cat->setCourseId($data['c_id']); |
||
| 519 | $cat->set_parent_id($data['parent_id']); |
||
| 520 | $cat->set_weight($data['weight']); |
||
| 521 | $cat->set_visible($data['visible']); |
||
| 522 | $cat->set_session_id($data['session_id']); |
||
| 523 | $cat->set_certificate_min_score($data['certif_min_score']); |
||
| 524 | $cat->set_grade_model_id($data['grade_model_id']); |
||
| 525 | $cat->set_locked($data['locked']); |
||
| 526 | $cat->setGenerateCertificates($data['generate_certificates']); |
||
| 527 | $cat->setIsRequirement($data['is_requirement']); |
||
| 528 | $categories[] = $cat; |
||
| 529 | } |
||
| 530 | |||
| 531 | return $categories; |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Create a category object from a GradebookCategory entity |
||
| 536 | * @param GradebookCategory $gradebookCategory The entity |
||
| 537 | * @return \Category |
||
| 538 | */ |
||
| 539 | public static function createCategoryObjectFromEntity( |
||
| 540 | GradebookCategory $gradebookCategory |
||
| 541 | ) { |
||
| 542 | $category = new Category(); |
||
| 543 | $category->set_id($gradebookCategory->getId()); |
||
| 544 | $category->set_name($gradebookCategory->getName()); |
||
| 545 | $category->set_description($gradebookCategory->getDescription()); |
||
| 546 | $category->set_user_id($gradebookCategory->getUserId()); |
||
| 547 | $category->set_course_code($gradebookCategory->getCourseCode()); |
||
| 548 | $category->set_parent_id($gradebookCategory->getParentId()); |
||
| 549 | $category->set_weight($gradebookCategory->getWeight()); |
||
| 550 | $category->set_visible($gradebookCategory->getVisible()); |
||
| 551 | $category->set_session_id($gradebookCategory->getSessionId()); |
||
| 552 | $category->set_certificate_min_score( |
||
| 553 | $gradebookCategory->getCertifMinScore() |
||
| 554 | ); |
||
| 555 | $category->set_grade_model_id($gradebookCategory->getGradeModelId()); |
||
| 556 | $category->set_locked($gradebookCategory->getLocked()); |
||
| 557 | $category->setGenerateCertificates( |
||
| 558 | $gradebookCategory->getGenerateCertificates() |
||
| 559 | ); |
||
| 560 | $category->setIsRequirement($gradebookCategory->getIsRequirement()); |
||
| 561 | |||
| 562 | return $category; |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Insert this category into the database |
||
| 567 | */ |
||
| 568 | public function add() |
||
| 569 | { |
||
| 570 | if (isset($this->name) && '-1' == $this->name) { |
||
| 571 | return false; |
||
| 572 | } |
||
| 573 | |||
| 574 | if (isset($this->name) && isset($this->user_id)) { |
||
| 575 | $em = Database::getManager(); |
||
| 576 | |||
| 577 | $courseInfo = api_get_course_info($this->course_code); |
||
| 578 | $course = api_get_user_course_entity($courseInfo['real_id']); |
||
| 579 | |||
| 580 | $category = new GradebookCategory(); |
||
| 581 | $category->setName($this->name); |
||
| 582 | $category->setDescription($this->description); |
||
| 583 | $category->setUserId($this->user_id); |
||
| 584 | $category->setCourse($course); |
||
| 585 | $category->setParentId($this->parent); |
||
| 586 | $category->setWeight($this->weight); |
||
| 587 | $category->setVisible($this->visible); |
||
| 588 | $category->setCertifMinScore($this->certificate_min_score); |
||
| 589 | $category->setSessionId($this->session_id); |
||
| 590 | $category->setGenerateCertificates($this->generateCertificates); |
||
| 591 | $category->setGradeModelId($this->grade_model_id); |
||
| 592 | $category->setIsRequirement($this->isRequirement); |
||
| 593 | $category->setLocked(false); |
||
| 594 | |||
| 595 | $em->persist($category); |
||
| 596 | $em->flush(); |
||
| 597 | |||
| 598 | $id = $category->getId(); |
||
| 599 | $this->set_id($id); |
||
| 600 | |||
| 601 | View Code Duplication | if (!empty($id)) { |
|
| 602 | $parent_id = $this->get_parent_id(); |
||
| 603 | $grade_model_id = $this->get_grade_model_id(); |
||
| 604 | if ($parent_id == 0) { |
||
| 605 | //do something |
||
| 606 | if (isset($grade_model_id) && !empty($grade_model_id) && $grade_model_id != '-1') { |
||
| 607 | $obj = new GradeModel(); |
||
| 608 | $components = $obj->get_components($grade_model_id); |
||
| 609 | $default_weight_setting = api_get_setting('gradebook_default_weight'); |
||
| 610 | $default_weight = 100; |
||
| 611 | if (isset($default_weight_setting)) { |
||
| 612 | $default_weight = $default_weight_setting; |
||
| 613 | } |
||
| 614 | foreach ($components as $component) { |
||
| 615 | $gradebook = new Gradebook(); |
||
| 616 | $params = array(); |
||
| 617 | |||
| 618 | $params['name'] = $component['acronym']; |
||
| 619 | $params['description'] = $component['title']; |
||
| 620 | $params['user_id'] = api_get_user_id(); |
||
| 621 | $params['parent_id'] = $id; |
||
| 622 | $params['weight'] = $component['percentage'] / 100 * $default_weight; |
||
| 623 | $params['session_id'] = api_get_session_id(); |
||
| 624 | $params['course_code'] = $this->get_course_code(); |
||
| 625 | |||
| 626 | $gradebook->save($params); |
||
| 627 | } |
||
| 628 | } |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | $gradebook = new Gradebook(); |
||
| 633 | $gradebook->update_skills_to_gradebook( |
||
| 634 | $this->id, |
||
| 635 | $this->get_skills(false) |
||
| 636 | ); |
||
| 637 | |||
| 638 | return $id; |
||
| 639 | } |
||
| 640 | } |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Update the properties of this category in the database |
||
| 644 | * @todo fix me |
||
| 645 | */ |
||
| 646 | public function save() |
||
| 647 | { |
||
| 648 | $em = Database::getManager(); |
||
| 649 | |||
| 650 | /** @var GradebookCategory $gradebookCategory */ |
||
| 651 | $gradebookCategory = $em |
||
| 652 | ->getRepository('ChamiloCoreBundle:GradebookCategory') |
||
| 653 | ->find($this->id); |
||
| 654 | |||
| 655 | if (empty($gradebookCategory)) { |
||
| 656 | return false; |
||
| 657 | } |
||
| 658 | |||
| 659 | $course = api_get_user_course_entity(); |
||
| 660 | |||
| 661 | $gradebookCategory->setName($this->name); |
||
| 662 | $gradebookCategory->setDescription($this->description); |
||
| 663 | $gradebookCategory->setUserId($this->user_id); |
||
| 664 | $gradebookCategory->setCourse($course); |
||
| 665 | //$gradebookCategory->setCourseCode($this->course_code); |
||
| 666 | $gradebookCategory->setParentId($this->parent); |
||
| 667 | $gradebookCategory->setWeight($this->weight); |
||
| 668 | $gradebookCategory->setVisible($this->visible); |
||
| 669 | $gradebookCategory->setCertifMinScore($this->certificate_min_score); |
||
| 670 | $gradebookCategory->setGenerateCertificates( |
||
| 671 | $this->generateCertificates |
||
| 672 | ); |
||
| 673 | $gradebookCategory->setGradeModelId($this->grade_model_id); |
||
| 674 | $gradebookCategory->setIsRequirement($this->isRequirement); |
||
| 675 | |||
| 676 | $em->merge($gradebookCategory); |
||
| 677 | $em->flush(); |
||
| 678 | |||
| 679 | if (!empty($this->id)) { |
||
| 680 | $parent_id = $this->get_parent_id(); |
||
| 681 | $grade_model_id = $this->get_grade_model_id(); |
||
| 682 | View Code Duplication | if ($parent_id == 0) { |
|
| 683 | if (isset($grade_model_id) && !empty($grade_model_id) && $grade_model_id != '-1') { |
||
| 684 | $obj = new GradeModel(); |
||
| 685 | $components = $obj->get_components($grade_model_id); |
||
| 686 | $default_weight_setting = api_get_setting('gradebook_default_weight'); |
||
| 687 | $default_weight = 100; |
||
| 688 | if (isset($default_weight_setting)) { |
||
| 689 | $default_weight = $default_weight_setting; |
||
| 690 | } |
||
| 691 | $final_weight = $this->get_weight(); |
||
| 692 | if (!empty($final_weight)) { |
||
| 693 | $default_weight = $this->get_weight(); |
||
| 694 | } |
||
| 695 | foreach ($components as $component) { |
||
| 696 | $gradebook = new Gradebook(); |
||
| 697 | $params = array(); |
||
| 698 | $params['name'] = $component['acronym']; |
||
| 699 | $params['description'] = $component['title']; |
||
| 700 | $params['user_id'] = api_get_user_id(); |
||
| 701 | $params['parent_id'] = $this->id; |
||
| 702 | $params['weight'] = $component['percentage'] / 100 * $default_weight; |
||
| 703 | $params['session_id'] = api_get_session_id(); |
||
| 704 | $params['course_code'] = $this->get_course_code(); |
||
| 705 | $gradebook->save($params); |
||
| 706 | } |
||
| 707 | } |
||
| 708 | } |
||
| 709 | } |
||
| 710 | |||
| 711 | $gradebook = new Gradebook(); |
||
| 712 | $gradebook->update_skills_to_gradebook( |
||
| 713 | $this->id, |
||
| 714 | $this->get_skills(false), |
||
| 715 | true |
||
| 716 | ); |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Update link weights see #5168 |
||
| 721 | * @param type $new_weight |
||
| 722 | */ |
||
| 723 | public function update_children_weight($new_weight) |
||
| 724 | { |
||
| 725 | $links = $this->get_links(); |
||
| 726 | $old_weight = $this->get_weight(); |
||
| 727 | |||
| 728 | if (!empty($links)) { |
||
| 729 | foreach ($links as $link_item) { |
||
| 730 | if (isset($link_item)) { |
||
| 731 | $new_item_weight = $new_weight * $link_item->get_weight() / $old_weight; |
||
| 732 | $link_item->set_weight($new_item_weight); |
||
| 733 | $link_item->save(); |
||
| 734 | } |
||
| 735 | } |
||
| 736 | } |
||
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Delete this evaluation from the database |
||
| 741 | */ |
||
| 742 | public function delete() |
||
| 743 | { |
||
| 744 | $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 745 | $sql = 'DELETE FROM '.$table.' WHERE id = '.intval($this->id); |
||
| 746 | Database::query($sql); |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Not delete this category from the database,when visible=3 is category eliminated |
||
| 751 | * @param int $courseId |
||
| 752 | */ |
||
| 753 | View Code Duplication | public function update_category_delete($courseId) |
|
| 754 | { |
||
| 755 | $tbl_grade_categories = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 756 | $sql = 'UPDATE '.$tbl_grade_categories.' SET visible=3 |
||
| 757 | WHERE c_id ="'.intval($courseId).'"'; |
||
| 758 | Database::query($sql); |
||
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Delete this category from the database |
||
| 763 | * @param int $courseId |
||
| 764 | */ |
||
| 765 | View Code Duplication | public static function deleteCategoryFromCourse($courseId) |
|
| 766 | { |
||
| 767 | $table = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 768 | $sql = 'DELETE FROM '.$table.' |
||
| 769 | WHERE c_id ="'.intval($courseId).'"'; |
||
| 770 | Database::query($sql); |
||
| 771 | } |
||
| 772 | |||
| 773 | /** |
||
| 774 | * @param int $course_id |
||
| 775 | * @return bool|string |
||
| 776 | */ |
||
| 777 | public function show_message_resource_delete($course_id) |
||
| 778 | { |
||
| 779 | $tbl_grade_categories = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 780 | $sql = 'SELECT count(*) AS num |
||
| 781 | FROM '.$tbl_grade_categories.' |
||
| 782 | WHERE |
||
| 783 | c_id = "'.Database::escape_string($course_id).'" AND |
||
| 784 | visible=3'; |
||
| 785 | $res = Database::query($sql); |
||
| 786 | $option = Database::fetch_array($res, 'ASSOC'); |
||
| 787 | if ($option['num'] >= 1) { |
||
| 788 | return ' <span class="resource-deleted">( '.get_lang('ResourceDeleted').' )</span>'; |
||
| 789 | } else { |
||
| 790 | return false; |
||
| 791 | } |
||
| 792 | } |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Shows all information of an category |
||
| 796 | */ |
||
| 797 | View Code Duplication | public function shows_all_information_an_category($selectcat = '') |
|
| 798 | { |
||
| 799 | if ($selectcat == '') { |
||
| 800 | return null; |
||
| 801 | } else { |
||
| 802 | $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 803 | $sql = 'SELECT |
||
| 804 | name, |
||
| 805 | description, |
||
| 806 | user_id, |
||
| 807 | c_id, |
||
| 808 | parent_id, |
||
| 809 | weight, |
||
| 810 | visible, |
||
| 811 | certif_min_score, |
||
| 812 | session_id, |
||
| 813 | generate_certificates, |
||
| 814 | is_requirement |
||
| 815 | FROM '.$tbl_category.' c |
||
| 816 | WHERE c.id='.intval($selectcat); |
||
| 817 | $result = Database::query($sql); |
||
| 818 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 819 | |||
| 820 | return $row; |
||
| 821 | } |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Check if a category name (with the same parent category) already exists |
||
| 826 | * @param string $name name to check (if not given, the name property of this object will be checked) |
||
| 827 | * @param int $parent parent category |
||
| 828 | * |
||
| 829 | * @return bool |
||
| 830 | */ |
||
| 831 | public function does_name_exist($name, $parent) |
||
| 832 | { |
||
| 833 | if (!isset($name)) { |
||
| 834 | $name = $this->name; |
||
| 835 | $parent = $this->parent; |
||
| 836 | } |
||
| 837 | $tbl_grade_categories = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY); |
||
| 838 | $sql = "SELECT count(id) AS number |
||
| 839 | FROM $tbl_grade_categories |
||
| 840 | WHERE name = '".Database::escape_string($name)."'"; |
||
| 841 | |||
| 842 | if (api_is_allowed_to_edit()) { |
||
| 843 | $parent = self::load($parent); |
||
| 844 | $code = $parent[0]->get_course_code(); |
||
| 845 | $courseInfo = api_get_course_info($code); |
||
| 846 | $courseId = $courseInfo['real_id']; |
||
| 847 | if (isset($code) && $code != '0') { |
||
| 848 | $main_course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 849 | $sql .= ' AND user_id IN ( |
||
| 850 | SELECT user_id FROM '.$main_course_user_table.' |
||
| 851 | WHERE c_id = '.$courseId.' AND status = '.COURSEMANAGER.' |
||
| 852 | )'; |
||
| 853 | } else { |
||
| 854 | $sql .= ' AND user_id = '.api_get_user_id(); |
||
| 855 | } |
||
| 856 | } else { |
||
| 857 | $sql .= ' AND user_id = '.api_get_user_id(); |
||
| 858 | } |
||
| 859 | |||
| 860 | if (!isset($parent)) { |
||
| 861 | $sql .= ' AND parent_id is null'; |
||
| 862 | } else { |
||
| 863 | $sql .= ' AND parent_id = '.intval($parent); |
||
| 864 | } |
||
| 865 | |||
| 866 | $result = Database::query($sql); |
||
| 867 | $number = Database::fetch_row($result); |
||
| 868 | |||
| 869 | return $number[0] != 0; |
||
| 870 | } |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Checks if the certificate is available for the given user in this category |
||
| 874 | * @param integer $user_id User ID |
||
| 875 | * @return boolean True if conditions match, false if fails |
||
| 876 | */ |
||
| 877 | public function is_certificate_available($user_id) |
||
| 878 | { |
||
| 879 | $score = $this->calc_score( |
||
| 880 | $user_id, |
||
| 881 | null, |
||
| 882 | $this->course_code, |
||
| 883 | $this->session_id |
||
| 884 | ); |
||
| 885 | |||
| 886 | if (isset($score) && isset($score[0])) { |
||
| 887 | // Get a percentage score to compare to minimum certificate score |
||
| 888 | // $certification_score = $score[0] / $score[1] * 100; |
||
| 889 | // Get real score not a percentage. |
||
| 890 | $certification_score = $score[0]; |
||
| 891 | if ($certification_score >= $this->certificate_min_score) { |
||
| 892 | return true; |
||
| 893 | } |
||
| 894 | } |
||
| 895 | |||
| 896 | return false; |
||
| 897 | } |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Is this category a course ? |
||
| 901 | * A category is a course if it has a course code and no parent category. |
||
| 902 | */ |
||
| 903 | public function is_course() |
||
| 904 | { |
||
| 905 | return (isset($this->course_code) && !empty($this->course_code) |
||
| 906 | && (!isset($this->parent) || $this->parent == 0)); |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Calculate the score of this category |
||
| 911 | * @param integer $stud_id student id (default: all students - then the average is returned) |
||
| 912 | * @param integer $session_id |
||
| 913 | * @param string $course_code |
||
| 914 | * @param int $session_id |
||
| 915 | * @return array (score sum, weight sum) |
||
| 916 | * or null if no scores available |
||
| 917 | */ |
||
| 918 | public function calc_score( |
||
| 919 | $stud_id = null, |
||
| 920 | $type = null, |
||
| 921 | $course_code = '', |
||
| 922 | $session_id = null |
||
| 923 | ) { |
||
| 924 | // Classic |
||
| 925 | if (!empty($stud_id) && $type == '') { |
||
| 926 | if (!empty($course_code)) { |
||
| 927 | $cats = $this->get_subcategories( |
||
| 928 | $stud_id, |
||
| 929 | $course_code, |
||
| 930 | $session_id |
||
| 931 | ); |
||
| 932 | $evals = $this->get_evaluations($stud_id, false, $course_code); |
||
| 933 | $links = $this->get_links($stud_id, false, $course_code); |
||
| 934 | } else { |
||
| 935 | $cats = $this->get_subcategories($stud_id); |
||
| 936 | $evals = $this->get_evaluations($stud_id); |
||
| 937 | $links = $this->get_links($stud_id); |
||
| 938 | } |
||
| 939 | |||
| 940 | // Calculate score |
||
| 941 | $count = 0; |
||
| 942 | $ressum = 0; |
||
| 943 | $weightsum = 0; |
||
| 944 | |||
| 945 | View Code Duplication | if (!empty($cats)) { |
|
| 946 | /** @var Category $cat */ |
||
| 947 | foreach ($cats as $cat) { |
||
| 948 | $cat->set_session_id($session_id); |
||
| 949 | $cat->set_course_code($course_code); |
||
| 950 | $cat->setStudentList($this->getStudentList()); |
||
| 951 | $score = $cat->calc_score( |
||
| 952 | $stud_id, |
||
| 953 | null, |
||
| 954 | $course_code, |
||
| 955 | $session_id |
||
| 956 | ); |
||
| 957 | |||
| 958 | $catweight = 0; |
||
| 959 | if ($cat->get_weight() != 0) { |
||
| 960 | $catweight = $cat->get_weight(); |
||
| 961 | $weightsum += $catweight; |
||
| 962 | } |
||
| 963 | |||
| 964 | if (isset($score) && !empty($score[1]) && !empty($catweight)) { |
||
| 965 | $ressum += $score[0] / $score[1] * $catweight; |
||
| 966 | } |
||
| 967 | } |
||
| 968 | } |
||
| 969 | |||
| 970 | $students = array(); |
||
| 971 | if (!empty($evals)) { |
||
| 972 | /** @var Evaluation $eval */ |
||
| 973 | foreach ($evals as $eval) { |
||
| 974 | $eval->setStudentList($this->getStudentList()); |
||
| 975 | $evalres = $eval->calc_score($stud_id, null); |
||
| 976 | |||
| 977 | if (isset($evalres) && $eval->get_weight() != 0) { |
||
| 978 | $evalweight = $eval->get_weight(); |
||
| 979 | $weightsum += $evalweight; |
||
| 980 | $count++; |
||
| 981 | View Code Duplication | if (!empty($evalres[1])) { |
|
| 982 | $ressum += $evalres[0] / $evalres[1] * $evalweight; |
||
| 983 | } |
||
| 984 | } else { |
||
| 985 | if ($eval->get_weight() != 0) { |
||
| 986 | $evalweight = $eval->get_weight(); |
||
| 987 | $weightsum += $evalweight; |
||
| 988 | } |
||
| 989 | } |
||
| 990 | } |
||
| 991 | } |
||
| 992 | |||
| 993 | if (!empty($links)) { |
||
| 994 | /** @var EvalLink|ExerciseLink $link */ |
||
| 995 | foreach ($links as $link) { |
||
| 996 | $link->setStudentList($this->getStudentList()); |
||
| 997 | |||
| 998 | if ($session_id) { |
||
| 999 | $link->set_session_id($session_id); |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | $linkres = $link->calc_score($stud_id, null); |
||
| 1003 | if (!empty($linkres) && $link->get_weight() != 0) { |
||
| 1004 | $students[$stud_id] = $linkres[0]; |
||
| 1005 | $linkweight = $link->get_weight(); |
||
| 1006 | $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1]; |
||
| 1007 | $count++; |
||
| 1008 | $weightsum += $linkweight; |
||
| 1009 | $ressum += $linkres[0] / $link_res_denom * $linkweight; |
||
| 1010 | } else { |
||
| 1011 | // Adding if result does not exists |
||
| 1012 | if ($link->get_weight() != 0) { |
||
| 1013 | $linkweight = $link->get_weight(); |
||
| 1014 | $weightsum += $linkweight; |
||
| 1015 | } |
||
| 1016 | } |
||
| 1017 | } |
||
| 1018 | } |
||
| 1019 | } else { |
||
| 1020 | if (!empty($course_code)) { |
||
| 1021 | $cats = $this->get_subcategories( |
||
| 1022 | null, |
||
| 1023 | $course_code, |
||
| 1024 | $session_id |
||
| 1025 | ); |
||
| 1026 | $evals = $this->get_evaluations(null, false, $course_code); |
||
| 1027 | $links = $this->get_links(null, false, $course_code); |
||
| 1028 | } else { |
||
| 1029 | $cats = $this->get_subcategories(null); |
||
| 1030 | $evals = $this->get_evaluations(null); |
||
| 1031 | $links = $this->get_links(null); |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | // Calculate score |
||
| 1035 | $count = 0; |
||
| 1036 | $ressum = 0; |
||
| 1037 | $weightsum = 0; |
||
| 1038 | $bestResult = 0; |
||
| 1039 | |||
| 1040 | View Code Duplication | if (!empty($cats)) { |
|
| 1041 | /** @var Category $cat */ |
||
| 1042 | foreach ($cats as $cat) { |
||
| 1043 | $cat->setStudentList($this->getStudentList()); |
||
| 1044 | $score = $cat->calc_score( |
||
| 1045 | null, |
||
| 1046 | $type, |
||
| 1047 | $course_code, |
||
| 1048 | $session_id |
||
| 1049 | ); |
||
| 1050 | |||
| 1051 | $catweight = 0; |
||
| 1052 | if ($cat->get_weight() != 0) { |
||
| 1053 | $catweight = $cat->get_weight(); |
||
| 1054 | $weightsum += $catweight; |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | if (isset($score) && !empty($score[1]) && !empty($catweight)) { |
||
| 1058 | $ressum += $score[0] / $score[1] * $catweight; |
||
| 1059 | |||
| 1060 | if ($ressum > $bestResult) { |
||
| 1061 | $bestResult = $ressum; |
||
| 1062 | } |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | } |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | if (!empty($evals)) { |
||
| 1069 | /** @var Evaluation $eval */ |
||
| 1070 | foreach ($evals as $eval) { |
||
| 1071 | $evalres = $eval->calc_score(null, $type); |
||
| 1072 | $eval->setStudentList($this->getStudentList()); |
||
| 1073 | |||
| 1074 | if (isset($evalres) && $eval->get_weight() != 0) { |
||
| 1075 | $evalweight = $eval->get_weight(); |
||
| 1076 | $weightsum += $evalweight; |
||
| 1077 | $count++; |
||
| 1078 | View Code Duplication | if (!empty($evalres[1])) { |
|
| 1079 | $ressum += $evalres[0] / $evalres[1] * $evalweight; |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | if ($ressum > $bestResult) { |
||
| 1083 | $bestResult = $ressum; |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | } else { |
||
| 1087 | if ($eval->get_weight() != 0) { |
||
| 1088 | $evalweight = $eval->get_weight(); |
||
| 1089 | $weightsum += $evalweight; |
||
| 1090 | } |
||
| 1091 | } |
||
| 1092 | } |
||
| 1093 | } |
||
| 1094 | if (!empty($links)) { |
||
| 1095 | /** @var EvalLink|ExerciseLink $link */ |
||
| 1096 | foreach ($links as $link) { |
||
| 1097 | $link->setStudentList($this->getStudentList()); |
||
| 1098 | |||
| 1099 | if ($session_id) { |
||
| 1100 | $link->set_session_id($session_id); |
||
| 1101 | } |
||
| 1102 | |||
| 1103 | $linkres = $link->calc_score($stud_id, $type); |
||
| 1104 | if (!empty($linkres) && $link->get_weight() != 0) { |
||
| 1105 | $students[$stud_id] = $linkres[0]; |
||
| 1106 | $linkweight = $link->get_weight(); |
||
| 1107 | $link_res_denom = $linkres[1] == 0 ? 1 : $linkres[1]; |
||
| 1108 | |||
| 1109 | $count++; |
||
| 1110 | $weightsum += $linkweight; |
||
| 1111 | $ressum += $linkres[0] / $link_res_denom * $linkweight; |
||
| 1112 | |||
| 1113 | if ($ressum > $bestResult) { |
||
| 1114 | $bestResult = $ressum; |
||
| 1115 | } |
||
| 1116 | } else { |
||
| 1117 | // Adding if result does not exists |
||
| 1118 | if ($link->get_weight() != 0) { |
||
| 1119 | $linkweight = $link->get_weight(); |
||
| 1120 | $weightsum += $linkweight; |
||
| 1121 | } |
||
| 1122 | } |
||
| 1123 | } |
||
| 1124 | } |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | switch ($type) { |
||
| 1128 | case 'best': |
||
| 1129 | if (empty($bestResult)) { |
||
| 1130 | return null; |
||
| 1131 | } |
||
| 1132 | return array($bestResult, $weightsum); |
||
| 1133 | break; |
||
| 1134 | case 'average': |
||
| 1135 | if (empty($ressum)) { |
||
| 1136 | return null; |
||
| 1137 | } |
||
| 1138 | return array($ressum, $weightsum); |
||
| 1139 | break; |
||
| 1140 | case 'ranking': |
||
| 1141 | // category ranking is calculated in gradebook_data_generator.class.php |
||
| 1142 | // function get_data |
||
| 1143 | return null; |
||
| 1144 | return AbstractLink::getCurrentUserRanking($stud_id, array()); |
||
| 1145 | break; |
||
| 1146 | default: |
||
| 1147 | return array($ressum, $weightsum); |
||
| 1148 | break; |
||
| 1149 | } |
||
| 1150 | } |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Delete this category and every subcategory, evaluation and result inside |
||
| 1154 | */ |
||
| 1155 | public function delete_all() |
||
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Return array of Category objects where a student is subscribed to. |
||
| 1203 | * |
||
| 1204 | * @param integer $stud_id |
||
| 1205 | * @param string $course_code |
||
| 1206 | * @param integer $session_id |
||
| 1207 | * @return array |
||
| 1208 | */ |
||
| 1209 | public function get_root_categories_for_student( |
||
| 1210 | $stud_id, |
||
| 1211 | $course_code = null, |
||
| 1212 | $session_id = null |
||
| 1292 | |||
| 1293 | /** |
||
| 1294 | * Return array of Category objects where a teacher is admin for. |
||
| 1295 | * |
||
| 1296 | * @param integer $user_id (to return everything, use 'null' here) |
||
| 1297 | * @param string $course_code (optional) |
||
| 1298 | * @param integer $session_id (optional) |
||
| 1299 | * @return array |
||
| 1300 | */ |
||
| 1301 | public function get_root_categories_for_teacher( |
||
| 1348 | |||
| 1349 | /** |
||
| 1350 | * Can this category be moved to somewhere else ? |
||
| 1351 | * The root and courses cannot be moved. |
||
| 1352 | * @return bool |
||
| 1353 | */ |
||
| 1354 | public function is_movable() |
||
| 1358 | |||
| 1359 | /** |
||
| 1360 | * Generate an array of possible categories where this category can be moved to. |
||
| 1361 | * Notice: its own parent will be included in the list: it's up to the frontend |
||
| 1362 | * to disable this element. |
||
| 1363 | * @return array 2-dimensional array - every element contains 3 subelements (id, name, level) |
||
| 1364 | */ |
||
| 1365 | public function get_target_categories() |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Internal function used by get_target_categories() |
||
| 1408 | * @param array $targets |
||
| 1409 | * @param integer $level |
||
| 1410 | * @param int $catid |
||
| 1411 | * |
||
| 1412 | * @return array |
||
| 1413 | */ |
||
| 1414 | View Code Duplication | private function add_target_subcategories($targets, $level, $catid) |
|
| 1434 | |||
| 1435 | /** |
||
| 1436 | * Internal function used by get_target_categories() and add_target_subcategories() |
||
| 1437 | * Can this category be moved to the given category ? |
||
| 1438 | * Impossible when origin and target are the same... children won't be processed |
||
| 1439 | * either. (a category can't be moved to one of its own children) |
||
| 1440 | */ |
||
| 1441 | private function can_be_moved_to_cat($cat) |
||
| 1445 | |||
| 1446 | /** |
||
| 1447 | * Move this category to the given category. |
||
| 1448 | * If this category moves from inside a course to outside, |
||
| 1449 | * its course code must be changed, as well as the course code |
||
| 1450 | * of all underlying categories and evaluations. All links will |
||
| 1451 | * be deleted as well ! |
||
| 1452 | */ |
||
| 1453 | public function move_to_cat($cat) |
||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Internal function used by move_to_cat() |
||
| 1465 | */ |
||
| 1466 | private function apply_course_code_to_children() |
||
| 1495 | |||
| 1496 | /** |
||
| 1497 | * Generate an array of all categories the user can navigate to |
||
| 1498 | */ |
||
| 1499 | public function get_tree() |
||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Internal function used by get_tree() |
||
| 1528 | * @param integer $level |
||
| 1529 | * @param null|integer $visible |
||
| 1530 | * @return array |
||
| 1531 | */ |
||
| 1532 | private function add_subtree($targets, $level, $catid, $visible) |
||
| 1554 | |||
| 1555 | /** |
||
| 1556 | * Generate an array of courses that a teacher hasn't created a category for. |
||
| 1557 | * @param integer $user_id |
||
| 1558 | * @return array 2-dimensional array - every element contains 2 subelements (code, title) |
||
| 1559 | */ |
||
| 1560 | public function get_not_created_course_categories($user_id) |
||
| 1590 | |||
| 1591 | /** |
||
| 1592 | * Generate an array of all courses that a teacher is admin of. |
||
| 1593 | * @param integer $user_id |
||
| 1594 | * @return array 2-dimensional array - every element contains 2 subelements (code, title) |
||
| 1595 | */ |
||
| 1596 | public function get_all_courses($user_id) |
||
| 1615 | |||
| 1616 | /** |
||
| 1617 | * Apply the same visibility to every subcategory, evaluation and link |
||
| 1618 | */ |
||
| 1619 | public function apply_visibility_to_children() |
||
| 1652 | |||
| 1653 | /** |
||
| 1654 | * Check if a category contains evaluations with a result for a given student |
||
| 1655 | */ |
||
| 1656 | public function has_evaluations_with_results_for_student($stud_id) |
||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * Retrieve all categories inside a course independent category |
||
| 1684 | * that should be visible to a student. |
||
| 1685 | * @param integer $cat_id parent category |
||
| 1686 | * @param $stud_id student id |
||
| 1687 | * @param array $cats optional: if defined, the categories will be added to this array |
||
| 1688 | * @return array |
||
| 1689 | */ |
||
| 1690 | public function get_independent_categories_with_result_for_student( |
||
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Return the session id (in any case, even if it's null or 0) |
||
| 1718 | * @return int Session id (can be null) |
||
| 1719 | */ |
||
| 1720 | public function get_session_id() |
||
| 1724 | |||
| 1725 | /** |
||
| 1726 | * Get appropriate subcategories visible for the user (and optionally the course and session) |
||
| 1727 | * @param int $stud_id student id (default: all students) |
||
| 1728 | * @param string $course_code Course code (optional) |
||
| 1729 | * @param int $session_id Session ID (optional) |
||
| 1730 | * @param bool $order |
||
| 1731 | |||
| 1732 | * @return array Array of subcategories |
||
| 1733 | */ |
||
| 1734 | public function get_subcategories( |
||
| 1793 | |||
| 1794 | /** |
||
| 1795 | * Get appropriate evaluations visible for the user |
||
| 1796 | * @param int $stud_id student id (default: all students) |
||
| 1797 | * @param boolean $recursive process subcategories (default: no recursion) |
||
| 1798 | * @param string $course_code |
||
| 1799 | * @param int $sessionId |
||
| 1800 | * |
||
| 1801 | * @return array |
||
| 1802 | */ |
||
| 1803 | public function get_evaluations( |
||
| 1866 | |||
| 1867 | /** |
||
| 1868 | * Get appropriate links visible for the user |
||
| 1869 | * @param int $stud_id student id (default: all students) |
||
| 1870 | * @param boolean $recursive process subcategories (default: no recursion) |
||
| 1871 | * @param string $course_code |
||
| 1872 | * @param int $sessionId |
||
| 1873 | * |
||
| 1874 | * @return array |
||
| 1875 | */ |
||
| 1876 | public function get_links( |
||
| 1940 | |||
| 1941 | /** |
||
| 1942 | * Get all the categories from with the same given direct parent |
||
| 1943 | * @param int $catId Category parent ID |
||
| 1944 | * @return array Array of Category objects |
||
| 1945 | */ |
||
| 1946 | View Code Duplication | public function getCategories($catId) |
|
| 1957 | |||
| 1958 | /** |
||
| 1959 | * Gets the type for the current object |
||
| 1960 | * @return string 'C' to represent "Category" object type |
||
| 1961 | */ |
||
| 1962 | public function get_item_type() |
||
| 1966 | |||
| 1967 | /** |
||
| 1968 | * @param array $skills |
||
| 1969 | */ |
||
| 1970 | public function set_skills($skills) |
||
| 1974 | |||
| 1975 | /** |
||
| 1976 | * @return null |
||
| 1977 | */ |
||
| 1978 | public function get_date() |
||
| 1982 | |||
| 1983 | /** |
||
| 1984 | * @return string |
||
| 1985 | */ |
||
| 1986 | public function get_icon_name() |
||
| 1990 | |||
| 1991 | /** |
||
| 1992 | * Find category by name |
||
| 1993 | * @param string $name_mask search string |
||
| 1994 | * @return array category objects matching the search criterium |
||
| 1995 | */ |
||
| 1996 | public function find_category($name_mask, $allcat) |
||
| 2007 | |||
| 2008 | /** |
||
| 2009 | * This function, locks a category , only one who can unlock it is |
||
| 2010 | * the platform administrator. |
||
| 2011 | * @param int locked 1 or unlocked 0 |
||
| 2012 | |||
| 2013 | * @return boolean|null |
||
| 2014 | * */ |
||
| 2015 | View Code Duplication | public function lock($locked) |
|
| 2022 | |||
| 2023 | /** |
||
| 2024 | * @param $locked |
||
| 2025 | */ |
||
| 2026 | public function lock_all_items($locked) |
||
| 2051 | |||
| 2052 | /** |
||
| 2053 | * Generates a certificate for this user if everything matches |
||
| 2054 | * @param int $category_id |
||
| 2055 | * @param int $user_id |
||
| 2056 | * @return bool|string |
||
| 2057 | */ |
||
| 2058 | public static function register_user_certificate($category_id, $user_id) |
||
| 2209 | |||
| 2210 | /** |
||
| 2211 | * @param int $catId |
||
| 2212 | * @param array $userList |
||
| 2213 | */ |
||
| 2214 | public static function generateCertificatesInUserList($catId, $userList) |
||
| 2222 | |||
| 2223 | /** |
||
| 2224 | * @param int $catId |
||
| 2225 | * @param array $userList |
||
| 2226 | */ |
||
| 2227 | public static function exportAllCertificates( |
||
| 2276 | |||
| 2277 | /** |
||
| 2278 | * @param int $catId |
||
| 2279 | */ |
||
| 2280 | public static function deleteAllCertificates($catId) |
||
| 2296 | |||
| 2297 | /** |
||
| 2298 | * Check whether a user has finished a course by its gradebook |
||
| 2299 | * @param int $userId The user ID |
||
| 2300 | * @param \Category $category Optional. The gradebook category. |
||
| 2301 | * To check by the gradebook category |
||
| 2302 | * @param int $categoryId Optional. The gradebook category ID. |
||
| 2303 | * To check by the category ID |
||
| 2304 | * @param string $courseCode Optional. The course code |
||
| 2305 | * @param int $sessionId Optional. The session ID |
||
| 2306 | * @param boolean $recalcutateScore Whether recalculate the score |
||
| 2307 | * @return boolean |
||
| 2308 | */ |
||
| 2309 | public static function userFinishedCourse( |
||
| 2354 | |||
| 2355 | /** |
||
| 2356 | * Get the current score (as percentage) on a gradebook category for a user |
||
| 2357 | * @param int $userId The user id |
||
| 2358 | * @param int $categoryId The gradebook category |
||
| 2359 | * @param int $courseCode The course code |
||
| 2360 | * @param int $sessionId Optional. The session id |
||
| 2361 | * @param bool $recalculate |
||
| 2362 | * |
||
| 2363 | * @return float The score |
||
| 2364 | */ |
||
| 2365 | public static function getCurrentScore( |
||
| 2395 | |||
| 2396 | /** |
||
| 2397 | * Calculate the current score on a gradebook category for a user |
||
| 2398 | * @param int $userId The user id |
||
| 2399 | * @param int $categoryId The gradebook category |
||
| 2400 | * @param int $courseCode The course code |
||
| 2401 | * @param int $sessionId Optional. The session id |
||
| 2402 | * @return float The score |
||
| 2403 | */ |
||
| 2404 | private static function calculateCurrentScore($userId, $categoryId, $courseCode, $sessionId) |
||
| 2440 | |||
| 2441 | /** |
||
| 2442 | * Register the current score for a user on a category gradebook |
||
| 2443 | * @param float $score The achieved score |
||
| 2444 | * @param int $userId The user id |
||
| 2445 | * @param int $categoryId The gradebook category |
||
| 2446 | * @return false|string The insert id |
||
| 2447 | */ |
||
| 2448 | public static function registerCurrentScore($score, $userId, $categoryId) |
||
| 2460 | |||
| 2461 | /** |
||
| 2462 | * @return array |
||
| 2463 | */ |
||
| 2464 | public function getStudentList() |
||
| 2468 | |||
| 2469 | /** |
||
| 2470 | * @param array $list |
||
| 2471 | */ |
||
| 2472 | public function setStudentList($list) |
||
| 2476 | |||
| 2477 | /** |
||
| 2478 | * @return int |
||
| 2479 | */ |
||
| 2480 | public function getCourseId() |
||
| 2484 | |||
| 2485 | /** |
||
| 2486 | * @param int $courseId |
||
| 2487 | * @return Category |
||
| 2488 | */ |
||
| 2489 | public function setCourseId($courseId) |
||
| 2495 | |||
| 2496 | } |
||
| 2497 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.