protected function display_content($question, $rowclasses)
 {
     if (!empty($question->modifierfirstname) && !empty($question->modifierlastname)) {
         $u = new \stdClass();
         $u = username_load_fields_from_object($u, $question, 'modifier');
         echo fullname($u);
     }
 }
Пример #2
0
 protected function display_content($question, $rowclasses)
 {
     if (!empty($question->creatorfirstname) && !empty($question->creatorlastname)) {
         $u = new \stdClass();
         $u = username_load_fields_from_object($u, $question, 'creator');
         $date = userdate($question->timecreated, get_string('strftimedatetime', 'langconfig'));
         echo fullname($u) . '<br>' . \html_writer::tag('span', $date, array('class' => 'date'));
     }
 }
 /**
  * Returns list of course contacts (usually teachers) to display in course link
  *
  * Roles to display are set up in $CFG->coursecontact
  *
  * The result is the list of users where user id is the key and the value
  * is an array with elements:
  *  - 'user' - object containing basic user information
  *  - 'role' - object containing basic role information (id, name, shortname, coursealias)
  *  - 'rolename' => role_get_name($role, $context, ROLENAME_ALIAS)
  *  - 'username' => fullname($user, $canviewfullnames)
  *
  * @return array
  */
 public function get_course_contacts()
 {
     global $CFG;
     if (empty($CFG->coursecontact)) {
         // No roles are configured to be displayed as course contacts.
         return array();
     }
     if ($this->coursecontacts === null) {
         $this->coursecontacts = array();
         $context = context_course::instance($this->id);
         if (!isset($this->record->managers)) {
             // Preload course contacts from DB.
             $courses = array($this->id => &$this->record);
             coursecat::preload_course_contacts($courses);
         }
         // Build return array with full roles names (for this course context) and users names.
         $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
         foreach ($this->record->managers as $ruser) {
             if (isset($this->coursecontacts[$ruser->id])) {
                 // Only display a user once with the highest sortorder role.
                 continue;
             }
             $user = new stdClass();
             $user = username_load_fields_from_object($user, $ruser, null, array('id', 'username'));
             $role = new stdClass();
             $role->id = $ruser->roleid;
             $role->name = $ruser->rolename;
             $role->shortname = $ruser->roleshortname;
             $role->coursealias = $ruser->rolecoursealias;
             $this->coursecontacts[$user->id] = array('user' => $user, 'role' => $role, 'rolename' => role_get_name($role, $context, ROLENAME_ALIAS), 'username' => fullname($user, $canviewfullnames));
         }
     }
     return $this->coursecontacts;
 }
Пример #4
0
 /**
  * Returns a list of forum posts for a discussion
  *
  * @param int $discussionid the post ids
  * @param string $sortby sort by this element (id, created or modified)
  * @param string $sortdirection sort direction: ASC or DESC
  *
  * @return array the forum post details
  * @since Moodle 2.7
  */
 public static function get_forum_discussion_posts($discussionid, $sortby = "created", $sortdirection = "DESC")
 {
     global $CFG, $DB, $USER;
     $warnings = array();
     // Validate the parameter.
     $params = self::validate_parameters(self::get_forum_discussion_posts_parameters(), array('discussionid' => $discussionid, 'sortby' => $sortby, 'sortdirection' => $sortdirection));
     // Compact/extract functions are not recommended.
     $discussionid = $params['discussionid'];
     $sortby = $params['sortby'];
     $sortdirection = $params['sortdirection'];
     $sortallowedvalues = array('id', 'created', 'modified');
     if (!in_array($sortby, $sortallowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for sortby parameter (value: ' . $sortby . '),' . 'allowed values are: ' . implode(',', $sortallowedvalues));
     }
     $sortdirection = strtoupper($sortdirection);
     $directionallowedvalues = array('ASC', 'DESC');
     if (!in_array($sortdirection, $directionallowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for sortdirection parameter (value: ' . $sortdirection . '),' . 'allowed values are: ' . implode(',', $directionallowedvalues));
     }
     $discussion = $DB->get_record('forum_discussions', array('id' => $discussionid), '*', MUST_EXIST);
     $forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST);
     $course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
     $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
     // Validate the module context. It checks everything that affects the module visibility (including groupings, etc..).
     $modcontext = context_module::instance($cm->id);
     self::validate_context($modcontext);
     // This require must be here, see mod/forum/discuss.php.
     require_once $CFG->dirroot . "/mod/forum/lib.php";
     // Check they have the view forum capability.
     require_capability('mod/forum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum');
     if (!($post = forum_get_post_full($discussion->firstpost))) {
         throw new moodle_exception('notexists', 'forum');
     }
     // This function check groups, qanda, timed discussions, etc.
     if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
         throw new moodle_exception('noviewdiscussionspermission', 'forum');
     }
     $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
     // We will add this field in the response.
     $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
     $forumtracked = forum_tp_is_tracked($forum);
     $sort = 'p.' . $sortby . ' ' . $sortdirection;
     $posts = forum_get_all_discussion_posts($discussion->id, $sort, $forumtracked);
     foreach ($posts as $pid => $post) {
         if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
             $warning = array();
             $warning['item'] = 'post';
             $warning['itemid'] = $post->id;
             $warning['warningcode'] = '1';
             $warning['message'] = 'You can\'t see this post';
             $warnings[] = $warning;
             continue;
         }
         // Function forum_get_all_discussion_posts adds postread field.
         // Note that the value returned can be a boolean or an integer. The WS expects a boolean.
         if (empty($post->postread)) {
             $posts[$pid]->postread = false;
         } else {
             $posts[$pid]->postread = true;
         }
         $posts[$pid]->canreply = $canreply;
         if (!empty($posts[$pid]->children)) {
             $posts[$pid]->children = array_keys($posts[$pid]->children);
         } else {
             $posts[$pid]->children = array();
         }
         $user = new stdclass();
         $user = username_load_fields_from_object($user, $post);
         $posts[$pid]->userfullname = fullname($user, $canviewfullname);
         $posts[$pid] = (array) $post;
     }
     $result = array();
     $result['posts'] = $posts;
     $result['warnings'] = $warnings;
     return $result;
 }
Пример #5
0
/**
 * Returns all activity in course workshops since a given time
 *
 * @param array $activities sequentially indexed array of objects
 * @param int $index
 * @param int $timestart
 * @param int $courseid
 * @param int $cmid
 * @param int $userid defaults to 0
 * @param int $groupid defaults to 0
 * @return void adds items into $activities and increases $index
 */
