/**
  * Gets the data for the table
  *
  * @return array $data The array of data to show
  */
 protected function get_data()
 {
     global $DB, $USER;
     $data = array();
     $sessions = $this->rtq->get_sessions();
     foreach ($sessions as $session) {
         /** @var \mod_activequiz\activequiz_session $session */
         $sessionattempts = $session->getall_attempts(false, 'closed', $USER->id);
         foreach ($sessionattempts as $sattempt) {
             $ditem = new \stdClass();
             $ditem->attemptid = $sattempt->id;
             $ditem->sessionid = $sattempt->sessionid;
             $ditem->sessionname = $session->get_session()->name;
             if ($this->rtq->group_mode()) {
                 $ditem->group = $this->rtq->get_groupmanager()->get_group_name($sattempt->forgroupid);
             }
             $ditem->timestart = $sattempt->timestart;
             $ditem->timefinish = $sattempt->timefinish;
             $ditem->grade = number_format($this->rtq->get_grader()->calculate_attempt_grade($sattempt), 2);
             $ditem->totalgrade = $this->rtq->getRTQ()->scale;
             $data[$sattempt->id] = $ditem;
         }
     }
     return $data;
 }
 /**
  * Separated function to process grading for the provided sessions
  *
  * @param array $sessions
  * @param int   $userid If specified will only process grading with that particular user
  *
  * @return bool true on success
  */
 protected function process_sessions($sessions, $userid = null)
 {
     global $DB;
     $sessionsgrades = array();
     foreach ($sessions as $session) {
         /** @var \mod_activequiz\activequiz_session $session */
         if ($session->get_session()->sessionopen === 1) {
             continue;
             // don't calculate grades for open sessions
         }
         // process grades when userid is present
         if (!is_null($userid)) {
             if (!isset($sessionsgrades[$userid])) {
                 $sessionsgrades[$userid] = array();
             }
             list($forgroupid, $grade) = $this->get_session_grade($session, $userid);
             if ($this->rtq->group_mode()) {
                 $this->calculate_group_grades($sessionsgrades, $forgroupid, $grade, $userid, $session->get_session()->id);
             } else {
                 $sessionsgrades[$userid][$session->get_session()->id] = $grade;
             }
         } else {
             // extra processing to get user grades into it separately
             // get and loop through the users for this session to get their grade
             foreach ($session->get_session_users() as $user) {
                 $uid = $user->userid;
                 if (!isset($sessionsgrades[$uid])) {
                     // add the userid to the sessions grade as an array
                     $sessionsgrades[$uid] = array();
                 }
                 list($forgroupid, $grade) = $this->get_session_grade($session, $uid);
                 if ($this->rtq->group_mode()) {
                     $this->calculate_group_grades($sessionsgrades, $forgroupid, $grade, $uid, $session->get_session()->id);
                 } else {
                     $sessionsgrades[$uid][$session->get_session()->id] = $grade;
                 }
             }
         }
     }
     $grades = $this->calculate_grade($sessionsgrades);
     // run the whole thing on a transaction (persisting to our table and gradebook updates)
     $transaction = $DB->start_delegated_transaction();
     // now that we have the final grades persist the grades to activequiz grades table
     $this->persist_grades($grades, $transaction);
     // update grades to gradebookapi
     $updated = activequiz_update_grades($this->rtq->getRTQ(), array_keys($grades));
     if ($updated === GRADE_UPDATE_FAILED) {
         $transaction->rollback(new \Exception('Unable to save grades to gradebook'));
     }
     // Allow commit if we get here
     $transaction->allow_commit();
     // if everything passes to here return true
     return true;
 }
 /**
  * Renders a response for a specific question and attempt
  *
  * @param \mod_activequiz\activequiz_attempt $attempt
  *
  * @return string HTML fragment for the response
  */
 public function render_response($attempt)
 {
     global $DB;
     $response = html_writer::start_div('response');
     // check if group mode, if so, give the group name the attempt is for
     if ($this->rtq->group_mode()) {
         $name = $this->rtq->get_groupmanager()->get_group_name($attempt->forgroupid);
     } else {
         $user = $DB->get_record('user', array('id' => $attempt->userid));
         $name = fullname($user);
     }
     $response .= html_writer::tag('h3', $name, array('class' => 'responsename'));
     $response .= html_writer::div($attempt->responsesummary, 'responsesummary');
     $response .= html_writer::end_div();
     return $response;
 }
 /**
  * Gets the data for the table
  *
  * @return array $data The array of data to show
  */
 protected function get_data()
 {
     global $DB;
     $data = array();
     $attempts = $this->session->getall_attempts(true);
     $userids = array();
     foreach ($attempts as $attempt) {
         $userids[] = $attempt->userid;
     }
     // get user records to get the full name
     if (!empty($userids)) {
         list($useridsql, $params) = $DB->get_in_or_equal($userids);
         $sql = 'SELECT * FROM {user} WHERE id ' . $useridsql;
         $userrecs = $DB->get_records_sql($sql, $params);
     } else {
         $userrecs = array();
     }
     foreach ($attempts as $attempt) {
         /** @var \mod_activequiz\activequiz_attempt $attempt */
         $ditem = new \stdClass();
         $ditem->attemptid = $attempt->id;
         $ditem->sessionid = $attempt->sessionid;
         if ($this->rtq->group_mode()) {
             $ditem->userid = $attempt->userid;
             $ditem->takenby = fullname($userrecs[$attempt->userid]);
             $ditem->groupname = $this->rtq->get_groupmanager()->get_group_name($attempt->forgroupid);
         } else {
             $ditem->userid = $attempt->userid;
             $userrec = $userrecs[$attempt->userid];
             $ditem->username = fullname($userrec);
         }
         $ditem->attemptno = $attempt->attemptnum;
         $ditem->preview = $attempt->preview;
         $ditem->status = $attempt->getStatus();
         $ditem->timestart = $attempt->timestart;
         $ditem->timefinish = $attempt->timefinish;
         $ditem->timemodified = $attempt->timemodified;
         $ditem->grade = number_format($this->rtq->get_grader()->calculate_attempt_grade($attempt), 2);
         $ditem->totalgrade = $this->rtq->getRTQ()->scale;
         $data[$attempt->id] = $ditem;
     }
     return $data;
 }
 /**
  * Gets the data for the table
  *
  * @return array $data The array of data to show
  */
 protected function get_data()
 {
     global $DB, $CFG;
     $data = array();
     $grades = \mod_activequiz\utils\grade::get_user_grade($this->rtq->getRTQ());
     $userids = array();
     foreach ($grades as $grade) {
         $userids[] = $grade->userid;
     }
     // get user records to get the full name
     if (!empty($userids)) {
         list($useridsql, $params) = $DB->get_in_or_equal($userids);
         $sql = 'SELECT * FROM {user} WHERE id ' . $useridsql;
         $userrecs = $DB->get_records_sql($sql, $params);
     } else {
         $userrecs = array();
     }
     foreach ($grades as $grade) {
         // check to see if the grade is for a gradebook role.  if not then their grade
         // shouldn't show up here
         $add = false;
         if ($roles = get_user_roles($this->rtq->getContext(), $grade->userid)) {
             $gradebookroles = explode(',', $CFG->gradebookroles);
             foreach ($roles as $role) {
                 if (in_array($role->roleid, $gradebookroles)) {
                     // if they have at least one gradebook role show their grade
                     $add = true;
                 }
             }
             if ($add === false) {
                 // don't show grade for non gradebook role
                 continue;
             }
         } else {
             // if there are no given roles for the context, then they aren't students
             continue;
         }
         $gradedata = new \stdClass();
         $gradedata->fullname = fullname($userrecs[$grade->userid]);
         if ($this->rtq->group_mode()) {
             $groups = $this->rtq->get_groupmanager()->get_user_groups($grade->userid);
             if (!empty($groups)) {
                 $groupstring = '';
                 foreach ($groups as $group) {
                     if (strlen($groupstring) > 0) {
                         // add a comma space if we're back in the foreach for a second or more time
                         $groupstring .= ', ';
                     }
                     $groupstring .= $this->rtq->get_groupmanager()->get_group_name($group->id);
                 }
                 $gradedata->group = $groupstring;
             } else {
                 $gradedata->group = ' - ';
             }
         }
         $gradedata->grade = number_format($grade->rawgrade, 2);
         $gradedata->timemodified = $grade->dategraded;
         $data[] = $gradedata;
     }
     return $data;
 }