]> git.mjollnir.org Git - moodle.git/commitdiff
[MDL-8407] Make the default role required to export database records weaker (use...
authorrobertall <robertall>
Thu, 12 Jun 2008 13:55:46 +0000 (13:55 +0000)
committerrobertall <robertall>
Thu, 12 Jun 2008 13:55:46 +0000 (13:55 +0000)
mod/data/export.php
mod/data/lib.php
mod/data/tabs.php

index a3e8fdba1b119217dd1035d87dc39cab68242118..3f1ec03bb17e159e8d3d0cc10e63b1c2183c1647 100644 (file)
@@ -30,7 +30,7 @@ if (! $context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
 }
 
 require_login($course->id, false, $cm);
-require_capability('mod/data:managetemplates', $context);
+require_capability(DATA_CAP_EXPORT, $context);
 
 // get fields for this database
 $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id');
@@ -67,7 +67,6 @@ if($mform->is_cancelled()) {
     $groupmode = groups_get_activity_groupmode($cm);
     $currenttab = 'export';
     include('tabs.php');
-
     $mform->display();
     print_footer();
     die;
@@ -80,7 +79,7 @@ foreach($fields as $key => $field) {
     if(empty($formdata['field_'.$field->field->id])) {
         // ignore values we aren't exporting
         unset($fields[$key]);
-    } else{
+    } else {
         $exportdata[0][] = $field->field->name;
     }
 }
index dfd631ce5a37b87362c2d8330e49eb22fe672609..1a3a44372bcb5159e4f346f6b3a856715740e8fb 100755 (executable)
 // Some constants
 define ('DATA_MAX_ENTRIES', 50);
 define ('DATA_PERPAGE_SINGLE', 1);
-
 define ('DATA_FIRSTNAME', -1);
 define ('DATA_LASTNAME', -2);
 define ('DATA_APPROVED', -3);
 define ('DATA_TIMEADDED', 0);
 define ('DATA_TIMEMODIFIED', -4);
 
+define ('DATA_CAP_EXPORT', 'mod/data:viewalluserpresets');
+// Users having assigned the default role "Non-editing teacher" can export database records
+// Using the mod/data capability "viewalluserpresets" for Moodle 1.9.x, so no change in the role system is required.
+// In Moodle >= 2, new roles may be introduced and used instead. 
+
 class data_field_base {     // Base class for Database Field Types (see field/*/field.class.php)
 
     var $type = 'unknown';  // Subclasses must override the type with their name
@@ -44,12 +48,14 @@ class data_field_base {     // Base class for Database Field Types (see field/*/
 
 // Constructor function
     function data_field_base($field=0, $data=0) {   // Field or data or both, each can be id or object
+<<<<<<< lib.php
         global $DB;
 
+=======
+>>>>>>> 1.137.2.35
         if (empty($field) && empty($data)) {
             error('Programmer error: You must specify field and/or data when defining field class. ');
         }
-
         if (!empty($field)) {
             if (is_object($field)) {
                 $this->field = $field;  // Programmer knows what they are doing, we hope
@@ -62,7 +68,6 @@ class data_field_base {     // Base class for Database Field Types (see field/*/
                 }
             }
         }
-
         if (empty($this->data)) {         // We need to define this properly
             if (!empty($data)) {
                 if (is_object($data)) {
@@ -74,7 +79,6 @@ class data_field_base {     // Base class for Database Field Types (see field/*/
                 error('Data id or object must be provided to field class');
             }
         }
-
         if (empty($this->field)) {         // We need to define some default values
             $this->define_default_field();
         }
@@ -95,11 +99,10 @@ class data_field_base {     // Base class for Database Field Types (see field/*/
         $this->field->param3 = '';
         $this->field->name = '';
         $this->field->description = '';
-
         return true;
     }
 
-// Set up the field object according to data in an object.  Now is the time to clean it!
+// Set up the field object according to data in an object. Now is the time to clean it!
     function define_field($data) {
         $this->field->type        = $this->type;
         $this->field->dataid      = $this->data->id;
@@ -243,9 +246,13 @@ class data_field_base {     // Base class for Database Field Types (see field/*/
     }
 
 // Update the content of one data field in the data_content table
+<<<<<<< lib.php
     function update_content($recordid, $value, $name=''){
         global $DB;
 
+=======
+    function update_content($recordid, $value, $name='') {
+>>>>>>> 1.137.2.35
         $content = new object();
         $content->fieldid = $this->field->id;
         $content->recordid = $recordid;
@@ -342,12 +349,12 @@ class data_field_base {     // Base class for Database Field Types (see field/*/
 }
 
 
-/*****************************************************************************
-/* Given a template and a dataid, generate a default case template               *
- * input @param template - addtemplate, singletemplate, listtempalte, rsstemplate*
- *       @param dataid                                                       *
- * output null                                                               *
- *****************************************************************************/
+/*
+/* Given a template and a dataid, generate a default case template
+ * input @param template - addtemplate, singletemplate, listtempalte, rsstemplate
+ *       @param dataid
+ * output null
+ */
 function data_generate_default_template(&$data, $template, $recordid=0, $form=false, $update=true) {
     global $DB;
 
@@ -492,14 +499,18 @@ function data_append_new_field_to_templates($data, $newfieldname) {
 
 
 /************************************************************************
- * given a field name *
+ * given a field name                                                   *
  * this function creates an instance of the particular subfield class   *
  ************************************************************************/
 function data_get_field_from_name($name, $data){
+<<<<<<< lib.php
     global $DB;
 
     $field = $DB->get_record('data_fields', array('name'=>$name));
 
+=======
+    $field = get_record('data_fields','name',$name);
+>>>>>>> 1.137.2.35
     if ($field) {
         return data_get_field($field, $data);
     } else {
@@ -508,14 +519,19 @@ function data_get_field_from_name($name, $data){
 }
 
 /************************************************************************
- * given a field id *
+ * given a field id                                                     *
  * this function creates an instance of the particular subfield class   *
  ************************************************************************/
+<<<<<<< lib.php
 function data_get_field_from_id($fieldid, $data){
     global $DB;
 
     $field = $DB->get_record('data_fields', array('id'=>$fieldid));
 
+=======
+function data_get_field_from_id($fieldid, $data) {
+    $field = get_record('data_fields','id',$fieldid);
+>>>>>>> 1.137.2.35
     if ($field) {
         return data_get_field($field, $data);
     } else {
@@ -524,12 +540,11 @@ function data_get_field_from_id($fieldid, $data){
 }
 
 /************************************************************************
- * given a field id *
+ * given a field id                                                     *
  * this function creates an instance of the particular subfield class   *
  ************************************************************************/
 function data_get_field_new($type, $data) {
     global $CFG;
-
     require_once($CFG->dirroot.'/mod/data/field/'.$type.'/field.class.php');
     $newfield = 'data_field_'.$type;
     $newfield = new $newfield(0, $data);
@@ -543,7 +558,6 @@ function data_get_field_new($type, $data) {
  ************************************************************************/
 function data_get_field($field, $data) {
     global $CFG;
-
     if ($field) {
         require_once('field/'.$field->type.'/field.class.php');
         $newfield = 'data_field_'.$field->type;
@@ -559,8 +573,12 @@ function data_get_field($field, $data) {
  * output bool                                                             *
  ***************************************************************************/
 function data_isowner($rid){
+<<<<<<< lib.php
     global $USER, $DB;
 
+=======
+    global $USER;
+>>>>>>> 1.137.2.35
     if (empty($USER->id)) {
         return false;
     }
@@ -577,10 +595,9 @@ function data_isowner($rid){
  * input object $data                                                  *
  * output bool                                                         *
  ***********************************************************************/
-function data_atmaxentries($data){
-    if (!$data->maxentries){
+function data_atmaxentries($data) {
+    if (!$data->maxentries) {
         return false;
-
     } else {
         return (data_numentries($data) >= $data->maxentries);
     }
@@ -592,10 +609,18 @@ function data_atmaxentries($data){
  * uses global $CFG, $USER                                            *
  * output int                                                         *
  **********************************************************************/
+<<<<<<< lib.php
 function data_numentries($data){
     global $USER, $CFG, $DB;
     $sql = 'SELECT COUNT(*) FROM {data_records} WHERE dataid=? AND userid=?';
     return $DB->count_records_sql($sql, array($data->id, $USER->id));
+=======
+function data_numentries($data) {
+    global $USER;
+    global $CFG;
+    $sql = 'SELECT COUNT(*) FROM '.$CFG->prefix.'data_records WHERE dataid='.$data->id.' AND userid='.$USER->id;
+    return count_records_sql($sql);
+>>>>>>> 1.137.2.35
 }
 
 /****************************************************************
@@ -604,12 +629,16 @@ function data_numentries($data){
  * input @param int $dataid, $groupid                           *
  * output bool                                                  *
  ****************************************************************/
+<<<<<<< lib.php
 function data_add_record($data, $groupid=0){
     global $USER, $DB;
 
+=======
+function data_add_record($data, $groupid=0) {
+    global $USER;
+>>>>>>> 1.137.2.35
     $cm = get_coursemodule_from_instance('data', $data->id);
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-
     $record = new object();
     $record->userid = $USER->id;
     $record->dataid = $data->id;
@@ -632,16 +661,20 @@ function data_add_record($data, $groupid=0){
  *       @param string $template                                   *
  * output bool                                                     *
  *******************************************************************/
+<<<<<<< lib.php
 function data_tags_check($dataid, $template) {
     global $DB;
 
+=======
+function data_tags_check($dataid, $template) {
+>>>>>>> 1.137.2.35
     // first get all the possible tags
     $fields = $DB->get_records('data_fields', array('dataid'=>$dataid));
     // then we generate strings to replace
     $tagsok = true; // let's be optimistic
-    foreach ($fields as $field){
+    foreach ($fields as $field) {
         $pattern="/\[\[".$field->name."\]\]/i";
-        if (preg_match_all($pattern, $template, $dummy)>1){
+        if (preg_match_all($pattern, $template, $dummy)>1) {
             $tagsok = false;
             notify ('[['.$field->name.']] - '.get_string('multipletags','data'));
         }
@@ -654,20 +687,27 @@ function data_tags_check($dataid, $template) {
  * Adds an instance of a data                                           *
  ************************************************************************/
 function data_add_instance($data) {
+<<<<<<< lib.php
     global $CFG, $DB;
 
+=======
+    global $CFG;
+>>>>>>> 1.137.2.35
     if (empty($data->assessed)) {
         $data->assessed = 0;
     }
 
     $data->timemodified = time();
+<<<<<<< lib.php
 
     if (! $data->id = $DB->insert_record('data', $data)) {
+=======
+    if (! $data->id = insert_record('data', $data)) {
+>>>>>>> 1.137.2.35
         return false;
     }
 
     data_grade_item_update($data);
-
     return $data->id;
 }
 
@@ -675,27 +715,32 @@ function data_add_instance($data) {
  * updates an instance of a data                                        *
  ************************************************************************/
 function data_update_instance($data) {
+<<<<<<< lib.php
     global $CFG, $DB;
 
+=======
+    global $CFG;
+>>>>>>> 1.137.2.35
     $data->timemodified = time();
-    $data->id           = $data->instance;
+    $data->id = $data->instance;
 
     if (empty($data->assessed)) {
         $data->assessed = 0;
     }
-
     if (empty($data->notification)) {
         $data->notification = 0;
     }
+<<<<<<< lib.php
 
     if (! $DB->update_record('data', $data)) {
+=======
+    if (! update_record('data', $data)) {
+>>>>>>> 1.137.2.35
         return false;
     }
 
     data_grade_item_update($data);
-
     return true;
-
 }
 
 /************************************************************************
@@ -704,21 +749,32 @@ function data_update_instance($data) {
 function data_delete_instance($id) {    // takes the dataid
     global $CFG, $DB;
 
+<<<<<<< lib.php
     if (! $data = $DB->get_record('data', array('id'=>$id))) {
+=======
+    global $CFG;
+    if (! $data = get_record('data', 'id', $id)) {
+>>>>>>> 1.137.2.35
         return false;
     }
 
     // Delete all the associated information
-
     // get all the records in this data
     $sql = 'SELECT c.* FROM {data_records} r LEFT JOIN {data_content} c ON c.recordid = r.id WHERE r.dataid =?';
 
+<<<<<<< lib.php
     if ($contents = $DB->get_records_sql($sql, array($id))){
 
         foreach($contents as $content){
 
             $field = $DB->get_record('data_fields', array('id'=>$content->fieldid));
             if ($g = data_get_field($field, $data)){
+=======
+    if ($contents = get_records_sql($sql)) {
+        foreach($contents as $content) {
+            $field = get_record('data_fields','id',$content->fieldid);
+            if ($g = data_get_field($field, $data)) {
+>>>>>>> 1.137.2.35
                 $g->delete_content_files($id, $content->recordid, $content->content);
             }
             //delete the content itself
@@ -731,11 +787,14 @@ function data_delete_instance($id) {    // takes the dataid
     $DB->delete_records('data_fields', array('dataid'=>$id));
 
     // Delete the instance itself
+<<<<<<< lib.php
 
     $result = $DB->delete_records('data', array('id'=>$id));
 
+=======
+    $result = delete_records('data', 'id', $id);
+>>>>>>> 1.137.2.35
     data_grade_item_delete($data);
-
     return $result;
 }
 
@@ -776,11 +835,16 @@ function data_user_complete($course, $user, $mod, $data) {
  * @return array array of grades, false if none
  */
 function data_get_user_grades($data, $userid=0) {
+<<<<<<< lib.php
     global $CFG, $DB;
 
     $user = $userid ? "AND u.id = :userid" : "";
     $params = array('userid'=>$userid, 'dataid'=>$data->id);
 
+=======
+    global $CFG;
+    $user = $userid ? "AND u.id = $userid" : "";
+>>>>>>> 1.137.2.35
     $sql = "SELECT u.id, u.id AS userid, avg(drt.rating) AS rawgrade
               FROM {user} u, {data_records} dr,
                    {data_ratings} drt
@@ -788,8 +852,12 @@ function data_get_user_grades($data, $userid=0) {
                    AND drt.userid != u.id AND dr.dataid = :dataid
                    $user
           GROUP BY u.id";
+<<<<<<< lib.php
 
     return $DB->get_records_sql($sql, $params);
+=======
+    return get_records_sql($sql);
+>>>>>>> 1.137.2.35
 }
 
 /**
@@ -805,17 +873,14 @@ function data_update_grades($data=null, $userid=0, $nullifnone=true) {
     if ($data != null) {
         if ($grades = data_get_user_grades($data, $userid)) {
             data_grade_item_update($data, $grades);
-
         } else if ($userid and $nullifnone) {
             $grade = new object();
             $grade->userid   = $userid;
             $grade->rawgrade = NULL;
             data_grade_item_update($data, $grade);
-
         } else {
             data_grade_item_update($data);
         }
-
     } else {
         $sql = "SELECT d.*, cm.idnumber as cmidnumber
                   FROM {data} d, {course_modules} cm, {modules} m
@@ -842,23 +907,25 @@ function data_update_grades($data=null, $userid=0, $nullifnone=true) {
  */
 function data_grade_item_update($data, $grades=NULL) {
     global $CFG;
+<<<<<<< lib.php
     require_once($CFG->libdir.'/gradelib.php');
 
+=======
+    if (!function_exists('grade_update')) { //workaround for buggy PHP versions
+        require_once($CFG->libdir.'/gradelib.php');
+    }
+>>>>>>> 1.137.2.35
     $params = array('itemname'=>$data->name, 'idnumber'=>$data->cmidnumber);
-
     if (!$data->assessed or $data->scale == 0) {
         $params['gradetype'] = GRADE_TYPE_NONE;
-
     } else if ($data->scale > 0) {
         $params['gradetype'] = GRADE_TYPE_VALUE;
         $params['grademax']  = $data->scale;
         $params['grademin']  = 0;
-
     } else if ($data->scale < 0) {
         $params['gradetype'] = GRADE_TYPE_SCALE;
         $params['scaleid']   = -$data->scale;
     }
-
     if ($grades  === 'reset') {
         $params['reset'] = true;
         $grades = NULL;
@@ -876,7 +943,6 @@ function data_grade_item_update($data, $grades=NULL) {
 function data_grade_item_delete($data) {
     global $CFG;
     require_once($CFG->libdir.'/gradelib.php');
-
     return grade_update('mod/data', $data->course, 'mod', 'data', $data->id, 0, NULL, array('deleted'=>1));
 }
 
@@ -886,6 +952,7 @@ function data_grade_item_delete($data) {
 function data_get_participants($dataid) {
 // Returns the users with data in one data
 // (users with records in data_records, data_comments and data_ratings)
+<<<<<<< lib.php
     global $CFG, $DB;
 
     $records = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
@@ -900,24 +967,44 @@ function data_get_participants($dataid) {
                                        FROM {user} u, {data_records} r, {data_ratings} a
                                       WHERE r.dataid = ? AND u.id = r.userid AND r.id = a.recordid", array($dataid));
 
+=======
+    global $CFG;
+    $records = get_records_sql("SELECT DISTINCT u.id, u.id
+                                FROM {$CFG->prefix}user u,
+                                     {$CFG->prefix}data_records r
+                                WHERE r.dataid = '$dataid'
+                                  AND u.id = r.userid");
+    $comments = get_records_sql("SELECT DISTINCT u.id, u.id
+                                 FROM {$CFG->prefix}user u,
+                                      {$CFG->prefix}data_records r,
+                                      {$CFG->prefix}data_comments c
+                                 WHERE r.dataid = '$dataid'
+                                   AND u.id = r.userid
+                                   AND r.id = c.recordid");
+    $ratings = get_records_sql("SELECT DISTINCT u.id, u.id
+                                FROM {$CFG->prefix}user u,
+                                     {$CFG->prefix}data_records r,
+                                     {$CFG->prefix}data_ratings a
+                                WHERE r.dataid = '$dataid'
+                                  AND u.id = r.userid
+                                  AND r.id = a.recordid");
+>>>>>>> 1.137.2.35
     $participants = array();
-
-    if ($records){
+    if ($records) {
         foreach ($records as $record) {
             $participants[$record->id] = $record;
         }
     }
-    if ($comments){
+    if ($comments) {
         foreach ($comments as $comment) {
             $participants[$comment->id] = $comment;
         }
     }
-    if ($ratings){
+    if ($ratings) {
         foreach ($ratings as $rating) {
             $participants[$rating->id] = $rating;
         }
     }
-
     return $participants;
 }
 
@@ -932,21 +1019,22 @@ function data_get_participants($dataid) {
  * output null                                                          *
  ************************************************************************/
 function data_print_template($template, $records, $data, $search='',$page=0, $return=false) {
+<<<<<<< lib.php
     global $CFG, $DB;
 
+=======
+    global $CFG;
+>>>>>>> 1.137.2.35
     $cm = get_coursemodule_from_instance('data', $data->id);
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-
     static $fields = NULL;
     static $isteacher;
     static $dataid = NULL;
-
     if (empty($dataid)) {
         $dataid = $data->id;
     } else if ($dataid != $data->id) {
         $fields = NULL;
     }
-
     if (empty($fields)) {
         $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id));
         foreach ($fieldrecords as $fieldrecord) {
@@ -954,23 +1042,18 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
         }
         $isteacher = has_capability('mod/data:managetemplates', $context);
     }
-
     if (empty($records)) {
         return;
     }
-
     foreach ($records as $record) {   // Might be just one for the single template
-
     // Replacing tags
         $patterns = array();
         $replacement = array();
-
     // Then we generate strings to replace for normal tags
         foreach ($fields as $field) {
             $patterns[]='[['.$field->field->name.']]';
             $replacement[] = highlight($search, $field->display_browse_field($record->id, $template));
         }
-
     // Replacing special tags (##Edit##, ##Delete##, ##More##)
         $patterns[]='##edit##';
         $patterns[]='##delete##';
@@ -1000,7 +1083,7 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
         $replacement [] = userdate($record->timemodified);
 
         $patterns[]='##approve##';
-        if (has_capability('mod/data:approve', $context) && ($data->approval) && (!$record->approved)){
+        if (has_capability('mod/data:approve', $context) && ($data->approval) && (!$record->approved)) {
             $replacement[] = '<span class="approve"><a href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&amp;approve='.$record->id.'&amp;sesskey='.sesskey().'"><img src="'.$CFG->pixpath.'/i/approve.gif" class="icon" alt="'.get_string('approve').'" /></a></span>';
         } else {
             $replacement[] = '';
@@ -1022,7 +1105,6 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
             return $newtext;
         } else {
             echo $newtext;
-
             // hack alert - return is always false in singletemplate anyway ;-)
             /**********************************
              *    Printing Ratings Form       *
@@ -1030,12 +1112,10 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
             if ($template == 'singletemplate') {    //prints ratings options
                 data_print_ratings($data, $record);
             }
-
             /**********************************
              *    Printing Ratings Form       *
              *********************************/
             if (($template == 'singletemplate') && ($data->comments)) {    //prints ratings options
-
                 data_print_comments($data, $record, $page);
             }
         }
@@ -1056,9 +1136,14 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
  *       @param string $search                                          *
  * output null                                                          *
  ************************************************************************/
+<<<<<<< lib.php
 function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC', $search_array = '', $advanced = 0, $mode= ''){
     global $CFG, $DB;
     
+=======
+function data_print_preference_form($data, $perpage, $search, $sort='', $order='ASC', $search_array = '', $advanced = 0, $mode= '') {
+    global $CFG;
+>>>>>>> 1.137.2.35
     $cm = get_coursemodule_from_instance('data', $data->id);
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     echo '<br /><div class="datapreferences">';
@@ -1134,7 +1219,6 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
         $checked = '';
     }
     print '
-            
         <script type="text/javascript">
         //<![CDATA[
         <!-- Start
@@ -1167,10 +1251,8 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
     echo '&nbsp;<input type="hidden" name="advanced" value="0" />';
     echo '&nbsp;<input type="checkbox" id="advancedcheckbox" name="advanced" value="1" '.$checked.' onchange="showHideAdvSearch(this.checked);" /><label for="advancedcheckbox">'.get_string('advancedsearch', 'data').'</label>';
     echo '&nbsp;<input type="submit" value="'.get_string('savesettings','data').'" />';
-    
     echo '<br />';
     echo '<div class="dataadvancedsearch" id="data_adv_form" style="display: ';
-    
     if ($advanced) {
         echo 'inline';
     }
@@ -1179,7 +1261,6 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
     }
     echo ';margin-left:auto;margin-right:auto;" >';
     echo '<table class="boxaligncenter">';
-    
     // print ASC or DESC
     echo '<tr><td colspan="2">&nbsp;</td></tr>';
     $i = 0;
@@ -1193,7 +1274,6 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
     static $fields = NULL;
     static $isteacher;
     static $dataid = NULL;
-
     if (empty($dataid)) {
         $dataid = $data->id;
     } else if ($dataid != $data->id) {
@@ -1205,7 +1285,6 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
         foreach ($fieldrecords as $fieldrecord) {
             $fields[]= data_get_field($fieldrecord, $data);
         }
-
         $isteacher = has_capability('mod/data:managetemplates', $context);
     }
 
@@ -1234,14 +1313,12 @@ function data_print_preference_form($data, $perpage, $search, $sort='', $order='
 
     // actual replacement of the tags
     $newtext = preg_replace($patterns, $replacement, $data->asearchtemplate);
-
     $options = new object();
     $options->para=false;
     $options->noclean=true;
     echo '<tr><td>';
     echo format_text($newtext, FORMAT_HTML, $options);
     echo '</td></tr>';
-
     echo '<tr><td colspan="4" style="text-align: center;"><br/><input type="submit" value="'.get_string('savesettings','data').'" /><input type="submit" name="resetadv" value="'.get_string('resetsettings','data').'" /></td></tr>';
     echo '</table>';
     echo '</div>';
@@ -1255,25 +1332,20 @@ function data_print_ratings($data, $record) {
 
     $cm = get_coursemodule_from_instance('data', $data->id);
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-
     if ($data->assessed and !empty($USER->id) and (has_capability('mod/data:rate', $context) or has_capability('mod/data:viewrating', $context) or data_isowner($record->id))) {
         if ($ratingsscale = make_grades_menu($data->scale)) {
             $ratingsmenuused = false;
-
             echo '<div class="ratings" style="text-align:center">';
             echo '<form id="form" method="post" action="rate.php">';
             echo '<input type="hidden" name="dataid" value="'.$data->id.'" />';
-
             if (has_capability('mod/data:rate', $context) and !data_isowner($record->id)) {
                 data_print_ratings_mean($record->id, $ratingsscale, has_capability('mod/data:viewrating', $context));
                 echo '&nbsp;';
                 data_print_rating_menu($record->id, $USER->id, $ratingsscale);
                 $ratingsmenuused = true;
-
             } else {
                 data_print_ratings_mean($record->id, $ratingsscale, true);
             }
-
             if ($data->scale < 0) {
                 if ($scale = $DB->get_record('scale', array('id'=>abs($data->scale)))) {
                     print_scale_menu_helpbutton($data->course, $scale );
@@ -1294,15 +1366,11 @@ function data_print_ratings_mean($recordid, $scale, $link=true) {
 // Print the multiple ratings on a post given to the current user by others.
 // Scale is an array of ratings
     static $strrate;
-
     $mean = data_get_ratings_mean($recordid, $scale);
-
     if ($mean !== "") {
-
         if (empty($strratings)) {
             $strratings = get_string("ratings", "data");
         }
-
         echo "$strratings: ";
         if ($link) {
             link_to_popup_window ("/mod/data/report.php?id=$recordid", "ratings", $mean, 400, 600);
@@ -1317,8 +1385,11 @@ function data_get_ratings_mean($recordid, $scale, $ratings=NULL) {
 // Return the mean rating of a post given to the current user by others.
 // Scale is an array of possible ratings in the scale
 // Ratings is an optional simple array of actual ratings (just integers)
+<<<<<<< lib.php
     global $DB;
 
+=======
+>>>>>>> 1.137.2.35
     if (!$ratings) {
         $ratings = array();
         if ($rates = $DB->get_records("data_ratings", array("recordid"=>$recordid))) {
@@ -1327,22 +1398,17 @@ function data_get_ratings_mean($recordid, $scale, $ratings=NULL) {
             }
         }
     }
-
     $count = count($ratings);
-
     if ($count == 0) {
         return "";
-
     } else if ($count == 1) {
         return $scale[$ratings[0]];
-
     } else {
         $total = 0;
         foreach ($ratings as $rating) {
             $total += $rating;
         }
         $mean = round( ((float)$total/(float)$count) + 0.001);  // Little fudge factor so that 0.5 goes UP
-
         if (isset($scale[$mean])) {
             return $scale[$mean]." ($count)";
         } else {
@@ -1356,52 +1422,72 @@ function data_print_rating_menu($recordid, $userid, $scale) {
 // Print the menu of ratings as part of a larger form.
 // If the post has already been - set that value.
 // Scale is an array of ratings
+<<<<<<< lib.php
     global $DB;
 
+=======
+>>>>>>> 1.137.2.35
     static $strrate;
+<<<<<<< lib.php
 
     if (!$rating = $DB->get_record("data_ratings", array("userid"=>$userid, "recordid"=>$recordid))) {
+=======
+    if (!$rating = get_record("data_ratings", "userid", $userid, "recordid", $recordid)) {
+>>>>>>> 1.137.2.35
         $rating->rating = -999;
     }
-
     if (empty($strrate)) {
         $strrate = get_string("rate", "data");
     }
-
     choose_from_menu($scale, $recordid, $rating->rating, "$strrate...", '', -999);
 }
 
 
 function data_get_ratings($recordid, $sort="u.firstname ASC") {
 // Returns a list of ratings for a particular post - sorted.
+<<<<<<< lib.php
     global $CFG, $DB;
 
     return $DB->get_records_sql("SELECT u.*, r.rating
                                    FROM {data_ratings} r, {user} u
                                   WHERE r.recordid = ? AND r.userid = u.id
                                ORDER BY $sort", array($recordid));
+=======
+    global $CFG;
+    return get_records_sql("SELECT u.*, r.rating
+                              FROM {$CFG->prefix}data_ratings r,
+                                   {$CFG->prefix}user u
+                             WHERE r.recordid = $recordid
+                               AND r.userid = u.id
+                             ORDER BY $sort");
+>>>>>>> 1.137.2.35
 }
 
 
 // prints all comments + a text box for adding additional comment
 function data_print_comments($data, $record, $page=0, $mform=false) {
+<<<<<<< lib.php
     global $CFG, $DB;
 
+=======
+    global $CFG;
+>>>>>>> 1.137.2.35
     echo '<a name="comments"></a>';
+<<<<<<< lib.php
 
     if ($comments = $DB->get_records('data_comments', array('recordid'=>$record->id))) {
+=======
+    if ($comments = get_records('data_comments','recordid',$record->id)) {
+>>>>>>> 1.137.2.35
         foreach ($comments as $comment) {
             data_print_comment($data, $comment, $page);
         }
         echo '<br />';
     }
-
     if (!isloggedin() or isguest()) {
         return;
     }
-
     $editor = optional_param('addcomment', 0, PARAM_BOOL);
-
     if (!$mform and !$editor) {
         echo '<div class="newcomment" style="text-align:center">';
         echo '<a href="view.php?d='.$data->id.'&amp;page='.$page.'&amp;mode=single&amp;addcomment=1">'.get_string('addcomment', 'data').'</a>';
@@ -1420,22 +1506,27 @@ function data_print_comments($data, $record, $page=0, $mform=false) {
 
 // prints a single comment entry
 function data_print_comment($data, $comment, $page=0) {
+<<<<<<< lib.php
     global $USER, $CFG, $DB;
 
+=======
+    global $USER, $CFG;
+>>>>>>> 1.137.2.35
     $cm = get_coursemodule_from_instance('data', $data->id);
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-
     $stredit = get_string('edit');
     $strdelete = get_string('delete');
+<<<<<<< lib.php
 
     $user = $DB->get_record('user', array('id'=>$comment->userid));
 
+=======
+    $user = get_record('user','id',$comment->userid);
+>>>>>>> 1.137.2.35
     echo '<table cellspacing="0" align="center" width="50%" class="datacomment forumpost">';
-
     echo '<tr class="header"><td class="picture left">';
     print_user_picture($user, $data->course, $user->picture);
     echo '</td>';
-
     echo '<td class="topic starter" align="left"><div class="author">';
     $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
     $by = new object();
@@ -1444,7 +1535,6 @@ function data_print_comment($data, $comment, $page=0) {
     $by->date = userdate($comment->modified);
     print_string('bynameondate', 'data', $by);
     echo '</div></td></tr>';
-
     echo '<tr><td class="left side">';
     if ($groups = groups_get_all_groups($data->course, $comment->userid, $cm->groupingid)) {
         print_group_picture($groups, $data->course, false, false, true);
@@ -1453,14 +1543,11 @@ function data_print_comment($data, $comment, $page=0) {
     }
 
 // Actual content
-
     echo '</td><td class="content" align="left">'."\n";
-
     // Print whole message
     echo format_text($comment->content, $comment->format);
 
 // Commands
-
     echo '<div class="commands">';
     if (data_isowner($comment->recordid) or has_capability('mod/data:managecomments', $context)) {
             echo '<a href="'.$CFG->wwwroot.'/mod/data/comment.php?rid='.$comment->recordid.'&amp;mode=edit&amp;commentid='.$comment->id.'&amp;page='.$page.'">'.$stredit.'</a>';
@@ -1468,7 +1555,6 @@ function data_print_comment($data, $comment, $page=0) {
     }
 
     echo '</div>';
-
     echo '</td></tr></table>'."\n\n";
 }
 
@@ -1483,9 +1569,14 @@ function data_get_post_actions() {
 }
 
 function data_fieldname_exists($name, $dataid, $fieldid=0) {
+<<<<<<< lib.php
     global $CFG, $DB;
 
     $LIKE = $DB->sql_ilike();
+=======
+    global $CFG;
+    $LIKE = sql_ilike();
+>>>>>>> 1.137.2.35
     if ($fieldid) {
         return $DB->record_exists_sql("SELECT * FROM {data_fields} df
                                         WHERE df.name $LIKE ? AND df.dataid = ?
@@ -1504,7 +1595,6 @@ function data_convert_arrays_to_strings(&$fieldinput) {
                 $str .= $inner . ',';
             }
             $str = substr($str, 0, -1);
-
             $fieldinput->$key = $str;
         }
     }
@@ -1523,8 +1613,12 @@ function data_convert_arrays_to_strings(&$fieldinput) {
  * @return boolean      - data module was converted or not
  */
 function data_convert_to_roles($data, $teacherroles=array(), $studentroles=array(), $cmid=NULL) {
+<<<<<<< lib.php
     global $CFG, $DB;
 
+=======
+    global $CFG;
+>>>>>>> 1.137.2.35
     if (!isset($data->participants) && !isset($data->assesspublic)
             && !isset($data->groupmode)) {
         // We assume that this database has already been converted to use the
@@ -1532,7 +1626,6 @@ function data_convert_to_roles($data, $teacherroles=array(), $studentroles=array
         // upgraded to use Roles.
         return false;
     }
-
     if (empty($cmid)) {
         // We were not given the course_module id. Try to find it.
         if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
@@ -1544,7 +1637,6 @@ function data_convert_to_roles($data, $teacherroles=array(), $studentroles=array
     }
     $context = get_context_instance(CONTEXT_MODULE, $cmid);
 
-
     // $data->participants:
     // 1 - Only teachers can add entries
     // 3 - Teachers and students can add entries
@@ -1667,13 +1759,10 @@ function data_preset_name($shortname, $path) {
  */
 function data_get_available_presets($context) {
     global $CFG, $USER;
-
     $presets = array();
-
     if ($dirs = get_list_of_plugins('mod/data/preset')) {
         foreach ($dirs as $dir) {
             $fulldir = $CFG->dirroot.'/mod/data/preset/'.$dir;
-
             if (is_directory_a_preset($fulldir)) {
                 $preset = new object;
                 $preset->path = $fulldir;
@@ -1695,13 +1784,10 @@ function data_get_available_presets($context) {
     if ($userids = get_list_of_plugins('data/preset', '', $CFG->dataroot)) {
         foreach ($userids as $userid) {
             $fulldir = $CFG->dataroot.'/data/preset/'.$userid;
-
             if ($userid == 0 || $USER->id == $userid || has_capability('mod/data:viewalluserpresets', $context)) {
-
                 if ($dirs = get_list_of_plugins('data/preset/'.$userid, '', $CFG->dataroot)) {
                     foreach ($dirs as $dir) {
                         $fulldir = $CFG->dataroot.'/data/preset/'.$userid.'/'.$dir;
-
                         if (is_directory_a_preset($fulldir)) {
                             $preset = new object;
                             $preset->path = $fulldir;
@@ -1722,34 +1808,26 @@ function data_get_available_presets($context) {
             }
         }
     }
-
     return $presets;
 }
 
 
 function data_print_header($course, $cm, $data, $currenttab='') {
-
     global $CFG, $displaynoticegood, $displaynoticebad;
-
     $navigation = build_navigation('', $cm);
     print_header_simple($data->name, '', $navigation,
             '', '', true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
             navmenu($course, $cm));
-
     print_heading(format_string($data->name));
 
 // Groups needed for Add entry tab
     $currentgroup = groups_get_activity_group($cm);
     $groupmode = groups_get_activity_groupmode($cm);
-
     // Print the tabs
-
     if ($currenttab) {
         include('tabs.php');
     }
-
     // Print any notices
-
     if (!empty($displaynoticegood)) {
         notify($displaynoticegood, 'notifysuccess');    // good (usually green)
     } else if (!empty($displaynoticebad)) {
@@ -1759,16 +1837,13 @@ function data_print_header($course, $cm, $data, $currenttab='') {
 
 function data_user_can_add_entry($data, $currentgroup, $groupmode) {
     global $USER;
-
     if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
         error('Course Module ID was incorrect');
     }
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-
     if (!has_capability('mod/data:writeentry', $context) and !has_capability('mod/data:manageentries',$context)) {
         return false;
     }
-
     if (!$groupmode or has_capability('moodle/site:accessallgroups', $context)) {
         return true;
     }
@@ -1798,7 +1873,6 @@ function is_directory_a_preset($directory) {
               file_exists($directory.'csstemplate.css') &&
               file_exists($directory.'jstemplate.js') &&
               file_exists($directory.'preset.xml');
-
     return $status;
 }
 
@@ -1817,14 +1891,17 @@ function clean_preset($folder) {
 
     // optional
     @unlink($folder.'/asearchtemplate.html');
-
     return $status;
 }
 
 
 function data_presets_export($course, $cm, $data) {
+<<<<<<< lib.php
     global $CFG, $DB;
 
+=======
+    global $CFG;
+>>>>>>> 1.137.2.35
     /* Info Collected. Now need to make files in moodledata/temp */
     $tempfolder = $CFG->dataroot.'/temp';
     $singletemplate     = fopen($tempfolder.'/singletemplate.html', 'w');
@@ -2025,33 +2102,26 @@ class PresetImporter {
         if (!confirm_sesskey()) {
             error("Sesskey Invalid");
         }
-
         $strblank = get_string('blank', 'data');
         $strcontinue = get_string('continue');
         $strwarning = get_string('mappingwarning', 'data');
         $strfieldmappings = get_string('fieldmappings', 'data');
         $strnew = get_string('new');
-
         $sesskey = sesskey();
-
         list($settings, $newfields,  $currentfields) = $this->get_settings();
-
         echo '<div class="presetmapping"><form action="preset.php" method="post">';
         echo '<div>';
         echo '<input type="hidden" name="action" value="finishimport" />';
         echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
         echo '<input type="hidden" name="d" value="'.$this->data->id.'" />';
         echo '<input type="hidden" name="fullname" value="'.$this->userid.'/'.$this->shortname.'" />';
-
         if (!empty($currentfields) && !empty($newfields)) {
             echo "<h3>$strfieldmappings ";
             helpbutton('fieldmappings', $strfieldmappings, 'data');
             echo '</h3><table>';
-
             foreach ($newfields as $nid => $newfield) {
                 echo "<tr><td><label for=\"id_$newfield->name\">$newfield->name</label></td>";
                 echo '<td><select name="field_'.$nid.'" id="id_'.$newfield->name.'">';
-
                 $selected = false;
                 foreach ($currentfields as $cid => $currentfield) {
                     if ($currentfield->type == $newfield->type) {
@@ -2064,7 +2134,6 @@ class PresetImporter {
                         }
                     }
                 }
-
                 if ($selected)
                     echo '<option value="-1">-</option>';
                 else
@@ -2073,40 +2142,35 @@ class PresetImporter {
             }
             echo '</table>';
             echo "<p>$strwarning</p>";
-
         } else if (empty($newfields)) {
             error("New preset has no defined fields!");
         }
-
         echo '<div class="overwritesettings"><label for="overwritesettings">'.get_string('overwritesettings', 'data').'</label>';
         echo '<input id="overwritesettings" name="overwritesettings" type="checkbox" /></label></div>';
-
         echo '<input class="button" type="submit" value="'.$strcontinue.'" /></div></form></div>';
-
     }
 
     function import() {
+<<<<<<< lib.php
         global $CFG, $DB;
 
+=======
+        global $CFG;
+>>>>>>> 1.137.2.35
         list($settings, $newfields, $currentfields) = $this->get_settings();
         $preservedfields = array();
-
         $overwritesettings = optional_param('overwritesettings', 0, PARAM_BOOL);
-
         /* Maps fields and makes new ones */
         if (!empty($newfields)) {
             /* We require an injective mapping, and need to know what to protect */
             foreach ($newfields as $nid => $newfield) {
                 $cid = optional_param("field_$nid", -1, PARAM_INT);
                 if ($cid == -1) continue;
-
                 if (array_key_exists($cid, $preservedfields)) error("Not an injective map");
                 else $preservedfields[$cid] = true;
             }
-
             foreach ($newfields as $nid => $newfield) {
                 $cid = optional_param("field_$nid", -1, PARAM_INT);
-
                 /* A mapping. Just need to change field params. Data kept. */
                 if ($cid != -1 and isset($currentfields[$cid])) {
                     $fieldobject = data_get_field_from_id($currentfields[$cid]->id, $this->data);
@@ -2122,7 +2186,6 @@ class PresetImporter {
                 /* Make a new field */
                 else {
                     include_once("field/$newfield->type/field.class.php");
-
                     if (!isset($newfield->description)) {
                         $newfield->description = '';
                     }
@@ -2140,7 +2203,6 @@ class PresetImporter {
                 if (!array_key_exists($cid, $preservedfields)) {
                     /* Data not used anymore so wipe! */
                     print "Deleting field $currentfield->name<br />";
-
                     $id = $currentfield->id;
                     //Why delete existing data records and related comments/ratings??
 /*
@@ -2187,24 +2249,24 @@ class PresetImporter {
             }
         }
 
+<<<<<<< lib.php
         data_update_instance($this->data);
 
+=======
+        data_update_instance(addslashes_object($this->data));
+>>>>>>> 1.137.2.35
         if (strstr($this->folder, '/temp/')) {
         // Removes the temporary files
             clean_preset($this->folder); 
         }
-
         return true;
     }
 }
 
 function data_preset_path($course, $userid, $shortname) {
     global $USER, $CFG;
-
     $context = get_context_instance(CONTEXT_COURSE, $course->id);
-
     $userid = (int)$userid;
-
     if ($userid > 0 && ($userid == $USER->id || has_capability('mod/data:viewalluserpresets', $context))) {
         return $CFG->dataroot.'/data/preset/'.$userid.'/'.$shortname;
     } else if ($userid == 0) {
@@ -2212,7 +2274,6 @@ function data_preset_path($course, $userid, $shortname) {
     } else if ($userid < 0) {
         return $CFG->dataroot.'/temp/data/'.-$userid.'/'.$shortname;
     }
-
     return 'Does it disturb you that this code will never run?';
 }
 
@@ -2248,13 +2309,23 @@ function data_reset_course_form_defaults($course) {
  * @param string optional type
  */
 function data_reset_gradebook($courseid, $type='') {
+<<<<<<< lib.php
     global $CFG, $DB;
 
+=======
+    global $CFG;
+>>>>>>> 1.137.2.35
     $sql = "SELECT d.*, cm.idnumber as cmidnumber, d.course as courseid
+<<<<<<< lib.php
               FROM {data} d, {course_modules} cm, {modules} m
              WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id AND d.course=?";
 
     if ($datas = $DB->get_records_sql($sql, array($courseid))) {
+=======
+              FROM {$CFG->prefix}data d, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+             WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id AND d.course=$courseid";
+    if ($datas = get_records_sql($sql)) {
+>>>>>>> 1.137.2.35
         foreach ($datas as $data) {
             data_grade_item_update($data, 'reset');
         }
@@ -2270,32 +2341,48 @@ function data_reset_gradebook($courseid, $type='') {
 function data_reset_userdata($data) {
     global $CFG, $DB;
     require_once($CFG->libdir.'/filelib.php');
-
     $componentstr = get_string('modulenameplural', 'data');
     $status = array();
-
     $allrecordssql = "SELECT r.id
+<<<<<<< lib.php
                         FROM {data_records} r
                              INNER JOIN {data} d ON r.dataid = d.id
                        WHERE d.course = ?";
 
+=======
+                        FROM {$CFG->prefix}data_records r
+                             INNER JOIN {$CFG->prefix}data d ON r.dataid = d.id
+                       WHERE d.course = {$data->courseid}";
+>>>>>>> 1.137.2.35
     $alldatassql = "SELECT d.id
+<<<<<<< lib.php
                       FROM {data} d
                      WHERE d.course=?";
 
+=======
+                      FROM {$CFG->prefix}data d
+                     WHERE d.course={$data->courseid}";
+>>>>>>> 1.137.2.35
     // delete entries if requested
     if (!empty($data->reset_data)) {
+<<<<<<< lib.php
         $DB->delete_records_select('data_ratings', "recordid IN ($allrecordssql)", array($data->courseid));
         $DB->delete_records_select('data_comments', "recordid IN ($allrecordssql)", array($data->courseid));
         $DB->delete_records_select('data_content', "recordid IN ($allrecordssql)", array($data->courseid));
         $DB->delete_records_select('data_records', "dataid IN ($alldatassql)", array($data->courseid));
 
         if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
+=======
+        delete_records_select('data_ratings', "recordid IN ($allrecordssql)");
+        delete_records_select('data_comments', "recordid IN ($allrecordssql)");
+        delete_records_select('data_content', "recordid IN ($allrecordssql)");
+        delete_records_select('data_records', "dataid IN ($alldatassql)");
+        if ($datas = get_records_sql($alldatassql)) {
+>>>>>>> 1.137.2.35
             foreach ($datas as $dataid=>$unused) {
                 fulldelete("$CFG->dataroot/$data->courseid/moddata/data/$dataid");
             }
         }
-
         if (empty($data->reset_gradebook_grades)) {
             // remove all grades from gradebook
             data_reset_gradebook($data->courseid);
@@ -2306,11 +2393,18 @@ function data_reset_userdata($data) {
     // remove entries by users not enrolled into course
     if (!empty($data->reset_data_notenrolled)) {
         $recordssql = "SELECT r.id, r.userid, r.dataid, u.id AS userexists, u.deleted AS userdeleted
+<<<<<<< lib.php
                          FROM {data_records} r
                               JOIN {data} d ON r.dataid = d.id
                               LEFT JOIN {user} u ON r.userid = u.id
                         WHERE d.course = ? AND r.userid > 0";
 
+=======
+                         FROM {$CFG->prefix}data_records r
+                              INNER JOIN {$CFG->prefix}data d ON r.dataid = d.id
+                              LEFT OUTER JOIN {$CFG->prefix}user u ON r.userid = u.id
+                        WHERE d.course = {$data->courseid} AND r.userid > 0";
+>>>>>>> 1.137.2.35
         $course_context = get_context_instance(CONTEXT_COURSE, $data->courseid);
         $notenrolled = array();
         $fields = array();
@@ -2343,13 +2437,16 @@ function data_reset_userdata($data) {
 
     // remove all ratings
     if (!empty($data->reset_data_ratings)) {
+<<<<<<< lib.php
         $DB->delete_records_select('data_ratings', "recordid IN ($allrecordssql)", array($data->courseid));
 
+=======
+        delete_records_select('data_ratings', "recordid IN ($allrecordssql)");
+>>>>>>> 1.137.2.35
         if (empty($data->reset_gradebook_grades)) {
             // remove all grades from gradebook
             data_reset_gradebook($data->courseid);
         }
-
         $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallratings'), 'error'=>false);
     }
 
@@ -2364,7 +2461,6 @@ function data_reset_userdata($data) {
         shift_course_mod_dates('data', array('timeavailablefrom', 'timeavailableto', 'timeviewfrom', 'timeviewto'), $data->timeshift, $data->courseid);
         $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
     }
-
     return $status;
 }
 ?>
index b7b19a6ff77a9bd574a806390c1f1ce60a341641..f34dda26acdd9ba00b5207044e225e8db582f9a4 100755 (executable)
             $addstring = empty($editentry) ? get_string('add', 'data') : get_string('editentry', 'data');
             $row[] = new tabobject('add', $CFG->wwwroot.'/mod/data/edit.php?d='.$data->id, $addstring);
         }
+        if (has_capability(DATA_CAP_EXPORT, $context)) {
+            // The capability required to Export database records is centrally defined in 'lib.php'
+            // and should be weaker than those required to edit Templates, Fields and Presets. 
+            $row[] = new tabobject('export', $CFG->wwwroot.'/mod/data/export.php?d='.$data->id,
+                         get_string('export', 'data'));
+        }
         if (has_capability('mod/data:managetemplates', $context)) {
             if ($currenttab == 'list') {
                 $defaultemplate = 'listtemplate';
@@ -70,8 +76,6 @@
                          get_string('fields','data'));
             $row[] = new tabobject('presets', $CFG->wwwroot.'/mod/data/preset.php?d='.$data->id,
                          get_string('presets', 'data'));
-            $row[] = new tabobject('export', $CFG->wwwroot.'/mod/data/export.php?d='.$data->id,
-                         get_string('export', 'data'));
         }
     }