Changeset 690 for trunk/app/controllers

Show
Ignore:
Timestamp:
08/04/08 12:40:39 (4 months ago)
Author:
aarkerio
Message:

Update

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/app/controllers/tests_controller.php

    r676 r690  
    4747    $user_id = (int) $this->Test->field('Test.user_id', array("Test.id"=>$test_id)); 
    4848     
     49    $this->__checkPermissions($vclassroom_id, $test_id); 
     50 
    4951    $this->pageTitle = __('Test', true); 
    5052     
     
    5254     
    5355    $this->Edublog->blog($user_id); // set edublog components 
    54     
     56     
     57    $conditions = array("Test.id"=>$test_id, "Test.status"=>1); 
     58 
     59    $fields = null; // array('Test.id', 'Test.title', 'Question.id'); 
     60 
     61    $order  = null; 
     62     
     63    $this->set('data', $this->Test->find($conditions, $fields, $order, 2)); 
     64 
     65    $this->set('vclassroom_id', $vclassroom_id); 
     66 } 
     67 
     68 public function result() 
     69 {  
     70 try { 
     71   //die(debug($this->data['Test'])); 
     72    
     73    $this->pageTitle = __('Test Results', true); 
     74     
     75    $this->layout    = $this->Edublog->layout($this->data['Test']['blogger_id']); 
     76     
     77    $this->__checkPermissions($this->data['Test']['vclassroom_id'], $this->data['Test']['test_id']); 
     78     
     79    $this->Edublog->blog($this->data['Test']['blogger_id']); // set edublog components 
     80    
     81    $this->Sanitize = new sanitize; 
     82 
     83    $this->Sanitize->clean($this->data['Test']);   
     84     
     85    $this->Test->unbindModel(array('belongsTo'=>array('User', 'Result'), 'hasAndBelongsToMany' => array('Vclassroom'))); 
     86     
     87    $fields = array('Test.title', 'Question.id'); 
     88    
     89    $tests = $this->Test->Question->findAll(array('Question.test_id'=>$this->data['Test']['test_id']), $fields); 
     90 
     91    //die(debug($this->data['Test'])); 
     92  
     93    $points     = (int) 0; 
     94 
     95    $this->data['Result']['user_id']        = (int) $this->Auth->user('id'); 
     96    $this->data['Result']['test_id']        = (int) $this->data['Test']['test_id'];  
     97    $this->data['Result']['vclassroom_id']  = (int) $this->data['Test']['vclassroom_id']; 
     98     
     99    //die( debug($this->data['Test'])); 
     100    foreach($this->data['Test'] as $k => $t): 
     101      if ( is_numeric($k) ): 
     102         $res = $this->Test->Question->Answer->find(array('Answer.id'=>$t)); 
     103         if ( $res['Answer']['correct'] == 1):    // is the answer correct? 
     104         $points += (int) $res['Question']['worth'];   //sum good points         
     105         $this->data['Result']['correct'] = 1; 
     106         else: 
     107         $this->data['Result']['correct'] = 0; 
     108     endif; 
     109         $this->data['Result']['question_id']  = $k; 
     110         $this->data['Result']['answer_id']    = $t; 
     111         $this->Test->Result->save($this->data); 
     112       endif; 
     113    endforeach; 
     114    // die('Points:  '. $points); 
     115   
     116    $maxpoints = (int) $this->data['Test']['maxpoints'];     
     117    $percentage = number_format( (($points / $maxpoints)*100), 2, '.', ''); 
     118    $this->data['Test']['results']     = $points;          
     119    $this->data['Test']['percentage']  = $percentage;      
     120    $this->set('data', $this->data['Test']); 
     121     
     122    $conditions = array('Question.test_id'=>$this->data['Result']['test_id']); 
     123    $fields     = array('Question.question', 'Question.hint', 'Question.explanation'); 
     124    $this->Test->Question->unbindAll();  
     125    $this->set('answers', $this->Test->Question->findAll($conditions, $fields)); 
     126  } 
     127  catch (Exception $e)  
     128  { 
     129      echo "Caught my exception\n" . $e; 
     130  }    
     131 } 
     132 /* 
     133  *  chk -- preview tests before send, this method is for 0.3 version aka TrikiTrakes     
     134  *  params 
     135  */ 
     136 private function chk()  
     137 { 
     138    try { 
     139      //exit(var_dump($this->data)); 
     140      $this->pageTitle = 'Exams'; 
     141       
     142      $this->layout    = $this->Edublog->layout($user_id); 
     143          
     144      $this->Edublog->blog($user_id); // set edublog components 
     145          
     146      $conditions = array("Test.status"=>1, "Test.id"=>$this->data['Test']['id']); 
     147   
     148      $this->set('data', $this->Test->find($conditions, null, null, 2)); 
     149      $this->set('question', $this->data['Question']); 
     150    }  
     151    catch (Exception $e)  
     152    { 
     153      echo "Caught my exception\n" . $e; 
     154    } 
     155 } 
     156 
     157  /* 
     158 *  checkPermissions -- check if student belongs to this class and if already had answered this test 
     159 *    
     160 */ 
     161 private function __checkPermissions($vclassroom_id, $test_id) 
     162 { 
    55163    // student belongst to this class? 
    56164    if ( $this->Auth->user() ): 
     
    66174             $this->set('already', true); 
    67175    endif; 
    68               
    69     $conditions = array("Test.id"=>$test_id, "Test.status"=>1); 
    70  
    71     $fields = null; // array('Test.id', 'Test.title', 'Question.id'); 
    72  
    73     $order  = null; 
    74      
    75     $this->set('data', $this->Test->find($conditions, $fields, $order, 2)); 
    76  
    77     $this->set('vclassroom_id', $vclassroom_id); 
    78  } 
    79  
    80  public function result() 
    81  {  
    82  try { 
    83    //die(debug($this->data['Test'])); 
    84    
    85     $this->pageTitle = 'Test Results'; 
    86      
    87     $this->layout    = $this->Edublog->layout($this->data['Test']['blogger_id']); 
    88      
    89     $this->Edublog->blog($this->data['Test']['blogger_id']); // set edublog components 
    90     
    91     $this->Sanitize = new sanitize; 
    92  
    93     $this->Sanitize->clean($this->data['Test']);   
    94      
    95     $this->Test->unbindModel(array('belongsTo'=>array('User', 'Result'), 'hasAndBelongsToMany' => array('Vclassroom'))); 
    96      
    97     $fields = array('Test.title', 'Question.id'); 
    98     
    99     $tests = $this->Test->Question->findAll(array('Question.test_id'=>$this->data['Test']['test_id']), $fields); 
    100  
    101     //die(debug($this->data['Test'])); 
    102   
    103     $points     = 0; 
    104  
    105     $this->data['Result']['user_id']        = $this->Auth->user('id'); 
    106     $this->data['Result']['test_id']        = $this->data['Test']['test_id'];  
    107     $this->data['Result']['vclassroom_id']  = $this->data['Test']['vclassroom_id']; 
    108  
    109     //die( debug($this->data['Test'])); 
    110     foreach($this->data['Test'] as $k => $t): 
    111       if ( is_numeric($k) ): 
    112          $res = $this->Test->Question->Answer->find(array('Answer.id'=>$t)); 
    113          if ( $res['Answer']['correct'] == 1):    // is the answer correct? 
    114          $points += (int) $res['Question']['worth'];   //sum good points         
    115          $this->data['Result']['correct'] = 1; 
    116          else: 
    117          $this->data['Result']['correct'] = 0; 
    118      endif; 
    119          $this->data['Result']['question_id']  = $k; 
    120          $this->data['Result']['answer_id']    = $t; 
    121          $this->Test->Result->create(); 
    122          $this->Test->Result->save($this->data['Result']); 
    123        endif; 
    124     endforeach; 
    125     // die('Points:  '. $points); 
    126    
    127     $maxpoints = (int) $this->data['Test']['maxpoints'];     
    128     $percentage = number_format( (($points / $maxpoints)*100), 2, '.', ''); 
    129     $this->data['Test']['results']     = $points;          
    130     $this->data['Test']['percentage']  = $percentage;      
    131     $this->set('data', $this->data['Test']);  
    132   } 
    133   catch (Exception $e)  
    134   { 
    135       echo "Caught my exception\n" . $e; 
    136   }    
    137  } 
    138  
    139  private function chk($user_id) // preview tests before send, this method is for 0.3 version     
    140  { 
    141     try { 
    142       //exit(var_dump($this->data)); 
    143       $this->pageTitle = 'Exams'; 
    144        
    145       $this->layout    = $this->Edublog->layout($user_id); 
    146           
    147       $this->Edublog->blog($user_id); // set edublog components 
    148           
    149       $conditions = array("Test.status"=>1, "Test.id"=>$this->data['Test']['id']); 
    150    
    151       $this->set('data', $this->Test->find($conditions, null, null, 2)); 
    152       $this->set('question', $this->data['Question']); 
    153     }  
    154     catch (Exception $e)  
    155     { 
    156       echo "Caught my exception\n" . $e; 
    157     } 
    158  } 
    159  
     176 
     177    return true; 
     178 } 
    160179   
    161180 /**   === ADMIN METHODS ====  **/