Exemplo n.º 1
0
 public function create_solution($task_set_id)
 {
     $this->load->library('form_validation');
     $this->form_validation->set_rules('solution[student_id]', 'lang:admin_solutions_list_form_field_student', 'required|exists_in_table[students.id.1.1]');
     $this->form_validation->set_rules('solution[points]', 'lang:admin_solutions_list_form_field_points', 'floatpoint');
     if ($this->form_validation->run()) {
         $this->_transaction_isolation();
         $this->db->trans_begin();
         $solution_data = $this->input->post('solution');
         $task_set = new Task_set();
         $task_set->where_related('course/participant/student', 'id', intval($solution_data['student_id']));
         $task_set->where_related('course/participant', 'allowed', 1);
         $task_set->group_start();
         $task_set->or_group_start();
         $task_set->group_start();
         $task_set->or_where('group_id', NULL);
         $task_set->or_where('`course_participants`.`group_id` = `task_sets`.`group_id`');
         $task_set->group_end();
         $task_set->where_subquery(0, '(SELECT COUNT(`tsp`.`id`) AS `count` FROM `task_set_permissions` tsp WHERE `tsp`.`task_set_id` = `task_sets`.`id` AND `tsp`.`enabled` = 1)');
         $task_set->group_end();
         $task_set->or_where_related('task_set_permission', '`group_id` = `course_participants`.`group_id`');
         $task_set->group_end();
         $task_set->get_by_id($task_set_id);
         $created_solution_id = NULL;
         if ($task_set->exists()) {
             $teacher = new Teacher();
             $teacher->get_by_id($this->usermanager->get_teacher_id());
             $solution = new Solution();
             $solution->from_array($solution_data, array('student_id', 'comment'));
             if (trim($solution_data['points']) != '' && is_float($solution_data['points'])) {
                 $solution->points = floatval($solution_data['points']);
             } else {
                 $solution->points = NULL;
             }
             $solution->revalidate = 0;
             $solution->save(array($teacher, $task_set));
             $solution->where('task_set_id', $task_set_id);
             $solution->where('student_id', intval($solution_data['student_id']));
             if ($solution->count() == 1) {
                 $created_solution_id = $solution->id;
                 $this->db->trans_commit();
                 $this->messages->add_message('lang:admin_solutions_list_new_solution_created', Messages::MESSAGE_TYPE_SUCCESS);
                 $this->_action_success();
                 $this->output->set_internal_value('student_id', $solution->student_id);
             } else {
                 $this->db->trans_rollback();
                 $this->messages->add_message('lang:admin_solutions_list_new_solution_error_solution_exists', Messages::MESSAGE_TYPE_ERROR);
             }
         } else {
             $this->db->trans_rollback();
             $this->messages->add_message('lang:admin_solutions_list_new_solution_error_student_not_in_course_or_group', Messages::MESSAGE_TYPE_ERROR);
         }
         redirect(create_internal_url('admin_solutions/new_solution_form/' . intval($task_set_id) . ($created_solution_id !== NULL ? '/' . intval($created_solution_id) : '')));
     } else {
         $this->new_solution_form($task_set_id);
     }
 }