Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/report/participation/index.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00026 require('../../config.php');
00027 require_once($CFG->dirroot.'/lib/tablelib.php');
00028 
00029 define('DEFAULT_PAGE_SIZE', 20);
00030 define('SHOW_ALL_PAGE_SIZE', 5000);
00031 
00032 $id         = required_param('id', PARAM_INT); // course id.
00033 $roleid     = optional_param('roleid', 0, PARAM_INT); // which role to show
00034 $instanceid = optional_param('instanceid', 0, PARAM_INT); // instance we're looking at.
00035 $timefrom   = optional_param('timefrom', 0, PARAM_INT); // how far back to look...
00036 $action     = optional_param('action', '', PARAM_ALPHA);
00037 $page       = optional_param('page', 0, PARAM_INT);                     // which page to show
00038 $perpage    = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);  // how many per page
00039 
00040 $url = new moodle_url('/report/participation/index.php', array('id'=>$id));
00041 if ($roleid !== 0) $url->param('roleid');
00042 if ($instanceid !== 0) $url->param('instanceid');
00043 if ($timefrom !== 0) $url->param('timefrom');
00044 if ($action !== '') $url->param('action');
00045 if ($page !== 0) $url->param('page');
00046 if ($perpage !== DEFAULT_PAGE_SIZE) $url->param('perpage');
00047 $PAGE->set_url($url);
00048 $PAGE->set_pagelayout('admin');
00049 
00050 if ($action != 'view' and $action != 'post') {
00051     $action = ''; // default to all (don't restrict)
00052 }
00053 
00054 if (!$course = $DB->get_record('course', array('id'=>$id))) {
00055     print_error('invalidcourse');
00056 }
00057 
00058 if ($roleid != 0 and !$role = $DB->get_record('role', array('id'=>$roleid))) {
00059     print_error('invalidrole');
00060 }
00061 
00062 require_login($course);
00063 $context = get_context_instance(CONTEXT_COURSE, $course->id);
00064 require_capability('report/participation:view', $context);
00065 
00066 add_to_log($course->id, "course", "report participation", "report/participation/index.php?id=$course->id", $course->id);
00067 
00068 $strparticipation = get_string('participationreport');
00069 $strviews         = get_string('views');
00070 $strposts         = get_string('posts');
00071 $strview          = get_string('view');
00072 $strpost          = get_string('post');
00073 $strallactions    = get_string('allactions');
00074 $strreports       = get_string('reports');
00075 
00076 $actionoptions = array('' => $strallactions,
00077                        'view' => $strview,
00078                        'post' => $strpost,);
00079 if (!array_key_exists($action, $actionoptions)) {
00080     $action = '';
00081 }
00082 
00083 $PAGE->set_title($course->shortname .': '. $strparticipation);
00084 $PAGE->set_heading($course->fullname);
00085 echo $OUTPUT->header();
00086 
00087 $modinfo = get_fast_modinfo($course);
00088 
00089 $modules = $DB->get_records_select('modules', "visible = 1", null, 'name ASC');
00090 
00091 $instanceoptions = array();
00092 foreach ($modules as $module) {
00093     if (empty($modinfo->instances[$module->name])) {
00094         continue;
00095     }
00096     $instances = array();
00097     foreach ($modinfo->instances[$module->name] as $cm) {
00098         // Skip modules such as label which do not actually have links;
00099         // this means there's nothing to participate in
00100         if (!$cm->has_view()) {
00101             continue;
00102         }
00103         $instances[$cm->id] = format_string($cm->name);
00104     }
00105     if (count($instances) == 0) {
00106         continue;
00107     }
00108     $instanceoptions[] = array(get_string('modulenameplural', $module->name)=>$instances);
00109 }
00110 
00111 $timeoptions = array();
00112 // get minimum log time for this course
00113 $minlog = $DB->get_field_sql('SELECT min(time) FROM {log} WHERE course = ?', array($course->id));
00114 
00115 $now = usergetmidnight(time());
00116 
00117 // days
00118 for ($i = 1; $i < 7; $i++) {
00119     if (strtotime('-'.$i.' days',$now) >= $minlog) {
00120         $timeoptions[strtotime('-'.$i.' days',$now)] = get_string('numdays','moodle',$i);
00121     }
00122 }
00123 // weeks
00124 for ($i = 1; $i < 10; $i++) {
00125     if (strtotime('-'.$i.' weeks',$now) >= $minlog) {
00126         $timeoptions[strtotime('-'.$i.' weeks',$now)] = get_string('numweeks','moodle',$i);
00127     }
00128 }
00129 // months
00130 for ($i = 2; $i < 12; $i++) {
00131     if (strtotime('-'.$i.' months',$now) >= $minlog) {
00132         $timeoptions[strtotime('-'.$i.' months',$now)] = get_string('nummonths','moodle',$i);
00133     }
00134 }
00135 // try a year
00136 if (strtotime('-1 year',$now) >= $minlog) {
00137     $timeoptions[strtotime('-1 year',$now)] = get_string('lastyear');
00138 }
00139 
00140 $roleoptions = array();
00141 // TODO: we need a new list of roles that are visible here
00142 if ($roles = get_roles_used_in_context($context)) {
00143     foreach ($roles as $r) {
00144         $roleoptions[$r->id] = $r->name;
00145     }
00146 }
00147 $guestrole = get_guest_role();
00148 if (empty($roleoptions[$guestrole->id])) {
00149         $roleoptions[$guestrole->id] = $guestrole->name;
00150 }
00151 
00152 $roleoptions = role_fix_names($roleoptions, $context);
00153 
00154 // print first controls.
00155 echo '<form class="participationselectform" action="index.php" method="get"><div>'."\n".
00156      '<input type="hidden" name="id" value="'.$course->id.'" />'."\n";
00157 echo '<label for="menuinstanceid">'.get_string('activitymodule').'</label>'."\n";
00158 echo html_writer::select($instanceoptions, 'instanceid', $instanceid);
00159 echo '<label for="menutimefrom">'.get_string('lookback').'</label>'."\n";
00160 echo html_writer::select($timeoptions,'timefrom',$timefrom);
00161 echo '<label for="menuroleid">'.get_string('showonly').'</label>'."\n";
00162 echo html_writer::select($roleoptions,'roleid',$roleid,false);
00163 echo '<label for="menuaction">'.get_string('showactions').'</label>'."\n";
00164 echo html_writer::select($actionoptions,'action',$action,false);
00165 echo '<input type="submit" value="'.get_string('go').'" />'."\n</div></form>\n";
00166 
00167 $baseurl =  $CFG->wwwroot.'/report/participation/index.php?id='.$course->id.'&amp;roleid='
00168     .$roleid.'&amp;instanceid='.$instanceid.'&amp;timefrom='.$timefrom.'&amp;action='.$action.'&amp;perpage='.$perpage;
00169 
00170 if (!empty($instanceid) && !empty($roleid)) {
00171     // from here assume we have at least the module we're using.
00172     $cm = $modinfo->cms[$instanceid];
00173     $modulename = get_string('modulename', $cm->modname);
00174 
00175     include_once($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php');
00176 
00177     $viewfun = $cm->modname.'_get_view_actions';
00178     $postfun = $cm->modname.'_get_post_actions';
00179 
00180     if (!function_exists($viewfun) || !function_exists($postfun)) {
00181         print_error('modulemissingcode', 'error', $baseurl, $cm->modname);
00182     }
00183 
00184     $viewnames = $viewfun();
00185     $postnames = $postfun();
00186 
00187     $table = new flexible_table('course-participation-'.$course->id.'-'.$cm->id.'-'.$roleid);
00188     $table->course = $course;
00189 
00190     $table->define_columns(array('fullname','count','select'));
00191     $table->define_headers(array(get_string('user'),((!empty($action)) ? get_string($action) : get_string('allactions')),get_string('select')));
00192     $table->define_baseurl($baseurl);
00193 
00194     $table->set_attribute('cellpadding','5');
00195     $table->set_attribute('class', 'generaltable generalbox reporttable');
00196 
00197     $table->sortable(true,'lastname','ASC');
00198     $table->no_sorting('select');
00199 
00200     $table->set_control_variables(array(
00201                                         TABLE_VAR_SORT    => 'ssort',
00202                                         TABLE_VAR_HIDE    => 'shide',
00203                                         TABLE_VAR_SHOW    => 'sshow',
00204                                         TABLE_VAR_IFIRST  => 'sifirst',
00205                                         TABLE_VAR_ILAST   => 'silast',
00206                                         TABLE_VAR_PAGE    => 'spage'
00207                                         ));
00208     $table->setup();
00209 
00210     switch ($action) {
00211         case 'view':
00212             $actions = $viewnames;
00213             break;
00214         case 'post':
00215             $actions = $postnames;
00216             break;
00217         default:
00218             // some modules have stuff we want to hide, ie mail blocked etc so do actually need to limit here.
00219             $actions = array_merge($viewnames, $postnames);
00220     }
00221 
00222     list($actionsql, $params) = $DB->get_in_or_equal($actions, SQL_PARAMS_NAMED, 'action');
00223     $actionsql = "action $actionsql";
00224 
00225     $relatedcontexts = get_related_contexts_string($context);
00226 
00227     $sql = "SELECT ra.userid, u.firstname, u.lastname, u.idnumber, l.actioncount AS count
00228             FROM (SELECT * FROM {role_assignments} WHERE contextid $relatedcontexts AND roleid = :roleid ) ra
00229             JOIN {user} u ON u.id = ra.userid
00230             LEFT JOIN (
00231                 SELECT userid, COUNT(action) AS actioncount FROM {log} WHERE cmid = :instanceid AND time > :timefrom AND $actionsql GROUP BY userid
00232             ) l ON (l.userid = ra.userid)";
00233     $params['roleid'] = $roleid;
00234     $params['instanceid'] = $instanceid;
00235     $params['timefrom'] = $timefrom;
00236 
00237     list($twhere, $tparams) = $table->get_sql_where();
00238     if ($twhere) {
00239         $sql .= ' WHERE '.$twhere; //initial bar
00240         $params = array_merge($params, $tparams);
00241     }
00242 
00243     if ($table->get_sql_sort()) {
00244         $sql .= ' ORDER BY '.$table->get_sql_sort();
00245     }
00246 
00247     $countsql = "SELECT COUNT(DISTINCT(ra.userid))
00248                    FROM {role_assignments} ra
00249                    JOIN {user} u ON u.id = ra.userid
00250                   WHERE ra.contextid $relatedcontexts AND ra.roleid = :roleid";
00251 
00252     $totalcount = $DB->count_records_sql($countsql, $params);
00253 
00254     if ($twhere) {
00255         $matchcount = $DB->count_records_sql($countsql.' AND '.$twhere, $params);
00256     } else {
00257         $matchcount = $totalcount;
00258     }
00259 
00260     echo '<div id="participationreport">' . "\n";
00261     echo '<p class="modulename">'.$modulename . ' ' . $strviews.': '.implode(', ',$viewnames).'<br />'."\n"
00262         . $modulename . ' ' . $strposts.': '.implode(', ',$postnames).'</p>'."\n";
00263 
00264     $table->initialbars($totalcount > $perpage);
00265     $table->pagesize($perpage, $matchcount);
00266 
00267     if (!$users = $DB->get_records_sql($sql, $params, $table->get_page_start(), $table->get_page_size())) {
00268         $users = array(); // tablelib will handle saying 'Nothing to display' for us.
00269     }
00270 
00271     $data = array();
00272 
00273     $a = new stdClass();
00274     $a->count = $totalcount;
00275     $a->items = $role->name;
00276 
00277     if ($matchcount != $totalcount) {
00278         $a->count = $matchcount.'/'.$a->count;
00279     }
00280 
00281     echo '<h2>'.get_string('counteditems', '', $a).'</h2>'."\n";
00282 
00283     echo '<form action="'.$CFG->wwwroot.'/user/action_redir.php" method="post" id="studentsform">'."\n";
00284     echo '<div>'."\n";
00285     echo '<input type="hidden" name="id" value="'.$id.'" />'."\n";
00286     echo '<input type="hidden" name="returnto" value="'. s($FULLME) .'" />'."\n";
00287     echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />'."\n";
00288 
00289     foreach ($users as $u) {
00290         $data = array('<a href="'.$CFG->wwwroot.'/user/view.php?id='.$u->userid.'&amp;course='.$course->id.'">'.fullname($u,true).'</a>'."\n",
00291                       ((!empty($u->count)) ? get_string('yes').' ('.$u->count.') ' : get_string('no')),
00292                       '<input type="checkbox" class="usercheckbox" name="user'.$u->userid.'" value="'.$u->count.'" />'."\n",
00293                       );
00294         $table->add_data($data);
00295     }
00296 
00297     $table->print_html();
00298 
00299     if ($perpage == SHOW_ALL_PAGE_SIZE) {
00300         echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.DEFAULT_PAGE_SIZE.'">'.get_string('showperpage', '', DEFAULT_PAGE_SIZE).'</a></div>'."\n";
00301     }
00302     else if ($matchcount > 0 && $perpage < $matchcount) {
00303         echo '<div id="showall"><a href="'.$baseurl.'&amp;perpage='.SHOW_ALL_PAGE_SIZE.'">'.get_string('showall', '', $matchcount).'</a></div>'."\n";
00304     }
00305 
00306     echo '<div class="selectbuttons">';
00307     echo '<input type="button" id="checkall" value="'.get_string('selectall').'" /> '."\n";
00308     echo '<input type="button" id="checknone" value="'.get_string('deselectall').'" /> '."\n";
00309     if ($perpage >= $matchcount) {
00310         echo '<input type="button" id="checknos" value="'.get_string('selectnos').'" />'."\n";
00311     }
00312     echo '</div>';
00313     echo '<div>';
00314     echo '<label for="formaction">'.get_string('withselectedusers').'</label>';
00315     $displaylist['messageselect.php'] = get_string('messageselectadd');
00316     echo html_writer::select($displaylist, 'formaction', '', array(''=>'choosedots'), array('id'=>'formactionselect'));
00317     echo $OUTPUT->help_icon('withselectedusers');
00318     echo '<input type="submit" value="' . get_string('ok') . '" />'."\n";
00319     echo '</div>';
00320     echo '</div>'."\n";
00321     echo '</form>'."\n";
00322     echo '</div>'."\n";
00323 
00324     $PAGE->requires->js_init_call('M.report_participation.init');
00325 }
00326 
00327 echo $OUTPUT->footer();
00328 
00329 
 All Data Structures Namespaces Files Functions Variables Enumerations