[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/survey/ -> download.php (source)

   1  <?php // $Id: download.php,v 1.27 2007/10/09 21:43:30 iarenaza Exp $
   2  
   3      require ("../../config.php");
   4  
   5  // Check that all the parameters have been provided.
   6  
   7      $id    = required_param('id', PARAM_INT);    // Course Module ID
   8      $type  = optional_param('type', 'xls', PARAM_ALPHA);
   9      $group = optional_param('group', 0, PARAM_INT);
  10  
  11      if (! $cm = get_record("course_modules", "id", $id)) {
  12          error("Course Module ID was incorrect");
  13      }
  14  
  15      if (! $course = get_record("course", "id", $cm->course)) {
  16          error("Course is misconfigured");
  17      }
  18  
  19      require_login($course->id, false, $cm);
  20      require_capability('mod/survey:download', get_context_instance(CONTEXT_MODULE, $cm->id)) ;
  21  
  22      if (! $survey = get_record("survey", "id", $cm->instance)) {
  23          error("Survey ID was incorrect");
  24      }
  25  
  26      add_to_log($course->id, "survey", "download", "download.php?id=$cm->id&amp;type=$type", "$survey->id", $cm->id);
  27  
  28  /// Check to see if groups are being used in this survey
  29  
  30      $groupmode = groups_get_activity_groupmode($cm);   // Groups are being used
  31  
  32      if ($groupmode and $group) {
  33          $users = groups_get_members($group);
  34      } else {
  35          $users = get_course_users($course->id);
  36          $group = false;
  37      }
  38  
  39  // Get all the questions and their proper order
  40  
  41      $questions = get_records_list("survey_questions", "id", $survey->questions);
  42      $order = explode(",", $survey->questions);
  43  
  44      $virtualscales = false;
  45      foreach ($order as $key => $qid) {  // Do we have virtual scales?
  46          $question = $questions[$qid];
  47          if ($question->type < 0) {
  48              $virtualscales = true;
  49              break;
  50          }
  51      }
  52  
  53      $fullorderlist = "";
  54      foreach ($order as $key => $qid) {    // build up list of actual questions
  55          $question = $questions[$qid];
  56  
  57          if (!(empty($fullorderlist))) {
  58              $fullorderlist .= ",";
  59          }
  60  
  61          if ($question->multi) {
  62              $addlist = $question->multi;
  63          } else {
  64              $addlist = $qid;
  65          }
  66  
  67          if ($virtualscales && ($question->type < 0)) {        // only use them
  68              $fullorderlist .= $addlist;
  69  
  70          } else if (!$virtualscales && ($question->type >= 0)){   // ignore them
  71              $fullorderlist .= $addlist;
  72          }
  73      }
  74  
  75      $fullquestions = get_records_list("survey_questions", "id", $fullorderlist);
  76  
  77  //  Question type of multi-questions overrides the type of single questions
  78      foreach ($order as $key => $qid) {
  79          $question = $questions[$qid];
  80  
  81          if ($question->multi) {
  82              $subs = explode(",", $question->multi);
  83              while (list ($skey, $sqid) = each ($subs)) {
  84                  $fullquestions["$sqid"]->type = $question->type;
  85              }
  86          }
  87      }
  88  
  89      $order     = explode(",", $fullorderlist);
  90      $questions = $fullquestions;
  91  
  92  //  Translate all the question texts
  93  
  94      foreach ($questions as $key => $question) {
  95          $questions[$key]->text = get_string($question->text, "survey");
  96      }
  97  
  98  
  99  // Get and collate all the results in one big array
 100  
 101      if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) {
 102          error("There are no answers for this survey yet.");
 103      }
 104  
 105      foreach ($aaa as $a) {
 106          if (!$group or isset($users[$a->userid])) {
 107              if (empty($results["$a->userid"])) { // init new array
 108                  $results["$a->userid"]["time"] = $a->time;
 109                  foreach ($order as $key => $qid) {
 110                      $results["$a->userid"]["$qid"]["answer1"] = "";
 111                      $results["$a->userid"]["$qid"]["answer2"] = "";
 112                  }
 113              }
 114              $results["$a->userid"]["$a->question"]["answer1"] = $a->answer1;
 115              $results["$a->userid"]["$a->question"]["answer2"] = $a->answer2;
 116          }
 117      }
 118  
 119  // Output the file as a valid ODS spreadsheet if required
 120  
 121      if ($type == "ods") {
 122          require_once("$CFG->libdir/odslib.class.php");
 123  
 124      /// Calculate file name
 125          $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.ods';
 126      /// Creating a workbook
 127          $workbook = new MoodleODSWorkbook("-");
 128      /// Sending HTTP headers
 129          $workbook->send($downloadfilename);
 130      /// Creating the first worksheet
 131          $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
 132  
 133          $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
 134          $col=0;
 135          foreach ($header as $item) {
 136              $myxls->write_string(0,$col++,$item);
 137          }
 138          foreach ($order as $key => $qid) {
 139              $question = $questions["$qid"];
 140              if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1")  {
 141                  $myxls->write_string(0,$col++,"$question->text");
 142              }
 143              if ($question->type == "2" || $question->type == "3")  {
 144                  $myxls->write_string(0,$col++,"$question->text (preferred)");
 145              }
 146          }
 147  
 148  //      $date = $workbook->addformat();
 149  //      $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
 150  
 151          $row = 0;
 152          foreach ($results as $user => $rest) {
 153              $col = 0;
 154              $row++;
 155              if (! $u = get_record("user", "id", $user)) {
 156                  error("Error finding student # $user");
 157              }
 158              if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
 159                  $notes = $n->notes;
 160              } else {
 161                  $notes = "No notes made";
 162              }
 163              $myxls->write_string($row,$col++,$survey->id);
 164              $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
 165              $myxls->write_string($row,$col++,$user);
 166              $myxls->write_string($row,$col++,$u->firstname);
 167              $myxls->write_string($row,$col++,$u->lastname);
 168              $myxls->write_string($row,$col++,$u->email);
 169              $myxls->write_string($row,$col++,$u->idnumber);
 170              $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
 171  //          $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
 172              $myxls->write_string($row,$col++,$notes);
 173  
 174              foreach ($order as $key => $qid) {
 175                  $question = $questions["$qid"];
 176                  if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1")  {
 177                      $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
 178                  }
 179                  if ($question->type == "2" || $question->type == "3")  {
 180                      $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
 181                  }
 182              }
 183          }
 184          $workbook->close();
 185  
 186          exit;
 187      }
 188  
 189  // Output the file as a valid Excel spreadsheet if required
 190  
 191      if ($type == "xls") {
 192          require_once("$CFG->libdir/excellib.class.php");
 193  
 194      /// Calculate file name
 195          $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.xls';
 196      /// Creating a workbook
 197          $workbook = new MoodleExcelWorkbook("-");
 198      /// Sending HTTP headers
 199          $workbook->send($downloadfilename);
 200      /// Creating the first worksheet
 201          $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
 202  
 203          $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
 204          $col=0;
 205          foreach ($header as $item) {
 206              $myxls->write_string(0,$col++,$item);
 207          }
 208          foreach ($order as $key => $qid) {
 209              $question = $questions["$qid"];
 210              if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1")  {
 211                  $myxls->write_string(0,$col++,"$question->text");
 212              }
 213              if ($question->type == "2" || $question->type == "3")  {
 214                  $myxls->write_string(0,$col++,"$question->text (preferred)");
 215              }
 216          }
 217  
 218  //      $date = $workbook->addformat();
 219  //      $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
 220  
 221          $row = 0;
 222          foreach ($results as $user => $rest) {
 223              $col = 0;
 224              $row++;
 225              if (! $u = get_record("user", "id", $user)) {
 226                  error("Error finding student # $user");
 227              }
 228              if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
 229                  $notes = $n->notes;
 230              } else {
 231                  $notes = "No notes made";
 232              }
 233              $myxls->write_string($row,$col++,$survey->id);
 234              $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
 235              $myxls->write_string($row,$col++,$user);
 236              $myxls->write_string($row,$col++,$u->firstname);
 237              $myxls->write_string($row,$col++,$u->lastname);
 238              $myxls->write_string($row,$col++,$u->email);
 239              $myxls->write_string($row,$col++,$u->idnumber);
 240              $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
 241  //          $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
 242              $myxls->write_string($row,$col++,$notes);
 243  
 244              foreach ($order as $key => $qid) {
 245                  $question = $questions["$qid"];
 246                  if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1")  {
 247                      $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
 248                  }
 249                  if ($question->type == "2" || $question->type == "3")  {
 250                      $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
 251                  }
 252              }
 253          }
 254          $workbook->close();
 255  
 256          exit;
 257      }
 258  
 259  // Otherwise, return the text file.
 260  
 261  // Print header to force download
 262  
 263      header("Content-Type: application/download\n");
 264  
 265      $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true)));
 266      header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
 267  
 268  // Print names of all the fields
 269  
 270      echo "surveyid    surveyname    userid    firstname    lastname    email    idnumber    time    ";
 271      foreach ($order as $key => $qid) {
 272          $question = $questions["$qid"];
 273          if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1")  {
 274              echo "$question->text    ";
 275          }
 276          if ($question->type == "2" || $question->type == "3")  {
 277               echo "$question->text (preferred)    ";
 278          }
 279      }
 280      echo "\n";
 281  
 282  // Print all the lines of data.
 283  
 284      foreach ($results as $user => $rest) {
 285          if (! $u = get_record("user", "id", $user)) {
 286              error("Error finding student # $user");
 287          }
 288          echo $survey->id."\t";
 289          echo strip_tags(format_string($survey->name,true))."\t";
 290          echo $user."\t";
 291          echo $u->firstname."\t";
 292          echo $u->lastname."\t";
 293          echo $u->email."\t";
 294          echo $u->idnumber."\t";
 295          echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
 296  
 297          foreach ($order as $key => $qid) {
 298              $question = $questions["$qid"];
 299              if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1")  {
 300                  echo $results["$user"]["$qid"]["answer1"]."    ";
 301              }
 302              if ($question->type == "2" || $question->type == "3")  {
 303                  echo $results["$user"]["$qid"]["answer2"]."    ";
 304              }
 305          }
 306          echo "\n";
 307      }
 308      exit;
 309  
 310  
 311  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7