/**
  * 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;
 }
 /**
  * Displays the view home.
  *
  * @param \mod_activequiz\forms\view\student_start_form $studentstartform
  * @param \mod_activequiz\activequiz_session            $session The activequiz session object to call methods on
  */
 public function view_student_home($studentstartform, $session)
 {
     global $USER;
     echo html_writer::start_div('activequizbox');
     if ($session->get_session()) {
         // check if there is an open session
         // if we have an existing session show the join quiz button
         $joinquiz = clone $this->pageurl;
         $joinquiz->param('action', 'quizstart');
         echo html_writer::tag('p', get_string('joinquizinstructions', 'activequiz'));
         echo html_writer::tag('p', get_string('sessionnametext', 'activequiz') . $session->get_session()->name);
         // see if the user has attempts, if so, let them know that continuing will continue them to their attempt
         if ($session->get_open_attempt_for_current_user()) {
             echo html_writer::tag('p', get_string('attemptstarted', 'activequiz'), array('id' => 'quizinfobox'));
         }
         // add the student join quiz form
         $studentstartform->display();
     } else {
         echo html_writer::tag('p', get_string('quiznotrunning', 'activequiz'));
         // show a reload page button to make it easy to reload page
         $reloadbutton = $this->output->single_button($this->pageurl, get_string('reload'), 'get');
         echo html_writer::tag('p', $reloadbutton);
     }
     echo html_writer::end_div();
     if (count($this->rtq->get_closed_sessions()) == 0) {
         return;
         // return early if there are no closed sessions
     }
     echo html_writer::start_div('activequizbox');
     // show overall grade
     $a = new stdClass();
     $usergrades = \mod_activequiz\utils\grade::get_user_grade($this->rtq->getRTQ(), $USER->id);
     // should only be 1 grade, but we'll always get end()
     if (!empty($usergrades)) {
         $usergrade = end($usergrades);
         $a->overallgrade = number_format($usergrade->rawgrade, 2);
         $a->scale = $this->rtq->getRTQ()->scale;
         echo html_writer::start_tag('h3');
         echo get_string('overallgrade', 'activequiz', $a);
         echo html_writer::end_tag('h3');
     } else {
         return;
         // if no user grade there are no attempts for this user
     }
     // show attempts table if rtq is set up to show attempts in the after review options
     if ($this->rtq->get_review_options('after')->attempt == 1) {
         echo html_writer::tag('h3', get_string('attempts', 'activequiz'));
         $viewownattemptstable = new \mod_activequiz\tableviews\ownattempts('viewownattempts', $this->rtq, $this->pageurl);
         $viewownattemptstable->setup();
         $viewownattemptstable->set_data();
         $viewownattemptstable->finish_output();
     }
     echo html_writer::end_div();
 }
示例#3
0
/**
 * Update grades depending on the userid and other settings
 *
 * @param      $activequiz
 * @param int  $userid
 * @param bool $nullifnone
 *
 * @return int Returns GRADE_UPDATE_OK, GRADE_UPDATE_FAILED, GRADE_UPDATE_MULTIPLE or GRADE_UPDATE_ITEM_LOCKED
 */
function activequiz_update_grades($activequiz, $userid = 0, $nullifnone = true)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/gradelib.php';
    if (!$activequiz->graded) {
        return activequiz_grade_item_update($activequiz);
    } else {
        if ($grades = \mod_activequiz\utils\grade::get_user_grade($activequiz, $userid)) {
            return activequiz_grade_item_update($activequiz, $grades);
        } else {
            if ($userid and $nullifnone) {
                $grade = new stdClass();
                $grade->userid = $userid;
                $grade->rawgrade = null;
                return activequiz_grade_item_update($activequiz, $grade);
            } else {
                return activequiz_grade_item_update($activequiz);
            }
        }
    }
}