コード例 #1
0
ファイル: lib.php プロジェクト: Br3nda/mahara
 /**
  * Gets a list of files in one folder
  *
  * @param integer $parentfolderid    Artefact id of the folder
  * @param integer $userid            Id of the owner, if the owner is a user
  * @param integer $group             Id of the owner, if the owner is a group
  * @param string  $institution       Id of the owner, if the owner is a institution
  * @param array   $filters           Filters to apply to the results. An array with keys 'artefacttype', 'filetype',
  *                                   where array values are arrays of artefacttype or mimetype strings.
  * @return array  A list of artefacts
  */
 public static function get_my_files_data($parentfolderid, $userid, $group = null, $institution = null, $filters = null)
 {
     global $USER;
     $select = '
         SELECT
             a.id, a.artefacttype, a.mtime, f.size, a.title, a.description,
             COUNT(c.id) AS childcount, COUNT (aa.artefact) AS attachcount';
     $from = '
         FROM {artefact} a
             LEFT OUTER JOIN {artefact_file_files} f ON f.artefact = a.id
             LEFT OUTER JOIN {artefact} c ON c.parent = a.id 
             LEFT OUTER JOIN {artefact_attachment} aa ON aa.attachment = a.id';
     if (!empty($filters['artefacttype'])) {
         $artefacttypes = $filters['artefacttype'];
         $artefacttypes[] = 'folder';
     } else {
         $artefacttypes = array_diff(PluginArtefactFile::get_artefact_types(), array('profileicon'));
     }
     $where = "\n            WHERE a.artefacttype IN (" . join(',', array_map('db_quote', $artefacttypes)) . ")";
     if (!empty($filters['filetype']) && is_array($filters['filetype'])) {
         $where .= "\n            AND (a.artefacttype = 'folder' OR f.filetype IN (" . join(',', array_map('db_quote', $filters['filetype'])) . '))';
     }
     $groupby = '
         GROUP BY
             a.id, a.artefacttype, a.mtime, f.size, a.title, a.description';
     $phvals = array();
     if ($institution) {
         if ($institution == 'mahara' && !$USER->get('admin')) {
             // If non-admins are browsing site files, only let them see the public folder & its contents
             $publicfolder = ArtefactTypeFolder::admin_public_folder_id();
             $from .= '
             LEFT OUTER JOIN {artefact_parent_cache} pub ON (a.id = pub.artefact AND pub.parent = ?)';
             $where .= '
             AND (pub.parent = ? OR a.id = ?)';
             $phvals = array($publicfolder, $publicfolder, $publicfolder);
         }
         $where .= '
         AND a.institution = ? AND a.owner IS NULL';
         $phvals[] = $institution;
     } else {
         if ($group) {
             $select .= ',
             r.can_edit, r.can_view, r.can_republish';
             $from .= '
             LEFT OUTER JOIN (
                 SELECT ar.artefact, ar.can_edit, ar.can_view, ar.can_republish
                 FROM {artefact_access_role} ar
                 INNER JOIN {group_member} gm ON ar.role = gm.role
                 WHERE gm.group = ? AND gm.member = ? 
             ) r ON r.artefact = a.id';
             $phvals[] = $group;
             $phvals[] = $USER->get('id');
             $where .= '
         AND a.group = ? AND a.owner IS NULL AND r.can_view = 1';
             $phvals[] = $group;
             $groupby .= ', r.can_edit, r.can_view, r.can_republish';
         } else {
             $where .= '
         AND a.institution IS NULL AND a.owner = ?';
             $phvals[] = $userid;
         }
     }
     if ($parentfolderid) {
         $where .= '
         AND a.parent = ? ';
         $phvals[] = $parentfolderid;
     } else {
         $where .= '
         AND a.parent IS NULL';
     }
     $filedata = get_records_sql_assoc($select . $from . $where . $groupby, $phvals);
     if (!$filedata) {
         $filedata = array();
     } else {
         foreach ($filedata as $item) {
             $item->mtime = format_date(strtotime($item->mtime), 'strfdaymonthyearshort');
             $item->tags = array();
             $item->icon = call_static_method(generate_artefact_class_name($item->artefacttype), 'get_icon', array('id' => $item->id));
             if ($item->size) {
                 // Doing this here now for non-js users
                 $item->size = ArtefactTypeFile::short_size($item->size, true);
             }
         }
         $where = 'artefact IN (' . join(',', array_keys($filedata)) . ')';
         $tags = get_records_select_array('artefact_tag', $where);
         if ($tags) {
             foreach ($tags as $t) {
                 $filedata[$t->artefact]->tags[] = $t->tag;
             }
         }
         if ($group) {
             // Fetch permissions for each artefact
             $perms = get_records_select_array('artefact_access_role', $where);
             if ($perms) {
                 foreach ($perms as $perm) {
                     $filedata[$perm->artefact]->permissions[$perm->role] = array('view' => $perm->can_view, 'edit' => $perm->can_edit, 'republish' => $perm->can_republish);
                 }
             }
         }
     }
     // Add parent folder to the list
     if (!empty($parentfolderid)) {
         $grandparentid = (int) get_field('artefact', 'parent', 'id', $parentfolderid);
         $filedata[$grandparentid] = (object) array('title' => get_string('parentfolder', 'artefact.file'), 'artefacttype' => 'folder', 'description' => get_string('parentfolder', 'artefact.file'), 'isparent' => true, 'id' => $grandparentid, 'icon' => ArtefactTypeFolder::get_icon());
     }
     uasort($filedata, array("ArtefactTypeFileBase", "my_files_cmp"));
     return $filedata;
 }