function workshop_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
    global $CFG, $COURSE, $USER, $DB;

    if ($COURSE->id == $courseid) {
        $course = $COURSE;
    } else {
        $course = $DB->get_record('course', array('id'=>$courseid));
    }

    $modinfo = get_fast_modinfo($course);

    $cm = $modinfo->cms[$cmid];

    $params = array();
    if ($userid) {
        $userselect = "AND (author.id = :authorid OR reviewer.id = :reviewerid)";
        $params['authorid'] = $userid;
        $params['reviewerid'] = $userid;
    } else {
        $userselect = "";
    }

    if ($groupid) {
        $groupselect = "AND (authorgroupmembership.groupid = :authorgroupid OR reviewergroupmembership.groupid = :reviewergroupid)";
        $groupjoin   = "LEFT JOIN {groups_members} authorgroupmembership ON authorgroupmembership.userid = author.id
                        LEFT JOIN {groups_members} reviewergroupmembership ON reviewergroupmembership.userid = reviewer.id";
        $params['authorgroupid'] = $groupid;
        $params['reviewergroupid'] = $groupid;
    } else {
        $groupselect = "";
        $groupjoin   = "";
    }

    $params['cminstance'] = $cm->instance;
    $params['submissionmodified'] = $timestart;
    $params['assessmentmodified'] = $timestart;

    $authornamefields = get_all_user_name_fields(true, 'author', null, 'author');
    $reviewerfields = get_all_user_name_fields(true, 'reviewer', null, 'reviewer');

    $sql = "SELECT s.id AS submissionid, s.title AS submissiontitle, s.timemodified AS submissionmodified,
                   author.id AS authorid, $authornamefields, author.picture AS authorpicture, author.imagealt AS authorimagealt,
                   author.email AS authoremail, a.id AS assessmentid, a.timemodified AS assessmentmodified,
                   reviewer.id AS reviewerid, $reviewerfields, reviewer.picture AS reviewerpicture,
                   reviewer.imagealt AS reviewerimagealt, reviewer.email AS revieweremail
              FROM {workshop_submissions} s
        INNER JOIN {workshop} w ON s.workshopid = w.id
        INNER JOIN {user} author ON s.authorid = author.id
         LEFT JOIN {workshop_assessments} a ON a.submissionid = s.id
         LEFT JOIN {user} reviewer ON a.reviewerid = reviewer.id
        $groupjoin
             WHERE w.id = :cminstance
                   AND s.example = 0
                   $userselect $groupselect
                   AND (s.timemodified > :submissionmodified OR a.timemodified > :assessmentmodified)
          ORDER BY s.timemodified ASC, a.timemodified ASC";

    $rs = $DB->get_recordset_sql($sql, $params);

    $groupmode       = groups_get_activity_groupmode($cm, $course);
    $context         = context_module::instance($cm->id);
    $grader          = has_capability('moodle/grade:viewall', $context);
    $accessallgroups = has_capability('moodle/site:accessallgroups', $context);
    $viewauthors     = has_capability('mod/workshop:viewauthornames', $context);
    $viewreviewers   = has_capability('mod/workshop:viewreviewernames', $context);

    $submissions = array(); // recent submissions indexed by submission id
    $assessments = array(); // recent assessments indexed by assessment id
    $users       = array();

    foreach ($rs as $activity) {

        // remember all user names we can use later
        if (empty($users[$activity->authorid])) {
            $u = new stdclass();
            $additionalfields = explode(',', user_picture::fields());
            $u = username_load_fields_from_object($u, $activity, 'author', $additionalfields);
            $users[$activity->authorid] = $u;
        }
        if ($activity->reviewerid and empty($users[$activity->reviewerid])) {
            $u = new stdclass();
            $additionalfields = explode(',', user_picture::fields());
            $u = username_load_fields_from_object($u, $activity, 'reviewer', $additionalfields);
            $users[$activity->reviewerid] = $u;
        }

        if ($activity->submissionmodified > $timestart and empty($submissions[$activity->submissionid])) {
            $s = new stdclass();
            $s->id = $activity->submissionid;
            $s->title = $activity->submissiontitle;
            $s->authorid = $activity->authorid;
            $s->timemodified = $activity->submissionmodified;
            if ($activity->authorid == $USER->id || has_capability('mod/workshop:viewauthornames', $context)) {
                $s->authornamevisible = true;
            } else {
                $s->authornamevisible = false;
            }

            // the following do-while wrapper allows to break from deeply nested if-statements
            do {
                if ($s->authorid === $USER->id) {
                    // own submissions always visible
                    $submissions[$activity->submissionid] = $s;
                    break;
                }

                if (has_capability('mod/workshop:viewallsubmissions', $context)) {
                    if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
                        if (isguestuser()) {
                            // shortcut - guest user does not belong into any group
                            break;
                        }

                        // this might be slow - show only submissions by users who share group with me in this cm
                        if (!$modinfo->get_groups($cm->groupingid)) {
                            break;
                        }
                        $authorsgroups = groups_get_all_groups($course->id, $s->authorid, $cm->groupingid);
                        if (is_array($authorsgroups)) {
                            $authorsgroups = array_keys($authorsgroups);
                            $intersect = array_intersect($authorsgroups, $modinfo->get_groups($cm->groupingid));
                            if (empty($intersect)) {
                                break;
                            } else {
                                // can see all submissions and shares a group with the author
                                $submissions[$activity->submissionid] = $s;
                                break;
                            }
                        }

                    } else {
                        // can see all submissions from all groups
                        $submissions[$activity->submissionid] = $s;
                    }
                }
            } while (0);
        }

        if ($activity->assessmentmodified > $timestart and empty($assessments[$activity->assessmentid])) {
            $a = new stdclass();
            $a->id = $activity->assessmentid;
            $a->submissionid = $activity->submissionid;
            $a->submissiontitle = $activity->submissiontitle;
            $a->reviewerid = $activity->reviewerid;
            $a->timemodified = $activity->assessmentmodified;
            if ($activity->reviewerid == $USER->id || has_capability('mod/workshop:viewreviewernames', $context)) {
                $a->reviewernamevisible = true;
            } else {
                $a->reviewernamevisible = false;
            }

            // the following do-while wrapper allows to break from deeply nested if-statements
            do {
                if ($a->reviewerid === $USER->id) {
                    // own assessments always visible
                    $assessments[$activity->assessmentid] = $a;
                    break;
                }

                if (has_capability('mod/workshop:viewallassessments', $context)) {
                    if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
                        if (isguestuser()) {
                            // shortcut - guest user does not belong into any group
                            break;
                        }

                        // this might be slow - show only submissions by users who share group with me in this cm
                        if (!$modinfo->get_groups($cm->groupingid)) {
                            break;
                        }
                        $reviewersgroups = groups_get_all_groups($course->id, $a->reviewerid, $cm->groupingid);
                        if (is_array($reviewersgroups)) {
                            $reviewersgroups = array_keys($reviewersgroups);
                            $intersect = array_intersect($reviewersgroups, $modinfo->get_groups($cm->groupingid));
                            if (empty($intersect)) {
                                break;
                            } else {
                                // can see all assessments and shares a group with the reviewer
                                $assessments[$activity->assessmentid] = $a;
                                break;
                            }
                        }

                    } else {
                        // can see all assessments from all groups
                        $assessments[$activity->assessmentid] = $a;
                    }
                }
            } while (0);
        }
    }
    $rs->close();

    $workshopname = format_string($cm->name, true);

    if ($grader) {
        require_once($CFG->libdir.'/gradelib.php');
        $grades = grade_get_grades($courseid, 'mod', 'workshop', $cm->instance, array_keys($users));
    }

    foreach ($submissions as $submission) {
        $tmpactivity                = new stdclass();
        $tmpactivity->type          = 'workshop';
        $tmpactivity->cmid          = $cm->id;
        $tmpactivity->name          = $workshopname;
        $tmpactivity->sectionnum    = $cm->sectionnum;
        $tmpactivity->timestamp     = $submission->timemodified;
        $tmpactivity->subtype       = 'submission';
        $tmpactivity->content       = $submission;
        if ($grader) {
            $tmpactivity->grade     = $grades->items[0]->grades[$submission->authorid]->str_long_grade;
        }
        if ($submission->authornamevisible and !empty($users[$submission->authorid])) {
            $tmpactivity->user      = $users[$submission->authorid];
        }
        $activities[$index++]       = $tmpactivity;
    }

    foreach ($assessments as $assessment) {
        $tmpactivity                = new stdclass();
        $tmpactivity->type          = 'workshop';
        $tmpactivity->cmid          = $cm->id;
        $tmpactivity->name          = $workshopname;
        $tmpactivity->sectionnum    = $cm->sectionnum;
        $tmpactivity->timestamp     = $assessment->timemodified;
        $tmpactivity->subtype       = 'assessment';
        $tmpactivity->content       = $assessment;
        if ($grader) {
            $tmpactivity->grade     = $grades->items[1]->grades[$assessment->reviewerid]->str_long_grade;
        }
        if ($assessment->reviewernamevisible and !empty($users[$assessment->reviewerid])) {
            $tmpactivity->user      = $users[$assessment->reviewerid];
        }
        $activities[$index++]       = $tmpactivity;
    }
}
Пример #6
0
 /**
  * Renders short summary of the submission
  *
  * @param workshop_submission_summary $summary
  * @return string text to be echo'ed
  */
 protected function render_workshop_submission_summary(workshop_submission_summary $summary)
 {
     $o = '';
     // output HTML code
     $anonymous = $summary->is_anonymous();
     $classes = 'submission-summary';
     if ($anonymous) {
         $classes .= ' anonymous';
     }
     $gradestatus = '';
     if ($summary->status == 'notgraded') {
         $classes .= ' notgraded';
         $gradestatus = $this->output->container(get_string('nogradeyet', 'workshop'), 'grade-status');
     } else {
         if ($summary->status == 'graded') {
             $classes .= ' graded';
             $gradestatus = $this->output->container(get_string('alreadygraded', 'workshop'), 'grade-status');
         }
     }
     $o .= $this->output->container_start($classes);
     // main wrapper
     $o .= html_writer::link($summary->url, format_string($summary->title), array('class' => 'title'));
     if (!$anonymous) {
         $author = new stdClass();
         $additionalfields = explode(',', user_picture::fields());
         $author = username_load_fields_from_object($author, $summary, 'author', $additionalfields);
         $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
         $userurl = new moodle_url('/user/view.php', array('id' => $author->id, 'course' => $this->page->course->id));
         $a = new stdClass();
         $a->name = fullname($author);
         $a->url = $userurl->out();
         $byfullname = get_string('byfullname', 'workshop', $a);
         $oo = $this->output->container($userpic, 'picture');
         $oo .= $this->output->container($byfullname, 'fullname');
         $o .= $this->output->container($oo, 'author');
     }
     $created = get_string('userdatecreated', 'workshop', userdate($summary->timecreated));
     $o .= $this->output->container($created, 'userdate created');
     if ($summary->timemodified > $summary->timecreated) {
         $modified = get_string('userdatemodified', 'workshop', userdate($summary->timemodified));
         $o .= $this->output->container($modified, 'userdate modified');
     }
     $o .= $gradestatus;
     $o .= $this->output->container_end();
     // end of the main wrapper
     return $o;
 }
Пример #7
0
 /**
  * displays the full report
  * @param \stdClass $scorm full SCORM object
  * @param \stdClass $cm - full course_module object
  * @param \stdClass $course - full course object
  * @param string $download - type of download being requested
  */
 public function display($scorm, $cm, $course, $download)
 {
     global $CFG, $DB, $OUTPUT, $PAGE;
     $contextmodule = \context_module::instance($cm->id);
     $action = optional_param('action', '', PARAM_ALPHA);
     $attemptids = optional_param_array('attemptid', array(), PARAM_RAW);
     $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_ALL_STUDENTS, PARAM_INT);
     $PAGE->set_url(new \moodle_url($PAGE->url, array('attemptsmode' => $attemptsmode)));
     if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) {
         if (scorm_delete_responses($attemptids, $scorm)) {
             // Delete responses.
             echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
         }
     }
     // Find out current groups mode.
     $currentgroup = groups_get_activity_group($cm, true);
     // Detailed report.
     $mform = new \mod_scorm_report_objectives_settings($PAGE->url, compact('currentgroup'));
     if ($fromform = $mform->get_data()) {
         $pagesize = $fromform->pagesize;
         $showobjectivescore = $fromform->objectivescore;
         set_user_preference('scorm_report_pagesize', $pagesize);
         set_user_preference('scorm_report_objectives_score', $showobjectivescore);
     } else {
         $pagesize = get_user_preferences('scorm_report_pagesize', 0);
         $showobjectivescore = get_user_preferences('scorm_report_objectives_score', 0);
     }
     if ($pagesize < 1) {
         $pagesize = SCORM_REPORT_DEFAULT_PAGE_SIZE;
     }
     // Select group menu.
     $displayoptions = array();
     $displayoptions['attemptsmode'] = $attemptsmode;
     $displayoptions['objectivescore'] = $showobjectivescore;
     $mform->set_data($displayoptions + array('pagesize' => $pagesize));
     if ($groupmode = groups_get_activity_groupmode($cm)) {
         // Groups are being used.
         if (!$download) {
             groups_print_activity_menu($cm, new \moodle_url($PAGE->url, $displayoptions));
         }
     }
     $formattextoptions = array('context' => \context_course::instance($course->id));
     // We only want to show the checkbox to delete attempts
     // if the user has permissions and if the report mode is showing attempts.
     $candelete = has_capability('mod/scorm:deleteresponses', $contextmodule) && $attemptsmode != SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO;
     // Select the students.
     $nostudents = false;
     if (empty($currentgroup)) {
         // All users who can attempt scoes.
         if (!($students = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', 'u.id', '', '', '', '', '', false))) {
             echo $OUTPUT->notification(get_string('nostudentsyet'));
             $nostudents = true;
             $allowedlist = '';
         } else {
             $allowedlist = array_keys($students);
         }
         unset($students);
     } else {
         // All users who can attempt scoes and who are in the currently selected group.
         $groupstudents = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', 'u.id', '', '', '', $currentgroup, '', false);
         if (!$groupstudents) {
             echo $OUTPUT->notification(get_string('nostudentsingroup'));
             $nostudents = true;
             $groupstudents = array();
         }
         $allowedlist = array_keys($groupstudents);
         unset($groupstudents);
     }
     if (!$nostudents) {
         // Now check if asked download of data.
         $coursecontext = \context_course::instance($course->id);
         if ($download) {
             $filename = clean_filename("{$course->shortname} " . format_string($scorm->name, true, $formattextoptions));
         }
         // Define table columns.
         $columns = array();
         $headers = array();
         if (!$download && $candelete) {
             $columns[] = 'checkbox';
             $headers[] = null;
         }
         if (!$download && $CFG->grade_report_showuserimage) {
             $columns[] = 'picture';
             $headers[] = '';
         }
         $columns[] = 'fullname';
         $headers[] = get_string('name');
         $extrafields = get_extra_user_fields($coursecontext);
         foreach ($extrafields as $field) {
             $columns[] = $field;
             $headers[] = get_user_field_name($field);
         }
         $columns[] = 'attempt';
         $headers[] = get_string('attempt', 'scorm');
         $columns[] = 'start';
         $headers[] = get_string('started', 'scorm');
         $columns[] = 'finish';
         $headers[] = get_string('last', 'scorm');
         $columns[] = 'score';
         $headers[] = get_string('score', 'scorm');
         $scoes = $DB->get_records('scorm_scoes', array("scorm" => $scorm->id), 'sortorder, id');
         foreach ($scoes as $sco) {
             if ($sco->launch != '') {
                 $columns[] = 'scograde' . $sco->id;
                 $headers[] = format_string($sco->title, '', $formattextoptions);
             }
         }
         $params = array();
         list($usql, $params) = $DB->get_in_or_equal($allowedlist, SQL_PARAMS_NAMED);
         // Construct the SQL.
         $select = 'SELECT DISTINCT ' . $DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)') . ' AS uniqueid, ';
         $select .= 'st.scormid AS scormid, st.attempt AS attempt, ' . \user_picture::fields('u', array('idnumber'), 'userid') . get_extra_user_fields_sql($coursecontext, 'u', '', array('email', 'idnumber')) . ' ';
         // This part is the same for all cases - join users and scorm_scoes_track tables.
         $from = 'FROM {user} u ';
         $from .= 'LEFT JOIN {scorm_scoes_track} st ON st.userid = u.id AND st.scormid = ' . $scorm->id;
         switch ($attemptsmode) {
             case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH:
                 // Show only students with attempts.
                 $where = ' WHERE u.id ' . $usql . ' AND st.userid IS NOT NULL';
                 break;
             case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
                 // Show only students without attempts.
                 $where = ' WHERE u.id ' . $usql . ' AND st.userid IS NULL';
                 break;
             case SCORM_REPORT_ATTEMPTS_ALL_STUDENTS:
                 // Show all students with or without attempts.
                 $where = ' WHERE u.id ' . $usql . ' AND (st.userid IS NOT NULL OR st.userid IS NULL)';
                 break;
         }
         $countsql = 'SELECT COUNT(DISTINCT(' . $DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)') . ')) AS nbresults, ';
         $countsql .= 'COUNT(DISTINCT(' . $DB->sql_concat('u.id', '\'#\'', 'st.attempt') . ')) AS nbattempts, ';
         $countsql .= 'COUNT(DISTINCT(u.id)) AS nbusers ';
         $countsql .= $from . $where;
         $nbmaincolumns = count($columns);
         // Get number of main columns used.
         $objectives = get_scorm_objectives($scorm->id);
         $nosort = array();
         foreach ($objectives as $scoid => $sco) {
             foreach ($sco as $id => $objectivename) {
                 $colid = $scoid . 'objectivestatus' . $id;
                 $columns[] = $colid;
                 $nosort[] = $colid;
                 if (!$displayoptions['objectivescore']) {
                     // Display the objective name only.
                     $headers[] = $objectivename;
                 } else {
                     // Display the objective status header with a "status" suffix to avoid confusion.
                     $headers[] = $objectivename . ' ' . get_string('status', 'scormreport_objectives');
                     // Now print objective score headers.
                     $colid = $scoid . 'objectivescore' . $id;
                     $columns[] = $colid;
                     $nosort[] = $colid;
                     $headers[] = $objectivename . ' ' . get_string('score', 'scormreport_objectives');
                 }
             }
         }
         $emptycell = '';
         // Used when an empty cell is being printed - in html we add a space.
         if (!$download) {
             $emptycell = '&nbsp;';
             $table = new \flexible_table('mod-scorm-report');
             $table->define_columns($columns);
             $table->define_headers($headers);
             $table->define_baseurl($PAGE->url);
             $table->sortable(true);
             $table->collapsible(true);
             // This is done to prevent redundant data, when a user has multiple attempts.
             $table->column_suppress('picture');
             $table->column_suppress('fullname');
             foreach ($extrafields as $field) {
                 $table->column_suppress($field);
             }
             foreach ($nosort as $field) {
                 $table->no_sorting($field);
             }
             $table->no_sorting('start');
             $table->no_sorting('finish');
             $table->no_sorting('score');
             $table->no_sorting('checkbox');
             $table->no_sorting('picture');
             foreach ($scoes as $sco) {
                 if ($sco->launch != '') {
                     $table->no_sorting('scograde' . $sco->id);
                 }
             }
             $table->column_class('picture', 'picture');
             $table->column_class('fullname', 'bold');
             $table->column_class('score', 'bold');
             $table->set_attribute('cellspacing', '0');
             $table->set_attribute('id', 'attempts');
             $table->set_attribute('class', 'generaltable generalbox');
             // Start working -- this is necessary as soon as the niceties are over.
             $table->setup();
         } else {
             if ($download == 'ODS') {
                 require_once "{$CFG->libdir}/odslib.class.php";
                 $filename .= ".ods";
                 // Creating a workbook.
                 $workbook = new \MoodleODSWorkbook("-");
                 // Sending HTTP headers.
                 $workbook->send($filename);
                 // Creating the first worksheet.
                 $sheettitle = get_string('report', 'scorm');
                 $myxls = $workbook->add_worksheet($sheettitle);
                 // Format types.
                 $format = $workbook->add_format();
                 $format->set_bold(0);
                 $formatbc = $workbook->add_format();
                 $formatbc->set_bold(1);
                 $formatbc->set_align('center');
                 $formatb = $workbook->add_format();
                 $formatb->set_bold(1);
                 $formaty = $workbook->add_format();
                 $formaty->set_bg_color('yellow');
                 $formatc = $workbook->add_format();
                 $formatc->set_align('center');
                 $formatr = $workbook->add_format();
                 $formatr->set_bold(1);
                 $formatr->set_color('red');
                 $formatr->set_align('center');
                 $formatg = $workbook->add_format();
                 $formatg->set_bold(1);
                 $formatg->set_color('green');
                 $formatg->set_align('center');
                 // Here starts workshhet headers.
                 $colnum = 0;
                 foreach ($headers as $item) {
                     $myxls->write(0, $colnum, $item, $formatbc);
                     $colnum++;
                 }
                 $rownum = 1;
             } else {
                 if ($download == 'Excel') {
                     require_once "{$CFG->libdir}/excellib.class.php";
                     $filename .= ".xls";
                     // Creating a workbook.
                     $workbook = new \MoodleExcelWorkbook("-");
                     // Sending HTTP headers.
                     $workbook->send($filename);
                     // Creating the first worksheet.
                     $sheettitle = get_string('report', 'scorm');
                     $myxls = $workbook->add_worksheet($sheettitle);
                     // Format types.
                     $format = $workbook->add_format();
                     $format->set_bold(0);
                     $formatbc = $workbook->add_format();
                     $formatbc->set_bold(1);
                     $formatbc->set_align('center');
                     $formatb = $workbook->add_format();
                     $formatb->set_bold(1);
                     $formaty = $workbook->add_format();
                     $formaty->set_bg_color('yellow');
                     $formatc = $workbook->add_format();
                     $formatc->set_align('center');
                     $formatr = $workbook->add_format();
                     $formatr->set_bold(1);
                     $formatr->set_color('red');
                     $formatr->set_align('center');
                     $formatg = $workbook->add_format();
                     $formatg->set_bold(1);
                     $formatg->set_color('green');
                     $formatg->set_align('center');
                     $colnum = 0;
                     foreach ($headers as $item) {
                         $myxls->write(0, $colnum, $item, $formatbc);
                         $colnum++;
                     }
                     $rownum = 1;
                 } else {
                     if ($download == 'CSV') {
                         $csvexport = new \csv_export_writer("tab");
                         $csvexport->set_filename($filename, ".txt");
                         $csvexport->add_data($headers);
                     }
                 }
             }
         }
         if (!$download) {
             $sort = $table->get_sql_sort();
         } else {
             $sort = '';
         }
         // Fix some wired sorting.
         if (empty($sort)) {
             $sort = ' ORDER BY uniqueid';
         } else {
             $sort = ' ORDER BY ' . $sort;
         }
         if (!$download) {
             // Add extra limits due to initials bar.
             list($twhere, $tparams) = $table->get_sql_where();
             if ($twhere) {
                 $where .= ' AND ' . $twhere;
                 // Initial bar.
                 $params = array_merge($params, $tparams);
             }
             if (!empty($countsql)) {
                 $count = $DB->get_record_sql($countsql, $params);
                 $totalinitials = $count->nbresults;
                 if ($twhere) {
                     $countsql .= ' AND ' . $twhere;
                 }
                 $count = $DB->get_record_sql($countsql, $params);
                 $total = $count->nbresults;
             }
             $table->pagesize($pagesize, $total);
             echo \html_writer::start_div('scormattemptcounts');
             if ($count->nbresults == $count->nbattempts) {
                 echo get_string('reportcountattempts', 'scorm', $count);
             } else {
                 if ($count->nbattempts > 0) {
                     echo get_string('reportcountallattempts', 'scorm', $count);
                 } else {
                     echo $count->nbusers . ' ' . get_string('users');
                 }
             }
             echo \html_writer::end_div();
         }
         // Fetch the attempts.
         if (!$download) {
             $attempts = $DB->get_records_sql($select . $from . $where . $sort, $params, $table->get_page_start(), $table->get_page_size());
             echo \html_writer::start_div('', array('id' => 'scormtablecontainer'));
             if ($candelete) {
                 // Start form.
                 $strreallydel = addslashes_js(get_string('deleteattemptcheck', 'scorm'));
                 echo \html_writer::start_tag('form', array('id' => 'attemptsform', 'method' => 'post', 'action' => $PAGE->url->out(false), 'onsubmit' => 'return confirm("' . $strreallydel . '");'));
                 echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'delete'));
                 echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
                 echo \html_writer::start_div('', array('style' => 'display: none;'));
                 echo \html_writer::input_hidden_params($PAGE->url);
                 echo \html_writer::end_div();
                 echo \html_writer::start_div();
             }
             $table->initialbars($totalinitials > 20);
             // Build table rows.
         } else {
             $attempts = $DB->get_records_sql($select . $from . $where . $sort, $params);
         }
         if ($attempts) {
             foreach ($attempts as $scouser) {
                 $row = array();
                 if (!empty($scouser->attempt)) {
                     $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
                 } else {
                     $timetracks = '';
                 }
                 if (in_array('checkbox', $columns)) {
                     if ($candelete && !empty($timetracks->start)) {
                         $row[] = \html_writer::checkbox('attemptid[]', $scouser->userid . ':' . $scouser->attempt, false);
                     } else {
                         if ($candelete) {
                             $row[] = '';
                         }
                     }
                 }
                 if (in_array('picture', $columns)) {
                     $user = new \stdClass();
                     $additionalfields = explode(',', \user_picture::fields());
                     $user = username_load_fields_from_object($user, $scouser, null, $additionalfields);
                     $user->id = $scouser->userid;
                     $row[] = $OUTPUT->user_picture($user, array('courseid' => $course->id));
                 }
                 if (!$download) {
                     $url = new \moodle_url('/user/view.php', array('id' => $scouser->userid, 'course' => $course->id));
                     $row[] = \html_writer::link($url, fullname($scouser));
                 } else {
                     $row[] = fullname($scouser);
                 }
                 foreach ($extrafields as $field) {
                     $row[] = s($scouser->{$field});
                 }
                 if (empty($timetracks->start)) {
                     $row[] = '-';
                     $row[] = '-';
                     $row[] = '-';
                     $row[] = '-';
                 } else {
                     if (!$download) {
                         $url = new \moodle_url('/mod/scorm/report/userreport.php', array('id' => $cm->id, 'user' => $scouser->userid, 'attempt' => $scouser->attempt));
                         $row[] = \html_writer::link($url, $scouser->attempt);
                     } else {
                         $row[] = $scouser->attempt;
                     }
                     if ($download == 'ODS' || $download == 'Excel') {
                         $row[] = userdate($timetracks->start, get_string("strftimedatetime", "langconfig"));
                     } else {
                         $row[] = userdate($timetracks->start);
                     }
                     if ($download == 'ODS' || $download == 'Excel') {
                         $row[] = userdate($timetracks->finish, get_string('strftimedatetime', 'langconfig'));
                     } else {
                         $row[] = userdate($timetracks->finish);
                     }
                     $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $scouser->attempt);
                 }
                 // Print out all scores of attempt.
                 foreach ($scoes as $sco) {
                     if ($sco->launch != '') {
                         if ($trackdata = scorm_get_tracks($sco->id, $scouser->userid, $scouser->attempt)) {
                             if ($trackdata->status == '') {
                                 $trackdata->status = 'notattempted';
                             }
                             $strstatus = get_string($trackdata->status, 'scorm');
                             if ($trackdata->score_raw != '') {
                                 // If raw score exists, print it.
                                 $score = $trackdata->score_raw;
                                 // Add max score if it exists.
                                 if (isset($trackdata->score_max)) {
                                     $score .= '/' . $trackdata->score_max;
                                 }
                             } else {
                                 // ...else print out status.
                                 $score = $strstatus;
                             }
                             if (!$download) {
                                 $url = new \moodle_url('/mod/scorm/report/userreporttracks.php', array('id' => $cm->id, 'scoid' => $sco->id, 'user' => $scouser->userid, 'attempt' => $scouser->attempt));
                                 $row[] = \html_writer::img($OUTPUT->pix_url($trackdata->status, 'scorm'), $strstatus, array('title' => $strstatus)) . \html_writer::empty_tag('br') . \html_writer::link($url, $score, array('title' => get_string('details', 'scorm')));
                             } else {
                                 $row[] = $score;
                             }
                             // Iterate over tracks and match objective id against values.
                             $scorm2004 = false;
                             if (scorm_version_check($scorm->version, SCORM_13)) {
                                 $scorm2004 = true;
                                 $objectiveprefix = "cmi.objectives.";
                             } else {
                                 $objectiveprefix = "cmi.objectives_";
                             }
                             $keywords = array(".id", $objectiveprefix);
                             $objectivestatus = array();
                             $objectivescore = array();
                             foreach ($trackdata as $name => $value) {
                                 if (strpos($name, $objectiveprefix) === 0 && strrpos($name, '.id') !== false) {
                                     $num = trim(str_ireplace($keywords, '', $name));
                                     if (is_numeric($num)) {
                                         if ($scorm2004) {
                                             $element = $objectiveprefix . $num . '.completion_status';
                                         } else {
                                             $element = $objectiveprefix . $num . '.status';
                                         }
                                         if (isset($trackdata->{$element})) {
                                             $objectivestatus[$value] = $trackdata->{$element};
                                         } else {
                                             $objectivestatus[$value] = '';
                                         }
                                         if ($displayoptions['objectivescore']) {
                                             $element = $objectiveprefix . $num . '.score.raw';
                                             if (isset($trackdata->{$element})) {
                                                 $objectivescore[$value] = $trackdata->{$element};
                                             } else {
                                                 $objectivescore[$value] = '';
                                             }
                                         }
                                     }
                                 }
                             }
                             // Interaction data.
                             if (!empty($objectives[$trackdata->scoid])) {
                                 foreach ($objectives[$trackdata->scoid] as $name) {
                                     if (isset($objectivestatus[$name])) {
                                         $row[] = s($objectivestatus[$name]);
                                     } else {
                                         $row[] = $emptycell;
                                     }
                                     if ($displayoptions['objectivescore']) {
                                         if (isset($objectivescore[$name])) {
                                             $row[] = s($objectivescore[$name]);
                                         } else {
                                             $row[] = $emptycell;
                                         }
                                     }
                                 }
                             }
                             // End of interaction data.
                         } else {
                             // If we don't have track data, we haven't attempted yet.
                             $strstatus = get_string('notattempted', 'scorm');
                             if (!$download) {
                                 $row[] = \html_writer::img($OUTPUT->pix_url('notattempted', 'scorm'), $strstatus, array('title' => $strstatus)) . \html_writer::empty_tag('br') . $strstatus;
                             } else {
                                 $row[] = $strstatus;
                             }
                             // Complete the empty cells.
                             for ($i = 0; $i < count($columns) - $nbmaincolumns; $i++) {
                                 $row[] = $emptycell;
                             }
                         }
                     }
                 }
                 if (!$download) {
                     $table->add_data($row);
                 } else {
                     if ($download == 'Excel' or $download == 'ODS') {
                         $colnum = 0;
                         foreach ($row as $item) {
                             $myxls->write($rownum, $colnum, $item, $format);
                             $colnum++;
                         }
                         $rownum++;
                     } else {
                         if ($download == 'CSV') {
                             $csvexport->add_data($row);
                         }
                     }
                 }
             }
             if (!$download) {
                 $table->finish_output();
                 if ($candelete) {
                     echo \html_writer::start_tag('table', array('id' => 'commands'));
                     echo \html_writer::start_tag('tr') . \html_writer::start_tag('td');
                     echo \html_writer::link('javascript:select_all_in(\'DIV\', null, \'scormtablecontainer\');', get_string('selectall', 'scorm')) . ' / ';
                     echo \html_writer::link('javascript:deselect_all_in(\'DIV\', null, \'scormtablecontainer\');', get_string('selectnone', 'scorm'));
                     echo '&nbsp;&nbsp;';
                     echo \html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('deleteselected', 'scorm'), 'class' => 'btn btn-secondary'));
                     echo \html_writer::end_tag('td') . \html_writer::end_tag('tr') . \html_writer::end_tag('table');
                     // Close form.
                     echo \html_writer::end_tag('div');
                     echo \html_writer::end_tag('form');
                 }
                 echo \html_writer::end_div();
                 if (!empty($attempts)) {
                     echo \html_writer::start_tag('table', array('class' => 'boxaligncenter')) . \html_writer::start_tag('tr');
                     echo \html_writer::start_tag('td');
                     echo $OUTPUT->single_button(new \moodle_url($PAGE->url, array('download' => 'ODS') + $displayoptions), get_string('downloadods'), 'post', ['class' => 'm-t-1']);
                     echo \html_writer::end_tag('td');
                     echo \html_writer::start_tag('td');
                     echo $OUTPUT->single_button(new \moodle_url($PAGE->url, array('download' => 'Excel') + $displayoptions), get_string('downloadexcel'), 'post', ['class' => 'm-t-1']);
                     echo \html_writer::end_tag('td');
                     echo \html_writer::start_tag('td');
                     echo $OUTPUT->single_button(new \moodle_url($PAGE->url, array('download' => 'CSV') + $displayoptions), get_string('downloadtext'), 'post', ['class' => 'm-t-1']);
                     echo \html_writer::end_tag('td');
                     echo \html_writer::start_tag('td');
                     echo \html_writer::end_tag('td');
                     echo \html_writer::end_tag('tr') . \html_writer::end_tag('table');
                 }
             }
         } else {
             if ($candelete && !$download) {
                 echo \html_writer::end_div();
                 echo \html_writer::end_tag('form');
                 $table->finish_output();
             }
             echo \html_writer::end_div();
         }
         // Show preferences form irrespective of attempts are there to report or not.
         if (!$download) {
             $mform->set_data(compact('detailedrep', 'pagesize', 'attemptsmode'));
             $mform->display();
         }
         if ($download == 'Excel' or $download == 'ODS') {
             $workbook->close();
             exit;
         } else {
             if ($download == 'CSV') {
                 $csvexport->download_file();
                 exit;
             }
         }
     } else {
         echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
     }
 }
