/** * Unsubscribe an volunteer from an campaign * * @print Results: * · 1: Success * · 0: No data send * · -1: No exists volunteer or is duplicated * · -2: Volunteer not subscribed * · -3: Problem in unsuscription process (Database ERROR) */ public static function campaign_unsuscribe() { global $wp; global $wpdb; if (!empty($_POST) && isset($_POST['token'])) { $token = $_POST['token']; $id_volunteer = API_Volunteer::get_volunter_id($token); $id_campaign = $wp->query_vars['cid']; if ($id_volunteer) { $is_suscribed = count($wpdb->get_results("SELECT id_campaign FROM " . CINDA_TABLE_SUSCRIPTIONS_NAME . " WHERE id_volunteer = " . $id_volunteer . " AND id_campaign = " . $id_campaign . "", "ARRAY_A")) == 1 ? 1 : 0; if ($is_suscribed) { $result = $wpdb->delete(CINDA_TABLE_SUSCRIPTIONS_NAME, array('id_volunteer' => $id_volunteer, 'id_campaign' => $id_campaign), array('%d', '%d')); if ($result) { echo 1; } else { echo -3; } } else { echo -2; } } else { echo -1; } } else { echo 0; } }
public static function get_all_trackings() { $trackings = array(); $id_volunteer = 0; global $wpdb; if (isset($_POST['token'])) { $id_volunteer = Volunteer::get_volunter_id($_POST['token']); } if ($id_volunteer) { $id_trackings = $wpdb->get_col('SELECT id FROM ' . CINDA_TABLE_TRACKINGS_NAME . ' where id_volunteer = ' . $id_volunteer . ' ORDER BY create_date, id ASC'); if (0 < count($id_trackings)) { foreach ($id_trackings as $id) { $tracking = new self($id); $trackings[] = $tracking->serialize(); } } } die(json_encode($trackings)); }
/** * Print json array with all information for the watchface application */ public static function get_watchfaceData() { global $wp; global $wpdb; $token = isset($_POST['token']) ? $_POST['token'] : ""; // TMP if (isset($_GET['dev']) && $_GET['dev'] == true) { $token = "04685cfa05ff2a0fa97a36be3997819a81c62522"; } if ($id_volunteer = API_Volunteer::get_volunter_id($token)) { $campaigns = new CampaignsList(array('orderby' => 'suscriptions', 'number' => 10, 'token' => $token)); $volunteers = new VolunteerList(array('number' => 10)); $contributions = new ContributionList(array('volunteer' => $id_volunteer)); $response = array(); // CAMPAIGNS if (0 < count($campaigns = $campaigns->get_campaigns())) { // Calc Angle $total_suscriptors = 0; foreach ($campaigns as $campaign) { $total_suscriptors += $campaign->suscriptions; } $response['campaigns'] = array(); $total_angle = 0; foreach ($campaigns as $campaign) { $angle = round($campaign->suscriptions * 100 / $total_suscriptors * 360 / 100); $total_angle += $angle; $response['campaigns'][] = array('id' => $campaign->ID, 'color' => $campaign->color, 'name' => $campaign->title, 'suscriptions' => $campaign->suscriptions, 'angle' => $angle); } if ($total_angle < 360) { $dif = 360 - $total_angle; $response['campaigns'][count($response['campaigns']) - 1]['angle'] += $dif; } } // VOLUNTEERS if (0 < count($volunteers = $volunteers->get_volunteers())) { $total_contributions = 0; foreach ($volunteers as $volunteer) { $total_contributions += $volunteer->contributions; } $response['volunteers'] = array(); foreach ($volunteers as $volunteer) { $response['volunteers'][] = array('id' => $volunteer->get_ID(), 'name' => $volunteer->nickname, 'avatar' => $volunteer->avatar, 'contributions' => $volunteer->contributions, 'angle' => round($volunteer->contributions * 100 / $total_contributions * 360 / 100)); } } // VOLUNTEERS $total_contributions = count($contributions = $contributions->get_contributions()); if (0 < $total_contributions) { $response['contributions'] = array(); foreach ($contributions as $contribution) { $data = $contribution->serialize(false); unset($data['author_id']); unset($data['author_name']); $data['angle'] = round(1 * 100 / $total_contributions * 360 / 100); $response['contributions'][] = $data; } } // TIMELINE $response['timeline'] = array(); $from = date('Y-m-1 00:00:00', strtotime("-5 month", strtotime('m'))); $to = date('Y-m-d 00:00:00', strtotime("+1 day")); $sql = "SELECT CONCAT( MONTH( p.post_date ), '-', YEAR( p.post_date ) ) AS month, COUNT( p.ID ) AS contributions\n\t\t\t\t\tFROM " . $wpdb->prefix . "posts AS p \n\t\t\t\t\tINNER JOIN " . $wpdb->prefix . "postmeta AS m ON p.ID = m.post_id \n\t\t\t\t\tWHERE m.meta_key = '" . CINDA_PREFIX . "author_id' \n\t\t\t\t\tAND m.meta_value = " . $id_volunteer . " \n\t\t\t\t\tAND STR_TO_DATE( p.post_date, '%Y-%m-%d %H:%i:%s') >= STR_TO_DATE( '" . $from . "', '%Y-%m-%d %H:%i:%s') \n\t\t\t\t\tAND STR_TO_DATE( p.post_date, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '" . $to . "', '%Y-%m-%d %H:%i:%s')\n\t\t\t\t\tGROUP BY MONTH( p.post_date );"; $months = $wpdb->get_results($sql, ARRAY_A); for ($i = 5, $e = 0; $i >= 0; $i--, $e++) { $month_tmp = date('n-Y', strtotime("-{$i} month", strtotime($to))); foreach ($months as $month) { if ($month['month'] == $month_tmp) { $response['timeline'][$e] = array('month' => $month['month'], 'contributions' => intval($month['contributions'])); break; } } if (!isset($response['timeline'][$e])) { $response['timeline'][$e] = array('month' => $month_tmp, 'contributions' => 0); } } die(json_encode($response)); } else { die(json_encode(0)); } }
/** * Constructor * @param unknown $ID */ function __construct($ID) { //if(self::contribution_exists($ID)){ // Check if exists Token if (isset($_POST['token'])) { $id_volunteer = API_Volunteer::get_volunter_id($_POST['token']); } // Auxiliar variables $post = get_post($ID); // ATTENTION: All start with prefix 'cinda_' $meta_values = get_post_meta($ID); // Campaign ID $this->id_campaign = $post->post_parent; $campaign = new API_Campaign($this->id_campaign); $this->campaign_name = $campaign->title; $this->campaign_image = $campaign->image; // Data Model $this->model = CindaQuery::get_model($this->id_campaign); // Data Values // ATTENTION: These fields do not need the prefix 'cinda_' $this->ID = intval($ID); $this->data['create_date'] = $post->post_date; // SET AUTHOR ID if (isset($meta_values[CINDA_PREFIX . 'author_id'][0])) { $this->data['author_id'] = intval($meta_values[CINDA_PREFIX . 'author_id'][0]); } else { $this->data['author_id'] = 0; } // SET AUTHOR NAME if ($this->data['author_id']) { $volunteer = new API_Volunteer($this->data['author_id']); $this->data['author_name'] = $volunteer->nickname; $this->data['author_image'] = $volunteer->avatar; } else { $this->data['author_name'] = __('unknown', 'Cinda'); } // SET TRACKING IDecho $meta_values[CINDA_PREFIX.'tracking'][0]; if ((current_user_can('manage_options') || isset($id_volunteer) && $this->data['author_id'] == $id_volunteer) && isset($meta_values[CINDA_PREFIX . 'tracking'][0]) && $meta_values[CINDA_PREFIX . 'tracking'][0] != "false") { $this->data['tracking'] = $meta_values[CINDA_PREFIX . 'tracking'][0]; } if (!isset($this->data['tracking']) || !$this->data['tracking']) { $this->data['tracking'] = 0; } // If data model not is empty if (0 < count($this->model)) { // Go over model fields foreach ($this->model as $field) { // If exists meta_value add to $data array if (isset($meta_values[CINDA_PREFIX . $field->field_name])) { if ($field->field_type == 'image' || $field->field_type == 'file') { $this->data[$field->field_name] = wp_get_attachment_image_src($meta_values[CINDA_PREFIX . $field->field_name][0], 'cinda-image')[0]; $this->data[$field->field_name . "_thumbnail"] = wp_get_attachment_image_src($meta_values[CINDA_PREFIX . $field->field_name][0], 'cinda-thumbnail')[0]; } else { $this->data[$field->field_name] = $meta_values[CINDA_PREFIX . $field->field_name][0]; } } else { $this->data[$field->field_name] = ""; } } } //} }