コード例 #2
0
ファイル: lib.php プロジェクト: vohung96/mahara
 /**
  * Gets a list of files in one folder
  *
  * @param integer $parentfolderid    Artefact id of the folder
  * @param integer $userid            Id of the owner, if the owner is a user
  * @param integer $group             Id of the owner, if the owner is a group
  * @param string  $institution       Id of the owner, if the owner is a institution
  * @param array   $filters           Filters to apply to the results. An array with keys 'artefacttype', 'filetype',
  *                                   where array values are arrays of artefacttype or mimetype strings.
  * @return array  A list of artefacts
  */
 public static function get_my_files_data($parentfolderid, $userid, $group = null, $institution = null, $filters = null)
 {
     global $USER;
     $select = '
         SELECT
             a.id, a.artefacttype, a.mtime, f.size, a.title, a.description, a.license, a.licensor, a.licensorurl, a.locked, a.allowcomments, u.profileicon AS defaultprofileicon,
             COUNT(DISTINCT c.id) AS childcount, COUNT (DISTINCT aa.artefact) AS attachcount, COUNT(DISTINCT va.view) AS viewcount, COUNT(DISTINCT s.id) AS skincount,
             COUNT(DISTINCT api.id) AS profileiconcount';
     $from = '
         FROM {artefact} a
             LEFT OUTER JOIN {artefact_file_files} f ON f.artefact = a.id
             LEFT OUTER JOIN {artefact} c ON c.parent = a.id
             LEFT OUTER JOIN {artefact} api ON api.parent = a.id AND api.artefacttype = \'profileicon\'
             LEFT OUTER JOIN {view_artefact} va ON va.artefact = a.id
             LEFT OUTER JOIN {artefact_attachment} aa ON aa.attachment = a.id
             LEFT OUTER JOIN {skin} s ON (s.bodybgimg = a.id OR s.viewbgimg = a.id)
             LEFT OUTER JOIN {usr} u ON a.id = u.profileicon AND a.owner = u.id';
     if (!empty($filters['artefacttype'])) {
         $artefacttypes = $filters['artefacttype'];
         $artefacttypes[] = 'folder';
     } else {
         $artefacttypes = PluginArtefactFile::get_artefact_types();
     }
     $where = "\n            WHERE a.artefacttype IN (" . join(',', array_map('db_quote', $artefacttypes)) . ")";
     if (!empty($filters['filetype']) && is_array($filters['filetype'])) {
         $where .= "\n            AND (a.artefacttype = 'folder' OR f.filetype IN (" . join(',', array_map('db_quote', $filters['filetype'])) . '))';
     }
     $groupby = '
         GROUP BY
             a.id, a.artefacttype, a.mtime, f.size, a.title, a.description, a.license, a.licensor, a.licensorurl, a.locked, a.allowcomments,
             u.profileicon';
     $phvals = array();
     if ($institution) {
         if ($institution == 'mahara' && !$USER->get('admin')) {
             // If non-admins are browsing site files, only let them see the public folder & its contents
             $publicfolder = ArtefactTypeFolder::admin_public_folder_id();
             $where .= '
             AND (a.path = ? OR a.path LIKE ?)';
             $phvals = array("/{$publicfolder}", "/{$publicfolder}/%");
         } else {
             $from .= '
                 LEFT OUTER JOIN {usr_institution} ui ON ui.institution = a.institution';
             $where .= ' AND a.institution = ? ';
             $phvals[] = $institution;
             // Check if user is an admin in this institution.
             if (!$USER->get('admin')) {
                 $where .= ' AND ui.admin = 1 AND ui.usr = ? ';
                 $phvals[] = $USER->get('id');
             }
         }
     } else {
         if ($group) {
             $select .= ',
             r.can_edit, r.can_view, r.can_republish, a.author';
             $from .= '
             LEFT OUTER JOIN (
                 SELECT ar.artefact, ar.can_edit, ar.can_view, ar.can_republish
                 FROM {artefact_access_role} ar
                 INNER JOIN {group_member} gm ON ar.role = gm.role
                 WHERE gm.group = ? AND gm.member = ?
             ) r ON r.artefact = a.id';
             $phvals[] = $group;
             $phvals[] = $USER->get('id');
             $where .= '
         AND a.group = ? AND (r.can_view = 1 OR a.author = ?)';
             $phvals[] = $group;
             $phvals[] = $USER->get('id');
             $groupby .= ', r.can_edit, r.can_view, r.can_republish, a.author';
         } else {
             $where .= '
         AND a.institution IS NULL AND a.owner = ?';
             $phvals[] = $userid;
         }
     }
     if ($parentfolderid) {
         $where .= '
         AND a.parent = ? ';
         $phvals[] = $parentfolderid;
         $parent = artefact_instance_from_id($parentfolderid);
         $can_view_parent = $USER->can_view_artefact($parent);
         if (!$can_view_parent) {
             return null;
         }
         $can_edit_parent = $USER->can_edit_artefact($parent);
     } else {
         $where .= '
         AND a.parent IS NULL';
         $can_edit_parent = true;
         $can_view_parent = true;
     }
     $filedata = get_records_sql_assoc($select . $from . $where . $groupby, $phvals);
     if (!$filedata) {
         $filedata = array();
     } else {
         foreach ($filedata as $item) {
             $item->mtime = format_date(strtotime($item->mtime), 'strfdaymonthyearshort');
             $item->tags = array();
             $item->allowcomments = (bool) $item->allowcomments;
             $item->icon = call_static_method(generate_artefact_class_name($item->artefacttype), 'get_icon', array('id' => $item->id));
             if ($item->size) {
                 // Doing this here now for non-js users
                 $item->size = ArtefactTypeFile::short_size($item->size, true);
             }
             if ($group) {
                 // site public files
                 if ($institution == 'mahara' && ArtefactTypeFolder::admin_public_folder_id() == $parentfolderid) {
                     $item->can_edit = 0;
                     $item->can_view = 1;
                     $item->can_republish = 1;
                 } else {
                     if (!empty($item->author) && $item->author == $USER->get('id')) {
                         $item->can_edit = 1;
                         $item->can_view = 1;
                         $item->can_republish = 1;
                     } else {
                         $item->can_edit = $can_edit_parent && $item->can_edit;
                         $item->can_view = $can_view_parent && $item->can_view;
                         $item->can_republish = $can_view_parent && $item->can_republish;
                     }
                 }
             }
             if (!empty($item->author)) {
                 if ($group && $item->author == $USER->get('id')) {
                     $item->can_edit = 1;
                     // This will show the delete, edit buttons in filelist, but doesn't change the actual permissions in the checkbox
                 }
             }
         }
         $where = 'artefact IN (' . join(',', array_keys($filedata)) . ')';
         $tags = get_records_select_array('artefact_tag', $where);
         if ($tags) {
             foreach ($tags as $t) {
                 $filedata[$t->artefact]->tags[] = $t->tag;
             }
         }
         if ($group) {
             // Fetch permissions for each artefact
             $perms = get_records_select_array('artefact_access_role', $where);
             if ($perms) {
                 foreach ($perms as $perm) {
                     $filedata[$perm->artefact]->permissions[$perm->role] = array('view' => $perm->can_view, 'edit' => $perm->can_edit, 'republish' => $perm->can_republish);
                 }
             }
         }
     }
     // Add parent folder to the list
     if (!empty($parentfolderid)) {
         $grandparentid = (int) get_field('artefact', 'parent', 'id', $parentfolderid);
         $filedata[$grandparentid] = (object) array('title' => get_string('parentfolder', 'artefact.file'), 'artefacttype' => 'folder', 'description' => get_string('parentfolder', 'artefact.file'), 'isparent' => true, 'id' => $grandparentid, 'icon' => ArtefactTypeFolder::get_icon());
     }
     uasort($filedata, array("ArtefactTypeFileBase", "my_files_cmp"));
     return $filedata;
 }