Ejemplo n.º 1
0
/**
 * Gets the table with the historical attempts of the user or group in HTML format 
 * displayed on the main screen.If the group identifier provided is not 0, the group is checked,
 * else the user is checked.
 * 
 * @param stdClass $treasurehunt The treasurehunt instance.
 * @param int $groupid The identifier of the group to which the user belongs.
 * @param int $userid The identifier of user.
 * @param int $roadid The identifier of the road of user.
 * @param int $cmid The identifier of course module activity.
 * @param string $username The user name.
 * @param bool $teacherreview If the function is invoked by a review of the teacher.
 * @return string 
 */
function treasurehunt_view_user_historical_attempts($treasurehunt, $groupid, $userid, $roadid, $cmid, $username, $teacherreview)
{
    global $PAGE;
    $roadfinished = treasurehunt_check_if_user_has_finished($userid, $groupid, $roadid);
    $attempts = treasurehunt_get_user_historical_attempts($groupid, $userid, $roadid);
    if (time() > $treasurehunt->cutoffdate && $treasurehunt->cutoffdate) {
        $outoftime = true;
    } else {
        $outoftime = false;
    }
    $output = $PAGE->get_renderer('mod_treasurehunt');
    $renderable = new treasurehunt_user_historical_attempts($attempts, $cmid, $username, $outoftime, $roadfinished, $teacherreview);
    return $output->render($renderable);
}
Ejemplo n.º 2
0
 public static function user_progress($treasurehuntid, $attempttimestamp, $roadtimestamp, $playwithoutmoving, $groupmode, $initialize, $selectedanswerid, $qoaremoved, $location)
 {
     global $USER, $DB;
     $params = self::validate_parameters(self::user_progress_parameters(), array('treasurehuntid' => $treasurehuntid, "attempttimestamp" => $attempttimestamp, "roadtimestamp" => $roadtimestamp, 'playwithoutmoving' => $playwithoutmoving, 'groupmode' => $groupmode, 'initialize' => $initialize, 'selectedanswerid' => $selectedanswerid, 'qoaremoved' => $qoaremoved));
     $cm = get_coursemodule_from_instance('treasurehunt', $params['treasurehuntid']);
     $treasurehunt = $DB->get_record('treasurehunt', array('id' => $cm->instance), '*', MUST_EXIST);
     $context = context_module::instance($cm->id);
     self::validate_context($context);
     require_capability('mod/treasurehunt:play', $context, null, false);
     // Get the group and road to which the user belongs.
     $userparams = treasurehunt_get_user_group_and_road($USER->id, $treasurehunt, $cm->id);
     // Get the total number of stages of the road of the user.
     $nostages = treasurehunt_get_total_stages($userparams->roadid);
     // Check if the user has finished the road.
     $roadfinished = treasurehunt_check_if_user_has_finished($USER->id, $userparams->groupid, $userparams->roadid);
     $changesingroupmode = false;
     $qoaremoved = $params['qoaremoved'];
     if ($params['groupmode'] != $treasurehunt->groupmode) {
         $changesingroupmode = true;
     }
     // Get the info of the newly discovered stages if any , and the new timestamp if they have changed.
     $updates = treasurehunt_check_attempts_updates($params['attempttimestamp'], $userparams->groupid, $USER->id, $userparams->roadid, $changesingroupmode);
     if ($updates->newroadtimestamp != $params['roadtimestamp']) {
         $updateroad = true;
     } else {
         $updateroad = false;
     }
     $available = treasurehunt_is_available($treasurehunt);
     $playmode = $params['playwithoutmoving'];
     if ($available->available && !$roadfinished) {
         $changesinplaymode = false;
         $playmode = treasurehunt_get_play_mode($USER->id, $userparams->groupid, $userparams->roadid, $treasurehunt);
         if ($params['playwithoutmoving'] != $playmode) {
             $changesinplaymode = true;
         }
         // Check if the user has correctly completed the question and the required activity.
         $qocsolved = treasurehunt_check_question_and_activity_solved($params['selectedanswerid'], $USER->id, $userparams->groupid, $userparams->roadid, $updateroad, $context, $treasurehunt, $nostages, $qoaremoved);
         if ($qocsolved->msg !== '') {
             $status['msg'] = $qocsolved->msg;
             $status['code'] = 0;
         }
         if ($qocsolved->success) {
             $playmode = treasurehunt_get_play_mode($USER->id, $userparams->groupid, $userparams->roadid, $treasurehunt);
         }
         if (count($qocsolved->updates)) {
             $updates->strings = array_merge($updates->strings, $qocsolved->updates);
         }
         if ($qocsolved->newattempt) {
             $updates->newattempttimestamp = $qocsolved->attempttimestamp;
         }
         if ($qocsolved->attemptsolved) {
             $updates->attemptsolved = true;
         }
         if ($qocsolved->roadfinished) {
             $roadfinished = true;
         }
         $qoaremoved = $qocsolved->qoaremoved;
         if (!$updates->geometrysolved && $location && !$updateroad && !$changesinplaymode && !$changesingroupmode) {
             $checklocation = treasurehunt_check_user_location($USER->id, $userparams->groupid, $userparams->roadid, treasurehunt_geojson_to_object($location), $context, $treasurehunt, $nostages);
             if ($checklocation->newattempt) {
                 $updates->newattempttimestamp = $checklocation->attempttimestamp;
                 $updates->newgeometry = true;
             }
             if ($checklocation->newstage) {
                 $updates->geometrysolved = true;
                 if ($checklocation->success) {
                     $playmode = treasurehunt_get_play_mode($USER->id, $userparams->groupid, $userparams->roadid, $treasurehunt);
                 }
             }
             if ($checklocation->roadfinished) {
                 $roadfinished = true;
             }
             if ($checklocation->update !== '') {
                 $updates->strings[] = $checklocation->update;
             }
             $status['msg'] = $checklocation->msg;
             $status['code'] = 0;
         }
     }
     $historicalattempts = array();
     // If there was any new attempt, reload the history of attempts.
     if ($updates->newattempttimestamp != $params['attempttimestamp'] || $params['initialize']) {
         $historicalattempts = treasurehunt_get_user_historical_attempts($userparams->groupid, $USER->id, $userparams->roadid);
     }
     $lastsuccessfulstage = array();
     if ($updates->geometrysolved || !$available->available || $updateroad || $updates->attemptsolved || $params['initialize'] || $changesingroupmode) {
         $lastsuccessfulstage = treasurehunt_get_last_successful_stage($USER->id, $userparams->groupid, $userparams->roadid, $nostages, $available->outoftime, $available->actnotavailableyet, $context);
     }
     if ($updates->newgeometry || $updateroad || $roadfinished || $params['initialize'] || $changesingroupmode) {
         list($userattempts, $firststagegeom) = treasurehunt_get_user_progress($userparams->roadid, $userparams->groupid, $USER->id, $treasurehuntid, $context);
     }
     // If the road has been edited, warning the user.
     if ($updateroad) {
         if ($location) {
             $status['msg'] = get_string('errsendinglocation', 'treasurehunt');
             $status['code'] = 1;
         }
         if ($params['selectedanswerid']) {
             $status['msg'] = get_string('errsendinganswer', 'treasurehunt');
             $status['code'] = 1;
         }
     }
     // If the activity is out of time and not being initialized, warning the user.
     if ($available->outoftime && !$params['initialize']) {
         $updates->strings[] = get_string('timeexceeded', 'treasurehunt');
     }
     if ($available->actnotavailableyet) {
         $updates->strings[] = get_string('actnotavailableyet', 'treasurehunt');
     }
     if (!$status) {
         $status['msg'] = get_string('userprogress', 'treasurehunt');
         $status['code'] = 0;
     }
     if ($params['playwithoutmoving'] != $playmode) {
         if (!$playmode) {
             $updates->strings[] = get_string('changetoplaywithmove', 'treasurehunt');
         } else {
             $updates->strings[] = get_string('changetoplaywithoutmoving', 'treasurehunt');
         }
     }
     $result = array();
     $result['infomsg'] = $updates->strings;
     $result['attempttimestamp'] = $updates->newattempttimestamp;
     $result['roadtimestamp'] = $updates->newroadtimestamp;
     $result['status'] = $status;
     if ($userattempts || $firststagegeom) {
         if ($firststagegeom) {
             $result['firststagegeom'] = $firststagegeom;
         }
         $result['attempts'] = $userattempts;
     }
     if ($lastsuccessfulstage) {
         $result['lastsuccessfulstage'] = $lastsuccessfulstage;
     }
     $result['roadfinished'] = $roadfinished;
     $result['available'] = $available->available;
     $result['playwithoutmoving'] = intval($playmode);
     $result['groupmode'] = intval($treasurehunt->groupmode);
     $result['historicalattempts'] = $historicalattempts;
     $result['qoaremoved'] = $qoaremoved;
     return $result;
 }