Ejemplo n.º 1
0
 public static function set_vote($vote, $metric, $context_id, $user_id)
 {
     $metric_type = Evaluate_Metrics::get_metric_types()[$metric['type']];
     $old_vote = $metric_type->get_vote($metric, $context_id, $user_id);
     $vote = $metric_type->validate_vote($vote, $old_vote, $metric['options']);
     $metric_type->set_vote($vote, $metric, $context_id, $user_id);
     $score = $metric_type->get_score($metric['metric_id'], $context_id);
     $score = $metric_type->modify_score($score, $vote, $old_vote, $metric, $context_id);
     return array('count' => $score['count'], 'average' => $score['average'], 'data' => $score['data'], 'vote' => $vote);
 }
Ejemplo n.º 2
0
 private static function save_post_data()
 {
     if (!isset($_POST['action']) || $_POST['action'] !== 'save') {
         return;
     }
     global $wpdb;
     // TODO: Verify nonce.
     $metric_type = Evaluate_Metrics::get_metric_types()[$_POST['type']];
     $options = $metric_type->filter_options($_POST['options']);
     $data = array('name' => sanitize_text_field($_POST['name']), 'type' => sanitize_text_field($_POST['type']), 'options' => serialize($options));
     if (empty($_POST['metric_id'])) {
         $data['created'] = current_time('mysql', 1);
         $wpdb->insert(Evaluate::$metric_table, $data);
         $metric_id = $wpdb->insert_id;
     } else {
         $wpdb->update(Evaluate::$metric_table, $data, array('metric_id' => $_POST['metric_id']));
         $metric_id = $_POST['metric_id'];
     }
     wp_redirect(add_query_arg('metric_id', $metric_id));
     exit;
 }
Ejemplo n.º 3
0
    public function render_options($options, $name = 'options[%s]')
    {
        $options = shortcode_atts(array('rubric' => '', 'fields' => array()), $options);
        $rubrics = apply_filters('evaluate_get_rubrics', array());
        $metric_types = Evaluate_Metrics::get_metric_types();
        foreach ($metric_types as $slug => $title) {
            if ($slug === self::SLUG) {
                unset($metric_types[$slug]);
            }
        }
        ?>
		<dt>Rubric Definition</dt>
		<dd>
			<select class="nav" data-anchor="rubric-options" data-siblings="true" name="<?php 
        printf($name, 'rubric');
        ?>
">
				<option value="">- Choose a Rubric -</option>
				<option value="custom" <?php 
        selected($options['rubric'], 'custom');
        ?>
>Custom</option>
				<?php 
        foreach ($rubrics as $slug => $rubric) {
            ?>
					<option value="<?php 
            echo $slug;
            ?>
" <?php 
            selected($slug, $options['rubric']);
            ?>
>
						<?php 
            echo $rubric['name'];
            ?>
					</option>
					<?php 
        }
        ?>
			</select>
			<ul id="rubric-fields" class="rubric-options-custom rubric-options"<?php 
        echo $options['rubric'] == 'custom' ? '' : ' style="display: none;"';
        ?>
>
				<?php 
        $field_name = sprintf($name, 'fields') . '[#][%s]';
        // ^ The '#' character will be replaced using JavaScript. If you change it, make sure you update evaluate-admin.js!
        // This is to get around the fact that "options['fields'][]['title']" is not a valid name attribute.
        if (!empty($options['fields'])) {
            foreach ($options['fields'] as $index => $field) {
                self::render_field($metric_types, $field_name, $field);
            }
        }
        self::render_field($metric_types, $field_name);
        ?>
			</ul>
			<?php 
        foreach ($rubrics as $slug => $rubric) {
            ?>
				<div class="rubric-options-<?php 
            echo $slug;
            ?>
 rubric-options"<?php 
            echo $slug == $options['rubric'] ? '' : ' style="display: none;"';
            ?>
>
					<p><?php 
            echo $rubric['description'];
            ?>
</p>
					<strong>Fields:</strong>
					<ul>
						<?php 
            foreach ($rubric['fields'] as $key => $field) {
                echo $field['title'] . " - a " . $metric_types[$field['type']]->name . " metric, with " . $field['weight'] . " weight.";
            }
            ?>
					</ul>
				</div>
				<?php 
        }
        ?>
		</dd>
		<?php 
    }