Пример #8
0
 /**
  * Renders short summary of the submission
  *
  * @param teamwork_discussion_summary $summary
  * @return string text to be echo'ed
  */
 protected function render_teamwork_discussion_summary(teamwork_discussion_summary $summary)
 {
     global $USER, $DB, $PAGE;
     $o = '';
     // output HTML code
     $anonymous = $summary->is_anonymous();
     $classes = 'discussion-summary';
     if ($anonymous) {
         $classes .= ' anonymous';
     }
     $gradestatus = '';
     if ($summary->status == 'notgraded') {
         $classes .= ' notgraded';
         $gradestatus = $this->output->container(get_string('nogradeyet', 'teamwork'), 'grade-status');
     } else {
         if ($summary->status == 'graded') {
             $classes .= ' graded';
             $gradestatus = $this->output->container(get_string('alreadygraded', 'teamwork'), 'grade-status');
         }
     }
     $o .= $this->output->container_start($classes);
     // main wrapper
     $grade = get_string('fen', 'teamwork', $summary->score);
     $o .= html_writer::link($summary->url, format_string($grade), array('class' => 'grade'));
     if (!$anonymous) {
         $author = new stdClass();
         // change in course_server.
         $any_student = $DB->get_record('user', array('username' => 'any_student'));
         $additionalfields = explode(',', user_picture::fields());
         if ($USER->id == $summary->authorid || has_capability('mod/teamwork:editsettings', $PAGE->context)) {
             $author = username_load_fields_from_object($author, $summary, 'author', $additionalfields);
         } else {
             $author = username_load_fields_from_object($author, $any_student, null, $additionalfields);
         }
         $userpic = $this->output->user_picture($author, array('courseid' => $this->page->course->id, 'size' => 35));
         $userurl = new moodle_url('/user/view.php', array('id' => $author->id, 'course' => $this->page->course->id));
         $a = new stdClass();
         $a->name = fullname($author);
         $a->url = $userurl->out();
         $byfullname = get_string('postbyfullname', 'teamwork', $a);
         $oo = $this->output->container($userpic, 'picture');
         $oo .= $this->output->container($byfullname, 'fullname');
         $o .= $this->output->container($oo, 'author');
     }
     $o .= html_writer::link($summary->url, format_string($summary->message), array('class' => 'description'));
     if ($summary->timemodified > $summary->timecreated) {
         $modified = get_string('userdatemodified', 'teamwork', userdate($summary->timemodified));
         $o .= $this->output->container($modified, 'userdate modified');
     }
     $o .= $gradestatus;
     $o .= $this->output->container_end();
     // end of the main wrapper
     return $o;
 }
 /**
  * Test function username_load_fields_from_object().
  */
 public function test_username_load_fields_from_object()
 {
     $this->resetAfterTest();
     // This object represents the information returned from an sql query.
     $userinfo = new stdClass();
     $userinfo->userid = 1;
     $userinfo->username = '******';
     $userinfo->firstname = 'Bruce';
     $userinfo->lastname = 'Campbell';
     $userinfo->firstnamephonetic = 'ブルース';
     $userinfo->lastnamephonetic = 'カンベッル';
     $userinfo->middlename = '';
     $userinfo->alternatename = '';
     $userinfo->email = '';
     $userinfo->picture = 23;
     $userinfo->imagealt = 'Michael Jordan draining another basket.';
     $userinfo->idnumber = 3982;
     // Just user name fields.
     $user = new stdClass();
     $user = username_load_fields_from_object($user, $userinfo);
     $expectedarray = new stdClass();
     $expectedarray->firstname = 'Bruce';
     $expectedarray->lastname = 'Campbell';
     $expectedarray->firstnamephonetic = 'ブルース';
     $expectedarray->lastnamephonetic = 'カンベッル';
     $expectedarray->middlename = '';
     $expectedarray->alternatename = '';
     $this->assertEquals($user, $expectedarray);
     // User information for showing a picture.
     $user = new stdClass();
     $additionalfields = explode(',', user_picture::fields());
     $user = username_load_fields_from_object($user, $userinfo, null, $additionalfields);
     $user->id = $userinfo->userid;
     $expectedarray = new stdClass();
     $expectedarray->id = 1;
     $expectedarray->firstname = 'Bruce';
     $expectedarray->lastname = 'Campbell';
     $expectedarray->firstnamephonetic = 'ブルース';
     $expectedarray->lastnamephonetic = 'カンベッル';
     $expectedarray->middlename = '';
     $expectedarray->alternatename = '';
     $expectedarray->email = '';
     $expectedarray->picture = 23;
     $expectedarray->imagealt = 'Michael Jordan draining another basket.';
     $this->assertEquals($user, $expectedarray);
     // Alter the userinfo object to have a prefix.
     $userinfo->authorfirstname = 'Bruce';
     $userinfo->authorlastname = 'Campbell';
     $userinfo->authorfirstnamephonetic = 'ブルース';
     $userinfo->authorlastnamephonetic = 'カンベッル';
     $userinfo->authormiddlename = '';
     $userinfo->authorpicture = 23;
     $userinfo->authorimagealt = 'Michael Jordan draining another basket.';
     $userinfo->authoremail = '*****@*****.**';
     // Return an object with user picture information.
     $user = new stdClass();
     $additionalfields = explode(',', user_picture::fields());
     $user = username_load_fields_from_object($user, $userinfo, 'author', $additionalfields);
     $user->id = $userinfo->userid;
     $expectedarray = new stdClass();
     $expectedarray->id = 1;
     $expectedarray->firstname = 'Bruce';
     $expectedarray->lastname = 'Campbell';
     $expectedarray->firstnamephonetic = 'ブルース';
     $expectedarray->lastnamephonetic = 'カンベッル';
     $expectedarray->middlename = '';
     $expectedarray->alternatename = '';
     $expectedarray->email = '*****@*****.**';
     $expectedarray->picture = 23;
     $expectedarray->imagealt = 'Michael Jordan draining another basket.';
     $this->assertEquals($user, $expectedarray);
 }
 /**
  * Generate the display of the user's picture column.
  * @param object $attempt the table row being output.
  * @return string HTML content to go inside the td.
  */
 public function col_picture($attempt)
 {
     global $OUTPUT;
     $user = new stdClass();
     $additionalfields = explode(',', user_picture::fields());
     $user = username_load_fields_from_object($user, $attempt, null, $additionalfields);
     $user->id = $attempt->userid;
     return $OUTPUT->user_picture($user);
 }
Пример #11
0
 /**
  * displays the full report
  * @param stdClass $scorm full SCORM object
  * @param stdClass $cm - full course_module object
  * @param stdClass $course - full course object
  * @param string $download - type of download being requested
  */
 function display($scorm, $cm, $course, $download)
 {
     global $CFG, $DB, $OUTPUT, $PAGE;
     $contextmodule = context_module::instance($cm->id);
     $action = optional_param('action', '', PARAM_ALPHA);
     $attemptids = optional_param_array('attemptid', array(), PARAM_RAW);
     $attemptsmode = optional_param('attemptsmode', SCORM_REPORT_ATTEMPTS_ALL_STUDENTS, PARAM_INT);
     $PAGE->set_url(new moodle_url($PAGE->url, array('attemptsmode' => $attemptsmode)));
     if ($action == 'delete' && has_capability('mod/scorm:deleteresponses', $contextmodule) && confirm_sesskey()) {
         if (scorm_delete_responses($attemptids, $scorm)) {
             //delete responses.
             add_to_log($course->id, 'scorm', 'delete attempts', 'report.php?id=' . $cm->id, implode(",", $attemptids), $cm->id);
             echo $OUTPUT->notification(get_string('scormresponsedeleted', 'scorm'), 'notifysuccess');
         }
     }
     // find out current groups mode
     $currentgroup = groups_get_activity_group($cm, true);
     // detailed report
     $mform = new mod_scorm_report_settings($PAGE->url, compact('currentgroup'));
     if ($fromform = $mform->get_data()) {
         $detailedrep = $fromform->detailedrep;
         $pagesize = $fromform->pagesize;
         set_user_preference('scorm_report_detailed', $detailedrep);
         set_user_preference('scorm_report_pagesize', $pagesize);
     } else {
         $detailedrep = get_user_preferences('scorm_report_detailed', false);
         $pagesize = get_user_preferences('scorm_report_pagesize', 0);
     }
     if ($pagesize < 1) {
         $pagesize = SCORM_REPORT_DEFAULT_PAGE_SIZE;
     }
     // select group menu
     $displayoptions = array();
     $displayoptions['attemptsmode'] = $attemptsmode;
     if ($groupmode = groups_get_activity_groupmode($cm)) {
         // Groups are being used
         if (!$download) {
             groups_print_activity_menu($cm, new moodle_url($PAGE->url, $displayoptions));
         }
     }
     // We only want to show the checkbox to delete attempts
     // if the user has permissions and if the report mode is showing attempts.
     $candelete = has_capability('mod/scorm:deleteresponses', $contextmodule) && $attemptsmode != SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO;
     // select the students
     $nostudents = false;
     if (empty($currentgroup)) {
         // all users who can attempt scoes
         if (!($students = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', 'u.id', '', '', '', '', '', false))) {
             echo $OUTPUT->notification(get_string('nostudentsyet'));
             $nostudents = true;
             $allowedlist = '';
         } else {
             $allowedlist = array_keys($students);
         }
         unset($students);
     } else {
         // all users who can attempt scoes and who are in the currently selected group
         if (!($groupstudents = get_users_by_capability($contextmodule, 'mod/scorm:savetrack', 'u.id', '', '', '', $currentgroup, '', false))) {
             echo $OUTPUT->notification(get_string('nostudentsingroup'));
             $nostudents = true;
             $groupstudents = array();
         }
         $allowedlist = array_keys($groupstudents);
         unset($groupstudents);
     }
     if (!$nostudents) {
         // Now check if asked download of data
         $coursecontext = context_course::instance($course->id);
         if ($download) {
             $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
             $filename = clean_filename("{$shortname} " . format_string($scorm->name, true));
         }
         // Define table columns
         $columns = array();
         $headers = array();
         if (!$download && $candelete) {
             $columns[] = 'checkbox';
             $headers[] = null;
         }
         if (!$download && $CFG->grade_report_showuserimage) {
             $columns[] = 'picture';
             $headers[] = '';
         }
         $columns[] = 'fullname';
         $headers[] = get_string('name');
         $extrafields = get_extra_user_fields($coursecontext);
         foreach ($extrafields as $field) {
             $columns[] = $field;
             $headers[] = get_user_field_name($field);
         }
         $columns[] = 'attempt';
         $headers[] = get_string('attempt', 'scorm');
         $columns[] = 'start';
         $headers[] = get_string('started', 'scorm');
         $columns[] = 'finish';
         $headers[] = get_string('last', 'scorm');
         $columns[] = 'score';
         $headers[] = get_string('score', 'scorm');
         if ($detailedrep && ($scoes = $DB->get_records('scorm_scoes', array("scorm" => $scorm->id), 'sortorder, id'))) {
             foreach ($scoes as $sco) {
                 if ($sco->launch != '') {
                     $columns[] = 'scograde' . $sco->id;
                     $headers[] = format_string($sco->title);
                 }
             }
         } else {
             $scoes = null;
         }
         if (!$download) {
             $table = new flexible_table('mod-scorm-report');
             $table->define_columns($columns);
             $table->define_headers($headers);
             $table->define_baseurl($PAGE->url);
             $table->sortable(true);
             $table->collapsible(true);
             // This is done to prevent redundant data, when a user has multiple attempts
             $table->column_suppress('picture');
             $table->column_suppress('fullname');
             foreach ($extrafields as $field) {
                 $table->column_suppress($field);
             }
             $table->no_sorting('start');
             $table->no_sorting('finish');
             $table->no_sorting('score');
             if ($scoes) {
                 foreach ($scoes as $sco) {
                     if ($sco->launch != '') {
                         $table->no_sorting('scograde' . $sco->id);
                     }
                 }
             }
             $table->column_class('picture', 'picture');
             $table->column_class('fullname', 'bold');
             $table->column_class('score', 'bold');
             $table->set_attribute('cellspacing', '0');
             $table->set_attribute('id', 'attempts');
             $table->set_attribute('class', 'generaltable generalbox');
             // Start working -- this is necessary as soon as the niceties are over
             $table->setup();
         } else {
             if ($download == 'ODS') {
                 require_once "{$CFG->libdir}/odslib.class.php";
                 $filename .= ".ods";
                 // Creating a workbook
                 $workbook = new MoodleODSWorkbook("-");
                 // Sending HTTP headers
                 $workbook->send($filename);
                 // Creating the first worksheet
                 $sheettitle = get_string('report', 'scorm');
                 $myxls = $workbook->add_worksheet($sheettitle);
                 // format types
                 $format = $workbook->add_format();
                 $format->set_bold(0);
                 $formatbc = $workbook->add_format();
                 $formatbc->set_bold(1);
                 $formatbc->set_align('center');
                 $formatb = $workbook->add_format();
                 $formatb->set_bold(1);
                 $formaty = $workbook->add_format();
                 $formaty->set_bg_color('yellow');
                 $formatc = $workbook->add_format();
                 $formatc->set_align('center');
                 $formatr = $workbook->add_format();
                 $formatr->set_bold(1);
                 $formatr->set_color('red');
                 $formatr->set_align('center');
                 $formatg = $workbook->add_format();
                 $formatg->set_bold(1);
                 $formatg->set_color('green');
                 $formatg->set_align('center');
                 // Here starts workshhet headers
                 $colnum = 0;
                 foreach ($headers as $item) {
                     $myxls->write(0, $colnum, $item, $formatbc);
                     $colnum++;
                 }
                 $rownum = 1;
             } else {
                 if ($download == 'Excel') {
                     require_once "{$CFG->libdir}/excellib.class.php";
                     $filename .= ".xls";
                     // Creating a workbook
                     $workbook = new MoodleExcelWorkbook("-");
                     // Sending HTTP headers
                     $workbook->send($filename);
                     // Creating the first worksheet
                     $sheettitle = get_string('report', 'scorm');
                     $myxls = $workbook->add_worksheet($sheettitle);
                     // format types
                     $format = $workbook->add_format();
                     $format->set_bold(0);
                     $formatbc = $workbook->add_format();
                     $formatbc->set_bold(1);
                     $formatbc->set_align('center');
                     $formatb = $workbook->add_format();
                     $formatb->set_bold(1);
                     $formaty = $workbook->add_format();
                     $formaty->set_bg_color('yellow');
                     $formatc = $workbook->add_format();
                     $formatc->set_align('center');
                     $formatr = $workbook->add_format();
                     $formatr->set_bold(1);
                     $formatr->set_color('red');
                     $formatr->set_align('center');
                     $formatg = $workbook->add_format();
                     $formatg->set_bold(1);
                     $formatg->set_color('green');
                     $formatg->set_align('center');
                     $colnum = 0;
                     foreach ($headers as $item) {
                         $myxls->write(0, $colnum, $item, $formatbc);
                         $colnum++;
                     }
                     $rownum = 1;
                 } else {
                     if ($download == 'CSV') {
                         $csvexport = new csv_export_writer("tab");
                         $csvexport->set_filename($filename, ".txt");
                         $csvexport->add_data($headers);
                     }
                 }
             }
         }
         $params = array();
         list($usql, $params) = $DB->get_in_or_equal($allowedlist, SQL_PARAMS_NAMED);
         // Construct the SQL
         $select = 'SELECT DISTINCT ' . $DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)') . ' AS uniqueid, ';
         $select .= 'st.scormid AS scormid, st.attempt AS attempt, ' . user_picture::fields('u', array('idnumber'), 'userid') . get_extra_user_fields_sql($coursecontext, 'u', '', array('email', 'idnumber')) . ' ';
         // This part is the same for all cases - join users and scorm_scoes_track tables
         $from = 'FROM {user} u ';
         $from .= 'LEFT JOIN {scorm_scoes_track} st ON st.userid = u.id AND st.scormid = ' . $scorm->id;
         switch ($attemptsmode) {
             case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH:
                 // Show only students with attempts
                 $where = ' WHERE u.id ' . $usql . ' AND st.userid IS NOT NULL';
                 break;
             case SCORM_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
                 // Show only students without attempts
                 $where = ' WHERE u.id ' . $usql . ' AND st.userid IS NULL';
                 break;
             case SCORM_REPORT_ATTEMPTS_ALL_STUDENTS:
                 // Show all students with or without attempts
                 $where = ' WHERE u.id ' . $usql . ' AND (st.userid IS NOT NULL OR st.userid IS NULL)';
                 break;
         }
         $countsql = 'SELECT COUNT(DISTINCT(' . $DB->sql_concat('u.id', '\'#\'', 'COALESCE(st.attempt, 0)') . ')) AS nbresults, ';
         $countsql .= 'COUNT(DISTINCT(' . $DB->sql_concat('u.id', '\'#\'', 'st.attempt') . ')) AS nbattempts, ';
         $countsql .= 'COUNT(DISTINCT(u.id)) AS nbusers ';
         $countsql .= $from . $where;
         if (!$download) {
             $sort = $table->get_sql_sort();
         } else {
             $sort = '';
         }
         // Fix some wired sorting
         if (empty($sort)) {
             $sort = ' ORDER BY uniqueid';
         } else {
             $sort = ' ORDER BY ' . $sort;
         }
         if (!$download) {
             // Add extra limits due to initials bar
             list($twhere, $tparams) = $table->get_sql_where();
             if ($twhere) {
                 $where .= ' AND ' . $twhere;
                 //initial bar
                 $params = array_merge($params, $tparams);
             }
             if (!empty($countsql)) {
                 $count = $DB->get_record_sql($countsql, $params);
                 $totalinitials = $count->nbresults;
                 if ($twhere) {
                     $countsql .= ' AND ' . $twhere;
                 }
                 $count = $DB->get_record_sql($countsql, $params);
                 $total = $count->nbresults;
             }
             $table->pagesize($pagesize, $total);
             echo '<div class="quizattemptcounts">';
             if ($count->nbresults == $count->nbattempts) {
                 echo get_string('reportcountattempts', 'scorm', $count);
             } else {
                 if ($count->nbattempts > 0) {
                     echo get_string('reportcountallattempts', 'scorm', $count);
                 } else {
                     echo $count->nbusers . ' ' . get_string('users');
                 }
             }
             echo '</div>';
         }
         // Fetch the attempts
         if (!$download) {
             $attempts = $DB->get_records_sql($select . $from . $where . $sort, $params, $table->get_page_start(), $table->get_page_size());
             echo '<div id="scormtablecontainer">';
             if ($candelete) {
                 // Start form
                 $strreallydel = addslashes_js(get_string('deleteattemptcheck', 'scorm'));
                 echo '<form id="attemptsform" method="post" action="' . $PAGE->url->out(false) . '" onsubmit="return confirm(\'' . $strreallydel . '\');">';
                 echo '<input type="hidden" name="action" value="delete"/>';
                 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
                 echo '<div style="display: none;">';
                 echo html_writer::input_hidden_params($PAGE->url);
                 echo '</div>';
                 echo '<div>';
             }
             $table->initialbars($totalinitials > 20);
             // Build table rows
         } else {
             $attempts = $DB->get_records_sql($select . $from . $where . $sort, $params);
         }
         if ($attempts) {
             foreach ($attempts as $scouser) {
                 $row = array();
                 if (!empty($scouser->attempt)) {
                     $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
                 } else {
                     $timetracks = '';
                 }
                 if (in_array('checkbox', $columns)) {
                     if ($candelete && !empty($timetracks->start)) {
                         $row[] = '<input type="checkbox" name="attemptid[]" value="' . $scouser->userid . ':' . $scouser->attempt . '" />';
                     } else {
                         if ($candelete) {
                             $row[] = '';
                         }
                     }
                 }
                 if (in_array('picture', $columns)) {
                     $user = new stdClass();
                     $additionalfields = explode(',', user_picture::fields());
                     $user = username_load_fields_from_object($user, $scouser, null, $additionalfields);
                     $user->id = $scouser->userid;
                     $row[] = $OUTPUT->user_picture($user, array('courseid' => $course->id));
                 }
                 if (!$download) {
                     $row[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $scouser->userid . '&amp;course=' . $course->id . '">' . fullname($scouser) . '</a>';
                 } else {
                     $row[] = fullname($scouser);
                 }
                 foreach ($extrafields as $field) {
                     $row[] = s($scouser->{$field});
                 }
                 if (empty($timetracks->start)) {
                     $row[] = '-';
                     $row[] = '-';
                     $row[] = '-';
                     $row[] = '-';
                 } else {
                     if (!$download) {
                         $row[] = '<a href="' . $CFG->wwwroot . '/mod/scorm/report/userreport.php?id=' . $cm->id . '&amp;user='******'&amp;attempt=' . $scouser->attempt . '">' . $scouser->attempt . '</a>';
                     } else {
                         $row[] = $scouser->attempt;
                     }
                     if ($download == 'ODS' || $download == 'Excel') {
                         $row[] = userdate($timetracks->start, get_string("strftimedatetime", "langconfig"));
                     } else {
                         $row[] = userdate($timetracks->start);
                     }
                     if ($download == 'ODS' || $download == 'Excel') {
                         $row[] = userdate($timetracks->finish, get_string('strftimedatetime', 'langconfig'));
                     } else {
                         $row[] = userdate($timetracks->finish);
                     }
                     $row[] = scorm_grade_user_attempt($scorm, $scouser->userid, $scouser->attempt);
                 }
                 // print out all scores of attempt
                 if ($scoes) {
                     foreach ($scoes as $sco) {
                         if ($sco->launch != '') {
                             if ($trackdata = scorm_get_tracks($sco->id, $scouser->userid, $scouser->attempt)) {
                                 if ($trackdata->status == '') {
                                     $trackdata->status = 'notattempted';
                                 }
                                 $strstatus = get_string($trackdata->status, 'scorm');
                                 // if raw score exists, print it
                                 if ($trackdata->score_raw != '') {
                                     $score = $trackdata->score_raw;
                                     // add max score if it exists
                                     if (scorm_version_check($scorm->version, SCORM_13)) {
                                         $maxkey = 'cmi.score.max';
                                     } else {
                                         $maxkey = 'cmi.core.score.max';
                                     }
                                     if (isset($trackdata->{$maxkey})) {
                                         $score .= '/' . $trackdata->{$maxkey};
                                     }
                                     // else print out status
                                 } else {
                                     $score = $strstatus;
                                 }
                                 if (!$download) {
                                     $row[] = '<img src="' . $OUTPUT->pix_url($trackdata->status, 'scorm') . '" alt="' . $strstatus . '" title="' . $strstatus . '" /><br/>
                                             <a href="' . $CFG->wwwroot . '/mod/scorm/report/userreporttracks.php?id=' . $cm->id . '&amp;scoid=' . $sco->id . '&amp;user='******'&amp;attempt=' . $scouser->attempt . '" title="' . get_string('details', 'scorm') . '">' . $score . '</a>';
                                 } else {
                                     $row[] = $score;
                                 }
                             } else {
                                 // if we don't have track data, we haven't attempted yet
                                 $strstatus = get_string('notattempted', 'scorm');
                                 if (!$download) {
                                     $row[] = '<img src="' . $OUTPUT->pix_url('notattempted', 'scorm') . '" alt="' . $strstatus . '" title="' . $strstatus . '" /><br/>' . $strstatus;
                                 } else {
                                     $row[] = $strstatus;
                                 }
                             }
                         }
                     }
                 }
                 if (!$download) {
                     $table->add_data($row);
                 } else {
                     if ($download == 'Excel' or $download == 'ODS') {
                         $colnum = 0;
                         foreach ($row as $item) {
                             $myxls->write($rownum, $colnum, $item, $format);
                             $colnum++;
                         }
                         $rownum++;
                     } else {
                         if ($download == 'CSV') {
                             $csvexport->add_data($row);
                         }
                     }
                 }
             }
             if (!$download) {
                 $table->finish_output();
                 if ($candelete) {
                     echo '<table id="commands">';
                     echo '<tr><td>';
                     echo '<a href="javascript:select_all_in(\'DIV\', null, \'scormtablecontainer\');">' . get_string('selectall', 'scorm') . '</a> / ';
                     echo '<a href="javascript:deselect_all_in(\'DIV\', null, \'scormtablecontainer\');">' . get_string('selectnone', 'scorm') . '</a> ';
                     echo '&nbsp;&nbsp;';
                     echo '<input type="submit" value="' . get_string('deleteselected', 'quiz_overview') . '"/>';
                     echo '</td></tr></table>';
                     // Close form
                     echo '</div>';
                     echo '</form>';
                 }
                 echo '</div>';
                 if (!empty($attempts)) {
                     echo '<table class="boxaligncenter"><tr>';
                     echo '<td>';
                     echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('download' => 'ODS') + $displayoptions), get_string('downloadods'));
                     echo "</td>\n";
                     echo '<td>';
                     echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('download' => 'Excel') + $displayoptions), get_string('downloadexcel'));
                     echo "</td>\n";
                     echo '<td>';
                     echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('download' => 'CSV') + $displayoptions), get_string('downloadtext'));
                     echo "</td>\n";
                     echo "<td>";
                     echo "</td>\n";
                     echo '</tr></table>';
                 }
             }
         } else {
             if ($candelete && !$download) {
                 echo '</div>';
                 echo '</form>';
                 $table->finish_output();
             }
             echo '</div>';
         }
         // Show preferences form irrespective of attempts are there to report or not
         if (!$download) {
             $mform->set_data(compact('detailedrep', 'pagesize', 'attemptsmode'));
             $mform->display();
         }
         if ($download == 'Excel' or $download == 'ODS') {
             $workbook->close();
             exit;
         } else {
             if ($download == 'CSV') {
                 $csvexport->download_file();
                 exit;
             }
         }
     } else {
         echo $OUTPUT->notification(get_string('noactivity', 'scorm'));
     }
 }
Пример #12
0
} else {
    $groupwhere = "";
}
if ($groupingid) {
    $groupingwhere = "AND gg.groupingid = :groupingid";
    $params['groupingid'] = $groupingid;
} else {
    $groupingwhere = "";
}
list($sort, $sortparams) = users_order_by_sql('u');
$allnames = get_all_user_name_fields(true, 'u');
$sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, {$allnames}, u.idnumber, u.username\n          FROM {groups} g\n               LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid\n               LEFT JOIN {groups_members} gm ON g.id = gm.groupid\n               LEFT JOIN {user} u ON gm.userid = u.id\n         WHERE g.courseid = :courseid {$groupwhere} {$groupingwhere}\n      ORDER BY g.name, {$sort}";
$rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
foreach ($rs as $row) {
    $user = new stdClass();
    $user = username_load_fields_from_object($user, $row, null, array('id' => 'userid', 'username', 'idnumber'));
    if (!$row->groupingid) {
        $row->groupingid = -1;
    }
    if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
        $members[$row->groupingid][$row->groupid] = array();
    }
    if (!empty($user->id)) {
        $members[$row->groupingid][$row->groupid][] = $user;
    }
}
$rs->close();
navigation_node::override_active_url(new moodle_url('/group/index.php', array('id' => $courseid)));
$PAGE->navbar->add(get_string('overview', 'group'));
/// Print header
$PAGE->set_title($strgroups);
Пример #13
0
 /**
  * Returns a list of forum discussions optionally sorted and paginated.
  *
  * @param int $forumid the forum instance id
  * @param string $sortby sort by this element (id, timemodified, timestart or timeend)
  * @param string $sortdirection sort direction: ASC or DESC
  * @param int $page page number
  * @param int $perpage items per page
  *
  * @return array the forum discussion details including warnings
  * @since Moodle 2.8
  */
 public static function get_forum_discussions_paginated($forumid, $sortby = 'timemodified', $sortdirection = 'DESC', $page = -1, $perpage = 0)
 {
     global $CFG, $DB, $USER, $PAGE;
     require_once $CFG->dirroot . "/mod/forum/lib.php";
     $warnings = array();
     $discussions = array();
     $params = self::validate_parameters(self::get_forum_discussions_paginated_parameters(), array('forumid' => $forumid, 'sortby' => $sortby, 'sortdirection' => $sortdirection, 'page' => $page, 'perpage' => $perpage));
     // Compact/extract functions are not recommended.
     $forumid = $params['forumid'];
     $sortby = $params['sortby'];
     $sortdirection = $params['sortdirection'];
     $page = $params['page'];
     $perpage = $params['perpage'];
     $sortallowedvalues = array('id', 'timemodified', 'timestart', 'timeend');
     if (!in_array($sortby, $sortallowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for sortby parameter (value: ' . $sortby . '),' . 'allowed values are: ' . implode(',', $sortallowedvalues));
     }
     $sortdirection = strtoupper($sortdirection);
     $directionallowedvalues = array('ASC', 'DESC');
     if (!in_array($sortdirection, $directionallowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for sortdirection parameter (value: ' . $sortdirection . '),' . 'allowed values are: ' . implode(',', $directionallowedvalues));
     }
     $forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
     $course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
     $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
     // Validate the module context. It checks everything that affects the module visibility (including groupings, etc..).
     $modcontext = context_module::instance($cm->id);
     self::validate_context($modcontext);
     // Check they have the view forum capability.
     require_capability('mod/forum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum');
     $sort = 'd.pinned DESC, d.' . $sortby . ' ' . $sortdirection;
     $alldiscussions = forum_get_discussions($cm, $sort, true, -1, -1, true, $page, $perpage, FORUM_POSTS_ALL_USER_GROUPS);
     if ($alldiscussions) {
         $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
         // Get the unreads array, this takes a forum id and returns data for all discussions.
         $unreads = array();
         if ($cantrack = forum_tp_can_track_forums($forum)) {
             if ($forumtracked = forum_tp_is_tracked($forum)) {
                 $unreads = forum_get_discussions_unread($cm);
             }
         }
         // The forum function returns the replies for all the discussions in a given forum.
         $replies = forum_count_discussion_replies($forumid, $sort, -1, $page, $perpage);
         foreach ($alldiscussions as $discussion) {
             // This function checks for qanda forums.
             // Note that the forum_get_discussions returns as id the post id, not the discussion id so we need to do this.
             $discussionrec = clone $discussion;
             $discussionrec->id = $discussion->discussion;
             if (!forum_user_can_see_discussion($forum, $discussionrec, $modcontext)) {
                 $warning = array();
                 // Function forum_get_discussions returns forum_posts ids not forum_discussions ones.
                 $warning['item'] = 'post';
                 $warning['itemid'] = $discussion->id;
                 $warning['warningcode'] = '1';
                 $warning['message'] = 'You can\'t see this discussion';
                 $warnings[] = $warning;
                 continue;
             }
             $discussion->numunread = 0;
             if ($cantrack && $forumtracked) {
                 if (isset($unreads[$discussion->discussion])) {
                     $discussion->numunread = (int) $unreads[$discussion->discussion];
                 }
             }
             $discussion->numreplies = 0;
             if (!empty($replies[$discussion->discussion])) {
                 $discussion->numreplies = (int) $replies[$discussion->discussion]->replies;
             }
             $discussion->name = external_format_string($discussion->name, $modcontext->id);
             $discussion->subject = external_format_string($discussion->subject, $modcontext->id);
             // Rewrite embedded images URLs.
             list($discussion->message, $discussion->messageformat) = external_format_text($discussion->message, $discussion->messageformat, $modcontext->id, 'mod_forum', 'post', $discussion->id);
             // List attachments.
             if (!empty($discussion->attachment)) {
                 $discussion->attachments = external_util::get_area_files($modcontext->id, 'mod_forum', 'attachment', $discussion->id);
             }
             $messageinlinefiles = external_util::get_area_files($modcontext->id, 'mod_forum', 'post', $discussion->id);
             if (!empty($messageinlinefiles)) {
                 $discussion->messageinlinefiles = $messageinlinefiles;
             }
             $discussion->locked = forum_discussion_is_locked($forum, $discussion);
             $discussion->canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
             if (forum_is_author_hidden($discussion, $forum)) {
                 $discussion->userid = null;
                 $discussion->userfullname = null;
                 $discussion->userpictureurl = null;
                 $discussion->usermodified = null;
                 $discussion->usermodifiedfullname = null;
                 $discussion->usermodifiedpictureurl = null;
             } else {
                 $picturefields = explode(',', user_picture::fields());
                 // Load user objects from the results of the query.
                 $user = new stdclass();
                 $user->id = $discussion->userid;
                 $user = username_load_fields_from_object($user, $discussion, null, $picturefields);
                 // Preserve the id, it can be modified by username_load_fields_from_object.
                 $user->id = $discussion->userid;
                 $discussion->userfullname = fullname($user, $canviewfullname);
                 $userpicture = new user_picture($user);
                 $userpicture->size = 1;
                 // Size f1.
                 $discussion->userpictureurl = $userpicture->get_url($PAGE)->out(false);
                 $usermodified = new stdclass();
                 $usermodified->id = $discussion->usermodified;
                 $usermodified = username_load_fields_from_object($usermodified, $discussion, 'um', $picturefields);
                 // Preserve the id (it can be overwritten due to the prefixed $picturefields).
                 $usermodified->id = $discussion->usermodified;
                 $discussion->usermodifiedfullname = fullname($usermodified, $canviewfullname);
                 $userpicture = new user_picture($usermodified);
                 $userpicture->size = 1;
                 // Size f1.
                 $discussion->usermodifiedpictureurl = $userpicture->get_url($PAGE)->out(false);
             }
             $discussions[] = $discussion;
         }
     }
     $result = array();
     $result['discussions'] = $discussions;
     $result['warnings'] = $warnings;
     return $result;
 }
Пример #14
0
/**
 * Returns all forum posts since a given time in specified forum.
 *
 * @todo Document this functions args
 * @global object
 * @global object
 * @global object
 * @global object
 */
function forum_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid = 0, $groupid = 0)
{
    global $CFG, $COURSE, $USER, $DB;
    if ($COURSE->id == $courseid) {
        $course = $COURSE;
    } else {
        $course = $DB->get_record('course', array('id' => $courseid));
    }
    $modinfo = get_fast_modinfo($course);
    $cm = $modinfo->cms[$cmid];
    $params = array($timestart, $cm->instance);
    if ($userid) {
        $userselect = "AND u.id = ?";
        $params[] = $userid;
    } else {
        $userselect = "";
    }
    if ($groupid) {
        $groupselect = "AND d.groupid = ?";
        $params[] = $groupid;
    } else {
        $groupselect = "";
    }
    $allnames = get_all_user_name_fields(true, 'u');
    if (!($posts = $DB->get_records_sql("SELECT p.*, f.type AS forumtype, d.forum, d.groupid,\n                                              d.timestart, d.timeend, d.userid AS duserid,\n                                              {$allnames}, u.email, u.picture, u.imagealt, u.email\n                                         FROM {forum_posts} p\n                                              JOIN {forum_discussions} d ON d.id = p.discussion\n                                              JOIN {forum} f             ON f.id = d.forum\n                                              JOIN {user} u              ON u.id = p.userid\n                                        WHERE p.created > ? AND f.id = ?\n                                              {$userselect} {$groupselect}\n                                     ORDER BY p.id ASC", $params))) {
        // order by initial posting date
        return;
    }
    $groupmode = groups_get_activity_groupmode($cm, $course);
    $cm_context = context_module::instance($cm->id);
    $viewhiddentimed = has_capability('mod/forum:viewhiddentimedposts', $cm_context);
    $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
    $printposts = array();
    foreach ($posts as $post) {
        if (!empty($CFG->forum_enabletimedposts) and $USER->id != $post->duserid and ($post->timestart > 0 and $post->timestart > time() or $post->timeend > 0 and $post->timeend < time())) {
            if (!$viewhiddentimed) {
                continue;
            }
        }
        if ($groupmode) {
            if ($post->groupid == -1 or $groupmode == VISIBLEGROUPS or $accessallgroups) {
                // oki (Open discussions have groupid -1)
            } else {
                // separate mode
                if (isguestuser()) {
                    // shortcut
                    continue;
                }
                if (!in_array($post->groupid, $modinfo->get_groups($cm->groupingid))) {
                    continue;
                }
            }
        }
        $printposts[] = $post;
    }
    if (!$printposts) {
        return;
    }
    $aname = format_string($cm->name, true);
    foreach ($printposts as $post) {
        $tmpactivity = new stdClass();
        $tmpactivity->type = 'forum';
        $tmpactivity->cmid = $cm->id;
        $tmpactivity->name = $aname;
        $tmpactivity->sectionnum = $cm->sectionnum;
        $tmpactivity->timestamp = $post->modified;
        $tmpactivity->content = new stdClass();
        $tmpactivity->content->id = $post->id;
        $tmpactivity->content->discussion = $post->discussion;
        $tmpactivity->content->subject = format_string($post->subject);
        $tmpactivity->content->parent = $post->parent;
        $tmpactivity->user = new stdClass();
        $additionalfields = array('id' => 'userid', 'picture', 'imagealt', 'email');
        $additionalfields = explode(',', user_picture::fields());
        $tmpactivity->user = username_load_fields_from_object($tmpactivity->user, $post, null, $additionalfields);
        $tmpactivity->user->id = $post->userid;
        $activities[$index++] = $tmpactivity;
    }
    return;
}
Пример #15
0
    /**
     * Add a LEAP2A entry element that corresponds to a submission including attachments.
     *
     * @param portfolio_format_leap2a_writer $leapwriter Writer object to add entries to.
     * @param workshop_submission $workshopsubmission
     * @param string $html The exported HTML content of the submission
     * @return int id of new entry
     */
    protected function export_content_leap2a(portfolio_format_leap2a_writer $leapwriter,
            workshop_submission $workshopsubmission, $html) {

        $entry = new portfolio_format_leap2a_entry('workshopsubmission'.$workshopsubmission->id,  s($workshopsubmission->title),
            'resource', $html);
        $entry->published = $workshopsubmission->timecreated;
        $entry->updated = $workshopsubmission->timemodified;
        $entry->author = (object)[
            'id' => $workshopsubmission->authorid,
            'email' => $workshopsubmission->authoremail
        ];
        username_load_fields_from_object($entry->author, $workshopsubmission);

        $leapwriter->link_files($entry, $this->multifiles);
        $entry->add_category('web', 'resource_type');
        $leapwriter->add_entry($entry);

        return $entry->id;
    }
Пример #16
0
 /**
  * Get messages function implementation.
  *
  * @since  2.8
  * @throws invalid_parameter_exception
  * @throws moodle_exception
  * @param  int      $useridto       the user id who received the message
  * @param  int      $useridfrom     the user id who send the message. -10 or -20 for no-reply or support user
  * @param  string   $type           type of message to return, expected values: notifications, conversations and both
  * @param  bool     $read           true for retreiving read messages, false for unread
  * @param  bool     $newestfirst    true for ordering by newest first, false for oldest first
  * @param  int      $limitfrom      limit from
  * @param  int      $limitnum       limit num
  * @return external_description
  */
 public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true, $newestfirst = true, $limitfrom = 0, $limitnum = 0)
 {
     global $CFG, $USER;
     $warnings = array();
     $params = array('useridto' => $useridto, 'useridfrom' => $useridfrom, 'type' => $type, 'read' => $read, 'newestfirst' => $newestfirst, 'limitfrom' => $limitfrom, 'limitnum' => $limitnum);
     $params = self::validate_parameters(self::get_messages_parameters(), $params);
     $context = context_system::instance();
     self::validate_context($context);
     $useridto = $params['useridto'];
     $useridfrom = $params['useridfrom'];
     $type = $params['type'];
     $read = $params['read'];
     $newestfirst = $params['newestfirst'];
     $limitfrom = $params['limitfrom'];
     $limitnum = $params['limitnum'];
     $allowedvalues = array('notifications', 'conversations', 'both');
     if (!in_array($type, $allowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' . 'allowed values are: ' . implode(',', $allowedvalues));
     }
     // Check if private messaging between users is allowed.
     if (empty($CFG->messaging)) {
         // If we are retreiving only conversations, and messaging is disabled, throw an exception.
         if ($type == "conversations") {
             throw new moodle_exception('disabled', 'message');
         }
         if ($type == "both") {
             $warning = array();
             $warning['item'] = 'message';
             $warning['itemid'] = $USER->id;
             $warning['warningcode'] = '1';
             $warning['message'] = 'Private messages (conversations) are not enabled in this site.
                 Only notifications will be returned';
             $warnings[] = $warning;
         }
     }
     if (!empty($useridto)) {
         if (core_user::is_real_user($useridto)) {
             $userto = core_user::get_user($useridto, '*', MUST_EXIST);
         } else {
             throw new moodle_exception('invaliduser');
         }
     }
     if (!empty($useridfrom)) {
         // We use get_user here because the from user can be the noreply or support user.
         $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST);
     }
     // Check if the current user is the sender/receiver or just a privileged user.
     if ($useridto != $USER->id and $useridfrom != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
         throw new moodle_exception('accessdenied', 'admin');
     }
     // Which type of messages to retrieve.
     $notifications = -1;
     if ($type != 'both') {
         $notifications = $type == 'notifications' ? 1 : 0;
     }
     $orderdirection = $newestfirst ? 'DESC' : 'ASC';
     $sort = "mr.timecreated {$orderdirection}";
     if ($messages = message_get_messages($useridto, $useridfrom, $notifications, $read, $sort, $limitfrom, $limitnum)) {
         $canviewfullname = has_capability('moodle/site:viewfullnames', $context);
         // In some cases, we don't need to get the to/from user objects from the sql query.
         $userfromfullname = '';
         $usertofullname = '';
         // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there.
         if (!empty($useridto)) {
             $usertofullname = fullname($userto, $canviewfullname);
             // The user from may or may not be filled.
             if (!empty($useridfrom)) {
                 $userfromfullname = fullname($userfrom, $canviewfullname);
             }
         } else {
             // If the useridto field is empty, the useridfrom must be filled.
             $userfromfullname = fullname($userfrom, $canviewfullname);
         }
         foreach ($messages as $mid => $message) {
             // Do not return deleted messages.
             if ($useridto == $USER->id and $message->timeusertodeleted or $useridfrom == $USER->id and $message->timeuserfromdeleted) {
                 unset($messages[$mid]);
                 continue;
             }
             // We need to get the user from the query.
             if (empty($userfromfullname)) {
                 // Check for non-reply and support users.
                 if (core_user::is_real_user($message->useridfrom)) {
                     $user = new stdClass();
                     $user = username_load_fields_from_object($user, $message, 'userfrom');
                     $message->userfromfullname = fullname($user, $canviewfullname);
                 } else {
                     $user = core_user::get_user($message->useridfrom);
                     $message->userfromfullname = fullname($user, $canviewfullname);
                 }
             } else {
                 $message->userfromfullname = $userfromfullname;
             }
             // We need to get the user from the query.
             if (empty($usertofullname)) {
                 $user = new stdClass();
                 $user = username_load_fields_from_object($user, $message, 'userto');
                 $message->usertofullname = fullname($user, $canviewfullname);
             } else {
                 $message->usertofullname = $usertofullname;
             }
             // This field is only available in the message_read table.
             if (!isset($message->timeread)) {
                 $message->timeread = 0;
             }
             $message->text = message_format_message_text($message);
             $messages[$mid] = (array) $message;
         }
     }
     $results = array('messages' => $messages, 'warnings' => $warnings);
     return $results;
 }
