root/trunk/app/models/test.php

Revision 859, 5.3 kB (checked in by aarkerio, 4 weeks ago)

Better helps

Line 
1<?php
2/**
3*  Karamelo eLearning Platform
4*  Chipotle Software 2002-2008
5*  GPLv3 manuel<arroba>mononeurona<punto>org
6*  @version 0.3
7**/ 
8
9class Test extends AppModel {
10   
11   public $belongsTo = array('User' =>
12             array('className'  => 'User',
13                   'conditions' => '',
14                   'order'      => '',
15                               'foreignKey' => 'user_id',
16                               'fields'     => 'id, username'
17                   )
18             );
19   
20   public $hasMany = array(
21                     'Question' =>
22                   array('className'     => 'Question',
23                 'conditions'    => null,
24                 'order'         => 'Question.id ASC',
25                 'limit'         => null,
26                 'foreignKey'    => 'test_id',
27                 'dependent'     => true,
28                 'exclusive'     => false,
29                             'finderQuery'   => null
30
31                 ),
32                   'Result' =>
33             array('className'     => 'Result',
34               'conditions'    => null,
35               'order'         => null,
36               'limit'         => null,
37               'foreignKey'    => 'test_id',
38               'dependent'     => true,
39               'exclusive'     => false,
40                           'finderQuery'   => null
41
42               )
43               );
44
45    public $hasAndBelongsToMany = array('Vclassroom' =>
46                       array('className'             => 'Vclassroom',
47                         'joinTable'             => 'tests_vclassrooms',
48                         'foreignKey'            => 'test_id',
49                         'associationForeignKey' => 'vclassroom_id',
50                         'conditions'            => null,
51                         'order'                 => null,
52                         'limit'                 => '',
53                         'unique'                => true,
54                         'finderQuery'           => '',
55                         'deleteQuery'           => ''
56                         )
57                            );   
58public $validate = array(
59              'title' => array(
60                          'rule'    => array('minLength', 4),
61                                      'message' => 'Minimum 4 characters long'
62                       ),
63              'description' => array(
64                             'rule'    => array('minLength', 4),
65                                         'message' => 'Minimum 4 characters long'
66                     ),
67                      'user_id' => array(
68                                 'rule'       => 'numeric',
69                                         'allowEmpty' => false,
70                                         'on'         => 'create', // but not on update
71                                         'required'   => true 
72                                )       
73                );
74/*
75 *  getPoints  returns points from already answered test by student
76 * 
77 */
78 public function getPoints($test_id, $user_id, $vclassroom_id)
79 {
80  $points = (int) 0;
81  $conditions = array('Result.test_id'=>$test_id, 'Result.user_id'=>$user_id, 'Result.vclassroom_id'=>$vclassroom_id);
82  $this->Result->unbindAll();  // removeds unnecesary stuff
83  $answers    = $this->Result->findAll($conditions);  // get the answers gived by student
84
85  if ( count($answers) < 1):    // test not answer yet
86    return null;
87  endif;
88
89  foreach($answers as $a):
90    $question = $this->Question->find(array('Question.id'=>$a['Result']['question_id']), array('worth')); //how much points question have?
91    foreach($question['Answer'] as $qa):
92        if ($qa['id'] == $a['Result']['answer_id'] && $qa['question_id'] == $a['Result']['question_id'] && $qa['correct'] == 1):
93            $points += (int) $question['Question']['worth']; 
94    endif;
95     endforeach;
96  endforeach;
97 
98  return $points;
99 }
100 public function linkClassroom($test_id, $user_id)
101 {
102    $conditions = array('Test.status'=>1, 'Test.user_id'=>$user_id, 'Test.id'=>$test_id);
103   
104    $vclassrooms = array();
105   
106    $result = $this->find($conditions);
107   
108    foreach ($result['Vclassroom'] as $val):
109       $vclassrooms[$val['name']] = $val['id'];
110    endforeach;
111   
112    return $vclassrooms;
113 }
114
115/*
116 *  check if studen already found treasure
117 *  int treasure_id
118 *  int user_id     
119 *  int vclassroom_id
120 *  return boolean
121 */
122 public function chk($test_id, $user_id, $vclassroom_id)
123 {
124    $conditions =  array("Result.user_id"=>$user_id, "Result.test_id"=>$test_id, "Result.vclassroom_id"=>$vclassroom_id);
125    $data       =  $this->Result->field('Result.id', $conditions);
126   
127    if ($data == false ):
128          return false;   
129    else:
130          return true;
131    endif;
132 }
133
134 public function getTest($user_id, $test_id, $vclassroom_id)
135 {
136  $conditions = array('Result.user_id'=>$user_id, 'Result.test_id'=>$test_id, 'Result.vclassroom_id'=>$vclassroom_id);
137  $this->Result->bindModel(array('belongsTo'=>array('Question', 'Answer')));
138  $fields     = array('Result.id', 'Result.created','Result.question_id','Test.title', 'Question.question', 'Question.worth', 'Answer.answer', 'Answer.correct');
139  $data       = $this->Result->findAll($conditions, $fields);
140   
141  return $data;
142 }
143
144 public function chkAnswers($answers, $test_id)
145 {
146    $this->Test->unbindModel(array('belongsTo'=>array('User'))); //just few data
147   
148    $result = 0;
149   
150    foreach($questions as $q_id => $a_id):  // question id and answer id
151         $correct_answer = $this->Test->Question->Answer->field('Answer.correct', array('Answer.id'=>$a_id));   // answer was correct?
152         
153           if ($correct_answer == 1):
154              $worth   = $this->Test->Question->field('Question.worth', array('Question.id'=>$q_id));         // how many points
155              $result += $worth;
156           endif;
157     endforeach;
158    //exit('Result was:' . $result)e
159    return $result; 
160  }
161}
162?>
Note: See TracBrowser for help on using the browser.