| 161 | | $record['tests'][$k]['TestsVclassroom']['title'] = $t['Test']['title']; |
| 162 | | $this->Test->Result->unbindAll(); |
| 163 | | $this->Test->Result->bindModel(array('belongsTo'=>array('Question','Test'))); |
| 164 | | |
| 165 | | $conditions = array('Result.vclassroom_id'=>$vclassroom_id,'Result.user_id'=>$user_id,'Result.test_id'=>$t['TestsVclassroom']['test_id']); |
| 166 | | $fields = array('Result.correct', 'Result.answer_id','Question.worth'); |
| 167 | | $results = $this->Test->Result->findAll($conditions, $fields); |
| 168 | | //debug($results); |
| 169 | | // Get points from answers |
| 170 | | if ( count($results) > 0 ): |
| 171 | | $points = (int) 0; |
| 172 | | else: |
| 173 | | $points = null; //student has not answered this test yet |
| 174 | | endif; |
| 175 | | |
| 176 | | foreach($results as $r): |
| 177 | | if ($r['Result']['correct'] == 1): |
| 178 | | $points += (int) $r['Question']['worth']; |
| 179 | | endif; |
| 180 | | endforeach; |
| 181 | | |
| 182 | | $record['tests'][$k]['TestsVclassroom']['points'] = $points; |
| 183 | | endforeach; |
| 184 | | //die(debug($record['tests'])); |
| | 159 | $record['tests'][$k]['TestsVclassroom']['points'] = $this->Test->getPoints($t['TestsVclassroom']['test_id'], $user_id, $vclassroom_id); |
| | 160 | endforeach; |
| | 161 | // die(debug($record['tests'])); |
| 426 | | // Consult Test Model |
| 427 | | /* $conditions = array('Result.vclassroom_id'=>$vclassroom_id, 'Result.user_id'=>$user_id); |
| 428 | | $fields = array('Result.points'); |
| 429 | | $tests = $this->Test->Result->findAll($conditions, $fields); |
| | 406 | // Consult TestsVclassroom Model |
| | 407 | $conditions = array('TestsVclassroom.vclassroom_id'=>$vclassroom_id); |
| | 408 | $fields = array('TestsVclassroom.test_id'); |
| | 409 | $tests = $this->TestsVclassroom->findAll($conditions, $fields); |
| | 410 | |
| 504 | | /** |
| 505 | | * Adds a join record between two records of a hasAndBelongsToMany association |
| 506 | | * |
| 507 | | * @param mixed $assoc The name of the HABTM association |
| 508 | | * @param mixed $assoc_ids The associated id or an array of associated ids |
| 509 | | * @param integer $id The id of the record in this model |
| 510 | | * @return boolean Success |
| 511 | | */ |
| 512 | | public function addAssoc($assoc, $assoc_ids, $id = null) |
| 513 | | { |
| 514 | | if ($id != null) |
| 515 | | { |
| 516 | | $this->id = $id; |
| 517 | | } |
| 518 | | |
| 519 | | $id = $this->id; |
| 520 | | |
| 521 | | if (is_array($this->id)) |
| 522 | | { |
| 523 | | $id = $this->id[0]; |
| 524 | | } |
| 525 | | |
| 526 | | if ($this->id !== null && $this->id !== false) |
| 527 | | { |
| 528 | | $db =& ConnectionManager::getDataSource($this->useDbConfig); |
| 529 | | |
| 530 | | $joinTable = $this->hasAndBelongsToMany[$assoc]['joinTable']; |
| 531 | | |
| 532 | | $table = $db->name($db->fullTableName($joinTable)); |
| 533 | | |
| 534 | | $keys[] = $this->hasAndBelongsToMany[$assoc]['foreignKey']; |
| 535 | | $keys[] = $this->hasAndBelongsToMany[$assoc]['associationForeignKey']; |
| 536 | | $fields = join(',', $keys); |
| 537 | | |
| 538 | | if ( !is_array($assoc_ids) ) |
| 539 | | { |
| 540 | | $assoc_ids = array($assoc_ids); |
| 541 | | } |
| 542 | | |
| 543 | | // to prevent duplicates |
| 544 | | $this->deleteAssoc($assoc,$assoc_ids,$id); |
| 545 | | |
| 546 | | foreach ($assoc_ids as $assoc_id) |
| 547 | | { |
| 548 | | $values[] = $db->value($id, $this->getColumnType($this->primaryKey)); |
| 549 | | $values[] = $db->value($assoc_id); |
| 550 | | $values = join(',', $values); |
| 551 | | |
| 552 | | $q = "INSERT INTO {$table} ({$fields}) VALUES ({$values})"; |
| 553 | | |
| 554 | | // echo $q; |
| 555 | | |
| 556 | | $db->execute($q); |
| 557 | | |
| 558 | | unset ($values); |
| 559 | | } |
| 560 | | |
| 561 | | return true; |
| 562 | | } |
| 563 | | else |
| 564 | | { |
| 565 | | return false; |
| 566 | | } |
| 567 | | } |
| 568 | | |
| 569 | | /** |
| 570 | | * Deletes any join records between two records of a hasAndBelongsToMany association |
| 571 | | * |
| 572 | | * @param integer $id The id of the record in this model |
| 573 | | * @param mixed $assoc The name of the HABTM association |
| 574 | | * @param mixed $assoc_ids The associated id or an array of associated ids |
| 575 | | * @return boolean Success |
| 576 | | */ |
| 577 | | public function deleteAssoc($assoc, $assoc_ids, $id = null) |
| 578 | | { |
| 579 | | if ($id != null) |
| 580 | | { |
| 581 | | $this->id = $id; |
| 582 | | } |
| 583 | | |
| 584 | | $id = $this->id; |
| 585 | | |
| 586 | | if (is_array($this->id)) |
| 587 | | { |
| 588 | | $id = $this->id[0]; |
| 589 | | } |
| 590 | | |
| 591 | | if ($this->id !== null && $this->id !== false) { |
| 592 | | $db =& ConnectionManager::getDataSource($this->useDbConfig); |
| 593 | | |
| 594 | | $joinTable = $this->hasAndBelongsToMany[$assoc]['joinTable']; |
| 595 | | $table = $db->name($db->fullTableName($joinTable)); |
| 596 | | |
| 597 | | $mainKey = $this->hasAndBelongsToMany[$assoc]['foreignKey']; |
| 598 | | $assocKey = $this->hasAndBelongsToMany[$assoc]['associationForeignKey']; |
| 599 | | |
| 600 | | if (!is_array($assoc_ids)) |
| 601 | | { |
| 602 | | $assoc_ids = array($assoc_ids); |
| 603 | | } |
| 604 | | |
| 605 | | foreach ($assoc_ids as $assoc_id) |
| 606 | | { |
| 607 | | $db->execute("DELETE FROM {$table} WHERE {$mainKey} = '{$id}' AND {$assocKey} = '{$assoc_id}'"); |
| 608 | | } |
| 609 | | |
| 610 | | return true; |
| 611 | | } |
| 612 | | else |
| 613 | | { |
| 614 | | return false; |
| 615 | | } |
| 616 | | } |