Пример #17
0
 /**
  * Returns a list of forum discussions optionally sorted and paginated.
  *
  * @param int $forumid the forum instance id
  * @param string $sortby sort by this element (id, timemodified, timestart or timeend)
  * @param string $sortdirection sort direction: ASC or DESC
  * @param int $page page number
  * @param int $perpage items per page
  *
  * @return array the forum discussion details including warnings
  * @since Moodle 2.8
  */
 public static function get_forum_discussions_paginated($forumid, $sortby = 'timemodified', $sortdirection = 'DESC', $page = -1, $perpage = 0)
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . "/mod/forum/lib.php";
     $warnings = array();
     $discussions = array();
     $params = self::validate_parameters(self::get_forum_discussions_paginated_parameters(), array('forumid' => $forumid, 'sortby' => $sortby, 'sortdirection' => $sortdirection, 'page' => $page, 'perpage' => $perpage));
     // Compact/extract functions are not recommended.
     $forumid = $params['forumid'];
     $sortby = $params['sortby'];
     $sortdirection = $params['sortdirection'];
     $page = $params['page'];
     $perpage = $params['perpage'];
     $sortallowedvalues = array('id', 'timemodified', 'timestart', 'timeend');
     if (!in_array($sortby, $sortallowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for sortby parameter (value: ' . $sortby . '),' . 'allowed values are: ' . implode(',', $sortallowedvalues));
     }
     $sortdirection = strtoupper($sortdirection);
     $directionallowedvalues = array('ASC', 'DESC');
     if (!in_array($sortdirection, $directionallowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for sortdirection parameter (value: ' . $sortdirection . '),' . 'allowed values are: ' . implode(',', $directionallowedvalues));
     }
     $forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
     $course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
     $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
     // Validate the module context. It checks everything that affects the module visibility (including groupings, etc..).
     $modcontext = context_module::instance($cm->id);
     self::validate_context($modcontext);
     // Check they have the view forum capability.
     require_capability('mod/forum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum');
     $sort = 'd.' . $sortby . ' ' . $sortdirection;
     $alldiscussions = forum_get_discussions($cm, $sort, true, -1, -1, true, $page, $perpage);
     if ($alldiscussions) {
         $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
         // Get the unreads array, this takes a forum id and returns data for all discussions.
         $unreads = array();
         if ($cantrack = forum_tp_can_track_forums($forum)) {
             if ($forumtracked = forum_tp_is_tracked($forum)) {
                 $unreads = forum_get_discussions_unread($cm);
             }
         }
         // The forum function returns the replies for all the discussions in a given forum.
         $replies = forum_count_discussion_replies($forumid, $sort, -1, $page, $perpage);
         foreach ($alldiscussions as $discussion) {
             // This function checks for qanda forums.
             // Note that the forum_get_discussions returns as id the post id, not the discussion id so we need to do this.
             $discussionrec = clone $discussion;
             $discussionrec->id = $discussion->discussion;
             if (!forum_user_can_see_discussion($forum, $discussionrec, $modcontext)) {
                 $warning = array();
                 // Function forum_get_discussions returns forum_posts ids not forum_discussions ones.
                 $warning['item'] = 'post';
                 $warning['itemid'] = $discussion->id;
                 $warning['warningcode'] = '1';
                 $warning['message'] = 'You can\'t see this discussion';
                 $warnings[] = $warning;
                 continue;
             }
             $discussion->numunread = 0;
             if ($cantrack && $forumtracked) {
                 if (isset($unreads[$discussion->discussion])) {
                     $discussion->numunread = (int) $unreads[$discussion->discussion];
                 }
             }
             $discussion->numreplies = 0;
             if (!empty($replies[$discussion->discussion])) {
                 $discussion->numreplies = (int) $replies[$discussion->discussion]->replies;
             }
             // Load user objects from the results of the query.
             $user = new stdclass();
             $user->id = $discussion->userid;
             $user = username_load_fields_from_object($user, $discussion);
             $discussion->userfullname = fullname($user, $canviewfullname);
             // We can have post written by users that are deleted. In this case, those users don't have a valid context.
             $usercontext = context_user::instance($user->id, IGNORE_MISSING);
             if ($usercontext) {
                 $discussion->userpictureurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
             } else {
                 $discussion->userpictureurl = '';
             }
             $usermodified = new stdclass();
             $usermodified->id = $discussion->usermodified;
             $usermodified = username_load_fields_from_object($usermodified, $discussion, 'um');
             $discussion->usermodifiedfullname = fullname($usermodified, $canviewfullname);
             // We can have post written by users that are deleted. In this case, those users don't have a valid context.
             $usercontext = context_user::instance($usermodified->id, IGNORE_MISSING);
             if ($usercontext) {
                 $discussion->usermodifiedpictureurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
             } else {
                 $discussion->usermodifiedpictureurl = '';
             }
             // Rewrite embedded images URLs.
             list($discussion->message, $discussion->messageformat) = external_format_text($discussion->message, $discussion->messageformat, $modcontext->id, 'mod_forum', 'post', $discussion->id);
             // List attachments.
             if (!empty($discussion->attachment)) {
                 $discussion->attachments = array();
                 $fs = get_file_storage();
                 if ($files = $fs->get_area_files($modcontext->id, 'mod_forum', 'attachment', $discussion->id, "filename", false)) {
                     foreach ($files as $file) {
                         $filename = $file->get_filename();
                         $discussion->attachments[] = array('filename' => $filename, 'mimetype' => $file->get_mimetype(), 'fileurl' => file_encode_url($CFG->wwwroot . '/webservice/pluginfile.php', '/' . $modcontext->id . '/mod_forum/attachment/' . $discussion->id . '/' . $filename));
                     }
                 }
             }
             $discussions[] = $discussion;
         }
     }
     $result = array();
     $result['discussions'] = $discussions;
     $result['warnings'] = $warnings;
     return $result;
 }
 /**
  * Get the current user record.
  *
  * @return stdClass|false
  */
 protected function get_user_record()
 {
     global $DB, $USER;
     if ($this->currentuserrecord === null) {
         $sqlme = "SELECT {$this->sql->fields}\n                        FROM {$this->sql->from}\n                       WHERE {$this->sql->where}\n                         AND x.userid = :myuserid";
         $record = $DB->get_record_sql($sqlme, $this->sql->params + array('myuserid' => $this->userid));
         // Hack so that admin can see something. Hopefully we won't create too many bugs in case of missing fields.
         if (empty($record) && $this->neighboursonly && $this->xpmanager->can_manage()) {
             $record = (object) array('id' => 0, 'userid' => $this->userid, 'courseid' => $this->xpmanager->get_courseid(), 'xp' => 0, 'lvl' => 1);
             $record = username_load_fields_from_object($record, $USER);
             $record->picture = $USER->picture;
             $record->imagealt = $USER->imagealt;
             $record->email = $USER->email;
         }
         $this->currentuserrecord = $record;
     }
     return $this->currentuserrecord;
 }
Пример #19
0
 /**
  * Method to display column grader.
  *
  * @param \stdClass $history an entry of history record.
  *
  * @return string HTML to display
  */
 public function col_grader(\stdClass $history)
 {
     if (empty($history->usermodified)) {
         // Not every row has a valid usermodified.
         return '';
     }
     $grader = new \stdClass();
     $grader = username_load_fields_from_object($grader, $history, 'grader');
     $name = fullname($grader);
     if ($this->download) {
         return $name;
     }
     $userid = $history->usermodified;
     $profileurl = new \moodle_url('/user/view.php', array('id' => $userid, 'course' => $this->courseid));
     return \html_writer::link($profileurl, $name);
 }