source-function-F_question_get_position

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: //============================================================+
  3: // File name   : tce_functions_questions.php
  4: // Begin       : 2008-11-26
  5: // Last Update : 2012-11-07
  6: //
  7: // Description : Functions to manipulate questions.
  8: //
  9: // Author: Nicola Asuni
 10: //
 11: // (c) Copyright:
 12: //               Nicola Asuni
 13: //               Tecnick.com LTD
 14: //               www.tecnick.com
 15: //               info@tecnick.com
 16: //
 17: // License:
 18: //    Copyright (C) 2004-2010  Nicola Asuni - Tecnick.com LTD
 19: //    See LICENSE.TXT file for more information.
 20: //============================================================+
 21: 
 22: /**
 23:  * @file
 24:  * Functions to manipulate questions.
 25:  * @package com.tecnick.tcexam.admin
 26:  * @author Nicola Asuni
 27:  * @since 2008-11-26
 28:  */
 29: 
 30: /**
 31:  * Enable/Disable selected question
 32:  * @author Nicola Asuni
 33:  * @since 2008-11-26
 34:  * @param $question_id (int) question ID
 35:  * @param $enabled (boolean) if true enables question, false otherwise
 36:  */
 37: function F_question_set_enabled($question_id, $enabled = true)
 38: {
 39:     global $l, $db;
 40:     require_once('../config/tce_config.php');
 41:     $question_id = intval($question_id);
 42:     $sql = 'UPDATE '.K_TABLE_QUESTIONS.' SET
 43:         question_enabled=\''.intval($enabled).'\'
 44:         WHERE question_id='.$question_id.'';
 45:     if (!$r = F_db_query($sql, $db)) {
 46:         F_display_db_error(false);
 47:     }
 48: }
 49: 
 50: /**
 51:  * Get question position
 52:  * @author Nicola Asuni
 53:  * @since 2008-11-26
 54:  * @param $question_id (int) question ID
 55:  * @return int question position
 56:  */
 57: function F_question_get_position($question_id)
 58: {
 59:     global $l, $db;
 60:     require_once('../config/tce_config.php');
 61:     $question_id = intval($question_id);
 62:     $question_position = 0;
 63:     $sql = 'SELECT question_position
 64:         FROM '.K_TABLE_QUESTIONS.'
 65:         WHERE question_id='.$question_id.'
 66:         LIMIT 1';
 67:     if ($r = F_db_query($sql, $db)) {
 68:         if ($m = F_db_fetch_array($r)) {
 69:             $question_position = $m['question_position'];
 70:         }
 71:     } else {
 72:         F_display_db_error();
 73:     }
 74:     return $question_position;
 75: }
 76: 
 77: /**
 78:  * Get question data
 79:  * @author Nicola Asuni
 80:  * @since 2008-11-26
 81:  * @param $question_id (int) question ID
 82:  * @return array containing selected question data, false in case of error
 83:  */
 84: function F_question_get_data($question_id)
 85: {
 86:     global $l, $db;
 87:     require_once('../config/tce_config.php');
 88:     $question_id = intval($question_id);
 89:     $question_position = 0;
 90:     $sql = 'SELECT *
 91:         FROM '.K_TABLE_QUESTIONS.'
 92:         WHERE question_id='.$question_id.'
 93:         LIMIT 1';
 94:     if ($r = F_db_query($sql, $db)) {
 95:         if ($m = F_db_fetch_array($r)) {
 96:             return $m;
 97:         }
 98:     } else {
 99:         F_display_db_error();
100:     }
101:     return false;
102: }
103: 
104: /**
105:  * Delete selected question (or disable it if used)
106:  * @author Nicola Asuni
107:  * @since 2008-11-26
108:  * @param $question_id (int) question ID
109:  * @param $subject_id (int) subject ID
110:  */
111: function F_question_delete($question_id, $subject_id)
112: {
113:     global $l, $db;
114:     require_once('../config/tce_config.php');
115:     $question_id = intval($question_id);
116:     $subject_id = intval($subject_id);
117:     // check if this record is used (test_log)
118:     if (!F_check_unique(K_TABLE_TESTS_LOGS, 'testlog_question_id='.$question_id.'')) {
119:         F_question_set_enabled($question_id, false);
120:     } else {
121:         $sql = 'START TRANSACTION';
122:         if (!$r = F_db_query($sql, $db)) {
123:             F_display_db_error();
124:         }
125:         // get question position (if defined)
126:         $question_position = F_question_get_position($question_id);
127:         // delete question
128:         $sql = 'DELETE FROM '.K_TABLE_QUESTIONS.' WHERE question_id='.$question_id.'';
129:         if (!$r = F_db_query($sql, $db)) {
130:             F_display_db_error(false);
131:             F_db_query('ROLLBACK', $db); // rollback transaction
132:         } else {
133:             // adjust questions ordering
134:             if ($question_position > 0) {
135:                 $sql = 'UPDATE '.K_TABLE_QUESTIONS.' SET
136:                     question_position=question_position-1
137:                     WHERE question_subject_id='.$subject_id.'
138:                         AND question_position>'.$question_position.'';
139:                 if (!$r = F_db_query($sql, $db)) {
140:                     F_display_db_error(false);
141:                     F_db_query('ROLLBACK', $db); // rollback transaction
142:                 }
143:             }
144:             $sql = 'COMMIT';
145:             if (!$r = F_db_query($sql, $db)) {
146:                 F_display_db_error();
147:             }
148:         }
149:     }
150: }
151: 
152: /**
153:  * Copy selected question to another topic
154:  * @author Nicola Asuni
155:  * @since 2008-11-26
156:  * @param $question_id (int) question ID
157:  * @param $new_subject_id (int) new subject ID
158:  */
159: function F_question_copy($question_id, $new_subject_id)
160: {
161:     global $l, $db;
162:     require_once('../config/tce_config.php');
163:     $question_id = intval($question_id);
164:     $new_subject_id = intval($new_subject_id);
165:     // check authorization
166:     $sql = 'SELECT subject_module_id FROM '.K_TABLE_SUBJECTS.' WHERE subject_id='.$new_subject_id.' LIMIT 1';
167:     if ($r = F_db_query($sql, $db)) {
168:         if ($m = F_db_fetch_array($r)) {
169:             $subject_module_id = $m['subject_module_id'];
170:             // check user's authorization for parent module
171:             if (!F_isAuthorizedUser(K_TABLE_MODULES, 'module_id', $subject_module_id, 'module_user_id')) {
172:                 return;
173:             }
174:         }
175:     } else {
176:         F_display_db_error();
177:         return;
178:     }
179:     $q = F_question_get_data($question_id);
180:     if ($q !== false) {
181:         if (K_DATABASE_TYPE == 'ORACLE') {
182:             $chksql = 'dbms_lob.instr(question_description,\''.F_escape_sql($db, $q['question_description']).'\',1,1)>0';
183:         } elseif ((K_DATABASE_TYPE == 'MYSQL') and defined('K_MYSQL_QA_BIN_UNIQUITY') and K_MYSQL_QA_BIN_UNIQUITY) {
184:             $chksql = 'question_description=\''.F_escape_sql($db, $q['question_description']).'\' COLLATE utf8_bin';
185:         } else {
186:             $chksql = 'question_description=\''.F_escape_sql($db, $q['question_description']).'\'';
187:         }
188:         if (F_check_unique(K_TABLE_QUESTIONS, $chksql.' AND question_subject_id='.$new_subject_id.'')) {
189:             $sql = 'START TRANSACTION';
190:             if (!$r = F_db_query($sql, $db)) {
191:                 F_display_db_error(false);
192:                 return;
193:             }
194:             // adjust questions ordering
195:             if ($q['question_position'] > 0) {
196:                 $sql = 'UPDATE '.K_TABLE_QUESTIONS.' SET
197:                     question_position=question_position+1
198:                     WHERE question_subject_id='.$new_subject_id.'
199:                         AND question_position>='.$q['question_position'].'';
200:                 if (!$r = F_db_query($sql, $db)) {
201:                     F_display_db_error(false);
202:                     F_db_query('ROLLBACK', $db); // rollback transaction
203:                 }
204:             }
205:             $sql = 'INSERT INTO '.K_TABLE_QUESTIONS.' (
206:                 question_subject_id,
207:                 question_description,
208:                 question_explanation,
209:                 question_type,
210:                 question_difficulty,
211:                 question_enabled,
212:                 question_position,
213:                 question_timer,
214:                 question_fullscreen,
215:                 question_inline_answers,
216:                 question_auto_next
217:                 ) VALUES (
218:                 '.$new_subject_id.',
219:                 \''.F_escape_sql($db, $q['question_description']).'\',
220:                 \''.F_escape_sql($db, $q['question_explanation']).'\',
221:                 \''.$q['question_type'].'\',
222:                 \''.$q['question_difficulty'].'\',
223:                 \''.$q['question_enabled'].'\',
224:                 '.F_zero_to_null($q['question_position']).',
225:                 \''.$q['question_timer'].'\',
226:                 \''.$q['question_fullscreen'].'\',
227:                 \''.$q['question_inline_answers'].'\',
228:                 \''.$q['question_auto_next'].'\'
229:                 )';
230:             if (!$r = F_db_query($sql, $db)) {
231:                 F_display_db_error(false);
232:             } else {
233:                 $new_question_id = F_db_insert_id($db, K_TABLE_QUESTIONS, 'question_id');
234:             }
235:             // copy associated answers
236:             $sql = 'SELECT *
237:                 FROM '.K_TABLE_ANSWERS.'
238:                 WHERE answer_question_id='.$question_id.'';
239:             if ($r = F_db_query($sql, $db)) {
240:                 while ($m = F_db_fetch_array($r)) {
241:                     $sqli = 'INSERT INTO '.K_TABLE_ANSWERS.' (
242:                         answer_question_id,
243:                         answer_description,
244:                         answer_explanation,
245:                         answer_isright,
246:                         answer_enabled,
247:                         answer_position,
248:                         answer_keyboard_key
249:                         ) VALUES (
250:                         '.$new_question_id.',
251:                         \''.F_escape_sql($db, $m['answer_description']).'\',
252:                         \''.F_escape_sql($db, $m['answer_explanation']).'\',
253:                         \''.$m['answer_isright'].'\',
254:                         \''.$m['answer_enabled'].'\',
255:                         '.F_zero_to_null($m['answer_position']).',
256:                         '.F_empty_to_null($m['answer_keyboard_key']).'
257:                         )';
258:                     if (!$ri = F_db_query($sqli, $db)) {
259:                         F_display_db_error(false);
260:                         F_db_query('ROLLBACK', $db); // rollback transaction
261:                     }
262:                 }
263:             } else {
264:                 F_display_db_error();
265:             }
266:             $sql = 'COMMIT';
267:             if (!$r = F_db_query($sql, $db)) {
268:                 F_display_db_error(false);
269:                 return;
270:             }
271:         }
272:     }
273: }
274: 
275: //============================================================+
276: // END OF FILE
277: //============================================================+
278: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy