예제 #1
0
 /**
  * @before _secure, _school
  */
 public function addToClass($user_id)
 {
     $usr = \User::first(array("id = ?" => $user_id), array("id"));
     if (!$usr) {
         self::redirect("/school");
     }
     $this->setSEO(array("title" => "Parent Info | Student | School"));
     $view = $this->getActionView();
     $grades = \Grade::all(array("organization_id = ?" => $this->organization->id), array("id", "title"));
     $enrollment = Enrollment::first(array("user_id = ?" => $user_id));
     if ($enrollment) {
         $view->set("success", "Student has already been added! Response will be updated");
     }
     if (RequestMethods::post("action") == "addToClass") {
         $classroom = Markup::checkValue(RequestMethods::post("classroom"));
         if (!$enrollment) {
             $enrollment = new Enrollment(array());
         }
         $enrollment->user_id = $usr->id;
         $enrollment->classroom_id = $classroom;
         $enrollment->organization_id = $this->organization->id;
         if ($enrollment->validate()) {
             $enrollment->save();
             $view->set("success", "Student successfully added to classroom");
         }
     }
     if ($enrollment) {
         $class = Classroom::first(array("id = ?" => $enrollment->classroom_id), array("id", "grade_id", "section"));
         foreach ($grades as $g) {
             if ($g->id == $class->grade_id) {
                 $grade = $g->id;
                 break;
             }
         }
         $view->set("class", $class);
     }
     if (!isset($grade)) {
         $grade = null;
     }
     $view->set("grade", $grade);
     $view->set("enrollment", $enrollment);
     $view->set("grades", $grades);
 }