Example #1
0
 /**
  * Send a grade and update our local copy
  *
  * Call the right LTI service to send a new grade up to the server.
  * update our local cached copy of the server_grade and the date
  * retrieved. This routine pulls the key and secret from the LTIX
  * session to avoid crossing cross tennant boundaries.
  *
  * @param $grade A new grade - floating point number between 0.0 and 1.0
  * @param $row An optional array with the data that has the result_id, sourcedid,
  * and service (url) if this is not present, the data is pulled from the LTI
  * session for the current user/link combination.
  * @param $debug_log An (optional) array (by reference) that returns the
  * steps that were taken.
  * Each entry is an array with the [0] element a message and an optional [1]
  * element as some detail (i.e. like a POST body)
  *
  * @return mixed If this works it returns true.  If not, you get
  * a string with an error.
  *
  */
 public function gradeSend($grade, $row = false, &$debug_log = false)
 {
     global $CFG, $USER;
     global $LastPOXGradeResponse;
     $LastPOXGradeResponse = false;
     $PDOX = LTIX::getConnection();
     // Secret and key from session to avoid crossing tenant boundaries
     $key_key = LTIX::sessionGet('key_key');
     $secret = LTIX::sessionGet('secret');
     if ($row !== false) {
         $result_url = isset($row['result_url']) ? $row['result_url'] : false;
         $sourcedid = isset($row['sourcedid']) ? $row['sourcedid'] : false;
         $service = isset($row['service']) ? $row['service'] : false;
         // Fall back to session if it is missing
         if ($service === false) {
             $service = LTIX::sessionGet('service');
         }
         $result_id = isset($row['result_id']) ? $row['result_id'] : false;
     } else {
         $result_url = LTIX::sessionGet('result_url');
         $sourcedid = LTIX::sessionGet('sourcedid');
         $service = LTIX::sessionGet('service');
         $result_id = LTIX::sessionGet('result_id');
     }
     // Update result in the database and in the LTI session area and
     // our local copy
     $_SESSION['lti']['grade'] = $grade;
     $this->grade = $grade;
     // Update the local copy of the grade in the lti_result table
     if ($PDOX !== false && $result_id !== false) {
         $stmt = $PDOX->queryReturnError("UPDATE {$CFG->dbprefix}lti_result SET grade = :grade,\n                    updated_at = NOW() WHERE result_id = :RID", array(':grade' => $grade, ':RID' => $result_id));
         if ($stmt->success) {
             $msg = "Grade updated result_id=" . $result_id . " grade={$grade}";
         } else {
             $msg = "Grade NOT updated result_id=" . $result_id . " grade={$grade}";
         }
         error_log($msg);
         if (is_array($debug_log)) {
             $debug_log[] = array($msg);
         }
     }
     if ($key_key == false || $secret === false || $sourcedid === false || $service === false || !isset($USER)) {
         error_log("Result::gradeSend stored data locally");
         return false;
     }
     // TODO: Fix this
     $comment = "";
     if (strlen($result_url) > 0) {
         $status = LTI::sendJSONGrade($grade, $comment, $result_url, $key_key, $secret, $debug_log);
     } else {
         $status = LTI::sendPOXGrade($grade, $sourcedid, $service, $key_key, $secret, $debug_log);
     }
     if ($status === true) {
         $msg = 'Grade sent ' . $grade . ' to ' . $sourcedid . ' by ' . $USER->id;
         if (is_array($debug_log)) {
             $debug_log[] = array($msg);
         }
         error_log($msg);
     } else {
         $msg = 'Grade failure ' . $grade . ' to ' . $sourcedid . ' by ' . $USER->id;
         if (is_array($debug_log)) {
             $debug_log[] = array($msg);
         }
         error_log($msg);
         return $status;
     }
     return $status;
 }