/**
  * Returns all award receipts for the given user
  * @param User $user
  * @return InternalAwardReceipts
  */
 private static function getByUser(User $user)
 {
     global $db;
     $query = "SELECT a.`id` as `award_receipt_id`, c.`id` as award_id, a.`user_id` , c.title, c.award_terms, c.disabled, a.year \n\t\t\t\tFROM `" . DATABASE_NAME . "`.`student_awards_internal` a \n\t\t\t\tleft join `" . DATABASE_NAME . "`.`student_awards_internal_types` c on c.id = a.award_id \n\t\t\t\tWHERE a.`user_id` = " . $db->qstr($user->getID()) . " \n\t\t\t\torder by a.year desc";
     $results = $db->GetAll($query);
     $receipts = array();
     if ($results) {
         foreach ($results as $result) {
             $award = InternalAward::fromArray($result);
             //for caching purposes
             $receipt = InternalAwardReceipt::fromArray($result);
             $receipts[] = $receipt;
         }
     }
     return new self($receipts);
 }
 /**
  * Compares award receipts by 'year' received, or by award 'title'
  * @param InternalAwardReceipt $ar
  * @param unknown_type $compare_by
  * @return number
  */
 public function compare($ar, $compare_by = "year")
 {
     switch ($compare_by) {
         case 'year':
             return $this->year == $ar->year ? 0 : ($this->year > $ar->year ? 1 : -1);
             break;
         case 'title':
             $award = $this->getAward();
             $other_award = $ar->getAward();
             return $award->compare($other_award);
             break;
     }
 }
function get_mspr_entity($type, $entity_id)
{
    switch ($type) {
        case 'studentships':
            $entity = Studentship::get($entity_id);
            break;
        case 'clineval':
            $entity = ClinicalPerformanceEvaluation::get($entity_id);
            break;
        case 'internal_awards':
            $entity = InternalAwardReceipt::get($entity_id);
            break;
        case 'external_awards':
            $entity = ExternalAwardReceipt::get($entity_id);
            break;
        case 'contributions':
            $entity = Contribution::get($entity_id);
            break;
        case 'student_run_electives':
            $entity = StudentRunElective::get($entity_id);
            break;
        case 'observerships':
            $entity = Observership::get($entity_id);
            break;
        case 'int_acts':
            $entity = InternationalActivity::get($entity_id);
            break;
        case 'critical_enquiry':
            $entity = CriticalEnquiry::get($entity_id);
            break;
        case 'community_based_project':
            $entity = CommunityBasedProject::get($entity_id);
            break;
        case 'research_citations':
            $entity = ResearchCitation::get($entity_id);
            break;
    }
    return $entity;
}
/**
 * Processes the various sections of the MSPR module
 */
