123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
-
-
- // This script fetches how many people, for every category, have answered a given score
- // and spits out a json with an array of category ID, category name, score and count
-
- // require_once 'processes/config.php';
- require_once 'processes/dbh.inc.php';
- // require_once 'processes/checkLogin.php';
-
-
-
- $experienceID = mysqli_real_escape_string($connection, trim($_POST['experienceID'])); // 26
- $momentID = mysqli_real_escape_string($connection, trim($_POST['momentID'])); // 196
- $studentID = mysqli_real_escape_string($connection, trim($_POST['studentID']));
-
-
-
- // Count the number of categories for the experience
- $sqlCountCategories = "SELECT DISTINCT C.id
- FROM category AS C
- JOIN question as Q
- JOIN subquestionnair_question AS SQ
- JOIN experience_subquestionnair AS ES
- WHERE C.id = Q.id_category
- AND SQ.id_question = Q.id
- AND SQ.id_subquestionnair = ES.id_subquestionnair
- AND ES.id_experience = '$experienceID'";
- $resultCountCategories = mysqli_query($connection, $sqlCountCategories);
- $countCategories = $resultCountCategories->num_rows;
-
-
-
- // Fetch all the results, depending if all students or a specific one
- if($studentID === "all") {
-
- $sql = "SELECT C.id AS categoryID,
- C.categoria AS categoryName,
- A.value AS score,
- COUNT(id_student) AS `count`
- FROM category AS C
- LEFT JOIN question AS Q
- ON Q.id_category = C.id
- JOIN answer AS A
- JOIN subquestionnair_question AS SQ
- JOIN experience_subquestionnair AS ES
- -- WHERE Q.id_category = C.id
- WHERE A.id_question = Q.id
- AND A.id_question = SQ.id_question
- AND A.id_subquestionnair = SQ.id_subquestionnair
- AND SQ.id_subquestionnair = ES.id_subquestionnair
- AND ES.id_experience = '$experienceID'
- AND SQ.id_subquestionnair = '$momentID'
- GROUP BY score, categoryID
- ORDER BY score, categoryID";
-
- } else {
-
- $sql = "SELECT C.id AS categoryID,
- C.categoria AS categoryName,
- A.value AS score,
- COUNT(id_student) AS `count`
- FROM category AS C
- LEFT JOIN question AS Q
- ON Q.id_category = C.id
- JOIN answer AS A
- JOIN subquestionnair_question AS SQ
- JOIN experience_subquestionnair AS ES
- -- WHERE Q.id_category = C.id
- WHERE A.id_question = Q.id
- AND A.id_question = SQ.id_question
- AND A.id_subquestionnair = SQ.id_subquestionnair
- AND SQ.id_subquestionnair = ES.id_subquestionnair
- AND ES.id_experience = '$experienceID' -- 10
- AND SQ.id_subquestionnair = '$momentID' -- 105
- AND A.id_student = '$studentID' -- 118
- GROUP BY score, categoryID
- ORDER BY score, categoryID";
-
- }
-
- $result = mysqli_query($connection, $sql);
-
-
-
- // Initialize Arrays
- $datasets = [];
- $dataLabels = [];
- $scoreToIndex = [];
- $i = 0;
- $categoryIDToIndex = [];
- $j = 0;
-
-
-
- // Prepare result
- while($row = mysqli_fetch_object($result)) {
-
-
- // Parse score (cause if might come from an open question)
- // or it might be a float, while we should expect an int
- // then cast it to string to use it as a map key
-
- $pointLabel = ($studentID === "all") ? " # of people who answered '" : " # of times student answered '";
-
- if(is_numeric($row->score)) {
- $score = $pointLabel . strval((int)$row->score) . "'"; // answers may be 5.0 or 5, so first we take out the .
- } else {
- continue;
- }
-
-
- // Create new label if new category is detected
- if($categoryIDToIndex[$row->categoryID] || $categoryIDToIndex[$row->categoryID] === 0) { // index 0 should be skipped
-
- // nothing
-
- } else {
-
- // Create new category
- $dataLabels[] = $row->categoryName;
-
- // Store category index for $data array (inside dataset)
- $categoryIDToIndex[$row->categoryID] = $j;
- $j++;
-
- }
-
-
-
- // Create dataset for corresponding score
- if($scoreToIndex[$score] || $scoreToIndex[$score] === 0) { // index 0 should be skipped
-
- // nothing
-
- } else {
-
- // serves as a tally to check
- // which scores have been already
- // processed AND as a map of
- // score to index of the
- // $dataset array
- $scoreToIndex[$score] = $i;
- $i++;
-
-
- // Create new dataset for this label
- $datasets[] = [
- 'label' => $score,
- 'data' => array_fill(0, $countCategories, 0), // array of countCategories 0's
- 'backgroundColor' => '#' . substr(sha1($score), 0, 6),
- // 'borderWidth' => 3,
- // 'borderColor' => '#' . substr(sha1($score), 0, 6),
- 'stack' => $score
- // 'fill' => false
- ];
-
- }
-
-
- // Append data point to corresponding category
- $datasets[$scoreToIndex[$score]]['data'][$categoryIDToIndex[$row->categoryID]] = (int)$row->count;
-
-
- }
-
-
-
-
- /*
- let data = {
- labels: dataLabels,
- datasets: [{
- label: auxiliaries['myChart2'].label,
- data: dataPoints,
- backgroundColor: auxiliaries['myChart2'].backgroundColor,
- borderColor: auxiliaries['myChart2'].borderColor,
- fill: false
- }]
- };
- */
-
-
- $arr = [
- 'datasets' => $datasets,
- 'dataLabels' => $dataLabels
- ];
-
-
- // Spit out response JSON
- echo header("Content-Type: application/json; charset=utf-8");
- echo json_encode($arr);
|