Пример #1
0
 public function action_edit()
 {
     $submitted = false;
     $id = $this->request->param('id');
     if (!$id) {
         Request::current()->redirect('course');
     }
     $course = ORM::factory('course', $id);
     if ($this->request->method() === 'POST' && $this->request->post()) {
         if (Arr::get($this->request->post(), 'save') !== null) {
             $submitted = true;
             $validator = $course->validator($this->request->post());
             $validator->bind(':course', $course);
             $validator->bind(':start_date', $this->request->post('start_date'));
             if ($validator->check()) {
                 $course->name = $this->request->post('name');
                 $course->description = $this->request->post('description');
                 $course->access_code = $this->request->post('access_code');
                 $course->start_date = $this->request->post('start_date');
                 $course->end_date = $this->request->post('end_date');
                 $course->save();
                 Session::instance()->set('success', 'Cources edited successfully.');
                 Model_Course::assign_users($course, $this->request->post('selected'));
                 Request::current()->redirect('course');
                 exit;
             } else {
                 $this->_errors = $validator->errors('course');
             }
         }
     }
     $saved_data = array('name' => $course->name, 'description' => $course->description, 'access_code' => $course->access_code, 'start_date' => $course->start_date, 'end_date' => $course->end_date);
     $form = $this->form('course/edit/id/' . $id, $submitted, $saved_data);
     $data = Model_Course::get_students($course);
     $cacheimage = CacheImage::instance();
     $user_ids = $data->as_array(NULL, 'id');
     $count = $course->users->count_all();
     $users = View::factory('course/assign')->bind('data', $data)->bind('count', $count)->bind('cacheimage', $cacheimage)->bind('user_ids', $user_ids);
     $teachers = Model_Role::get_users('teacher');
     $assigned_teachers = Model_Course::get_teachers($course)->as_array(null, 'id');
     $teachers_table = View::factory('course/teachers')->bind('teachers', $teachers)->bind('cacheimage', $cacheimage)->bind('assigned_teachers', $assigned_teachers);
     $batches = ORM::factory('batch')->find_all();
     $view = View::factory('course/form')->bind('form', $form)->bind('users', $users)->bind('batches', $batches)->bind('teachers_table', $teachers_table)->bind('course_id', $id);
     Breadcrumbs::add(array('Courses', Url::site('course')));
     Breadcrumbs::add(array($course->name, Url::site('course/edit/id/' . $id)));
     $this->content = $view;
 }