function process_manage_award_details()
{
    if (isset($_POST['action'])) {
        $action = $_POST['action'];
        switch ($action) {
            case "add_award_recipient":
                $award_id = isset($_POST['award_id']) ? $_POST['award_id'] : 0;
                $user_id = isset($_POST['internal_award_user_id']) ? $_POST['internal_award_user_id'] : 0;
                if ($user_id && $award_id) {
                    $year = $_POST['internal_award_year'];
                    $info = array("award_id" => $award_id, "user_id" => $user_id, "year" => $year);
                    InternalAwardReceipt::create($info);
                }
                break;
            case "remove_award_recipient":
                $id = isset($_POST['internal_award_id']) ? $_POST['internal_award_id'] : 0;
                if ($id) {
                    $recipient = InternalAwardReceipt::get($id);
                    if ($recipient) {
                        $recipient->delete();
                    }
                }
                break;
            case "edit_award_details":
                $award_id = isset($_POST['award_id']) ? $_POST['award_id'] : 0;
                $disabled = (bool) $_POST['award_disabled'];
                $title = clean_input($_POST['award_title'], array("notags", "specialchars"));
                $terms = clean_input($_POST['award_terms'], array("notags", "specialchars", "nl2br"));
                if (!$title || !$terms) {
                    add_error("Insufficient information please check the fields and try again");
                } else {
                    if ($award_id) {
                        $award = InternalAward::get($award_id);
                        if ($award) {
                            edit_award_details($award, $title, $terms, $disabled);
                        }
                    } else {
                        add_error("Award not found");
                    }
                }
                break;
            case "new_award":
                $title = clean_input($_POST['award_title'], array("notags", "specialchars"));
                $terms = clean_input($_POST['award_terms'], array("notags", "specialchars", "nl2br"));
                if (!$title || !$terms) {
                    add_error("Insufficient information please check the fields and try again");
                } else {
                    InternalAward::create($title, $terms);
                }
                break;
            case "remove_award":
                $award_id = isset($_POST['award_id']) ? $_POST['award_id'] : 0;
                if ($award_id) {
                    $award = InternalAward::get($award_id);
                    $award->delete();
                }
                break;
        }
    }
}
 public function process()
 {
     global $ENTRADA_USER;
     $user = $this->_user;
     $translator = $this->_translator;
     $type = $this->type;
     static $valid = array("studentships" => array("add", "remove", "edit"), "clineval" => array("add", "remove", "edit"), "internal_awards" => array("add", "remove", "edit"), "student_run_electives" => array("add", "remove", "edit"), "observerships" => array("add", "remove", "edit"), "int_acts" => array("add", "remove", "edit"), "external_awards" => array("approve", "unapprove", "reject", "add", "edit"), "contributions" => array("approve", "unapprove", "reject", "add", "edit"), "critical_enquiry" => array("approve", "unapprove", "reject", "add", "edit"), "community_based_project" => array("approve", "unapprove", "reject", "add", "edit"), "research_citations" => array("approve", "unapprove", "reject", "add", "edit", "resequence"));
     $section = filter_input(INPUT_GET, 'mspr-section', FILTER_CALLBACK, array('options' => 'strtolower'));
     if ($section) {
         $params = array('entity_id' => FILTER_VALIDATE_INT, 'action' => array('filter' => FILTER_CALLBACK, 'options' => 'strtolower'), 'comment' => FILTER_SANITIZE_STRING, 'user_id' => FILTER_VALIDATE_INT);
         $inputs = filter_input_array(INPUT_POST, $params);
         extract($inputs);
         if (!$action) {
             add_error($translator->translate("mspr_no_action"));
         }
         if (!array_key_exists($section, $valid)) {
             add_error($translator->translate("mspr_invalid_section"));
         } else {
             if (!in_array($action, $valid[$section])) {
                 add_error($translator->translate("mspr_invalid_action"));
             }
         }
         if ($action == "reject" && MSPR_REJECTION_REASON_REQUIRED) {
             if (!$comment) {
                 add_error($translator->translate("mspr_no_reject_reason"));
             }
         }
         if (!has_error() && in_array($action, array("add", "edit", "resequence"))) {
             $inputs = get_mspr_inputs($section);
             process_mspr_inputs($section, $inputs, $translator);
             //modifies inputs/adds errors
         }
         if (!has_error()) {
             $inputs['user_id'] = $user_id;
             if ($action == "add") {
                 if (AUTO_APPROVE_ADMIN_MSPR_SUBMISSIONS) {
                     $inputs['status'] = 1;
                 }
                 switch ($section) {
                     case "clineval":
                         ClinicalPerformanceEvaluation::create($inputs);
                         break;
                     case "observerships":
                         Observership::create($inputs);
                         break;
                     case 'studentships':
                         Studentship::create($inputs);
                         break;
                     case 'internal_awards':
                         InternalAwardReceipt::create($inputs);
                         break;
                     case 'external_awards':
                         ExternalAwardReceipt::create($inputs);
                         break;
                     case 'contributions':
                         Contribution::create($inputs);
                         break;
                     case 'student_run_electives':
                         StudentRunElective::create($inputs);
                         break;
                     case 'int_acts':
                         InternationalActivity::create($inputs);
                         break;
                     case 'critical_enquiry':
                         if (CriticalEnquiry::get($user_id)) {
                             add_error($translator->translate("mspr_too_many_critical_enquiry"));
                         } else {
                             CriticalEnquiry::create($inputs);
                         }
                         break;
                     case 'community_based_project':
                         if (CommunityBasedProject::get($user_id)) {
                             add_error($translator->translate("mspr_too_many_community_based_project"));
                         } else {
                             CommunityBasedProject::create($inputs);
                         }
                         break;
                     case 'research_citations':
                         ResearchCitation::create($inputs);
                         break;
                 }
             } elseif ($action == "resequence") {
                 switch ($section) {
                     case 'research_citations':
                         ResearchCitations::setSequence($user_id, $inputs['research_citations']);
                         break;
                 }
             } else {
                 //everything else requires an entity
                 if ($entity_id) {
                     $entity = get_mspr_entity($section, $entity_id);
                     if ($entity) {
                         switch ($action) {
                             case "approve":
                                 $entity->approve();
                                 break;
                             case "unapprove":
                                 $entity->unapprove();
                                 break;
                             case "remove":
                                 $entity->delete();
                                 break;
                             case "edit":
                                 if ($entity instanceof Approvable) {
                                     if (AUTO_APPROVE_ADMIN_MSPR_EDITS) {
                                         $inputs['comment'] = "";
                                         $inputs['status'] = 1;
                                     } else {
                                         $inputs['comment'] = $entity->getComment();
                                         $inputs['status'] = $entity->getStatus();
                                     }
                                 }
                                 $entity->update($inputs);
                                 //inputs processed above
                                 break;
                             case "reject":
                                 if (MSPR_REJECTION_SEND_EMAIL) {
                                     $sub_info = get_submission_information($entity);
                                     $reason_type = !$comment ? "noreason" : "reason";
                                     $active_user = User::get($ENTRADA_USER->getID());
                                     if ($active_user && $type) {
                                         submission_rejection_notification($reason_type, array("firstname" => $user->getFirstname(), "lastname" => $user->getLastname(), "email" => $user->getEmail()), array("to_fullname" => $user->getFirstname() . " " . $user->getLastname(), "from_firstname" => $active_user->getFirstname(), "from_lastname" => $active_user->getLastname(), "reason" => clean_input($comment, array("notags", "specialchars")), "submission_details" => $sub_info, "application_name" => APPLICATION_NAME . " MSPR System"));
                                     } else {
                                         add_error($translator->translate("mspr_email_failed"));
                                     }
                                 }
                                 $entity->reject($comment);
                                 break;
                         }
                     } else {
                         add_error($translator->translate("mspr_invalid_entity"));
                     }
                 } else {
                     add_error($translator->translate("mspr_no_entity"));
                 }
             }
         }
         switch ($section) {
             case 'studentships':
                 $studentships = Studentships::get($user);
                 display_status_messages();
                 echo display_studentships($studentships, $type);
                 break;
             case 'clineval':
                 $clinical_evaluation_comments = ClinicalPerformanceEvaluations::get($user);
                 display_status_messages();
                 echo display_clineval($clinical_evaluation_comments, $type);
                 break;
             case 'internal_awards':
                 $internal_awards = InternalAwardReceipts::get($user);
                 display_status_messages();
                 echo display_internal_awards($internal_awards, $type);
                 break;
             case 'external_awards':
                 $external_awards = ExternalAwardReceipts::get($user);
                 display_status_messages();
                 echo display_external_awards($external_awards, $type);
                 break;
             case 'contributions':
                 $contributions = Contributions::get($user);
                 display_status_messages();
                 echo display_contributions($contributions, $type);
                 break;
             case 'student_run_electives':
                 $student_run_electives = StudentRunElectives::get($user);
                 display_status_messages();
                 echo display_student_run_electives($student_run_electives, $type);
                 break;
             case 'observerships':
                 $observerships = Observerships::get($user);
                 display_status_messages();
                 echo display_observerships($observerships, $type);
                 break;
             case 'int_acts':
                 $int_acts = InternationalActivities::get($user);
                 display_status_messages();
                 echo display_international_activities($int_acts, $type);
                 break;
             case 'critical_enquiry':
                 $critical_enquiry = CriticalEnquiry::get($user);
                 display_status_messages();
                 echo display_critical_enquiry($critical_enquiry, $type);
                 break;
             case 'community_based_project':
                 $community_based_project = CommunityBasedProject::get($user);
                 display_status_messages();
                 echo display_community_based_project($community_based_project, $type);
                 break;
             case 'research_citations':
                 $research_citations = ResearchCitations::get($user);
                 display_status_messages();
                 echo display_research_citations($research_citations, $type);
                 break;
         }
     }
 }