changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / mpi / mpi_survey.php
1 <?php
2
3 /*
4    This mpi allows for polls/surveys, whose results are stored into
5    named datapages.
6
7       !! What would you say?
8       <?plugin Survey data=PollOneData
9          answer1="I vote for this!"
10          second="another option"
11          3="no meaning on that issue."
12       ?>
13
14    Except for the data= setting, the named parameters are free-form and
15    only associate the text to an internal (named hash) entry.
16
17    If you later want to make a poll read-only, you could use:
18    <?plugin-show Survey data="data/var/poll1.dat" ... ?>  (this is just
19    a pagename, that __looks__ like a filename).
20 */
21
22
23 define("EWIKI_UP_SURVEY", "_voting4");
24
25
26 $ewiki_plugins["mpi"]["survey"] = "ewiki_mpi_survey";
27 $ewiki_plugins["view_stat"][] = "ewiki_show_survey";
28
29 function ewiki_mpi_survey($action, &$args, &$iii, &$s)
30 {
31    global $ewiki_id, $ewiki_plugins;
32    $o = "";
33
34    #-- load data page
35    if (! ($df = $args["data"])) {
36       return;
37    }
38    unset($args["data"]);
39    unset($args["_"]);
40    unset($args[""]);
41    $data = ewiki_db::GET($df);
42    if (!$data["version"]) {
43       $data = ewiki_new_data($df, EWIKI_DB_F_BINARY);
44       $data["version"]--;
45    }
46    if ($data["flags"] != EWIKI_DB_F_BINARY) {
47       return;
48    }
49    $survey = unserialize($data["content"]);
50
51    #-- operation
52    $vote = @$_REQUEST[EWIKI_UP_SURVEY];
53    if ($vote == "$") {
54       $action = "show";
55    }
56
57    if ($action=="html")
58    {
59       #-- show entries
60       if (!$vote) {
61          $o = "\n"
62             . '<form action="'.$_SERVER["REQUEST_URI"].'" method="POST" enctype="multipart/form-data">'
63             . '<input type="hidden" name="id" value="'.htmlentities($ewiki_id).'">'
64             . "\n";
65          foreach ($args as $name=>$text) {
66             if (!$name || !$text || ($name=="data")) { continue; }
67             $o .= '<input type="radio" name="'.EWIKI_UP_SURVEY.'" value="'
68                 . htmlentities($name) . '"> ' . $text . "<br />\n";
69          }
70          $o .= '<input type="submit" value="vote">';
71          $o .= "\n</form>\n<br /><br />\n";
72          $o .= '<a href="'.ewiki_script("",$ewiki_id,array(EWIKI_UP_SURVEY=>"$")).'">show results</a><br />';
73       }
74
75       #-- store an entry
76       if ($vote) {
77          $survey[$vote]++;
78          $data["content"] = serialize($survey);
79          $data["version"]++;
80          $data["lastmodified"] = time();
81          $data["author"] = ewiki_author();
82          ewiki_db::WRITE($data);
83
84          #-- show it
85          $action = "show";
86       }
87    }
88
89    if ($action=="show")
90    {
91       $o .= $ewiki_plugins["view_stat"][0]($survey, $args);
92    }
93    return($o);
94 }
95
96
97 function ewiki_show_survey($count, $text) {
98
99    $o = "";
100    $char = "*";    // char for result bars (<img> alt text or <div> content)
101    $clen = 60;     // max len of above
102    $px = 6;        // pixel size of each $char, for CSS
103    $colors = array(
104       "#ff8888", "#88ee88", "#9999ff", "#ffbb33", "#eeee66",
105       "#dd99dd", "#555555", "#dddddd",
106 //      "poll1.png", "poll2.png", "poll3.png", "poll4.png", "poll5.png",
107    );
108
109    $all = 0;
110    $max = 0;
111    foreach ($text as $name=>$uu) {
112       $all += $count[$name];
113       $max = max($max, $count[$name]);
114    }
115
116    $i = 0;
117    if ($max && $all) {
118       $o .= "\n";
119       foreach ($text as $name=>$title) {
120
121          #-- calc
122          $n = 0 + $count[$name];                 // number of votes
123          $n_percent = ((int) (1000*$n/$all))/10;
124          $bar_chars = (int)($clen * ($n/$max));  // num of chars in bar
125          $bar_width = $n_c * $px;                // pixel width of bar
126          $bar_str = "[" . str_repeat($char, $bar_chars) . "]";
127
128          #-- visualization
129          $color = $colors[$i];
130          if ($color{0} == "#") {
131             $bar = "<div style=\"color:$color; background:$color; width:$bar_width; height: 5px; border: 1px solid #333333;\">" . $bar_str . "</div>\n";
132          }
133          else {
134             $bar = "<div><img src=\"$color\" height=\"6\" width=\"$bar_width\" alt=\"$bar_str\" /></div>";
135          }
136          $i += 1;
137          $i %= count($colors);
138
139          #-- print
140          $o .= "$title<br />\n";
141          $o .= "<b>$n</b> votes, $n_percent%<br />\n";
142          $o .= $bar;
143          $o .= "<br />\n\n";
144
145       }
146    }
147
148    return($o);
149 }
150
151
152 ?>