/**
  * Closes the attempt
  *
  * @param \mod_activequiz\activequiz $rtq
  *
  * @return bool Weather or not it was successful
  */
 public function close_attempt($rtq)
 {
     $this->quba->finish_all_questions(time());
     $this->attempt->status = self::FINISHED;
     $this->attempt->timefinish = time();
     $this->save();
     $params = array('objectid' => $this->attempt->id, 'context' => $rtq->getContext(), 'relateduserid' => $this->attempt->userid);
     $event = \mod_activequiz\event\attempt_ended::create($params);
     $event->add_record_snapshot('activequiz_attempts', $this->attempt);
     $event->trigger();
     return true;
 }
 /**
  * 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;
 }