Changeset 746 for trunk/app/models

Show
Ignore:
Timestamp:
09/01/08 20:58:25 (3 months ago)
Author:
aarkerio
Message:

Little Bugs fixed

Location:
trunk/app/models
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/app/models/test.php

    r745 r746  
    7171 { 
    7272  $points = (int) 0; 
     73  $conditions = array('Result.test_id'=>$test_id, 'Result.user_id'=>$user_id, 'Result.vclassroom_id'=>$vclassroom_id); 
     74  $this->Result->unbindAll();  // removeds unnecesary stuff 
     75  $answers    = $this->Result->findAll($conditions);  // get the answers gived by student 
    7376 
    74   $conditions = array('Result.test_id'=>$test_id, 'Result.user_id'=>$user_id, 'Result.vclassroom_id'=>$vclassroom_id); 
    75    
    76   $this->Result->unbindAll();  // removeds unnecesary stuff 
     77  if ( count($answers) < 1):    // test not answer yet 
     78    return null; 
     79  endif; 
    7780 
    78   $answers    = $this->Result->findAll($conditions);  // get the answers gived by student 
    79    
    8081  foreach($answers as $a): 
    81     $question = $this->Question->find(array('Question.id'=>$a['Result']['question_id']), array('worth')); // how much points question have? 
     82    $question = $this->Question->find(array('Question.id'=>$a['Result']['question_id']), array('worth')); //how much points question have? 
    8283    foreach($question['Answer'] as $qa): 
    8384        if ($qa['id'] == $a['Result']['answer_id'] && $qa['question_id'] == $a['Result']['question_id'] && $qa['correct'] == 1): 
  • trunk/app/models/vclassroom.php

    r745 r746  
    140140 { 
    141141   // 1) wee need check tree models: Test, Webquest and Treasures ans results associated to them: 
    142    // Result, ResultTreasure and ResultWebquest, so will get the stundents answer   
     142   // Result, ResultTreasure and ResultWebquest, so will get the students answer   
    143143  try{    
    144144     $record                  = array(); 
     
    151151     // Consult Test Model associated to this Vclassroom 
    152152     $this->TestsVclassroom->bindModel(array('belongsTo'=>array('Test'))); 
    153       
    154153     $conditions        = array('TestsVclassroom.vclassroom_id'=>$vclassroom_id); 
    155154     $fields            = array('TestsVclassroom.test_id', 'TestsVclassroom.id', 'Test.title');  
    156155     $record['tests']   = $this->TestsVclassroom->findAll($conditions, $fields); 
    157      // die(debug($record)); 
    158156 
    159157     // foreach test check in Result model 
    160158     foreach($record['tests'] as $k => $t): 
    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'])); 
    185162 
    186163     // Now Treasure model 
     
    418395   endif;  
    419396 } 
    420  
     397/* 
     398 *  studentPoints  
     399 *    
     400 */ 
    421401 public function studentPoints($user_id, $vclassroom_id) 
    422402 { 
     
    424404     $points  = (int) 0; // student total points in eCourse  
    425405    
    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      
    430411     foreach ($tests as $test): 
    431             $points += (int) $this->Test->getPoints($test['test']['id'], $user_id, $vclassroom_id); 
    432      endforeach; 
    433      die('pints  '. $points);*/ 
     412            $points += (int) $this->Test->getPoints($test['TestsVclassroom']['test_id'], $user_id, $vclassroom_id); 
     413     endforeach; 
    434414      
    435415     // Consult Treasure Model 
     
    456436            $points += $pa['Participation']['points']; 
    457437     endforeach; 
    458    
     438 
     439     // Reports 
     440     $conditions     = array('Report.vclassroom_id'=>$vclassroom_id, 'Report.student_id'=>$user_id); 
     441     $fields         = array('Report.points'); //null;  
     442     $reports        = $this->Report->findAll($conditions, $fields); 
     443     foreach ($reports as $rep): 
     444            $points += $rep['Report']['points']; 
     445     endforeach; 
     446      
    459447     // Webquest 
    460448     $conditions     = array('ResultWebquest.vclassroom_id'=>$vclassroom_id, 'ResultWebquest.user_id'=>$user_id); 
     
    465453     endforeach; 
    466454          
    467      //die(debug($record)); 
     455     //die(debug($points)); 
    468456     return $points; 
    469457  } 
     
    483471   $data        = $this->UsersVclassroom->findAll($conditions, $fields); 
    484472 
    485    if ( $data == null ) 
    486    { 
     473   if ( $data == null ): 
    487474     return null;  
    488    } 
    489    else 
    490    { 
     475   else: 
    491476       $vclassrooms = array(); 
    492  
    493        foreach ($data as $v) 
    494        { 
    495      foreach($v['UsersVclassroom'] as $m) 
    496      { 
    497          array_push($vclassrooms, $m); 
    498      } 
    499        } 
    500    } 
     477       foreach ($data as $v): 
     478      foreach($v['UsersVclassroom'] as $m): 
     479          array_push($vclassrooms, $m); 
     480      endforeach; 
     481       endforeach; 
     482   endif; 
    501483   return $vclassrooms; 
    502484 } 
    503485 
    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   } 
    617486/* 
    618487 *  check if student belongs to class group