Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/view.php
Go to the documentation of this file.
00001 <?php
00003 //                                                                       //
00004 // NOTICE OF COPYRIGHT                                                   //
00005 //                                                                       //
00006 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00007 //          http://moodle.org                                            //
00008 //                                                                       //
00009 // Copyright (C) 2005 Martin Dougiamas  http://dougiamas.com             //
00010 //                                                                       //
00011 // This program is free software; you can redistribute it and/or modify  //
00012 // it under the terms of the GNU General Public License as published by  //
00013 // the Free Software Foundation; either version 2 of the License, or     //
00014 // (at your option) any later version.                                   //
00015 //                                                                       //
00016 // This program is distributed in the hope that it will be useful,       //
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00019 // GNU General Public License for more details:                          //
00020 //                                                                       //
00021 //          http://www.gnu.org/copyleft/gpl.html                         //
00022 //                                                                       //
00024 
00025     require_once(dirname(__FILE__) . '/../../config.php');
00026     require_once($CFG->dirroot . '/mod/data/lib.php');
00027     require_once($CFG->libdir . '/rsslib.php');
00028     require_once($CFG->libdir . '/completionlib.php');
00029 
00031     $id = optional_param('id', 0, PARAM_INT);  // course module id
00032     $d = optional_param('d', 0, PARAM_INT);   // database id
00033     $rid = optional_param('rid', 0, PARAM_INT);    //record id
00034     $mode = optional_param('mode', '', PARAM_ALPHA);    // Force the browse mode  ('single')
00035     $filter = optional_param('filter', 0, PARAM_BOOL);
00036     // search filter will only be applied when $filter is true
00037 
00038     $edit = optional_param('edit', -1, PARAM_BOOL);
00039     $page = optional_param('page', 0, PARAM_INT);
00041     $approve = optional_param('approve', 0, PARAM_INT);    //approval recordid
00042     $delete = optional_param('delete', 0, PARAM_INT);    //delete recordid
00043 
00044     if ($id) {
00045         if (! $cm = get_coursemodule_from_id('data', $id)) {
00046             print_error('invalidcoursemodule');
00047         }
00048         if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
00049             print_error('coursemisconf');
00050         }
00051         if (! $data = $DB->get_record('data', array('id'=>$cm->instance))) {
00052             print_error('invalidcoursemodule');
00053         }
00054         $record = NULL;
00055 
00056     } else if ($rid) {
00057         if (! $record = $DB->get_record('data_records', array('id'=>$rid))) {
00058             print_error('invalidrecord', 'data');
00059         }
00060         if (! $data = $DB->get_record('data', array('id'=>$record->dataid))) {
00061             print_error('invalidid', 'data');
00062         }
00063         if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
00064             print_error('coursemisconf');
00065         }
00066         if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
00067             print_error('invalidcoursemodule');
00068         }
00069     } else {   // We must have $d
00070         if (! $data = $DB->get_record('data', array('id'=>$d))) {
00071             print_error('invalidid', 'data');
00072         }
00073         if (! $course = $DB->get_record('course', array('id'=>$data->course))) {
00074             print_error('coursemisconf');
00075         }
00076         if (! $cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
00077             print_error('invalidcoursemodule');
00078         }
00079         $record = NULL;
00080     }
00081 
00082     require_course_login($course, true, $cm);
00083 
00084     require_once($CFG->dirroot . '/comment/lib.php');
00085     comment::init();
00086 
00087     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00088     require_capability('mod/data:viewentry', $context);
00089 
00091     if (has_capability('mod/data:managetemplates', $context)) {
00092         if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) {      // Brand new database!
00093             redirect($CFG->wwwroot.'/mod/data/field.php?d='.$data->id);  // Redirect to field entry
00094         }
00095     }
00096 
00097 
00099     if (!isset($SESSION->dataprefs)) {
00100         $SESSION->dataprefs = array();
00101     }
00102     if (!isset($SESSION->dataprefs[$data->id])) {
00103         $SESSION->dataprefs[$data->id] = array();
00104         $SESSION->dataprefs[$data->id]['search'] = '';
00105         $SESSION->dataprefs[$data->id]['search_array'] = array();
00106         $SESSION->dataprefs[$data->id]['sort'] = $data->defaultsort;
00107         $SESSION->dataprefs[$data->id]['advanced'] = 0;
00108         $SESSION->dataprefs[$data->id]['order'] = ($data->defaultsortdir == 0) ? 'ASC' : 'DESC';
00109     }
00110 
00111     // reset advanced form
00112     if (!is_null(optional_param('resetadv', null, PARAM_RAW))) {
00113         $SESSION->dataprefs[$data->id]['search_array'] = array();
00114         // we need the redirect to cleanup the form state properly
00115         redirect("view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=1");
00116     }
00117 
00118     $advanced = optional_param('advanced', -1, PARAM_INT);
00119     if ($advanced == -1) {
00120         $advanced = $SESSION->dataprefs[$data->id]['advanced'];
00121     } else {
00122         if (!$advanced) {
00123             // explicitly switched to normal mode - discard all advanced search settings
00124             $SESSION->dataprefs[$data->id]['search_array'] = array();
00125         }
00126         $SESSION->dataprefs[$data->id]['advanced'] = $advanced;
00127     }
00128 
00129     $search_array = $SESSION->dataprefs[$data->id]['search_array'];
00130 
00131     if (!empty($advanced)) {
00132         $search = '';
00133         $vals = array();
00134         $fields = $DB->get_records('data_fields', array('dataid'=>$data->id));
00135 
00136         //Added to ammend paging error. This error would occur when attempting to go from one page of advanced
00137         //search results to another.  All fields were reset in the page transfer, and there was no way of determining
00138         //whether or not the user reset them.  This would cause a blank search to execute whenever the user attempted
00139         //to see any page of results past the first.
00140         //This fix works as follows:
00141         //$paging flag is set to false when page 0 of the advanced search results is viewed for the first time.
00142         //Viewing any page of results after page 0 passes the false $paging flag though the URL (see line 523) and the
00143         //execution falls through to the second condition below, allowing paging to be set to true.
00144         //Paging remains true and keeps getting passed though the URL until a new search is performed
00145         //(even if page 0 is revisited).
00146         //A false $paging flag generates advanced search results based on the fields input by the user.
00147         //A true $paging flag generates davanced search results from the $SESSION global.
00148 
00149         $paging = optional_param('paging', NULL, PARAM_BOOL);
00150         if($page == 0 && !isset($paging)) {
00151             $paging = false;
00152         }
00153         else {
00154             $paging = true;
00155         }
00156         if (!empty($fields)) {
00157             foreach($fields as $field) {
00158                 $searchfield = data_get_field_from_id($field->id, $data);
00159                 //Get field data to build search sql with.  If paging is false, get from user.
00160                 //If paging is true, get data from $search_array which is obtained from the $SESSION (see line 116).
00161                 if(!$paging) {
00162                     $val = $searchfield->parse_search_field();
00163                 } else {
00164                     //Set value from session if there is a value @ the required index.
00165                     if (isset($search_array[$field->id])) {
00166                         $val = $search_array[$field->id]->data;
00167                     } else {             //If there is not an entry @ the required index, set value to blank.
00168                         $val = '';
00169                     }
00170                 }
00171                 if (!empty($val)) {
00172                     $search_array[$field->id] = new stdClass();
00173                     list($search_array[$field->id]->sql, $search_array[$field->id]->params) = $searchfield->generate_sql('c'.$field->id, $val);
00174                     $search_array[$field->id]->data = $val;
00175                     $vals[] = $val;
00176                 } else {
00177                     // clear it out
00178                     unset($search_array[$field->id]);
00179                 }
00180             }
00181         }
00182 
00183         if (!$paging) {
00184             // name searching
00185             $fn = optional_param('u_fn', '', PARAM_NOTAGS);
00186             $ln = optional_param('u_ln', '', PARAM_NOTAGS);
00187         } else {
00188             $fn = isset($search_array[DATA_FIRSTNAME]) ? $search_array[DATA_FIRSTNAME]->data : '';
00189             $ln = isset($search_array[DATA_LASTNAME]) ? $search_array[DATA_LASTNAME]->data : '';
00190         }
00191         if (!empty($fn)) {
00192             $search_array[DATA_FIRSTNAME] = new stdClass();
00193             $search_array[DATA_FIRSTNAME]->sql    = '';
00194             $search_array[DATA_FIRSTNAME]->params = array();
00195             $search_array[DATA_FIRSTNAME]->field  = 'u.firstname';
00196             $search_array[DATA_FIRSTNAME]->data   = $fn;
00197             $vals[] = $fn;
00198         } else {
00199             unset($search_array[DATA_FIRSTNAME]);
00200         }
00201         if (!empty($ln)) {
00202             $search_array[DATA_LASTNAME] = new stdClass();
00203             $search_array[DATA_LASTNAME]->sql     = '';
00204             $search_array[DATA_LASTNAME]->params = array();
00205             $search_array[DATA_LASTNAME]->field   = 'u.lastname';
00206             $search_array[DATA_LASTNAME]->data    = $ln;
00207             $vals[] = $ln;
00208         } else {
00209             unset($search_array[DATA_LASTNAME]);
00210         }
00211 
00212         $SESSION->dataprefs[$data->id]['search_array'] = $search_array;     // Make it sticky
00213 
00214         // in case we want to switch to simple search later - there might be multiple values there ;-)
00215         if ($vals) {
00216             $val = reset($vals);
00217             if (is_string($val)) {
00218                 $search = $val;
00219             }
00220         }
00221 
00222     } else {
00223         $search = optional_param('search', $SESSION->dataprefs[$data->id]['search'], PARAM_NOTAGS);
00224         //Paging variable not used for standard search. Set it to null.
00225         $paging = NULL;
00226     }
00227 
00228     // Disable search filters if $filter is not true:
00229     if (! $filter) {
00230         $search = '';
00231     }
00232 
00233     $textlib = textlib_get_instance();
00234     if ($textlib->strlen($search) < 2) {
00235         $search = '';
00236     }
00237     $SESSION->dataprefs[$data->id]['search'] = $search;   // Make it sticky
00238 
00239     $sort = optional_param('sort', $SESSION->dataprefs[$data->id]['sort'], PARAM_INT);
00240     $SESSION->dataprefs[$data->id]['sort'] = $sort;       // Make it sticky
00241 
00242     $order = (optional_param('order', $SESSION->dataprefs[$data->id]['order'], PARAM_ALPHA) == 'ASC') ? 'ASC': 'DESC';
00243     $SESSION->dataprefs[$data->id]['order'] = $order;     // Make it sticky
00244 
00245 
00246     $oldperpage = get_user_preferences('data_perpage_'.$data->id, 10);
00247     $perpage = optional_param('perpage', $oldperpage, PARAM_INT);
00248 
00249     if ($perpage < 2) {
00250         $perpage = 2;
00251     }
00252     if ($perpage != $oldperpage) {
00253         set_user_preference('data_perpage_'.$data->id, $perpage);
00254     }
00255 
00256     add_to_log($course->id, 'data', 'view', "view.php?id=$cm->id", $data->id, $cm->id);
00257 
00258 
00259     $urlparams = array('d' => $data->id);
00260     if ($record) {
00261         $urlparams['rid'] = $record->id;
00262     }
00263     if ($page) {
00264         $urlparams['page'] = $page;
00265     }
00266     if ($mode) {
00267         $urlparams['mode'] = $mode;
00268     }
00269     if ($filter) {
00270         $urlparams['filter'] = $filter;
00271     }
00272 // Initialize $PAGE, compute blocks
00273     $PAGE->set_url('/mod/data/view.php', $urlparams);
00274 
00275     if (($edit != -1) and $PAGE->user_allowed_editing()) {
00276         $USER->editing = $edit;
00277     }
00278 
00279     $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
00280 
00282     $meta = '';
00283     if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
00284         $rsstitle = $courseshortname . ': %fullname%';
00285         rss_add_http_header($context, 'mod_data', $data, $rsstitle);
00286     }
00287     if ($data->csstemplate) {
00288         $PAGE->requires->css('/mod/data/css.php?d='.$data->id);
00289     }
00290     if ($data->jstemplate) {
00291         $PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
00292     }
00293 
00294     // Mark as viewed
00295     $completion = new completion_info($course);
00296     $completion->set_module_viewed($cm);
00297 
00299     // Note: MDL-19010 there will be further changes to printing header and blocks.
00300     // The code will be much nicer than this eventually.
00301     $title = $courseshortname.': ' . format_string($data->name);
00302 
00303     if ($PAGE->user_allowed_editing()) {
00304         $buttons = '<table><tr><td><form method="get" action="view.php"><div>'.
00305             '<input type="hidden" name="id" value="'.$cm->id.'" />'.
00306             '<input type="hidden" name="edit" value="'.($PAGE->user_is_editing()?'off':'on').'" />'.
00307             '<input type="submit" value="'.get_string($PAGE->user_is_editing()?'blockseditoff':'blocksediton').'" /></div></form></td></tr></table>';
00308         $PAGE->set_button($buttons);
00309     }
00310 
00311     if ($mode == 'asearch') {
00312         $PAGE->navbar->add(get_string('search'));
00313     }
00314 
00315     $PAGE->set_title($title);
00316     $PAGE->set_heading($course->fullname);
00317 
00318     echo $OUTPUT->header();
00319 
00321     $returnurl = $CFG->wwwroot . '/mod/data/view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).'&amp;order='.s($order).'&amp;';
00322     groups_print_activity_menu($cm, $returnurl);
00323     $currentgroup = groups_get_activity_group($cm);
00324     $groupmode = groups_get_activity_groupmode($cm);
00325 
00326     // detect entries not approved yet and show hint instead of not found error
00327     if ($record and $data->approval and !$record->approved and $record->userid != $USER->id and !has_capability('mod/data:manageentries', $context)) {
00328         if (!$currentgroup or $record->groupid == $currentgroup or $record->groupid == 0) {
00329             print_error('notapproved', 'data');
00330         }
00331     }
00332 
00333     echo $OUTPUT->heading(format_string($data->name));
00334 
00335     // Do we need to show a link to the RSS feed for the records?
00336     //this links has been Settings (database activity administration) block
00337     /*if (!empty($CFG->enablerssfeeds) && !empty($CFG->data_enablerssfeeds) && $data->rssarticles > 0) {
00338         echo '<div style="float:right;">';
00339         rss_print_link($context->id, $USER->id, 'mod_data', $data->id, get_string('rsstype'));
00340         echo '</div>';
00341         echo '<div style="clear:both;"></div>';
00342     }*/
00343 
00344     if ($data->intro and empty($page) and empty($record) and $mode != 'single') {
00345         $options = new stdClass();
00346         $options->noclean = true;
00347         echo $OUTPUT->box(format_module_intro('data', $data, $cm->id), 'generalbox', 'intro');
00348     }
00349 
00351 
00352     if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) {
00353         if ($confirm = optional_param('confirm',0,PARAM_INT)) {
00354             if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) {   // Need to check this is valid
00355                 if ($deleterecord->dataid == $data->id) {                       // Must be from this database
00356                     if ($contents = $DB->get_records('data_content', array('recordid'=>$deleterecord->id))) {
00357                         foreach ($contents as $content) {  // Delete files or whatever else this field allows
00358                             if ($field = data_get_field_from_id($content->fieldid, $data)) { // Might not be there
00359                                 $field->delete_content($content->recordid);
00360                             }
00361                         }
00362                     }
00363                     $DB->delete_records('data_content', array('recordid'=>$deleterecord->id));
00364                     $DB->delete_records('data_records', array('id'=>$deleterecord->id));
00365 
00366                     add_to_log($course->id, 'data', 'record delete', "view.php?id=$cm->id", $data->id, $cm->id);
00367 
00368                     echo $OUTPUT->notification(get_string('recorddeleted','data'), 'notifysuccess');
00369                 }
00370             }
00371 
00372         } else {   // Print a confirmation page
00373             if ($deleterecord = $DB->get_record('data_records', array('id'=>$delete))) {   // Need to check this is valid
00374                 if ($deleterecord->dataid == $data->id) {                       // Must be from this database
00375                     $deletebutton = new single_button(new moodle_url('/mod/data/view.php?d='.$data->id.'&delete='.$delete.'&confirm=1'), get_string('delete'), 'post');
00376                     echo $OUTPUT->confirm(get_string('confirmdeleterecord','data'),
00377                             $deletebutton, 'view.php?d='.$data->id);
00378 
00379                     $records[] = $deleterecord;
00380                     echo data_print_template('singletemplate', $records, $data, '', 0, true);
00381 
00382                     echo $OUTPUT->footer();
00383                     exit;
00384                 }
00385             }
00386         }
00387     }
00388 
00389 
00390 //if data activity closed dont let students in
00391 $showactivity = true;
00392 if (!has_capability('mod/data:manageentries', $context)) {
00393     $timenow = time();
00394     if (!empty($data->timeavailablefrom) && $data->timeavailablefrom > $timenow) {
00395         echo $OUTPUT->notification(get_string('notopenyet', 'data', userdate($data->timeavailablefrom)));
00396         $showactivity = false;
00397     } else if (!empty($data->timeavailableto) && $timenow > $data->timeavailableto) {
00398         echo $OUTPUT->notification(get_string('expired', 'data', userdate($data->timeavailableto)));
00399         $showactivity = false;
00400     }
00401 }
00402 
00403 if ($showactivity) {
00404     // Print the tabs
00405     if ($record or $mode == 'single') {
00406         $currenttab = 'single';
00407     } elseif($mode == 'asearch') {
00408         $currenttab = 'asearch';
00409     }
00410     else {
00411         $currenttab = 'list';
00412     }
00413     include('tabs.php');
00414 
00415     if ($mode == 'asearch') {
00416         $maxcount = 0;
00417 
00418     } else {
00420         $params = array(); // named params array
00421 
00422         $approvecap = has_capability('mod/data:approve', $context);
00423 
00424         if ($approve && confirm_sesskey() && $approvecap) {
00425             if ($approverecord = $DB->get_record('data_records', array('id'=>$approve))) {   // Need to check this is valid
00426                 if ($approverecord->dataid == $data->id) {                       // Must be from this database
00427                     $newrecord = new stdClass();
00428                     $newrecord->id = $approverecord->id;
00429                     $newrecord->approved = 1;
00430                     $DB->update_record('data_records', $newrecord);
00431                     echo $OUTPUT->notification(get_string('recordapproved','data'), 'notifysuccess');
00432                 }
00433             }
00434         }
00435 
00436          $numentries = data_numentries($data);
00438         if ($data->requiredentries > 0 && $numentries < $data->requiredentries && !has_capability('mod/data:manageentries', $context)) {
00439             $data->entriesleft = $data->requiredentries - $numentries;
00440             $strentrieslefttoadd = get_string('entrieslefttoadd', 'data', $data);
00441             echo $OUTPUT->notification($strentrieslefttoadd);
00442         }
00443 
00445         $requiredentries_allowed = true;
00446         if ($data->requiredentriestoview > 0 && $numentries < $data->requiredentriestoview && !has_capability('mod/data:manageentries', $context)) {
00447             $data->entrieslefttoview = $data->requiredentriestoview - $numentries;
00448             $strentrieslefttoaddtoview = get_string('entrieslefttoaddtoview', 'data', $data);
00449             echo $OUTPUT->notification($strentrieslefttoaddtoview);
00450             $requiredentries_allowed = false;
00451         }
00452 
00454         if (!$approvecap && $data->approval) {
00455             if (isloggedin()) {
00456                 $approveselect = ' AND (r.approved=1 OR r.userid=:myid1) ';
00457                 $params['myid1'] = $USER->id;
00458             } else {
00459                 $approveselect = ' AND r.approved=1 ';
00460             }
00461         } else {
00462             $approveselect = ' ';
00463         }
00464 
00465         if ($currentgroup) {
00466             $groupselect = " AND (r.groupid = :currentgroup OR r.groupid = 0)";
00467             $params['currentgroup'] = $currentgroup;
00468         } else {
00469             $groupselect = ' ';
00470         }
00471 
00472         // Init some variables to be used by advanced search
00473         $advsearchselect = '';
00474         $advwhere        = '';
00475         $advtables       = '';
00476         $advparams       = array();
00477 
00479         if ($sort <= 0 or !$sortfield = data_get_field_from_id($sort, $data)) {
00480 
00481             switch ($sort) {
00482                 case DATA_LASTNAME:
00483                     $ordering = "u.lastname $order, u.firstname $order";
00484                     break;
00485                 case DATA_FIRSTNAME:
00486                     $ordering = "u.firstname $order, u.lastname $order";
00487                     break;
00488                 case DATA_APPROVED:
00489                     $ordering = "r.approved $order, r.timecreated $order";
00490                     break;
00491                 case DATA_TIMEMODIFIED:
00492                     $ordering = "r.timemodified $order";
00493                     break;
00494                 case DATA_TIMEADDED:
00495                 default:
00496                     $sort     = 0;
00497                     $ordering = "r.timecreated $order";
00498             }
00499 
00500             $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname';
00501             $count = ' COUNT(DISTINCT c.recordid) ';
00502             $tables = '{data_content} c,{data_records} r, {data_content} cs, {user} u ';
00503             $where =  'WHERE c.recordid = r.id
00504                          AND r.dataid = :dataid
00505                          AND r.userid = u.id
00506                          AND cs.recordid = r.id ';
00507             $params['dataid'] = $data->id;
00508             $sortorder = ' ORDER BY '.$ordering.', r.id ASC ';
00509             $searchselect = '';
00510 
00511             // If requiredentries is not reached, only show current user's entries
00512             if (!$requiredentries_allowed) {
00513                 $where .= ' AND u.id = :myid2 ';
00514                 $params['myid2'] = $USER->id;
00515             }
00516 
00517             if (!empty($advanced)) {                                                  //If advanced box is checked.
00518                 $i = 0;
00519                 foreach($search_array as $key => $val) {                              //what does $search_array hold?
00520                     if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) {
00521                         $i++;
00522                         $searchselect .= " AND ".$DB->sql_like($val->field, ":search_flname_$i", false);
00523                         $params['search_flname_'.$i] = "%$val->data%";
00524                         continue;
00525                     }
00526                     $advtables .= ', {data_content} c'.$key.' ';
00527                     $advwhere .= ' AND c'.$key.'.recordid = r.id';
00528                     $advsearchselect .= ' AND ('.$val->sql.') ';
00529                     $advparams = array_merge($advparams, $val->params);
00530                 }
00531             } else if ($search) {
00532                 $searchselect = " AND (".$DB->sql_like('cs.content', ':search1', false)." OR ".$DB->sql_like('u.firstname', ':search2', false)." OR ".$DB->sql_like('u.lastname', ':search3', false)." ) ";
00533                 $params['search1'] = "%$search%";
00534                 $params['search2'] = "%$search%";
00535                 $params['search3'] = "%$search%";
00536             } else {
00537                 $searchselect = ' ';
00538             }
00539 
00540         } else {
00541 
00542             $sortcontent = $DB->sql_compare_text('c.' . $sortfield->get_sort_field());
00543             $sortcontentfull = $sortfield->get_sort_sql($sortcontent);
00544 
00545             $what = ' DISTINCT r.id, r.approved, r.timecreated, r.timemodified, r.userid, u.firstname, u.lastname, ' . $sortcontentfull . ' AS _order ';
00546             $count = ' COUNT(DISTINCT c.recordid) ';
00547             $tables = '{data_content} c, {data_records} r, {data_content} cs, {user} u ';
00548             $where =  'WHERE c.recordid = r.id
00549                          AND c.fieldid = :sort
00550                          AND r.dataid = :dataid
00551                          AND r.userid = u.id
00552                          AND cs.recordid = r.id ';
00553             $params['dataid'] = $data->id;
00554             $params['sort'] = $sort;
00555             $sortorder = ' ORDER BY _order '.$order.' , r.id ASC ';
00556             $searchselect = '';
00557 
00558             // If requiredentries is not reached, only show current user's entries
00559             if (!$requiredentries_allowed) {
00560                 $where .= ' AND u.id = ' . $USER->id;
00561                 $params['myid2'] = $USER->id;
00562             }
00563 
00564             if (!empty($advanced)) {                                                  //If advanced box is checked.
00565                 foreach($search_array as $key => $val) {                              //what does $search_array hold?
00566                     if ($key == DATA_FIRSTNAME or $key == DATA_LASTNAME) {
00567                         $i++;
00568                         $searchselect .= " AND ".$DB->sql_like($val->field, ":search_flname_$i", false);
00569                         $params['search_flname_'.$i] = "%$val->data%";
00570                         continue;
00571                     }
00572                     $advtables .= ', {data_content} c'.$key.' ';
00573                     $advwhere .= ' AND c'.$key.'.recordid = r.id AND c'.$key.'.fieldid = '.$key;
00574                     $advsearchselect .= ' AND ('.$val->sql.') ';
00575                     $advparams = array_merge($advparams, $val->params);
00576                 }
00577             } else if ($search) {
00578                 $searchselect = " AND (".$DB->sql_like('cs.content', ':search1', false)." OR ".$DB->sql_like('u.firstname', ':search2', false)." OR ".$DB->sql_like('u.lastname', ':search3', false)." ) ";
00579                 $params['search1'] = "%$search%";
00580                 $params['search2'] = "%$search%";
00581                 $params['search3'] = "%$search%";
00582             } else {
00583                 $searchselect = ' ';
00584             }
00585         }
00586 
00588 
00589         $fromsql    = "FROM $tables $advtables $where $advwhere $groupselect $approveselect $searchselect $advsearchselect";
00590         $sqlselect  = "SELECT $what $fromsql $sortorder";
00591         $sqlcount   = "SELECT $count $fromsql";   // Total number of records when searching
00592         $sqlmax     = "SELECT $count FROM $tables $where $groupselect $approveselect"; // number of all recoirds user may see
00593         $allparams  = array_merge($params, $advparams);
00594 
00596 
00597         $totalcount = $DB->count_records_sql($sqlcount, $allparams);
00598         if (empty($searchselect) && empty($advsearchselect)) {
00599             $maxcount = $totalcount;
00600         } else {
00601             $maxcount = $DB->count_records_sql($sqlmax, $params);
00602         }
00603 
00604         if ($record) {     // We need to just show one, so where is it in context?
00605             $nowperpage = 1;
00606             $mode = 'single';
00607 
00608             $page = 0;
00609             // TODO: Improve this because we are executing $sqlselect twice (here and some lines below)!
00610             if ($allrecordids = $DB->get_fieldset_sql($sqlselect, $allparams)) {
00611                 $page = (int)array_search($record->id, $allrecordids);
00612                 unset($allrecordids);
00613             }
00614 
00615         } else if ($mode == 'single') {  // We rely on ambient $page settings
00616             $nowperpage = 1;
00617 
00618         } else {
00619             $nowperpage = $perpage;
00620         }
00621 
00623 
00624         if (!$records = $DB->get_records_sql($sqlselect, $allparams, $page * $nowperpage, $nowperpage)) {
00625             // Nothing to show!
00626             if ($record) {         // Something was requested so try to show that at least (bug 5132)
00627                 if (has_capability('mod/data:manageentries', $context) || empty($data->approval) ||
00628                          $record->approved || (isloggedin() && $record->userid == $USER->id)) {
00629                     if (!$currentgroup || $record->groupid == $currentgroup || $record->groupid == 0) {
00630                         // OK, we can show this one
00631                         $records = array($record->id => $record);
00632                         $totalcount = 1;
00633                     }
00634                 }
00635             }
00636         }
00637 
00638         if (empty($records)) {
00639             if ($maxcount){
00640                 $a = new stdClass();
00641                 $a->max = $maxcount;
00642                 $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
00643                 echo $OUTPUT->notification(get_string('foundnorecords','data', $a));
00644             } else {
00645                 echo $OUTPUT->notification(get_string('norecords','data'));
00646             }
00647 
00648         } else { //  We have some records to print
00649 
00650             if ($maxcount != $totalcount) {
00651                 $a = new stdClass();
00652                 $a->num = $totalcount;
00653                 $a->max = $maxcount;
00654                 $a->reseturl = "view.php?id=$cm->id&amp;mode=$mode&amp;search=&amp;advanced=0";
00655                 echo $OUTPUT->notification(get_string('foundrecords', 'data', $a), 'notifysuccess');
00656             }
00657 
00658             if ($mode == 'single') { // Single template
00659                 $baseurl = 'view.php?d=' . $data->id . '&mode=single&';
00660                 if (!empty($search)) {
00661                     $baseurl .= 'filter=1&';
00662                 }
00663                 if (!empty($page)) {
00664                     $baseurl .= 'page=' . $page;
00665                 }
00666                 echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
00667 
00668                 if (empty($data->singletemplate)){
00669                     echo $OUTPUT->notification(get_string('nosingletemplate','data'));
00670                     data_generate_default_template($data, 'singletemplate', 0, false, false);
00671                 }
00672 
00673                 //data_print_template() only adds ratings for singletemplate which is why we're attaching them here
00674                 //attach ratings to data records
00675                 require_once($CFG->dirroot.'/rating/lib.php');
00676                 if ($data->assessed != RATING_AGGREGATE_NONE) {
00677                     $ratingoptions = new stdClass;
00678                     $ratingoptions->context = $context;
00679                     $ratingoptions->component = 'mod_data';
00680                     $ratingoptions->ratingarea = 'entry';
00681                     $ratingoptions->items = $records;
00682                     $ratingoptions->aggregate = $data->assessed;//the aggregation method
00683                     $ratingoptions->scaleid = $data->scale;
00684                     $ratingoptions->userid = $USER->id;
00685                     $ratingoptions->returnurl = $CFG->wwwroot.'/mod/data/'.$baseurl;
00686                     $ratingoptions->assesstimestart = $data->assesstimestart;
00687                     $ratingoptions->assesstimefinish = $data->assesstimefinish;
00688 
00689                     $rm = new rating_manager();
00690                     $records = $rm->get_ratings($ratingoptions);
00691                 }
00692 
00693                 data_print_template('singletemplate', $records, $data, $search, $page);
00694 
00695                 echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
00696 
00697             } else {                                  // List template
00698                 $baseurl = 'view.php?d='.$data->id.'&amp;';
00699                 //send the advanced flag through the URL so it is remembered while paging.
00700                 $baseurl .= 'advanced='.$advanced.'&amp;';
00701                 if (!empty($search)) {
00702                     $baseurl .= 'filter=1&amp;';
00703                 }
00704                 //pass variable to allow determining whether or not we are paging through results.
00705                 $baseurl .= 'paging='.$paging.'&amp;';
00706 
00707                 echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
00708 
00709                 if (empty($data->listtemplate)){
00710                     echo $OUTPUT->notification(get_string('nolisttemplate','data'));
00711                     data_generate_default_template($data, 'listtemplate', 0, false, false);
00712                 }
00713                 echo $data->listtemplateheader;
00714                 data_print_template('listtemplate', $records, $data, $search, $page);
00715                 echo $data->listtemplatefooter;
00716 
00717                 echo $OUTPUT->paging_bar($totalcount, $page, $nowperpage, $baseurl);
00718             }
00719 
00720         }
00721     }
00722 
00723     $search = trim($search);
00724     if (empty($records)) {
00725         $records = array();
00726     }
00727 
00728     if ($mode == '' && !empty($CFG->enableportfolios)) {
00729         require_once($CFG->libdir . '/portfoliolib.php');
00730         $button = new portfolio_add_button();
00731         $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id), '/mod/data/locallib.php');
00732         if (data_portfolio_caller::has_files($data)) {
00733             $button->set_formats(array(PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_LEAP2A)); // no plain html for us
00734         }
00735         echo $button->to_html(PORTFOLIO_ADD_FULL_FORM);
00736     }
00737 
00738     //Advanced search form doesn't make sense for single (redirects list view)
00739     if (($maxcount || $mode == 'asearch') && $mode != 'single') {
00740         data_print_preference_form($data, $perpage, $search, $sort, $order, $search_array, $advanced, $mode);
00741     }
00742 }
00743 
00744 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations