function getTest() { $t = new Test(); foreach ($t->get_by_id(2)->topic->get() as $o) { echo $o->to_json(); } }
function addBatch() { $users = str_getcsv($_POST['users']); $saved = TRUE; $testid = $this->session->userdata('testid'); $test = new Test(); $test->get_by_id($testid); $this->load->helper("pinyin"); foreach ($users as $user) { $u = new User(); $u->uStudId = $user[0]; $u->uName = str_replace(' ', '', $user[1]); //$u->uPassword = $user[0];//密码等于用户的学号 $pinyin = get_pinyin($u->uName); $u->uPassword = str_replace('_', '', $pinyin); //用拼音作为密码 $u->uType = 'student'; if (!$u->save()) { $saved = FALSE; break; } $test->save($u); } return 'saved'; }
function lists($id = 0) { if (!$id) { $id = $this->session->userdata("testid"); } $test = new Test(); $test->get_by_id((int) $id); $t = $test->topic->get(); $this->session->set_userdata(array('testid' => $id)); //datamapper的json扩展真是强大 echo $t->all_to_json(); }
/** * This method will initialize this instance of test object with Test model, or id of test model. * @param DataMapper|integer $test_model Test model or id of test record. * @throws TestException can be thrown if $test_model is not Test object, test record not exists or test type or subtype is not supported. */ public function initialize($test_model) { if (is_object($test_model) && !$test_model instanceof Test) { throw new TestException($this->CI->lang->line('tests_general_error_cant_initialize_with_non_test_model'), 1000001); } elseif (is_integer($test_model)) { $test_model_id = $test_model; $test_model = new Test(); $test_model->get_by_id($test_model_id); } if ($test_model->exists()) { if ($test_model->type !== $this->test_type) { throw new TestException(sprintf($this->CI->lang->line('tests_general_error_test_type_is_not_supported'), get_class($this)), 1000002); } elseif (!array_key_exists($test_model->subtype, $this->test_subtypes)) { throw new TestException(sprintf($this->CI->lang->line('tests_general_error_test_subtype_is_not_supported'), get_class($this)), 1000003); } $current_test = array('subtype' => $test_model->subtype, 'config' => unserialize($test_model->configuration), 'id' => $test_model->id, 'enable_scoring' => (int) $test_model->enable_scoring > 0 ? TRUE : FALSE, 'task_id' => $test_model->task_id, 'timeout' => $test_model->timeout); $this->current_test = $current_test; } else { throw new TestException($this->CI->lang->line('tests_general_error_test_record_not_exists'), 1000004); } }
public function edit($id = 0) { $obj = new Test(); $obj->get_by_id((int) $id); if (!$_POST) { echo $obj->to_json(); } else { if (isset($_POST['model']) and $model = $_POST['model']) { $obj->from_json($model); if ($obj->save()) { $this->_user->save($obj); //保存关系 echo $obj->to_json(); } else { echo json_encode(array('error' => $obj->error->string)); } } else { if (isset($_POST['_method']) and $_POST['_method'] === 'DELETE') { $this->_user->delete($obj); $obj->delete(); } } } }
public function run_single_test($test_id, $source_file, $evaluation = FALSE, $student_id = NULL, $token = '') { $output = new stdClass(); $output->text = ''; $output->code = 0; try { $test = new Test(); $test->get_by_id(intval($test_id)); if ($test->exists()) { $test_object = $this->load->test($test->type); $test_object->initialize($test); $output->text = $test_object->run(decode_from_url($source_file), (bool) (int) $evaluation && strlen($token) > 0, $student_id, $token); } } catch (Exception $e) { $output->text = $e->getMessage(); $output->code = $e->getCode(); } $this->output->set_content_type('application/json'); $this->output->set_output(json_encode($output)); }